// Balance releases tasks if this node has 120% more tasks than the average
// node in the cluster.
Balance releases tasks if this node has 120% more tasks than the average
node in the cluster.
// Balance releases tasks if this node has 120% more tasks than the average // node in the cluster .
func (e *FairBalancer) Balance() []string {
	nodetasks := e.bc.Tasks()
	e.delay = time.Time{}
	if len(nodetasks) < 2 {
		return nil
	}
	current, err := e.clusterstate.NodeTaskCount()
	if err != nil {
		Warnf("Error retrieving cluster state: %v", err)
		return nil
	}
	shouldrelease := current[e.nodeid] - e.desiredCount(current)
	if shouldrelease < 1 {
		return nil
	}
	releasetasks := make([]string, 0, shouldrelease)
	releaseset := make(map[string]struct{}, shouldrelease)
	random := rand.New(rand.NewSource(time.Now().UnixNano()))
	for len(releasetasks) < shouldrelease {
		tid := nodetasks[random.Intn(len(nodetasks))].Task().ID()
		if _, ok := releaseset[tid]; !ok {
			releasetasks = append(releasetasks, tid)
			releaseset[tid] = struct{}{}
		}
	}
	e.delay = time.Now().Add(time.Duration(len(releasetasks)) * time.Second)
	return releasetasks
}
func ( e * FairBalancer ) Balance ( ) [ ] string { nodetasks := e . bc . Tasks ( ) e . delay = time . Time { } if len ( nodetasks ) < 2 { return nil } current , err := e . clusterstate . NodeTaskCount ( ) if err != nil { Warnf ( " " , err ) return nil } shouldrelease := current [ e . nodeid ] - e . desiredCount ( current ) if shouldrelease < 1 { return nil } releasetasks := make ( [ ] string , 0 , shouldrelease ) releaseset := make ( map [ string ] struct { } , shouldrelease ) random := rand . New ( rand . NewSource ( time . Now ( ) . UnixNano ( ) ) ) for len ( releasetasks ) < shouldrelease { tid := nodetasks [ random . Intn ( len ( nodetasks ) ) ] . Task ( ) . ID ( ) if _ , ok := releaseset [ tid ] ; ! ok { releasetasks = append ( releasetasks , tid ) releaseset [ tid ] = struct { } { } } } e . delay = time . Now ( ) . Add ( time . Duration ( len ( releasetasks ) ) * time . Second ) return releasetasks }
Balance releases tasks if this node has 120% more tasks than the average
node in the cluster.
Balance releases tasks if this node has 120% more tasks than the average node in the cluster .
// Reset delay // If local tasks <= 1 this node should never rebalance