| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
package executor |
| 20 |
|
| 21 |
import ( |
| 22 |
"fmt" |
| 23 |
"net" |
| 24 |
"os" |
| 25 |
"sync" |
| 26 |
"time" |
| 27 |
|
| 28 |
"github.com/gogo/protobuf/proto" |
| 29 |
log "github.com/golang/glog" |
| 30 |
"github.com/mesos/mesos-go/api/v0/mesosproto" |
| 31 |
"github.com/mesos/mesos-go/api/v0/mesosutil" |
| 32 |
"github.com/mesos/mesos-go/api/v0/mesosutil/process" |
| 33 |
"github.com/mesos/mesos-go/api/v0/messenger" |
| 34 |
"github.com/mesos/mesos-go/api/v0/messenger/sessionid" |
| 35 |
"github.com/mesos/mesos-go/api/v0/upid" |
| 36 |
"github.com/pborman/uuid" |
| 37 |
"golang.org/x/net/context" |
| 38 |
) |
| 39 |
|
| 40 |
const ( |
| 41 |
defaultRecoveryTimeout = 15 * time.Minute |
| 42 |
) |
| 43 |
|
| 44 |
type DriverConfig struct { |
| 45 |
Executor Executor |
| 46 |
HostnameOverride string |
| 47 |
BindingAddress net.IP |
| 48 |
BindingPort uint16 |
| 49 |
PublishedAddress net.IP |
| 50 |
NewMessenger func() (messenger.Messenger, error) |
| 51 |
} |
| 52 |
|
| 53 |
|
| 54 |
type MesosExecutorDriver struct { |
| 55 |
lock sync.RWMutex |
| 56 |
cond *sync.Cond |
| 57 |
self *upid.UPID |
| 58 |
stopCh chan struct{} |
| 59 |
status mesosproto.Status |
| 60 |
messenger messenger.Messenger |
| 61 |
slaveUPID *upid.UPID |
| 62 |
slaveID *mesosproto.SlaveID |
| 63 |
frameworkID *mesosproto.FrameworkID |
| 64 |
executorID *mesosproto.ExecutorID |
| 65 |
workDir string |
| 66 |
connected bool |
| 67 |
connection uuid.UUID |
| 68 |
local bool |
| 69 |
directory string |
| 70 |
checkpoint bool |
| 71 |
recoveryTimeout time.Duration |
| 72 |
recoveryTimer *time.Timer |
| 73 |
updates map[string]*mesosproto.StatusUpdate |
| 74 |
tasks map[string]*mesosproto.TaskInfo |
| 75 |
withExecutor func(f func(e Executor)) |
| 76 |
started chan struct{} |
| 77 |
} |
| 78 |
|
| 79 |
|
| 80 |
func NewMesosExecutorDriver(config DriverConfig) (*MesosExecutorDriver, error) { |
| 81 |
if config.Executor == nil { |
| 82 |
msg := "Executor callback interface cannot be nil." |
| 83 |
log.Errorln(msg) |
| 84 |
return nil, fmt.Errorf(msg) |
| 85 |
} |
| 86 |
|
| 87 |
hostname := mesosutil.GetHostname(config.HostnameOverride) |
| 88 |
newMessenger := config.NewMessenger |
| 89 |
if newMessenger == nil { |
| 90 |
newMessenger = func() (messenger.Messenger, error) { |
| 91 |
process := process.New("executor") |
| 92 |
return messenger.ForHostname(process, hostname, config.BindingAddress, config.BindingPort, config.PublishedAddress) |
| 93 |
} |
| 94 |
} |
| 95 |
|
| 96 |
driver := &MesosExecutorDriver{ |
| 97 |
status: mesosproto.Status_DRIVER_NOT_STARTED, |
| 98 |
stopCh: make(chan struct{}), |
| 99 |
updates: make(map[string]*mesosproto.StatusUpdate), |
| 100 |
tasks: make(map[string]*mesosproto.TaskInfo), |
| 101 |
workDir: ".", |
| 102 |
started: make(chan struct{}), |
| 103 |
recoveryTimeout: defaultRecoveryTimeout, |
| 104 |
} |
| 105 |
driver.cond = sync.NewCond(&driver.lock) |
| 106 |
|
| 107 |
var execLock sync.Mutex |
| 108 |
driver.withExecutor = func(f func(e Executor)) { |
| 109 |
go func() { |
| 110 |
execLock.Lock() |
| 111 |
defer execLock.Unlock() |
| 112 |
f(config.Executor) |
| 113 |
}() |
| 114 |
} |
| 115 |
var err error |
| 116 |
if driver.messenger, err = newMessenger(); err != nil { |
| 117 |
return nil, err |
| 118 |
} |
| 119 |
if err = driver.init(); err != nil { |
| 120 |
log.Errorf("failed to initialize the driver: %v", err) |
| 121 |
return nil, err |
| 122 |
} |
| 123 |
return driver, nil |
| 124 |
} |
| 125 |
|
| 126 |
|
| 127 |
func (driver *MesosExecutorDriver) context() context.Context { |
| 128 |
return sessionid.NewContext(context.TODO(), driver.connection.String()) |
| 129 |
} |
| 130 |
|
| 131 |
|
| 132 |
func (driver *MesosExecutorDriver) init() error { |
| 133 |
log.Infof("Init mesos executor driver\n") |
| 134 |
log.Infof("Protocol Version: %v\n", mesosutil.MesosVersion) |
| 135 |
|
| 136 |
|
| 137 |
if err := driver.parseEnviroments(); err != nil { |
| 138 |
log.Errorf("Failed to parse environments: %v\n", err) |
| 139 |
return err |
| 140 |
} |
| 141 |
|
| 142 |
type messageHandler func(context.Context, *upid.UPID, proto.Message) |
| 143 |
|
| 144 |
guard := func(h messageHandler) messenger.MessageHandler { |
| 145 |
return messenger.MessageHandler(func(from *upid.UPID, pbMsg proto.Message) { |
| 146 |
driver.lock.Lock() |
| 147 |
defer driver.lock.Unlock() |
| 148 |
h(driver.context(), from, pbMsg) |
| 149 |
}) |
| 150 |
} |
| 151 |
|
| 152 |
|
| 153 |
driver.messenger.Install(guard(driver.registered), &mesosproto.ExecutorRegisteredMessage{}) |
| 154 |
driver.messenger.Install(guard(driver.reregistered), &mesosproto.ExecutorReregisteredMessage{}) |
| 155 |
driver.messenger.Install(guard(driver.reconnect), &mesosproto.ReconnectExecutorMessage{}) |
| 156 |
driver.messenger.Install(guard(driver.runTask), &mesosproto.RunTaskMessage{}) |
| 157 |
driver.messenger.Install(guard(driver.killTask), &mesosproto.KillTaskMessage{}) |
| 158 |
driver.messenger.Install(guard(driver.statusUpdateAcknowledgement), &mesosproto.StatusUpdateAcknowledgementMessage{}) |
| 159 |
driver.messenger.Install(guard(driver.frameworkMessage), &mesosproto.FrameworkToExecutorMessage{}) |
| 160 |
driver.messenger.Install(guard(driver.shutdown), &mesosproto.ShutdownExecutorMessage{}) |
| 161 |
driver.messenger.Install(guard(driver.frameworkError), &mesosproto.FrameworkErrorMessage{}) |
| 162 |
driver.messenger.Install(guard(driver.networkError), &mesosproto.InternalNetworkError{}) |
| 163 |
return nil |
| 164 |
} |
| 165 |
|
| 166 |
func (driver *MesosExecutorDriver) parseEnviroments() error { |
| 167 |
var value string |
| 168 |
|
| 169 |
value = os.Getenv("MESOS_LOCAL") |
| 170 |
if len(value) > 0 { |
| 171 |
driver.local = true |
| 172 |
} |
| 173 |
|
| 174 |
value = os.Getenv("MESOS_SLAVE_PID") |
| 175 |
if len(value) == 0 { |
| 176 |
return fmt.Errorf("Cannot find MESOS_SLAVE_PID in the environment") |
| 177 |
} |
| 178 |
upid, err := upid.Parse(value) |
| 179 |
if err != nil { |
| 180 |
log.Errorf("Cannot parse UPID %v\n", err) |
| 181 |
return err |
| 182 |
} |
| 183 |
driver.slaveUPID = upid |
| 184 |
|
| 185 |
value = os.Getenv("MESOS_SLAVE_ID") |
| 186 |
driver.slaveID = &mesosproto.SlaveID{Value: proto.String(value)} |
| 187 |
|
| 188 |
value = os.Getenv("MESOS_FRAMEWORK_ID") |
| 189 |
driver.frameworkID = &mesosproto.FrameworkID{Value: proto.String(value)} |
| 190 |
|
| 191 |
value = os.Getenv("MESOS_EXECUTOR_ID") |
| 192 |
driver.executorID = &mesosproto.ExecutorID{Value: proto.String(value)} |
| 193 |
|
| 194 |
value = os.Getenv("MESOS_DIRECTORY") |
| 195 |
if len(value) > 0 { |
| 196 |
driver.workDir = value |
| 197 |
} |
| 198 |
|
| 199 |
value = os.Getenv("MESOS_CHECKPOINT") |
| 200 |
if value == "1" { |
| 201 |
driver.checkpoint = true |
| 202 |
} |
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
return nil |
| 211 |
} |
| 212 |
|
| 213 |
|
| 214 |
func (driver *MesosExecutorDriver) Status() mesosproto.Status { |
| 215 |
driver.lock.RLock() |
| 216 |
defer driver.lock.RUnlock() |
| 217 |
return driver.status |
| 218 |
} |
| 219 |
|
| 220 |
func (driver *MesosExecutorDriver) Running() bool { |
| 221 |
driver.lock.RLock() |
| 222 |
defer driver.lock.RUnlock() |
| 223 |
return driver.status == mesosproto.Status_DRIVER_RUNNING |
| 224 |
} |
| 225 |
|
| 226 |
func (driver *MesosExecutorDriver) stopped() bool { |
| 227 |
return driver.status != mesosproto.Status_DRIVER_RUNNING |
| 228 |
} |
| 229 |
|
| 230 |
func (driver *MesosExecutorDriver) Connected() bool { |
| 231 |
driver.lock.RLock() |
| 232 |
defer driver.lock.RUnlock() |
| 233 |
return driver.connected |
| 234 |
} |
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
func (driver *MesosExecutorDriver) networkError(ctx context.Context, from *upid.UPID, pbMsg proto.Message) { |
| 245 |
if driver.status == mesosproto.Status_DRIVER_ABORTED { |
| 246 |
log.V(1).Info("ignoring network error because aborted") |
| 247 |
return |
| 248 |
} |
| 249 |
if driver.connected { |
| 250 |
driver.connected = false |
| 251 |
msg := pbMsg.(*mesosproto.InternalNetworkError) |
| 252 |
session := msg.GetSession() |
| 253 |
|
| 254 |
if session != driver.connection.String() { |
| 255 |
log.V(1).Infoln("ignoring netwok error for disconnected/stale session") |
| 256 |
return |
| 257 |
} |
| 258 |
|
| 259 |
if driver.checkpoint { |
| 260 |
log.Infoln("slave disconnected, will wait for recovery") |
| 261 |
driver.withExecutor(func(e Executor) { e.Disconnected(driver) }) |
| 262 |
|
| 263 |
if driver.recoveryTimer != nil { |
| 264 |
driver.recoveryTimer.Stop() |
| 265 |
} |
| 266 |
t := time.NewTimer(driver.recoveryTimeout) |
| 267 |
driver.recoveryTimer = t |
| 268 |
go func() { |
| 269 |
select { |
| 270 |
case <-t.C: |
| 271 |
|
| 272 |
driver.lock.Lock() |
| 273 |
defer driver.lock.Unlock() |
| 274 |
driver.recoveryTimedOut(session) |
| 275 |
|
| 276 |
case <-driver.stopCh: |
| 277 |
|
| 278 |
return |
| 279 |
} |
| 280 |
}() |
| 281 |
return |
| 282 |
} |
| 283 |
} |
| 284 |
log.Infoln("slave exited ... shutting down") |
| 285 |
driver.withExecutor(func(e Executor) { e.Shutdown(driver) }) |
| 286 |
driver.abort() |
| 287 |
} |
| 288 |
|
| 289 |
func (driver *MesosExecutorDriver) recoveryTimedOut(connection string) { |
| 290 |
if driver.connected { |
| 291 |
return |
| 292 |
} |
| 293 |
|
| 294 |
if connection == driver.connection.String() { |
| 295 |
log.Info("recovery timeout of %v exceeded; shutting down", driver.recoveryTimeout) |
| 296 |
driver.shutdown(driver.context(), nil, nil) |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
func (driver *MesosExecutorDriver) registered(_ context.Context, from *upid.UPID, pbMsg proto.Message) { |
| 301 |
if driver.status == mesosproto.Status_DRIVER_ABORTED { |
| 302 |
log.V(1).Infof("ignoring registration message from slave because aborted") |
| 303 |
return |
| 304 |
} |
| 305 |
log.Infoln("Executor driver registered") |
| 306 |
|
| 307 |
msg := pbMsg.(*mesosproto.ExecutorRegisteredMessage) |
| 308 |
slaveID := msg.GetSlaveId() |
| 309 |
executorInfo := msg.GetExecutorInfo() |
| 310 |
frameworkInfo := msg.GetFrameworkInfo() |
| 311 |
slaveInfo := msg.GetSlaveInfo() |
| 312 |
|
| 313 |
if driver.stopped() { |
| 314 |
log.Infof("Ignoring registered message from slave %v, because the driver is stopped!\n", slaveID) |
| 315 |
return |
| 316 |
} |
| 317 |
|
| 318 |
log.Infof("Registered on slave %v\n", slaveID) |
| 319 |
driver.connected = true |
| 320 |
driver.connection = uuid.NewUUID() |
| 321 |
driver.cond.Broadcast() |
| 322 |
driver.withExecutor(func(e Executor) { e.Registered(driver, executorInfo, frameworkInfo, slaveInfo) }) |
| 323 |
} |
| 324 |
|
| 325 |
func (driver *MesosExecutorDriver) reregistered(_ context.Context, from *upid.UPID, pbMsg proto.Message) { |
| 326 |
if driver.status == mesosproto.Status_DRIVER_ABORTED { |
| 327 |
log.V(1).Infof("ignoring reregistration message from slave because aborted") |
| 328 |
return |
| 329 |
} |
| 330 |
log.Infoln("Executor driver reregistered") |
| 331 |
|
| 332 |
msg := pbMsg.(*mesosproto.ExecutorReregisteredMessage) |
| 333 |
slaveID := msg.GetSlaveId() |
| 334 |
slaveInfo := msg.GetSlaveInfo() |
| 335 |
|
| 336 |
if driver.stopped() { |
| 337 |
log.Infof("Ignoring re-registered message from slave %v, because the driver is stopped!\n", slaveID) |
| 338 |
return |
| 339 |
} |
| 340 |
|
| 341 |
log.Infof("Re-registered on slave %v\n", slaveID) |
| 342 |
driver.connected = true |
| 343 |
driver.connection = uuid.NewUUID() |
| 344 |
driver.cond.Broadcast() |
| 345 |
driver.withExecutor(func(e Executor) { e.Reregistered(driver, slaveInfo) }) |
| 346 |
} |
| 347 |
|
| 348 |
func (driver *MesosExecutorDriver) send(ctx context.Context, upid *upid.UPID, msg proto.Message) error { |
| 349 |
c := make(chan error, 1) |
| 350 |
go func() { c <- driver.messenger.Send(ctx, upid, msg) }() |
| 351 |
|
| 352 |
select { |
| 353 |
case <-ctx.Done(): |
| 354 |
<-c |
| 355 |
return ctx.Err() |
| 356 |
case err := <-c: |
| 357 |
return err |
| 358 |
} |
| 359 |
} |
| 360 |
|
| 361 |
func (driver *MesosExecutorDriver) reconnect(ctx context.Context, from *upid.UPID, pbMsg proto.Message) { |
| 362 |
if driver.status == mesosproto.Status_DRIVER_ABORTED { |
| 363 |
log.V(1).Infof("ignoring reconnect message from slave because aborted") |
| 364 |
return |
| 365 |
} |
| 366 |
log.Infoln("Executor driver reconnect") |
| 367 |
|
| 368 |
msg := pbMsg.(*mesosproto.ReconnectExecutorMessage) |
| 369 |
slaveID := msg.GetSlaveId() |
| 370 |
|
| 371 |
if driver.stopped() { |
| 372 |
log.Infof("Ignoring reconnect message from slave %v, because the driver is stopped!\n", slaveID) |
| 373 |
return |
| 374 |
} |
| 375 |
|
| 376 |
log.Infof("Received reconnect request from slave %v\n", slaveID) |
| 377 |
driver.slaveUPID = from |
| 378 |
|
| 379 |
message := &mesosproto.ReregisterExecutorMessage{ |
| 380 |
ExecutorId: driver.executorID, |
| 381 |
FrameworkId: driver.frameworkID, |
| 382 |
} |
| 383 |
|
| 384 |
for _, u := range driver.updates { |
| 385 |
message.Updates = append(message.Updates, u) |
| 386 |
} |
| 387 |
|
| 388 |
for _, t := range driver.tasks { |
| 389 |
message.Tasks = append(message.Tasks, t) |
| 390 |
} |
| 391 |
|
| 392 |
if err := driver.send(ctx, driver.slaveUPID, message); err != nil { |
| 393 |
log.Errorf("Failed to send %v: %v\n", message, err) |
| 394 |
} |
| 395 |
} |
| 396 |
|
| 397 |
func (driver *MesosExecutorDriver) runTask(_ context.Context, from *upid.UPID, pbMsg proto.Message) { |
| 398 |
if driver.status == mesosproto.Status_DRIVER_ABORTED { |
| 399 |
log.V(1).Infof("ignoring runTask message from slave because aborted") |
| 400 |
return |
| 401 |
} |
| 402 |
log.Infoln("Executor driver runTask") |
| 403 |
|
| 404 |
msg := pbMsg.(*mesosproto.RunTaskMessage) |
| 405 |
task := msg.GetTask() |
| 406 |
taskID := task.GetTaskId() |
| 407 |
|
| 408 |
if driver.stopped() { |
| 409 |
log.Infof("Ignoring run task message for task %v because the driver is stopped!\n", taskID) |
| 410 |
return |
| 411 |
} |
| 412 |
if _, ok := driver.tasks[taskID.String()]; ok { |
| 413 |
log.Fatalf("Unexpected duplicate task %v\n", taskID) |
| 414 |
} |
| 415 |
|
| 416 |
log.Infof("Executor asked to run task '%v'\n", taskID) |
| 417 |
driver.tasks[taskID.String()] = task |
| 418 |
driver.withExecutor(func(e Executor) { e.LaunchTask(driver, task) }) |
| 419 |
} |
| 420 |
|
| 421 |
func (driver *MesosExecutorDriver) killTask(_ context.Context, from *upid.UPID, pbMsg proto.Message) { |
| 422 |
if driver.status == mesosproto.Status_DRIVER_ABORTED { |
| 423 |
log.V(1).Infof("ignoring killTask message from slave because aborted") |
| 424 |
return |
| 425 |
} |
| 426 |
log.Infoln("Executor driver killTask") |
| 427 |
|
| 428 |
msg := pbMsg.(*mesosproto.KillTaskMessage) |
| 429 |
taskID := msg.GetTaskId() |
| 430 |
|
| 431 |
if driver.stopped() { |
| 432 |
log.Infof("Ignoring kill task message for task %v, because the driver is stopped!\n", taskID) |
| 433 |
return |
| 434 |
} |
| 435 |
|
| 436 |
log.Infof("Executor driver is asked to kill task '%v'\n", taskID) |
| 437 |
driver.withExecutor(func(e Executor) { e.KillTask(driver, taskID) }) |
| 438 |
} |
| 439 |
|
| 440 |
func (driver *MesosExecutorDriver) statusUpdateAcknowledgement(_ context.Context, from *upid.UPID, pbMsg proto.Message) { |
| 441 |
if driver.status == mesosproto.Status_DRIVER_ABORTED { |
| 442 |
log.V(1).Infof("ignoring status update ack message because aborted") |
| 443 |
return |
| 444 |
} |
| 445 |
log.Infoln("Executor statusUpdateAcknowledgement") |
| 446 |
|
| 447 |
msg := pbMsg.(*mesosproto.StatusUpdateAcknowledgementMessage) |
| 448 |
log.Infof("Receiving status update acknowledgement %v", msg) |
| 449 |
|
| 450 |
frameworkID := msg.GetFrameworkId() |
| 451 |
taskID := msg.GetTaskId() |
| 452 |
uuid := uuid.UUID(msg.GetUuid()) |
| 453 |
|
| 454 |
if driver.stopped() { |
| 455 |
log.Infof("Ignoring status update acknowledgement %v for task %v of framework %v because the driver is stopped!\n", |
| 456 |
uuid, taskID, frameworkID) |
| 457 |
} |
| 458 |
|
| 459 |
|
| 460 |
delete(driver.updates, uuid.String()) |
| 461 |
|
| 462 |
delete(driver.tasks, taskID.String()) |
| 463 |
} |
| 464 |
|
| 465 |
func (driver *MesosExecutorDriver) frameworkMessage(_ context.Context, from *upid.UPID, pbMsg proto.Message) { |
| 466 |
if driver.status == mesosproto.Status_DRIVER_ABORTED { |
| 467 |
log.V(1).Infof("ignoring frameworkMessage message from slave because aborted") |
| 468 |
return |
| 469 |
} |
| 470 |
log.Infoln("Executor driver received frameworkMessage") |
| 471 |
|
| 472 |
msg := pbMsg.(*mesosproto.FrameworkToExecutorMessage) |
| 473 |
data := msg.GetData() |
| 474 |
|
| 475 |
if driver.stopped() { |
| 476 |
log.Infof("Ignoring framework message because the driver is stopped!\n") |
| 477 |
return |
| 478 |
} |
| 479 |
|
| 480 |
log.Infof("Executor driver receives framework message\n") |
| 481 |
driver.withExecutor(func(e Executor) { e.FrameworkMessage(driver, string(data)) }) |
| 482 |
} |
| 483 |
|
| 484 |
func (driver *MesosExecutorDriver) shutdown(_ context.Context, _ *upid.UPID, _ proto.Message) { |
| 485 |
if driver.status == mesosproto.Status_DRIVER_ABORTED { |
| 486 |
log.V(1).Infof("ignoring shutdown message because aborted") |
| 487 |
return |
| 488 |
} |
| 489 |
log.Infoln("Executor driver received shutdown") |
| 490 |
|
| 491 |
if driver.stopped() { |
| 492 |
log.Infof("Ignoring shutdown message because the driver is stopped!\n") |
| 493 |
return |
| 494 |
} |
| 495 |
|
| 496 |
log.Infof("Executor driver is asked to shutdown\n") |
| 497 |
|
| 498 |
driver.withExecutor(func(e Executor) { e.Shutdown(driver) }) |
| 499 |
|
| 500 |
driver.stop() |
| 501 |
} |
| 502 |
|
| 503 |
func (driver *MesosExecutorDriver) frameworkError(_ context.Context, from *upid.UPID, pbMsg proto.Message) { |
| 504 |
if driver.status == mesosproto.Status_DRIVER_ABORTED { |
| 505 |
log.V(1).Infof("ignoring framework error message because aborted") |
| 506 |
return |
| 507 |
} |
| 508 |
log.Infoln("Executor driver received error") |
| 509 |
|
| 510 |
msg := pbMsg.(*mesosproto.FrameworkErrorMessage) |
| 511 |
driver.withExecutor(func(e Executor) { e.Error(driver, msg.GetMessage()) }) |
| 512 |
} |
| 513 |
|
| 514 |
|
| 515 |
|
| 516 |
|
| 517 |
func (driver *MesosExecutorDriver) Start() (mesosproto.Status, error) { |
| 518 |
driver.lock.Lock() |
| 519 |
defer driver.lock.Unlock() |
| 520 |
return driver.start() |
| 521 |
} |
| 522 |
|
| 523 |
func (driver *MesosExecutorDriver) start() (mesosproto.Status, error) { |
| 524 |
log.Infoln("Starting the executor driver") |
| 525 |
|
| 526 |
if driver.status != mesosproto.Status_DRIVER_NOT_STARTED { |
| 527 |
return driver.status, fmt.Errorf("Unable to Start, expecting status %s, but got %s", mesosproto.Status_DRIVER_NOT_STARTED, driver.status) |
| 528 |
} |
| 529 |
|
| 530 |
|
| 531 |
if err := driver.messenger.Start(); err != nil { |
| 532 |
log.Errorf("Failed to start executor: %v\n", err) |
| 533 |
return driver.status, err |
| 534 |
} |
| 535 |
|
| 536 |
pid := driver.messenger.UPID() |
| 537 |
driver.self = &pid |
| 538 |
|
| 539 |
|
| 540 |
log.V(3).Infoln("Sending Executor registration") |
| 541 |
message := &mesosproto.RegisterExecutorMessage{ |
| 542 |
FrameworkId: driver.frameworkID, |
| 543 |
ExecutorId: driver.executorID, |
| 544 |
} |
| 545 |
|
| 546 |
if err := driver.send(driver.context(), driver.slaveUPID, message); err != nil { |
| 547 |
log.Errorf("Stopping the executor, failed to send %v: %v\n", message, err) |
| 548 |
err0 := driver._stop(driver.status) |
| 549 |
if err0 != nil { |
| 550 |
log.Errorf("Failed to stop executor: %v\n", err) |
| 551 |
return driver.status, err0 |
| 552 |
} |
| 553 |
return driver.status, err |
| 554 |
} |
| 555 |
|
| 556 |
driver.status = mesosproto.Status_DRIVER_RUNNING |
| 557 |
close(driver.started) |
| 558 |
|
| 559 |
log.Infoln("Mesos executor is started with PID=", driver.self.String()) |
| 560 |
return driver.status, nil |
| 561 |
} |
| 562 |
|
| 563 |
|
| 564 |
|
| 565 |
func (driver *MesosExecutorDriver) Stop() (mesosproto.Status, error) { |
| 566 |
driver.lock.Lock() |
| 567 |
defer driver.lock.Unlock() |
| 568 |
return driver.stop() |
| 569 |
} |
| 570 |
|
| 571 |
func (driver *MesosExecutorDriver) stop() (mesosproto.Status, error) { |
| 572 |
log.Infoln("Stopping the executor driver") |
| 573 |
if driver.status != mesosproto.Status_DRIVER_RUNNING { |
| 574 |
return driver.status, fmt.Errorf("Unable to Stop, expecting status %s, but got %s", mesosproto.Status_DRIVER_RUNNING, driver.status) |
| 575 |
} |
| 576 |
return mesosproto.Status_DRIVER_STOPPED, driver._stop(mesosproto.Status_DRIVER_STOPPED) |
| 577 |
} |
| 578 |
|
| 579 |
|
| 580 |
|
| 581 |
func (driver *MesosExecutorDriver) _stop(stopStatus mesosproto.Status) error { |
| 582 |
err := driver.messenger.Stop() |
| 583 |
defer func() { |
| 584 |
select { |
| 585 |
case <-driver.stopCh: |
| 586 |
|
| 587 |
default: |
| 588 |
close(driver.stopCh) |
| 589 |
} |
| 590 |
driver.cond.Broadcast() |
| 591 |
}() |
| 592 |
|
| 593 |
driver.status = stopStatus |
| 594 |
if err != nil { |
| 595 |
return err |
| 596 |
} |
| 597 |
return nil |
| 598 |
} |
| 599 |
|
| 600 |
|
| 601 |
|
| 602 |
func (driver *MesosExecutorDriver) Abort() (mesosproto.Status, error) { |
| 603 |
driver.lock.Lock() |
| 604 |
defer driver.lock.Unlock() |
| 605 |
return driver.abort() |
| 606 |
} |
| 607 |
|
| 608 |
func (driver *MesosExecutorDriver) abort() (mesosproto.Status, error) { |
| 609 |
if driver.status != mesosproto.Status_DRIVER_RUNNING { |
| 610 |
return driver.status, fmt.Errorf("Unable to Stop, expecting status %s, but got %s", mesosproto.Status_DRIVER_RUNNING, driver.status) |
| 611 |
} |
| 612 |
|
| 613 |
log.Infoln("Aborting the executor driver") |
| 614 |
return mesosproto.Status_DRIVER_ABORTED, driver._stop(mesosproto.Status_DRIVER_ABORTED) |
| 615 |
} |
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
func (driver *MesosExecutorDriver) Join() (mesosproto.Status, error) { |
| 620 |
driver.lock.Lock() |
| 621 |
defer driver.lock.Unlock() |
| 622 |
return driver.join() |
| 623 |
} |
| 624 |
|
| 625 |
func (driver *MesosExecutorDriver) join() (mesosproto.Status, error) { |
| 626 |
log.Infoln("Waiting for the executor driver to stop") |
| 627 |
if driver.status != mesosproto.Status_DRIVER_RUNNING { |
| 628 |
return driver.status, fmt.Errorf("Unable to Join, expecting status %s, but got %s", mesosproto.Status_DRIVER_RUNNING, driver.status) |
| 629 |
} |
| 630 |
for { |
| 631 |
select { |
| 632 |
case <-driver.stopCh: |
| 633 |
return driver.status, nil |
| 634 |
default: |
| 635 |
driver.cond.Wait() |
| 636 |
} |
| 637 |
} |
| 638 |
} |
| 639 |
|
| 640 |
|
| 641 |
func (driver *MesosExecutorDriver) Run() (mesosproto.Status, error) { |
| 642 |
driver.lock.Lock() |
| 643 |
defer driver.lock.Unlock() |
| 644 |
return driver.run() |
| 645 |
} |
| 646 |
|
| 647 |
func (driver *MesosExecutorDriver) run() (mesosproto.Status, error) { |
| 648 |
stat, err := driver.start() |
| 649 |
|
| 650 |
if err != nil { |
| 651 |
return driver.stop() |
| 652 |
} |
| 653 |
|
| 654 |
if stat != mesosproto.Status_DRIVER_RUNNING { |
| 655 |
return stat, fmt.Errorf("Unable to continue to Run, expecting status %s, but got %s", mesosproto.Status_DRIVER_RUNNING, driver.status) |
| 656 |
} |
| 657 |
|
| 658 |
return driver.join() |
| 659 |
} |
| 660 |
|
| 661 |
|
| 662 |
func (driver *MesosExecutorDriver) SendStatusUpdate(taskStatus *mesosproto.TaskStatus) (mesosproto.Status, error) { |
| 663 |
driver.lock.Lock() |
| 664 |
defer driver.lock.Unlock() |
| 665 |
return driver.sendStatusUpdate(taskStatus) |
| 666 |
} |
| 667 |
|
| 668 |
func (driver *MesosExecutorDriver) sendStatusUpdate(taskStatus *mesosproto.TaskStatus) (mesosproto.Status, error) { |
| 669 |
log.V(3).Infoln("Sending task status update: ", taskStatus.String()) |
| 670 |
|
| 671 |
if driver.status != mesosproto.Status_DRIVER_RUNNING { |
| 672 |
return driver.status, fmt.Errorf("Unable to SendStatusUpdate, expecting driver.status %s, but got %s", mesosproto.Status_DRIVER_RUNNING, driver.status) |
| 673 |
} |
| 674 |
|
| 675 |
if taskStatus.GetState() == mesosproto.TaskState_TASK_STAGING { |
| 676 |
err := fmt.Errorf("Executor is not allowed to send TASK_STAGING status update. Aborting!") |
| 677 |
log.Errorln(err) |
| 678 |
if err0 := driver._stop(mesosproto.Status_DRIVER_ABORTED); err0 != nil { |
| 679 |
log.Errorln("Error while stopping the driver", err0) |
| 680 |
} |
| 681 |
|
| 682 |
return driver.status, err |
| 683 |
} |
| 684 |
|
| 685 |
|
| 686 |
update := driver.makeStatusUpdate(taskStatus) |
| 687 |
log.Infof("Executor sending status update %v\n", update.String()) |
| 688 |
|
| 689 |
|
| 690 |
driver.updates[uuid.UUID(update.GetUuid()).String()] = update |
| 691 |
|
| 692 |
|
| 693 |
message := &mesosproto.StatusUpdateMessage{ |
| 694 |
Update: update, |
| 695 |
Pid: proto.String(driver.self.String()), |
| 696 |
} |
| 697 |
|
| 698 |
if err := driver.send(driver.context(), driver.slaveUPID, message); err != nil { |
| 699 |
log.Errorf("Failed to send %v: %v\n", message, err) |
| 700 |
return driver.status, err |
| 701 |
} |
| 702 |
|
| 703 |
return driver.status, nil |
| 704 |
} |
| 705 |
|
| 706 |
func (driver *MesosExecutorDriver) makeStatusUpdate(taskStatus *mesosproto.TaskStatus) *mesosproto.StatusUpdate { |
| 707 |
now := float64(time.Now().Unix()) |
| 708 |
|
| 709 |
taskStatus.Timestamp = proto.Float64(now) |
| 710 |
taskStatus.SlaveId = driver.slaveID |
| 711 |
update := &mesosproto.StatusUpdate{ |
| 712 |
FrameworkId: driver.frameworkID, |
| 713 |
ExecutorId: driver.executorID, |
| 714 |
SlaveId: driver.slaveID, |
| 715 |
Status: taskStatus, |
| 716 |
Timestamp: proto.Float64(now), |
| 717 |
Uuid: uuid.NewUUID(), |
| 718 |
} |
| 719 |
return update |
| 720 |
} |
| 721 |
|
| 722 |
|
| 723 |
|
| 724 |
func (driver *MesosExecutorDriver) SendFrameworkMessage(data string) (mesosproto.Status, error) { |
| 725 |
driver.lock.Lock() |
| 726 |
defer driver.lock.Unlock() |
| 727 |
return driver.sendFrameworkMessage(data) |
| 728 |
} |
| 729 |
|
| 730 |
func (driver *MesosExecutorDriver) sendFrameworkMessage(data string) (mesosproto.Status, error) { |
| 731 |
log.V(3).Infoln("Sending framework message", string(data)) |
| 732 |
|
| 733 |
if driver.status != mesosproto.Status_DRIVER_RUNNING { |
| 734 |
return driver.status, fmt.Errorf("Unable to SendFrameworkMessage, expecting status %s, but got %s", mesosproto.Status_DRIVER_RUNNING, driver.status) |
| 735 |
} |
| 736 |
|
| 737 |
message := &mesosproto.ExecutorToFrameworkMessage{ |
| 738 |
SlaveId: driver.slaveID, |
| 739 |
FrameworkId: driver.frameworkID, |
| 740 |
ExecutorId: driver.executorID, |
| 741 |
Data: []byte(data), |
| 742 |
} |
| 743 |
|
| 744 |
|
| 745 |
if err := driver.send(driver.context(), driver.slaveUPID, message); err != nil { |
| 746 |
log.Errorln("Failed to send message %v: %v", message, err) |
| 747 |
return driver.status, err |
| 748 |
} |
| 749 |
return driver.status, nil |
| 750 |
} |