PACS System 0.1.0
PACS DICOM system library
Loading...
Searching...
No Matches
hsm_migration_service.h
Go to the documentation of this file.
1// BSD 3-Clause License
2// Copyright (c) 2021-2025, 🍀☀🌕🌥 🌊
3// See the LICENSE file in the project root for full license information.
4
18#pragma once
19
20#include "hsm_storage.h"
21#include "hsm_types.h"
22
23#include <atomic>
24#include <chrono>
25#include <condition_variable>
26#include <functional>
27#include <memory>
28#include <mutex>
29#include <optional>
30#include <string>
31#include <thread>
32#include <vector>
33
34namespace kcenon::thread {
35class thread_pool;
36} // namespace kcenon::thread
37
38namespace kcenon::logger {
39class logger;
40} // namespace kcenon::logger
41
42namespace kcenon::pacs::storage {
43
49 std::chrono::seconds migration_interval{3600}; // 1 hour default
50
53
55 bool auto_start{false};
56
59 std::function<void(const migration_result& result)>;
61
64 std::function<void(const std::string& uid, const std::string& error)>;
66};
67
100public:
101 // =========================================================================
102 // Construction
103 // =========================================================================
104
111 explicit hsm_migration_service(hsm_storage& storage,
112 const migration_service_config& config = {});
113
122 hsm_storage& storage,
123 std::shared_ptr<kcenon::thread::thread_pool> thread_pool,
124 const migration_service_config& config = {});
125
130
134
138
139 // =========================================================================
140 // Lifecycle Management
141 // =========================================================================
142
149 void start();
150
159 void stop(bool wait_for_completion = true);
160
166 [[nodiscard]] auto is_running() const noexcept -> bool;
167
168 // =========================================================================
169 // Manual Operations
170 // =========================================================================
171
180 [[nodiscard]] auto run_migration_cycle() -> migration_result;
181
189 void trigger_cycle();
190
191 // =========================================================================
192 // Statistics and Monitoring
193 // =========================================================================
194
200 [[nodiscard]] auto get_last_result() const
201 -> std::optional<migration_result>;
202
208 [[nodiscard]] auto get_cumulative_stats() const -> migration_result;
209
215 [[nodiscard]] auto time_until_next_cycle() const
216 -> std::optional<std::chrono::seconds>;
217
223 [[nodiscard]] auto cycles_completed() const noexcept -> std::size_t;
224
225 // =========================================================================
226 // Configuration
227 // =========================================================================
228
236 void set_migration_interval(std::chrono::seconds interval);
237
243 [[nodiscard]] auto get_migration_interval() const noexcept
244 -> std::chrono::seconds;
245
252
258 void set_error_callback(migration_service_config::error_callback callback);
259
260private:
261 // =========================================================================
262 // Internal Methods
263 // =========================================================================
264
268 void run_loop();
269
274 [[nodiscard]] auto execute_cycle() -> migration_result;
275
280 void update_stats(const migration_result& result);
281
282 // =========================================================================
283 // Member Variables
284 // =========================================================================
285
288
290 std::shared_ptr<kcenon::thread::thread_pool> thread_pool_;
291
294
296 std::thread worker_thread_;
297
299 mutable std::mutex mutex_;
300
302 std::condition_variable cv_;
303
305 std::atomic<bool> stop_requested_{false};
306
308 std::atomic<bool> running_{false};
309
311 std::atomic<bool> cycle_in_progress_{false};
312
314 std::optional<migration_result> last_result_;
315
318
320 std::atomic<std::size_t> cycles_count_{0};
321
323 std::chrono::steady_clock::time_point next_cycle_time_;
324};
325
326} // namespace kcenon::pacs::storage
std::atomic< bool > cycle_in_progress_
Flag indicating a cycle is in progress.
~hsm_migration_service()
Destructor - ensures graceful shutdown.
void update_stats(const migration_result &result)
Update cumulative statistics.
void set_progress_callback(migration_service_config::progress_callback callback)
Set the progress callback.
void stop(bool wait_for_completion=true)
Stop the background migration service.
auto get_last_result() const -> std::optional< migration_result >
Get the result of the last migration cycle.
hsm_migration_service & operator=(hsm_migration_service &&)=delete
void set_migration_interval(std::chrono::seconds interval)
Update the migration interval.
std::atomic< bool > stop_requested_
Flag to signal shutdown.
hsm_storage & storage_
Reference to managed HSM storage.
std::mutex mutex_
Mutex for thread synchronization.
hsm_migration_service(hsm_storage &storage, const migration_service_config &config={})
Construct migration service.
std::chrono::steady_clock::time_point next_cycle_time_
Time of next scheduled cycle.
std::thread worker_thread_
Background worker thread.
auto cycles_completed() const noexcept -> std::size_t
Get the number of cycles completed.
hsm_migration_service(hsm_migration_service &&)=delete
Non-movable.
auto run_migration_cycle() -> migration_result
Manually trigger a migration cycle.
std::atomic< bool > running_
Flag indicating service is running.
auto is_running() const noexcept -> bool
Check if the service is running.
void trigger_cycle()
Trigger next cycle immediately.
void set_error_callback(migration_service_config::error_callback callback)
Set the error callback.
std::atomic< std::size_t > cycles_count_
Number of completed cycles.
auto execute_cycle() -> migration_result
Execute a single migration cycle.
hsm_migration_service & operator=(const hsm_migration_service &)=delete
auto get_cumulative_stats() const -> migration_result
Get total statistics since service started.
std::optional< migration_result > last_result_
Last migration result.
auto time_until_next_cycle() const -> std::optional< std::chrono::seconds >
Get the time until the next scheduled migration.
auto get_migration_interval() const noexcept -> std::chrono::seconds
Get the current migration interval.
migration_result cumulative_stats_
Cumulative statistics.
migration_service_config config_
Service configuration.
hsm_migration_service(const hsm_migration_service &)=delete
Non-copyable.
void start()
Start the background migration service.
std::shared_ptr< kcenon::thread::thread_pool > thread_pool_
Thread pool for parallel migrations (optional)
std::condition_variable cv_
Condition variable for sleep/wake.
Hierarchical Storage Management (HSM) for multi-tier DICOM storage.
Types for Hierarchical Storage Management (HSM)
std::shared_mutex mutex
Mutex for thread-safe access.
std::function< bool(std::size_t bytes_transferred, std::size_t total_bytes)> progress_callback
Callback type for upload/download progress tracking.
Definition s3_storage.h:115
Result of a migration operation.
Definition hsm_types.h:230
Configuration for the migration service.
bool auto_start
Whether to start automatically on construction.
std::size_t max_concurrent_migrations
Maximum concurrent migrations.
std::function< void(const migration_result &result)> progress_callback
Callback for migration progress updates.
std::function< void(const std::string &uid, const std::string &error)> error_callback
Callback for migration errors.
std::chrono::seconds migration_interval
Interval between migration cycles.
std::string_view uid