-
Notifications
You must be signed in to change notification settings - Fork 5
/
driver.go
332 lines (274 loc) · 8.21 KB
/
driver.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
package gomes
import (
"code.google.com/p/goprotobuf/proto"
"fmt"
mesos "github.com/vladimirvivien/gomes/mesosproto"
"log"
"os"
"os/user"
)
type MesosError string
func NewMesosError(msg string) MesosError {
return MesosError(msg)
}
func (err MesosError) Error() string {
return string(err)
}
type SchedulerDriver struct {
Master string
Scheduler *Scheduler
FrameworkInfo *mesos.FrameworkInfo
Status mesos.Status
masterClient *masterClient
schedMsgQ chan interface{}
controlQ chan mesos.Status
schedProc *schedulerProcess
connected bool
failover bool
}
func NewSchedDriver(scheduler *Scheduler, framework *mesos.FrameworkInfo, master string) (*SchedulerDriver, error) {
if master == "" {
return nil, fmt.Errorf("Missing master address.")
}
if framework == nil {
return nil, fmt.Errorf("Missing FrameworkInfo.")
}
// set default userid
if framework.GetUser() == "" {
user, err := user.Current()
if err != nil || user == nil {
framework.User = proto.String("unknown")
} else {
framework.User = proto.String(user.Username)
}
}
// set default hostname
if framework.GetHostname() == "" {
host, err := os.Hostname()
if err != nil || host == "" {
host = "unknown"
}
framework.Hostname = proto.String(host)
}
driver := &SchedulerDriver{
Master: master,
FrameworkInfo: framework,
Status: mesos.Status_DRIVER_NOT_STARTED,
schedMsgQ: make(chan interface{}, 10),
controlQ: make(chan mesos.Status),
connected: false,
failover: false,
}
driver.Scheduler = scheduler
proc, err := newSchedulerProcess(driver.schedMsgQ)
if err != nil {
return nil, err
}
driver.schedProc = proc
go setupSchedMsgQ(driver)
driver.masterClient = newMasterClient(master)
driver.Status = mesos.Status_DRIVER_NOT_STARTED
return driver, nil
}
func (driver *SchedulerDriver) Start() mesos.Status {
if driver.Status != mesos.Status_DRIVER_NOT_STARTED {
return driver.Status
}
// start sched proc and proc.server (http)
err := driver.schedProc.start()
if err != nil {
driver.Status = mesos.Status_DRIVER_ABORTED
driver.schedMsgQ <- err
return driver.Status
}
// register framework
err = driver.masterClient.RegisterFramework(driver.schedProc.processId, driver.FrameworkInfo)
if err != nil {
driver.Status = mesos.Status_DRIVER_ABORTED
driver.schedMsgQ <- NewMesosError("Failed to register the framework:" + err.Error())
} else {
driver.Status = mesos.Status_DRIVER_RUNNING
}
return driver.Status
}
func (driver *SchedulerDriver) Join() mesos.Status {
if driver.Status != mesos.Status_DRIVER_RUNNING {
return driver.Status
}
return <-driver.controlQ
}
func (driver *SchedulerDriver) Run() mesos.Status {
driver.Status = driver.Start()
if driver.Status != mesos.Status_DRIVER_RUNNING {
return driver.Status
}
return driver.Join()
}
func (driver *SchedulerDriver) Stop(failover bool) mesos.Status {
log.Printf("Stopping framework [%s]", driver.FrameworkInfo.GetId().GetValue())
if driver.Status != mesos.Status_DRIVER_RUNNING {
return driver.Status
}
err := driver.schedProc.stop()
if err != nil {
driver.schedMsgQ <- err
}
if driver.connected && !failover {
err = driver.masterClient.UnregisterFramework(driver.schedProc.processId, driver.FrameworkInfo.Id)
if err != nil {
driver.Status = mesos.Status_DRIVER_ABORTED //TODO confirm logic
driver.schedMsgQ <- NewMesosError("Failed to unregister the framework:" + err.Error())
} else {
driver.Status = mesos.Status_DRIVER_STOPPED
driver.connected = false // assume disconnection.
}
}
driver.controlQ <- driver.Status // signal
return driver.Status
}
func (driver *SchedulerDriver) Abort() mesos.Status {
log.Printf("Aborting framework [%s]", driver.FrameworkInfo.GetId().GetValue())
if driver.Status != mesos.Status_DRIVER_RUNNING {
return driver.Status
}
if !driver.connected {
log.Println("Not sending deactivate message, master is disconnected.")
} else {
err := driver.masterClient.DeactivateFramework(driver.schedProc.processId, driver.FrameworkInfo.Id)
if err != nil {
driver.schedMsgQ <- NewMesosError("Failed to abort the framework:" + err.Error())
} else {
driver.schedProc.aborted = true
driver.Status = mesos.Status_DRIVER_ABORTED
}
}
driver.controlQ <- driver.Status // signal
return driver.Status
}
func (driver *SchedulerDriver) KillTask(taskId *mesos.TaskID) mesos.Status {
if driver.Status != mesos.Status_DRIVER_RUNNING {
return driver.Status
}
if !driver.connected {
log.Println("Ignoring kill task message, master is disconnected")
} else {
err := driver.masterClient.KillTask(driver.schedProc.processId, taskId)
if err != nil {
log.Println("Unable to kill requested task", taskId.GetValue(), ":", err)
}
}
return driver.Status
}
func setupSchedMsgQ(driver *SchedulerDriver) {
sched := driver.Scheduler
for event := range driver.schedMsgQ {
if sched == nil {
log.Println("WARN: Scheduler not set, no event will be handled.")
}
switch msg := event.(type) {
case *mesos.FrameworkRegisteredMessage:
driver.handleRegistered(msg)
case *mesos.FrameworkReregisteredMessage:
driver.handleReregistered(msg)
case *mesos.ResourceOffersMessage:
driver.handleResourceOffers(msg)
case *mesos.RescindResourceOfferMessage:
go func() {
if sched.OfferRescinded != nil {
sched.OfferRescinded(driver, msg.OfferId)
}
}()
case *mesos.StatusUpdateMessage:
go func() {
if sched.StatusUpdate != nil {
sched.StatusUpdate(driver, msg.Update.Status)
}
}()
case *mesos.ExecutorToFrameworkMessage:
go func() {
if sched.FrameworkMessage != nil {
sched.FrameworkMessage(driver, msg.ExecutorId, msg.SlaveId, msg.Data)
}
}()
case *mesos.LostSlaveMessage:
go func() {
if sched.SlaveLost != nil {
sched.SlaveLost(driver, msg.SlaveId)
}
}()
case MesosError:
go func() {
driver.handleError(msg)
}()
default:
go func() {
err := NewMesosError("Driver received unexpected event.")
driver.handleError(err)
}()
}
}
}
func (driver *SchedulerDriver) handleRegistered(msg *mesos.FrameworkRegisteredMessage) {
if driver.Status == mesos.Status_DRIVER_ABORTED {
log.Println("Ignoring FrameworkRegisteredMessage, the driver is aborted!")
return
}
if driver.connected == true {
log.Println("Ignoring FrameworkRegisteredMessage, the driver is already connected!")
return
}
//TODO detect if message was from leading-master (sched.cpp)
log.Printf("Framework registered with ID [%s] ", msg.GetFrameworkId().GetValue())
// TODO add synchronization
driver.connected = true
driver.failover = false
sched := driver.Scheduler
if sched != nil && sched.Registered != nil {
go sched.Registered(driver, msg.FrameworkId, msg.MasterInfo)
}
}
func (driver *SchedulerDriver) handleReregistered(msg *mesos.FrameworkReregisteredMessage) {
if driver.Status == mesos.Status_DRIVER_ABORTED {
log.Println("Ignoring FrameworkReRegisteredMessage, the driver is aborted!")
return
}
if driver.connected == true {
log.Println("Ignoring FrameworkReRegisteredMessage, the driver is already connected!")
return
}
//TODO detect if message was from leading-master (sched.cpp)
log.Printf("Framework re-registered with ID [%s] ", msg.GetFrameworkId().GetValue())
// TODO add synchronization
driver.connected = true
driver.failover = false
sched := driver.Scheduler
if sched != nil && sched.Reregistered != nil {
sched.Reregistered(driver, msg.MasterInfo)
}
}
func (driver *SchedulerDriver) handleResourceOffers(msg *mesos.ResourceOffersMessage) {
if driver.Status == mesos.Status_DRIVER_ABORTED {
log.Println("Ignoring ResourceOffersMessage, the driver is aborted!")
return
}
if !driver.connected {
log.Println("Ignoring ResourceOffersMessage, the driver is not connected!")
return
}
sched := driver.Scheduler
if sched != nil && sched.ResourceOffers != nil {
go sched.ResourceOffers(driver, msg.Offers)
}
}
func (driver *SchedulerDriver) handleError(err MesosError) {
if driver.Status == mesos.Status_DRIVER_ABORTED {
log.Println("Ignoring error because driver is aborted.")
return
}
stat := driver.Abort()
if stat == mesos.Status_DRIVER_ABORTED {
if driver.Scheduler.Error != nil {
driver.Scheduler.Error(driver, err)
}
}
}