2017-01-06 23:40:05 -08:00
package schedulers
import (
2017-01-08 19:08:46 -08:00
"bitbucket.org/sunybingcloud/electron/constants"
2017-01-06 23:40:05 -08:00
"bitbucket.org/sunybingcloud/electron/def"
2017-01-28 21:56:23 -05:00
powCap "bitbucket.org/sunybingcloud/electron/powerCapping"
2017-01-08 19:08:46 -08:00
"bitbucket.org/sunybingcloud/electron/rapl"
2017-01-28 19:45:47 -05:00
"bitbucket.org/sunybingcloud/electron/utilities/mesosUtils"
"bitbucket.org/sunybingcloud/electron/utilities/offerUtils"
2017-01-06 23:40:05 -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"
2017-01-08 19:08:46 -08:00
"math"
2017-01-06 23:40:05 -08:00
"os"
"sort"
"sync"
"time"
)
// Decides if to take an offer or not
2017-02-27 18:37:53 -05:00
func ( s * BPSWMaxMinProacCC ) takeOffer ( offer * mesos . Offer , task def . Task ,
totalCPU , totalRAM , totalWatts float64 ) bool {
2017-01-28 19:40:39 -05:00
cpus , mem , watts := offerUtils . OfferAgg ( offer )
2017-01-06 23:40:05 -08:00
//TODO: Insert watts calculation here instead of taking them as a parameter
2017-02-09 18:05:38 -05:00
wattsConsideration , err := def . WattsToConsider ( task , s . classMapWatts , offer )
if err != nil {
// Error in determining wattsConsideration
log . Fatal ( err )
}
2017-02-27 18:37:53 -05:00
if ( cpus >= ( totalCPU + task . CPU ) ) && ( mem >= ( totalRAM + task . RAM ) ) &&
( ! s . wattsAsAResource || ( watts >= ( totalWatts + wattsConsideration ) ) ) {
2017-01-06 23:40:05 -08:00
return true
}
return false
}
2017-02-09 18:05:38 -05:00
type BPSWMaxMinProacCC struct {
2017-02-09 20:27:18 -05:00
base // Type embedding to inherit common functions
taskMonitor map [ string ] [ ] def . Task
availablePower map [ string ] float64
totalPower map [ string ] float64
capper * powCap . ClusterwideCapper
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.
2017-01-06 23:40:05 -08:00
}
// New electron scheduler
2017-02-09 18:41:40 -05:00
func NewBPSWMaxMinProacCC ( tasks [ ] def . Task , wattsAsAResource bool , schedTracePrefix string , classMapWatts bool ) * BPSWMaxMinProacCC {
2017-01-06 23:40:05 -08:00
sort . Sort ( def . WattsSorter ( tasks ) )
logFile , err := os . Create ( "./" + schedTracePrefix + "_schedTrace.log" )
if err != nil {
log . Fatal ( err )
}
2017-02-09 18:05:38 -05:00
s := & BPSWMaxMinProacCC {
2017-02-09 20:27:18 -05:00
base : base {
tasks : tasks ,
wattsAsAResource : wattsAsAResource ,
classMapWatts : classMapWatts ,
Shutdown : make ( chan struct { } ) ,
Done : make ( chan struct { } ) ,
PCPLog : make ( chan struct { } ) ,
running : make ( map [ string ] map [ string ] bool ) ,
RecordPCP : false ,
schedTrace : log . New ( logFile , "" , log . LstdFlags ) ,
} ,
taskMonitor : make ( map [ string ] [ ] def . Task ) ,
availablePower : make ( map [ string ] float64 ) ,
totalPower : make ( map [ string ] float64 ) ,
capper : powCap . GetClusterwideCapperInstance ( ) ,
ticker : time . NewTicker ( 10 * time . Second ) ,
recapTicker : time . NewTicker ( 20 * time . Second ) ,
isCapping : false ,
isRecapping : false ,
2017-01-06 23:40:05 -08:00
}
return s
}
// mutex
var bpMaxMinProacCCMutex sync . Mutex
2017-02-09 18:05:38 -05:00
func ( s * BPSWMaxMinProacCC ) newTask ( offer * mesos . Offer , task def . Task ) * mesos . TaskInfo {
2017-01-06 23:40:05 -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 ) ,
}
2017-02-09 18:41:40 -05:00
if s . wattsAsAResource {
2017-02-09 18:05:38 -05:00
if wattsToConsider , err := def . WattsToConsider ( task , s . classMapWatts , offer ) ; err == nil {
log . Printf ( "Watts considered for host[%s] and task[%s] = %f" , * offer . Hostname , task . Name , wattsToConsider )
resources = append ( resources , mesosutil . NewScalarResource ( "watts" , wattsToConsider ) )
} else {
// Error in determining wattsConsideration
log . Fatal ( err )
}
2017-01-06 23:40:05 -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
} ,
} ,
}
}
// go routine to cap the entire cluster in regular intervals of time.
2017-01-14 20:04:37 -05:00
var bpMaxMinProacCCCapValue = 0.0 // initial value to indicate that we haven't capped the cluster yet.
2017-01-08 19:08:46 -08:00
var bpMaxMinProacCCNewCapValue = 0.0 // newly computed cap value
2017-02-09 18:05:38 -05:00
func ( s * BPSWMaxMinProacCC ) startCapping ( ) {
2017-01-06 23:40:05 -08:00
go func ( ) {
for {
select {
case <- s . ticker . C :
2017-01-08 19:08:46 -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 23:40:05 -08:00
bpMaxMinProacCCMutex . Lock ( )
2017-01-08 19:08:46 -08:00
if s . isCapping {
if int ( math . Floor ( bpMaxMinProacCCNewCapValue + 0.5 ) ) != int ( math . Floor ( bpMaxMinProacCCCapValue + 0.5 ) ) {
// updating cap value
bpMaxMinProacCCCapValue = bpMaxMinProacCCNewCapValue
if bpMaxMinProacCCCapValue > 0.0 {
2017-03-23 22:16:05 -04:00
for host , _ := range constants . Hosts {
2017-01-08 19:08:46 -08:00
// Rounding cap value to nearest int
2017-02-15 19:22:56 -05:00
if err := rapl . Cap ( host , "rapl" , float64 ( int ( math . Floor ( bpMaxMinProacCCCapValue + 0.5 ) ) ) ) ; err != nil {
2017-01-08 19:08:46 -08:00
log . Println ( err )
}
}
log . Printf ( "Capped the cluster to %d" , int ( math . Floor ( bpMaxMinProacCCCapValue + 0.5 ) ) )
2017-01-06 23:40:05 -08:00
}
}
}
bpMaxMinProacCCMutex . Unlock ( )
}
}
} ( )
}
// go routine to recap the entire cluster in regular intervals of time.
var bpMaxMinProacCCRecapValue = 0.0 // The cluster-wide cap value when recapping.
2017-02-09 18:05:38 -05:00
func ( s * BPSWMaxMinProacCC ) startRecapping ( ) {
2017-01-06 23:40:05 -08:00
go func ( ) {
for {
select {
case <- s . recapTicker . C :
bpMaxMinProacCCMutex . Lock ( )
// If stopped performing cluster-wide capping, then we need to recap.
if s . isRecapping && bpMaxMinProacCCRecapValue > 0.0 {
2017-03-23 22:16:05 -04:00
for host , _ := range constants . Hosts {
2017-01-06 23:40:05 -08:00
// Rounding the recap value to the nearest int
2017-02-15 19:22:56 -05:00
if err := rapl . Cap ( host , "rapl" , float64 ( int ( math . Floor ( bpMaxMinProacCCRecapValue + 0.5 ) ) ) ) ; err != nil {
2017-01-06 23:40:05 -08:00
log . Println ( err )
}
}
log . Printf ( "Capped the cluster to %d" , int ( math . Floor ( bpMaxMinProacCCRecapValue + 0.5 ) ) )
}
// Setting the recapping to false
s . isRecapping = false
bpMaxMinProacCCMutex . Unlock ( )
}
}
} ( )
}
// Stop cluster-wide capping
2017-02-09 18:05:38 -05:00
func ( s * BPSWMaxMinProacCC ) stopCapping ( ) {
2017-01-06 23:40:05 -08:00
if s . isCapping {
log . Println ( "Stopping the cluster-wide capping." )
s . ticker . Stop ( )
bpMaxMinProacCCMutex . Lock ( )
s . isCapping = false
s . isRecapping = true
bpMaxMinProacCCMutex . Unlock ( )
}
}
// Stop the cluster-wide recapping
2017-02-09 18:05:38 -05:00
func ( s * BPSWMaxMinProacCC ) stopRecapping ( ) {
2017-01-06 23:40:05 -08:00
// If not capping, then definitely recapping.
if ! s . isCapping && s . isRecapping {
log . Println ( "Stopping the cluster-wide re-capping." )
s . recapTicker . Stop ( )
bpMaxMinProacCCMutex . Lock ( )
s . isRecapping = false
bpMaxMinProacCCMutex . Unlock ( )
}
}
// Determine if the remaining space inside of the offer is enough for
// the task we need to create. If it is, create TaskInfo and return it.
2017-02-11 01:23:07 -05:00
func ( s * BPSWMaxMinProacCC ) CheckFit (
i int ,
2017-01-06 23:40:05 -08:00
task def . Task ,
2017-02-09 18:05:38 -05:00
wattsConsideration float64 ,
2017-01-06 23:40:05 -08:00
offer * mesos . Offer ,
totalCPU * float64 ,
totalRAM * float64 ,
totalWatts * float64 ) ( bool , * mesos . TaskInfo ) {
// Does the task fit
2017-02-27 18:37:53 -05:00
if s . takeOffer ( offer , task , * totalCPU , * totalRAM , * totalWatts ) {
2017-01-06 23:40:05 -08:00
// Capping the cluster if haven't yet started
if ! s . isCapping {
bpMaxMinProacCCMutex . Lock ( )
s . isCapping = true
bpMaxMinProacCCMutex . Unlock ( )
s . startCapping ( )
}
tempCap , err := s . capper . FCFSDeterminedCap ( s . totalPower , & task )
if err == nil {
bpMaxMinProacCCMutex . Lock ( )
2017-01-08 19:08:46 -08:00
bpMaxMinProacCCNewCapValue = tempCap
2017-01-06 23:40:05 -08:00
bpMaxMinProacCCMutex . Unlock ( )
} else {
log . Println ( "Failed to determine new cluster-wide cap:" )
log . Println ( err )
}
2017-02-09 18:05:38 -05:00
* totalWatts += wattsConsideration
2017-01-06 23:40:05 -08:00
* totalCPU += task . CPU
* totalRAM += task . RAM
log . Println ( "Co-Located with: " )
coLocated ( s . running [ offer . GetSlaveId ( ) . GoString ( ) ] )
taskToSchedule := s . newTask ( offer , task )
fmt . Println ( "Inst: " , * task . Instances )
s . schedTrace . Print ( offer . GetHostname ( ) + ":" + taskToSchedule . GetTaskId ( ) . GetValue ( ) )
* 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
s . stopCapping ( )
s . startRecapping ( ) // Load changes after every task finishes and hence, we need to change the capping of the cluster.
close ( s . Shutdown )
}
}
return true , taskToSchedule
}
return false , nil
}
2017-02-09 18:05:38 -05:00
func ( s * BPSWMaxMinProacCC ) ResourceOffers ( driver sched . SchedulerDriver , offers [ ] * mesos . Offer ) {
2017-01-06 23:40:05 -08:00
log . Printf ( "Received %d resource offers" , len ( offers ) )
// retrieving the available power for all the hosts in the offers.
for _ , offer := range offers {
2017-03-25 18:06:39 -04:00
offerUtils . UpdateEnvironment ( offer )
2017-01-28 19:40:39 -05:00
_ , _ , offerWatts := offerUtils . OfferAgg ( offer )
2017-01-06 23:40:05 -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 23:40:05 -08:00
log . Println ( "Number of tasks still running: " , s . tasksRunning )
continue
default :
}
tasks := [ ] * mesos . TaskInfo { }
offerTaken := false
totalWatts := 0.0
totalCPU := 0.0
totalRAM := 0.0
// Assumes s.tasks is ordered in non-decreasing median max peak order
// Attempt to schedule a single instance of the heaviest workload available first
// Start from the back until one fits
2017-01-08 19:08:46 -08:00
for i := len ( s . tasks ) - 1 ; i >= 0 ; i -- {
2017-01-06 23:40:05 -08:00
task := s . tasks [ i ]
2017-02-09 18:05:38 -05:00
wattsConsideration , err := def . WattsToConsider ( task , s . classMapWatts , offer )
if err != nil {
// Error in determining wattsConsideration
log . Fatal ( err )
}
2017-02-09 22:48:34 -05:00
// Don't take offer if it doesn't match our task's host requirement
if offerUtils . HostMismatch ( * offer . Hostname , task . Host ) {
continue
2017-01-06 23:40:05 -08:00
}
// TODO: Fix this so index doesn't need to be passed
2017-02-09 18:05:38 -05:00
taken , taskToSchedule := s . CheckFit ( i , task , wattsConsideration , offer ,
& totalCPU , & totalRAM , & totalWatts )
2017-01-06 23:40:05 -08:00
if taken {
offerTaken = true
tasks = append ( tasks , taskToSchedule )
break
}
}
// Pack the rest of the offer with the smallest tasks
2017-02-09 18:05:38 -05:00
for i := 0 ; i < len ( s . tasks ) ; i ++ {
task := s . tasks [ i ]
wattsConsideration , err := def . WattsToConsider ( task , s . classMapWatts , offer )
if err != nil {
// Error in determining wattsConsideration
log . Fatal ( err )
}
2017-01-06 23:40:05 -08:00
2017-02-09 22:48:34 -05:00
// Don't take offer if it doesn't match our task's host requirement
if offerUtils . HostMismatch ( * offer . Hostname , task . Host ) {
continue
2017-01-06 23:40:05 -08:00
}
for * task . Instances > 0 {
// TODO: Fix this so index doesn't need to be passed
2017-02-09 18:05:38 -05:00
taken , taskToSchedule := s . CheckFit ( i , task , wattsConsideration , offer ,
& totalCPU , & totalRAM , & totalWatts )
2017-01-06 23:40:05 -08:00
if taken {
offerTaken = true
tasks = append ( tasks , taskToSchedule )
} else {
break // Continue on to next task
}
}
}
if offerTaken {
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 23:40:05 -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 23:40:05 -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 23:40:05 -08:00
}
}
}
2017-02-09 18:05:38 -05:00
func ( s * BPSWMaxMinProacCC ) StatusUpdate ( driver sched . SchedulerDriver , status * mesos . TaskStatus ) {
2017-01-06 23:40:05 -08: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 )
// 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-08 19:08:46 -08:00
//tempCap, err := s.capper.CleverRecap(s.totalPower, s.taskMonitor, *status.TaskId.Value)
2017-01-06 23:40:05 -08:00
if err == nil {
// If new determined recap value is different from the current recap value, then we need to recap.
if int ( math . Floor ( tempCap + 0.5 ) ) != int ( math . Floor ( bpMaxMinProacCCRecapValue + 0.5 ) ) {
bpMaxMinProacCCRecapValue = tempCap
bpMaxMinProacCCMutex . Lock ( )
s . isRecapping = true
bpMaxMinProacCCMutex . Unlock ( )
log . Printf ( "Determined re-cap value: %f\n" , bpMaxMinProacCCRecapValue )
} else {
bpMaxMinProacCCMutex . Lock ( )
s . isRecapping = false
bpMaxMinProacCCMutex . 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 )
}