forked from vladimirvivien/gomes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
driver.go
254 lines (215 loc) · 6 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
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
}
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,
schedMsgQ: make(chan interface{}, 10),
controlQ: make(chan mesos.Status),
}
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 && driver.Status != mesos.Status_DRIVER_ABORTED {
return driver.Status
}
err := driver.schedProc.stop()
if err != nil {
driver.schedMsgQ <- err
}
if driver.masterClient.connected && !failover {
err = driver.masterClient.UnregisterFramework(driver.schedProc.processId, driver.FrameworkInfo.Id)
if err != nil {
driver.Status = mesos.Status_DRIVER_ABORTED
driver.schedMsgQ <- NewMesosError("Failed to unregister the framework:" + err.Error())
} else {
driver.Status = mesos.Status_DRIVER_STOPPED
}
}
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.masterClient.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 setupSchedMsgQ(driver *SchedulerDriver) {
sched := driver.Scheduler
for event := range driver.schedMsgQ {
if driver.Scheduler == nil {
log.Println("WARN: Scheduler not set.")
break
}
switch msg := event.(type) {
case *mesos.FrameworkRegisteredMessage:
go func() {
if sched.Registered != nil {
sched.Registered(driver, msg.FrameworkId, msg.MasterInfo)
}
}()
case *mesos.FrameworkReregisteredMessage:
go func() {
if sched.Reregistered != nil {
sched.Reregistered(driver, msg.MasterInfo)
}
}()
case *mesos.ResourceOffersMessage:
go func() {
if sched.ResourceOffers != nil {
sched.ResourceOffers(driver, msg.Offers)
}
}()
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.handleDriverError(msg)
}()
default:
go func() {
err := NewMesosError("Driver received unexpected event.")
driver.handleDriverError(err)
}()
}
}
}
func (driver *SchedulerDriver) handleDriverError(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)
}
}
}