From cfbb2f4bdde0b1d5150c7a66c7bef314c2f620c5 Mon Sep 17 00:00:00 2001 From: Pradyumna Kaushik Date: Thu, 8 Nov 2018 20:07:06 -0500 Subject: [PATCH] Fix: create log dir after validating cmdline args. Initialization of the logger happens after all the command-line arguments are validated. So, in case any of the command-line arguments were invalid, the log directory is not created. Retrofitted the driver code and the scheduler builder to only use the language provided 'log' package as elektron's logger will not yet be initialized. --- scheduler.go | 59 +++++++++++++++++++------------------------ schedulers/helpers.go | 26 +++++++++---------- 2 files changed, 38 insertions(+), 47 deletions(-) diff --git a/scheduler.go b/scheduler.go index fddfa59..9fc8b1d 100644 --- a/scheduler.go +++ b/scheduler.go @@ -1,20 +1,20 @@ // 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 . -// +// package main @@ -95,19 +95,9 @@ func main() { os.Exit(1) } - // Creating logger and attaching different logging platforms. - startTime := time.Now() - formattedStartTime := startTime.Format("20060102150405") - // Checking if prefix contains any special characters - if strings.Contains(*pcplogPrefix, "/") { - log.Fatal("log file prefix should not contain '/'.") - } - logPrefix := *pcplogPrefix + "_" + formattedStartTime - logger := elekLogDef.BuildLogger(startTime, logPrefix) // Logging channels. logMType := make(chan elekLogDef.LogMessageType) logMsg := make(chan string) - go logger.Listen(logMType, logMsg) // First we need to build the scheduler using scheduler options. var schedOptions []schedulers.SchedulerOptions = make([]schedulers.SchedulerOptions, 0, 10) @@ -149,9 +139,7 @@ func main() { if *enableSchedPolicySwitch { // Scheduling policy config file required. if spcf := *schedPolConfigFile; spcf == "" { - logger.WriteLog(elekLogDef.ERROR, "No file containing characteristics for"+ - " scheduling policies") - os.Exit(1) + log.Fatal("Scheduling policy characteristics file not provided.") } else { // Initializing the characteristics of the scheduling policies. schedulers.InitSchedPolicyCharacteristics(spcf) @@ -169,7 +157,7 @@ func main() { // If CMW is disabled, then the Median of Medians Max Peak Power Usage value is used // as the watts value for each task. if *wattsAsAResource { - logger.WriteLog(elekLogDef.GENERAL, "WaaR enabled...") + log.Println("WaaR enabled...") schedOptions = append(schedOptions, schedulers.WithWattsAsAResource(*wattsAsAResource)) schedOptions = append(schedOptions, schedulers.WithClassMapWatts(*classMapWatts)) } @@ -186,8 +174,7 @@ func main() { "prog-extrema": {}, } if _, ok := powercapValues[*powerCapPolicy]; !ok { - logger.WriteLog(elekLogDef.ERROR, "Incorrect power-capping algorithm specified.") - os.Exit(1) + log.Fatal("Incorrect power-capping algorithm specified.") } else { // Indicating which power capping algorithm to use, if any. // The pcp-logging with/without power capping will be run after the @@ -207,9 +194,8 @@ func main() { // These values are not used to configure the scheduler. // hiThreshold and loThreshold are passed to the powercappers. if *hiThreshold < *loThreshold { - logger.WriteLog(elekLogDef.ERROR, "High threshold is of a"+ - " lower value than low threshold.") - os.Exit(1) + log.Fatal("High threshold is of a lower value than low " + + "threshold.") } } } @@ -218,15 +204,11 @@ func main() { // Tasks // If httpServer is disabled, then path of file containing workload needs to be provided. if *tasksFile == "" { - logger.WriteLog(elekLogDef.ERROR, "No file containing tasks specification"+ - " provided.") - os.Exit(1) + log.Fatal("Tasks specifications file not provided.") } tasks, err := def.TasksFromJSON(*tasksFile) if err != nil || len(tasks) == 0 { - logger.WriteLog(elekLogDef.ERROR, "Invalid tasks specification file "+ - "provided.") - os.Exit(1) + log.Fatal("Invalid tasks specification file provided.") } schedOptions = append(schedOptions, schedulers.WithTasks(tasks)) @@ -243,11 +225,22 @@ func main() { Scheduler: scheduler, }) if err != nil { - logger.WriteLog(elekLogDef.ERROR, fmt.Sprintf("Unable to create scheduler driver:"+ - " %s", err)) - os.Exit(1) + log.Fatal(fmt.Sprintf("Unable to create scheduler driver: %s", err)) } + // If here, then all command-line arguments validate. + // Creating logger and attaching different logging platforms. + startTime := time.Now() + formattedStartTime := startTime.Format("20060102150405") + // Checking if prefix contains any special characters. + if strings.Contains(*pcplogPrefix, "/") { + log.Fatal("log file prefix should not contain '/'.") + } + logPrefix := *pcplogPrefix + "_" + formattedStartTime + logger := elekLogDef.BuildLogger(startTime, logPrefix) + // Starting the logging go-routine. + go logger.Listen(logMType, logMsg) + // Starting PCP logging. if noPowercap { go pcp.Start(pcpLog, &recordPCP, logMType, logMsg, *pcpConfigFile) diff --git a/schedulers/helpers.go b/schedulers/helpers.go index b841227..a1d6fdb 100644 --- a/schedulers/helpers.go +++ b/schedulers/helpers.go @@ -1,24 +1,25 @@ // 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 . -// +// package schedulers import ( + "fmt" mesos "github.com/mesos/mesos-go/api/v0/mesosproto" sched "github.com/mesos/mesos-go/api/v0/scheduler" "github.com/pkg/errors" @@ -27,6 +28,7 @@ import ( elekLogDef "gitlab.com/spdf/elektron/logging/def" "gitlab.com/spdf/elektron/utilities" "gitlab.com/spdf/elektron/utilities/mesosUtils" + "log" ) func coLocated(tasks map[string]bool, s BaseScheduler) { @@ -150,10 +152,8 @@ func WithSchedPolSwitchEnabled(enableSchedPolicySwitch bool, switchingCriteria s func WithNameOfFirstSchedPolToFix(nameOfFirstSchedPol string) SchedulerOptions { return func(s ElectronScheduler) error { if nameOfFirstSchedPol == "" { - lmt := elekLogDef.WARNING - msgColor := elekLogDef.LogMessageColors[lmt] - msg := msgColor.Sprintf("First scheduling policy to deploy not mentioned. This is now going to be determined at runtime.") - s.(*BaseScheduler).Log(lmt, msg) + log.Println("First scheduling policy to deploy not mentioned. This is now" + + " going to be determined at runtime.") return nil } if _, ok := SchedPolicies[nameOfFirstSchedPol]; !ok { @@ -170,15 +170,13 @@ func WithFixedSchedulingWindow(toFixSchedWindow bool, fixedSchedWindowSize int) if fixedSchedWindowSize <= 0 { return errors.New("Invalid value of scheduling window size. Please provide a value > 0.") } - lmt := elekLogDef.WARNING - msgColor := elekLogDef.LogMessageColors[lmt] - msg := msgColor.Sprintf("Fixing the size of the scheduling window to %d...", fixedSchedWindowSize) - s.(*BaseScheduler).Log(lmt, msg) + log.Println(fmt.Sprintf("Fixing the size of the scheduling window to %d.."+ + ".", fixedSchedWindowSize)) s.(*BaseScheduler).toFixSchedWindow = toFixSchedWindow s.(*BaseScheduler).schedWindowSize = fixedSchedWindowSize } // There shouldn't be any error for this scheduler option. - // If toFixSchedWindow is set to false, then the fixedSchedWindowSize would be ignored. In this case, + // If fixSchedWindow is set to false, then the fixedSchedWindowSize would be ignored. In this case, // the size of the scheduling window would be determined at runtime. return nil }