载入中...
搜索中...
未找到
Transaction.h
浏览该文件的文档.
1#pragma once
2#include "common/Export.h"
3
4
5#include "common/Module.h"
6#include "common/Result.h"
7#include "common/Snapshot.h"
8#include "common/UuidV7.h"
9
10#include <cstddef>
11#include <cstdint>
12#include <deque>
13#include <memory>
14#include <optional>
15#include <span>
16#include <string>
17#include <string_view>
18#include <vector>
19
20namespace eve::transaction {
21
32public:
39 TransactionContext(std::string transactionId, std::string correlationId = {}, std::string causationId = {})
41 : transactionId_(std::move(transactionId)),
43 correlationId_(std::move(correlationId)),
45 causationId_(std::move(causationId)) {}
46
55 std::string causationId = {})
57 : identity_(transactionId),
59 transactionId_(transactionId.isNil() ? std::string{} : transactionId.format()),
61 correlationId_(std::move(correlationId)),
63 causationId_(std::move(causationId)) {}
64
66 [[nodiscard]] const eve::TransactionId& identity() const noexcept { return identity_; }
68 const std::string& transactionId() const noexcept { return transactionId_; }
70 const std::string& correlationId() const noexcept { return correlationId_; }
72 const std::string& causationId() const noexcept { return causationId_; }
73
74private:
75 eve::TransactionId identity_;
76 std::string transactionId_;
77 std::string correlationId_;
78 std::string causationId_;
79};
80
90
111
130public:
132 virtual ~ITransactionParticipant() = default;
133
138 virtual std::string_view name() const noexcept { return "transaction-participant"; }
139
145 [[nodiscard]] virtual Result<void> prepare(const TransactionContext& context) = 0;
146
152 [[nodiscard]] virtual Result<void> commit(const TransactionContext& context) = 0;
153
159 [[nodiscard]] virtual Result<void> rollback(const TransactionContext& context) = 0;
160
167 [[nodiscard]] virtual Result<void> compensate(const TransactionContext& context) = 0;
168};
169
180public:
194 [[nodiscard]] Result<TransactionReceipt> execute(const TransactionContext& context,
195 std::span<ITransactionParticipant*> participants) const;
196
206 [[nodiscard]] Result<TransactionReceipt> compensate(const TransactionContext& context,
207 std::span<ITransactionParticipant*> participants) const;
208};
209
212
214struct Operation {
215 std::string id;
216 std::string kind;
217 std::string target;
218 std::string payload = "null";
219 bool valid = false;
220 bool checked = false;
221 std::string error;
224};
225
238
246public:
248 const std::string& id() const;
250 [[nodiscard]] const eve::TransactionId& identity() const noexcept;
252 State state() const;
254 const std::string& correlation() const;
256 const std::string& causation() const;
265 [[nodiscard]] eve::Result<eve::OperationId> stage(const std::string& kind, const std::string& target,
266 const std::string& payloadJson = "null",
267 eve::OperationId operationId = {});
269 [[nodiscard]] eve::Result<void> markValid(const std::string& operationId);
271 [[nodiscard]] eve::Result<void> markValid(eve::OperationId operationId);
273 [[nodiscard]] eve::Result<void> markInvalid(const std::string& operationId, const std::string& error);
275 [[nodiscard]] eve::Result<void> markInvalid(eve::OperationId operationId, const std::string& error);
277 [[nodiscard]] eve::Result<void> validate();
279 [[nodiscard]] eve::Result<void> commit();
281 [[nodiscard]] eve::Result<void> rollback(const std::string& reason = {});
283 [[nodiscard]] eve::Result<void> fail(const std::string& error);
285 const std::string& error() const;
287 int operationCount() const;
296 [[nodiscard]] const Operation* operationAt(int index) const;
305 [[nodiscard]] const Operation* findOperation(const std::string& operationId) const;
314 [[nodiscard]] const Operation* findOperation(eve::OperationId operationId) const;
316 int eventCount() const;
325 [[nodiscard]] const Event* eventAt(int index) const;
327 std::string snapshotJson() const;
328
329private:
330 friend class Ledger;
331 Plan(std::string id, std::string correlation, std::string causation, eve::TransactionId identity = {},
332 eve::UuidEntropySource operationEntropy = {}, eve::UuidClock operationClock = {});
333 void emit(const std::string& type, const std::string& operationId = {}, const std::string& detail = {});
334
335 std::string id_;
336 eve::TransactionId identity_;
337 State state_ = State::Open;
338 std::string correlation_;
339 std::string causation_;
340 std::string error_;
341 uint64_t nextOperation_ = 1;
342 uint64_t nextEvent_ = 1;
343 std::deque<Operation> operations_;
344 std::deque<Event> events_;
345 std::optional<eve::UuidV7Generator> operationIdGenerator_;
346};
347
350public:
352 explicit Ledger(eve::PersistentId instanceId = {}, eve::UuidEntropySource transactionEntropy = {},
353 eve::UuidClock transactionClock = {});
354
355 // std::vector<std::unique_ptr<Plan>> makes the implicit copy operations
356 // ill-formed (C2280) as soon as a class-level dllexport instantiates them;
357 // spell the four out so the export surface stays defined. Semantics unchanged:
358 // the ledger was never copyable in practice.
359 Ledger(const Ledger&) = delete;
360 Ledger& operator=(const Ledger&) = delete;
362 Ledger(Ledger&&) = default;
364 Ledger& operator=(Ledger&&) = default;
372 [[nodiscard]] eve::Result<Plan*> create(const std::string& correlation = {}, const std::string& causation = {},
375 [[nodiscard]] Plan* find(const std::string& transactionId);
377 [[nodiscard]] Plan* find(eve::TransactionId transactionId);
379 int count() const;
381 [[nodiscard]] Plan* at(int index);
383 std::string snapshotJson() const;
390 [[nodiscard]] eve::Result<void> restore(std::string_view json);
391
397 [[nodiscard]] eve::Result<eve::SnapshotEnvelope> snapshot(const eve::SnapshotHashProvider& hashProvider) const;
404 [[nodiscard]] eve::Result<void> restoreSnapshot(const eve::SnapshotEnvelope& snapshot,
405 const eve::SnapshotHashProvider& hashProvider);
407 [[nodiscard]] eve::Result<std::string> snapshotEnvelopeJson(const eve::SnapshotHashProvider& hashProvider) const;
409 [[nodiscard]] eve::Result<void> restoreSnapshotJson(std::string_view json,
410 const eve::SnapshotHashProvider& hashProvider);
411
412private:
413 uint64_t nextTransaction_ = 1;
414 std::vector<std::unique_ptr<Plan>> plans_;
415 eve::PersistentId instanceId_;
416 eve::Revision revision_;
418 eve::UuidEntropySource transactionEntropy_;
419 eve::UuidClock transactionClock_;
420 std::optional<eve::UuidV7Generator> transactionIdGenerator_;
421};
422
424EVENGINE_API_FOUNDATION std::string stateName(State state);
425
428public:
431 Transaction() = default;
433 ~Transaction() override = default;
434
435 // std::vector<std::unique_ptr<Ledger>> makes the implicit copy operations
436 // ill-formed (C2280) as soon as a class-level dllexport instantiates them;
437 // spell the four out so the export surface stays defined. Semantics unchanged:
438 // a module instance was never copyable in practice.
439 Transaction(const Transaction&) = delete;
445
455 [[nodiscard("transaction ledger ownership must be retained or explicitly handled")]] static Ledger* newLedger();
456
457private:
458 std::vector<std::unique_ptr<Ledger>> ledgers_;
459};
460
461} // namespace eve::transaction
LogicalId target
int instanceId
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
std::string id
Snapshot snapshot
std::string error
Definition Package.cpp:174
int detail
Move-only, checked operation results for the common layer.
std::string kind
std::string string
Versioned, integrity-checked persistence envelopes.
int count
std::size_t at
uint32_t index
UUIDv7 generation kept separate from lightweight identity values.
const VegetationPresetContext & context
uint64_t identity
EVENGINE_API_FOUNDATION public API.
Definition Module.h:48
Move-only operation result carrying either a value or Status.
Definition Result.h:176
Id128 public API.
Definition Identity.h:111
Coordinates participant lifecycles with deterministic ordering.
Synchronous participant contract for one coordinator transaction.
virtual ~ITransactionParticipant()=default
Releases ITransactionParticipant resources.
virtual Result< void > compensate(const TransactionContext &context)=0
Undo an already committed external effect by compensation.
virtual Result< void > prepare(const TransactionContext &context)=0
Validate and stage local changes for the transaction.
virtual Result< void > rollback(const TransactionContext &context)=0
Discard a prepared but not committed stage.
virtual std::string_view name() const noexcept
Stable diagnostic name for this participant.
virtual Result< void > commit(const TransactionContext &context)=0
Publish this participant's staged changes.
Deterministic owner and identifier allocator for transaction plans.
Ledger(Ledger &&)=default
Ledger.
Ledger & operator=(const Ledger &)=delete
Ledger(const Ledger &)=delete
Ledger & operator=(Ledger &&)=default
Operator =.
Immutable-after-validation operation plan for script-coordinated work.
Immutable context shared by every participant of one transaction.
Definition Transaction.h:31
const std::string & correlationId() const noexcept
Business-chain correlation identifier, possibly empty.
Definition Transaction.h:70
TransactionContext(std::string transactionId, std::string correlationId={}, std::string causationId={})
Construct transaction correlation metadata.
Definition Transaction.h:39
TransactionContext(eve::TransactionId transactionId, std::string correlationId={}, std::string causationId={})
Construct a canonical UUID-backed transaction context.
Definition Transaction.h:54
const std::string & causationId() const noexcept
Directly causing command/event identifier, possibly empty.
Definition Transaction.h:72
const eve::TransactionId & identity() const noexcept
Canonical UUID-backed identity; nil only for the legacy string facade.
Definition Transaction.h:66
const std::string & transactionId() const noexcept
Stable identifier projection retained for compatibility.
Definition Transaction.h:68
Script module factory for generic transaction ledgers.
Transaction & operator=(const Transaction &)=delete
Transaction()=default
Transaction.
~Transaction() override=default
Transaction.
Transaction(const Transaction &)=delete
Transaction(Transaction &&)=default
Transaction.
Transaction & operator=(Transaction &&)=default
Operator =.
std::string stateName(State state)
Returns the stable lowercase name of a transaction state.
State
Lifecycle state of a generic transaction plan.
CoordinatorState
Terminal or in-flight outcome of coordinator-managed participants.
Definition Transaction.h:82
std::function< std::chrono::system_clock::time_point()> UuidClock
Clock callback used by the injectable UUIDv7 generator.
Definition UuidV7.h:29
std::function< bool(std::span< std::uint8_t > bytes)> UuidEntropySource
Entropy callback used by UUIDv7 generation.
Definition UuidV7.h:26
std::function< Result< ContentId >(std::string_view canonicalInput)> SnapshotHashProvider
Injected content-digest implementation used by snapshots.
Definition Snapshot.h:36
Stable outer format shared by persistence and cross-process snapshots.
Definition Snapshot.h:46
Deterministically ordered transaction lifecycle event.
eve::OperationId operationIdentity
Canonical operation identity projection, when available.
eve::TransactionId transactionIdentity
Canonical transaction identity projection, when available.
One inert, script-interpreted operation in a transaction plan.
eve::OperationId identity
Canonical operation identity; nil for legacy string-only plans.
Observable summary returned after an atomic participant commit.
Definition Transaction.h:98
eve::TransactionId identity
Canonical transaction identity; nil for legacy string contexts.