From 3ed52303f0ff0645b8f4978b5470745a66c7ef03 Mon Sep 17 00:00:00 2001 From: Carlos Segarra Date: Sat, 20 Apr 2024 16:54:37 +0000 Subject: [PATCH] planner: implement compaction planner policy --- .../faabric/batch-scheduler/BatchScheduler.h | 2 ++ .../batch-scheduler/CompactScheduler.h | 31 +++++++++++++++++++ include/faabric/planner/Planner.h | 2 ++ src/batch-scheduler/BatchScheduler.cpp | 14 ++++++++- src/planner/Planner.cpp | 8 +++++ src/planner/PlannerEndpointHandler.cpp | 21 ++++++++++++- src/planner/planner.proto | 2 ++ 7 files changed, 78 insertions(+), 2 deletions(-) create mode 100644 include/faabric/batch-scheduler/CompactScheduler.h diff --git a/include/faabric/batch-scheduler/BatchScheduler.h b/include/faabric/batch-scheduler/BatchScheduler.h index 30453ddde..e7c93ad73 100644 --- a/include/faabric/batch-scheduler/BatchScheduler.h +++ b/include/faabric/batch-scheduler/BatchScheduler.h @@ -120,4 +120,6 @@ class BatchScheduler std::shared_ptr getBatchScheduler(); void resetBatchScheduler(); + +void resetBatchScheduler(const std::string& newMode); } diff --git a/include/faabric/batch-scheduler/CompactScheduler.h b/include/faabric/batch-scheduler/CompactScheduler.h new file mode 100644 index 000000000..1cc155e1a --- /dev/null +++ b/include/faabric/batch-scheduler/CompactScheduler.h @@ -0,0 +1,31 @@ +#pragma once + +#include +#include +#include + +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 makeSchedulingDecision( + HostMap& hostMap, + const InFlightReqs& inFlightReqs, + std::shared_ptr req) override; + + private: + bool isFirstDecisionBetter( + std::shared_ptr decisionA, + std::shared_ptr decisionB) override; + + std::vector getSortedHosts( + HostMap& hostMap, + const InFlightReqs& inFlightReqs, + std::shared_ptr req, + const DecisionType& decisionType) override; +}; +} diff --git a/include/faabric/planner/Planner.h b/include/faabric/planner/Planner.h index db8f9defb..0c28aeeee 100644 --- a/include/faabric/planner/Planner.h +++ b/include/faabric/planner/Planner.h @@ -33,6 +33,8 @@ class Planner void printConfig() const; + void setPolicy(const std::string& newPolicy); + // ---------- // Util public API // ---------- diff --git a/src/batch-scheduler/BatchScheduler.cpp b/src/batch-scheduler/BatchScheduler.cpp index 71bbf699b..dd9985901 100644 --- a/src/batch-scheduler/BatchScheduler.cpp +++ b/src/batch-scheduler/BatchScheduler.cpp @@ -1,5 +1,6 @@ #include #include +#include #include #include @@ -20,7 +21,9 @@ std::shared_ptr getBatchScheduler() if (mode == "bin-pack") { batchScheduler = std::make_shared(); - } else { + } else if (mode == "compact") { + batchScheduler = std::make_shared(); + }else { SPDLOG_ERROR("Unrecognised batch scheduler mode: {}", mode); throw std::runtime_error("Unrecognised batch scheduler mode"); } @@ -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 req) diff --git a/src/planner/Planner.cpp b/src/planner/Planner.cpp index 577d5bc2c..09a88cae7 100644 --- a/src/planner/Planner.cpp +++ b/src/planner/Planner.cpp @@ -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"); diff --git a/src/planner/PlannerEndpointHandler.cpp b/src/planner/PlannerEndpointHandler.cpp index a31fb92b1..f5d9424d4 100644 --- a/src/planner/PlannerEndpointHandler.cpp +++ b/src/planner/PlannerEndpointHandler.cpp @@ -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( @@ -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); diff --git a/src/planner/planner.proto b/src/planner/planner.proto index 60ac6e4c4..af6b00b1d 100644 --- a/src/planner/planner.proto +++ b/src/planner/planner.proto @@ -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"]; @@ -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"]; }