载入中...
搜索中...
未找到
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
9#include <cstddef>
10#include <cstdint>
11#include <deque>
12#include <memory>
13#include <optional>
14#include <span>
15#include <string>
16#include <string_view>
17#include <vector>
18
19namespace eve::transaction {
20
31public:
38 TransactionContext(std::string transactionId, std::string correlationId = {}, std::string causationId = {})
40 : transactionId_(std::move(transactionId)),
42 correlationId_(std::move(correlationId)),
44 causationId_(std::move(causationId)) {}
45
54 std::string causationId = {})
56 : identity_(transactionId),
58 transactionId_(transactionId.isNil() ? std::string{} : transactionId.format()),
60 correlationId_(std::move(correlationId)),
62 causationId_(std::move(causationId)) {}
63
65 [[nodiscard]] const eve::TransactionId& identity() const noexcept { return identity_; }
67 const std::string& transactionId() const noexcept { return transactionId_; }
69 const std::string& correlationId() const noexcept { return correlationId_; }
71 const std::string& causationId() const noexcept { return causationId_; }
72
73private:
74 eve::TransactionId identity_;
75 std::string transactionId_;
76 std::string correlationId_;
77 std::string causationId_;
78};
79
89
110
129public:
131 virtual ~ITransactionParticipant() = default;
132
137 virtual std::string_view name() const noexcept { return "transaction-participant"; }
138
144 [[nodiscard]] virtual Result<void> prepare(const TransactionContext& context) = 0;
145
151 [[nodiscard]] virtual Result<void> commit(const TransactionContext& context) = 0;
152
158 [[nodiscard]] virtual Result<void> rollback(const TransactionContext& context) = 0;
159
166 [[nodiscard]] virtual Result<void> compensate(const TransactionContext& context) = 0;
167};
168
179public:
190 [[nodiscard]] Result<TransactionReceipt> execute(const TransactionContext& context,
191 std::span<ITransactionParticipant*> participants) const;
192
202 [[nodiscard]] Result<TransactionReceipt> compensate(const TransactionContext& context,
203 std::span<ITransactionParticipant*> participants) const;
204};
205
208
210struct Operation {
211 std::string id;
212 std::string kind;
213 std::string target;
214 std::string payload = "null";
215 bool valid = false;
216 bool checked = false;
217 std::string error;
220};
221
234
242public:
244 const std::string& id() const;
246 [[nodiscard]] const eve::TransactionId& identity() const noexcept;
248 State state() const;
250 const std::string& correlation() const;
252 const std::string& causation() const;
261 [[nodiscard]] eve::Result<eve::OperationId> stage(const std::string& kind, const std::string& target,
262 const std::string& payloadJson = "null",
263 eve::OperationId operationId = {});
265 [[nodiscard]] eve::Result<void> markValid(const std::string& operationId);
267 [[nodiscard]] eve::Result<void> markValid(eve::OperationId operationId);
269 [[nodiscard]] eve::Result<void> markInvalid(const std::string& operationId, const std::string& error);
271 [[nodiscard]] eve::Result<void> markInvalid(eve::OperationId operationId, const std::string& error);
273 [[nodiscard]] eve::Result<void> validate();
275 [[nodiscard]] eve::Result<void> commit();
277 [[nodiscard]] eve::Result<void> rollback(const std::string& reason = {});
279 [[nodiscard]] eve::Result<void> fail(const std::string& error);
281 const std::string& error() const;
283 int operationCount() const;
292 [[nodiscard]] const Operation* operationAt(int index) const;
301 [[nodiscard]] const Operation* findOperation(const std::string& operationId) const;
310 [[nodiscard]] const Operation* findOperation(eve::OperationId operationId) const;
312 int eventCount() const;
321 [[nodiscard]] const Event* eventAt(int index) const;
323 std::string snapshotJson() const;
324
325private:
326 friend class Ledger;
327 Plan(std::string id, std::string correlation, std::string causation, eve::TransactionId identity = {},
328 eve::UuidEntropySource operationEntropy = {}, eve::UuidClock operationClock = {});
329 void emit(const std::string& type, const std::string& operationId = {}, const std::string& detail = {});
330
331 std::string id_;
332 eve::TransactionId identity_;
333 State state_ = State::Open;
334 std::string correlation_;
335 std::string causation_;
336 std::string error_;
337 uint64_t nextOperation_ = 1;
338 uint64_t nextEvent_ = 1;
339 std::deque<Operation> operations_;
340 std::deque<Event> events_;
341 std::optional<eve::UuidV7Generator> operationIdGenerator_;
342};
343
346public:
348 explicit Ledger(eve::PersistentId instanceId = {}, eve::UuidEntropySource transactionEntropy = {},
349 eve::UuidClock transactionClock = {});
350
351 // std::vector<std::unique_ptr<Plan>> makes the implicit copy operations
352 // ill-formed (C2280) as soon as a class-level dllexport instantiates them;
353 // spell the four out so the export surface stays defined. Semantics unchanged:
354 // the ledger was never copyable in practice.
355 Ledger(const Ledger&) = delete;
356 Ledger& operator=(const Ledger&) = delete;
358 Ledger(Ledger&&) = default;
360 Ledger& operator=(Ledger&&) = default;
368 [[nodiscard]] eve::Result<Plan*> create(const std::string& correlation = {}, const std::string& causation = {},
369 eve::TransactionId identity = {});
371 [[nodiscard]] Plan* find(const std::string& transactionId);
373 [[nodiscard]] Plan* find(eve::TransactionId transactionId);
375 int count() const;
377 [[nodiscard]] Plan* at(int index);
379 std::string snapshotJson() const;
386 [[nodiscard]] eve::Result<void> restore(std::string_view json);
387
393 [[nodiscard]] eve::Result<eve::SnapshotEnvelope> snapshot(const eve::SnapshotHashProvider& hashProvider) const;
400 [[nodiscard]] eve::Result<void> restoreSnapshot(const eve::SnapshotEnvelope& snapshot,
401 const eve::SnapshotHashProvider& hashProvider);
403 [[nodiscard]] eve::Result<std::string> snapshotEnvelopeJson(const eve::SnapshotHashProvider& hashProvider) const;
405 [[nodiscard]] eve::Result<void> restoreSnapshotJson(std::string_view json,
406 const eve::SnapshotHashProvider& hashProvider);
407
408private:
409 uint64_t nextTransaction_ = 1;
410 std::vector<std::unique_ptr<Plan>> plans_;
411 eve::PersistentId instanceId_;
412 eve::Revision revision_;
414 eve::UuidEntropySource transactionEntropy_;
415 eve::UuidClock transactionClock_;
416 std::optional<eve::UuidV7Generator> transactionIdGenerator_;
417};
418
420EVENGINE_API_FOUNDATION std::string stateName(State state);
421
424public:
427 Transaction() = default;
429 ~Transaction() override = default;
430
431 // std::vector<std::unique_ptr<Ledger>> makes the implicit copy operations
432 // ill-formed (C2280) as soon as a class-level dllexport instantiates them;
433 // spell the four out so the export surface stays defined. Semantics unchanged:
434 // a module instance was never copyable in practice.
435 Transaction(const Transaction&) = delete;
441
451 [[nodiscard("transaction ledger ownership must be retained or explicitly handled")]] static Ledger* newLedger();
452
453private:
454 std::vector<std::unique_ptr<Ledger>> ledgers_;
455};
456
457} // namespace eve::transaction
LogicalId target
#define EVENGINE_API_FOUNDATION
每个链接组(link group)各自的导出宏。
Definition Export.h:106
TokenKind kind
std::string error
Definition Package.cpp:60
std::string id
Definition PlayHost.cpp:108
int detail
Move-only, checked operation results for the common layer.
std::string string
Versioned, integrity-checked persistence envelopes.
std::uint32_t count
uint32_t index
std::size_t at
const VegetationPresetContext & context
EVENGINE_API_FOUNDATION public API.
Definition Module.h:46
Move-only operation result carrying either a value or Status.
Definition Result.h:155
Id128 public API.
Definition Identity.h:112
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:30
const std::string & correlationId() const noexcept
Business-chain correlation identifier, possibly empty.
Definition Transaction.h:69
TransactionContext(std::string transactionId, std::string correlationId={}, std::string causationId={})
Construct transaction correlation metadata.
Definition Transaction.h:38
TransactionContext(eve::TransactionId transactionId, std::string correlationId={}, std::string causationId={})
Construct a canonical UUID-backed transaction context.
Definition Transaction.h:53
const std::string & causationId() const noexcept
Directly causing command/event identifier, possibly empty.
Definition Transaction.h:71
const eve::TransactionId & identity() const noexcept
Canonical UUID-backed identity; nil only for the legacy string facade.
Definition Transaction.h:65
const std::string & transactionId() const noexcept
Stable identifier projection retained for compatibility.
Definition Transaction.h:67
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:81
std::function< std::chrono::system_clock::time_point()> UuidClock
Clock callback used by the injectable UUIDv7 generator.
Definition Identity.h:469
std::function< bool(std::span< std::uint8_t > bytes)> UuidEntropySource
Entropy callback used by UUIDv7 generation.
Definition Identity.h:466
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:97
eve::TransactionId identity
Canonical transaction identity; nil for legacy string contexts.
Definition Transaction.h:99