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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515 1516 1517 1518 1519 1520 1521 1522 1523 1524 1525 1526 1527 1528 1529 1530 1531 1532 1533 1534 1535 1536 1537 1538 1539
|
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package scheduler
import (
"errors"
"fmt"
"math"
"math/rand"
"net"
"os/user"
"sync"
"sync/atomic"
"time"
"github.com/gogo/protobuf/proto"
log "github.com/golang/glog"
"github.com/mesos/mesos-go/api/v0/auth"
"github.com/mesos/mesos-go/api/v0/detector"
mesos "github.com/mesos/mesos-go/api/v0/mesosproto"
"github.com/mesos/mesos-go/api/v0/mesosproto/scheduler"
util "github.com/mesos/mesos-go/api/v0/mesosutil"
"github.com/mesos/mesos-go/api/v0/mesosutil/process"
"github.com/mesos/mesos-go/api/v0/messenger"
"github.com/mesos/mesos-go/api/v0/messenger/sessionid"
"github.com/mesos/mesos-go/api/v0/upid"
"github.com/pborman/uuid"
"golang.org/x/net/context"
)
const (
defaultAuthenticationTimeout = 30 * time.Second // timeout interval for an authentication attempt
registrationRetryIntervalMax = float64(1 * time.Minute)
registrationBackoffFactor = 2 * time.Second
)
var (
ErrDisconnected = errors.New("disconnected from mesos master")
errAuthenticationCanceled = errors.New("authentication canceled")
)
type ErrDriverAborted struct {
Reason string
}
func (err *ErrDriverAborted) Error() string {
if err.Reason != "" {
return err.Reason
}
return "driver-aborted"
}
// helper to track authentication progress and to prevent multiple close() ops
// against a signalling chan. it's safe to invoke the func's of this struct
// even if the receiver pointer is nil.
type authenticationAttempt struct {
done chan struct{}
doneOnce sync.Once
}
func (a *authenticationAttempt) cancel() {
if a != nil {
a.doneOnce.Do(func() { close(a.done) })
}
}
func (a *authenticationAttempt) inProgress() bool {
if a != nil {
select {
case <-a.done:
return false
default:
return true
}
}
return false
}
type DriverConfig struct {
Scheduler Scheduler
Framework *mesos.FrameworkInfo
Master string
Credential *mesos.Credential // optional
WithAuthContext func(context.Context) context.Context // required when Credential != nil
HostnameOverride string // optional
BindingAddress net.IP // optional
BindingPort uint16 // optional
PublishedAddress net.IP // optional
NewMessenger func() (messenger.Messenger, error) // optional
NewDetector func() (detector.Master, error) // optional
}
// Concrete implementation of a SchedulerDriver that connects a
// Scheduler with a Mesos master. The MesosSchedulerDriver is
// thread-safe.
//
// Note that scheduler failover is supported in Mesos. After a
// scheduler is registered with Mesos it may failover (to a new
// process on the same machine or across multiple machines) by
// creating a new driver with the ID given to it in
// Scheduler.Registered().
//
// The driver is responsible for invoking the Scheduler callbacks as
// it communicates with the Mesos master.
//
// Note that blocking on the MesosSchedulerDriver (e.g., via
// MesosSchedulerDriver.Join) doesn't affect the scheduler callbacks
// in anyway because they are handled by a different thread.
//
// TODO(yifan): examples.
// See src/examples/test_framework.cpp for an example of using the
// MesosSchedulerDriver.
type MesosSchedulerDriver struct {
masterPid *upid.UPID
frameworkInfo *mesos.FrameworkInfo
self *upid.UPID
stopCh chan struct{}
status mesos.Status
messenger messenger.Messenger
masterDetector detector.Master
connected bool
connection uuid.UUID
failoverTimeout float64
failover bool
cache *schedCache
updates map[string]*mesos.StatusUpdate // Key is a UUID string.
tasks map[string]*mesos.TaskInfo // Key is a UUID string.
credential *mesos.Credential
authenticated bool
authenticating *authenticationAttempt
reauthenticate bool
withAuthContext func(context.Context) context.Context
dispatch func(context.Context, *upid.UPID, proto.Message) error // send a message somewhere
started chan struct{} // signal chan that closes upon a successful call to Start()
eventLock sync.RWMutex // guard for all driver state
withScheduler func(f func(s Scheduler)) // execute some func with respect to the given scheduler; should be the last thing invoked in a handler (lock semantics)
done chan struct{} // signal chan that closes when no more events will be processed
}
// Create a new mesos scheduler driver with the given
// scheduler, framework info,
// master address, and credential(optional)
func NewMesosSchedulerDriver(config DriverConfig) (initializedDriver *MesosSchedulerDriver, err error) {
if config.Scheduler == nil {
err = fmt.Errorf("Scheduler callbacks required.")
} else if config.Master == "" {
err = fmt.Errorf("Missing master location URL.")
} else if config.Framework == nil {
err = fmt.Errorf("FrameworkInfo must be provided.")
} else if config.Credential != nil && config.WithAuthContext == nil {
err = fmt.Errorf("WithAuthContext must be provided when Credential != nil")
}
if err != nil {
return
}
framework := proto.Clone(config.Framework).(*mesos.FrameworkInfo)
// set default userid
if framework.GetUser() == "" {
user, err := user.Current()
if err != nil || user == nil {
if err != nil {
log.Warningf("Failed to obtain username: %v\n", err)
} else {
log.Warningln("Failed to obtain username.")
}
framework.User = proto.String("")
} else {
framework.User = proto.String(user.Username)
}
}
// default hostname
hostname := util.GetHostname(config.HostnameOverride)
if framework.GetHostname() == "" {
framework.Hostname = proto.String(hostname)
}
driver := &MesosSchedulerDriver{
frameworkInfo: framework,
stopCh: make(chan struct{}),
status: mesos.Status_DRIVER_NOT_STARTED,
cache: newSchedCache(),
credential: config.Credential,
failover: framework.Id != nil && len(framework.Id.GetValue()) > 0,
withAuthContext: config.WithAuthContext,
started: make(chan struct{}),
done: make(chan struct{}),
}
driver.withScheduler = driver.makeWithScheduler(config.Scheduler)
if framework.FailoverTimeout != nil && *framework.FailoverTimeout > 0 {
driver.failoverTimeout = *framework.FailoverTimeout * float64(time.Second)
log.V(1).Infof("found failover_timeout = %v", time.Duration(driver.failoverTimeout))
}
newDetector := config.NewDetector
if newDetector == nil {
newDetector = func() (detector.Master, error) {
return detector.New(config.Master)
}
}
newMessenger := config.NewMessenger
if newMessenger == nil {
newMessenger = func() (messenger.Messenger, error) {
process := process.New("scheduler")
return messenger.ForHostname(process, hostname, config.BindingAddress, config.BindingPort, config.PublishedAddress)
}
}
// initialize new detector.
if driver.masterDetector, err = newDetector(); err != nil {
return
} else if driver.messenger, err = newMessenger(); err != nil {
return
} else if err = driver.init(); err != nil {
return
} else {
initializedDriver = driver
}
return
}
func (driver *MesosSchedulerDriver) makeWithScheduler(cs Scheduler) func(func(Scheduler)) {
// mechanism that allows us to asynchronously invoke scheduler callbacks, but in a manner
// such that the callback invocations are serialized. useful because this will decouple the
// goroutine executing a messenger callback from the goroutine executing a scheduler callback,
// while preserving the serialization semantics for each type of callback handling.
// we use a chan to maintain the order of callback invocations; this is important for maintaining
// the order in which status updates are processed.
schedQueue := make(chan func(s Scheduler))
go func() {
defer func() {
close(driver.done)
log.V(1).Infoln("finished processing scheduler events")
}()
for f := range schedQueue {
f(cs)
}
}()
var schedLock sync.Mutex // synchronize write access to schedQueue
abort := int32(0)
// assume that when withScheduler is invoked eventLock is locked
return func(f func(s Scheduler)) {
const timeout = 1 * time.Second
t := time.NewTimer(timeout)
defer t.Stop()
trySend := func() (done bool) {
// don't block while attempting to enqueue a scheduler op; this could
// take a while depending upon the external scheduler implementation.
// also, it allows for multiple go-routines to re-compete for the lock
// every so often - this avoids indefinitely blocking a call to Abort().
driver.eventLock.Unlock()
schedLock.Lock()
defer func() {
schedLock.Unlock()
driver.eventLock.Lock()
}()
if atomic.LoadInt32(&abort) == 1 {
// can't send anymore
return true
}
// try to write to event queue...
select {
case schedQueue <- f:
done = true
case <-driver.stopCh:
done = true
case <-t.C:
}
// if stopping then close out the queue (keeping this check separate from
// the above on purpose! otherwise we could miss the close signal)
select {
case <-driver.stopCh:
if atomic.CompareAndSwapInt32(&abort, 0, 1) {
defer close(schedQueue)
log.V(1).Infoln("stopping scheduler event queue..")
// one last attempt, before we run out of time
select {
case schedQueue <- f:
case <-t.C:
}
}
default:
}
return
}
for !trySend() {
t.Reset(timeout) // TODO(jdef) add jitter to this
}
// have to do this outside trySend because here we're guarded by eventLock; it's ok
// if this happens more then once.
if atomic.LoadInt32(&abort) == 1 {
driver.withScheduler = func(f func(_ Scheduler)) {}
}
}
}
// ctx returns the current context.Context for the driver, expects to be invoked
// only when eventLock is locked.
func (driver *MesosSchedulerDriver) context() context.Context {
// set a "session" attribute so that the messenger can see it
// and use it for reporting delivery errors.
return sessionid.NewContext(context.TODO(), driver.connection.String())
}
// init initializes the driver.
func (driver *MesosSchedulerDriver) init() error {
log.Infof("Initializing mesos scheduler driver\n")
driver.dispatch = driver.messenger.Send
// serialize all callbacks from the messenger
type messageHandler func(context.Context, *upid.UPID, proto.Message)
guarded := func(h messageHandler) messenger.MessageHandler {
return messenger.MessageHandler(func(from *upid.UPID, msg proto.Message) {
driver.eventLock.Lock()
defer driver.eventLock.Unlock()
h(driver.context(), from, msg)
})
}
// Install handlers.
driver.messenger.Install(guarded(driver.frameworkRegistered), &mesos.FrameworkRegisteredMessage{})
driver.messenger.Install(guarded(driver.frameworkReregistered), &mesos.FrameworkReregisteredMessage{})
driver.messenger.Install(guarded(driver.resourcesOffered), &mesos.ResourceOffersMessage{})
driver.messenger.Install(guarded(driver.resourceOfferRescinded), &mesos.RescindResourceOfferMessage{})
driver.messenger.Install(guarded(driver.statusUpdated), &mesos.StatusUpdateMessage{})
driver.messenger.Install(guarded(driver.slaveLost), &mesos.LostSlaveMessage{})
driver.messenger.Install(guarded(driver.frameworkMessageRcvd), &mesos.ExecutorToFrameworkMessage{})
driver.messenger.Install(guarded(driver.frameworkErrorRcvd), &mesos.FrameworkErrorMessage{})
driver.messenger.Install(guarded(driver.exitedExecutor), &mesos.ExitedExecutorMessage{})
driver.messenger.Install(guarded(driver.handleMasterChanged), &mesos.InternalMasterChangeDetected{})
driver.messenger.Install(guarded(driver.handleAuthenticationResult), &mesos.InternalAuthenticationResult{})
driver.messenger.Install(guarded(driver.handleNetworkError), &mesos.InternalNetworkError{})
return nil
}
func (driver *MesosSchedulerDriver) handleNetworkError(_ context.Context, from *upid.UPID, pbMsg proto.Message) {
msg := pbMsg.(*mesos.InternalNetworkError)
if driver.status == mesos.Status_DRIVER_ABORTED {
log.Info("ignoring network error because the driver is aborted.")
return
} else if !from.Equal(driver.self) {
log.Errorf("ignoring network error because message received from upid '%v'", from)
return
} else if !driver.connected {
log.V(1).Infof("ignoring network error since we're not currently connected")
return
}
if driver.masterPid.String() == msg.GetPid() && driver.connection.String() == msg.GetSession() {
// fire a disconnection event
log.V(3).Info("Disconnecting scheduler.")
// need to set all 3 of these at once, since withScheduler() temporarily releases the lock and we don't
// want inconsistent connection facts
driver.masterPid = nil
driver.connected = false
driver.authenticated = false
driver.withScheduler(func(s Scheduler) { s.Disconnected(driver) })
log.Info("master disconnected")
}
}
// lead master detection callback.
func (driver *MesosSchedulerDriver) handleMasterChanged(_ context.Context, from *upid.UPID, pbMsg proto.Message) {
if driver.status == mesos.Status_DRIVER_ABORTED {
log.Info("Ignoring master change because the driver is aborted.")
return
} else if !from.Equal(driver.self) {
log.Errorf("ignoring master changed message received from upid '%v'", from)
return
}
// Reconnect every time a master is detected.
wasConnected := driver.connected
driver.connected = false
driver.authenticated = false
alertScheduler := false
if wasConnected {
log.V(3).Info("Disconnecting scheduler.")
driver.masterPid = nil
alertScheduler = true
}
msg := pbMsg.(*mesos.InternalMasterChangeDetected)
master := msg.Master
if master != nil {
log.Infof("New master %s detected\n", master.GetPid())
pid, err := upid.Parse(master.GetPid())
if err != nil {
panic("Unable to parse Master's PID value.") // this should not happen.
}
driver.masterPid = pid // save for downstream ops.
defer driver.tryAuthentication()
} else {
log.Infoln("No master detected.")
}
if alertScheduler {
driver.withScheduler(func(s Scheduler) { s.Disconnected(driver) })
}
}
// tryAuthentication expects to be guarded by eventLock
func (driver *MesosSchedulerDriver) tryAuthentication() {
if driver.authenticated {
// programming error
panic("already authenticated")
}
masterPid := driver.masterPid // save for referencing later in goroutine
if masterPid == nil {
log.Info("skipping authentication attempt because we lost the master")
return
}
if driver.authenticating.inProgress() {
// authentication is in progress, try to cancel it (we may too late already)
driver.authenticating.cancel()
driver.reauthenticate = true
return
}
if driver.credential != nil {
// authentication can block and we don't want to hold up the messenger loop
authenticating := &authenticationAttempt{done: make(chan struct{})}
go func() {
defer authenticating.cancel()
result := &mesos.InternalAuthenticationResult{
//TODO(jdef): is this really needed?
Success: proto.Bool(false),
Completed: proto.Bool(false),
Pid: proto.String(masterPid.String()),
}
// don't reference driver.authenticating here since it may have changed
if err := driver.authenticate(masterPid, authenticating); err != nil {
log.Errorf("Scheduler failed to authenticate: %v\n", err)
if err == auth.AuthenticationFailed {
result.Completed = proto.Bool(true)
}
} else {
result.Completed = proto.Bool(true)
result.Success = proto.Bool(true)
}
pid := driver.messenger.UPID()
driver.messenger.Route(context.TODO(), &pid, result)
}()
driver.authenticating = authenticating
} else {
log.Infoln("No credentials were provided. " +
"Attempting to register scheduler without authentication.")
driver.authenticated = true
go driver.doReliableRegistration(float64(registrationBackoffFactor))
}
}
func (driver *MesosSchedulerDriver) handleAuthenticationResult(_ context.Context, from *upid.UPID, pbMsg proto.Message) {
if driver.status != mesos.Status_DRIVER_RUNNING {
log.V(1).Info("ignoring authenticate because driver is not running")
return
}
if !from.Equal(driver.self) {
log.Errorf("ignoring authentication result message received from upid '%v'", from)
return
}
if driver.authenticated {
// programming error
panic("already authenticated")
}
if driver.masterPid == nil {
log.Infoln("ignoring authentication result because master is lost")
driver.authenticating.cancel() // cancel any in-progress background attempt
// disable future retries until we get a new master
driver.reauthenticate = false
return
}
msg := pbMsg.(*mesos.InternalAuthenticationResult)
if driver.reauthenticate || !msg.GetCompleted() || driver.masterPid.String() != msg.GetPid() {
log.Infof("failed to authenticate with master %v: master changed", driver.masterPid)
driver.authenticating.cancel() // cancel any in-progress background authentication
driver.reauthenticate = false
driver.tryAuthentication()
return
}
if !msg.GetSuccess() {
log.Errorf("master %v refused authentication", driver.masterPid)
return
}
driver.authenticated = true
go driver.doReliableRegistration(float64(registrationBackoffFactor))
}
// ------------------------- Accessors ----------------------- //
// Status returns the current driver status
func (driver *MesosSchedulerDriver) Status() mesos.Status {
driver.eventLock.RLock()
defer driver.eventLock.RUnlock()
return driver.status
}
// Running returns true if the driver is in the DRIVER_RUNNING state
func (driver *MesosSchedulerDriver) Running() bool {
driver.eventLock.RLock()
defer driver.eventLock.RUnlock()
return driver.status == mesos.Status_DRIVER_RUNNING
}
// Connected returns true if the driver has a registered (and authenticated, if enabled)
// connection to the leading mesos master
func (driver *MesosSchedulerDriver) Connected() bool {
driver.eventLock.RLock()
defer driver.eventLock.RUnlock()
return driver.connected
}
// stopped returns true if the driver status != DRIVER_RUNNING; expects to be guarded by eventLock
func (driver *MesosSchedulerDriver) stopped() bool {
return driver.status != mesos.Status_DRIVER_RUNNING
}
// ---------------------- Handlers for Events from Master --------------- //
func (driver *MesosSchedulerDriver) frameworkRegistered(_ context.Context, from *upid.UPID, pbMsg proto.Message) {
log.V(2).Infoln("Handling scheduler driver framework registered event.")
msg := pbMsg.(*mesos.FrameworkRegisteredMessage)
masterInfo := msg.GetMasterInfo()
masterPid := masterInfo.GetPid()
frameworkId := msg.GetFrameworkId()
if driver.status == mesos.Status_DRIVER_ABORTED {
log.Infof("ignoring FrameworkRegisteredMessage from master %s, driver is aborted", masterPid)
return
}
if driver.connected {
log.Infoln("ignoring FrameworkRegisteredMessage from master, driver is already connected", masterPid)
return
}
if driver.stopped() {
log.Infof("ignoring FrameworkRegisteredMessage from master %s, driver is stopped", masterPid)
return
}
if !driver.masterPid.Equal(from) {
log.Warningf("ignoring framework registered message because it was sent from '%v' instead of leading master '%v'", from, driver.masterPid)
return
}
log.Infof("Framework registered with ID=%s\n", frameworkId.GetValue())
driver.frameworkInfo.Id = frameworkId // generated by master.
driver.connected = true
driver.failover = false
driver.connection = uuid.NewUUID()
driver.withScheduler(func(s Scheduler) { s.Registered(driver, frameworkId, masterInfo) })
}
func (driver *MesosSchedulerDriver) frameworkReregistered(_ context.Context, from *upid.UPID, pbMsg proto.Message) {
log.V(1).Infoln("Handling Scheduler re-registered event.")
msg := pbMsg.(*mesos.FrameworkReregisteredMessage)
if driver.status == mesos.Status_DRIVER_ABORTED {
log.Infoln("Ignoring FrameworkReregisteredMessage from master, driver is aborted!")
return
}
if driver.connected {
log.Infoln("Ignoring FrameworkReregisteredMessage from master,driver is already connected!")
return
}
if !driver.masterPid.Equal(from) {
log.Warningf("ignoring framework re-registered message because it was sent from '%v' instead of leading master '%v'", from, driver.masterPid)
return
}
// TODO(vv) detect if message was from leading-master (sched.cpp)
log.Infof("Framework re-registered with ID [%s] ", msg.GetFrameworkId().GetValue())
driver.connected = true
driver.failover = false
driver.connection = uuid.NewUUID()
driver.withScheduler(func(s Scheduler) { s.Reregistered(driver, msg.GetMasterInfo()) })
}
func (driver *MesosSchedulerDriver) resourcesOffered(_ context.Context, from *upid.UPID, pbMsg proto.Message) {
log.V(2).Infoln("Handling resource offers.")
msg := pbMsg.(*mesos.ResourceOffersMessage)
if driver.status == mesos.Status_DRIVER_ABORTED {
log.Infoln("Ignoring ResourceOffersMessage, the driver is aborted!")
return
}
if !driver.connected {
log.Infoln("Ignoring ResourceOffersMessage, the driver is not connected!")
return
}
pidStrings := msg.GetPids()
if len(pidStrings) != len(msg.Offers) {
log.Errorln("Ignoring offers, Offer count does not match Slave PID count.")
return
}
for i, offer := range msg.Offers {
if pid, err := upid.Parse(pidStrings[i]); err == nil {
driver.cache.putOffer(offer, pid)
log.V(2).Infof("Cached offer %s from SlavePID %s", offer.Id.GetValue(), pid)
} else {
log.Warningf("Failed to parse offer PID '%v': %v", pid, err)
}
}
driver.withScheduler(func(s Scheduler) { s.ResourceOffers(driver, msg.Offers) })
}
func (driver *MesosSchedulerDriver) resourceOfferRescinded(_ context.Context, from *upid.UPID, pbMsg proto.Message) {
log.V(1).Infoln("Handling resource offer rescinded.")
msg := pbMsg.(*mesos.RescindResourceOfferMessage)
if driver.status == mesos.Status_DRIVER_ABORTED {
log.Infoln("Ignoring RescindResourceOfferMessage, the driver is aborted!")
return
}
if !driver.connected {
log.Infoln("Ignoring ResourceOffersMessage, the driver is not connected!")
return
}
// TODO(vv) check for leading master (see sched.cpp)
log.V(1).Infoln("Rescinding offer ", msg.OfferId.GetValue())
driver.cache.removeOffer(msg.OfferId)
driver.withScheduler(func(s Scheduler) { s.OfferRescinded(driver, msg.OfferId) })
}
func (driver *MesosSchedulerDriver) send(ctx context.Context, upid *upid.UPID, msg proto.Message) error {
c := make(chan error, 1)
go func() { c <- driver.dispatch(ctx, upid, msg) }()
select {
case <-ctx.Done():
<-c // wait for Send(...)
return ctx.Err()
case err := <-c:
return err
}
}
// statusUpdated expects to be guarded by eventLock
func (driver *MesosSchedulerDriver) statusUpdated(ctx context.Context, from *upid.UPID, pbMsg proto.Message) {
msg := pbMsg.(*mesos.StatusUpdateMessage)
if driver.status != mesos.Status_DRIVER_RUNNING {
log.V(1).Infoln("Ignoring StatusUpdate message, the driver is not running!")
return
}
if !from.Equal(driver.self) {
if !driver.connected {
log.V(1).Infoln("Ignoring StatusUpdate message, the driver is not connected!")
return
}
if !driver.masterPid.Equal(from) {
log.Warningf("ignoring status message because it was sent from '%v' instead of leading master '%v'", from, driver.masterPid)
return
}
}
log.V(2).Infof("Received status update from %q status source %q", from.String(), msg.GetPid())
status := msg.Update.GetStatus()
// see https://github.com/apache/mesos/blob/master/src/sched/sched.cpp#L887
// If the update does not have a 'uuid', it does not need
// acknowledging. However, prior to 0.23.0, the update uuid
// was required and always set. We also don't want to ACK updates
// that were internally generated. In 0.24.0, we can rely on the
// update uuid check here, until then we must still check for
// this being sent from the driver (from == UPID()) or from
// the master (pid == UPID()).
// TODO(vinod): Get rid of this logic in 0.25.0 because master
// and slave correctly set task status in 0.24.0.
if clearUUID := len(msg.Update.Uuid) == 0 || from.Equal(driver.self) || msg.GetPid() == driver.self.String(); clearUUID {
status.Uuid = nil
} else {
status.Uuid = msg.Update.Uuid
}
if driver.status == mesos.Status_DRIVER_ABORTED {
log.V(1).Infoln("Not sending StatusUpdate ACK, the driver is aborted!")
} else {
// Send StatusUpdate Acknowledgement; see above for the rules.
// Only send ACK if udpate was not from this driver and spec'd a UUID; this is compat w/ 0.23+
ackRequired := len(msg.Update.Uuid) > 0 && !from.Equal(driver.self) && msg.GetPid() != driver.self.String()
if ackRequired {
ackMsg := &mesos.StatusUpdateAcknowledgementMessage{
SlaveId: msg.Update.SlaveId,
FrameworkId: driver.frameworkInfo.Id,
TaskId: msg.Update.Status.TaskId,
Uuid: msg.Update.Uuid,
}
log.V(2).Infof("Sending ACK for status update %+v to %q", *msg.Update, from.String())
if err := driver.send(ctx, driver.masterPid, ackMsg); err != nil {
log.Errorf("Failed to send StatusUpdate ACK message: %v", err)
}
} else {
log.V(2).Infof("Not sending ACK, update is not from slave %q", from.String())
}
}
driver.withScheduler(func(s Scheduler) { s.StatusUpdate(driver, status) })
}
func (driver *MesosSchedulerDriver) exitedExecutor(_ context.Context, from *upid.UPID, pbMsg proto.Message) {
log.V(1).Infoln("Handling ExitedExceutor event.")
msg := pbMsg.(*mesos.ExitedExecutorMessage)
if driver.status == mesos.Status_DRIVER_ABORTED {
log.V(1).Infoln("Ignoring ExitedExecutor message, the driver is aborted!")
return
}
if !driver.connected {
log.V(1).Infoln("Ignoring ExitedExecutor message, the driver is not connected!")
return
}
status := msg.GetStatus()
log.V(2).Infoln("Lost executor %q from slave %q for framework %q with status %d",
msg.GetExecutorId().GetValue(),
msg.GetSlaveId().GetValue(),
msg.GetFrameworkId().GetValue(),
status)
driver.withScheduler(func(s Scheduler) { s.ExecutorLost(driver, msg.GetExecutorId(), msg.GetSlaveId(), int(status)) })
}
func (driver *MesosSchedulerDriver) slaveLost(_ context.Context, from *upid.UPID, pbMsg proto.Message) {
log.V(1).Infoln("Handling LostSlave event.")
msg := pbMsg.(*mesos.LostSlaveMessage)
if driver.status == mesos.Status_DRIVER_ABORTED {
log.V(1).Infoln("Ignoring LostSlave message, the driver is aborted!")
return
}
if !driver.connected {
log.V(1).Infoln("Ignoring LostSlave message, the driver is not connected!")
return
}
// TODO(VV) - detect leading master (see sched.cpp)
log.V(2).Infoln("Lost slave ", msg.SlaveId.GetValue())
driver.cache.removeSlavePid(msg.SlaveId)
driver.withScheduler(func(s Scheduler) { s.SlaveLost(driver, msg.SlaveId) })
}
func (driver *MesosSchedulerDriver) frameworkMessageRcvd(_ context.Context, from *upid.UPID, pbMsg proto.Message) {
log.V(1).Infoln("Handling framework message event.")
msg := pbMsg.(*mesos.ExecutorToFrameworkMessage)
if driver.status == mesos.Status_DRIVER_ABORTED {
log.V(1).Infoln("Ignoring framwork message, the driver is aborted!")
return
}
log.V(1).Infoln("Received Framwork Message ", msg.String())
driver.withScheduler(func(s Scheduler) { s.FrameworkMessage(driver, msg.ExecutorId, msg.SlaveId, string(msg.Data)) })
}
func (driver *MesosSchedulerDriver) frameworkErrorRcvd(ctx context.Context, from *upid.UPID, pbMsg proto.Message) {
log.V(1).Infoln("Handling framework error event.")
msg := pbMsg.(*mesos.FrameworkErrorMessage)
driver.fatal(ctx, msg.GetMessage())
}
// ---------------------- Interface Methods ---------------------- //
// Starts the scheduler driver.
// Returns immediately if an error occurs within start sequence.
func (driver *MesosSchedulerDriver) Start() (mesos.Status, error) {
driver.eventLock.Lock()
defer driver.eventLock.Unlock()
return driver.start()
}
// start expected to be guarded by eventLock
func (driver *MesosSchedulerDriver) start() (mesos.Status, error) {
select {
case <-driver.started:
return driver.status, errors.New("Unable to Start: driver has already been started once.")
default: // proceed
}
log.Infoln("Starting the scheduler driver...")
if driver.status != mesos.Status_DRIVER_NOT_STARTED {
return driver.status, fmt.Errorf("Unable to Start, expecting driver status %s, but is %s:", mesos.Status_DRIVER_NOT_STARTED, driver.status)
}
// Start the messenger.
if err := driver.messenger.Start(); err != nil {
log.Errorf("Scheduler failed to start the messenger: %v\n", err)
return driver.status, err
}
pid := driver.messenger.UPID()
driver.self = &pid
driver.status = mesos.Status_DRIVER_RUNNING
close(driver.started)
log.Infof("Mesos scheduler driver started with PID=%v", driver.self)
listener := detector.OnMasterChanged(func(m *mesos.MasterInfo) {
driver.messenger.Route(context.TODO(), driver.self, &mesos.InternalMasterChangeDetected{
Master: m,
})
})
if driver.masterDetector != nil {
// register with Detect() AFTER we have a self pid from the messenger, otherwise things get ugly
// because our internal messaging depends on it. detector callbacks are routed over the messenger
// bus, maintaining serial (concurrency-safe) callback execution.
log.V(1).Infof("starting master detector %T: %+v", driver.masterDetector, driver.masterDetector)
driver.masterDetector.Detect(listener)
log.V(2).Infoln("master detector started")
}
return driver.status, nil
}
// authenticate against the spec'd master pid using the configured authenticationProvider.
// the authentication process is canceled upon either cancelation of authenticating, or
// else because it timed out (see defaultAuthenticationTimeout, auth.Timeout).
//
// TODO(jdef) perhaps at some point in the future this will get pushed down into
// the messenger layer (e.g. to use HTTP-based authentication). We'd probably still
// specify the callback.Handler here, along with the user-selected authentication
// provider. Perhaps in the form of some messenger.AuthenticationConfig.
//
func (driver *MesosSchedulerDriver) authenticate(pid *upid.UPID, authenticating *authenticationAttempt) error {
log.Infof("authenticating with master %v", pid)
var (
authTimeout = defaultAuthenticationTimeout
ctx = driver.withAuthContext(context.TODO())
handler = &CredentialHandler{
pid: pid,
client: driver.self,
credential: driver.credential,
}
)
// check for authentication timeout override
if d, ok := auth.TimeoutFrom(ctx); ok {
authTimeout = d
}
ctx, cancel := context.WithTimeout(ctx, authTimeout)
ctx = auth.WithParentUPID(ctx, *driver.self)
ch := make(chan error, 1)
go func() { ch <- auth.Login(ctx, handler) }()
select {
case <-ctx.Done():
<-ch
return ctx.Err()
case <-authenticating.done:
cancel()
<-ch
return errAuthenticationCanceled
case e := <-ch:
cancel()
return e
}
}
func (driver *MesosSchedulerDriver) doReliableRegistration(maxBackoff float64) {
for {
if !driver.registerOnce() {
return
}
maxBackoff = math.Min(maxBackoff, registrationRetryIntervalMax)
// If failover timeout is present, bound the maximum backoff
// by 1/10th of the failover timeout.
if driver.failoverTimeout > 0 {
maxBackoff = math.Min(maxBackoff, driver.failoverTimeout/10.0)
}
// Determine the delay for next attempt by picking a random
// duration between 0 and 'maxBackoff' (jitter).
delay := time.Duration(maxBackoff * rand.Float64())
log.V(1).Infof("will retry registration in %v if necessary", delay)
t := time.NewTimer(delay)
defer t.Stop()
select {
case <-driver.stopCh:
return
case <-t.C:
maxBackoff *= 2
}
}
}
// registerOnce returns true if we should attempt another registration later; it is *not*
// guarded by eventLock: all access to mutable members of MesosSchedulerDriver should be
// explicitly synchronized.
func (driver *MesosSchedulerDriver) registerOnce() bool {
var (
failover bool
pid *upid.UPID
info *mesos.FrameworkInfo
ctx context.Context
)
if func() bool {
driver.eventLock.RLock()
defer driver.eventLock.RUnlock()
if driver.stopped() || driver.connected || driver.masterPid == nil || (driver.credential != nil && !driver.authenticated) {
log.V(1).Infof("skipping registration request: stopped=%v, connected=%v, authenticated=%v",
driver.stopped(), driver.connected, driver.authenticated)
return false
}
failover = driver.failover
pid = driver.masterPid
info = proto.Clone(driver.frameworkInfo).(*mesos.FrameworkInfo)
ctx = driver.context()
return true
}() {
// register framework
var message proto.Message
if len(info.GetId().GetValue()) > 0 {
// not the first time, or failing over
log.V(1).Infof("Reregistering with master: %v", pid)
message = &mesos.ReregisterFrameworkMessage{
Framework: info,
Failover: proto.Bool(failover),
}
} else {
log.V(1).Infof("Registering with master: %v", pid)
message = &mesos.RegisterFrameworkMessage{
Framework: info,
}
}
if err := driver.send(ctx, pid, message); err != nil {
log.Errorf("failed to send RegisterFramework message: %v", err)
if _, err = driver.Stop(failover); err != nil {
log.Errorf("failed to stop scheduler driver: %v", err)
}
}
return true
}
return false
}
//Join blocks until the driver is stopped.
//Should follow a call to Start()
func (driver *MesosSchedulerDriver) Join() (mesos.Status, error) {
driver.eventLock.Lock()
defer driver.eventLock.Unlock()
return driver.join()
}
// join expects to be guarded by eventLock
func (driver *MesosSchedulerDriver) join() (stat mesos.Status, err error) {
if stat = driver.status; stat != mesos.Status_DRIVER_RUNNING {
err = fmt.Errorf("Unable to Join, expecting driver status %s, but is %s", mesos.Status_DRIVER_RUNNING, stat)
return
}
timeout := 1 * time.Second
t := time.NewTimer(timeout)
defer t.Stop()
driver.eventLock.Unlock()
defer func() {
driver.eventLock.Lock()
stat = driver.status
}()
waitForDeath:
for {
select {
case <-driver.done:
break waitForDeath
case <-t.C:
}
t.Reset(timeout)
}
return
}
//Run starts and joins driver process and waits to be stopped or aborted.
func (driver *MesosSchedulerDriver) Run() (mesos.Status, error) {
driver.eventLock.Lock()
defer driver.eventLock.Unlock()
return driver.run(driver.context())
}
// run expected to be guarded by eventLock
func (driver *MesosSchedulerDriver) run(ctx context.Context) (mesos.Status, error) {
stat, err := driver.start()
if err != nil {
return driver.stop(ctx, err, false)
}
if stat != mesos.Status_DRIVER_RUNNING {
return stat, fmt.Errorf("Unable to Run, expecting driver status %s, but is %s:", mesos.Status_DRIVER_RUNNING, driver.status)
}
log.Infoln("Scheduler driver running. Waiting to be stopped.")
return driver.join()
}
//Stop stops the driver.
func (driver *MesosSchedulerDriver) Stop(failover bool) (mesos.Status, error) {
driver.eventLock.Lock()
defer driver.eventLock.Unlock()
return driver.stop(driver.context(), nil, failover)
}
// stop expects to be guarded by eventLock
func (driver *MesosSchedulerDriver) stop(ctx context.Context, cause error, failover bool) (mesos.Status, error) {
log.Infoln("Stopping the scheduler driver")
if stat := driver.status; stat != mesos.Status_DRIVER_RUNNING {
return stat, fmt.Errorf("Unable to Stop, expected driver status %s, but is %s", mesos.Status_DRIVER_RUNNING, stat)
}
if driver.connected && !failover {
// unregister the framework
log.Infoln("Unregistering the scheduler driver")
message := &mesos.UnregisterFrameworkMessage{
FrameworkId: driver.frameworkInfo.Id,
}
//TODO(jdef) this is actually a little racy: we send an 'unregister' message but then
// immediately afterward the messenger is stopped in driver._stop(). so the unregister message
// may not actually end up being sent out.
if err := driver.send(ctx, driver.masterPid, message); err != nil {
log.Errorf("Failed to send UnregisterFramework message while stopping driver: %v\n", err)
if cause == nil {
cause = &ErrDriverAborted{}
}
return driver._stop(cause, mesos.Status_DRIVER_ABORTED)
}
time.Sleep(2 * time.Second)
}
// stop messenger
return driver._stop(cause, mesos.Status_DRIVER_STOPPED)
}
// stop expects to be guarded by eventLock
func (driver *MesosSchedulerDriver) _stop(cause error, stopStatus mesos.Status) (mesos.Status, error) {
// stop messenger
defer func() {
select {
case <-driver.stopCh:
return
default:
}
close(driver.stopCh)
// decouple to avoid deadlock (avoid nested withScheduler() invocations)
go func() {
driver.eventLock.Lock()
defer driver.eventLock.Unlock()
if cause != nil {
log.V(1).Infof("Sending error via withScheduler: %v", cause)
driver.withScheduler(func(s Scheduler) { s.Error(driver, cause.Error()) })
} else {
// send a noop func, withScheduler needs to see that stopCh is closed
log.V(1).Infof("Sending kill signal to withScheduler")
driver.withScheduler(func(_ Scheduler) {})
}
}()
}()
driver.status = stopStatus
driver.connected = false
driver.connection = uuid.UUID{}
log.Info("stopping messenger")
err := driver.messenger.Stop()
log.Infof("Stop() complete with status %v error %v", stopStatus, err)
return stopStatus, err
}
func (driver *MesosSchedulerDriver) Abort() (stat mesos.Status, err error) {
driver.eventLock.Lock()
defer driver.eventLock.Unlock()
return driver.abort(driver.context(), nil)
}
// abort expects to be guarded by eventLock
func (driver *MesosSchedulerDriver) abort(ctx context.Context, cause error) (stat mesos.Status, err error) {
if driver.masterDetector != nil {
defer driver.masterDetector.Cancel()
}
log.Infof("Aborting framework [%+v]", driver.frameworkInfo.Id)
if driver.connected {
_, err = driver.stop(ctx, cause, true)
} else {
driver._stop(cause, mesos.Status_DRIVER_ABORTED)
}
stat = mesos.Status_DRIVER_ABORTED
driver.status = stat
return
}
func (driver *MesosSchedulerDriver) AcceptOffers(offerIds []*mesos.OfferID, operations []*mesos.Offer_Operation, filters *mesos.Filters) (mesos.Status, error) {
driver.eventLock.Lock()
defer driver.eventLock.Unlock()
if stat := driver.status; stat != mesos.Status_DRIVER_RUNNING {
return stat, fmt.Errorf("Unable to AcceptOffers, expected driver status %s, but got %s", mesos.Status_DRIVER_RUNNING, stat)
}
ctx := driver.context()
if !driver.connected {
err := ErrDisconnected
for _, operation := range operations {
if *operation.Type == mesos.Offer_Operation_LAUNCH {
// decouple lost task processing to avoid deadlock (avoid nested withScheduler() invocations)
operation := operation
go func() {
driver.eventLock.Lock()
defer driver.eventLock.Unlock()
for _, task := range operation.Launch.TaskInfos {
driver.pushLostTask(ctx, task, err.Error())
}
}()
}
}
log.Errorf("Failed to send LaunchTask message: %v\n", err)
return driver.status, err
}
okOperations := make([]*mesos.Offer_Operation, 0, len(operations))
for _, offerId := range offerIds {
for _, operation := range operations {
// Keep only the slave PIDs where we run tasks so we can send
// framework messages directly.
if !driver.cache.containsOffer(offerId) {
log.Warningf("Attempting to accept offers with unknown offer %s\n", offerId.GetValue())
continue
}
// Validate
switch *operation.Type {
case mesos.Offer_Operation_LAUNCH:
tasks := []*mesos.TaskInfo{}
// Set TaskInfo.executor.framework_id, if it's missing.
for _, task := range operation.Launch.TaskInfos {
newTask := *task
if newTask.Executor != nil && newTask.Executor.FrameworkId == nil {
newTask.Executor.FrameworkId = driver.frameworkInfo.Id
}
tasks = append(tasks, &newTask)
}
for _, task := range tasks {
if driver.cache.getOffer(offerId).offer.SlaveId.Equal(task.SlaveId) {
// cache the tasked slave, for future communication
pid := driver.cache.getOffer(offerId).slavePid
driver.cache.putSlavePid(task.SlaveId, pid)
} else {
log.Warningf("Attempting to launch task %s with the wrong slaveId offer %s\n", task.TaskId.GetValue(), task.SlaveId.GetValue())
}
}
operation.Launch.TaskInfos = tasks
okOperations = append(okOperations, operation)
case mesos.Offer_Operation_RESERVE:
// Only send reserved resources
filtered := util.FilterResources(operation.Reserve.Resources, func(res *mesos.Resource) bool { return res.Reservation != nil })
operation.Reserve.Resources = filtered
okOperations = append(okOperations, operation)
case mesos.Offer_Operation_UNRESERVE:
// Only send reserved resources
filtered := util.FilterResources(operation.Unreserve.Resources, func(res *mesos.Resource) bool { return res.Reservation != nil })
operation.Unreserve.Resources = filtered
okOperations = append(okOperations, operation)
case mesos.Offer_Operation_CREATE:
// Only send reserved resources disks with volumes
filtered := util.FilterResources(operation.Create.Volumes, func(res *mesos.Resource) bool {
return res.Reservation != nil && res.Disk != nil && res.GetName() == "disk"
})
operation.Create.Volumes = filtered
okOperations = append(okOperations, operation)
case mesos.Offer_Operation_DESTROY:
// Only send reserved resources disks with volumes
filtered := util.FilterResources(operation.Destroy.Volumes, func(res *mesos.Resource) bool {
return res.Reservation != nil && res.Disk != nil && res.GetName() == "disk"
})
operation.Destroy.Volumes = filtered
okOperations = append(okOperations, operation)
}
}
driver.cache.removeOffer(offerId) // if offer
}
// Accept Offers
message := &scheduler.Call{
FrameworkId: driver.frameworkInfo.Id,
Type: scheduler.Call_ACCEPT.Enum(),
Accept: &scheduler.Call_Accept{
OfferIds: offerIds,
Operations: okOperations,
Filters: filters,
},
}
if err := driver.send(ctx, driver.masterPid, message); err != nil {
for _, operation := range operations {
if *operation.Type == mesos.Offer_Operation_LAUNCH {
// decouple lost task processing to avoid deadlock (avoid nested withScheduler() invocations)
operation := operation
go func() {
driver.eventLock.Lock()
defer driver.eventLock.Unlock()
for _, task := range operation.Launch.TaskInfos {
driver.pushLostTask(ctx, task, "Unable to launch tasks: "+err.Error())
}
}()
}
}
log.Errorf("Failed to send LaunchTask message: %v\n", err)
return driver.status, err
}
return driver.status, nil
}
func (driver *MesosSchedulerDriver) LaunchTasks(offerIds []*mesos.OfferID, tasks []*mesos.TaskInfo, filters *mesos.Filters) (mesos.Status, error) {
driver.eventLock.Lock()
defer driver.eventLock.Unlock()
if stat := driver.status; stat != mesos.Status_DRIVER_RUNNING {
return stat, fmt.Errorf("Unable to LaunchTasks, expected driver status %s, but got %s", mesos.Status_DRIVER_RUNNING, stat)
}
ctx := driver.context()
// Launch tasks
if !driver.connected {
log.Infoln("Ignoring LaunchTasks message, disconnected from master.")
// decouple lost task processing to avoid deadlock (avoid nested withScheduler() invocations)
err := ErrDisconnected
go func() {
driver.eventLock.Lock()
defer driver.eventLock.Unlock()
// Send statusUpdate with status=TASK_LOST for each task.
// See sched.cpp L#823
for _, task := range tasks {
driver.pushLostTask(ctx, task, err.Error())
}
}()
return driver.status, err
}
okTasks := make([]*mesos.TaskInfo, 0, len(tasks))
// Set TaskInfo.executor.framework_id, if it's missing.
for _, task := range tasks {
if task.Executor != nil && task.Executor.FrameworkId == nil {
task.Executor.FrameworkId = driver.frameworkInfo.Id
}
okTasks = append(okTasks, task)
}
for _, offerId := range offerIds {
for _, task := range okTasks {
// Keep only the slave PIDs where we run tasks so we can send
// framework messages directly.
if driver.cache.containsOffer(offerId) {
if driver.cache.getOffer(offerId).offer.SlaveId.Equal(task.SlaveId) {
// cache the tasked slave, for future communication
pid := driver.cache.getOffer(offerId).slavePid
driver.cache.putSlavePid(task.SlaveId, pid)
} else {
log.Warningf("Attempting to launch task %s with the wrong slaveId offer %s\n", task.TaskId.GetValue(), task.SlaveId.GetValue())
}
} else {
log.Warningf("Attempting to launch task %s with unknown offer %s\n", task.TaskId.GetValue(), offerId.GetValue())
}
}
driver.cache.removeOffer(offerId) // if offer
}
// launch tasks
message := &mesos.LaunchTasksMessage{
FrameworkId: driver.frameworkInfo.Id,
OfferIds: offerIds,
Tasks: okTasks,
Filters: filters,
}
if err := driver.send(ctx, driver.masterPid, message); err != nil {
// decouple lost task processing to avoid deadlock (avoid nested withScheduler() invocations)
go func() {
driver.eventLock.Lock()
defer driver.eventLock.Unlock()
for _, task := range tasks {
driver.pushLostTask(ctx, task, "Unable to launch tasks: "+err.Error())
}
}()
log.Errorf("Failed to send LaunchTask message: %v\n", err)
return driver.status, err
}
return driver.status, nil
}
// pushLostTask expects to be guarded by eventLock
func (driver *MesosSchedulerDriver) pushLostTask(ctx context.Context, taskInfo *mesos.TaskInfo, why string) {
msg := &mesos.StatusUpdateMessage{
Update: &mesos.StatusUpdate{
FrameworkId: driver.frameworkInfo.Id,
Status: &mesos.TaskStatus{
TaskId: taskInfo.TaskId,
State: mesos.TaskState_TASK_LOST.Enum(),
Source: mesos.TaskStatus_SOURCE_MASTER.Enum(),
Message: proto.String(why),
Reason: mesos.TaskStatus_REASON_MASTER_DISCONNECTED.Enum(),
},
SlaveId: taskInfo.SlaveId,
ExecutorId: taskInfo.Executor.ExecutorId,
Timestamp: proto.Float64(float64(time.Now().Unix())),
},
Pid: proto.String(driver.self.String()),
}
// put it on internal chanel
// will cause handler to push to attached Scheduler
driver.statusUpdated(ctx, driver.self, msg)
}
func (driver *MesosSchedulerDriver) KillTask(taskId *mesos.TaskID) (mesos.Status, error) {
driver.eventLock.Lock()
defer driver.eventLock.Unlock()
if stat := driver.status; stat != mesos.Status_DRIVER_RUNNING {
return stat, fmt.Errorf("Unable to KillTask, expecting driver status %s, but got %s", mesos.Status_DRIVER_RUNNING, stat)
}
if !driver.connected {
log.Infoln("Ignoring kill task message, disconnected from master.")
return driver.status, ErrDisconnected
}
message := &mesos.KillTaskMessage{
FrameworkId: driver.frameworkInfo.Id,
TaskId: taskId,
}
if err := driver.send(driver.context(), driver.masterPid, message); err != nil {
log.Errorf("Failed to send KillTask message: %v\n", err)
return driver.status, err
}
return driver.status, nil
}
func (driver *MesosSchedulerDriver) RequestResources(requests []*mesos.Request) (mesos.Status, error) {
driver.eventLock.Lock()
defer driver.eventLock.Unlock()
if stat := driver.status; stat != mesos.Status_DRIVER_RUNNING {
return stat, fmt.Errorf("Unable to RequestResources, expecting driver status %s, but got %s", mesos.Status_DRIVER_RUNNING, stat)
}
if !driver.connected {
log.Infoln("Ignoring request resource message, disconnected from master.")
return driver.status, ErrDisconnected
}
message := &mesos.ResourceRequestMessage{
FrameworkId: driver.frameworkInfo.Id,
Requests: requests,
}
if err := driver.send(driver.context(), driver.masterPid, message); err != nil {
log.Errorf("Failed to send ResourceRequest message: %v\n", err)
return driver.status, err
}
return driver.status, nil
}
func (driver *MesosSchedulerDriver) DeclineOffer(offerId *mesos.OfferID, filters *mesos.Filters) (mesos.Status, error) {
// NOTE: don't lock eventLock here because we're delegating to LaunchTasks() and that does it for us
// launching an empty task list will decline the offer
return driver.LaunchTasks([]*mesos.OfferID{offerId}, []*mesos.TaskInfo{}, filters)
}
func (driver *MesosSchedulerDriver) ReviveOffers() (mesos.Status, error) {
driver.eventLock.Lock()
defer driver.eventLock.Unlock()
if stat := driver.status; stat != mesos.Status_DRIVER_RUNNING {
return stat, fmt.Errorf("Unable to ReviveOffers, expecting driver status %s, but got %s", mesos.Status_DRIVER_RUNNING, stat)
}
if !driver.connected {
log.Infoln("Ignoring revive offers message, disconnected from master.")
return driver.status, ErrDisconnected
}
message := &mesos.ReviveOffersMessage{
FrameworkId: driver.frameworkInfo.Id,
}
if err := driver.send(driver.context(), driver.masterPid, message); err != nil {
log.Errorf("Failed to send ReviveOffers message: %v\n", err)
return driver.status, err
}
return driver.status, nil
}
func (driver *MesosSchedulerDriver) SendFrameworkMessage(executorId *mesos.ExecutorID, slaveId *mesos.SlaveID, data string) (mesos.Status, error) {
driver.eventLock.Lock()
defer driver.eventLock.Unlock()
if stat := driver.status; stat != mesos.Status_DRIVER_RUNNING {
return stat, fmt.Errorf("Unable to SendFrameworkMessage, expecting driver status %s, but got %s", mesos.Status_DRIVER_RUNNING, stat)
}
if !driver.connected {
log.Infoln("Ignoring send framework message, disconnected from master.")
return driver.status, ErrDisconnected
}
message := &mesos.FrameworkToExecutorMessage{
SlaveId: slaveId,
FrameworkId: driver.frameworkInfo.Id,
ExecutorId: executorId,
Data: []byte(data),
}
// Use list of cached slaveIds from previous offers.
// Send frameworkMessage directly to cached slave, otherwise to master.
if driver.cache.containsSlavePid(slaveId) {
slavePid := driver.cache.getSlavePid(slaveId)
if slavePid.Equal(driver.self) {
return driver.status, nil
}
if err := driver.send(driver.context(), slavePid, message); err != nil {
log.Errorf("Failed to send framework to executor message: %v\n", err)
return driver.status, err
}
} else {
// slavePid not cached, send to master.
if err := driver.send(driver.context(), driver.masterPid, message); err != nil {
log.Errorf("Failed to send framework to executor message: %v\n", err)
return driver.status, err
}
}
return driver.status, nil
}
func (driver *MesosSchedulerDriver) ReconcileTasks(statuses []*mesos.TaskStatus) (mesos.Status, error) {
driver.eventLock.Lock()
defer driver.eventLock.Unlock()
if stat := driver.status; stat != mesos.Status_DRIVER_RUNNING {
return stat, fmt.Errorf("Unable to ReconcileTasks, expecting driver status %s, but got %s", mesos.Status_DRIVER_RUNNING, stat)
}
if !driver.connected {
log.Infoln("Ignoring send Reconcile Tasks message, disconnected from master.")
return driver.status, ErrDisconnected
}
message := &mesos.ReconcileTasksMessage{
FrameworkId: driver.frameworkInfo.Id,
Statuses: statuses,
}
if err := driver.send(driver.context(), driver.masterPid, message); err != nil {
log.Errorf("Failed to send reconcile tasks message: %v\n", err)
return driver.status, err
}
return driver.status, nil
}
// error expects to be guarded by eventLock
func (driver *MesosSchedulerDriver) fatal(ctx context.Context, err string) {
if driver.status == mesos.Status_DRIVER_ABORTED {
log.V(3).Infoln("Ignoring error message, the driver is aborted!")
return
}
driver.abort(ctx, &ErrDriverAborted{Reason: err})
}
|