-
Notifications
You must be signed in to change notification settings - Fork 0
/
scheduler.go
157 lines (126 loc) · 4.41 KB
/
scheduler.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package main
import (
"encoding/json"
"log"
"time"
proto "github.com/gogo/protobuf/proto"
mesos "github.com/mesos/mesos-go/mesosproto"
util "github.com/mesos/mesos-go/mesosutil"
sched "github.com/mesos/mesos-go/scheduler"
)
const (
offerTimeout = 1 * time.Minute
)
var (
filters = &mesos.Filters{RefuseSeconds: proto.Float64(5)}
)
type Scheduler struct {
executor *mesos.ExecutorInfo
tasksLaunched int
tasksFinished int
taskStatusesChans map[string]chan *mesos.TaskStatus
}
func NewScheduler() *Scheduler {
return &Scheduler{
executor: &mesos.ExecutorInfo{
ExecutorId: util.NewExecutorID(executorId),
Command: util.NewCommandInfo(executorCommand),
},
tasksLaunched: 0,
tasksFinished: 0,
taskStatusesChans: make(map[string]chan *mesos.TaskStatus),
}
}
func (s *Scheduler) Registered(driver sched.SchedulerDriver, frameworkId *mesos.FrameworkID, masterInfo *mesos.MasterInfo) {
log.Printf("Framework Registered with Master %v", masterInfo)
}
func (s *Scheduler) Reregistered(driver sched.SchedulerDriver, masterInfo *mesos.MasterInfo) {
log.Printf("Framework Re-Registered with Master %v", masterInfo)
}
func (s *Scheduler) Disconnected(sched.SchedulerDriver) {
log.Printf("Disconnected")
}
func (s *Scheduler) ResourceOffers(driver sched.SchedulerDriver, offers []*mesos.Offer) {
for _, offer := range offers {
log.Printf("Received offer %s", offer.Id.GetValue())
go s.handleOffer(offer)
}
}
func (s *Scheduler) handleOffer(offer *mesos.Offer) {
select {
case task := <-tasks:
log.Printf("Launching task %s for offer %s", task.Id, offer.Id.GetValue())
s.launchTaskWithOffer(task, offer)
default:
log.Printf("No tasks available; Declining offer %s", offer.Id.GetValue())
schedulerDriver.DeclineOffer(offer.Id, filters)
}
}
func (s *Scheduler) launchTaskWithOffer(task *Task, offer *mesos.Offer) {
mems := 0.0
cpus := 0.0
taskJsonBytes, err := json.Marshal(task)
cpuResources := util.FilterResources(offer.Resources, func(res *mesos.Resource) bool {
return res.GetName() == "cpus"
})
memResources := util.FilterResources(offer.Resources, func(res *mesos.Resource) bool {
return res.GetName() == "mem"
})
for _, res := range cpuResources {
cpus += res.GetScalar().GetValue()
}
for _, res := range memResources {
mems += res.GetScalar().GetValue()
}
if err != nil {
log.Printf("Declining offer %s for task %s due to marshal error: %v", offer.Id.GetValue(), task.Id, err)
schedulerDriver.DeclineOffer(offer.Id, filters)
return
}
if cpus < cpusPerTask || mems < memoryPerTask {
log.Printf("Declining offer %s for task %s due to insufficient offer resources", offer.Id.GetValue(), task.Id)
schedulerDriver.DeclineOffer(offer.Id, filters)
return
}
taskId := &mesos.TaskID{
Value: proto.String(task.Id),
}
taskInfo := &mesos.TaskInfo{
Name: proto.String(task.Cmd),
TaskId: taskId,
SlaveId: offer.SlaveId,
Data: taskJsonBytes,
Executor: s.executor,
Resources: []*mesos.Resource{
util.NewScalarResource("cpus", cpusPerTask),
util.NewScalarResource("mem", memoryPerTask),
},
}
s.taskStatusesChans[task.Id] = task.Build.TaskStatusesChan
schedulerDriver.LaunchTasks([]*mesos.OfferID{offer.Id}, []*mesos.TaskInfo{taskInfo}, filters)
}
func (s *Scheduler) StatusUpdate(driver sched.SchedulerDriver, status *mesos.TaskStatus) {
statusChan := s.taskStatusesChans[status.TaskId.GetValue()]
log.Printf("Status update: task %v is in state %s", status.TaskId.GetValue(), status.State.Enum().String())
go func() { statusChan <- status }()
if status.GetState() == mesos.TaskState_TASK_LOST ||
status.GetState() == mesos.TaskState_TASK_KILLED ||
status.GetState() == mesos.TaskState_TASK_FAILED {
log.Printf("Task %v is in unexpected state %s with message %s", status.TaskId.GetValue(), status.State.String(), status.GetMessage())
}
}
func (s *Scheduler) OfferRescinded(sched.SchedulerDriver, *mesos.OfferID) {
log.Printf("Offer rescinded")
}
func (s *Scheduler) FrameworkMessage(sched.SchedulerDriver, *mesos.ExecutorID, *mesos.SlaveID, string) {
log.Printf("Framework received message")
}
func (s *Scheduler) SlaveLost(sched.SchedulerDriver, *mesos.SlaveID) {
log.Printf("Slave lost")
}
func (s *Scheduler) ExecutorLost(sched.SchedulerDriver, *mesos.ExecutorID, *mesos.SlaveID, int) {
log.Printf("Executor lost")
}
func (s *Scheduler) Error(driver sched.SchedulerDriver, err string) {
log.Printf("Scheduler received error: %v", err)
}