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
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
"rust-analyzer.cargo.features": [
"stub_supervisor_api_client"
],
"rust-analyzer.cargo.noDefaultFeatures": true,
"rust-analyzer.check.command": "clippy",
"rust-analyzer.rustfmt.overrideCommand": [
"${workspaceFolder}/.vscode/rustfmt.sh"
Expand Down
2 changes: 2 additions & 0 deletions src/health_monitoring_lib/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ CC_SOURCES = [
"cpp/common.cpp",
"cpp/deadline_monitor.cpp",
"cpp/heartbeat_monitor.cpp",
"cpp/logic_monitor.cpp",
"cpp/health_monitor.cpp",
]

Expand All @@ -37,6 +38,7 @@ CC_HDRS = [
"cpp/include/score/hm/tag.h",
"cpp/include/score/hm/deadline/deadline_monitor.h",
"cpp/include/score/hm/heartbeat/heartbeat_monitor.h",
"cpp/include/score/hm/logic/logic_monitor.h",
"cpp/include/score/hm/health_monitor.h",
]

Expand Down
33 changes: 33 additions & 0 deletions src/health_monitoring_lib/cpp/health_monitor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ using namespace score::hm;
using namespace score::hm::internal;
using namespace score::hm::deadline;
using namespace score::hm::heartbeat;
using namespace score::hm::logic;

// Functions below must match functions defined in `crate::ffi`.

Expand All @@ -34,12 +35,18 @@ FFICode health_monitor_builder_add_deadline_monitor(FFIHandle health_monitor_bui
FFICode health_monitor_builder_add_heartbeat_monitor(FFIHandle health_monitor_builder_handle,
const MonitorTag* monitor_tag,
FFIHandle heartbeat_monitor_builder_handle);
FFICode health_monitor_builder_add_logic_monitor(FFIHandle health_monitor_builder_handle,
const MonitorTag* monitor_tag,
FFIHandle logic_monitor_builder_handle);
FFICode health_monitor_get_deadline_monitor(FFIHandle health_monitor_handle,
const MonitorTag* monitor_tag,
FFIHandle* deadline_monitor_handle_out);
FFICode health_monitor_get_heartbeat_monitor(FFIHandle health_monitor_handle,
const MonitorTag* monitor_tag,
FFIHandle* heartbeat_monitor_handle_out);
FFICode health_monitor_get_logic_monitor(FFIHandle health_monitor_handle,
const MonitorTag* monitor_tag,
FFIHandle* logic_monitor_handle_out);
FFICode health_monitor_start(FFIHandle health_monitor_handle);
FFICode health_monitor_destroy(FFIHandle health_monitor_handle);
}
Expand Down Expand Up @@ -92,6 +99,20 @@ HealthMonitorBuilder HealthMonitorBuilder::add_heartbeat_monitor(const MonitorTa
return std::move(*this);
}

HealthMonitorBuilder HealthMonitorBuilder::add_logic_monitor(const MonitorTag& monitor_tag,
LogicMonitorBuilder&& monitor) &&
{
auto monitor_handle = monitor.drop_by_rust();
SCORE_LANGUAGE_FUTURECPP_PRECONDITION(monitor_handle.has_value());
SCORE_LANGUAGE_FUTURECPP_PRECONDITION(health_monitor_builder_handle_.as_rust_handle().has_value());

auto result{health_monitor_builder_add_logic_monitor(
health_monitor_builder_handle_.as_rust_handle().value(), &monitor_tag, monitor_handle.value())};
SCORE_LANGUAGE_FUTURECPP_ASSERT(result == kSuccess);

return std::move(*this);
}

HealthMonitorBuilder HealthMonitorBuilder::with_internal_processing_cycle(std::chrono::milliseconds cycle_duration) &&
{
internal_processing_cycle_duration_ = cycle_duration;
Expand Down Expand Up @@ -155,6 +176,18 @@ score::cpp::expected<HeartbeatMonitor, Error> HealthMonitor::get_heartbeat_monit
return score::cpp::expected<HeartbeatMonitor, Error>(HeartbeatMonitor{handle});
}

score::cpp::expected<LogicMonitor, Error> HealthMonitor::get_logic_monitor(const MonitorTag& monitor_tag)
{
FFIHandle handle{nullptr};
auto result{health_monitor_get_logic_monitor(health_monitor_, &monitor_tag, &handle)};
if (result != kSuccess)
{
return score::cpp::unexpected(static_cast<Error>(result));
}

return score::cpp::expected<LogicMonitor, Error>(LogicMonitor{handle});
}

void HealthMonitor::start()
{
auto result{health_monitor_start(health_monitor_)};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <score/hm/common.h>
#include <score/hm/deadline/deadline_monitor.h>
#include <score/hm/heartbeat/heartbeat_monitor.h>
#include <score/hm/logic/logic_monitor.h>
#include <score/hm/tag.h>

namespace score::hm
Expand Down Expand Up @@ -47,6 +48,9 @@ class HealthMonitorBuilder final
HealthMonitorBuilder add_heartbeat_monitor(const MonitorTag& monitor_tag,
heartbeat::HeartbeatMonitorBuilder&& monitor) &&;

/// Adds a logic monitor for a specific identifier tag.
HealthMonitorBuilder add_logic_monitor(const MonitorTag& monitor_tag, logic::LogicMonitorBuilder&& monitor) &&;

/// Sets the cycle duration for supervisor API notifications.
/// This duration determines how often the health monitor notifies the supervisor that the system is alive.
HealthMonitorBuilder with_supervisor_api_cycle(std::chrono::milliseconds cycle_duration) &&;
Expand Down Expand Up @@ -78,6 +82,7 @@ class HealthMonitor final

score::cpp::expected<deadline::DeadlineMonitor, Error> get_deadline_monitor(const MonitorTag& monitor_tag);
score::cpp::expected<heartbeat::HeartbeatMonitor, Error> get_heartbeat_monitor(const MonitorTag& monitor_tag);
score::cpp::expected<logic::LogicMonitor, Error> get_logic_monitor(const MonitorTag& monitor_tag);

void start();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/********************************************************************************
* Copyright (c) 2026 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
#ifndef SCORE_HM_LOGIC_LOGIC_MONITOR_H
#define SCORE_HM_LOGIC_LOGIC_MONITOR_H

#include "score/hm/common.h"
#include "score/hm/tag.h"
#include <score/expected.hpp>
#include <vector>

namespace score::hm
{
// Forward declaration
class HealthMonitor;
class HealthMonitorBuilder;
} // namespace score::hm

namespace score::hm::logic
{

class LogicMonitorBuilder final : public internal::RustDroppable<LogicMonitorBuilder>
{
public:
/// Create a new `LogicMonitorBuilder`.
///
/// - `initial_state` - starting point.
LogicMonitorBuilder(const StateTag& initial_state);

LogicMonitorBuilder(const LogicMonitorBuilder&) = delete;
LogicMonitorBuilder& operator=(const LogicMonitorBuilder&) = delete;

LogicMonitorBuilder(LogicMonitorBuilder&&) = default;
LogicMonitorBuilder& operator=(LogicMonitorBuilder&&) = delete;

/// Add state along with allowed transitions.
/// If state already exists - it is overwritten.
LogicMonitorBuilder add_state(const StateTag& state, const std::vector<StateTag>& allowed_states) &&;

protected:
std::optional<internal::FFIHandle> _drop_by_rust_impl()
{
return monitor_builder_handle_.drop_by_rust();
}

private:
internal::DroppableFFIHandle monitor_builder_handle_;

// Allow to hide drop_by_rust implementation
friend class internal::RustDroppable<LogicMonitorBuilder>;

// Allow HealthMonitorBuilder to access drop_by_rust implementation
friend class ::score::hm::HealthMonitorBuilder;
};

class LogicMonitor final
{
public:
LogicMonitor(const LogicMonitor&) = delete;
LogicMonitor& operator=(const LogicMonitor&) = delete;

LogicMonitor(LogicMonitor&& other) noexcept = default;
LogicMonitor& operator=(LogicMonitor&& other) noexcept = default;

/// Perform transition to a new state.
/// On success, current state is returned.
score::cpp::expected<StateTag, Error> transition(const StateTag& state);

/// Current monitor state.
score::cpp::expected<StateTag, Error> state();

Comment on lines +65 to +80
private:
explicit LogicMonitor(internal::FFIHandle monitor_handle);

// Only `HealthMonitor` is allowed to create `LogicMonitor` instances.
friend class score::hm::HealthMonitor;
internal::DroppableFFIHandle monitor_handle_;
};

} // namespace score::hm::logic

#endif // SCORE_HM_LOGIC_LOGIC_MONITOR_H
27 changes: 24 additions & 3 deletions src/health_monitoring_lib/cpp/include/score/hm/tag.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,13 @@
#define SCORE_HM_TAG_H

#include <cstddef>
#include <string_view>

namespace score::hm
{

/// Common string-based tag.
template <typename T>
class Tag
{
public:
Expand All @@ -29,21 +31,40 @@ class Tag
{
}

bool operator==(const T& other) const noexcept
{
std::string_view this_sv{data_, length_};
std::string_view other_sv{other.data_, other.length_};
return this_sv.compare(other_sv) == 0;
}

bool operator!=(const T& other) const noexcept
{
return !(*this == other);
}

private:
/// SAFETY: This has to be FFI compatible with the Rust side representation.
const char* const data_;
const char* data_;
size_t length_;
};
Comment on lines 23 to 50

/// Monitor tag.
class MonitorTag : public Tag
class MonitorTag : public Tag<MonitorTag>
{
public:
using Tag::Tag;
};

/// Deadline tag.
class DeadlineTag : public Tag
class DeadlineTag : public Tag<DeadlineTag>
{
public:
using Tag::Tag;
};

/// State tag.
class StateTag : public Tag<StateTag>
{
public:
using Tag::Tag;
Expand Down
94 changes: 94 additions & 0 deletions src/health_monitoring_lib/cpp/logic_monitor.cpp
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Add unit tests

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unit tests to be added once API is mroe complete, ETA 1-2 weeks.
#121

Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/********************************************************************************
* Copyright (c) 2026 Contributors to the Eclipse Foundation
*
* See the NOTICE file(s) distributed with this work for additional
* information regarding copyright ownership.
*
* This program and the accompanying materials are made available under the
* terms of the Apache License Version 2.0 which is available at
* https://www.apache.org/licenses/LICENSE-2.0
*
* SPDX-License-Identifier: Apache-2.0
********************************************************************************/
#include "score/hm/logic/logic_monitor.h"
#include <score/assert.hpp>

namespace
{
extern "C" {
using namespace score::hm;
using namespace score::hm::internal;
using namespace score::hm::logic;

FFICode logic_monitor_builder_create(const StateTag* initial_state, FFIHandle* logic_monitor_builder_handle_out);
FFICode logic_monitor_builder_destroy(FFIHandle logic_monitor_builder_handle);
FFICode logic_monitor_builder_add_state(FFIHandle logic_monitor_builder_handle,
const StateTag* state,
const StateTag* allowed_states,
size_t num_allowed_states);
FFICode logic_monitor_destroy(FFIHandle logic_monitor_handle);
FFICode logic_monitor_transition(FFIHandle logic_monitor_handle, const StateTag* target_state);
FFICode logic_monitor_state(FFIHandle logic_monitor_handle, StateTag* state_out);
}

FFIHandle logic_monitor_builder_create_wrapper(const StateTag& initial_state)
{
FFIHandle handle{nullptr};
auto result{logic_monitor_builder_create(&initial_state, &handle)};
SCORE_LANGUAGE_FUTURECPP_ASSERT(result == kSuccess);
return handle;
}
} // namespace

namespace score::hm::logic
{
LogicMonitorBuilder::LogicMonitorBuilder(const StateTag& initial_state)
: monitor_builder_handle_{logic_monitor_builder_create_wrapper(initial_state), &logic_monitor_builder_destroy}
{
}

LogicMonitorBuilder LogicMonitorBuilder::add_state(const StateTag& state,
const std::vector<StateTag>& allowed_states) &&
{
auto monitor_builder_handle{monitor_builder_handle_.as_rust_handle()};
SCORE_LANGUAGE_FUTURECPP_PRECONDITION(monitor_builder_handle.has_value());

auto result{logic_monitor_builder_add_state(
monitor_builder_handle.value(), &state, allowed_states.data(), allowed_states.size())};
SCORE_LANGUAGE_FUTURECPP_ASSERT(result == kSuccess);

return std::move(*this);
}

LogicMonitor::LogicMonitor(FFIHandle monitor_handle) : monitor_handle_{monitor_handle, &logic_monitor_destroy} {}

score::cpp::expected<StateTag, Error> LogicMonitor::transition(const StateTag& state)
{
auto monitor_handle{monitor_handle_.as_rust_handle()};
SCORE_LANGUAGE_FUTURECPP_PRECONDITION(monitor_handle.has_value());

auto result{logic_monitor_transition(monitor_handle.value(), &state)};
if (result != kSuccess)
{
return score::cpp::unexpected(static_cast<Error>(result));
}

return score::cpp::expected<StateTag, Error>(state);
}

score::cpp::expected<StateTag, Error> LogicMonitor::state()
{
auto monitor_handle{monitor_handle_.as_rust_handle()};
SCORE_LANGUAGE_FUTURECPP_PRECONDITION(monitor_handle.has_value());

StateTag state_tag{""};
auto result{logic_monitor_state(monitor_handle.value(), &state_tag)};
if (result != kSuccess)
{
return score::cpp::unexpected(static_cast<Error>(result));
}

return score::cpp::expected<StateTag, Error>(state_tag);
}

} // namespace score::hm::logic
Loading
Loading