4747@ NotThreadSafe
4848public class ConsensusCommit extends AbstractDistributedTransaction {
4949 private static final Logger logger = LoggerFactory .getLogger (ConsensusCommit .class );
50+ private final TransactionContext context ;
5051 private final CrudHandler crud ;
5152 private final CommitHandler commit ;
5253 private final ConsensusCommitMutationOperationChecker mutationOperationChecker ;
5354 @ Nullable private final CoordinatorGroupCommitter groupCommitter ;
5455
5556 @ SuppressFBWarnings ("EI_EXPOSE_REP2" )
5657 public ConsensusCommit (
58+ TransactionContext context ,
5759 CrudHandler crud ,
5860 CommitHandler commit ,
5961 ConsensusCommitMutationOperationChecker mutationOperationChecker ,
6062 @ Nullable CoordinatorGroupCommitter groupCommitter ) {
63+ this .context = checkNotNull (context );
6164 this .crud = checkNotNull (crud );
6265 this .commit = checkNotNull (commit );
6366 this .mutationOperationChecker = mutationOperationChecker ;
@@ -66,23 +69,23 @@ public ConsensusCommit(
6669
6770 @ Override
6871 public String getId () {
69- return crud . getSnapshot (). getId () ;
72+ return context . transactionId ;
7073 }
7174
7275 @ Override
7376 public Optional <Result > get (Get get ) throws CrudException {
74- return crud .get (copyAndSetTargetToIfNot (get ));
77+ return crud .get (copyAndSetTargetToIfNot (get ), context );
7578 }
7679
7780 @ Override
7881 public List <Result > scan (Scan scan ) throws CrudException {
79- return crud .scan (copyAndSetTargetToIfNot (scan ));
82+ return crud .scan (copyAndSetTargetToIfNot (scan ), context );
8083 }
8184
8285 @ Override
8386 public Scanner getScanner (Scan scan ) throws CrudException {
8487 scan = copyAndSetTargetToIfNot (scan );
85- return crud .getScanner (scan );
88+ return crud .getScanner (scan , context );
8689 }
8790
8891 /** @deprecated As of release 3.13.0. Will be removed in release 5.0.0. */
@@ -91,7 +94,7 @@ public Scanner getScanner(Scan scan) throws CrudException {
9194 public void put (Put put ) throws CrudException {
9295 put = copyAndSetTargetToIfNot (put );
9396 checkMutation (put );
94- crud .put (put );
97+ crud .put (put , context );
9598 }
9699
97100 /** @deprecated As of release 3.13.0. Will be removed in release 5.0.0. */
@@ -108,7 +111,7 @@ public void put(List<Put> puts) throws CrudException {
108111 public void delete (Delete delete ) throws CrudException {
109112 delete = copyAndSetTargetToIfNot (delete );
110113 checkMutation (delete );
111- crud .delete (delete );
114+ crud .delete (delete , context );
112115 }
113116
114117 /** @deprecated As of release 3.13.0. Will be removed in release 5.0.0. */
@@ -126,15 +129,15 @@ public void insert(Insert insert) throws CrudException {
126129 insert = copyAndSetTargetToIfNot (insert );
127130 Put put = ConsensusCommitUtils .createPutForInsert (insert );
128131 checkMutation (put );
129- crud .put (put );
132+ crud .put (put , context );
130133 }
131134
132135 @ Override
133136 public void upsert (Upsert upsert ) throws CrudException {
134137 upsert = copyAndSetTargetToIfNot (upsert );
135138 Put put = ConsensusCommitUtils .createPutForUpsert (upsert );
136139 checkMutation (put );
137- crud .put (put );
140+ crud .put (put , context );
138141 }
139142
140143 @ Override
@@ -144,13 +147,13 @@ public void update(Update update) throws CrudException {
144147 Put put = ConsensusCommitUtils .createPutForUpdate (update );
145148 checkMutation (put );
146149 try {
147- crud .put (put );
150+ crud .put (put , context );
148151 } catch (UnsatisfiedConditionException e ) {
149152 if (update .getCondition ().isPresent ()) {
150153 throw new UnsatisfiedConditionException (
151154 ConsensusCommitUtils .convertUnsatisfiedConditionExceptionMessageForUpdate (
152155 e , update .getCondition ().get ()),
153- crud . getSnapshot (). getId ());
156+ getId ());
154157 }
155158
156159 // If the condition is not specified, it means that the record does not exist. In this case,
@@ -179,13 +182,13 @@ public void mutate(List<? extends Mutation> mutations) throws CrudException {
179182
180183 @ Override
181184 public void commit () throws CommitException , UnknownTransactionStatusException {
182- if (!crud .areAllScannersClosed ()) {
185+ if (!context .areAllScannersClosed ()) {
183186 throw new IllegalStateException (CoreError .CONSENSUS_COMMIT_SCANNER_NOT_CLOSED .buildMessage ());
184187 }
185188
186189 // Execute implicit pre-read
187190 try {
188- crud .readIfImplicitPreReadEnabled ();
191+ crud .readIfImplicitPreReadEnabled (context );
189192 } catch (CrudConflictException e ) {
190193 throw new CommitConflictException (
191194 CoreError .CONSENSUS_COMMIT_CONFLICT_OCCURRED_WHILE_IMPLICIT_PRE_READ .buildMessage (),
@@ -197,29 +200,34 @@ public void commit() throws CommitException, UnknownTransactionStatusException {
197200 }
198201
199202 try {
200- crud .waitForRecoveryCompletionIfNecessary ();
203+ crud .waitForRecoveryCompletionIfNecessary (context );
201204 } catch (CrudConflictException e ) {
202205 throw new CommitConflictException (e .getMessage (), e , getId ());
203206 } catch (CrudException e ) {
204207 throw new CommitException (e .getMessage (), e , getId ());
205208 }
206209
207- commit .commit (crud . getSnapshot (), crud . isReadOnly () );
210+ commit .commit (context );
208211 }
209212
210213 @ Override
211214 public void rollback () {
212215 try {
213- crud .closeScanners ();
216+ context .closeScanners ();
214217 } catch (CrudException e ) {
215218 logger .warn ("Failed to close the scanner" , e );
216219 }
217220
218- if (groupCommitter != null && !crud . isReadOnly () ) {
219- groupCommitter .remove (crud . getSnapshot (). getId ());
221+ if (groupCommitter != null && !context . readOnly ) {
222+ groupCommitter .remove (getId ());
220223 }
221224 }
222225
226+ @ VisibleForTesting
227+ TransactionContext getTransactionContext () {
228+ return context ;
229+ }
230+
223231 @ VisibleForTesting
224232 CrudHandler getCrudHandler () {
225233 return crud ;
@@ -230,6 +238,11 @@ CommitHandler getCommitHandler() {
230238 return commit ;
231239 }
232240
241+ @ VisibleForTesting
242+ void waitForRecoveryCompletion () throws CrudException {
243+ crud .waitForRecoveryCompletion (context );
244+ }
245+
233246 private void checkMutation (Mutation mutation ) throws CrudException {
234247 try {
235248 mutationOperationChecker .check (mutation );
0 commit comments