1818 * Handles the Coordinator-side (state-table) operations of the Consensus Commit protocol: writing
1919 * the COMMITTED / ABORTED records to the Coordinator state table and resolving putState conflicts.
2020 *
21- * <p>Methods here only touch the Coordinator state table via {@link Coordinator}; they never touch
22- * user data tables. When {@code commitState} loses a putState race and the persisted state turns
23- * out to be ABORTED (or absent), this handler reports a {@link CommitConflictException} and leaves
24- * the rollback of the transaction's prepared records to the caller. This keeps the handler free of
25- * any participant-side dependency.
21+ * <p>Operates on a transaction id and a pre-encoded {@link WriteSet} (plus, for the commit path,
22+ * the {@code committedAt} to stamp); callers encode the write set before calling.
23+ *
24+ * <p>When {@code commitState} loses a putState race and the persisted state turns out to be ABORTED
25+ * (or absent), this handler reports a {@link CommitConflictException} and leaves the rollback of
26+ * the transaction's prepared records to the caller.
2627 */
2728@ ThreadSafe
2829class CoordinatorCommitHandler {
2930 private static final Logger logger = LoggerFactory .getLogger (CoordinatorCommitHandler .class );
3031
3132 private final Coordinator coordinator ;
32- private final WriteSetEncoder writeSetEncoder ;
33- private final boolean coordinatorWriteSetLoggingEnabled ;
3433
3534 @ SuppressFBWarnings ("EI_EXPOSE_REP2" )
36- CoordinatorCommitHandler (
37- Coordinator coordinator ,
38- WriteSetEncoder writeSetEncoder ,
39- boolean coordinatorWriteSetLoggingEnabled ) {
35+ CoordinatorCommitHandler (Coordinator coordinator ) {
4036 this .coordinator = checkNotNull (coordinator );
41- this .writeSetEncoder = checkNotNull (writeSetEncoder );
42- this .coordinatorWriteSetLoggingEnabled = coordinatorWriteSetLoggingEnabled ;
43- }
44-
45- /**
46- * Writes the COMMITTED state. When coordinator write-set logging is enabled, the {@code
47- * tx_write_set} column is populated from the given context's snapshot; otherwise the column is
48- * omitted.
49- *
50- * @return the {@code committedAt} timestamp written to the COMMITTED Coordinator state row. The
51- * caller stamps the committed data records with the same value so the row and the records
52- * share one timestamp.
53- */
54- long commitState (TransactionContext context )
55- throws CommitConflictException , UnknownTransactionStatusException {
56- // The tx_write_set column is added only when the opt-in `coordinator.write_set_logging.enabled`
57- // config is enabled. When it's disabled the column is not part of the Coordinator schema, so we
58- // must skip encoding/persisting the WriteSet entirely.
59- WriteSet writeSet =
60- coordinatorWriteSetLoggingEnabled
61- ? writeSetEncoder .encodeSingleGroupWriteSet (context , false )
62- : null ;
63- return commitStateInternal (context , writeSet );
6437 }
6538
6639 /**
67- * Writes the COMMITTED state without persisting a {@code tx_write_set}.
40+ * Writes the COMMITTED state for the transaction identified by {@code id}, persisting the given
41+ * pre-encoded {@code writeSet} (or none when {@code null}).
6842 *
69- * @return the {@code committedAt} timestamp written to the COMMITTED Coordinator state row.
43+ * @return the {@code committedAt} stamped on the COMMITTED Coordinator state row — the value this
44+ * call generates, or, when this commit lost a putState race to an already-COMMITTED row, that
45+ * row's committedAt. The caller stamps the committed data records with the returned value so
46+ * the row and the records share one timestamp.
7047 */
71- long commitStateWithoutWriteSet ( TransactionContext context )
48+ long commitState ( String id , @ Nullable WriteSet writeSet )
7249 throws CommitConflictException , UnknownTransactionStatusException {
73- return commitStateInternal (context , null );
74- }
75-
76- private long commitStateInternal (TransactionContext context , @ Nullable WriteSet writeSet )
77- throws CommitConflictException , UnknownTransactionStatusException {
78- String id = context .transactionId ;
7950 try {
8051 long committedAt = System .currentTimeMillis ();
8152 Coordinator .State state =
@@ -84,7 +55,7 @@ private long commitStateInternal(TransactionContext context, @Nullable WriteSet
8455 logger .debug ("Transaction {} is committed successfully at {}" , id , committedAt );
8556 return committedAt ;
8657 } catch (CoordinatorConflictException e ) {
87- return handleCommitConflict (context , e );
58+ return handleCommitConflict (id , e );
8859 } catch (CoordinatorException e ) {
8960 throw new UnknownTransactionStatusException (
9061 CoreError .CONSENSUS_COMMIT_UNKNOWN_COORDINATOR_STATUS .buildMessage (e .getMessage ()),
@@ -95,21 +66,21 @@ private long commitStateInternal(TransactionContext context, @Nullable WriteSet
9566
9667 /**
9768 * Resolves a putState conflict. Returns the {@code committedAt} of the already-persisted
98- * COMMITTED state when this transaction turns out to be already committed; otherwise rolls back
99- * the records and throws .
69+ * COMMITTED state when this transaction turns out to be already committed; otherwise reports the
70+ * conflict ( the caller rolls the records back) by throwing {@link CommitConflictException} .
10071 */
101- long handleCommitConflict (TransactionContext context , Exception cause )
72+ long handleCommitConflict (String id , Exception cause )
10273 throws CommitConflictException , UnknownTransactionStatusException {
10374 try {
104- Optional <Coordinator .State > s = coordinator .getState (context . transactionId );
75+ Optional <Coordinator .State > s = coordinator .getState (id );
10576 if (s .isPresent ()) {
10677 Coordinator .State persisted = s .get ();
10778 if (persisted .getState () == TransactionState .ABORTED ) {
10879 throw new CommitConflictException (
10980 CoreError .CONSENSUS_COMMIT_CONFLICT_OCCURRED_WHEN_COMMITTING_STATE .buildMessage (
11081 cause .getMessage ()),
11182 cause ,
112- context . transactionId );
83+ id );
11384 }
11485 // Otherwise the coordinator state is present and COMMITTED, which means this transaction
11586 // has already committed. Only Two-phase Commit I/F reaches this branch: there the same
@@ -163,37 +134,21 @@ long handleCommitConflict(TransactionContext context, Exception cause)
163134 CoreError .CONSENSUS_COMMIT_CONFLICT_OCCURRED_WHEN_COMMITTING_STATE .buildMessage (
164135 cause .getMessage ()),
165136 cause ,
166- context . transactionId );
137+ id );
167138 }
168139 } catch (CoordinatorException ex ) {
169140 throw new UnknownTransactionStatusException (
170141 CoreError .CONSENSUS_COMMIT_CANNOT_GET_COORDINATOR_STATUS .buildMessage (ex .getMessage ()),
171142 ex ,
172- context . transactionId );
143+ id );
173144 }
174145 }
175146
176147 /**
177- * Writes the ABORTED state. When coordinator write-set logging is enabled, the {@code
178- * tx_write_set} column is populated from the given context's snapshot; otherwise the column is
179- * omitted.
148+ * Writes the ABORTED state for the transaction identified by {@code id}, persisting the given
149+ * pre-encoded {@code writeSet} (or none when {@code null}).
180150 */
181- TransactionState abortState (TransactionContext context ) throws UnknownTransactionStatusException {
182- // Same opt-in gating as commitState: skip WriteSet encoding when write-set logging is
183- // disabled, since the Coordinator schema does not include the column in that case.
184- WriteSet writeSet =
185- coordinatorWriteSetLoggingEnabled
186- ? writeSetEncoder .encodeSingleGroupWriteSet (context , false )
187- : null ;
188- return abortStateInternal (context .transactionId , writeSet );
189- }
190-
191- /** Writes the ABORTED state without persisting a {@code tx_write_set} via a single putState. */
192- TransactionState abortStateWithoutWriteSet (String id ) throws UnknownTransactionStatusException {
193- return abortStateInternal (id , null );
194- }
195-
196- private TransactionState abortStateInternal (String id , @ Nullable WriteSet writeSet )
151+ TransactionState abortState (String id , @ Nullable WriteSet writeSet )
197152 throws UnknownTransactionStatusException {
198153 try {
199154 Coordinator .State state =
0 commit comments