...

Source file src/scheduler/scheduler/forward_scheduler.go

Documentation: scheduler/scheduler

     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 scheduler
    20  
    21  import (
    22  	"fmt"
    23  	"scheduler/log"
    24  	"scheduler/service_discovery"
    25  	"scheduler/types"
    26  	"time"
    27  )
    28  
    29  const ForwardSchedulerName = "ForwardScheduler"
    30  
    31  // ForwardScheduler scheduler forwards all the requests to a random node, this is used for testing purposes
    32  type ForwardScheduler struct {
    33  	// MaxHops is the maximum number of hops that a request can be subjected to before being executed
    34  	MaxHops uint
    35  }
    36  
    37  func (s ForwardScheduler) GetFullName() string {
    38  	return fmt.Sprintf("%s(%d)", ForwardSchedulerName, s.MaxHops)
    39  }
    40  
    41  func (s ForwardScheduler) GetScheduler() *types.SchedulerDescriptor {
    42  	return &types.SchedulerDescriptor{
    43  		Name: ForwardSchedulerName,
    44  		Parameters: []string{
    45  			fmt.Sprintf("%d", s.MaxHops),
    46  		},
    47  	}
    48  }
    49  
    50  // Schedule a service request. This call is blocking until the job has been executed locally or externally.
    51  func (s ForwardScheduler) Schedule(req *types.ServiceRequest) (*JobResult, error) {
    52  	log.Log.Debugf("Scheduling job %s", req.ServiceName)
    53  	now := time.Now()
    54  	timingsStart := types.TimingsStart{ArrivedAt: &now}
    55  
    56  	jobMustExecutedHere := req.External && req.ExternalJobRequest.Hops >= int(s.MaxHops)
    57  
    58  	// check if the balancing condition is hit
    59  	if !jobMustExecutedHere {
    60  		// save time
    61  		startedProbingTime := time.Now()
    62  		timingsStart.ProbingStartedAt = &startedProbingTime
    63  		// get N Random machines and ask them for load and pick the least loaded
    64  		randomMachine, err := service_discovery.GetNRandomMachines(1, true)
    65  		// save time
    66  		endProbingTime := time.Now()
    67  		timingsStart.ProbingEndedAt = &endProbingTime
    68  		if err != nil {
    69  			log.Log.Debugf("Error in retrieving machines %s", err.Error())
    70  			return executeJobLocally(req, &timingsStart, s.GetFullName())
    71  		}
    72  		if len(randomMachine) == 0 {
    73  			log.Log.Debugf("No random machines retrieved")
    74  			return executeJobLocally(req, &timingsStart, s.GetFullName())
    75  		}
    76  		log.Log.Debugf("Forwarding to random machine: %s", randomMachine)
    77  		return executeJobExternally(req, randomMachine[0], &timingsStart, s.GetFullName())
    78  	}
    79  
    80  	return executeJobLocally(req, &timingsStart, s.GetFullName())
    81  }
    82  

View as plain text