From 3cda734374e61cc0a1f71949fbe10db7e01ff605 Mon Sep 17 00:00:00 2001 From: Daouda Diakite Date: Wed, 14 Feb 2024 15:03:23 +0100 Subject: [PATCH 01/15] Add new methods to session service --- .../header/sessions/SessionsClient.h | 40 ++++++++++ .../source/sessions/SessionsClient.cpp | 74 ++++++++++++++++++- 2 files changed, 113 insertions(+), 1 deletion(-) diff --git a/packages/cpp/ArmoniK.Api.Client/header/sessions/SessionsClient.h b/packages/cpp/ArmoniK.Api.Client/header/sessions/SessionsClient.h index 349748160..d472af0db 100644 --- a/packages/cpp/ArmoniK.Api.Client/header/sessions/SessionsClient.h +++ b/packages/cpp/ArmoniK.Api.Client/header/sessions/SessionsClient.h @@ -57,6 +57,46 @@ class SessionsClient { int32_t page_size = 500, armonik::api::grpc::v1::sessions::ListSessionsRequest::Sort sort = default_sort); + /** + * Pause a session + * + * @param session_id Session Id + * @return SessionRaw object containing information about the session + */ + armonik::api::grpc::v1::sessions::SessionRaw pause_session(std::string session_id); + + /** + * Resume a session + * + * @param session_id Session Id + * @return SessionRaw object containing information about the session + */ + armonik::api::grpc::v1::sessions::SessionRaw resume_session(std::string session_id); + + /** + * Purge a session + * + * @param session_id Session Id + * @return SessionRaw object containing information about the session + */ + armonik::api::grpc::v1::sessions::SessionRaw purge_session(std::string session_id); + + /** + * Delete a session + * + * @param session_id Session Id + * @return SessionRaw object containing information about the session + */ + armonik::api::grpc::v1::sessions::SessionRaw delete_session(std::string session_id); + + /** + * Stop a new tasks submission in a session + * + * @param session_id Session Id + * @return SessionRaw object containing information about the session + */ + armonik::api::grpc::v1::sessions::SessionRaw stop_submission_session(std::string session_id); + private: std::unique_ptr stub; static const armonik::api::grpc::v1::sessions::ListSessionsRequest::Sort default_sort; diff --git a/packages/cpp/ArmoniK.Api.Client/source/sessions/SessionsClient.cpp b/packages/cpp/ArmoniK.Api.Client/source/sessions/SessionsClient.cpp index 7780b2f18..bae6aabb8 100644 --- a/packages/cpp/ArmoniK.Api.Client/source/sessions/SessionsClient.cpp +++ b/packages/cpp/ArmoniK.Api.Client/source/sessions/SessionsClient.cpp @@ -1,5 +1,6 @@ #include - +#include "sessions_common.pb.h" +#include "sessions_service.grpc.pb.h" #include "exceptions/ArmoniKApiException.h" #include "sessions/SessionsClient.h" @@ -106,3 +107,74 @@ armonik::api::client::SessionsClient::list_sessions(armonik::api::grpc::v1::sess return rawSessions; } } + +armonik::api::grpc::v1::sessions::SessionRaw +armonik::api::client::SessionsClient::pause_session(std::string session_id) { + ::grpc::ClientContext context; + armonik::api::grpc::v1::sessions::PauseSessionRequest request; + armonik::api::grpc::v1::sessions::PauseSessionResponse response; + + request.set_session_id(std::move(session_id)); + auto status = stub->PauseSession(&context, request, &response); + if(!status.ok()){ + throw armonik::api::common::exceptions::ArmoniKApiException("Could not pause session : " + status.error_message()); + } + + return std::move(*response.mutable_session()); +} + +armonik::api::grpc::v1::sessions::SessionRaw armonik::api::client::SessionsClient::resume_session(std::string session_id) { + ::grpc::ClientContext context; + armonik::api::grpc::v1::sessions::ResumeSessionRequest request; + armonik::api::grpc::v1::sessions::ResumeSessionResponse response; + + request.set_session_id(std::move(session_id)); + auto status = stub->ResumeSession(&context, request, &response); + if(!status.ok()){ + throw armonik::api::common::exceptions::ArmoniKApiException("Could not resume session : " + status.error_message()); + } + + return std::move(*response.mutable_session()); +} + +armonik::api::grpc::v1::sessions::SessionRaw armonik::api::client::SessionsClient::purge_session(std::string session_id) { + ::grpc::ClientContext context; + armonik::api::grpc::v1::sessions::PurgeSessionRequest request; + armonik::api::grpc::v1::sessions::PurgeSessionResponse response; + + request.set_session_id(std::move(session_id)); + auto status = stub->PurgeSession(&context, request, &response); + if(!status.ok()){ + throw armonik::api::common::exceptions::ArmoniKApiException("Could not purge session : " + status.error_message()); + } + + return std::move(*response.mutable_session()); +} + +armonik::api::grpc::v1::sessions::SessionRaw armonik::api::client::SessionsClient::delete_session(std::string session_id) { + ::grpc::ClientContext context; + armonik::api::grpc::v1::sessions::DeleteSessionRequest request; + armonik::api::grpc::v1::sessions::DeleteSessionResponse response; + + request.set_session_id(std::move(session_id)); + auto status = stub->DeleteSession(&context, request, &response); + if(!status.ok()){ + throw armonik::api::common::exceptions::ArmoniKApiException("Could not delete session : " + status.error_message()); + } + + return std::move(*response.mutable_session()); +} + +armonik::api::grpc::v1::sessions::SessionRaw armonik::api::client::SessionsClient::stop_submission_session(std::string session_id) { + ::grpc::ClientContext context; + armonik::api::grpc::v1::sessions::StopSubmissionRequest request; + armonik::api::grpc::v1::sessions::StopSubmissionResponse response; + + request.set_session_id(std::move(session_id)); + auto status = stub->StopSubmission(&context, request, &response); + if(!status.ok()){ + throw armonik::api::common::exceptions::ArmoniKApiException("Could not stop submission session : " + status.error_message()); + } + + return std::move(*response.mutable_session()); +} \ No newline at end of file From a90a083d41ad16b81a0a0638d7014ef6498ddfa4 Mon Sep 17 00:00:00 2001 From: Daouda Diakite Date: Wed, 14 Feb 2024 15:15:33 +0100 Subject: [PATCH 02/15] Add tests for new methods --- .../source/SessionClientTest.cpp | 77 +++++++++++++++++++ 1 file changed, 77 insertions(+) diff --git a/packages/cpp/ArmoniK.Api.Tests/source/SessionClientTest.cpp b/packages/cpp/ArmoniK.Api.Tests/source/SessionClientTest.cpp index 39616eb33..9161ab595 100644 --- a/packages/cpp/ArmoniK.Api.Tests/source/SessionClientTest.cpp +++ b/packages/cpp/ArmoniK.Api.Tests/source/SessionClientTest.cpp @@ -101,3 +101,80 @@ TEST(Sessions, can_list_sessions_small_page) { ASSERT_GE(list.size(), expected_n_sessions); ASSERT_GE(total, expected_n_sessions); } + +TEST(Sessions, can_pause_session) { + Logger log{armonik::api::common::logger::writer_console(), armonik::api::common::logger::formatter_plain(true)}; + std::shared_ptr<::grpc::Channel> channel; + armonik::api::grpc::v1::TaskOptions task_options; + init(channel, task_options, log); + + armonik::api::client::SessionsClient client(armonik::api::grpc::v1::sessions::Sessions::NewStub(channel)); + + std::string session_id = client.create_session(task_options); + + armonik::api::grpc::v1::sessions::SessionRaw response; + ASSERT_NO_THROW(response = client.pause_session(session_id)); + ASSERT_EQ(response.session_id(), session_id); +} + +TEST(Sessions, can_resume_session) { + Logger log{armonik::api::common::logger::writer_console(), armonik::api::common::logger::formatter_plain(true)}; + std::shared_ptr<::grpc::Channel> channel; + armonik::api::grpc::v1::TaskOptions task_options; + init(channel, task_options, log); + + armonik::api::client::SessionsClient client(armonik::api::grpc::v1::sessions::Sessions::NewStub(channel)); + + std::string session_id = client.create_session(task_options); + + ASSERT_NO_THROW(client.pause_session(session_id)); + + armonik::api::grpc::v1::sessions::SessionRaw response; + ASSERT_NO_THROW(response = client.resume_session(session_id)); + ASSERT_EQ(response.session_id(), session_id); +} + +TEST(Sessions, can_purge_session) { + Logger log{armonik::api::common::logger::writer_console(), armonik::api::common::logger::formatter_plain(true)}; + std::shared_ptr<::grpc::Channel> channel; + armonik::api::grpc::v1::TaskOptions task_options; + init(channel, task_options, log); + + armonik::api::client::SessionsClient client(armonik::api::grpc::v1::sessions::Sessions::NewStub(channel)); + + std::string session_id = client.create_session(task_options); + + armonik::api::grpc::v1::sessions::SessionRaw response; + ASSERT_NO_THROW(response = client.purge_session(session_id)); + ASSERT_EQ(response.session_id(), session_id); +} + +TEST(Sessions, can_delete_session) { + Logger log{armonik::api::common::logger::writer_console(), armonik::api::common::logger::formatter_plain(true)}; + std::shared_ptr<::grpc::Channel> channel; + armonik::api::grpc::v1::TaskOptions task_options; + init(channel, task_options, log); + + armonik::api::client::SessionsClient client(armonik::api::grpc::v1::sessions::Sessions::NewStub(channel)); + + std::string session_id = client.create_session(task_options); + + armonik::api::grpc::v1::sessions::SessionRaw response; + ASSERT_NO_THROW(response = client.delete_session(session_id)); + ASSERT_EQ(response.session_id(), session_id); +} + +TEST(Sessions, can_stop_submission) { + Logger log{armonik::api::common::logger::writer_console(), armonik::api::common::logger::formatter_plain(true)}; + std::shared_ptr<::grpc::Channel> channel; + armonik::api::grpc::v1::TaskOptions task_options; + init(channel, task_options, log); + + armonik::api::client::SessionsClient client(armonik::api::grpc::v1::sessions::Sessions::NewStub(channel)); + + std::string session_id = client.create_session(task_options); + + armonik::api::grpc::v1::sessions::SessionRaw response; + ASSERT_NO_THROW(response = client.stop_submission_session(session_id)); + ASSERT_EQ(response.session_id(), session_id); +} From dfa2b881863dfdbc4ac59e7ccdc704b2ca29f58d Mon Sep 17 00:00:00 2001 From: Daouda Diakite Date: Wed, 14 Feb 2024 15:18:02 +0100 Subject: [PATCH 03/15] Format --- .../header/sessions/SessionsClient.h | 18 +++++------ .../source/sessions/SessionsClient.cpp | 31 +++++++++++-------- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/packages/cpp/ArmoniK.Api.Client/header/sessions/SessionsClient.h b/packages/cpp/ArmoniK.Api.Client/header/sessions/SessionsClient.h index d472af0db..ccc690076 100644 --- a/packages/cpp/ArmoniK.Api.Client/header/sessions/SessionsClient.h +++ b/packages/cpp/ArmoniK.Api.Client/header/sessions/SessionsClient.h @@ -59,7 +59,7 @@ class SessionsClient { /** * Pause a session - * + * * @param session_id Session Id * @return SessionRaw object containing information about the session */ @@ -67,7 +67,7 @@ class SessionsClient { /** * Resume a session - * + * * @param session_id Session Id * @return SessionRaw object containing information about the session */ @@ -75,25 +75,25 @@ class SessionsClient { /** * Purge a session - * + * * @param session_id Session Id - * @return SessionRaw object containing information about the session + * @return SessionRaw object containing information about the session */ armonik::api::grpc::v1::sessions::SessionRaw purge_session(std::string session_id); /** - * Delete a session - * + * Delete a session + * * @param session_id Session Id - * @return SessionRaw object containing information about the session + * @return SessionRaw object containing information about the session */ armonik::api::grpc::v1::sessions::SessionRaw delete_session(std::string session_id); /** * Stop a new tasks submission in a session - * + * * @param session_id Session Id - * @return SessionRaw object containing information about the session + * @return SessionRaw object containing information about the session */ armonik::api::grpc::v1::sessions::SessionRaw stop_submission_session(std::string session_id); diff --git a/packages/cpp/ArmoniK.Api.Client/source/sessions/SessionsClient.cpp b/packages/cpp/ArmoniK.Api.Client/source/sessions/SessionsClient.cpp index bae6aabb8..9b6d8e7d7 100644 --- a/packages/cpp/ArmoniK.Api.Client/source/sessions/SessionsClient.cpp +++ b/packages/cpp/ArmoniK.Api.Client/source/sessions/SessionsClient.cpp @@ -1,8 +1,8 @@ -#include +#include "sessions/SessionsClient.h" +#include "exceptions/ArmoniKApiException.h" #include "sessions_common.pb.h" #include "sessions_service.grpc.pb.h" -#include "exceptions/ArmoniKApiException.h" -#include "sessions/SessionsClient.h" +#include static armonik::api::grpc::v1::sessions::ListSessionsRequest::Sort get_default_sort() { armonik::api::grpc::v1::sessions::ListSessionsRequest::Sort sort; @@ -116,64 +116,69 @@ armonik::api::client::SessionsClient::pause_session(std::string session_id) { request.set_session_id(std::move(session_id)); auto status = stub->PauseSession(&context, request, &response); - if(!status.ok()){ + if (!status.ok()) { throw armonik::api::common::exceptions::ArmoniKApiException("Could not pause session : " + status.error_message()); } return std::move(*response.mutable_session()); } -armonik::api::grpc::v1::sessions::SessionRaw armonik::api::client::SessionsClient::resume_session(std::string session_id) { +armonik::api::grpc::v1::sessions::SessionRaw +armonik::api::client::SessionsClient::resume_session(std::string session_id) { ::grpc::ClientContext context; armonik::api::grpc::v1::sessions::ResumeSessionRequest request; armonik::api::grpc::v1::sessions::ResumeSessionResponse response; request.set_session_id(std::move(session_id)); auto status = stub->ResumeSession(&context, request, &response); - if(!status.ok()){ + if (!status.ok()) { throw armonik::api::common::exceptions::ArmoniKApiException("Could not resume session : " + status.error_message()); } return std::move(*response.mutable_session()); } -armonik::api::grpc::v1::sessions::SessionRaw armonik::api::client::SessionsClient::purge_session(std::string session_id) { +armonik::api::grpc::v1::sessions::SessionRaw +armonik::api::client::SessionsClient::purge_session(std::string session_id) { ::grpc::ClientContext context; armonik::api::grpc::v1::sessions::PurgeSessionRequest request; armonik::api::grpc::v1::sessions::PurgeSessionResponse response; request.set_session_id(std::move(session_id)); auto status = stub->PurgeSession(&context, request, &response); - if(!status.ok()){ + if (!status.ok()) { throw armonik::api::common::exceptions::ArmoniKApiException("Could not purge session : " + status.error_message()); } return std::move(*response.mutable_session()); } -armonik::api::grpc::v1::sessions::SessionRaw armonik::api::client::SessionsClient::delete_session(std::string session_id) { +armonik::api::grpc::v1::sessions::SessionRaw +armonik::api::client::SessionsClient::delete_session(std::string session_id) { ::grpc::ClientContext context; armonik::api::grpc::v1::sessions::DeleteSessionRequest request; armonik::api::grpc::v1::sessions::DeleteSessionResponse response; request.set_session_id(std::move(session_id)); auto status = stub->DeleteSession(&context, request, &response); - if(!status.ok()){ + if (!status.ok()) { throw armonik::api::common::exceptions::ArmoniKApiException("Could not delete session : " + status.error_message()); } return std::move(*response.mutable_session()); } -armonik::api::grpc::v1::sessions::SessionRaw armonik::api::client::SessionsClient::stop_submission_session(std::string session_id) { +armonik::api::grpc::v1::sessions::SessionRaw +armonik::api::client::SessionsClient::stop_submission_session(std::string session_id) { ::grpc::ClientContext context; armonik::api::grpc::v1::sessions::StopSubmissionRequest request; armonik::api::grpc::v1::sessions::StopSubmissionResponse response; request.set_session_id(std::move(session_id)); auto status = stub->StopSubmission(&context, request, &response); - if(!status.ok()){ - throw armonik::api::common::exceptions::ArmoniKApiException("Could not stop submission session : " + status.error_message()); + if (!status.ok()) { + throw armonik::api::common::exceptions::ArmoniKApiException("Could not stop submission session : " + + status.error_message()); } return std::move(*response.mutable_session()); From 83786ce68915ef34ad34066b820198790e5e69d8 Mon Sep 17 00:00:00 2001 From: Daouda Diakite Date: Thu, 15 Feb 2024 14:13:41 +0100 Subject: [PATCH 04/15] Add versions service methods --- .../header/versions/VersionsClient.h | 29 +++++++++++++++++++ .../source/versions/VersionsClient.cpp | 28 ++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 packages/cpp/ArmoniK.Api.Client/header/versions/VersionsClient.h create mode 100644 packages/cpp/ArmoniK.Api.Client/source/versions/VersionsClient.cpp diff --git a/packages/cpp/ArmoniK.Api.Client/header/versions/VersionsClient.h b/packages/cpp/ArmoniK.Api.Client/header/versions/VersionsClient.h new file mode 100644 index 000000000..35ffe94dd --- /dev/null +++ b/packages/cpp/ArmoniK.Api.Client/header/versions/VersionsClient.h @@ -0,0 +1,29 @@ +#pragma once + +#include "versions_common.pb.h" +#include "versions_service.grpc.pb.h" + +namespace armonik { +namespace api { +namespace client { + +/** + * Versions Client wrapper + */ +class VersionsClient { +public: + explicit VersionsClient(std::unique_ptr stub) + : stub(std::move(stub)){}; + + /** + * Get versions of ArmoniK components + * @return Mapping between component names and their versions + */ + std::map list_versions(); + +private: + std::unique_ptr stub; +}; +} // namespace client +} // namespace api +} // namespace armonik diff --git a/packages/cpp/ArmoniK.Api.Client/source/versions/VersionsClient.cpp b/packages/cpp/ArmoniK.Api.Client/source/versions/VersionsClient.cpp new file mode 100644 index 000000000..90a592adf --- /dev/null +++ b/packages/cpp/ArmoniK.Api.Client/source/versions/VersionsClient.cpp @@ -0,0 +1,28 @@ +#include "versions/VersionsClient.h" +#include "exceptions/ArmoniKApiException.h" + +namespace armonik { +namespace api { +namespace client { + +std::map VersionsClient::list_versions() { + ::grpc::ClientContext context; + armonik::api::grpc::v1::versions::ListVersionsRequest request; + armonik::api::grpc::v1::versions::ListVersionsResponse response; + + std::map mapping; + + auto status = stub->ListVersions(&context, request, &response); + if (!status.ok()) { + throw armonik::api::common::exceptions::ArmoniKApiException("Could not get list versions : " + + status.error_message()); + } + + mapping.insert({"api", response.api()}); + mapping.insert({"core", response.core()}); + + return mapping; +} +} // namespace client +} // namespace api +} // namespace armonik \ No newline at end of file From 00f08f2c548e30cc09ec94a4428824411cf3e29e Mon Sep 17 00:00:00 2001 From: Daouda Diakite Date: Thu, 15 Feb 2024 14:14:45 +0100 Subject: [PATCH 05/15] Versions method tests --- .../source/VersionsClientTest.cpp | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 packages/cpp/ArmoniK.Api.Tests/source/VersionsClientTest.cpp diff --git a/packages/cpp/ArmoniK.Api.Tests/source/VersionsClientTest.cpp b/packages/cpp/ArmoniK.Api.Tests/source/VersionsClientTest.cpp new file mode 100644 index 000000000..0d5c02ecc --- /dev/null +++ b/packages/cpp/ArmoniK.Api.Tests/source/VersionsClientTest.cpp @@ -0,0 +1,28 @@ +#include + +#include "common.h" +#include "logger/formatter.h" +#include "logger/logger.h" +#include "logger/writer.h" + +#include "versions/VersionsClient.h" + +using Logger = armonik::api::common::logger::Logger; + +TEST(Versions, can_list_versions) { + Logger log{armonik::api::common::logger::writer_console(), armonik::api::common::logger::formatter_plain(true)}; + std::shared_ptr<::grpc::Channel> channel; + armonik::api::grpc::v1::TaskOptions task_options; + init(channel, task_options, log); + + armonik::api::client::VersionsClient client(armonik::api::grpc::v1::versions::Versions::NewStub(channel)); + + std::map versions; + ASSERT_NO_THROW(versions = client.list_versions()); + + std::cout << "API version: " << versions.at("api") << "\n" + << "Core version: " << versions.at("core") << std::endl; + + ASSERT_NE(versions.at("api"), "Unknown"); + ASSERT_NE(versions.at("core"), "Unknown"); +} \ No newline at end of file From ca4348ca2a9cc69a5bb432d5d4cca1fc64a6e19f Mon Sep 17 00:00:00 2001 From: Daouda Diakite Date: Thu, 15 Feb 2024 15:35:27 +0100 Subject: [PATCH 06/15] Add partitions service methods --- .../header/partitions/PartitionsClient.h | 27 ++++++ .../source/partitions/PartitionsClient.cpp | 83 +++++++++++++++++++ 2 files changed, 110 insertions(+) create mode 100644 packages/cpp/ArmoniK.Api.Client/header/partitions/PartitionsClient.h create mode 100644 packages/cpp/ArmoniK.Api.Client/source/partitions/PartitionsClient.cpp diff --git a/packages/cpp/ArmoniK.Api.Client/header/partitions/PartitionsClient.h b/packages/cpp/ArmoniK.Api.Client/header/partitions/PartitionsClient.h new file mode 100644 index 000000000..c640e1b14 --- /dev/null +++ b/packages/cpp/ArmoniK.Api.Client/header/partitions/PartitionsClient.h @@ -0,0 +1,27 @@ +#pragma once + +#include "partitions_common.pb.h" +#include "partitions_service.grpc.pb.h" + +namespace armonik { +namespace api { +namespace client { + +class PartitionsClient { +public: + explicit PartitionsClient(std::unique_ptr stub) + : stub(std::move(stub)){}; + std::vector + list_partitions(armonik::api::grpc::v1::partitions::Filters filters, int32_t &total, int32_t page = -1, + int32_t page_size = 500, + armonik::api::grpc::v1::partitions::ListPartitionsRequest::Sort sort = default_sort()); + + armonik::api::grpc::v1::partitions::PartitionRaw get_partition(std::string partition_id); + +private: + std::unique_ptr stub; + static armonik::api::grpc::v1::partitions::ListPartitionsRequest::Sort default_sort(); +}; +} // namespace client +} // namespace api +} // namespace armonik \ No newline at end of file diff --git a/packages/cpp/ArmoniK.Api.Client/source/partitions/PartitionsClient.cpp b/packages/cpp/ArmoniK.Api.Client/source/partitions/PartitionsClient.cpp new file mode 100644 index 000000000..a2759a1a3 --- /dev/null +++ b/packages/cpp/ArmoniK.Api.Client/source/partitions/PartitionsClient.cpp @@ -0,0 +1,83 @@ +#include + +#include "exceptions/ArmoniKApiException.h" +#include "partitions/PartitionsClient.h" + +namespace armonik { +namespace api { +namespace client { + +std::vector +PartitionsClient::list_partitions(armonik::api::grpc::v1::partitions::Filters filters, int32_t &total, int32_t page, + int32_t page_size, + armonik::api::grpc::v1::partitions::ListPartitionsRequest::Sort sort) { + ::grpc::ClientContext context; + armonik::api::grpc::v1::partitions::ListPartitionsRequest request; + armonik::api::grpc::v1::partitions::ListPartitionsResponse response; + + *request.mutable_filters() = std::move(filters); + *request.mutable_sort() = std::move(sort); + request.set_page_size(page_size); + + if (page >= 0) { + request.set_page(page); + ::grpc::ClientContext context; + auto status = stub->ListPartitions(&context, request, &response); + if (!status.ok()) { + throw armonik::api::common::exceptions::ArmoniKApiException("Unable to list partitions " + + status.error_message()); + } + total = response.total(); + return {response.partitions().begin(), response.partitions().end()}; + } else { + std::vector rawPartitions; + int current_page = 0; + do { + request.set_page(current_page); + ::grpc::ClientContext context; + auto status = stub->ListPartitions(&context, request, &response); + if (!status.ok()) { + throw armonik::api::common::exceptions::ArmoniKApiException("Unable to list partitions " + + status.error_message()); + } + rawPartitions.insert(rawPartitions.end(), + response.partitions().begin() + ((int32_t)rawPartitions.size() - current_page * page_size), + response.partitions().end()); + if (response.partitions_size() >= page_size) { + current_page++; + } + + response.clear_partitions(); + } while ((int32_t)rawPartitions.size() < response.total()); + + total = response.total(); + + return rawPartitions; + } +} + +armonik::api::grpc::v1::partitions::PartitionRaw PartitionsClient::get_partition(std::string partition_id) { + ::grpc::ClientContext context; + armonik::api::grpc::v1::partitions::GetPartitionRequest request; + armonik::api::grpc::v1::partitions::GetPartitionResponse response; + + *request.mutable_id() = std::move(partition_id); + auto status = stub->GetPartition(&context, request, &response); + if (!status.ok()) { + throw armonik::api::common::exceptions::ArmoniKApiException("Could not get partition : " + status.error_message()); + } + + return response.partition(); +} + +armonik::api::grpc::v1::partitions::ListPartitionsRequest::Sort PartitionsClient::default_sort() { + armonik::api::grpc::v1::partitions::ListPartitionsRequest::Sort sort; + sort.set_direction(grpc::v1::sort_direction::SORT_DIRECTION_ASC); + sort.mutable_field()->mutable_partition_raw_field()->set_field( + grpc::v1::partitions::PARTITION_RAW_ENUM_FIELD_PRIORITY); + return sort; +} + +} // namespace client +} // namespace api +} // namespace armonik \ No newline at end of file From e305c33dfde7c6aabc75d02e043b61ddc648fcd6 Mon Sep 17 00:00:00 2001 From: Daouda Diakite Date: Thu, 15 Feb 2024 16:03:11 +0100 Subject: [PATCH 07/15] Tests for partitions service --- .../source/PartitionsClientTest.cpp | 56 +++++++++++++++++++ 1 file changed, 56 insertions(+) create mode 100644 packages/cpp/ArmoniK.Api.Tests/source/PartitionsClientTest.cpp diff --git a/packages/cpp/ArmoniK.Api.Tests/source/PartitionsClientTest.cpp b/packages/cpp/ArmoniK.Api.Tests/source/PartitionsClientTest.cpp new file mode 100644 index 000000000..8e117fb57 --- /dev/null +++ b/packages/cpp/ArmoniK.Api.Tests/source/PartitionsClientTest.cpp @@ -0,0 +1,56 @@ +#include + +#include "common.h" +#include "logger/formatter.h" +#include "logger/logger.h" +#include "logger/writer.h" + +#include "partitions/PartitionsClient.h" +#include "sessions/SessionsClient.h" + +using Logger = armonik::api::common::logger::Logger; + +TEST(Partitions, can_get_partition) { + Logger log{armonik::api::common::logger::writer_console(), armonik::api::common::logger::formatter_plain(true)}; + std::shared_ptr<::grpc::Channel> channel; + armonik::api::grpc::v1::TaskOptions task_options; + init(channel, task_options, log); + task_options.set_partition_id("default"); + + armonik::api::client::PartitionsClient client(armonik::api::grpc::v1::partitions::Partitions::NewStub(channel)); + + armonik::api::grpc::v1::partitions::PartitionRaw partition; + ASSERT_NO_THROW(partition = client.get_partition(task_options.partition_id())); + ASSERT_EQ(partition.id(), task_options.partition_id()); + ASSERT_EQ(partition.pod_max(), 100); + ASSERT_EQ(partition.pod_reserved(), 1); + ASSERT_EQ(partition.priority(), 1); +} + +TEST(Partitions, can_list_partitions) { + Logger log{armonik::api::common::logger::writer_console(), armonik::api::common::logger::formatter_plain(true)}; + std::shared_ptr<::grpc::Channel> channel; + armonik::api::grpc::v1::TaskOptions task_options; + init(channel, task_options, log); + task_options.set_partition_id("default"); + + armonik::api::client::PartitionsClient client(armonik::api::grpc::v1::partitions::Partitions::NewStub(channel)); + + armonik::api::grpc::v1::partitions::Filters filters; + armonik::api::grpc::v1::partitions::FilterField filter_field; + filter_field.mutable_field()->mutable_partition_raw_field()->set_field( + armonik::api::grpc::v1::partitions::PARTITION_RAW_ENUM_FIELD_ID); + filter_field.mutable_filter_string()->set_value(task_options.partition_id()); + filter_field.mutable_filter_string()->set_operator_(armonik::api::grpc::v1::FILTER_STRING_OPERATOR_EQUAL); + *filters.mutable_or_()->Add()->mutable_and_()->Add() = filter_field; + + int total; + + std::vector partitions = client.list_partitions(filters, total); + for (auto &&partition : partitions) { + std::cout << *partition.mutable_id() << std::endl; + } + ASSERT_TRUE(!partitions.empty()); + ASSERT_EQ(partitions.size(), 1); + ASSERT_EQ(partitions.size(), total); +} \ No newline at end of file From 8b7d3be742eb228a748f017643691299b2eb356e Mon Sep 17 00:00:00 2001 From: Daouda Diakite Date: Thu, 15 Feb 2024 17:45:48 +0100 Subject: [PATCH 08/15] Apply format --- .../cpp/ArmoniK.Api.Client/header/partitions/PartitionsClient.h | 2 +- .../ArmoniK.Api.Client/source/partitions/PartitionsClient.cpp | 2 +- .../cpp/ArmoniK.Api.Client/source/sessions/SessionsClient.cpp | 2 +- .../cpp/ArmoniK.Api.Client/source/versions/VersionsClient.cpp | 2 +- packages/cpp/ArmoniK.Api.Tests/source/PartitionsClientTest.cpp | 2 +- packages/cpp/ArmoniK.Api.Tests/source/VersionsClientTest.cpp | 2 +- 6 files changed, 6 insertions(+), 6 deletions(-) diff --git a/packages/cpp/ArmoniK.Api.Client/header/partitions/PartitionsClient.h b/packages/cpp/ArmoniK.Api.Client/header/partitions/PartitionsClient.h index c640e1b14..48c990879 100644 --- a/packages/cpp/ArmoniK.Api.Client/header/partitions/PartitionsClient.h +++ b/packages/cpp/ArmoniK.Api.Client/header/partitions/PartitionsClient.h @@ -24,4 +24,4 @@ class PartitionsClient { }; } // namespace client } // namespace api -} // namespace armonik \ No newline at end of file +} // namespace armonik diff --git a/packages/cpp/ArmoniK.Api.Client/source/partitions/PartitionsClient.cpp b/packages/cpp/ArmoniK.Api.Client/source/partitions/PartitionsClient.cpp index a2759a1a3..878e47099 100644 --- a/packages/cpp/ArmoniK.Api.Client/source/partitions/PartitionsClient.cpp +++ b/packages/cpp/ArmoniK.Api.Client/source/partitions/PartitionsClient.cpp @@ -80,4 +80,4 @@ armonik::api::grpc::v1::partitions::ListPartitionsRequest::Sort PartitionsClient } // namespace client } // namespace api -} // namespace armonik \ No newline at end of file +} // namespace armonik diff --git a/packages/cpp/ArmoniK.Api.Client/source/sessions/SessionsClient.cpp b/packages/cpp/ArmoniK.Api.Client/source/sessions/SessionsClient.cpp index 9b6d8e7d7..429500c2c 100644 --- a/packages/cpp/ArmoniK.Api.Client/source/sessions/SessionsClient.cpp +++ b/packages/cpp/ArmoniK.Api.Client/source/sessions/SessionsClient.cpp @@ -182,4 +182,4 @@ armonik::api::client::SessionsClient::stop_submission_session(std::string sessio } return std::move(*response.mutable_session()); -} \ No newline at end of file +} diff --git a/packages/cpp/ArmoniK.Api.Client/source/versions/VersionsClient.cpp b/packages/cpp/ArmoniK.Api.Client/source/versions/VersionsClient.cpp index 90a592adf..69f65f8a0 100644 --- a/packages/cpp/ArmoniK.Api.Client/source/versions/VersionsClient.cpp +++ b/packages/cpp/ArmoniK.Api.Client/source/versions/VersionsClient.cpp @@ -25,4 +25,4 @@ std::map VersionsClient::list_versions() { } } // namespace client } // namespace api -} // namespace armonik \ No newline at end of file +} // namespace armonik diff --git a/packages/cpp/ArmoniK.Api.Tests/source/PartitionsClientTest.cpp b/packages/cpp/ArmoniK.Api.Tests/source/PartitionsClientTest.cpp index 8e117fb57..f5ea9eca2 100644 --- a/packages/cpp/ArmoniK.Api.Tests/source/PartitionsClientTest.cpp +++ b/packages/cpp/ArmoniK.Api.Tests/source/PartitionsClientTest.cpp @@ -53,4 +53,4 @@ TEST(Partitions, can_list_partitions) { ASSERT_TRUE(!partitions.empty()); ASSERT_EQ(partitions.size(), 1); ASSERT_EQ(partitions.size(), total); -} \ No newline at end of file +} diff --git a/packages/cpp/ArmoniK.Api.Tests/source/VersionsClientTest.cpp b/packages/cpp/ArmoniK.Api.Tests/source/VersionsClientTest.cpp index 0d5c02ecc..7750c8df2 100644 --- a/packages/cpp/ArmoniK.Api.Tests/source/VersionsClientTest.cpp +++ b/packages/cpp/ArmoniK.Api.Tests/source/VersionsClientTest.cpp @@ -25,4 +25,4 @@ TEST(Versions, can_list_versions) { ASSERT_NE(versions.at("api"), "Unknown"); ASSERT_NE(versions.at("core"), "Unknown"); -} \ No newline at end of file +} From 5920c1946713b2e202b4c8b71f388ccc5750152e Mon Sep 17 00:00:00 2001 From: Daouda Diakite Date: Fri, 16 Feb 2024 11:46:24 +0100 Subject: [PATCH 09/15] Booleans for client and worker for stop submission methode --- .../cpp/ArmoniK.Api.Client/header/sessions/SessionsClient.h | 5 ++++- .../ArmoniK.Api.Client/source/sessions/SessionsClient.cpp | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/packages/cpp/ArmoniK.Api.Client/header/sessions/SessionsClient.h b/packages/cpp/ArmoniK.Api.Client/header/sessions/SessionsClient.h index ccc690076..528909ad4 100644 --- a/packages/cpp/ArmoniK.Api.Client/header/sessions/SessionsClient.h +++ b/packages/cpp/ArmoniK.Api.Client/header/sessions/SessionsClient.h @@ -93,9 +93,12 @@ class SessionsClient { * Stop a new tasks submission in a session * * @param session_id Session Id + * @param client boolean to stop client's task submission + * @param worker boolean to stop worker's task submissions * @return SessionRaw object containing information about the session */ - armonik::api::grpc::v1::sessions::SessionRaw stop_submission_session(std::string session_id); + armonik::api::grpc::v1::sessions::SessionRaw stop_submission_session(std::string session_id, bool client = true, + bool worker = true); private: std::unique_ptr stub; diff --git a/packages/cpp/ArmoniK.Api.Client/source/sessions/SessionsClient.cpp b/packages/cpp/ArmoniK.Api.Client/source/sessions/SessionsClient.cpp index 429500c2c..db23e59d4 100644 --- a/packages/cpp/ArmoniK.Api.Client/source/sessions/SessionsClient.cpp +++ b/packages/cpp/ArmoniK.Api.Client/source/sessions/SessionsClient.cpp @@ -169,12 +169,14 @@ armonik::api::client::SessionsClient::delete_session(std::string session_id) { } armonik::api::grpc::v1::sessions::SessionRaw -armonik::api::client::SessionsClient::stop_submission_session(std::string session_id) { +armonik::api::client::SessionsClient::stop_submission_session(std::string session_id, bool client, bool worker) { ::grpc::ClientContext context; armonik::api::grpc::v1::sessions::StopSubmissionRequest request; armonik::api::grpc::v1::sessions::StopSubmissionResponse response; request.set_session_id(std::move(session_id)); + request.set_client(client); + request.set_worker(worker); auto status = stub->StopSubmission(&context, request, &response); if (!status.ok()) { throw armonik::api::common::exceptions::ArmoniKApiException("Could not stop submission session : " + From e35ebdf03621dca8ac186c1f59510f88e70806d5 Mon Sep 17 00:00:00 2001 From: Daouda Diakite Date: Fri, 16 Feb 2024 12:15:16 +0100 Subject: [PATCH 10/15] Add close session method cpp --- .../header/sessions/SessionsClient.h | 8 ++++++++ .../source/sessions/SessionsClient.cpp | 15 +++++++++++++++ 2 files changed, 23 insertions(+) diff --git a/packages/cpp/ArmoniK.Api.Client/header/sessions/SessionsClient.h b/packages/cpp/ArmoniK.Api.Client/header/sessions/SessionsClient.h index 528909ad4..24c3da906 100644 --- a/packages/cpp/ArmoniK.Api.Client/header/sessions/SessionsClient.h +++ b/packages/cpp/ArmoniK.Api.Client/header/sessions/SessionsClient.h @@ -100,6 +100,14 @@ class SessionsClient { armonik::api::grpc::v1::sessions::SessionRaw stop_submission_session(std::string session_id, bool client = true, bool worker = true); + /** + * Resume a session + * + * @param session_id Session Id + * @return SessionRaw object containing information about the session + */ + armonik::api::grpc::v1::sessions::SessionRaw close_session(std::string session_id); + private: std::unique_ptr stub; static const armonik::api::grpc::v1::sessions::ListSessionsRequest::Sort default_sort; diff --git a/packages/cpp/ArmoniK.Api.Client/source/sessions/SessionsClient.cpp b/packages/cpp/ArmoniK.Api.Client/source/sessions/SessionsClient.cpp index db23e59d4..1b7573f18 100644 --- a/packages/cpp/ArmoniK.Api.Client/source/sessions/SessionsClient.cpp +++ b/packages/cpp/ArmoniK.Api.Client/source/sessions/SessionsClient.cpp @@ -185,3 +185,18 @@ armonik::api::client::SessionsClient::stop_submission_session(std::string sessio return std::move(*response.mutable_session()); } + +armonik::api::grpc::v1::sessions::SessionRaw +armonik::api::client::SessionsClient::close_session(std::string session_id) { + ::grpc::ClientContext context; + armonik::api::grpc::v1::sessions::CloseSessionRequest request; + armonik::api::grpc::v1::sessions::CloseSessionResponse response; + + request.set_session_id(std::move(session_id)); + auto status = stub->CloseSession(&context, request, &response); + if (!status.ok()) { + throw armonik::api::common::exceptions::ArmoniKApiException("Could not close session : " + status.error_message()); + } + + return std::move(*response.mutable_session()); +} From c4313779da26566c2ecf05208bfd06283b38ce9f Mon Sep 17 00:00:00 2001 From: Daouda Diakite Date: Fri, 16 Feb 2024 12:15:38 +0100 Subject: [PATCH 11/15] Test close session method cpp --- .../source/SessionClientTest.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/cpp/ArmoniK.Api.Tests/source/SessionClientTest.cpp b/packages/cpp/ArmoniK.Api.Tests/source/SessionClientTest.cpp index 9161ab595..a48ba9829 100644 --- a/packages/cpp/ArmoniK.Api.Tests/source/SessionClientTest.cpp +++ b/packages/cpp/ArmoniK.Api.Tests/source/SessionClientTest.cpp @@ -144,6 +144,8 @@ TEST(Sessions, can_purge_session) { std::string session_id = client.create_session(task_options); + ASSERT_NO_THROW(client.close_session(session_id)); + armonik::api::grpc::v1::sessions::SessionRaw response; ASSERT_NO_THROW(response = client.purge_session(session_id)); ASSERT_EQ(response.session_id(), session_id); @@ -178,3 +180,18 @@ TEST(Sessions, can_stop_submission) { ASSERT_NO_THROW(response = client.stop_submission_session(session_id)); ASSERT_EQ(response.session_id(), session_id); } + +TEST(Sessions, can_close_session) { + Logger log{armonik::api::common::logger::writer_console(), armonik::api::common::logger::formatter_plain(true)}; + std::shared_ptr<::grpc::Channel> channel; + armonik::api::grpc::v1::TaskOptions task_options; + init(channel, task_options, log); + + armonik::api::client::SessionsClient client(armonik::api::grpc::v1::sessions::Sessions::NewStub(channel)); + + std::string session_id = client.create_session(task_options); + + armonik::api::grpc::v1::sessions::SessionRaw response; + ASSERT_NO_THROW(response = client.close_session(session_id)); + ASSERT_EQ(response.session_id(), session_id); +} From cb38078d33424866d068f780a93aba78f576808a Mon Sep 17 00:00:00 2001 From: Daouda Diakite Date: Fri, 16 Feb 2024 16:07:40 +0100 Subject: [PATCH 12/15] Add result availability for events service cpp --- .../header/events/EventsClient.h | 21 +++++ .../source/events/EventsClient.cpp | 80 +++++++++++++++++++ 2 files changed, 101 insertions(+) create mode 100644 packages/cpp/ArmoniK.Api.Client/header/events/EventsClient.h create mode 100644 packages/cpp/ArmoniK.Api.Client/source/events/EventsClient.cpp diff --git a/packages/cpp/ArmoniK.Api.Client/header/events/EventsClient.h b/packages/cpp/ArmoniK.Api.Client/header/events/EventsClient.h new file mode 100644 index 000000000..adfcaf9a1 --- /dev/null +++ b/packages/cpp/ArmoniK.Api.Client/header/events/EventsClient.h @@ -0,0 +1,21 @@ +#pragma once + +#include "events_common.pb.h" +#include "events_service.grpc.pb.h" + +namespace armonik { +namespace api { +namespace client { +class EventsClient { +public: + explicit EventsClient(std::unique_ptr stub) + : stub(std::move(stub)) {} + + void wait_for_result_availability(std::string session_id, std::vector result_ids); + +private: + std::unique_ptr stub; +}; +} // namespace client +} // namespace api +} // namespace armonik diff --git a/packages/cpp/ArmoniK.Api.Client/source/events/EventsClient.cpp b/packages/cpp/ArmoniK.Api.Client/source/events/EventsClient.cpp new file mode 100644 index 000000000..18c9c89c3 --- /dev/null +++ b/packages/cpp/ArmoniK.Api.Client/source/events/EventsClient.cpp @@ -0,0 +1,80 @@ +#include "events/EventsClient.h" +#include "events_common.pb.h" +#include "events_service.grpc.pb.h" +#include "exceptions/ArmoniKApiException.h" +#include "objects.pb.h" +#include "results_service.grpc.pb.h" + +namespace armonik { +namespace api { +namespace client { + +void EventsClient::wait_for_result_availability(std::string session_id, std::vector result_ids) { + + ::grpc::ClientContext context; + armonik::api::grpc::v1::events::EventSubscriptionRequest request; + + armonik::api::grpc::v1::events::EventSubscriptionResponse response; + + armonik::api::grpc::v1::results::Filters filters; + armonik::api::grpc::v1::results::FilterField filter_field; + filter_field.mutable_field()->mutable_result_raw_field()->set_field( + armonik::api::grpc::v1::results::RESULT_RAW_ENUM_FIELD_RESULT_ID); + filter_field.mutable_filter_string()->set_operator_(grpc::v1::FILTER_STRING_OPERATOR_EQUAL); + for (auto &&result_id : result_ids) { + filter_field.mutable_filter_string()->set_value(result_id); + *filters.mutable_or_()->Add()->mutable_and_()->Add() = filter_field; + } + + *request.mutable_session_id() = std::move(session_id); + *request.mutable_results_filters() = filters; + request.add_returned_events(static_cast( + armonik::api::grpc::v1::events::EventSubscriptionResponse::UpdateCase::kResultStatusUpdate)); + request.add_returned_events(static_cast( + armonik::api::grpc::v1::events::EventSubscriptionResponse::UpdateCase::kNewResult)); + + auto stream = stub->GetEvents(&context, request); + if (!stream) { + throw armonik::api::common::exceptions::ArmoniKApiException("Result has been aborted"); + } + + while (stream->Read(&response)) { + if (response.update_case() == + armonik::api::grpc::v1::events::EventSubscriptionResponse::UpdateCase::kResultStatusUpdate) { + if (response.mutable_result_status_update()->status() == + armonik::api::grpc::v1::result_status::ResultStatus::RESULT_STATUS_COMPLETED) { + result_ids.erase( + std::remove(result_ids.begin(), result_ids.end(), response.mutable_result_status_update()->result_id()), + result_ids.end()); + if (result_ids.empty()) { + break; + } + } + + if (response.mutable_result_status_update()->status() == + armonik::api::grpc::v1::result_status::ResultStatus::RESULT_STATUS_ABORTED) { + throw armonik::api::common::exceptions::ArmoniKApiException("Result has been aborted"); + } + } + + if (response.update_case() == armonik::api::grpc::v1::events::EventSubscriptionResponse::UpdateCase::kNewResult) { + if (response.mutable_new_result()->status() == + armonik::api::grpc::v1::result_status::ResultStatus::RESULT_STATUS_COMPLETED) { + result_ids.erase(std::remove(result_ids.begin(), result_ids.end(), response.mutable_new_result()->result_id()), + result_ids.end()); + if (result_ids.empty()) { + break; + } + } + + if (response.mutable_new_result()->status() == + armonik::api::grpc::v1::result_status::ResultStatus::RESULT_STATUS_ABORTED) { + throw armonik::api::common::exceptions::ArmoniKApiException("Result has been aborted"); + } + } + } +} + +} // namespace client +} // namespace api +} // namespace armonik From 70f7630409aa765ab72ebc62218594858d08223c Mon Sep 17 00:00:00 2001 From: Daouda Diakite Date: Fri, 16 Feb 2024 16:18:16 +0100 Subject: [PATCH 13/15] Test wait for results --- .../source/EventsClientTest.cpp | 34 +++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 packages/cpp/ArmoniK.Api.Tests/source/EventsClientTest.cpp diff --git a/packages/cpp/ArmoniK.Api.Tests/source/EventsClientTest.cpp b/packages/cpp/ArmoniK.Api.Tests/source/EventsClientTest.cpp new file mode 100644 index 000000000..6a998f49a --- /dev/null +++ b/packages/cpp/ArmoniK.Api.Tests/source/EventsClientTest.cpp @@ -0,0 +1,34 @@ +#include + +#include "common.h" +#include "logger/formatter.h" +#include "logger/logger.h" +#include "logger/writer.h" + +#include "events/EventsClient.h" +#include "results/ResultsClient.h" +#include "results_service.grpc.pb.h" +#include "sessions/SessionsClient.h" + +using Logger = armonik::api::common::logger::Logger; + +TEST(Events, getEvents) { + Logger log{armonik::api::common::logger::writer_console(), armonik::api::common::logger::formatter_plain(true)}; + std::shared_ptr<::grpc::Channel> channel; + armonik::api::grpc::v1::TaskOptions task_options; + init(channel, task_options, log); + + armonik::api::client::EventsClient client(armonik::api::grpc::v1::events::Events::NewStub(channel)); + + auto session_id = armonik::api::client::SessionsClient(armonik::api::grpc::v1::sessions::Sessions::NewStub(channel)) + .create_session(task_options); + + auto result_client = armonik::api::client::ResultsClient(armonik::api::grpc::v1::results::Results::NewStub(channel)); + auto payload_id = result_client.create_results( + session_id, std::vector>{{"name", "payload"}})["name"]; + auto result_id = result_client.create_results_metadata(session_id, {"result"})["result"]; + + ASSERT_NO_THROW(result_client.upload_result_data(session_id, result_id, "name")); + ASSERT_NO_THROW(client.wait_for_result_availability(session_id, {result_id, payload_id})); + ASSERT_EQ(result_client.download_result_data(session_id, result_id), "name"); +} From 2e45747b839d903aa695903e91f41b93736ec814 Mon Sep 17 00:00:00 2001 From: Daouda Diakite Date: Mon, 19 Feb 2024 11:12:05 +0100 Subject: [PATCH 14/15] PR comments --- .../header/versions/VersionsClient.h | 12 +++- .../source/events/EventsClient.cpp | 70 +++++++++---------- .../source/versions/VersionsClient.cpp | 7 +- .../source/VersionsClientTest.cpp | 10 +-- 4 files changed, 53 insertions(+), 46 deletions(-) diff --git a/packages/cpp/ArmoniK.Api.Client/header/versions/VersionsClient.h b/packages/cpp/ArmoniK.Api.Client/header/versions/VersionsClient.h index 35ffe94dd..8e771cb3c 100644 --- a/packages/cpp/ArmoniK.Api.Client/header/versions/VersionsClient.h +++ b/packages/cpp/ArmoniK.Api.Client/header/versions/VersionsClient.h @@ -7,6 +7,16 @@ namespace armonik { namespace api { namespace client { +/** + * @brief Data structure for components version + * @param api ArmoniK API version + * @param core ArmoniK CORE version + */ +struct versions_info { + std::string api; + std::string core; +}; + /** * Versions Client wrapper */ @@ -19,7 +29,7 @@ class VersionsClient { * Get versions of ArmoniK components * @return Mapping between component names and their versions */ - std::map list_versions(); + versions_info list_versions(); private: std::unique_ptr stub; diff --git a/packages/cpp/ArmoniK.Api.Client/source/events/EventsClient.cpp b/packages/cpp/ArmoniK.Api.Client/source/events/EventsClient.cpp index 18c9c89c3..0d3f01fda 100644 --- a/packages/cpp/ArmoniK.Api.Client/source/events/EventsClient.cpp +++ b/packages/cpp/ArmoniK.Api.Client/source/events/EventsClient.cpp @@ -3,7 +3,11 @@ #include "events_service.grpc.pb.h" #include "exceptions/ArmoniKApiException.h" #include "objects.pb.h" -#include "results_service.grpc.pb.h" + +using armonik::api::grpc::v1::events::EventSubscriptionRequest; +using armonik::api::grpc::v1::events::EventSubscriptionResponse; +using armonik::api::grpc::v1::result_status::ResultStatus; +using namespace armonik::api::grpc::v1::events; namespace armonik { namespace api { @@ -12,9 +16,9 @@ namespace client { void EventsClient::wait_for_result_availability(std::string session_id, std::vector result_ids) { ::grpc::ClientContext context; - armonik::api::grpc::v1::events::EventSubscriptionRequest request; + EventSubscriptionRequest request; - armonik::api::grpc::v1::events::EventSubscriptionResponse response; + EventSubscriptionResponse response; armonik::api::grpc::v1::results::Filters filters; armonik::api::grpc::v1::results::FilterField filter_field; @@ -28,10 +32,8 @@ void EventsClient::wait_for_result_availability(std::string session_id, std::vec *request.mutable_session_id() = std::move(session_id); *request.mutable_results_filters() = filters; - request.add_returned_events(static_cast( - armonik::api::grpc::v1::events::EventSubscriptionResponse::UpdateCase::kResultStatusUpdate)); - request.add_returned_events(static_cast( - armonik::api::grpc::v1::events::EventSubscriptionResponse::UpdateCase::kNewResult)); + request.add_returned_events(static_cast(EventSubscriptionResponse::UpdateCase::kResultStatusUpdate)); + request.add_returned_events(static_cast(EventSubscriptionResponse::UpdateCase::kNewResult)); auto stream = stub->GetEvents(&context, request); if (!stream) { @@ -39,38 +41,36 @@ void EventsClient::wait_for_result_availability(std::string session_id, std::vec } while (stream->Read(&response)) { - if (response.update_case() == - armonik::api::grpc::v1::events::EventSubscriptionResponse::UpdateCase::kResultStatusUpdate) { - if (response.mutable_result_status_update()->status() == - armonik::api::grpc::v1::result_status::ResultStatus::RESULT_STATUS_COMPLETED) { - result_ids.erase( - std::remove(result_ids.begin(), result_ids.end(), response.mutable_result_status_update()->result_id()), - result_ids.end()); - if (result_ids.empty()) { - break; - } - } - - if (response.mutable_result_status_update()->status() == - armonik::api::grpc::v1::result_status::ResultStatus::RESULT_STATUS_ABORTED) { + std::string update_or_new; + switch (response.update_case()) { + case EventSubscriptionResponse::UpdateCase::kResultStatusUpdate: + switch (response.mutable_result_status_update()->status()) { + case ResultStatus::RESULT_STATUS_COMPLETED: + update_or_new = response.mutable_result_status_update()->result_id(); + break; + case ResultStatus::RESULT_STATUS_ABORTED: throw armonik::api::common::exceptions::ArmoniKApiException("Result has been aborted"); + default: + break; } - } - - if (response.update_case() == armonik::api::grpc::v1::events::EventSubscriptionResponse::UpdateCase::kNewResult) { - if (response.mutable_new_result()->status() == - armonik::api::grpc::v1::result_status::ResultStatus::RESULT_STATUS_COMPLETED) { - result_ids.erase(std::remove(result_ids.begin(), result_ids.end(), response.mutable_new_result()->result_id()), - result_ids.end()); - if (result_ids.empty()) { - break; - } - } - - if (response.mutable_new_result()->status() == - armonik::api::grpc::v1::result_status::ResultStatus::RESULT_STATUS_ABORTED) { + break; + case EventSubscriptionResponse::UpdateCase::kNewResult: + switch (response.mutable_new_result()->status()) { + case ResultStatus::RESULT_STATUS_COMPLETED: + update_or_new = response.mutable_new_result()->result_id(); + break; + case ResultStatus::RESULT_STATUS_ABORTED: throw armonik::api::common::exceptions::ArmoniKApiException("Result has been aborted"); + default: + break; } + break; + default: + break; + } + result_ids.erase(std::remove(result_ids.begin(), result_ids.end(), update_or_new), result_ids.end()); + if (result_ids.empty()) { + break; } } } diff --git a/packages/cpp/ArmoniK.Api.Client/source/versions/VersionsClient.cpp b/packages/cpp/ArmoniK.Api.Client/source/versions/VersionsClient.cpp index 69f65f8a0..f59b1b420 100644 --- a/packages/cpp/ArmoniK.Api.Client/source/versions/VersionsClient.cpp +++ b/packages/cpp/ArmoniK.Api.Client/source/versions/VersionsClient.cpp @@ -5,21 +5,18 @@ namespace armonik { namespace api { namespace client { -std::map VersionsClient::list_versions() { +versions_info VersionsClient::list_versions() { ::grpc::ClientContext context; armonik::api::grpc::v1::versions::ListVersionsRequest request; armonik::api::grpc::v1::versions::ListVersionsResponse response; - std::map mapping; - auto status = stub->ListVersions(&context, request, &response); if (!status.ok()) { throw armonik::api::common::exceptions::ArmoniKApiException("Could not get list versions : " + status.error_message()); } - mapping.insert({"api", response.api()}); - mapping.insert({"core", response.core()}); + versions_info mapping = {response.api(), response.core()}; return mapping; } diff --git a/packages/cpp/ArmoniK.Api.Tests/source/VersionsClientTest.cpp b/packages/cpp/ArmoniK.Api.Tests/source/VersionsClientTest.cpp index 7750c8df2..5d98832cc 100644 --- a/packages/cpp/ArmoniK.Api.Tests/source/VersionsClientTest.cpp +++ b/packages/cpp/ArmoniK.Api.Tests/source/VersionsClientTest.cpp @@ -17,12 +17,12 @@ TEST(Versions, can_list_versions) { armonik::api::client::VersionsClient client(armonik::api::grpc::v1::versions::Versions::NewStub(channel)); - std::map versions; + armonik::api::client::versions_info versions; ASSERT_NO_THROW(versions = client.list_versions()); - std::cout << "API version: " << versions.at("api") << "\n" - << "Core version: " << versions.at("core") << std::endl; + std::cout << "API version: " << versions.api << "\n" + << "Core version: " << versions.core << std::endl; - ASSERT_NE(versions.at("api"), "Unknown"); - ASSERT_NE(versions.at("core"), "Unknown"); + ASSERT_NE(versions.api, "Unknown"); + ASSERT_NE(versions.core, "Unknown"); } From f9b8d527c9c6a15fbee57a4963d5799a4cd0c465 Mon Sep 17 00:00:00 2001 From: Daouda Diakite Date: Mon, 19 Feb 2024 15:20:32 +0100 Subject: [PATCH 15/15] More PR comments --- .../source/events/EventsClient.cpp | 8 ++- .../source/partitions/PartitionsClient.cpp | 34 ++++----- .../source/sessions/SessionsClient.cpp | 72 +++++++++---------- .../source/versions/VersionsClient.cpp | 8 ++- 4 files changed, 62 insertions(+), 60 deletions(-) diff --git a/packages/cpp/ArmoniK.Api.Client/source/events/EventsClient.cpp b/packages/cpp/ArmoniK.Api.Client/source/events/EventsClient.cpp index 0d3f01fda..84ffe4e81 100644 --- a/packages/cpp/ArmoniK.Api.Client/source/events/EventsClient.cpp +++ b/packages/cpp/ArmoniK.Api.Client/source/events/EventsClient.cpp @@ -68,9 +68,11 @@ void EventsClient::wait_for_result_availability(std::string session_id, std::vec default: break; } - result_ids.erase(std::remove(result_ids.begin(), result_ids.end(), update_or_new), result_ids.end()); - if (result_ids.empty()) { - break; + if (!update_or_new.empty()) { + result_ids.erase(std::remove(result_ids.begin(), result_ids.end(), update_or_new), result_ids.end()); + if (result_ids.empty()) { + break; + } } } } diff --git a/packages/cpp/ArmoniK.Api.Client/source/partitions/PartitionsClient.cpp b/packages/cpp/ArmoniK.Api.Client/source/partitions/PartitionsClient.cpp index 878e47099..3c8e05609 100644 --- a/packages/cpp/ArmoniK.Api.Client/source/partitions/PartitionsClient.cpp +++ b/packages/cpp/ArmoniK.Api.Client/source/partitions/PartitionsClient.cpp @@ -3,17 +3,17 @@ #include "exceptions/ArmoniKApiException.h" #include "partitions/PartitionsClient.h" +using namespace armonik::api::grpc::v1::partitions; + namespace armonik { namespace api { namespace client { -std::vector -PartitionsClient::list_partitions(armonik::api::grpc::v1::partitions::Filters filters, int32_t &total, int32_t page, - int32_t page_size, - armonik::api::grpc::v1::partitions::ListPartitionsRequest::Sort sort) { +std::vector PartitionsClient::list_partitions(Filters filters, int32_t &total, int32_t page, + int32_t page_size, ListPartitionsRequest::Sort sort) { ::grpc::ClientContext context; - armonik::api::grpc::v1::partitions::ListPartitionsRequest request; - armonik::api::grpc::v1::partitions::ListPartitionsResponse response; + ListPartitionsRequest request; + ListPartitionsResponse response; *request.mutable_filters() = std::move(filters); *request.mutable_sort() = std::move(sort); @@ -28,9 +28,10 @@ PartitionsClient::list_partitions(armonik::api::grpc::v1::partitions::Filters fi status.error_message()); } total = response.total(); - return {response.partitions().begin(), response.partitions().end()}; + return {std::make_move_iterator(response.partitions().begin()), + std::make_move_iterator(response.partitions().end())}; } else { - std::vector rawPartitions; + std::vector rawPartitions; int current_page = 0; do { request.set_page(current_page); @@ -40,9 +41,8 @@ PartitionsClient::list_partitions(armonik::api::grpc::v1::partitions::Filters fi throw armonik::api::common::exceptions::ArmoniKApiException("Unable to list partitions " + status.error_message()); } - rawPartitions.insert(rawPartitions.end(), - response.partitions().begin() + ((int32_t)rawPartitions.size() - current_page * page_size), - response.partitions().end()); + rawPartitions.insert(rawPartitions.end(), std::make_move_iterator(response.partitions().begin()), + std::make_move_iterator(response.partitions().end())); if (response.partitions_size() >= page_size) { current_page++; } @@ -56,10 +56,10 @@ PartitionsClient::list_partitions(armonik::api::grpc::v1::partitions::Filters fi } } -armonik::api::grpc::v1::partitions::PartitionRaw PartitionsClient::get_partition(std::string partition_id) { +PartitionRaw PartitionsClient::get_partition(std::string partition_id) { ::grpc::ClientContext context; - armonik::api::grpc::v1::partitions::GetPartitionRequest request; - armonik::api::grpc::v1::partitions::GetPartitionResponse response; + GetPartitionRequest request; + GetPartitionResponse response; *request.mutable_id() = std::move(partition_id); auto status = stub->GetPartition(&context, request, &response); @@ -67,11 +67,11 @@ armonik::api::grpc::v1::partitions::PartitionRaw PartitionsClient::get_partition throw armonik::api::common::exceptions::ArmoniKApiException("Could not get partition : " + status.error_message()); } - return response.partition(); + return std::move(*response.mutable_partition()); } -armonik::api::grpc::v1::partitions::ListPartitionsRequest::Sort PartitionsClient::default_sort() { - armonik::api::grpc::v1::partitions::ListPartitionsRequest::Sort sort; +ListPartitionsRequest::Sort PartitionsClient::default_sort() { + ListPartitionsRequest::Sort sort; sort.set_direction(grpc::v1::sort_direction::SORT_DIRECTION_ASC); sort.mutable_field()->mutable_partition_raw_field()->set_field( grpc::v1::partitions::PARTITION_RAW_ENUM_FIELD_PRIORITY); diff --git a/packages/cpp/ArmoniK.Api.Client/source/sessions/SessionsClient.cpp b/packages/cpp/ArmoniK.Api.Client/source/sessions/SessionsClient.cpp index 1b7573f18..31cce4d07 100644 --- a/packages/cpp/ArmoniK.Api.Client/source/sessions/SessionsClient.cpp +++ b/packages/cpp/ArmoniK.Api.Client/source/sessions/SessionsClient.cpp @@ -4,22 +4,23 @@ #include "sessions_service.grpc.pb.h" #include -static armonik::api::grpc::v1::sessions::ListSessionsRequest::Sort get_default_sort() { - armonik::api::grpc::v1::sessions::ListSessionsRequest::Sort sort; +using namespace armonik::api::grpc::v1::sessions; + +static ListSessionsRequest::Sort get_default_sort() { + ListSessionsRequest::Sort sort; sort.mutable_field()->mutable_session_raw_field()->set_field( armonik::api::grpc::v1::sessions::SESSION_RAW_ENUM_FIELD_CREATED_AT); sort.set_direction(armonik::api::grpc::v1::sort_direction::SORT_DIRECTION_ASC); return sort; } -const armonik::api::grpc::v1::sessions::ListSessionsRequest::Sort armonik::api::client::SessionsClient::default_sort = - get_default_sort(); +const ListSessionsRequest::Sort armonik::api::client::SessionsClient::default_sort = get_default_sort(); std::string armonik::api::client::SessionsClient::create_session(armonik::api::grpc::v1::TaskOptions default_task_options, const std::vector &partitions) { ::grpc::ClientContext context; - armonik::api::grpc::v1::sessions::CreateSessionRequest request; - armonik::api::grpc::v1::sessions::CreateSessionReply response; + CreateSessionRequest request; + CreateSessionReply response; *request.mutable_default_task_option() = std::move(default_task_options); request.mutable_partition_ids()->Add(partitions.begin(), partitions.end()); @@ -31,10 +32,10 @@ armonik::api::client::SessionsClient::create_session(armonik::api::grpc::v1::Tas return std::move(*response.mutable_session_id()); } -armonik::api::grpc::v1::sessions::SessionRaw armonik::api::client::SessionsClient::get_session(std::string session_id) { +SessionRaw armonik::api::client::SessionsClient::get_session(std::string session_id) { ::grpc::ClientContext context; - armonik::api::grpc::v1::sessions::GetSessionRequest request; - armonik::api::grpc::v1::sessions::GetSessionResponse response; + GetSessionRequest request; + GetSessionResponse response; request.set_session_id(std::move(session_id)); @@ -45,11 +46,10 @@ armonik::api::grpc::v1::sessions::SessionRaw armonik::api::client::SessionsClien return std::move(*response.mutable_session()); } -armonik::api::grpc::v1::sessions::SessionRaw -armonik::api::client::SessionsClient::cancel_session(std::string session_id) { +SessionRaw armonik::api::client::SessionsClient::cancel_session(std::string session_id) { ::grpc::ClientContext context; - armonik::api::grpc::v1::sessions::CancelSessionRequest request; - armonik::api::grpc::v1::sessions::CancelSessionResponse response; + CancelSessionRequest request; + CancelSessionResponse response; request.set_session_id(std::move(session_id)); auto status = stub->CancelSession(&context, request, &response); @@ -63,8 +63,8 @@ std::vector armonik::api::client::SessionsClient::list_sessions(armonik::api::grpc::v1::sessions::Filters filters, int32_t &total, int32_t page, int32_t page_size, armonik::api::grpc::v1::sessions::ListSessionsRequest::Sort sort) { - armonik::api::grpc::v1::sessions::ListSessionsRequest request; - armonik::api::grpc::v1::sessions::ListSessionsResponse response; + ListSessionsRequest request; + ListSessionsResponse response; *request.mutable_filters() = std::move(filters); *request.mutable_sort() = std::move(sort); @@ -81,7 +81,7 @@ armonik::api::client::SessionsClient::list_sessions(armonik::api::grpc::v1::sess total = response.total(); return {response.sessions().begin(), response.sessions().end()}; } else { - std::vector rawSessions; + std::vector rawSessions; int current_page = 0; do { request.set_page(current_page); @@ -108,11 +108,10 @@ armonik::api::client::SessionsClient::list_sessions(armonik::api::grpc::v1::sess } } -armonik::api::grpc::v1::sessions::SessionRaw -armonik::api::client::SessionsClient::pause_session(std::string session_id) { +SessionRaw armonik::api::client::SessionsClient::pause_session(std::string session_id) { ::grpc::ClientContext context; - armonik::api::grpc::v1::sessions::PauseSessionRequest request; - armonik::api::grpc::v1::sessions::PauseSessionResponse response; + PauseSessionRequest request; + PauseSessionResponse response; request.set_session_id(std::move(session_id)); auto status = stub->PauseSession(&context, request, &response); @@ -126,8 +125,8 @@ armonik::api::client::SessionsClient::pause_session(std::string session_id) { armonik::api::grpc::v1::sessions::SessionRaw armonik::api::client::SessionsClient::resume_session(std::string session_id) { ::grpc::ClientContext context; - armonik::api::grpc::v1::sessions::ResumeSessionRequest request; - armonik::api::grpc::v1::sessions::ResumeSessionResponse response; + ResumeSessionRequest request; + ResumeSessionResponse response; request.set_session_id(std::move(session_id)); auto status = stub->ResumeSession(&context, request, &response); @@ -138,11 +137,10 @@ armonik::api::client::SessionsClient::resume_session(std::string session_id) { return std::move(*response.mutable_session()); } -armonik::api::grpc::v1::sessions::SessionRaw -armonik::api::client::SessionsClient::purge_session(std::string session_id) { +SessionRaw armonik::api::client::SessionsClient::purge_session(std::string session_id) { ::grpc::ClientContext context; - armonik::api::grpc::v1::sessions::PurgeSessionRequest request; - armonik::api::grpc::v1::sessions::PurgeSessionResponse response; + PurgeSessionRequest request; + PurgeSessionResponse response; request.set_session_id(std::move(session_id)); auto status = stub->PurgeSession(&context, request, &response); @@ -153,11 +151,10 @@ armonik::api::client::SessionsClient::purge_session(std::string session_id) { return std::move(*response.mutable_session()); } -armonik::api::grpc::v1::sessions::SessionRaw -armonik::api::client::SessionsClient::delete_session(std::string session_id) { +SessionRaw armonik::api::client::SessionsClient::delete_session(std::string session_id) { ::grpc::ClientContext context; - armonik::api::grpc::v1::sessions::DeleteSessionRequest request; - armonik::api::grpc::v1::sessions::DeleteSessionResponse response; + DeleteSessionRequest request; + DeleteSessionResponse response; request.set_session_id(std::move(session_id)); auto status = stub->DeleteSession(&context, request, &response); @@ -168,11 +165,11 @@ armonik::api::client::SessionsClient::delete_session(std::string session_id) { return std::move(*response.mutable_session()); } -armonik::api::grpc::v1::sessions::SessionRaw -armonik::api::client::SessionsClient::stop_submission_session(std::string session_id, bool client, bool worker) { +SessionRaw armonik::api::client::SessionsClient::stop_submission_session(std::string session_id, bool client, + bool worker) { ::grpc::ClientContext context; - armonik::api::grpc::v1::sessions::StopSubmissionRequest request; - armonik::api::grpc::v1::sessions::StopSubmissionResponse response; + StopSubmissionRequest request; + StopSubmissionResponse response; request.set_session_id(std::move(session_id)); request.set_client(client); @@ -186,11 +183,10 @@ armonik::api::client::SessionsClient::stop_submission_session(std::string sessio return std::move(*response.mutable_session()); } -armonik::api::grpc::v1::sessions::SessionRaw -armonik::api::client::SessionsClient::close_session(std::string session_id) { +SessionRaw armonik::api::client::SessionsClient::close_session(std::string session_id) { ::grpc::ClientContext context; - armonik::api::grpc::v1::sessions::CloseSessionRequest request; - armonik::api::grpc::v1::sessions::CloseSessionResponse response; + CloseSessionRequest request; + CloseSessionResponse response; request.set_session_id(std::move(session_id)); auto status = stub->CloseSession(&context, request, &response); diff --git a/packages/cpp/ArmoniK.Api.Client/source/versions/VersionsClient.cpp b/packages/cpp/ArmoniK.Api.Client/source/versions/VersionsClient.cpp index f59b1b420..088c67e5a 100644 --- a/packages/cpp/ArmoniK.Api.Client/source/versions/VersionsClient.cpp +++ b/packages/cpp/ArmoniK.Api.Client/source/versions/VersionsClient.cpp @@ -1,14 +1,18 @@ #include "versions/VersionsClient.h" #include "exceptions/ArmoniKApiException.h" +using armonik::api::grpc::v1::versions::ListVersionsRequest; +using armonik::api::grpc::v1::versions::ListVersionsResponse; +using namespace armonik::api::grpc::v1::versions; + namespace armonik { namespace api { namespace client { versions_info VersionsClient::list_versions() { ::grpc::ClientContext context; - armonik::api::grpc::v1::versions::ListVersionsRequest request; - armonik::api::grpc::v1::versions::ListVersionsResponse response; + ListVersionsRequest request; + ListVersionsResponse response; auto status = stub->ListVersions(&context, request, &response); if (!status.ok()) {