| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
package main |
| 20 |
|
| 21 |
import ( |
| 22 |
"flag" |
| 23 |
"fmt" |
| 24 |
"io/ioutil" |
| 25 |
"net" |
| 26 |
"net/http" |
| 27 |
"os" |
| 28 |
"strconv" |
| 29 |
"strings" |
| 30 |
|
| 31 |
"github.com/gogo/protobuf/proto" |
| 32 |
log "github.com/golang/glog" |
| 33 |
"github.com/mesos/mesos-go/api/v0/auth" |
| 34 |
"github.com/mesos/mesos-go/api/v0/auth/sasl" |
| 35 |
"github.com/mesos/mesos-go/api/v0/auth/sasl/mech" |
| 36 |
mesos "github.com/mesos/mesos-go/api/v0/mesosproto" |
| 37 |
util "github.com/mesos/mesos-go/api/v0/mesosutil" |
| 38 |
sched "github.com/mesos/mesos-go/api/v0/scheduler" |
| 39 |
"golang.org/x/net/context" |
| 40 |
) |
| 41 |
|
| 42 |
const ( |
| 43 |
CPUS_PER_TASK = 1 |
| 44 |
MEM_PER_TASK = 128 |
| 45 |
DISK_PER_TASK = 20 |
| 46 |
defaultArtifactPort = 12345 |
| 47 |
) |
| 48 |
|
| 49 |
var ( |
| 50 |
address = flag.String("address", "127.0.0.1", "Binding address for artifact server") |
| 51 |
artifactPort = flag.Int("artifactPort", defaultArtifactPort, "Binding port for artifact server") |
| 52 |
authProvider = flag.String("mesos_authentication_provider", sasl.ProviderName, |
| 53 |
fmt.Sprintf("Authentication provider to use, default is SASL that supports mechanisms: %+v", mech.ListSupported())) |
| 54 |
master = flag.String("master", "127.0.0.1:5050", "Master address <ip:port>") |
| 55 |
executorPath = flag.String("executor", "./executor", "Path to test executor") |
| 56 |
taskCount = flag.String("task-count", "5", "Total task count to run.") |
| 57 |
role = flag.String("role", "golang", "Role for framework and tasks.") |
| 58 |
mesosAuthPrincipal = flag.String("mesos_authentication_principal", "", "Mesos authentication principal.") |
| 59 |
mesosAuthSecretFile = flag.String("mesos_authentication_secret_file", "", "Mesos authentication secret file.") |
| 60 |
execUri *string |
| 61 |
execCmd string |
| 62 |
) |
| 63 |
|
| 64 |
type ExampleScheduler struct { |
| 65 |
tasks []*ExampleTask |
| 66 |
} |
| 67 |
|
| 68 |
type ExampleTaskState int |
| 69 |
|
| 70 |
const ( |
| 71 |
InitState ExampleTaskState = iota |
| 72 |
RequestedReservationState |
| 73 |
LaunchedState |
| 74 |
FinishedState |
| 75 |
UnreservedState |
| 76 |
) |
| 77 |
|
| 78 |
type ExampleTask struct { |
| 79 |
executor *mesos.ExecutorInfo |
| 80 |
name string |
| 81 |
persistenceId string |
| 82 |
containerPath string |
| 83 |
state ExampleTaskState |
| 84 |
} |
| 85 |
|
| 86 |
func newExampleScheduler() *ExampleScheduler { |
| 87 |
total, err := strconv.Atoi(*taskCount) |
| 88 |
if err != nil { |
| 89 |
total = 5 |
| 90 |
} |
| 91 |
|
| 92 |
tasks := make([]*ExampleTask, total) |
| 93 |
for i := 0; i < total; i++ { |
| 94 |
tasks[i] = &ExampleTask{ |
| 95 |
executor: prepareExecutorInfo(fmt.Sprintf("persistent-task-executor-%d", i+1)), |
| 96 |
name: fmt.Sprintf("persistent-task-%d", i+1), |
| 97 |
persistenceId: fmt.Sprintf("persistence-%d", i+1), |
| 98 |
containerPath: "data", |
| 99 |
state: InitState, |
| 100 |
} |
| 101 |
} |
| 102 |
|
| 103 |
return &ExampleScheduler{ |
| 104 |
tasks: tasks, |
| 105 |
} |
| 106 |
} |
| 107 |
|
| 108 |
func getResource(name string, resources []*mesos.Resource, withReservation bool) float64 { |
| 109 |
filtered := util.FilterResources(resources, func(res *mesos.Resource) bool { |
| 110 |
if withReservation { |
| 111 |
return res.GetName() == name && res.Reservation != nil |
| 112 |
} |
| 113 |
return res.GetName() == name && res.Reservation == nil |
| 114 |
}) |
| 115 |
val := 0.0 |
| 116 |
for _, res := range filtered { |
| 117 |
val += res.GetScalar().GetValue() |
| 118 |
} |
| 119 |
return val |
| 120 |
} |
| 121 |
|
| 122 |
func resourcesHaveVolume(resources []*mesos.Resource, persistenceId string) bool { |
| 123 |
filtered := util.FilterResources(resources, func(res *mesos.Resource) bool { |
| 124 |
return res.GetName() == "disk" && |
| 125 |
res.Reservation != nil && |
| 126 |
res.Disk != nil && |
| 127 |
res.Disk.Persistence.GetId() == persistenceId |
| 128 |
}) |
| 129 |
return len(filtered) > 0 |
| 130 |
} |
| 131 |
|
| 132 |
func getUnreservedResources(resources []*mesos.Resource) (float64, float64, float64) { |
| 133 |
return getResource("cpus", resources, false), getResource("mem", resources, false), getResource("disk", resources, false) |
| 134 |
} |
| 135 |
|
| 136 |
func getReservedResources(resources []*mesos.Resource) (float64, float64, float64) { |
| 137 |
return getResource("cpus", resources, true), getResource("mem", resources, true), getResource("disk", resources, true) |
| 138 |
} |
| 139 |
|
| 140 |
func (sched *ExampleScheduler) Registered(driver sched.SchedulerDriver, frameworkId *mesos.FrameworkID, masterInfo *mesos.MasterInfo) { |
| 141 |
log.Infoln("Framework Registered with Master ", masterInfo) |
| 142 |
} |
| 143 |
|
| 144 |
func (sched *ExampleScheduler) Reregistered(driver sched.SchedulerDriver, masterInfo *mesos.MasterInfo) { |
| 145 |
log.Infoln("Framework Re-Registered with Master ", masterInfo) |
| 146 |
} |
| 147 |
|
| 148 |
func (sched *ExampleScheduler) Disconnected(sched.SchedulerDriver) { |
| 149 |
log.Fatalf("disconnected from master, aborting") |
| 150 |
} |
| 151 |
|
| 152 |
func (sched *ExampleScheduler) ResourceOffers(driver sched.SchedulerDriver, offers []*mesos.Offer) { |
| 153 |
for _, offer := range offers { |
| 154 |
operations := []*mesos.Offer_Operation{} |
| 155 |
resourcesToCreate := []*mesos.Resource{} |
| 156 |
resourcesToDestroy := []*mesos.Resource{} |
| 157 |
resourcesToReserve := []*mesos.Resource{} |
| 158 |
resourcesToUnreserve := []*mesos.Resource{} |
| 159 |
taskInfosToLaunch := []*mesos.TaskInfo{} |
| 160 |
totalUnreserved := 0 |
| 161 |
|
| 162 |
unreservedCpus, unreservedMem, unreservedDisk := getUnreservedResources(offer.Resources) |
| 163 |
reservedCpus, reservedMem, reservedDisk := getReservedResources(offer.Resources) |
| 164 |
|
| 165 |
log.Infoln("Received Offer <", offer.Id.GetValue(), "> with unreserved cpus=", unreservedCpus, " mem=", unreservedMem, " disk=", unreservedDisk) |
| 166 |
log.Infoln("Received Offer <", offer.Id.GetValue(), "> with reserved cpus=", reservedCpus, " mem=", reservedMem, " disk=", reservedDisk) |
| 167 |
|
| 168 |
for _, task := range sched.tasks { |
| 169 |
switch task.state { |
| 170 |
case InitState: |
| 171 |
if CPUS_PER_TASK <= unreservedCpus && |
| 172 |
MEM_PER_TASK <= unreservedMem && |
| 173 |
DISK_PER_TASK <= unreservedDisk { |
| 174 |
|
| 175 |
resourcesToReserve = append(resourcesToReserve, []*mesos.Resource{ |
| 176 |
util.NewScalarResourceWithReservation("cpus", CPUS_PER_TASK, *mesosAuthPrincipal, *role), |
| 177 |
util.NewScalarResourceWithReservation("mem", MEM_PER_TASK, *mesosAuthPrincipal, *role), |
| 178 |
util.NewScalarResourceWithReservation("disk", DISK_PER_TASK, *mesosAuthPrincipal, *role), |
| 179 |
}...) |
| 180 |
resourcesToCreate = append(resourcesToCreate, |
| 181 |
util.NewVolumeResourceWithReservation(DISK_PER_TASK, task.containerPath, task.persistenceId, mesos.Volume_RW.Enum(), *mesosAuthPrincipal, *role)) |
| 182 |
task.state = RequestedReservationState |
| 183 |
unreservedCpus = unreservedCpus - CPUS_PER_TASK |
| 184 |
unreservedMem = unreservedMem - MEM_PER_TASK |
| 185 |
unreservedDisk = unreservedDisk - DISK_PER_TASK |
| 186 |
} |
| 187 |
case RequestedReservationState: |
| 188 |
if CPUS_PER_TASK <= reservedCpus && |
| 189 |
MEM_PER_TASK <= reservedMem && |
| 190 |
DISK_PER_TASK <= reservedDisk && |
| 191 |
resourcesHaveVolume(offer.Resources, task.persistenceId) { |
| 192 |
|
| 193 |
taskId := &mesos.TaskID{ |
| 194 |
Value: proto.String(task.name), |
| 195 |
} |
| 196 |
|
| 197 |
taskInfo := &mesos.TaskInfo{ |
| 198 |
Name: proto.String("go-task-" + taskId.GetValue()), |
| 199 |
TaskId: taskId, |
| 200 |
SlaveId: offer.SlaveId, |
| 201 |
Executor: task.executor, |
| 202 |
Resources: []*mesos.Resource{ |
| 203 |
util.NewScalarResourceWithReservation("cpus", CPUS_PER_TASK, *mesosAuthPrincipal, *role), |
| 204 |
util.NewScalarResourceWithReservation("mem", MEM_PER_TASK, *mesosAuthPrincipal, *role), |
| 205 |
util.NewVolumeResourceWithReservation(DISK_PER_TASK, task.containerPath, task.persistenceId, mesos.Volume_RW.Enum(), *mesosAuthPrincipal, *role), |
| 206 |
}, |
| 207 |
} |
| 208 |
|
| 209 |
taskInfosToLaunch = append(taskInfosToLaunch, taskInfo) |
| 210 |
task.state = LaunchedState |
| 211 |
reservedCpus = reservedCpus - CPUS_PER_TASK |
| 212 |
reservedMem = reservedMem - MEM_PER_TASK |
| 213 |
reservedDisk = reservedDisk - DISK_PER_TASK |
| 214 |
|
| 215 |
log.Infof("Prepared task: %s with offer %s for launch\n", taskInfo.GetName(), offer.Id.GetValue()) |
| 216 |
} |
| 217 |
case FinishedState: |
| 218 |
resourcesToDestroy = append(resourcesToDestroy, |
| 219 |
util.NewVolumeResourceWithReservation(DISK_PER_TASK, task.containerPath, task.persistenceId, mesos.Volume_RW.Enum(), *mesosAuthPrincipal, *role)) |
| 220 |
resourcesToUnreserve = append(resourcesToUnreserve, []*mesos.Resource{ |
| 221 |
util.NewScalarResourceWithReservation("cpus", CPUS_PER_TASK, *mesosAuthPrincipal, *role), |
| 222 |
util.NewScalarResourceWithReservation("mem", MEM_PER_TASK, *mesosAuthPrincipal, *role), |
| 223 |
util.NewScalarResourceWithReservation("disk", DISK_PER_TASK, *mesosAuthPrincipal, *role), |
| 224 |
}...) |
| 225 |
task.state = UnreservedState |
| 226 |
case UnreservedState: |
| 227 |
totalUnreserved = totalUnreserved + 1 |
| 228 |
} |
| 229 |
} |
| 230 |
|
| 231 |
|
| 232 |
if len(resourcesToReserve) == 0 && len(resourcesToCreate) == 0 && len(taskInfosToLaunch) == 0 { |
| 233 |
if reservedCpus >= 0.0 { |
| 234 |
resourcesToUnreserve = append(resourcesToUnreserve, util.NewScalarResourceWithReservation("cpus", reservedCpus, *mesosAuthPrincipal, *role)) |
| 235 |
} |
| 236 |
if reservedMem >= 0.0 { |
| 237 |
resourcesToUnreserve = append(resourcesToUnreserve, util.NewScalarResourceWithReservation("mem", reservedCpus, *mesosAuthPrincipal, *role)) |
| 238 |
} |
| 239 |
if reservedDisk >= 0.0 { |
| 240 |
filtered := util.FilterResources(offer.Resources, func(res *mesos.Resource) bool { |
| 241 |
return res.GetName() == "disk" && |
| 242 |
res.Reservation != nil && |
| 243 |
res.Disk != nil |
| 244 |
}) |
| 245 |
for _, volume := range filtered { |
| 246 |
resourcesToDestroy = append(resourcesToDestroy, |
| 247 |
util.NewVolumeResourceWithReservation( |
| 248 |
volume.GetScalar().GetValue(), |
| 249 |
volume.Disk.Volume.GetContainerPath(), |
| 250 |
volume.Disk.Persistence.GetId(), |
| 251 |
volume.Disk.Volume.Mode, |
| 252 |
*mesosAuthPrincipal, |
| 253 |
*role)) |
| 254 |
} |
| 255 |
resourcesToUnreserve = append(resourcesToUnreserve, util.NewScalarResourceWithReservation("mem", reservedDisk, *mesosAuthPrincipal, *role)) |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
|
| 260 |
if len(resourcesToReserve) > 0 { |
| 261 |
operations = append(operations, util.NewReserveOperation(resourcesToReserve)) |
| 262 |
} |
| 263 |
if len(resourcesToCreate) > 0 { |
| 264 |
operations = append(operations, util.NewCreateOperation(resourcesToCreate)) |
| 265 |
} |
| 266 |
if len(resourcesToUnreserve) > 0 { |
| 267 |
operations = append(operations, util.NewUnreserveOperation(resourcesToUnreserve)) |
| 268 |
} |
| 269 |
if len(resourcesToDestroy) > 0 { |
| 270 |
operations = append(operations, util.NewDestroyOperation(resourcesToDestroy)) |
| 271 |
} |
| 272 |
if len(taskInfosToLaunch) > 0 { |
| 273 |
operations = append(operations, util.NewLaunchOperation(taskInfosToLaunch)) |
| 274 |
} |
| 275 |
|
| 276 |
log.Infoln("Accepting offers with ", len(operations), "operations for offer", offer.Id.GetValue()) |
| 277 |
refuseSeconds := 5.0 |
| 278 |
if len(operations) == 0 { |
| 279 |
refuseSeconds = 5.0 |
| 280 |
} |
| 281 |
driver.AcceptOffers([]*mesos.OfferID{offer.Id}, operations, &mesos.Filters{RefuseSeconds: proto.Float64(refuseSeconds)}) |
| 282 |
|
| 283 |
if totalUnreserved >= len(sched.tasks) { |
| 284 |
log.Infoln("Total tasks completed and unreserved, stopping framework.") |
| 285 |
driver.Stop(false) |
| 286 |
} |
| 287 |
} |
| 288 |
} |
| 289 |
|
| 290 |
func (sched *ExampleScheduler) StatusUpdate(driver sched.SchedulerDriver, status *mesos.TaskStatus) { |
| 291 |
log.Infoln("Status update: task", status.TaskId.GetValue(), " is in state ", status.State.Enum().String()) |
| 292 |
for _, task := range sched.tasks { |
| 293 |
if task.name == status.TaskId.GetValue() && |
| 294 |
(status.GetState() == mesos.TaskState_TASK_FINISHED || |
| 295 |
status.GetState() == mesos.TaskState_TASK_LOST || |
| 296 |
status.GetState() == mesos.TaskState_TASK_KILLED || |
| 297 |
status.GetState() == mesos.TaskState_TASK_FAILED || |
| 298 |
status.GetState() == mesos.TaskState_TASK_ERROR) { |
| 299 |
|
| 300 |
|
| 301 |
task.state = FinishedState |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
if status.GetState() == mesos.TaskState_TASK_LOST || |
| 306 |
status.GetState() == mesos.TaskState_TASK_KILLED || |
| 307 |
status.GetState() == mesos.TaskState_TASK_FAILED || |
| 308 |
status.GetState() == mesos.TaskState_TASK_ERROR { |
| 309 |
log.Infoln( |
| 310 |
"Task", status.TaskId.GetValue(), |
| 311 |
"is in unexpected state", status.State.String(), |
| 312 |
"with message", status.GetMessage(), |
| 313 |
". Unreserving resources", |
| 314 |
) |
| 315 |
} |
| 316 |
} |
| 317 |
|
| 318 |
func (sched *ExampleScheduler) OfferRescinded(_ sched.SchedulerDriver, oid *mesos.OfferID) { |
| 319 |
log.Errorf("offer rescinded: %v", oid) |
| 320 |
} |
| 321 |
func (sched *ExampleScheduler) FrameworkMessage(_ sched.SchedulerDriver, eid *mesos.ExecutorID, sid *mesos.SlaveID, msg string) { |
| 322 |
log.Errorf("framework message from executor %q slave %q: %q", eid, sid, msg) |
| 323 |
} |
| 324 |
func (sched *ExampleScheduler) SlaveLost(_ sched.SchedulerDriver, sid *mesos.SlaveID) { |
| 325 |
log.Errorf("slave lost: %v", sid) |
| 326 |
} |
| 327 |
func (sched *ExampleScheduler) ExecutorLost(_ sched.SchedulerDriver, eid *mesos.ExecutorID, sid *mesos.SlaveID, code int) { |
| 328 |
log.Errorf("executor %q lost on slave %q code %d", eid, sid, code) |
| 329 |
} |
| 330 |
func (sched *ExampleScheduler) Error(_ sched.SchedulerDriver, err string) { |
| 331 |
log.Errorf("Scheduler received error: %v", err) |
| 332 |
} |
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
func init() { |
| 337 |
flag.Parse() |
| 338 |
log.Infoln("Initializing the Example Scheduler...") |
| 339 |
} |
| 340 |
|
| 341 |
|
| 342 |
func serveExecutorArtifact(path string) (*string, string) { |
| 343 |
serveFile := func(pattern string, filename string) { |
| 344 |
http.HandleFunc(pattern, func(w http.ResponseWriter, r *http.Request) { |
| 345 |
http.ServeFile(w, r, filename) |
| 346 |
}) |
| 347 |
} |
| 348 |
|
| 349 |
|
| 350 |
pathSplit := strings.Split(path, "/") |
| 351 |
var base string |
| 352 |
if len(pathSplit) > 0 { |
| 353 |
base = pathSplit[len(pathSplit)-1] |
| 354 |
} else { |
| 355 |
base = path |
| 356 |
} |
| 357 |
serveFile("/"+base, path) |
| 358 |
|
| 359 |
hostURI := fmt.Sprintf("http://%s:%d/%s", *address, *artifactPort, base) |
| 360 |
log.V(2).Infof("Hosting artifact '%s' at '%s'", path, hostURI) |
| 361 |
|
| 362 |
return &hostURI, base |
| 363 |
} |
| 364 |
|
| 365 |
func prepareExecutorInfo(id string) *mesos.ExecutorInfo { |
| 366 |
executorUris := []*mesos.CommandInfo_URI{} |
| 367 |
executorUris = append(executorUris, &mesos.CommandInfo_URI{Value: execUri, Executable: proto.Bool(true)}) |
| 368 |
|
| 369 |
|
| 370 |
v := 0 |
| 371 |
if f := flag.Lookup("v"); f != nil && f.Value != nil { |
| 372 |
if vstr := f.Value.String(); vstr != "" { |
| 373 |
if vi, err := strconv.ParseInt(vstr, 10, 32); err == nil { |
| 374 |
v = int(vi) |
| 375 |
} |
| 376 |
} |
| 377 |
} |
| 378 |
executorCommand := fmt.Sprintf("./%s -logtostderr=true -v=%d", execCmd, v) |
| 379 |
|
| 380 |
go http.ListenAndServe(fmt.Sprintf("%s:%d", *address, *artifactPort), nil) |
| 381 |
log.V(2).Info("Serving executor artifacts...") |
| 382 |
|
| 383 |
|
| 384 |
return &mesos.ExecutorInfo{ |
| 385 |
ExecutorId: util.NewExecutorID(id), |
| 386 |
Name: proto.String("Test Executor (Go)"), |
| 387 |
Source: proto.String("go_test"), |
| 388 |
Command: &mesos.CommandInfo{ |
| 389 |
Value: proto.String(executorCommand), |
| 390 |
Uris: executorUris, |
| 391 |
}, |
| 392 |
} |
| 393 |
} |
| 394 |
|
| 395 |
func parseIP(address string) net.IP { |
| 396 |
addr, err := net.LookupIP(address) |
| 397 |
if err != nil { |
| 398 |
log.Fatal(err) |
| 399 |
} |
| 400 |
if len(addr) < 1 { |
| 401 |
log.Fatalf("failed to parse IP from address '%v'", address) |
| 402 |
} |
| 403 |
return addr[0] |
| 404 |
} |
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
func main() { |
| 409 |
execUri, execCmd = serveExecutorArtifact(*executorPath) |
| 410 |
|
| 411 |
|
| 412 |
fwinfo := &mesos.FrameworkInfo{ |
| 413 |
User: proto.String(""), |
| 414 |
Name: proto.String("Test Framework (Go)"), |
| 415 |
Role: proto.String(*role), |
| 416 |
} |
| 417 |
|
| 418 |
cred := (*mesos.Credential)(nil) |
| 419 |
if *mesosAuthPrincipal != "" { |
| 420 |
fwinfo.Principal = proto.String(*mesosAuthPrincipal) |
| 421 |
cred = &mesos.Credential{ |
| 422 |
Principal: proto.String(*mesosAuthPrincipal), |
| 423 |
} |
| 424 |
if *mesosAuthSecretFile != "" { |
| 425 |
_, err := os.Stat(*mesosAuthSecretFile) |
| 426 |
if err != nil { |
| 427 |
log.Fatal("missing secret file: ", err.Error()) |
| 428 |
} |
| 429 |
secret, err := ioutil.ReadFile(*mesosAuthSecretFile) |
| 430 |
if err != nil { |
| 431 |
log.Fatal("failed to read secret file: ", err.Error()) |
| 432 |
} |
| 433 |
cred.Secret = proto.String(string(secret)) |
| 434 |
} |
| 435 |
} |
| 436 |
bindingAddress := parseIP(*address) |
| 437 |
config := sched.DriverConfig{ |
| 438 |
Scheduler: newExampleScheduler(), |
| 439 |
Framework: fwinfo, |
| 440 |
Master: *master, |
| 441 |
Credential: cred, |
| 442 |
BindingAddress: bindingAddress, |
| 443 |
WithAuthContext: func(ctx context.Context) context.Context { |
| 444 |
ctx = auth.WithLoginProvider(ctx, *authProvider) |
| 445 |
ctx = sasl.WithBindingAddress(ctx, bindingAddress) |
| 446 |
return ctx |
| 447 |
}, |
| 448 |
} |
| 449 |
driver, err := sched.NewMesosSchedulerDriver(config) |
| 450 |
|
| 451 |
if err != nil { |
| 452 |
log.Errorln("Unable to create a SchedulerDriver ", err.Error()) |
| 453 |
} |
| 454 |
|
| 455 |
if stat, err := driver.Run(); err != nil { |
| 456 |
log.Infof("Framework stopped with status %s and error: %s\n", stat.String(), err.Error()) |
| 457 |
} |
| 458 |
log.Infof("framework terminating") |
| 459 |
} |