Skip to content

Commit

Permalink
[control-plane] Update backend allocation to pass both app and proces…
Browse files Browse the repository at this point in the history
…s nam
  • Loading branch information
mcopik committed Oct 17, 2024
1 parent 990c2a1 commit 1b88f06
Show file tree
Hide file tree
Showing 7 changed files with 33 additions and 29 deletions.
12 changes: 6 additions & 6 deletions control-plane/include/praas/control-plane/backend.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,13 @@ namespace praas::control_plane::backend {
* @param resources [TODO:description]
*/
virtual void allocate_process(
process::ProcessPtr, const process::Resources& resources,
std::string app, process::ProcessPtr, const process::Resources& resources,
std::function<void(std::shared_ptr<ProcessInstance>&&, std::optional<std::string>)>&&
callback
) = 0;

virtual void shutdown(
const std::string& name,
const std::string& app, const std::string& name,
process::ProcessObserver instance,
std::function<void(std::optional<std::string>)>&& callback
) = 0;
Expand Down Expand Up @@ -164,13 +164,13 @@ namespace praas::control_plane::backend {
~DockerBackend() override;

void allocate_process(
process::ProcessPtr, const process::Resources& resources,
std::string app, process::ProcessPtr, const process::Resources& resources,
std::function<void(std::shared_ptr<ProcessInstance>&&, std::optional<std::string>)>&&
callback
) override;

void shutdown(
const std::string& name,
const std::string& app, const std::string& name,
process::ProcessObserver instance,
std::function<void(std::optional<std::string>)>&& callback
) override;
Expand Down Expand Up @@ -232,13 +232,13 @@ namespace praas::control_plane::backend {
~FargateBackend() override;

void allocate_process(
process::ProcessPtr, const process::Resources& resources,
std::string app, process::ProcessPtr, const process::Resources& resources,
std::function<void(std::shared_ptr<ProcessInstance>&&, std::optional<std::string>)>&&
callback
) override;

void shutdown(
const std::string& name,
const std::string& app, const std::string& name,
process::ProcessObserver instance,
std::function<void(std::optional<std::string>)>&& callback
) override;
Expand Down
12 changes: 7 additions & 5 deletions control-plane/src/application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ namespace praas::control_plane {
process->set_creation_callback(std::move(callback), wait_for_allocation);

backend.allocate_process(
process, resources,
_name, process, resources,
[process, &poller, name, this](
std::shared_ptr<backend::ProcessInstance>&& instance,
const std::optional<std::string>& error
Expand Down Expand Up @@ -177,7 +177,7 @@ namespace praas::control_plane {
spdlog::info("Allocating process for invocation {}", name);
poller.add_process(process);
backend.allocate_process(
process, resources,
_name, process, resources,
[=, this](
std::shared_ptr<backend::ProcessInstance>&& instance,
const std::optional<std::string>& msg
Expand Down Expand Up @@ -330,7 +330,7 @@ namespace praas::control_plane {
_active_processes.insert(std::move(nh));

backend.allocate_process(
proc_ptr, proc_ptr->_resources,
_name, proc_ptr, proc_ptr->_resources,
[proc_ptr, &poller, process_name, this](
std::shared_ptr<backend::ProcessInstance>&& instance,
const std::optional<std::string>& error
Expand Down Expand Up @@ -478,12 +478,14 @@ namespace praas::control_plane {

auto iter = _active_processes.find(process_name);
if (iter == _active_processes.end() || (*iter).second->status() != process::Status::ALLOCATED) {
throw praas::common::InvalidConfigurationError("Process is not alive!");
//throw praas::common::InvalidConfigurationError("Process is not alive!");
callback("Process is not alive!");
return;
}

(*iter).second->set_status(process::Status::CLOSING);
backend.shutdown(
(*iter).second->name(), (*iter).second,
_name, (*iter).second->name(), (*iter).second,
[this, process_name, callback=std::move(callback)](std::optional<std::string> msg) {

if(!msg) {
Expand Down
10 changes: 6 additions & 4 deletions control-plane/src/backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ namespace praas::control_plane::backend {
}

void DockerBackend::allocate_process(
process::ProcessPtr process, const process::Resources& resources,
std::string app, process::ProcessPtr process, const process::Resources& resources,
std::function<void(std::shared_ptr<ProcessInstance>&&, std::optional<std::string>)>&& callback
)
{
Expand All @@ -104,6 +104,7 @@ namespace praas::control_plane::backend {
_http_client.post(
"/create",
{
{"app", app},
{"process", process->name()},
},
std::move(body),
Expand Down Expand Up @@ -138,14 +139,15 @@ namespace praas::control_plane::backend {
}

void DockerBackend::shutdown(
const std::string& name,
const std::string& app, const std::string& name,
process::ProcessObserver instance,
std::function<void(std::optional<std::string>)>&& callback
)
{
_http_client.post(
"/kill",
{
{"app", app},
{"process", name},
},
[callback = std::move(callback), name, instance, this](
Expand Down Expand Up @@ -314,7 +316,7 @@ namespace praas::control_plane::backend {
}

void FargateBackend::allocate_process(
process::ProcessPtr process, const process::Resources& resources,
std::string app, process::ProcessPtr process, const process::Resources& resources,
std::function<void(std::shared_ptr<ProcessInstance>&&, std::optional<std::string>)>&& callback
)
{
Expand Down Expand Up @@ -369,7 +371,7 @@ namespace praas::control_plane::backend {
}

void FargateBackend::shutdown(
const std::string& name,
const std::string& app, const std::string& name,
process::ProcessObserver instance,
std::function<void(std::optional<std::string>)>&& callback
)
Expand Down
2 changes: 1 addition & 1 deletion control-plane/src/worker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ namespace praas::control_plane::worker {

auto proc_ptr = std::get<1>(val.value());

spdlog::debug("Requesitng new invocation on process, status: {}", proc_ptr->status());
spdlog::debug("Requesting new invocation on process, status: {}", proc_ptr->status());

if(proc_ptr->status() == process::Status::ALLOCATED) {

Expand Down
4 changes: 2 additions & 2 deletions control-plane/tests/integration/http.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,9 @@ TEST_F(HttpTCPIntegration, Invoke)
std::string process_name{"controlplane-0"};

std::promise<void> created_process;
EXPECT_CALL(backend, allocate_process(testing::_, testing::_, testing::_))
EXPECT_CALL(backend, allocate_process(testing::_, testing::_, testing::_, testing::_))
.Times(testing::Exactly(1))
.WillOnce([&](process::ProcessPtr ptr, const process::Resources&, auto&& callback) {
.WillOnce([&](auto, process::ProcessPtr ptr, const process::Resources&, auto&& callback) {
EXPECT_EQ(ptr->name(), process_name);

created_process.set_value();
Expand Down
8 changes: 4 additions & 4 deletions control-plane/tests/mocks.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,13 @@ class MockBackend : public backend::Backend {

MOCK_METHOD(
void, allocate_process,
(process::ProcessPtr, const process::Resources&,
(std::string, process::ProcessPtr, const process::Resources&,
std::function<
void(std::shared_ptr<backend::ProcessInstance>&&, std::optional<std::string>)>&&),
()
);
MOCK_METHOD(void, shutdown,
(const std::string& name,
(const std::string&, const std::string& name,
process::ProcessObserver instance,
std::function<void(std::optional<std::string>)>&& callback),
()
Expand Down Expand Up @@ -74,8 +74,8 @@ void setup_mocks(MockBackend& backend, bool mock_tcp_connection = false)
using callback_t =
std::function<void(std::shared_ptr<backend::ProcessInstance>&&, std::optional<std::string>)>;

ON_CALL(backend, allocate_process(testing::_, testing::_, testing::_))
.WillByDefault([=](process::ProcessPtr ptr, const auto&, callback_t&& callback) {
ON_CALL(backend, allocate_process(testing::_, testing::_, testing::_, testing::_))
.WillByDefault([=](std::string, process::ProcessPtr ptr, const auto&, callback_t&& callback) {
callback(std::make_shared<MockBackendInstance>(), "");
if (mock_tcp_connection) {
ptr->set_status(process::Status::ALLOCATED);
Expand Down
14 changes: 7 additions & 7 deletions control-plane/tests/unit/process.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ TEST_F(CreateProcessTest, CreateProcess)
std::string resource_name{"sandbox"};
process::Resources resources{"1", "128", resource_name};

EXPECT_CALL(backend, allocate_process(testing::_, testing::_, testing::_))
EXPECT_CALL(backend, allocate_process(testing::_, testing::_, testing::_, testing::_))
.Times(testing::Exactly(1));

EXPECT_CALL(poller, add_process(testing::_)).Times(1);
Expand All @@ -57,7 +57,7 @@ TEST_F(CreateProcessTest, CreateProcess)
std::string resource_name{"sandbox"};
process::Resources resources{"1", "128", resource_name};

EXPECT_CALL(backend, allocate_process(testing::_, testing::_, testing::_))
EXPECT_CALL(backend, allocate_process(testing::_, testing::_, testing::_, testing::_))
.Times(testing::Exactly(0));

// Constraints should be verified
Expand All @@ -80,7 +80,7 @@ TEST_F(CreateProcessTest, CreateProcessIncorrectConfig)

std::string resource_name{"sandbox"};
process::Resources resources{"1", "128", resource_name};
EXPECT_CALL(backend, allocate_process(testing::_, testing::_, testing::_))
EXPECT_CALL(backend, allocate_process(testing::_, testing::_, testing::_, testing::_))
.Times(testing::Exactly(0));

EXPECT_THROW(
Expand All @@ -92,7 +92,7 @@ TEST_F(CreateProcessTest, CreateProcessIncorrectConfig)

std::string proc_name{"proc2"};
resources = process::Resources{"5", "128", resource_name};
EXPECT_CALL(backend, allocate_process(testing::_, testing::_, testing::_))
EXPECT_CALL(backend, allocate_process(testing::_, testing::_, testing::_, testing::_))
.Times(testing::Exactly(0));
EXPECT_CALL(backend, max_vcpus()).Times(1);

Expand All @@ -105,7 +105,7 @@ TEST_F(CreateProcessTest, CreateProcessIncorrectConfig)
// Incorrect amount of memory

resources = process::Resources{"1", "8192", resource_name};
EXPECT_CALL(backend, allocate_process(testing::_, testing::_, testing::_))
EXPECT_CALL(backend, allocate_process(testing::_, testing::_, testing::_, testing::_))
.Times(testing::Exactly(0));
EXPECT_CALL(backend, max_memory()).Times(1);

Expand All @@ -127,8 +127,8 @@ TEST_F(CreateProcessTest, CreateProcessFailure)
using callback_t =
std::function<void(std::shared_ptr<backend::ProcessInstance>&&, std::optional<std::string>)>;

EXPECT_CALL(backend, allocate_process(testing::_, testing::_, testing::_))
.WillOnce([&](auto, const auto&, callback_t&& callback) {
EXPECT_CALL(backend, allocate_process(testing::_, testing::_, testing::_, testing::_))
.WillOnce([&](auto, auto, const auto&, callback_t&& callback) {
callback(nullptr, "Failed allocation");
});

Expand Down

0 comments on commit 1b88f06

Please sign in to comment.