//AttemptsForActivity returns the number of times a given activity has been attempted.
//It will return 0 if the activity has never failed, has been canceled, or has been completed successfully
170497
func (a *EventCorrelator) AttemptsForActivity(info *ActivityInfo) int {
	a.checkInit()
	if info == nil || info.ActivityId == "" {
		return 0
	}
	return a.ActivityAttempts[info.ActivityId]
}
121926
func AttemptRangeQuery(quest string, low, high uint32) *GraphQuery {
	return &GraphQuery{
		AttemptRange: []*GraphQuery_AttemptRange{{Quest: quest, Low: low, High: high}}}
}
378953
func (act *Activity) Following() *FollowingActivity {
	insta := act.inst
	nact := &FollowingActivity{inst: insta}
	return nact
}
863727
func (s *RedisDeviceStore) CountForApp(appID string) (int, error) {
	opts := new(storage.ListOptions)
	opts.UseCache(s.listResultCacheKey(appID), listCacheTTL)
	return s.store.Count(fmt.Sprintf("%s:*", appID), opts)
}
1156277
func (s *Snapshot) Intentions() (structs.Intentions, error) {
	ixns, err := s.tx.Get(intentionsTableName, "id")
	if err != nil {
		return nil, err
	}

	var ret structs.Intentions
	for wrapped := ixns.Next(); wrapped != nil; wrapped = ixns.Next() {
		ret = append(ret, wrapped.(*structs.Intention))
	}

	return ret, nil
}
800867
func (s *ScheduleExecution) RollForward(c context.Context) (muts []tumble.Mutation, err error) {
	a := model.AttemptFromID(s.For)
	if err = ds.Get(c, a); err != nil {
		logging.WithError(err).Errorf(c, "loading attempt")
		return
	}

	if a.State != dm.Attempt_SCHEDULING {
		logging.Infof(c, "EARLY EXIT: already scheduling")
		return
	}

	q := model.QuestFromID(s.For.Quest)
	if err = ds.Get(ds.WithoutTransaction(c), q); err != nil {
		logging.WithError(err).Errorf(c, "loading quest")
		return
	}

	prevResult := (*dm.JsonResult)(nil)
	if a.LastSuccessfulExecution != 0 {
		prevExecution := model.ExecutionFromID(c, s.For.Execution(a.LastSuccessfulExecution))
		if err = ds.Get(c, prevExecution); err != nil {
			logging.Errorf(c, "loading previous execution: %s", err)
			return
		}
		prevResult = prevExecution.Result.Data
	}

	reg := distributor.GetRegistry(c)
	dist, ver, err := reg.MakeDistributor(c, q.Desc.DistributorConfigName)
	if err != nil {
		logging.WithError(err).Errorf(c, "making distributor %s", q.Desc.DistributorConfigName)
		return
	}

	a.CurExecution++
	if err = a.ModifyState(c, dm.Attempt_EXECUTING); err != nil {
		logging.WithError(err).Errorf(c, "modifying state")
		return
	}

	eid := dm.NewExecutionID(s.For.Quest, s.For.Id, a.CurExecution)
	e := model.MakeExecution(c, eid, q.Desc.DistributorConfigName, ver)
	e.TimeToStart = google.DurationFromProto(q.Desc.Meta.Timeouts.Start)
	e.TimeToRun = google.DurationFromProto(q.Desc.Meta.Timeouts.Run)

	exAuth := &dm.Execution_Auth{Id: eid, Token: e.Token}

	var distTok distributor.Token
	distTok, e.TimeToStop, err = dist.Run(&q.Desc, exAuth, prevResult)
	if e.TimeToStop <= 0 {
		e.TimeToStop = google.DurationFromProto(q.Desc.Meta.Timeouts.Stop)
	}
	e.DistributorToken = string(distTok)
	if err != nil {
		if transient.Tag.In(err) {
			// tumble will retry us later
			logging.WithError(err).Errorf(c, "got transient error in ScheduleExecution")
			return
		}
		logging.WithError(err).Errorf(c, "got non-transient error in ScheduleExecution")
		origErr := err

		// put a and e to the transaction buffer, so that
		// FinishExecution.RollForward can see them.
		if err = ds.Put(c, a, e); err != nil {
			logging.WithError(err).Errorf(c, "putting attempt+execution for non-transient distributor error")
			return
		}
		return NewFinishExecutionAbnormal(
			eid, dm.AbnormalFinish_REJECTED,
			fmt.Sprintf("rejected during scheduling with non-transient error: %s", origErr),
		).RollForward(c)
	}

	if err = ResetExecutionTimeout(c, e); err != nil {
		logging.WithError(err).Errorf(c, "resetting timeout")
		return
	}

	if err = ds.Put(c, a, e); err != nil {
		logging.WithError(err).Errorf(c, "putting attempt+execution")
	}

	return
}
1084287
func (e *FwdEdge) Fwd(c context.Context) (*Attempt, *FwdDep) {
	atmpt := &Attempt{ID: *e.From}
	return atmpt, &FwdDep{
		Depender: ds.KeyForObj(c, atmpt),
		Dependee: *e.To,
	}
}
1276098
func (s *HistoryEvent) SetActivityTaskCancelRequestedEventAttributes(v *ActivityTaskCancelRequestedEventAttributes) *HistoryEvent {
	s.ActivityTaskCancelRequestedEventAttributes = v
	return s
}
261777
func (c *Client) DeprecateActivityType(request DeprecateActivityType) error {
	err := c.swfReqNoResponse("DeprecateActivityType", request)
	return err
}
532224
func (s *HistoryEvent) SetActivityTaskFailedEventAttributes(v *ActivityTaskFailedEventAttributes) *HistoryEvent {
	s.ActivityTaskFailedEventAttributes = v
	return s
}
39528
func (c *Client) RespondActivityTaskCompleted(request RespondActivityTaskCompletedRequest) error {
	err := c.swfReqNoResponse("RespondActivityTaskCompleted", request)
	return err
}
437329
func (a *EventCorrelator) AttemptsForCancellation(info *CancellationInfo) int {
	a.checkInit()
	if info == nil || info.WorkflowId == "" {
		return 0
	}
	return a.CancelationAttempts[info.WorkflowId]
}
1000135
func (r *RateLimiter) Try() (ok bool, remaining time.Duration) {
	r.mtx.Lock()
	defer r.mtx.Unlock()
	now := time.Now()
	if l := r.times.Len(); l < r.limit {
		r.times.PushBack(now)
		return true, 0
	}
	frnt := r.times.Front()
	if diff := now.Sub(frnt.Value.(time.Time)); diff < r.interval {
		return false, r.interval - diff
	}
	frnt.Value = now
	r.times.MoveToBack(frnt)
	return true, 0
}
879256
func (f *FailureCounter) Failed() bool {
	inLock(&f.countLock, func() { f.count++ })
	return f.count >= f.failedThreshold || f.failed
}
1213911
func OnActivityFailed(activityName string, deciders ...Decider) Decider {
	return OnActivityEvents(activityName, []string{
		swf.EventTypeActivityTaskFailed,
		swf.EventTypeScheduleActivityTaskFailed,
	}, deciders...)
}
1272462
func (t *trackingConn) UpdateClientActivity() {
	t.Lock()
	defer t.Unlock()
	t.lastActive = t.clock.Now().UTC()
}
860477
func (b *Batch) Attempts() int {
	b.metrics.l.Lock()
	defer b.metrics.l.Unlock()

	var attempts int
	for _, metric := range b.metrics.m {
		attempts += metric.Attempts
	}
	return attempts
}