NeoGraph 0.16.0
A C++20 Graph Agent Engine Library — LangGraph for C++
Loading...
Searching...
No Matches
grpc_checkpoint.h
1// neograph::grpc — remote CheckpointStore over gRPC.
2//
3// Two halves, both built on neograph::graph::CheckpointStore:
4//
5// * GrpcCheckpointStore — a CheckpointStore the engine SETS; every
6// save/load round-trips to a remote gRPC CheckpointService instead
7// of a local DB. The agent process carries no DB driver.
8// * CheckpointServiceImpl + run_checkpoint_server — the SERVER side:
9// wraps any real CheckpointStore (InMemory/Sqlite/Postgres) and
10// exposes it over gRPC. Lives in a separate process, owns the DB.
11//
12// Opt-in (NEOGRAPH_BUILD_GRPC=ON, default OFF). Whole header behind
13// NEOGRAPH_HAVE_GRPC so the umbrella include never pulls grpc++.
14//
15// make_checkpoint_service() returns a std::unique_ptr<::grpc::Service>, which
16// the caller destroys: the header therefore includes the gRPC service base
17// (<grpcpp/impl/service_type.h>), and including it alone is enough to build
18// and destroy the service. The generated neograph.grpc.pb.h is not needed.
19
20#pragma once
21
22#ifdef NEOGRAPH_HAVE_GRPC
23
24#include <chrono>
25#include <memory>
26#include <optional>
27#include <string>
28#include <vector>
29
30// ::grpc::Service, complete for the destructor of the returned unique_ptr.
31#if __has_include(<grpcpp/impl/service_type.h>)
32#include <grpcpp/impl/service_type.h>
33#else
34#include <grpcpp/impl/codegen/service_type.h>
35#endif
36
38#include <neograph/grpc/serve_options.h>
39
40namespace neograph::grpc {
41
42// ── Client side ──────────────────────────────────────────────────────
43//
44// Set this on the engine just like SqliteCheckpointStore:
45// auto store = std::make_shared<neograph::grpc::GrpcCheckpointStore>(
46// "checkpoint-host:50071");
47// auto engine = GraphEngine::compile(def, ctx, store);
48//
49// Every checkpoint then save/loads over gRPC. The sync checkpoint
50// operations, managed-budget denial query, and owned journal operations are
51// overridden; async peers inherit the base facade (run_sync over the sync
52// impl), and pending-writes stay the legacy no-op default.
53class NEOGRAPH_API GrpcCheckpointStore : public neograph::graph::CheckpointStore {
54public:
58 explicit GrpcCheckpointStore(const std::string& target);
61 GrpcCheckpointStore(const std::string& target,
62 std::shared_ptr<sp::NativeArchive> native_archive);
63 ~GrpcCheckpointStore() override;
64
67 void set_rpc_timeout(std::chrono::milliseconds timeout);
68
69 void save(const neograph::graph::Checkpoint& cp) override;
70 std::optional<neograph::graph::Checkpoint>
71 load_latest(const std::string& thread_id) override;
72 std::optional<neograph::graph::Checkpoint>
73 load_by_id(const std::string& id) override;
74 std::vector<neograph::graph::Checkpoint>
75 list(const std::string& thread_id, int limit = 100) override;
76 void delete_thread(const std::string& thread_id) override;
77 bool requires_managed_budget(const std::string& thread_id) override;
78 std::shared_ptr<neograph::graph::OwnedManagedBudgetLease> acquire_managed_budget_lease(
79 const neograph::graph::ManagedBudgetLeaseScope& scope,
80 const std::string& expected_checkpoint_id,
81 const std::string& expected_checkpoint_commitment) override;
82 neograph::graph::ManagedBudgetEffectReceipt begin_managed_budget_effect(
83 const std::shared_ptr<neograph::graph::OwnedManagedBudgetLease>& lease,
84 const std::string& effect_id, std::uint64_t exact_claim_amount,
85 const std::string& prepared_request_digest) override;
86 void settle_managed_budget_effect(
87 const std::shared_ptr<neograph::graph::OwnedManagedBudgetLease>& lease,
89 sp::runtime::Result genuine_outcome,
90 const neograph::UsageAccumulator::AuthoritySnapshot& authority) override;
91 void publish_managed_budget_checkpoint(
92 const std::shared_ptr<neograph::graph::OwnedManagedBudgetLease>& lease,
93 const neograph::graph::Checkpoint& checkpoint) override;
94 void release_managed_budget_lease(
95 const std::shared_ptr<neograph::graph::OwnedManagedBudgetLease>& lease) override;
96
97private:
98 struct Impl;
99 std::unique_ptr<Impl> impl_;
100};
101
102// ── Server side ──────────────────────────────────────────────────────
103//
104// Build + run a blocking gRPC CheckpointService backed by `backend`
105// (e.g. a SqliteCheckpointStore) on `address` ("127.0.0.1:50071"), until
106// the process is signalled. Insecure credentials, and on an address other
107// than loopback one warning on stderr — pass TLS or mTLS credentials in the
108// ServeOptions overload below, or front it with your own ServerBuilder.
109NEOGRAPH_API void run_checkpoint_server(
110 const std::string& address,
111 std::shared_ptr<neograph::graph::CheckpointStore> backend);
117NEOGRAPH_API std::unique_ptr<::grpc::Service> make_checkpoint_service(
118 std::shared_ptr<neograph::graph::CheckpointStore> backend,
119 std::shared_ptr<sp::NativeArchive> native_archive = {});
122NEOGRAPH_API void run_checkpoint_server(
123 const std::string& address,
124 std::shared_ptr<neograph::graph::CheckpointStore> backend,
125 std::shared_ptr<sp::NativeArchive> native_archive);
129NEOGRAPH_API void run_checkpoint_server(
130 const std::string& address,
131 std::shared_ptr<neograph::graph::CheckpointStore> backend,
132 std::shared_ptr<sp::NativeArchive> native_archive,
133 const ServeOptions& options);
134
135// ── Serialization (exposed for tests / payload-size measurement) ─────
136//
137// Full Checkpoint ⇄ JSON. Handles the fields a flat field-per-column
138// mapping cannot: next_nodes (vector), interrupt_phase (enum via to_string /
139// parse_checkpoint_phase), barrier_state (nested map), schema_version.
140NEOGRAPH_API std::string checkpoint_to_json(
142NEOGRAPH_API neograph::graph::Checkpoint checkpoint_from_json(
143 const std::string& s);
144
145} // namespace neograph::grpc
146
147#endif // NEOGRAPH_HAVE_GRPC
Checkpoint system for graph execution state persistence and time-travel.
Abstract interface for checkpoint persistence backends.
Definition checkpoint.h:400
Actual write-ahead claim, bound to one original generation and owned actor.
Definition checkpoint.h:96
Serialized snapshot of graph execution state at a single super-step.
Definition checkpoint.h:173