open-nomad/nomad/drainer_shims.go

57 lines
1.8 KiB
Go
Raw Normal View History

2018-02-27 00:28:10 +00:00
package nomad
import (
"time"
"github.com/hashicorp/nomad/nomad/structs"
)
2018-02-27 00:28:10 +00:00
// drainerShim implements the drainer.RaftApplier interface required by the
// NodeDrainer.
type drainerShim struct {
s *Server
}
2018-05-11 00:22:06 +00:00
func (d drainerShim) NodesDrainComplete(nodes []string, event *structs.NodeEvent) (uint64, error) {
2018-03-09 22:15:21 +00:00
args := &structs.BatchNodeUpdateDrainRequest{
Updates: make(map[string]*structs.DrainUpdate, len(nodes)),
2018-05-11 00:22:06 +00:00
NodeEvents: make(map[string]*structs.NodeEvent, len(nodes)),
2018-02-27 00:28:10 +00:00
WriteRequest: structs.WriteRequest{Region: d.s.config.Region},
UpdatedAt: time.Now().Unix(),
2018-02-27 00:28:10 +00:00
}
2018-03-09 22:15:21 +00:00
update := &structs.DrainUpdate{}
for _, node := range nodes {
args.Updates[node] = update
2018-05-11 00:22:06 +00:00
if event != nil {
args.NodeEvents[node] = event
}
2018-03-09 22:15:21 +00:00
}
resp, index, err := d.s.raftApply(structs.BatchNodeUpdateDrainRequestType, args)
2018-03-06 22:37:37 +00:00
return d.convertApplyErrors(resp, index, err)
2018-02-27 00:28:10 +00:00
}
2018-03-06 22:37:37 +00:00
func (d drainerShim) AllocUpdateDesiredTransition(allocs map[string]*structs.DesiredTransition, evals []*structs.Evaluation) (uint64, error) {
2018-02-27 00:28:10 +00:00
args := &structs.AllocUpdateDesiredTransitionRequest{
Allocs: allocs,
Evals: evals,
WriteRequest: structs.WriteRequest{Region: d.s.config.Region},
}
2018-03-06 22:37:37 +00:00
resp, index, err := d.s.raftApply(structs.AllocUpdateDesiredTransitionRequestType, args)
return d.convertApplyErrors(resp, index, err)
}
// convertApplyErrors parses the results of a raftApply and returns the index at
// which it was applied and any error that occurred. Raft Apply returns two
// separate errors, Raft library errors and user returned errors from the FSM.
// This helper, joins the errors by inspecting the applyResponse for an error.
2018-03-06 22:37:37 +00:00
func (d drainerShim) convertApplyErrors(applyResp interface{}, index uint64, err error) (uint64, error) {
if applyResp != nil {
if fsmErr, ok := applyResp.(error); ok && fsmErr != nil {
2018-03-06 22:37:37 +00:00
return index, fsmErr
}
}
2018-03-06 22:37:37 +00:00
return index, err
2018-02-27 00:28:10 +00:00
}