This repository has been archived on 2024-04-10. You can view files and clone it, but you cannot make any changes to it's state, such as pushing and creating new issues, pull requests or comments.
elektron/utilities/offerUtils/offerUtils.go
Pradyumna Kaushik 6fb0e4a3fe move cfg to loggers + refactor + log fn wrappers
1. Instead of maintaining a global config, each specialized logger
now stores its config.
2. Refactored logInterface to elektronLogger.
3. Refactored loggerImpl to baseElektronLogger to be consistent with
the rest of the code base.
4. Wrapped elektronLogger#Log(...) and elektronLogf(...) so that we
do not have to use the instance of elektronLogger everytime we want
to log. Instead, we just do logging.Log(...) or logging.Logf(...).
5. Wrapped elektronLogger#WithFields(...) and
elektronLogger#WithField(...).
6. Refactored codebase to adhere to the changes.
2019-12-05 21:32:37 -05:00

109 lines
3.5 KiB
Go

// Copyright (C) 2018 spdfg
//
// 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/>.
//
package offerUtils
import (
"strings"
mesos "github.com/mesos/mesos-go/api/v0/mesosproto"
log "github.com/sirupsen/logrus"
"github.com/spdfg/elektron/constants"
elekLog "github.com/spdfg/elektron/logging"
elekLogTypes "github.com/spdfg/elektron/logging/types"
)
func OfferAgg(offer *mesos.Offer) (float64, float64, float64) {
var cpus, mem, watts float64
for _, resource := range offer.Resources {
switch resource.GetName() {
case "cpus":
cpus += *resource.GetScalar().Value
case "mem":
mem += *resource.GetScalar().Value
case "watts":
watts += *resource.GetScalar().Value
}
}
return cpus, mem, watts
}
// Determine the power class of the host in the offer.
func PowerClass(offer *mesos.Offer) string {
var powerClass string
for _, attr := range offer.GetAttributes() {
if attr.GetName() == "class" {
powerClass = attr.GetText().GetValue()
}
}
return powerClass
}
// Implements the sort.Sort interface to sort Offers based on CPU.
// TODO: Have a generic sorter that sorts based on a defined requirement (CPU, RAM, DISK or Watts).
type OffersSorter []*mesos.Offer
func (offersSorter OffersSorter) Len() int {
return len(offersSorter)
}
func (offersSorter OffersSorter) Swap(i, j int) {
offersSorter[i], offersSorter[j] = offersSorter[j], offersSorter[i]
}
func (offersSorter OffersSorter) Less(i, j int) bool {
// Getting CPU resource availability of offersSorter[i].
cpu1, _, _ := OfferAgg(offersSorter[i])
// Getting CPU resource availability of offersSorter[j].
cpu2, _, _ := OfferAgg(offersSorter[j])
return cpu1 <= cpu2
}
// Is there a mismatch between the task's host requirement and the host corresponding to the offer.
func HostMismatch(offerHost string, taskHost string) bool {
if taskHost != "" && !strings.HasPrefix(offerHost, taskHost) {
return true
}
return false
}
// If the host in the offer is a new host, add the host to the set of Hosts and
// register the powerclass of this host.
func UpdateEnvironment(offer *mesos.Offer) {
var host = offer.GetHostname()
// If this host is not present in the set of hosts.
if _, ok := constants.Hosts[host]; !ok {
elekLog.WithFields(log.Fields{"Adding host": host}).Log(elekLogTypes.CONSOLE, log.InfoLevel, "New host detected")
// Add this host.
constants.Hosts[host] = struct{}{}
// Get the power class of this host.
class := PowerClass(offer)
elekLog.WithFields(log.Fields{"host": host, "PowerClass": class}).Log(elekLogTypes.CONSOLE,
log.InfoLevel, "Registering the power class...")
// If new power class, register the power class.
if _, ok := constants.PowerClasses[class]; !ok {
constants.PowerClasses[class] = make(map[string]struct{})
}
// If the host of this class is not yet present in PowerClasses[class], add it.
if _, ok := constants.PowerClasses[class][host]; !ok {
constants.PowerClasses[class][host] = struct{}{}
}
}
}