Skip to content

Commit

Permalink
planner: implement compaction planner policy
Browse files Browse the repository at this point in the history
  • Loading branch information
csegarragonz committed Apr 21, 2024
1 parent 5b65b3e commit 3ed5230
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 2 deletions.
2 changes: 2 additions & 0 deletions include/faabric/batch-scheduler/BatchScheduler.h
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,6 @@ class BatchScheduler
std::shared_ptr<BatchScheduler> getBatchScheduler();

void resetBatchScheduler();

void resetBatchScheduler(const std::string& newMode);
}
31 changes: 31 additions & 0 deletions include/faabric/batch-scheduler/CompactScheduler.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
#pragma once

#include <faabric/batch-scheduler/BatchScheduler.h>
#include <faabric/batch-scheduler/SchedulingDecision.h>
#include <faabric/util/batch.h>

namespace faabric::batch_scheduler {

// This batch scheduler behaves in the same way than BinPack for NEW and
// SCALE_CHANGE requests, but for DIST_CHANGE requests it tries to compact
// to the fewest number of VMs.
class CompactScheduler final : public BatchScheduler
{
public:
std::shared_ptr<SchedulingDecision> makeSchedulingDecision(
HostMap& hostMap,
const InFlightReqs& inFlightReqs,
std::shared_ptr<faabric::BatchExecuteRequest> req) override;

private:
bool isFirstDecisionBetter(
std::shared_ptr<SchedulingDecision> decisionA,
std::shared_ptr<SchedulingDecision> decisionB) override;

std::vector<Host> getSortedHosts(
HostMap& hostMap,
const InFlightReqs& inFlightReqs,
std::shared_ptr<faabric::BatchExecuteRequest> req,
const DecisionType& decisionType) override;
};
}
2 changes: 2 additions & 0 deletions include/faabric/planner/Planner.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ class Planner

void printConfig() const;

void setPolicy(const std::string& newPolicy);

// ----------
// Util public API
// ----------
Expand Down
14 changes: 13 additions & 1 deletion src/batch-scheduler/BatchScheduler.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <faabric/batch-scheduler/BatchScheduler.h>
#include <faabric/batch-scheduler/BinPackScheduler.h>
#include <faabric/batch-scheduler/CompactScheduler.h>
#include <faabric/util/config.h>
#include <faabric/util/logging.h>

Expand All @@ -20,7 +21,9 @@ std::shared_ptr<BatchScheduler> getBatchScheduler()

if (mode == "bin-pack") {
batchScheduler = std::make_shared<BinPackScheduler>();
} else {
} else if (mode == "compact") {
batchScheduler = std::make_shared<CompactScheduler>();
}else {
SPDLOG_ERROR("Unrecognised batch scheduler mode: {}", mode);
throw std::runtime_error("Unrecognised batch scheduler mode");
}
Expand All @@ -33,6 +36,15 @@ void resetBatchScheduler()
batchScheduler = nullptr;
}

void resetBatchScheduler(const std::string& newMode)
{
resetBatchScheduler();

faabric::util::getSystemConfig().batchSchedulerMode = newMode;

getBatchScheduler();
}

DecisionType BatchScheduler::getDecisionType(
const InFlightReqs& inFlightReqs,
std::shared_ptr<faabric::BatchExecuteRequest> req)
Expand Down
8 changes: 8 additions & 0 deletions src/planner/Planner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,14 @@ void Planner::printConfig() const
SPDLOG_INFO("HTTP_SERVER_THREADS {}", config.numthreadshttpserver());
}

void Planner::setPolicy(const std::string& newPolicy)
{
// Acquire lock to prevent any changes in state whilst we change the policy
faabric::util::FullLock lock(plannerMx);

faabric::batch_scheduler::resetBatchScheduler(newPolicy);
}

bool Planner::reset()
{
SPDLOG_INFO("Resetting planner");
Expand Down
21 changes: 20 additions & 1 deletion src/planner/PlannerEndpointHandler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -267,7 +267,6 @@ void PlannerEndpointHandler::onRequest(
return ctx.sendFunction(std::move(response));
}
case faabric::planner::HttpMessage_Type_PRELOAD_SCHEDULING_DECISION: {
// foo bar
// in: BatchExecuteRequest
// out: none
SPDLOG_DEBUG(
Expand Down Expand Up @@ -308,6 +307,26 @@ void PlannerEndpointHandler::onRequest(

return ctx.sendFunction(std::move(response));
}
case faabric::planner::HttpMessage_Type_SET_POLICY: {
SPDLOG_DEBUG(
"Planner received SET_POLICY request");

std::string newPolicy = msg.payloadjson();

try {
faabric::planner::getPlanner().setPolicy(newPolicy);
} catch (std::exception& e) {
response.result(beast::http::status::bad_request);
response.body() = std::string("Unrecognised policy name: " + newPolicy);
return ctx.sendFunction(std::move(response));
}

// Prepare the response
response.result(beast::http::status::ok);
response.body() = std::string("Policy set correctly");

return ctx.sendFunction(std::move(response));
}
default: {
SPDLOG_ERROR("Unrecognised message type {}", msg.type());
response.result(beast::http::status::bad_request);
Expand Down
2 changes: 2 additions & 0 deletions src/planner/planner.proto
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ message HttpMessage {
EXECUTE_BATCH = 10;
EXECUTE_BATCH_STATUS = 11;
PRELOAD_SCHEDULING_DECISION = 12;
SET_POLICY = 13;
}

Type type = 1 [json_name = "http_type"];
Expand All @@ -55,6 +56,7 @@ message HttpMessage {
// - EXECUTE_BATCH_STATUS: where the body is a BER too
// - PRELOAD_SCHEDULING_DECISION: where the body is a
// faabric::BatchExecuteRequest
// - SET_POLICY: where the body is a string with the new planner policy
string payloadJson = 2 [json_name = "payload"];
}

Expand Down

0 comments on commit 3ed5230

Please sign in to comment.