package schedulers
import (
"bitbucket.org/sunybingcloud/elektron/constants"
"bitbucket.org/sunybingcloud/elektron/def"
"errors"
elecLogDef "bitbucket.org/sunybingcloud/elektron/logging/def"
"bitbucket.org/sunybingcloud/elektron/utilities"
"bitbucket.org/sunybingcloud/elektron/utilities/mesosUtils"
mesos "github.com/mesos/mesos-go/api/v0/mesosproto"
sched "github.com/mesos/mesos-go/api/v0/scheduler"
"log"
)
func coLocated(tasks map[string]bool, s baseScheduler) {
for task := range tasks {
s.Log(elecLogDef.GENERAL, task)
}
s.Log(elecLogDef.GENERAL, "---------------------")
// Get the powerClass of the given hostname.
func hostToPowerClass(hostName string) string {
for powerClass, hosts := range constants.PowerClasses {
if _, ok := hosts[hostName]; ok {
return powerClass
return ""
// scheduler policy options to help initialize schedulers
type schedPolicyOption func(e ElectronScheduler) error
func WithSchedPolicy(schedPolicyName string) schedPolicyOption {
return func(s ElectronScheduler) error {
if schedPolicy, ok := SchedPolicies[schedPolicyName]; !ok {
return errors.New("Incorrect scheduling policy.")
} else {
s.(*baseScheduler).curSchedPolicy = schedPolicy
return nil
func WithTasks(ts []def.Task) schedPolicyOption {
if ts == nil {
return errors.New("Task[] is empty.")
s.(*baseScheduler).tasks = ts
func WithWattsAsAResource(waar bool) schedPolicyOption {
s.(*baseScheduler).wattsAsAResource = waar
func WithClassMapWatts(cmw bool) schedPolicyOption {
s.(*baseScheduler).classMapWatts = cmw
func WithRecordPCP(recordPCP *bool) schedPolicyOption {
s.(*baseScheduler).RecordPCP = recordPCP
func WithShutdown(shutdown chan struct{}) schedPolicyOption {
if shutdown == nil {
return errors.New("Shutdown channel is nil.")
s.(*baseScheduler).Shutdown = shutdown
func WithDone(done chan struct{}) schedPolicyOption {
if done == nil {
return errors.New("Done channel is nil.")
s.(*baseScheduler).Done = done
func WithPCPLog(pcpLog chan struct{}) schedPolicyOption {
if pcpLog == nil {
return errors.New("PCPLog channel is nil.")
s.(*baseScheduler).PCPLog = pcpLog
func WithLoggingChannels(lmt chan elecLogDef.LogMessageType, msg chan string) schedPolicyOption {
s.(*baseScheduler).logMsgType = lmt
s.(*baseScheduler).logMsg = msg
func WithSchedPolSwitchEnabled(enableSchedPolicySwitch bool) schedPolicyOption {
s.(*baseScheduler).schedPolSwitchEnabled = enableSchedPolicySwitch
// Launch tasks.
func LaunchTasks(offerIDs []*mesos.OfferID, tasksToLaunch []*mesos.TaskInfo, driver sched.SchedulerDriver) error {
driver.LaunchTasks(offerIDs, tasksToLaunch, mesosUtils.DefaultFilter)
// Update resource availability
var err error
for _, task := range tasksToLaunch {
err = utilities.ResourceAvailabilityUpdate("ON_TASK_ACTIVE_STATE", *task.TaskId, *task.SlaveId)
if err != nil {
log.Println(err)
return err