2018-10-06 20:03:14 -07:00
// Copyright (C) 2018 spdf
//
// This file is part of Elektron.
//
// Elektron is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Elektron is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Elektron. If not, see <http://www.gnu.org/licenses/>.
//
2016-10-13 17:15:09 -04:00
package def
2016-09-16 19:06:53 -04:00
import (
"encoding/json"
2018-09-30 18:23:38 -07:00
"os"
2018-01-19 17:46:35 -05:00
mesos "github.com/mesos/mesos-go/api/v0/mesosproto"
2016-09-16 19:06:53 -04:00
"github.com/pkg/errors"
2018-09-30 18:23:38 -07:00
"gitlab.com/spdf/elektron/constants"
"gitlab.com/spdf/elektron/utilities/offerUtils"
2016-09-16 19:06:53 -04:00
)
type Task struct {
2017-01-03 20:57:25 -05:00
Name string ` json:"name" `
CPU float64 ` json:"cpu" `
RAM float64 ` json:"ram" `
Watts float64 ` json:"watts" `
Image string ` json:"image" `
CMD string ` json:"cmd" `
Instances * int ` json:"inst" `
Host string ` json:"host" `
TaskID string ` json:"taskID" `
2016-12-23 21:04:15 -05:00
ClassToWatts map [ string ] float64 ` json:"class_to_watts" `
2016-09-16 19:06:53 -04:00
}
func TasksFromJSON ( uri string ) ( [ ] Task , error ) {
var tasks [ ] Task
file , err := os . Open ( uri )
if err != nil {
return nil , errors . Wrap ( err , "Error opening file" )
}
err = json . NewDecoder ( file ) . Decode ( & tasks )
if err != nil {
return nil , errors . Wrap ( err , "Error unmarshalling" )
}
2018-01-26 17:29:43 -05:00
initTaskResourceRequirements ( tasks )
2016-09-16 19:06:53 -04:00
return tasks , nil
2016-10-13 17:15:09 -04:00
}
2016-10-15 21:24:14 -04:00
2016-11-10 19:47:03 -05:00
// Update the host on which the task needs to be scheduled.
2016-11-28 17:18:33 -05:00
func ( tsk * Task ) UpdateHost ( newHost string ) bool {
2016-11-10 19:47:03 -05:00
// Validation
2016-11-28 17:18:33 -05:00
isCorrectHost := false
2017-03-23 22:13:29 -04:00
for existingHost , _ := range constants . Hosts {
2016-11-28 17:18:33 -05:00
if newHost == existingHost {
isCorrectHost = true
2016-11-15 15:11:00 -05:00
}
}
2016-11-28 17:18:33 -05:00
if ! isCorrectHost {
2016-11-10 19:47:03 -05:00
return false
} else {
2016-11-28 17:18:33 -05:00
tsk . Host = newHost
2016-11-10 19:47:03 -05:00
return true
}
}
// Set the taskID of the task.
func ( tsk * Task ) SetTaskID ( taskID string ) bool {
// Validation
if taskID == "" {
return false
} else {
tsk . TaskID = taskID
return true
}
}
2017-02-09 18:05:38 -05:00
/ *
Determine the watts value to consider for each task .
This value could either be task . Watts or task . ClassToWatts [ < power class > ]
2017-09-28 15:36:47 -04:00
If task . ClassToWatts is not present , then return task . Watts ( this would be for workloads which don ' t have classMapWatts ) .
2017-02-09 18:05:38 -05:00
* /
func WattsToConsider ( task Task , classMapWatts bool , offer * mesos . Offer ) ( float64 , error ) {
if classMapWatts {
2017-09-28 15:36:47 -04:00
// Checking if ClassToWatts was present in the workload.
2017-02-09 18:05:38 -05:00
if task . ClassToWatts != nil {
return task . ClassToWatts [ offerUtils . PowerClass ( offer ) ] , nil
} else {
// Checking whether task.Watts is 0.0. If yes, then throwing an error.
if task . Watts == 0.0 {
return task . Watts , errors . New ( "Configuration error in task. Watts attribute is 0 for " + task . Name )
}
return task . Watts , nil
}
} else {
// Checking whether task.Watts is 0.0. If yes, then throwing an error.
if task . Watts == 0.0 {
return task . Watts , errors . New ( "Configuration error in task. Watts attribute is 0 for " + task . Name )
}
return task . Watts , nil
}
}
2016-11-10 19:47:03 -05:00
// Compare two tasks.
func Compare ( task1 * Task , task2 * Task ) bool {
// If comparing the same pointers (checking the addresses).
if task1 == task2 {
return true
}
2016-11-28 20:02:03 -05:00
if task1 . TaskID != task2 . TaskID {
2016-11-10 19:47:03 -05:00
return false
2016-11-28 20:02:03 -05:00
} else {
return true
2016-11-10 19:47:03 -05:00
}
}