github.com/zoomfoo/nomad@v0.8.5-0.20180907175415-f28fd3a1a056/nomad/drainer_shims.go (about)

     1  package nomad
     2  
     3  import "github.com/hashicorp/nomad/nomad/structs"
     4  
     5  // drainerShim implements the drainer.RaftApplier interface required by the
     6  // NodeDrainer.
     7  type drainerShim struct {
     8  	s *Server
     9  }
    10  
    11  func (d drainerShim) NodesDrainComplete(nodes []string, event *structs.NodeEvent) (uint64, error) {
    12  	args := &structs.BatchNodeUpdateDrainRequest{
    13  		Updates:      make(map[string]*structs.DrainUpdate, len(nodes)),
    14  		NodeEvents:   make(map[string]*structs.NodeEvent, len(nodes)),
    15  		WriteRequest: structs.WriteRequest{Region: d.s.config.Region},
    16  	}
    17  
    18  	update := &structs.DrainUpdate{}
    19  	for _, node := range nodes {
    20  		args.Updates[node] = update
    21  		if event != nil {
    22  			args.NodeEvents[node] = event
    23  		}
    24  	}
    25  
    26  	resp, index, err := d.s.raftApply(structs.BatchNodeUpdateDrainRequestType, args)
    27  	return d.convertApplyErrors(resp, index, err)
    28  }
    29  
    30  func (d drainerShim) AllocUpdateDesiredTransition(allocs map[string]*structs.DesiredTransition, evals []*structs.Evaluation) (uint64, error) {
    31  	args := &structs.AllocUpdateDesiredTransitionRequest{
    32  		Allocs:       allocs,
    33  		Evals:        evals,
    34  		WriteRequest: structs.WriteRequest{Region: d.s.config.Region},
    35  	}
    36  	resp, index, err := d.s.raftApply(structs.AllocUpdateDesiredTransitionRequestType, args)
    37  	return d.convertApplyErrors(resp, index, err)
    38  }
    39  
    40  // convertApplyErrors parses the results of a raftApply and returns the index at
    41  // which it was applied and any error that occurred. Raft Apply returns two
    42  // separate errors, Raft library errors and user returned errors from the FSM.
    43  // This helper, joins the errors by inspecting the applyResponse for an error.
    44  func (d drainerShim) convertApplyErrors(applyResp interface{}, index uint64, err error) (uint64, error) {
    45  	if applyResp != nil {
    46  		if fsmErr, ok := applyResp.(error); ok && fsmErr != nil {
    47  			return index, fsmErr
    48  		}
    49  	}
    50  	return index, err
    51  }