-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Add faabric execution context * Trailing whitespace * Add impl to cmake * Exec context tests * Fix EC test * Add isSet * Tidy up imports * Add convenience method to test fixture * Add test for context in executor
- Loading branch information
Showing
9 changed files
with
263 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
#pragma once | ||
|
||
#include <faabric/proto/faabric.pb.h> | ||
#include <faabric/scheduler/Scheduler.h> | ||
|
||
namespace faabric::scheduler { | ||
|
||
/** | ||
* Globally-accessible wrapper that allows executing applications to query | ||
* their execution context. The context is thread-local, so applications can | ||
* query which specific message they are executing. | ||
*/ | ||
class ExecutorContext | ||
{ | ||
public: | ||
ExecutorContext(Executor* executorIn, | ||
std::shared_ptr<faabric::BatchExecuteRequest> reqIn, | ||
int msgIdx); | ||
|
||
static bool isSet(); | ||
|
||
static void set(Executor* executorIn, | ||
std::shared_ptr<faabric::BatchExecuteRequest> reqIn, | ||
int msgIdxIn); | ||
|
||
static void unset(); | ||
|
||
static std::shared_ptr<ExecutorContext> get(); | ||
|
||
Executor* getExecutor() { return executor; } | ||
|
||
std::shared_ptr<faabric::BatchExecuteRequest> getBatchRequest() | ||
{ | ||
return req; | ||
} | ||
|
||
faabric::Message& getMsg() | ||
{ | ||
if (req == nullptr) { | ||
throw std::runtime_error( | ||
"Getting message when no request set in context"); | ||
} | ||
return req->mutable_messages()->at(msgIdx); | ||
} | ||
|
||
int getMsgIdx() { return msgIdx; } | ||
|
||
private: | ||
Executor* executor = nullptr; | ||
std::shared_ptr<faabric::BatchExecuteRequest> req = nullptr; | ||
int msgIdx = 0; | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
faabric_lib(scheduler | ||
ExecGraph.cpp | ||
ExecutorContext.cpp | ||
ExecutorFactory.cpp | ||
Executor.cpp | ||
FunctionCallClient.cpp | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <faabric/scheduler/ExecutorContext.h> | ||
|
||
namespace faabric::scheduler { | ||
|
||
static thread_local std::shared_ptr<ExecutorContext> context = nullptr; | ||
|
||
ExecutorContext::ExecutorContext( | ||
Executor* executorIn, | ||
std::shared_ptr<faabric::BatchExecuteRequest> reqIn, | ||
int msgIdxIn) | ||
: executor(executorIn) | ||
, req(reqIn) | ||
, msgIdx(msgIdxIn) | ||
{} | ||
|
||
bool ExecutorContext::isSet() | ||
{ | ||
return context != nullptr; | ||
} | ||
|
||
void ExecutorContext::set(Executor* executorIn, | ||
std::shared_ptr<faabric::BatchExecuteRequest> reqIn, | ||
int appIdxIn) | ||
{ | ||
context = std::make_shared<ExecutorContext>(executorIn, reqIn, appIdxIn); | ||
} | ||
|
||
void ExecutorContext::unset() | ||
{ | ||
context = nullptr; | ||
} | ||
|
||
std::shared_ptr<ExecutorContext> ExecutorContext::get() | ||
{ | ||
if (context == nullptr) { | ||
SPDLOG_ERROR("No executor context set"); | ||
throw std::runtime_error("No executor context set"); | ||
} | ||
return context; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
#include <catch2/catch.hpp> | ||
|
||
#include "faabric_utils.h" | ||
|
||
#include <faabric/scheduler/ExecutorContext.h> | ||
#include <faabric/util/func.h> | ||
|
||
using namespace faabric::scheduler; | ||
|
||
namespace tests { | ||
|
||
TEST_CASE_METHOD(ExecutorContextTestFixture, | ||
"Test executor context", | ||
"[scheduler]") | ||
{ | ||
REQUIRE(!ExecutorContext::isSet()); | ||
|
||
// Getting with no context should fail | ||
REQUIRE_THROWS(ExecutorContext::get()); | ||
|
||
faabric::Message msg = faabric::util::messageFactory("foo", "bar"); | ||
|
||
std::shared_ptr<DummyExecutorFactory> fac = | ||
std::make_shared<DummyExecutorFactory>(); | ||
auto exec = fac->createExecutor(msg); | ||
|
||
auto req = faabric::util::batchExecFactory("foo", "bar", 5); | ||
|
||
SECTION("Set both executor and request") | ||
{ | ||
ExecutorContext::set(exec.get(), req, 3); | ||
|
||
std::shared_ptr<ExecutorContext> ctx = ExecutorContext::get(); | ||
REQUIRE(ctx->getExecutor() == exec.get()); | ||
REQUIRE(ctx->getBatchRequest() == req); | ||
REQUIRE(ctx->getMsgIdx() == 3); | ||
REQUIRE(ctx->getMsg().id() == req->mutable_messages()->at(3).id()); | ||
} | ||
|
||
SECTION("Just set executor") | ||
{ | ||
ExecutorContext::set(exec.get(), nullptr, 0); | ||
|
||
std::shared_ptr<ExecutorContext> ctx = ExecutorContext::get(); | ||
REQUIRE(ctx->getExecutor() == exec.get()); | ||
REQUIRE(ctx->getBatchRequest() == nullptr); | ||
REQUIRE(ctx->getMsgIdx() == 0); | ||
|
||
REQUIRE_THROWS(ctx->getMsg()); | ||
} | ||
|
||
SECTION("Just set request") | ||
{ | ||
ExecutorContext::set(nullptr, req, 3); | ||
|
||
std::shared_ptr<ExecutorContext> ctx = ExecutorContext::get(); | ||
REQUIRE(ctx->getExecutor() == nullptr); | ||
REQUIRE(ctx->getBatchRequest() == req); | ||
REQUIRE(ctx->getMsgIdx() == 3); | ||
REQUIRE(ctx->getMsg().id() == req->mutable_messages()->at(3).id()); | ||
} | ||
|
||
ExecutorContext::unset(); | ||
REQUIRE_THROWS(ExecutorContext::get()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.