Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .devcontainer/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ ENV TZ="Europe/Berlin"

RUN apt-get update -y && \
apt-get autoremove -y
RUN apt-get install -y software-properties-common bash-completion cmake build-essential iputils-ping rsync p7zip-full libuv1-dev git nlohmann-json3-dev librabbitmq-dev libboost-dev libgtest-dev pybind11-dev libgmock-dev flex bison
RUN apt-get install -y software-properties-common bash-completion cmake build-essential iputils-ping rsync p7zip-full libuv1-dev git librabbitmq-dev libboost-dev libgtest-dev pybind11-dev libgmock-dev flex bison

WORKDIR /home

Expand Down
5 changes: 2 additions & 3 deletions .github/workflows/mqss_client-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ jobs:
sudo apt-get install -y \
software-properties-common \
cmake \
nlohmann-json3-dev \
librabbitmq-dev \
libboost-dev \
libgtest-dev \
Expand All @@ -40,12 +39,12 @@ jobs:

- name: Configure project
run: |
cmake -S . -B build -DBUILD_UNIT_TESTS=ON
cmake -S . -B build -DMQSSCLIENT_BUILD_UNIT_TESTS=ON

- name: Build
run: |
cmake --build build --parallel

- name: Run tests
run: |
ctest --test-dir build/tests/unit/c++/ --output-on-failure
ctest --test-dir build --output-on-failure --no-tests=error
27 changes: 14 additions & 13 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ cmake_minimum_required(VERSION 3.19...3.30)

project(
MQSS-Client
VERSION 0.1
VERSION 0.3.0
DESCRIPTION "MQSS Client"
LANGUAGES CXX C)

Expand All @@ -29,16 +29,16 @@ set(CMAKE_EXPORT_COMPILE_COMMANDS
ON
CACHE BOOL "Export compile commands" FORCE)

option(BUILD_TESTS "Build the tests" OFF)
option(BUILD_UNIT_TESTS "Build the unit tests" OFF)
option(BUILD_INTEGRATION_TESTS "Build the unit tests" OFF)
option(ENABLE_COVERAGE "Enabling coverage" OFF)
option(BUILD_BINDINGS "Build Python Bindings" OFF)
option(BUILD_SHARED_LIBS "Build Shared Library " ON)
option(MQSSCLIENT_BUILD_TESTS "Build the tests" OFF)
option(MQSSCLIENT_BUILD_UNIT_TESTS "Build the unit tests" OFF)
option(MQSSCLIENT_BUILD_INTEGRATION_TESTS "Build the unit tests" OFF)
option(MQSSCLIENT_ENABLE_COVERAGE "Enabling coverage" OFF)
option(MQSSCLIENT_BUILD_PYTHON_BINDINGS "Build Python Bindings" OFF)
option(MQSSCLIENT_BUILD_SHARED_LIBS "Build Shared Library " ON)

list(APPEND CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake")

if(ENABLE_COVERAGE)
if(MQSSCLIENT_ENABLE_COVERAGE)
add_library(coverage_flags INTERFACE)

target_compile_options(coverage_flags INTERFACE --coverage -fprofile-arcs -ftest-coverage -O0)
Expand All @@ -56,22 +56,23 @@ set(CMAKE_CXX_EXTENSIONS OFF)

include(cmake/ExternalDependencies.cmake)

if(BUILD_TESTS OR BUILD_UNIT_TESTS)
if(MQSSCLIENT_BUILD_TESTS OR MQSSCLIENT_BUILD_UNIT_TESTS)
add_compile_definitions(ENABLE_UNIT_TEST)
endif()

add_subdirectory(src)

if(BUILD_TESTS
OR BUILD_UNIT_TESTS
OR BUILD_INTEGRATION_TESTS)
if(MQSSCLIENT_BUILD_TESTS
OR MQSSCLIENT_BUILD_UNIT_TESTS
OR MQSSCLIENT_BUILD_INTEGRATION_TESTS)
enable_testing()
add_subdirectory(tests)
endif()

if(BUILD_DOCUMENTATION)
add_subdirectory(docs)
endif()

if(BUILD_BINDINGS)
if(MQSSCLIENT_BUILD_PYTHON_BINDINGS)
add_subdirectory(bindings)
endif()
15 changes: 15 additions & 0 deletions cmake/ExternalDependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,21 @@
include(FetchContent)
set(FETCH_PACKAGES "")

set(NLOHMANN_JSON_VERSION
3.12.0
CACHE STRING "Minimum nlohmann_json version and fallback release")
find_package(nlohmann_json ${NLOHMANN_JSON_VERSION} CONFIG QUIET)
if(NOT nlohmann_json_FOUND)
# The public dependency must be exported alongside the installed MQSS targets.
set(JSON_Install ON)
FetchContent_Declare(
nlohmann_json
GIT_REPOSITORY https://github.com/nlohmann/json
GIT_TAG v${NLOHMANN_JSON_VERSION}
GIT_SHALLOW TRUE)
list(APPEND FETCH_PACKAGES nlohmann_json)
endif()

if(BUILD_DOCUMENTATION)
set(CMAKE_POLICY_DEFAULT_CMP0116
NEW
Expand Down
8 changes: 6 additions & 2 deletions docs/build.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,13 @@ To build the **MQSS Client**, the following components are required.
1. CMake, From Version 3.19 to 3.30.
2. Compiler that supports C++-17 (Every version after Clang 5 and GCC 8).
3. Doxygen 1.15 to build documentation.
4. CURL to communicate with the REST API.
4. CURL 7.62 or newer to communicate with the REST API and validate URLs.
5. RabbitMQ-C to communicate with the RabbitMQ Queues.
6. Niels Lohmann's JSON to parse the JSON data.
6. [Niels Lohmann's JSON](https://github.com/nlohmann/json) to parse the JSON data.
`NLOHMANN_JSON_VERSION` sets the minimum version and fallback release (default:
`3.12.0`). If a suitable installed version is unavailable, CMake fetches the
corresponding Git tag from the repository. Override it at configuration time
with `-DNLOHMANN_JSON_VERSION=<version>`.
7. Pybind11 to create Python bindings.

> [!NOTE] We strongly recommend using Dev Containers with Visual Studio Code. We provided the
Expand Down
33 changes: 33 additions & 0 deletions docs/examples.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,39 @@ This page contains usage examples for the **MQSS Client**.
Before using the **MQSS Client**, an instance needs to be created. The client can operate in two
modes: HPC mode (using RabbitMQ) or remote API mode.

In remote API mode, provide an absolute `http://` or `https://` API base URL,
including its version path (for example, `https://host/v1/`). A trailing slash
is added if missing. Credentials, queries, and fragments in the URL are rejected
with `std::invalid_argument`.

For subdomains of `quantum.lrz.de` and `quantumpathway.eu`, HTTPS URLs with an omitted
port or port `4000`, and an omitted path, `/`, `/v1`, or `/v1/`, are normalized to
`https://<host>:4000/v1/` before connecting. For example,
`https://test.quantumpathway.eu/` becomes `https://test.quantumpathway.eu:4000/v1/`.
The supplied hostname is preserved. Domain suffixes are matched on complete DNS
labels, ignoring case. Custom hosts, explicit alternative
ports, and other paths keep their configured endpoint, with a trailing slash added
if needed.

Construction sends an authenticated `GET <base URL>/resources` request. It throws
`std::runtime_error` if the connection fails, the server returns a non-2xx status
(including authentication errors and redirects), or the check exceeds five seconds.
The connection timeout is three seconds. The C API returns `NULL` on construction
failure.

All REST requests use the same three-second connection timeout and five-second
overall timeout, and accept only HTTP 2xx responses. Failed GET and POST requests
return an empty response, which the client maps to its existing empty, optional,
or error-count return values. Failed cancellation requests throw
`std::runtime_error` in C++ (`RuntimeError` in Python). The void C cancellation
function contains these errors but cannot return a failure status.

When supplying a custom `MQSSBaseClient`, `MQSSClient` defaults to REST job
serialization. Pass `true` as the second constructor argument for HPC serialization.
Blocking job-result polling sleeps for up to two seconds between status checks,
limited by the remaining timeout. The timeout counts time spent sleeping;
network requests add to the total duration and retain their separate REST limits.

```cpp
#include <mqss/client.h>

Expand Down
2 changes: 2 additions & 0 deletions include/mqss-c/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ extern "C" {

typedef struct MQSSOpaqueClient* MQSSClientRef;

// Returns NULL if arguments are invalid or the connection check fails.
MQSSClientRef MQSSClientCreateClient(const char* token, const char* urlOrQueue,
bool isHpc);

Expand All @@ -39,6 +40,7 @@ MQSSResourceRef MQSSClientGetResourceInfo(MQSSClientRef client,

int MQSSClientSubmitJob(MQSSClientRef client, MQSSJobRef job);

// Transport failures are contained; this void API cannot report their status.
void MQSSClientCancelJob(MQSSClientRef client, MQSSJobRef job);

MQSSJobResultRef MQSSClientGetJobResult(MQSSClientRef client, MQSSJobRef job,
Expand Down
11 changes: 8 additions & 3 deletions include/mqss/client.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,17 +47,22 @@ class MQSSClient {

private:
std::unique_ptr<JobResult> waitForJobResult(const JobRequest& job,
size_t poll_seconds);
int timeout, int pollSeconds = 2);
bool mIsHpc;
std::unique_ptr<MQSSBaseClient> mClient;

public:
// REST mode normalizes known portal URLs, validates the base URL, and checks
// GET <base>/resources.
// Throws std::invalid_argument for invalid URLs or std::runtime_error if
// the server cannot return HTTP 2xx within five seconds.
MQSSClient(const std::string& token = "",
const std::string& url_or_queue = MQP_DEFAULT_URL,
bool is_hpc = false);

explicit MQSSClient(std::unique_ptr<MQSSBaseClient> client)
: mClient(std::move(client)) {}
explicit MQSSClient(std::unique_ptr<MQSSBaseClient> client,
bool is_hpc = false)
: mIsHpc(is_hpc), mClient(std::move(client)) {}

// Resources
std::vector<Resource> getAllResources() const;
Expand Down
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,8 @@ sdist.exclude = [
"**/*.pyc",
]
[tool.scikit-build.cmake.define]
BUILD_TESTS = "OFF"
BUILD_BINDINGS = "ON"
MQSSCLIENT_BUILD_TESTS = "OFF"
MQSSCLIENT_BUILD_PYTHON_BINDINGS = "ON"
[tool.scikit-build.metadata.version]
provider = "scikit_build_core.metadata.setuptools_scm"
[tool.setuptools_scm]
Expand Down
8 changes: 4 additions & 4 deletions src/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,13 @@
#
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

find_package(CURL REQUIRED)
find_package(nlohmann_json REQUIRED)
# URL validation and normalization use the curl_url API
find_package(CURL 7.62 REQUIRED)
find_package(rabbitmq-c REQUIRED)

file(GLOB_RECURSE MQSS_CLIENT_SOURCES **.cpp)

if(BUILD_SHARED_LIBS)
if(MQSSCLIENT_BUILD_SHARED_LIBS)
add_library(mqss_client SHARED ${MQSS_CLIENT_SOURCES})
else()
add_library(mqss_client STATIC ${MQSS_CLIENT_SOURCES})
Expand All @@ -32,7 +32,7 @@ target_include_directories(
mqss_client PUBLIC $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/../include>
$<INSTALL_INTERFACE:include>)

target_link_libraries(mqss_client PUBLIC curl nlohmann_json rabbitmq)
target_link_libraries(mqss_client PUBLIC CURL::libcurl nlohmann_json::nlohmann_json rabbitmq)

include(GNUInstallDirs)

Expand Down
12 changes: 10 additions & 2 deletions src/c_bindings/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ MQSSClientRef MQSSClientCreateClient(const char* token, const char* urlOrQueue,
if (!token || !urlOrQueue)
return nullptr;

return wrap<MQSSClientRef>(new MQSSClient(token, urlOrQueue, isHpc));
try {
return wrap<MQSSClientRef>(new MQSSClient(token, urlOrQueue, isHpc));
} catch (const std::exception&) {
return nullptr;
}
}
MQSSResourceRef* MQSSClientGetAllResources(MQSSClientRef client, int* size) {
if (!client || !size)
Expand Down Expand Up @@ -80,7 +84,11 @@ int MQSSClientSubmitJob(MQSSClientRef client, MQSSJobRef job) {
void MQSSClientCancelJob(MQSSClientRef client, MQSSJobRef job) {
if (!client || !job)
return;
unwrap<MQSSClient>(client)->cancelJob(*unwrap<CircuitJobRequest>(job));
try {
unwrap<MQSSClient>(client)->cancelJob(*unwrap<CircuitJobRequest>(job));
} catch (const std::exception&) {
// This void C API cannot report transport failures to the caller.
}
}

MQSSJobResultRef MQSSClientGetJobResult(MQSSClientRef client, MQSSJobRef job,
Expand Down
16 changes: 9 additions & 7 deletions src/client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
#include "clients/rest_client.h"
#include "mqss-c/client.h"

#include <algorithm>
#include <chrono>
#include <optional>
#include <thread>
Expand Down Expand Up @@ -121,19 +122,20 @@ std::unique_ptr<JobResult> MQSSClient::getJobResult(const JobRequest& job,
}

std::unique_ptr<JobResult> MQSSClient::waitForJobResult(const JobRequest& job,
size_t timeout) {
size_t poll_seconds = 2;

int timeout,
int pollSeconds) {
while (timeout > 0) {
std::string status = getJobStatus(job);
if (status == "COMPLETED")
break;
return getJobResult(job);
if (status == "FAILED" || status == "CANCELLED" || status.empty())
return nullptr;
std::this_thread::sleep_for(std::chrono::seconds(poll_seconds));
timeout -= poll_seconds;

const int sleepSeconds = std::min(timeout, pollSeconds);
std::this_thread::sleep_for(std::chrono::seconds(sleepSeconds));
timeout -= sleepSeconds;
}
return timeout <= 0 ? nullptr : getJobResult(job);
return nullptr;
}

int MQSSClient::getNumberPendingJobs(const std::string& resource) const {
Expand Down
13 changes: 10 additions & 3 deletions src/clients/hpc_client.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,14 @@ std::string MQSSHPCClient::post(const std::string& path,
}

void MQSSHPCClient::del(const std::string& path) {
std::string request, response;
mRabbitmqClient.send(mOffloadListenerQueueName, request);
response = mRabbitmqClient.receive(mResponseQueueName);
const nlohmann::json request = {
{"authorization", ""},
{"method", "DELETE"},
{"request", path},
{"data", ""},
{"response_queue", mResponseQueueName},
};
if (mRabbitmqClient.send(mOffloadListenerQueueName, request.dump()) != 0)
throw std::runtime_error("Failed to send HPC cancellation request");
mRabbitmqClient.receive(mResponseQueueName);
}
Loading
Loading