|
| 1 | +// Copyright 2026 Stellar Development Foundation and contributors. Licensed |
| 2 | +// under the Apache License, Version 2.0. See the COPYING file at the root |
| 3 | +// of this distribution or at http://www.apache.org/licenses/LICENSE-2.0 |
| 4 | + |
| 5 | +#pragma once |
| 6 | + |
| 7 | +#include "ledger/LedgerEntryScope.h" |
| 8 | +#include "ledger/LedgerHashUtils.h" |
| 9 | +#include "ledger/LedgerTxn.h" |
| 10 | +#include "util/UnorderedMap.h" |
| 11 | +#include "util/UnorderedSet.h" |
| 12 | +#include "xdr/Stellar-ledger-entries.h" |
| 13 | + |
| 14 | +namespace stellar |
| 15 | +{ |
| 16 | + |
| 17 | +// A LedgerKey that caches its own hash. |
| 18 | +// |
| 19 | +// The parallel apply maps are keyed by LedgerKey and the same key is looked |
| 20 | +// up many times as it flows tx map -> thread map -> global map, so caching |
| 21 | +// the hash avoids repeated re-hashing of the same key. |
| 22 | +class ParallelApplyLedgerKey |
| 23 | +{ |
| 24 | + public: |
| 25 | + explicit ParallelApplyLedgerKey(LedgerKey const& ledgerKey) |
| 26 | + : mLedgerKey(ledgerKey), mHash(std::hash<LedgerKey>{}(mLedgerKey)) |
| 27 | + { |
| 28 | + } |
| 29 | + explicit ParallelApplyLedgerKey(LedgerKey&& ledgerKey) |
| 30 | + : mLedgerKey(std::move(ledgerKey)) |
| 31 | + , mHash(std::hash<LedgerKey>{}(mLedgerKey)) |
| 32 | + { |
| 33 | + } |
| 34 | + |
| 35 | + LedgerKey const& |
| 36 | + ledgerKey() const |
| 37 | + { |
| 38 | + return mLedgerKey; |
| 39 | + } |
| 40 | + |
| 41 | + operator LedgerKey const&() const |
| 42 | + { |
| 43 | + return mLedgerKey; |
| 44 | + } |
| 45 | + |
| 46 | + size_t |
| 47 | + hash() const |
| 48 | + { |
| 49 | + return mHash; |
| 50 | + } |
| 51 | + |
| 52 | + private: |
| 53 | + LedgerKey mLedgerKey; |
| 54 | + size_t mHash; |
| 55 | +}; |
| 56 | + |
| 57 | +inline bool |
| 58 | +operator==(ParallelApplyLedgerKey const& lhs, ParallelApplyLedgerKey const& rhs) |
| 59 | +{ |
| 60 | + return lhs.ledgerKey() == rhs.ledgerKey(); |
| 61 | +} |
| 62 | + |
| 63 | +using ParallelApplyLedgerKeySet = UnorderedSet<ParallelApplyLedgerKey>; |
| 64 | +template <typename T> |
| 65 | +using ParallelApplyLedgerKeyMap = UnorderedMap<ParallelApplyLedgerKey, T>; |
| 66 | + |
| 67 | +// Tracks entry updates within a transaction during parallel apply phases. If |
| 68 | +// the transaction succeeds, the thread's ParallelApplyEntryMap should be |
| 69 | +// updated with the entries from the TxModifiedEntryMap. |
| 70 | +using TxParApplyLedgerEntry = |
| 71 | + ScopedLedgerEntry<StaticLedgerEntryScope::TxParApply>; |
| 72 | +using TxModifiedEntryMap = ParallelApplyLedgerKeyMap<TxParApplyLedgerEntryOpt>; |
| 73 | + |
| 74 | +// Used to track the current state of an entry during parallel apply phases. Can |
| 75 | +// be updated by successful transactions. |
| 76 | +template <StaticLedgerEntryScope S> struct ParallelApplyEntry |
| 77 | +{ |
| 78 | + // Will not be set if the entry doesn't exist, or if no tx was able to load |
| 79 | + // it due to hitting read limits. |
| 80 | + ScopedLedgerEntryOpt<S> mLedgerEntry; |
| 81 | + bool mIsDirty; |
| 82 | + static ParallelApplyEntry |
| 83 | + clean(ScopedLedgerEntryOpt<S> const& e) |
| 84 | + { |
| 85 | + return ParallelApplyEntry{e, false}; |
| 86 | + } |
| 87 | + static ParallelApplyEntry |
| 88 | + dirty(ScopedLedgerEntryOpt<S> const& e) |
| 89 | + { |
| 90 | + return ParallelApplyEntry{e, true}; |
| 91 | + } |
| 92 | + template <StaticLedgerEntryScope S2> |
| 93 | + ParallelApplyEntry<S2> |
| 94 | + rescope(LedgerEntryScope<S> const& s1, |
| 95 | + LedgerEntryScope<S2> const& s2) const& |
| 96 | + { |
| 97 | + auto adoptedEntry = s2.scopeAdoptEntryOptFrom(mLedgerEntry, s1); |
| 98 | + return ParallelApplyEntry<S2>{adoptedEntry, mIsDirty}; |
| 99 | + } |
| 100 | + // Moves the entry payload into the new scope and thus makes the current |
| 101 | + // entry invalid. |
| 102 | + template <StaticLedgerEntryScope S2> |
| 103 | + ParallelApplyEntry<S2> |
| 104 | + rescope(LedgerEntryScope<S> const& s1, LedgerEntryScope<S2> const& s2) && |
| 105 | + { |
| 106 | + auto adoptedEntry = |
| 107 | + s2.scopeAdoptEntryOptFrom(std::move(mLedgerEntry), s1); |
| 108 | + return ParallelApplyEntry<S2>{std::move(adoptedEntry), mIsDirty}; |
| 109 | + } |
| 110 | +}; |
| 111 | +using GlobalParallelApplyEntry = |
| 112 | + ParallelApplyEntry<StaticLedgerEntryScope::GlobalParApply>; |
| 113 | +using ThreadParallelApplyEntry = |
| 114 | + ParallelApplyEntry<StaticLedgerEntryScope::ThreadParApply>; |
| 115 | +using TxParallelApplyEntry = |
| 116 | + ParallelApplyEntry<StaticLedgerEntryScope::TxParApply>; |
| 117 | + |
| 118 | +// This is a map of all entries that will be read and/or written during parallel |
| 119 | +// apply phases: there is one such "global" map which disjoint per-thread maps |
| 120 | +// get split off of, modified during applyThread, and merged back into. Once all |
| 121 | +// threads return, the updates from each threads entry map should be committed |
| 122 | +// to LedgerTxn. |
| 123 | +template <StaticLedgerEntryScope S> |
| 124 | +using ParallelApplyEntryMap = ParallelApplyLedgerKeyMap<ParallelApplyEntry<S>>; |
| 125 | +using GlobalParallelApplyEntryMap = |
| 126 | + ParallelApplyEntryMap<StaticLedgerEntryScope::GlobalParApply>; |
| 127 | +using ThreadParallelApplyEntryMap = |
| 128 | + ParallelApplyEntryMap<StaticLedgerEntryScope::ThreadParApply>; |
| 129 | +using TxParallelApplyEntryMap = |
| 130 | + ParallelApplyEntryMap<StaticLedgerEntryScope::TxParApply>; |
| 131 | + |
| 132 | +// Returned by each parallel transaction on success. It will contain the entries |
| 133 | +// modified by the transaction and the keys restored. |
| 134 | +class ParallelTxSuccessVal |
| 135 | + : public LedgerEntryScope<StaticLedgerEntryScope::TxParApply> |
| 136 | +{ |
| 137 | + public: |
| 138 | + ParallelTxSuccessVal(TxModifiedEntryMap&& modifiedEntryMap, |
| 139 | + ScopeIdT txScopeID) |
| 140 | + : LedgerEntryScope(txScopeID) |
| 141 | + , mModifiedEntryMap(std::move(modifiedEntryMap)) |
| 142 | + { |
| 143 | + // The ModifiedEntryMap should not be used for reading entries, only |
| 144 | + // to serve as a source for thread state to scopeAdoptEntryFrom. So |
| 145 | + // we deactivate ourselves as a LedgerEntryScope on construction, to |
| 146 | + // prevent accidental reads. |
| 147 | + scopeDeactivate(); |
| 148 | + } |
| 149 | + ParallelTxSuccessVal(TxModifiedEntryMap&& modifiedEntryMap, |
| 150 | + RestoredEntries&& restoredEntries, ScopeIdT txScopeID) |
| 151 | + : LedgerEntryScope(txScopeID) |
| 152 | + , mModifiedEntryMap(std::move(modifiedEntryMap)) |
| 153 | + , mRestoredEntries(std::move(restoredEntries)) |
| 154 | + { |
| 155 | + scopeDeactivate(); |
| 156 | + } |
| 157 | + |
| 158 | + TxModifiedEntryMap const& |
| 159 | + getModifiedEntryMap() const |
| 160 | + { |
| 161 | + return mModifiedEntryMap; |
| 162 | + } |
| 163 | + RestoredEntries const& |
| 164 | + getRestoredEntries() const |
| 165 | + { |
| 166 | + return mRestoredEntries; |
| 167 | + } |
| 168 | + |
| 169 | + friend class TxParallelApplyLedgerState; |
| 170 | + |
| 171 | + private: |
| 172 | + // This will contain a key for every entry modified by a transaction |
| 173 | + TxModifiedEntryMap mModifiedEntryMap; |
| 174 | + RestoredEntries mRestoredEntries; |
| 175 | +}; |
| 176 | +} // namespace stellar |
| 177 | + |
| 178 | +namespace std |
| 179 | +{ |
| 180 | +template <> class hash<stellar::ParallelApplyLedgerKey> |
| 181 | +{ |
| 182 | + public: |
| 183 | + size_t |
| 184 | + operator()(stellar::ParallelApplyLedgerKey const& key) const |
| 185 | + { |
| 186 | + return key.hash(); |
| 187 | + } |
| 188 | +}; |
| 189 | +} // namespace std |
0 commit comments