2017-01-06 16:01:45 -08:00
package schedulers
import (
"bitbucket.org/sunybingcloud/electron/constants"
"bitbucket.org/sunybingcloud/electron/def"
"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:45 -08:00
"errors"
"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"
2017-01-06 16:01:45 -08:00
"sort"
"strings"
"sync"
"time"
)
// Decides if to take offer or not
func ( s * BPSWClassMapWattsPistonCapping ) 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:45 -08: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
}
type BPSWClassMapWattsPistonCapping struct {
2017-01-06 16:49:00 -08:00
base // Type embedded to inherit common functions
2017-01-06 16:01:45 -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
totalPower map [ string ] float64
ignoreWatts bool
ticker * time . Ticker
isCapping bool
// First set of PCP values are garbage values, signal to logger to start recording when we're
// about to schedule the new task
RecordPCP bool
// This channel is closed when the program receives an interrupt,
// signalling that program should shutdown
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:45 -08:00
}
// New electron scheduler
2017-01-06 16:49:00 -08:00
func NewBPSWClassMapWattsPistonCapping ( tasks [ ] def . Task , ignoreWatts bool , schedTracePrefix string ) * BPSWClassMapWattsPistonCapping {
2017-01-06 16:01:45 -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:45 -08:00
s := & BPSWClassMapWattsPistonCapping {
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 ) ,
totalPower : make ( map [ string ] float64 ) ,
RecordPCP : false ,
ticker : time . NewTicker ( 5 * time . Second ) ,
isCapping : false ,
2017-01-06 16:49:00 -08:00
schedTrace : log . New ( logFile , "" , log . LstdFlags ) ,
2017-01-06 16:01:45 -08:00
}
return s
}
2017-01-31 15:33:31 -05:00
func ( s * BPSWClassMapWattsPistonCapping ) newTask ( offer * mesos . Offer , task def . Task , powerClass string ) * mesos . TaskInfo {
2017-01-06 16:01:45 -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 )
}
2017-01-06 21:05:56 -08:00
// Add task to list of tasks running on node
s . running [ offer . GetSlaveId ( ) . GoString ( ) ] [ taskName ] = true
2017-01-06 16:01:45 -08:00
// 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 list of tasks running on node
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:45 -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 * BPSWClassMapWattsPistonCapping ) Disconnected ( sched . SchedulerDriver ) {
2017-01-06 16:49:00 -08:00
// Need to stop the capping process
s . ticker . Stop ( )
bpswClassMapWattsPistonMutex . Lock ( )
s . isCapping = false
bpswClassMapWattsPistonMutex . Unlock ( )
2017-01-06 16:01:45 -08:00
log . Println ( "Framework disconnected with master" )
}
// mutex
var bpswClassMapWattsPistonMutex sync . Mutex
2017-01-06 21:05:56 -08:00
// go routine to cap each node in the cluster at regular intervals of time
2017-01-06 16:01:45 -08:00
var bpswClassMapWattsPistonCapValues = make ( map [ string ] float64 )
// Storing the previous cap value for each host so as to not repeatedly cap the nodes to the same value. (reduces overhead)
var bpswClassMapWattsPistonPreviousRoundedCapValues = make ( map [ string ] int )
func ( s * BPSWClassMapWattsPistonCapping ) startCapping ( ) {
go func ( ) {
for {
select {
case <- s . ticker . C :
// Need to cap each node
bpswClassMapWattsPistonMutex . Lock ( )
for host , capValue := range bpswClassMapWattsPistonCapValues {
roundedCapValue := int ( math . Floor ( capValue + 0.5 ) )
// has the cap value changed
if previousRoundedCap , ok := bpswClassMapWattsPistonPreviousRoundedCapValues [ host ] ; ok {
if previousRoundedCap != roundedCapValue {
if err := rapl . Cap ( host , "rapl" , roundedCapValue ) ; err != nil {
log . Println ( err )
} else {
2017-01-14 17:21:46 -05:00
log . Printf ( "Capped [%s] at %d" , host , roundedCapValue )
2017-01-06 16:01:45 -08:00
}
bpswClassMapWattsPistonPreviousRoundedCapValues [ host ] = roundedCapValue
}
} else {
if err := rapl . Cap ( host , "rapl" , roundedCapValue ) ; err != nil {
log . Println ( err )
} else {
2017-01-14 17:21:46 -05:00
log . Printf ( "Capped [%s] at %d" , host , roundedCapValue )
2017-01-06 16:01:45 -08:00
}
bpswClassMapWattsPistonPreviousRoundedCapValues [ host ] = roundedCapValue
}
}
bpswClassMapWattsPistonMutex . Unlock ( )
}
}
} ( )
}
// Stop the capping
func ( s * BPSWClassMapWattsPistonCapping ) stopCapping ( ) {
if s . isCapping {
log . Println ( "Stopping the capping." )
s . ticker . Stop ( )
bpswClassMapWattsPistonMutex . Lock ( )
s . isCapping = false
bpswClassMapWattsPistonMutex . Unlock ( )
}
}
func ( s * BPSWClassMapWattsPistonCapping ) ResourceOffers ( driver sched . SchedulerDriver , offers [ ] * mesos . Offer ) {
log . Printf ( "Received %d resource offers" , len ( offers ) )
// retrieving the total power for each host in the offers.
for _ , offer := range offers {
if _ , ok := s . totalPower [ * offer . Hostname ] ; ! ok {
2017-01-28 19:40:39 -05:00
_ , _ , offerWatts := offerUtils . OfferAgg ( offer )
2017-01-06 16:01:45 -08:00
s . totalPower [ * offer . Hostname ] = offerWatts
}
}
// Displaying the totalPower
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:45 -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:45 -08:00
taken := false
totalWatts := 0.0
totalCPU := 0.0
totalRAM := 0.0
// Store the partialLoad for host corresponding to this offer
// Once we can't fit any more tasks, we update the capValue for this host with partialLoad and then launch the fitted tasks.
partialLoad := 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:45 -08: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
}
}
for * task . Instances > 0 {
2017-01-31 15:33:31 -05:00
powerClass := offerUtils . PowerClass ( offer )
2017-01-06 16:01:45 -08:00
// Does the task fit
// OR lazy evaluation. If ignoreWatts 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:45 -08:00
( offerCPU >= ( totalCPU + task . CPU ) ) &&
( offerRAM >= ( totalRAM + task . RAM ) ) {
2017-01-08 18:58:39 -08:00
// Start piston capping if haven't started yet
if ! s . isCapping {
s . isCapping = true
s . startCapping ( )
}
2017-01-31 15:33:31 -05:00
fmt . Println ( "Watts being used: " , task . ClassToWatts [ powerClass ] )
2017-01-06 16:01:45 -08:00
taken = true
2017-01-31 15:33:31 -05:00
totalWatts += task . ClassToWatts [ powerClass ]
2017-01-06 16:01:45 -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:45 -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:45 -08:00
* task . Instances --
partialLoad += ( ( task . Watts * constants . CapMargin ) / s . totalPower [ * offer . Hostname ] ) * 100
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" )
close ( s . Shutdown )
}
}
} else {
break // Continue on to the next task
}
}
}
if taken {
// Updating the cap value for offer.Hostname
bpswClassMapWattsPistonMutex . Lock ( )
bpswClassMapWattsPistonCapValues [ * offer . Hostname ] += partialLoad
bpswClassMapWattsPistonMutex . Unlock ( )
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:45 -08:00
} else {
// If there was no match for task
log . Println ( "There is not enough resources to launch task: " )
2017-01-28 19:40:39 -05:00
cpus , mem , watts := offerUtils . OfferAgg ( offer )
2017-01-06 16:01:45 -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:45 -08:00
}
}
}
// Remove finished task from the taskMonitor
func ( s * BPSWClassMapWattsPistonCapping ) deleteFromTaskMonitor ( finishedTaskID string ) ( def . Task , string , error ) {
hostOfFinishedTask := ""
indexOfFinishedTask := - 1
found := false
var finishedTask def . Task
for host , tasks := range s . taskMonitor {
for i , task := range tasks {
if task . TaskID == finishedTaskID {
hostOfFinishedTask = host
indexOfFinishedTask = i
found = true
}
}
if found {
break
}
}
if hostOfFinishedTask != "" && indexOfFinishedTask != - 1 {
finishedTask = s . taskMonitor [ hostOfFinishedTask ] [ indexOfFinishedTask ]
log . Printf ( "Removing task with TaskID [%s] from the list of running tasks\n" ,
s . taskMonitor [ hostOfFinishedTask ] [ indexOfFinishedTask ] . TaskID )
s . taskMonitor [ hostOfFinishedTask ] = append ( s . taskMonitor [ hostOfFinishedTask ] [ : indexOfFinishedTask ] ,
s . taskMonitor [ hostOfFinishedTask ] [ indexOfFinishedTask + 1 : ] ... )
} else {
return finishedTask , hostOfFinishedTask , errors . New ( "Finished Task not present in TaskMonitor" )
}
return finishedTask , hostOfFinishedTask , nil
}
func ( s * BPSWClassMapWattsPistonCapping ) StatusUpdate ( driver sched . SchedulerDriver , status * mesos . TaskStatus ) {
log . Printf ( "Received task status [%s] for task [%s]\n" , NameFor ( status . State ) , * status . TaskId . Value )
if * status . State == mesos . TaskState_TASK_RUNNING {
bpswClassMapWattsPistonMutex . Lock ( )
s . tasksRunning ++
bpswClassMapWattsPistonMutex . Unlock ( )
} else if IsTerminal ( status . State ) {
delete ( s . running [ status . GetSlaveId ( ) . GoString ( ) ] , * status . TaskId . Value )
// Deleting the task from the taskMonitor
finishedTask , hostOfFinishedTask , err := s . deleteFromTaskMonitor ( * status . TaskId . Value )
if err != nil {
log . Println ( err )
}
// Need to update the cap values for host of the finishedTask
bpswClassMapWattsPistonMutex . Lock ( )
bpswClassMapWattsPistonCapValues [ hostOfFinishedTask ] -= ( ( finishedTask . Watts * constants . CapMargin ) / s . totalPower [ hostOfFinishedTask ] ) * 100
// Checking to see if the cap value has become 0, in which case we uncap the host.
if int ( math . Floor ( bpswClassMapWattsPistonCapValues [ hostOfFinishedTask ] + 0.5 ) ) == 0 {
bpswClassMapWattsPistonCapValues [ hostOfFinishedTask ] = 100
}
s . tasksRunning --
bpswClassMapWattsPistonMutex . Unlock ( )
if s . tasksRunning == 0 {
select {
case <- s . Shutdown :
s . stopCapping ( )
close ( s . Done )
default :
}
}
}
log . Printf ( "DONE: Task status [%s] for task [%s]" , NameFor ( status . State ) , * status . TaskId . Value )
}