github.com/ranjib/nomad@v0.1.1-0.20160225204057-97751b02f70b/scheduler/context.go (about)

     1  package scheduler
     2  
     3  import (
     4  	"fmt"
     5  	"log"
     6  	"regexp"
     7  
     8  	"github.com/hashicorp/go-version"
     9  	"github.com/hashicorp/nomad/nomad/structs"
    10  )
    11  
    12  // Context is used to track contextual information used for placement
    13  type Context interface {
    14  	// State is used to inspect the current global state
    15  	State() State
    16  
    17  	// Plan returns the current plan
    18  	Plan() *structs.Plan
    19  
    20  	// Logger provides a way to log
    21  	Logger() *log.Logger
    22  
    23  	// Metrics returns the current metrics
    24  	Metrics() *structs.AllocMetric
    25  
    26  	// Reset is invoked after making a placement
    27  	Reset()
    28  
    29  	// ProposedAllocs returns the proposed allocations for a node
    30  	// which is the existing allocations, removing evictions, and
    31  	// adding any planned placements.
    32  	ProposedAllocs(nodeID string) ([]*structs.Allocation, error)
    33  
    34  	// RegexpCache is a cache of regular expressions
    35  	RegexpCache() map[string]*regexp.Regexp
    36  
    37  	// ConstraintCache is a cache of version constraints
    38  	ConstraintCache() map[string]version.Constraints
    39  
    40  	// Eligibility returns a tracker for node eligibility in the context of the
    41  	// eval.
    42  	Eligibility() *EvalEligibility
    43  }
    44  
    45  // EvalCache is used to cache certain things during an evaluation
    46  type EvalCache struct {
    47  	reCache         map[string]*regexp.Regexp
    48  	constraintCache map[string]version.Constraints
    49  }
    50  
    51  func (e *EvalCache) RegexpCache() map[string]*regexp.Regexp {
    52  	if e.reCache == nil {
    53  		e.reCache = make(map[string]*regexp.Regexp)
    54  	}
    55  	return e.reCache
    56  }
    57  func (e *EvalCache) ConstraintCache() map[string]version.Constraints {
    58  	if e.constraintCache == nil {
    59  		e.constraintCache = make(map[string]version.Constraints)
    60  	}
    61  	return e.constraintCache
    62  }
    63  
    64  // EvalContext is a Context used during an Evaluation
    65  type EvalContext struct {
    66  	EvalCache
    67  	state       State
    68  	plan        *structs.Plan
    69  	logger      *log.Logger
    70  	metrics     *structs.AllocMetric
    71  	eligibility *EvalEligibility
    72  }
    73  
    74  // NewEvalContext constructs a new EvalContext
    75  func NewEvalContext(s State, p *structs.Plan, log *log.Logger) *EvalContext {
    76  	ctx := &EvalContext{
    77  		state:   s,
    78  		plan:    p,
    79  		logger:  log,
    80  		metrics: new(structs.AllocMetric),
    81  	}
    82  	return ctx
    83  }
    84  
    85  func (e *EvalContext) State() State {
    86  	return e.state
    87  }
    88  
    89  func (e *EvalContext) Plan() *structs.Plan {
    90  	return e.plan
    91  }
    92  
    93  func (e *EvalContext) Logger() *log.Logger {
    94  	return e.logger
    95  }
    96  
    97  func (e *EvalContext) Metrics() *structs.AllocMetric {
    98  	return e.metrics
    99  }
   100  
   101  func (e *EvalContext) SetState(s State) {
   102  	e.state = s
   103  }
   104  
   105  func (e *EvalContext) Reset() {
   106  	e.metrics = new(structs.AllocMetric)
   107  }
   108  
   109  func (e *EvalContext) ProposedAllocs(nodeID string) ([]*structs.Allocation, error) {
   110  	// Get the existing allocations that are non-terminal
   111  	existingAlloc, err := e.state.AllocsByNodeTerminal(nodeID, false)
   112  	if err != nil {
   113  		return nil, err
   114  	}
   115  
   116  	// Determine the proposed allocation by first removing allocations
   117  	// that are planned evictions and adding the new allocations.
   118  	proposed := existingAlloc
   119  	if update := e.plan.NodeUpdate[nodeID]; len(update) > 0 {
   120  		proposed = structs.RemoveAllocs(existingAlloc, update)
   121  	}
   122  	proposed = append(proposed, e.plan.NodeAllocation[nodeID]...)
   123  
   124  	// Ensure the return is not nil
   125  	if proposed == nil {
   126  		proposed = make([]*structs.Allocation, 0)
   127  	}
   128  	return proposed, nil
   129  }
   130  
   131  func (e *EvalContext) Eligibility() *EvalEligibility {
   132  	if e.eligibility == nil {
   133  		e.eligibility = NewEvalEligibility()
   134  	}
   135  
   136  	return e.eligibility
   137  }
   138  
   139  type ComputedClassFeasibility byte
   140  
   141  const (
   142  	// EvalComputedClassUnknown is the initial state until the eligibility has
   143  	// been explicitely marked to eligible/ineligible or escaped.
   144  	EvalComputedClassUnknown ComputedClassFeasibility = iota
   145  
   146  	// EvalComputedClassIneligible is used to mark the computed class as
   147  	// ineligible for the evaluation.
   148  	EvalComputedClassIneligible
   149  
   150  	// EvalComputedClassIneligible is used to mark the computed class as
   151  	// eligible for the evaluation.
   152  	EvalComputedClassEligible
   153  
   154  	// EvalComputedClassEscaped signals that computed class can not determine
   155  	// eligibility because a constraint exists that is not captured by computed
   156  	// node classes.
   157  	EvalComputedClassEscaped
   158  )
   159  
   160  // EvalEligibility tracks eligibility of nodes by computed node class over the
   161  // course of an evaluation.
   162  type EvalEligibility struct {
   163  	// job tracks the eligibility at the job level per computed node class.
   164  	job map[string]ComputedClassFeasibility
   165  
   166  	// jobEscaped marks whether constraints have escaped at the job level.
   167  	jobEscaped bool
   168  
   169  	// taskGroups tracks the eligibility at the task group level per computed
   170  	// node class.
   171  	taskGroups map[string]map[string]ComputedClassFeasibility
   172  
   173  	// tgEscapedConstraints is a map of task groups to whether constraints have
   174  	// escaped.
   175  	tgEscapedConstraints map[string]bool
   176  }
   177  
   178  // NewEvalEligibility returns an eligibility tracker for the context of an evaluation.
   179  func NewEvalEligibility() *EvalEligibility {
   180  	return &EvalEligibility{
   181  		job:                  make(map[string]ComputedClassFeasibility),
   182  		taskGroups:           make(map[string]map[string]ComputedClassFeasibility),
   183  		tgEscapedConstraints: make(map[string]bool),
   184  	}
   185  }
   186  
   187  // SetJob takes the job being evaluated and calculates the escaped constraints
   188  // at the job and task group level.
   189  func (e *EvalEligibility) SetJob(job *structs.Job) {
   190  	// Determine whether the job has escaped constraints.
   191  	e.jobEscaped = len(structs.EscapedConstraints(job.Constraints)) != 0
   192  
   193  	// Determine the escaped constraints per task group.
   194  	for _, tg := range job.TaskGroups {
   195  		constraints := tg.Constraints
   196  		for _, task := range tg.Tasks {
   197  			constraints = append(constraints, task.Constraints...)
   198  		}
   199  
   200  		e.tgEscapedConstraints[tg.Name] = len(structs.EscapedConstraints(constraints)) != 0
   201  	}
   202  }
   203  
   204  // HasEscaped returns whether any of the constraints in the passed job have
   205  // escaped computed node classes.
   206  func (e *EvalEligibility) HasEscaped() bool {
   207  	if e.jobEscaped {
   208  		return true
   209  	}
   210  
   211  	for _, escaped := range e.tgEscapedConstraints {
   212  		if escaped {
   213  			return true
   214  		}
   215  	}
   216  
   217  	return false
   218  }
   219  
   220  // GetClasses returns the tracked classes to their eligibility, across the job
   221  // and task groups.
   222  func (e *EvalEligibility) GetClasses() map[string]bool {
   223  	elig := make(map[string]bool)
   224  
   225  	// Go through the job.
   226  	for class, feas := range e.job {
   227  		switch feas {
   228  		case EvalComputedClassEligible:
   229  			elig[class] = true
   230  		case EvalComputedClassIneligible:
   231  			elig[class] = false
   232  		}
   233  	}
   234  
   235  	// Go through the task groups.
   236  	for _, classes := range e.taskGroups {
   237  		for class, feas := range classes {
   238  			switch feas {
   239  			case EvalComputedClassEligible:
   240  				elig[class] = true
   241  			case EvalComputedClassIneligible:
   242  				// Only mark as ineligible if it hasn't been marked before. This
   243  				// prevents one task group marking a class as ineligible when it
   244  				// is eligible on another task group.
   245  				if _, ok := elig[class]; !ok {
   246  					elig[class] = false
   247  				}
   248  			}
   249  		}
   250  	}
   251  
   252  	return elig
   253  }
   254  
   255  // JobStatus returns the eligibility status of the job.
   256  func (e *EvalEligibility) JobStatus(class string) ComputedClassFeasibility {
   257  	// COMPAT: Computed node class was introduced in 0.3. Clients running < 0.3
   258  	// will not have a computed class. The safest value to return is the escaped
   259  	// case, since it disables any optimization.
   260  	if e.jobEscaped || class == "" {
   261  		fmt.Println(e.jobEscaped, class)
   262  		return EvalComputedClassEscaped
   263  	}
   264  
   265  	if status, ok := e.job[class]; ok {
   266  		return status
   267  	}
   268  	return EvalComputedClassUnknown
   269  }
   270  
   271  // SetJobEligibility sets the eligibility status of the job for the computed
   272  // node class.
   273  func (e *EvalEligibility) SetJobEligibility(eligible bool, class string) {
   274  	if eligible {
   275  		e.job[class] = EvalComputedClassEligible
   276  	} else {
   277  		e.job[class] = EvalComputedClassIneligible
   278  	}
   279  }
   280  
   281  // TaskGroupStatus returns the eligibility status of the task group.
   282  func (e *EvalEligibility) TaskGroupStatus(tg, class string) ComputedClassFeasibility {
   283  	// COMPAT: Computed node class was introduced in 0.3. Clients running < 0.3
   284  	// will not have a computed class. The safest value to return is the escaped
   285  	// case, since it disables any optimization.
   286  	if class == "" {
   287  		return EvalComputedClassEscaped
   288  	}
   289  
   290  	if escaped, ok := e.tgEscapedConstraints[tg]; ok {
   291  		if escaped {
   292  			return EvalComputedClassEscaped
   293  		}
   294  	}
   295  
   296  	if classes, ok := e.taskGroups[tg]; ok {
   297  		if status, ok := classes[class]; ok {
   298  			return status
   299  		}
   300  	}
   301  	return EvalComputedClassUnknown
   302  }
   303  
   304  // SetTaskGroupEligibility sets the eligibility status of the task group for the
   305  // computed node class.
   306  func (e *EvalEligibility) SetTaskGroupEligibility(eligible bool, tg, class string) {
   307  	var eligibility ComputedClassFeasibility
   308  	if eligible {
   309  		eligibility = EvalComputedClassEligible
   310  	} else {
   311  		eligibility = EvalComputedClassIneligible
   312  	}
   313  
   314  	if classes, ok := e.taskGroups[tg]; ok {
   315  		classes[class] = eligibility
   316  	} else {
   317  		e.taskGroups[tg] = map[string]ComputedClassFeasibility{class: eligibility}
   318  	}
   319  }