...

Source file src/scheduler/api/configuration.go

Documentation: scheduler/api

     1  /*
     2   * P2PFaaS - A framework for FaaS Load Balancing
     3   * Copyright (c) 2019 - 2022. Gabriele Proietti Mattia <pm.gabriele@outlook.com>
     4   *
     5   * This program is free software: you can redistribute it and/or modify
     6   * it under the terms of the GNU General Public License as published by
     7   * the Free Software Foundation, either version 3 of the License, or
     8   * (at your option) any later version.
     9   *
    10   * This program is distributed in the hope that it will be useful,
    11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
    12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13   * GNU General Public License for more details.
    14   *
    15   * You should have received a copy of the GNU General Public License
    16   * along with this program.  If not, see <https://www.gnu.org/licenses/>.
    17   */
    18  
    19  package api
    20  
    21  import (
    22  	"encoding/json"
    23  	"io/ioutil"
    24  	"net/http"
    25  	"scheduler/config"
    26  	"scheduler/errors"
    27  	"scheduler/log"
    28  	"scheduler/scheduler"
    29  	"scheduler/types"
    30  	"scheduler/utils"
    31  )
    32  
    33  // GetConfiguration Retrieve the current configuration of the system.
    34  func GetConfiguration(w http.ResponseWriter, r *http.Request) {
    35  	configuration, err := json.Marshal(config.GetConfigurationDynamicCopy())
    36  	if err != nil {
    37  		log.Log.Errorf("Cannot encode configuration to json")
    38  		errors.ReplyWithError(&w, errors.GenericError, nil)
    39  		return
    40  	}
    41  
    42  	utils.HttpSendJSONResponse(&w, 200, string(configuration), nil)
    43  }
    44  
    45  // SetConfiguration Set the configuration of the system (conform to config.ConfigurationSetExp) and save it to a file,
    46  // in such a way it is load at the startup. This configuration does not include the scheduler information.
    47  func SetConfiguration(w http.ResponseWriter, r *http.Request) {
    48  	currentConfiguration := config.GetConfigurationDynamicCopy()
    49  	reqBody, _ := ioutil.ReadAll(r.Body)
    50  
    51  	var newConfiguration *config.ConfigurationDynamic
    52  	var err error
    53  
    54  	// do the merge with the default configuration or existing
    55  	err = json.Unmarshal(reqBody, &currentConfiguration)
    56  	newConfiguration = currentConfiguration
    57  	if err != nil {
    58  		log.Log.Errorf("Cannot encode passed configuration: %s", err)
    59  		errors.ReplyWithError(&w, errors.GenericError, nil)
    60  		return
    61  	}
    62  
    63  	config.SetRunningFunctionMax(newConfiguration.ParallelRunningFunctionsMax)
    64  	config.SetQueueLengthMax(newConfiguration.QueueLengthMax)
    65  	config.SetQueueEnabled(newConfiguration.QueueEnabled)
    66  
    67  	// save configuration to file
    68  	err = config.SaveConfigurationDynamicToConfigFile()
    69  	if err != nil {
    70  		log.Log.Warningf("Cannot save configuration to file %s", config.GetConfigFilePath())
    71  	}
    72  
    73  	log.Log.Infof("Configuration updated")
    74  
    75  	w.WriteHeader(200)
    76  }
    77  
    78  // GetScheduler Retrieves the scheduler information.
    79  func GetScheduler(w http.ResponseWriter, r *http.Request) {
    80  	sched, err := json.Marshal(scheduler.GetScheduler())
    81  	if err != nil {
    82  		log.Log.Errorf("Cannot encode configuration to json")
    83  		errors.ReplyWithError(&w, errors.GenericError, nil)
    84  		return
    85  	}
    86  
    87  	utils.HttpSendJSONResponse(&w, 200, string(sched), nil)
    88  }
    89  
    90  // SetScheduler Sets the scheduler information and save the configuration to file in such a way it is loaded automatically at startup.
    91  func SetScheduler(w http.ResponseWriter, r *http.Request) {
    92  	var proposedScheduler = types.SchedulerDescriptor{}
    93  	reqBody, _ := ioutil.ReadAll(r.Body)
    94  
    95  	err := json.Unmarshal(reqBody, &proposedScheduler)
    96  	if err != nil {
    97  		log.Log.Errorf("Cannot decode passed configuration: %s", err.Error())
    98  		errors.ReplyWithError(&w, errors.InputNotValid, nil)
    99  		return
   100  	}
   101  
   102  	err = scheduler.SetScheduler(&proposedScheduler)
   103  	if err != nil {
   104  		log.Log.Errorf("Cannot set new scheduler: %s", err.Error())
   105  		errors.ReplyWithErrorMessage(&w, errors.GenericError, err.Error(), nil)
   106  		return
   107  	}
   108  
   109  	// save configuration to file
   110  	err = config.SaveConfigurationSchedulerToConfigFile(scheduler.GetScheduler())
   111  	if err != nil {
   112  		log.Log.Errorf("Cannot save configuration to file %s", config.GetConfigSchedulerFilePath())
   113  	}
   114  
   115  	log.Log.Infof("Configuration updated with scheduler: %s", scheduler.GetName())
   116  
   117  	w.WriteHeader(200)
   118  }
   119  

View as plain text