2016-10-13 17:15:09 -04:00
package schedulers
import (
2016-11-10 19:50:00 -05:00
"bitbucket.org/sunybingcloud/electron/def"
2017-01-28 19:40:39 -05:00
"bitbucket.org/sunybingcloud/electron/utilities/mesosUtils"
"bitbucket.org/sunybingcloud/electron/utilities/offerUtils"
2016-10-13 17:15:09 -04:00
"fmt"
"github.com/golang/protobuf/proto"
mesos "github.com/mesos/mesos-go/mesosproto"
"github.com/mesos/mesos-go/mesosutil"
sched "github.com/mesos/mesos-go/scheduler"
"log"
2017-01-14 20:04:37 -05:00
"os"
2016-10-15 21:24:14 -04:00
"sort"
2016-10-13 17:15:09 -04:00
"strings"
"time"
)
// Decides if to take an offer or not
2016-10-16 14:48:31 -04:00
func ( * BinPackSortedWatts ) takeOffer ( offer * mesos . Offer , task def . Task ) bool {
2016-10-13 17:15:09 -04:00
2017-01-28 19:40:39 -05:00
cpus , mem , watts := offerUtils . OfferAgg ( offer )
2016-10-13 17:15:09 -04:00
//TODO: Insert watts calculation here instead of taking them as a parameter
if cpus >= task . CPU && mem >= task . RAM && watts >= task . Watts {
return true
}
return false
}
2016-10-16 14:48:31 -04:00
type BinPackSortedWatts struct {
2017-01-14 19:44:50 -05:00
base // Type embedded to inherit common functions
2016-10-13 17:15:09 -04:00
tasksCreated int
tasksRunning int
tasks [ ] def . Task
metrics map [ string ] def . Metric
running map [ string ] map [ string ] bool
ignoreWatts bool
// First set of PCP values are garbage values, signal to logger to start recording when we're
// about to schedule a new task
RecordPCP bool
// This channel is closed when the program receives an interrupt,
// signalling that the program should shut down.
Shutdown chan struct { }
// This channel is closed after shutdown is closed, and only when all
// outstanding tasks have been cleaned up
Done chan struct { }
// Controls when to shutdown pcp logging
PCPLog chan struct { }
2017-01-14 19:44:50 -05:00
schedTrace * log . Logger
2016-10-13 17:15:09 -04:00
}
// New electron scheduler
2017-01-14 19:44:50 -05:00
func NewBinPackSortedWatts ( tasks [ ] def . Task , ignoreWatts bool , schedTracePrefix string ) * BinPackSortedWatts {
2016-10-15 21:24:14 -04:00
sort . Sort ( def . WattsSorter ( tasks ) )
2016-10-13 17:15:09 -04:00
2017-01-14 19:44:50 -05:00
logFile , err := os . Create ( "./" + schedTracePrefix + "_schedTrace.log" )
if err != nil {
log . Fatal ( err )
}
2016-10-16 14:48:31 -04:00
s := & BinPackSortedWatts {
2016-10-13 17:15:09 -04:00
tasks : tasks ,
ignoreWatts : ignoreWatts ,
Shutdown : make ( chan struct { } ) ,
Done : make ( chan struct { } ) ,
PCPLog : make ( chan struct { } ) ,
running : make ( map [ string ] map [ string ] bool ) ,
RecordPCP : false ,
2017-01-14 19:44:50 -05:00
schedTrace : log . New ( logFile , "" , log . LstdFlags ) ,
2016-10-13 17:15:09 -04:00
}
return s
}
2016-10-16 14:48:31 -04:00
func ( s * BinPackSortedWatts ) newTask ( offer * mesos . Offer , task def . Task ) * mesos . TaskInfo {
2016-10-13 17:15:09 -04:00
taskName := fmt . Sprintf ( "%s-%d" , task . Name , * task . Instances )
s . tasksCreated ++
if ! s . RecordPCP {
// Turn on logging
s . RecordPCP = true
time . Sleep ( 1 * time . Second ) // Make sure we're recording by the time the first task starts
}
// If this is our first time running into this Agent
if _ , ok := s . running [ offer . GetSlaveId ( ) . GoString ( ) ] ; ! ok {
s . running [ offer . GetSlaveId ( ) . GoString ( ) ] = make ( map [ string ] bool )
}
// Add task to list of tasks running on node
s . running [ offer . GetSlaveId ( ) . GoString ( ) ] [ taskName ] = true
resources := [ ] * mesos . Resource {
mesosutil . NewScalarResource ( "cpus" , task . CPU ) ,
mesosutil . NewScalarResource ( "mem" , task . RAM ) ,
}
if ! s . ignoreWatts {
resources = append ( resources , mesosutil . NewScalarResource ( "watts" , task . Watts ) )
}
return & mesos . TaskInfo {
Name : proto . String ( taskName ) ,
TaskId : & mesos . TaskID {
Value : proto . String ( "electron-" + taskName ) ,
} ,
SlaveId : offer . SlaveId ,
Resources : resources ,
Command : & mesos . CommandInfo {
Value : proto . String ( task . CMD ) ,
} ,
Container : & mesos . ContainerInfo {
Type : mesos . ContainerInfo_DOCKER . Enum ( ) ,
Docker : & mesos . ContainerInfo_DockerInfo {
Image : proto . String ( task . Image ) ,
Network : mesos . ContainerInfo_DockerInfo_BRIDGE . Enum ( ) , // Run everything isolated
} ,
} ,
}
}
2016-10-16 14:48:31 -04:00
func ( s * BinPackSortedWatts ) ResourceOffers ( driver sched . SchedulerDriver , offers [ ] * mesos . Offer ) {
2016-10-13 17:15:09 -04:00
log . Printf ( "Received %d resource offers" , len ( offers ) )
for _ , offer := range offers {
select {
case <- s . Shutdown :
log . Println ( "Done scheduling tasks: declining offer on [" , offer . GetHostname ( ) , "]" )
2017-01-28 19:40:39 -05:00
driver . DeclineOffer ( offer . Id , mesosUtils . LongFilter )
2016-10-13 17:15:09 -04:00
log . Println ( "Number of tasks still running: " , s . tasksRunning )
continue
default :
}
tasks := [ ] * mesos . TaskInfo { }
2017-01-28 19:40:39 -05:00
offer_cpu , offer_ram , offer_watts := offerUtils . OfferAgg ( offer )
2016-10-13 17:15:09 -04:00
2017-02-04 16:59:25 -05:00
offerTaken := false
2016-10-15 21:24:14 -04:00
totalWatts := 0.0
2016-10-16 14:48:31 -04:00
totalCPU := 0.0
totalRAM := 0.0
2017-01-14 19:44:50 -05:00
for i := 0 ; i < len ( s . tasks ) ; i ++ {
task := s . tasks [ i ]
2016-10-13 17:15:09 -04:00
// Check host if it exists
if task . Host != "" {
// Don't take offer if it doesn't match our task's host requirement
if ! strings . HasPrefix ( * offer . Hostname , task . Host ) {
continue
}
}
2016-10-15 21:24:14 -04:00
for * task . Instances > 0 {
// Does the task fit
2016-10-16 14:48:31 -04:00
if ( s . ignoreWatts || offer_watts >= ( totalWatts + task . Watts ) ) &&
( offer_cpu >= ( totalCPU + task . CPU ) ) &&
( offer_ram >= ( totalRAM + task . RAM ) ) {
2016-10-13 17:15:09 -04:00
2017-02-04 16:59:25 -05:00
offerTaken = true
2016-10-15 21:24:14 -04:00
totalWatts += task . Watts
2016-10-16 14:48:31 -04:00
totalCPU += task . CPU
totalRAM += task . RAM
2016-10-15 21:24:14 -04:00
log . Println ( "Co-Located with: " )
coLocated ( s . running [ offer . GetSlaveId ( ) . GoString ( ) ] )
2017-01-14 19:44:50 -05:00
taskToSchedule := s . newTask ( offer , task )
tasks = append ( tasks , taskToSchedule )
2016-10-13 17:15:09 -04:00
2016-10-15 21:24:14 -04:00
fmt . Println ( "Inst: " , * task . Instances )
2017-01-14 19:44:50 -05:00
s . schedTrace . Print ( offer . GetHostname ( ) + ":" + taskToSchedule . GetTaskId ( ) . GetValue ( ) )
2016-10-15 21:24:14 -04:00
* task . Instances --
2016-10-13 17:15:09 -04:00
2016-10-15 21:24:14 -04:00
if * task . Instances <= 0 {
// All instances of task have been scheduled, remove it
s . tasks = append ( s . tasks [ : i ] , s . tasks [ i + 1 : ] ... )
2016-10-13 17:15:09 -04:00
2016-10-15 21:24:14 -04:00
if len ( s . tasks ) <= 0 {
log . Println ( "Done scheduling all tasks" )
close ( s . Shutdown )
}
2016-10-13 17:15:09 -04:00
}
2016-10-15 21:24:14 -04:00
} else {
break // Continue on to next offer
2016-10-13 17:15:09 -04:00
}
}
}
2017-02-04 16:59:25 -05:00
if offerTaken {
2016-10-15 21:24:14 -04:00
log . Printf ( "Starting on [%s]\n" , offer . GetHostname ( ) )
2017-01-28 19:40:39 -05:00
driver . LaunchTasks ( [ ] * mesos . OfferID { offer . Id } , tasks , mesosUtils . DefaultFilter )
2016-10-15 21:24:14 -04:00
} else {
// If there was no match for the task
2016-10-13 17:15:09 -04:00
fmt . Println ( "There is not enough resources to launch a task:" )
2017-01-28 19:40:39 -05:00
cpus , mem , watts := offerUtils . OfferAgg ( offer )
2016-10-13 17:15:09 -04:00
log . Printf ( "<CPU: %f, RAM: %f, Watts: %f>\n" , cpus , mem , watts )
2017-01-28 19:40:39 -05:00
driver . DeclineOffer ( offer . Id , mesosUtils . DefaultFilter )
2016-10-13 17:15:09 -04:00
}
}
}
2016-10-16 14:48:31 -04:00
func ( s * BinPackSortedWatts ) StatusUpdate ( driver sched . SchedulerDriver , status * mesos . TaskStatus ) {
2016-10-13 17:15:09 -04:00
log . Printf ( "Received task status [%s] for task [%s]" , NameFor ( status . State ) , * status . TaskId . Value )
if * status . State == mesos . TaskState_TASK_RUNNING {
s . tasksRunning ++
} else if IsTerminal ( status . State ) {
delete ( s . running [ status . GetSlaveId ( ) . GoString ( ) ] , * status . TaskId . Value )
s . tasksRunning --
if s . tasksRunning == 0 {
select {
case <- s . Shutdown :
close ( s . Done )
default :
}
}
}
log . Printf ( "DONE: Task status [%s] for task [%s]" , NameFor ( status . State ) , * status . TaskId . Value )
}