2017-01-06 16:01:18 -08:00
package schedulers
import (
"bitbucket.org/sunybingcloud/electron/constants"
"bitbucket.org/sunybingcloud/electron/def"
2017-01-28 21:56:23 -05:00
powCap "bitbucket.org/sunybingcloud/electron/powerCapping"
2017-01-06 16:01:18 -08:00
"bitbucket.org/sunybingcloud/electron/rapl"
2017-01-28 19:40:39 -05:00
"bitbucket.org/sunybingcloud/electron/utilities/mesosUtils"
"bitbucket.org/sunybingcloud/electron/utilities/offerUtils"
2017-01-06 16:01:18 -08: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"
"math"
2017-01-06 16:49:00 -08:00
"os"
"sort"
2017-01-06 16:01:18 -08:00
"strings"
"sync"
"time"
)
// Decides if to take an offer or not
func ( * BPSWClassMapWattsProacCC ) takeOffer ( offer * mesos . Offer , task def . Task ) bool {
2017-01-28 19:40:39 -05:00
cpus , mem , watts := offerUtils . OfferAgg ( offer )
2017-01-06 16:01:18 -08:00
// TODO: Insert watts calculation here instead of taking them as parameter
if cpus >= task . CPU && mem >= task . RAM && watts >= task . Watts {
return true
}
return false
}
type BPSWClassMapWattsProacCC struct {
2017-01-06 16:49:00 -08:00
base // Type embedding to inherit common functions
2017-01-06 16:01:18 -08:00
tasksCreated int
tasksRunning int
tasks [ ] def . Task
metrics map [ string ] def . Metric
running map [ string ] map [ string ] bool
taskMonitor map [ string ] [ ] def . Task
availablePower map [ string ] float64
totalPower map [ string ] float64
ignoreWatts bool
2017-01-28 21:56:23 -05:00
capper * powCap . ClusterwideCapper
2017-01-06 16:01:18 -08:00
ticker * time . Ticker
recapTicker * time . Ticker
isCapping bool // indicate whether we are currently performing cluster-wide capping.
isRecapping bool // indicate whether we are currently performing cluster-wide recapping.
// 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-06 16:49:00 -08:00
schedTrace * log . Logger
2017-01-06 16:01:18 -08:00
}
// New electron scheduler
2017-01-06 16:49:00 -08:00
func NewBPSWClassMapWattsProacCC ( tasks [ ] def . Task , ignoreWatts bool , schedTracePrefix string ) * BPSWClassMapWattsProacCC {
2017-01-06 16:01:18 -08:00
sort . Sort ( def . WattsSorter ( tasks ) )
2017-01-06 16:49:00 -08:00
logFile , err := os . Create ( "./" + schedTracePrefix + "_schedTrace.log" )
if err != nil {
log . Fatal ( err )
}
2017-01-06 16:01:18 -08:00
s := & BPSWClassMapWattsProacCC {
tasks : tasks ,
ignoreWatts : ignoreWatts ,
Shutdown : make ( chan struct { } ) ,
Done : make ( chan struct { } ) ,
PCPLog : make ( chan struct { } ) ,
running : make ( map [ string ] map [ string ] bool ) ,
taskMonitor : make ( map [ string ] [ ] def . Task ) ,
availablePower : make ( map [ string ] float64 ) ,
totalPower : make ( map [ string ] float64 ) ,
RecordPCP : false ,
2017-01-28 21:56:23 -05:00
capper : powCap . GetClusterwideCapperInstance ( ) ,
2017-01-06 16:01:18 -08:00
ticker : time . NewTicker ( 10 * time . Second ) ,
recapTicker : time . NewTicker ( 20 * time . Second ) ,
isCapping : false ,
isRecapping : false ,
2017-01-06 16:49:00 -08:00
schedTrace : log . New ( logFile , "" , log . LstdFlags ) ,
2017-01-06 16:01:18 -08:00
}
return s
}
// mutex
var bpswClassMapWattsProacCCMutex sync . Mutex
2017-01-31 15:33:31 -05:00
func ( s * BPSWClassMapWattsProacCC ) newTask ( offer * mesos . Offer , task def . Task , powerClass string ) * mesos . TaskInfo {
2017-01-06 16:01:18 -08: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 )
}
// Setting the task ID to the task. This is done so that we can consider each task to be different,
// even though they have the same parameters.
task . SetTaskID ( * proto . String ( "electron-" + taskName ) )
// Add task to the list of tasks running on the node.
s . running [ offer . GetSlaveId ( ) . GoString ( ) ] [ taskName ] = true
if len ( s . taskMonitor [ * offer . Hostname ] ) == 0 {
s . taskMonitor [ * offer . Hostname ] = [ ] def . Task { task }
} else {
s . taskMonitor [ * offer . Hostname ] = append ( s . taskMonitor [ * offer . Hostname ] , task )
}
resources := [ ] * mesos . Resource {
mesosutil . NewScalarResource ( "cpus" , task . CPU ) ,
mesosutil . NewScalarResource ( "mem" , task . RAM ) ,
}
if ! s . ignoreWatts {
2017-01-31 15:33:31 -05:00
resources = append ( resources , mesosutil . NewScalarResource ( "watts" , task . ClassToWatts [ powerClass ] ) )
2017-01-06 16:01:18 -08:00
}
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
} ,
} ,
}
}
func ( s * BPSWClassMapWattsProacCC ) Disconnected ( sched . SchedulerDriver ) {
// Need to stop the capping process
s . ticker . Stop ( )
s . recapTicker . Stop ( )
bpswClassMapWattsProacCCMutex . Lock ( )
s . isCapping = false
bpswClassMapWattsProacCCMutex . Unlock ( )
log . Println ( "Framework disconnected with master" )
}
// go routine to cap the entire cluster in regular intervals of time.
2017-01-28 19:40:39 -05:00
var bpswClassMapWattsProacCCCapValue = 0.0 // initial value to indicate that we haven't capped the cluster yet.
2017-01-08 18:58:00 -08:00
var bpswClassMapWattsProacCCNewCapValue = 0.0 // newly computed cap value
2017-01-06 16:01:18 -08:00
func ( s * BPSWClassMapWattsProacCC ) startCapping ( ) {
go func ( ) {
for {
select {
case <- s . ticker . C :
2017-01-08 18:58:00 -08:00
// Need to cap the cluster only if new cap value different from old cap value.
// This way we don't unnecessarily cap the cluster.
2017-01-06 16:01:18 -08:00
bpswClassMapWattsProacCCMutex . Lock ( )
2017-01-08 18:58:00 -08:00
if s . isCapping {
2017-01-08 19:08:02 -08:00
if int ( math . Floor ( bpswClassMapWattsProacCCNewCapValue + 0.5 ) ) != int ( math . Floor ( bpswClassMapWattsProacCCCapValue + 0.5 ) ) {
2017-01-08 18:58:00 -08:00
// updating cap value
bpswClassMapWattsProacCCCapValue = bpswClassMapWattsProacCCNewCapValue
if bpswClassMapWattsProacCCCapValue > 0.0 {
for _ , host := range constants . Hosts {
// Rounding cap value to nearest int
if err := rapl . Cap ( host , "rapl" , int ( math . Floor ( bpswClassMapWattsProacCCCapValue + 0.5 ) ) ) ; err != nil {
log . Println ( err )
}
}
log . Printf ( "Capped the cluster to %d" , int ( math . Floor ( bpswClassMapWattsProacCCCapValue + 0.5 ) ) )
2017-01-06 16:01:18 -08:00
}
}
}
bpswClassMapWattsProacCCMutex . Unlock ( )
}
}
} ( )
}
// go routine to recap the entire cluster in regular intervals of time.
2017-01-08 18:58:00 -08:00
var bpswClassMapWattsProacCCRecapValue = 0.0 // The cluster-wide cap value when recapping
2017-01-06 16:01:18 -08:00
func ( s * BPSWClassMapWattsProacCC ) startRecapping ( ) {
go func ( ) {
for {
select {
case <- s . recapTicker . C :
bpswClassMapWattsProacCCMutex . Lock ( )
// If stopped performing cluster wide capping, then we need to recap
2017-01-08 18:58:00 -08:00
if s . isRecapping && bpswClassMapWattsProacCCRecapValue > 0.0 {
2017-01-06 16:01:18 -08:00
for _ , host := range constants . Hosts {
// Rounding capValue to the nearest int
2017-01-14 20:04:37 -05:00
if err := rapl . Cap ( host , "rapl" , int ( math . Floor ( bpswClassMapWattsProacCCRecapValue + 0.5 ) ) ) ; err != nil {
2017-01-06 16:01:18 -08:00
log . Println ( err )
}
}
2017-01-14 20:04:37 -05:00
log . Printf ( "Recapping the cluster to %d" , int ( math . Floor ( bpswClassMapWattsProacCCRecapValue + 0.5 ) ) )
2017-01-06 16:01:18 -08:00
}
// Setting recapping to false
s . isRecapping = false
bpswClassMapWattsProacCCMutex . Unlock ( )
}
}
} ( )
}
// Stop cluster wide capping
func ( s * BPSWClassMapWattsProacCC ) stopCapping ( ) {
if s . isCapping {
log . Println ( "Stopping the cluster-wide capping." )
s . ticker . Stop ( )
bpswClassMapWattsProacCCMutex . Lock ( )
s . isCapping = false
s . isRecapping = true
bpswClassMapWattsProacCCMutex . Unlock ( )
}
}
// Stop the cluster wide recapping
func ( s * BPSWClassMapWattsProacCC ) stopRecapping ( ) {
// If not capping, then definitely recapping.
if ! s . isCapping && s . isRecapping {
log . Println ( "Stopping the cluster-wide re-capping." )
s . recapTicker . Stop ( )
bpswClassMapWattsProacCCMutex . Lock ( )
s . isRecapping = false
bpswClassMapWattsProacCCMutex . Unlock ( )
}
}
func ( s * BPSWClassMapWattsProacCC ) ResourceOffers ( driver sched . SchedulerDriver , offers [ ] * mesos . Offer ) {
log . Printf ( "Received %d resource offers" , len ( offers ) )
// retrieving the available power for all the hosts in the offers.
for _ , offer := range offers {
2017-01-28 19:40:39 -05:00
_ , _ , offerWatts := offerUtils . OfferAgg ( offer )
2017-01-06 16:01:18 -08:00
s . availablePower [ * offer . Hostname ] = offerWatts
// setting total power if the first time
if _ , ok := s . totalPower [ * offer . Hostname ] ; ! ok {
s . totalPower [ * offer . Hostname ] = offerWatts
}
}
for host , tpower := range s . totalPower {
log . Printf ( "TotalPower[%s] = %f" , host , tpower )
}
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 )
2017-01-06 16:01:18 -08:00
log . Println ( "Number of tasks still running: " , s . tasksRunning )
continue
default :
}
tasks := [ ] * mesos . TaskInfo { }
2017-01-28 19:40:39 -05:00
offerCPU , offerRAM , offerWatts := offerUtils . OfferAgg ( offer )
2017-01-06 16:01:18 -08:00
taken := false
totalWatts := 0.0
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 ]
2017-01-06 16:01:18 -08:00
// Check host if it exists
if task . Host != "" {
// Don't take offer it it doesn't match our task's host requirement.
if strings . HasPrefix ( * offer . Hostname , task . Host ) {
continue
}
}
for * task . Instances > 0 {
2017-01-31 15:33:31 -05:00
powerClass := offerUtils . PowerClass ( offer )
2017-01-06 16:01:18 -08:00
// Does the task fit
// OR Lazy evaluation. If ignore watts is set to true, second statement won't
// be evaluated.
2017-01-31 15:33:31 -05:00
if ( s . ignoreWatts || ( offerWatts >= ( totalWatts + task . ClassToWatts [ powerClass ] ) ) ) &&
2017-01-06 16:01:18 -08:00
( offerCPU >= ( totalCPU + task . CPU ) ) &&
( offerRAM >= ( totalRAM + task . RAM ) ) {
// Capping the cluster if haven't yet started
if ! s . isCapping {
bpswClassMapWattsProacCCMutex . Lock ( )
s . isCapping = true
bpswClassMapWattsProacCCMutex . Unlock ( )
s . startCapping ( )
}
2017-01-31 15:33:31 -05:00
fmt . Println ( "Watts being used: " , task . ClassToWatts [ powerClass ] )
2017-01-06 16:01:18 -08:00
tempCap , err := s . capper . FCFSDeterminedCap ( s . totalPower , & task )
if err == nil {
bpswClassMapWattsProacCCMutex . Lock ( )
2017-01-08 18:58:00 -08:00
bpswClassMapWattsProacCCNewCapValue = tempCap
2017-01-06 16:01:18 -08:00
bpswClassMapWattsProacCCMutex . Unlock ( )
} else {
log . Println ( "Failed to determine new cluster-wide cap:" )
log . Println ( err )
}
taken = true
2017-01-31 15:33:31 -05:00
totalWatts += task . ClassToWatts [ powerClass ]
2017-01-06 16:01:18 -08:00
totalCPU += task . CPU
totalRAM += task . RAM
log . Println ( "Co-Located with: " )
coLocated ( s . running [ offer . GetSlaveId ( ) . GoString ( ) ] )
2017-01-31 15:33:31 -05:00
taskToSchedule := s . newTask ( offer , task , powerClass )
2017-01-06 16:49:00 -08:00
tasks = append ( tasks , taskToSchedule )
2017-01-06 16:01:18 -08:00
fmt . Println ( "Inst: " , * task . Instances )
2017-01-06 16:49:00 -08:00
s . schedTrace . Print ( offer . GetHostname ( ) + ":" + taskToSchedule . GetTaskId ( ) . GetValue ( ) )
2017-01-06 16:01:18 -08:00
* task . Instances --
if * task . Instances <= 0 {
// All instances of task have been scheduled, remove it
s . tasks = append ( s . tasks [ : i ] , s . tasks [ i + 1 : ] ... )
if len ( s . tasks ) == 0 {
log . Println ( "Done scheduling all tasks." )
// Need to stop the cluster wide capping as there aren't any more tasks to schedule.
s . stopCapping ( )
s . startRecapping ( ) // Load changes after every task finishes and hence, we need to change the capping of the cluster.
close ( s . Shutdown )
}
}
} else {
break // Continue on to the next task
}
}
}
if taken {
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 )
2017-01-06 16:01:18 -08:00
} else {
// If there was no match for the task
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 )
2017-01-06 16:01:18 -08: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 )
2017-01-06 16:01:18 -08:00
}
}
}
func ( s * BPSWClassMapWattsProacCC ) StatusUpdate ( driver sched . SchedulerDriver , status * mesos . TaskStatus ) {
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 )
// Need to remove the task from the window
s . capper . TaskFinished ( * status . TaskId . Value )
// Determining the new cluster wide recap value
2017-01-08 19:15:53 -08:00
//tempCap, err := s.capper.NaiveRecap(s.totalPower, s.taskMonitor, *status.TaskId.Value)
2017-01-06 16:49:00 -08:00
tempCap , err := s . capper . CleverRecap ( s . totalPower , s . taskMonitor , * status . TaskId . Value )
2017-01-06 16:01:18 -08:00
if err == nil {
// If new determined cap value is different from the current recap value, then we need to recap
2017-01-14 20:04:37 -05:00
if int ( math . Floor ( tempCap + 0.5 ) ) != int ( math . Floor ( bpswClassMapWattsProacCCRecapValue + 0.5 ) ) {
2017-01-08 18:58:00 -08:00
bpswClassMapWattsProacCCRecapValue = tempCap
2017-01-06 16:01:18 -08:00
bpswClassMapWattsProacCCMutex . Lock ( )
s . isRecapping = true
bpswClassMapWattsProacCCMutex . Unlock ( )
2017-01-08 18:58:00 -08:00
log . Printf ( "Determined re-cap value: %f\n" , bpswClassMapWattsProacCCRecapValue )
2017-01-06 16:01:18 -08:00
} else {
bpswClassMapWattsProacCCMutex . Lock ( )
s . isRecapping = false
bpswClassMapWattsProacCCMutex . Unlock ( )
}
} else {
log . Println ( err )
}
s . tasksRunning --
if s . tasksRunning == 0 {
select {
case <- s . Shutdown :
// Need to stop the cluster-wide recapping
s . stopRecapping ( )
close ( s . Done )
default :
}
}
}
log . Printf ( "DONE: Task status [%s] for task[%s]" , NameFor ( status . State ) , * status . TaskId . Value )
}