13
13
*/
14
14
package io .streamnative .pulsar .handlers .kop .coordinator .transaction ;
15
15
16
+ import com .google .common .collect .ImmutableSet ;
16
17
import com .google .common .collect .Sets ;
17
18
import io .streamnative .pulsar .handlers .kop .scala .Either ;
18
19
import io .streamnative .pulsar .handlers .kop .utils .CoreUtils ;
19
- import java .util .Collections ;
20
20
import java .util .HashMap ;
21
- import java .util .HashSet ;
22
21
import java .util .Map ;
23
22
import java .util .Optional ;
24
23
import java .util .Set ;
@@ -132,7 +131,7 @@ public TxnTransitMetadata prepareTransitionTo(TransactionState newState,
132
131
short newEpoch ,
133
132
short newLastEpoch ,
134
133
int newTxnTimeoutMs ,
135
- Set <TopicPartition > newTopicPartitions ,
134
+ ImmutableSet <TopicPartition > newTopicPartitions ,
136
135
long newTxnStartTimestamp ,
137
136
long updateTimestamp ) {
138
137
if (pendingState .isPresent ()) {
@@ -157,7 +156,7 @@ public TxnTransitMetadata prepareTransitionTo(TransactionState newState,
157
156
.lastProducerEpoch (newLastEpoch )
158
157
.txnTimeoutMs (newTxnTimeoutMs )
159
158
.txnState (newState )
160
- .topicPartitions (Collections . unmodifiableSet ( newTopicPartitions ) )
159
+ .topicPartitions (newTopicPartitions )
161
160
.txnStartTimestamp (newTxnStartTimestamp )
162
161
.txnLastUpdateTimestamp (updateTimestamp ).build ();
163
162
if (log .isDebugEnabled ()) {
@@ -175,6 +174,8 @@ public TxnTransitMetadata prepareTransitionTo(TransactionState newState,
175
174
176
175
/**
177
176
* Transaction transit metadata.
177
+ *
178
+ * this is a immutable object representing the target transition of the transaction metadata.
178
179
*/
179
180
@ ToString
180
181
@ Builder
@@ -187,7 +188,7 @@ public static class TxnTransitMetadata {
187
188
private short lastProducerEpoch ;
188
189
private int txnTimeoutMs ;
189
190
private TransactionState txnState ;
190
- private Set <TopicPartition > topicPartitions ;
191
+ private ImmutableSet <TopicPartition > topicPartitions ;
191
192
private long txnStartTimestamp ;
192
193
private long txnLastUpdateTimestamp ;
193
194
}
@@ -236,7 +237,7 @@ public void completeTransitionTo(TxnTransitMetadata transitMetadata) {
236
237
throwStateTransitionFailure (transitMetadata );
237
238
} else {
238
239
txnStartTimestamp = transitMetadata .txnStartTimestamp ;
239
- addPartitions (transitMetadata .topicPartitions );
240
+ topicPartitions . addAll (transitMetadata .topicPartitions );
240
241
}
241
242
break ;
242
243
case PREPARE_ABORT : // from endTxn
@@ -258,7 +259,7 @@ public void completeTransitionTo(TxnTransitMetadata transitMetadata) {
258
259
throwStateTransitionFailure (transitMetadata );
259
260
} else {
260
261
this .txnStartTimestamp = transitMetadata .txnStartTimestamp ;
261
- this .topicPartitions = Collections . emptySet ();
262
+ this .topicPartitions . clear ();
262
263
}
263
264
break ;
264
265
case PREPARE_EPOCH_FENCE :
@@ -308,7 +309,7 @@ public TxnTransitMetadata prepareNoTransit() {
308
309
// do not call transitTo as it will set the pending state,
309
310
// a follow-up call to abort the transaction will set its pending state
310
311
return new TxnTransitMetadata (producerId , lastProducerId , producerEpoch , lastProducerEpoch , txnTimeoutMs ,
311
- state , topicPartitions , txnStartTimestamp , txnLastUpdateTimestamp );
312
+ state , ImmutableSet . copyOf ( topicPartitions ) , txnStartTimestamp , txnLastUpdateTimestamp );
312
313
}
313
314
314
315
public TxnTransitMetadata prepareFenceProducerEpoch () {
@@ -332,7 +333,7 @@ public TxnTransitMetadata prepareFenceProducerEpoch() {
332
333
bumpedEpoch ,
333
334
RecordBatch .NO_PRODUCER_EPOCH ,
334
335
txnTimeoutMs ,
335
- topicPartitions ,
336
+ ImmutableSet . copyOf ( topicPartitions ) ,
336
337
txnStartTimestamp ,
337
338
txnLastUpdateTimestamp );
338
339
}
@@ -377,7 +378,7 @@ public Either<Errors, TxnTransitMetadata> prepareIncrementProducerEpoch(Integer
377
378
378
379
return errorsOrBumpEpochResult .map (bumpEpochResult -> prepareTransitionTo (TransactionState .EMPTY ,
379
380
producerId , bumpEpochResult .bumpedEpoch , bumpEpochResult .lastEpoch , newTxnTimeoutMs ,
380
- Collections . emptySet (), -1 , updateTimestamp ));
381
+ ImmutableSet . of (), -1 , updateTimestamp ));
381
382
}
382
383
383
384
@ AllArgsConstructor
@@ -400,7 +401,7 @@ public TxnTransitMetadata prepareProducerIdRotation(Long newProducerId,
400
401
(short ) 0 ,
401
402
recordLastEpoch ? producerEpoch : RecordBatch .NO_PRODUCER_EPOCH ,
402
403
newTxnTimeoutMs ,
403
- Collections . emptySet (),
404
+ ImmutableSet . of (),
404
405
-1 ,
405
406
updateTimestamp );
406
407
}
@@ -419,7 +420,8 @@ private boolean hasPendingTransaction() {
419
420
return flag ;
420
421
}
421
422
422
- public TxnTransitMetadata prepareAddPartitions (Set <TopicPartition > addedTopicPartitions , Long updateTimestamp ) {
423
+ public TxnTransitMetadata prepareAddPartitions (ImmutableSet <TopicPartition > addedTopicPartitions ,
424
+ Long updateTimestamp ) {
423
425
long newTxnStartTimestamp ;
424
426
switch (state ) {
425
427
case EMPTY :
@@ -430,18 +432,17 @@ public TxnTransitMetadata prepareAddPartitions(Set<TopicPartition> addedTopicPar
430
432
default :
431
433
newTxnStartTimestamp = txnStartTimestamp ;
432
434
}
433
- Set <TopicPartition > newPartitionSet = new HashSet <>();
434
- if (topicPartitions != null ) {
435
- newPartitionSet .addAll (topicPartitions );
436
- }
437
- newPartitionSet .addAll (new HashSet <>(addedTopicPartitions ));
435
+ ImmutableSet <TopicPartition > partitions = ImmutableSet .<TopicPartition >builder ()
436
+ .addAll ((topicPartitions != null ) ? topicPartitions : ImmutableSet .of ())
437
+ .addAll (addedTopicPartitions )
438
+ .build ();
438
439
return prepareTransitionTo (TransactionState .ONGOING , producerId , producerEpoch , lastProducerEpoch ,
439
- txnTimeoutMs , newPartitionSet , newTxnStartTimestamp , updateTimestamp );
440
+ txnTimeoutMs , partitions , newTxnStartTimestamp , updateTimestamp );
440
441
}
441
442
442
443
public TxnTransitMetadata prepareAbortOrCommit (TransactionState newState , Long updateTimestamp ) {
443
444
return prepareTransitionTo (newState , producerId , producerEpoch , lastProducerEpoch ,
444
- txnTimeoutMs , topicPartitions , txnStartTimestamp , updateTimestamp );
445
+ txnTimeoutMs , ImmutableSet . copyOf ( topicPartitions ) , txnStartTimestamp , updateTimestamp );
445
446
}
446
447
447
448
public TxnTransitMetadata prepareComplete (Long updateTimestamp ) {
@@ -455,12 +456,12 @@ public TxnTransitMetadata prepareComplete(Long updateTimestamp) {
455
456
hasFailedEpochFence = false ;
456
457
457
458
return prepareTransitionTo (newState , producerId , producerEpoch , lastProducerEpoch ,
458
- txnTimeoutMs , Collections . emptySet (), txnStartTimestamp , updateTimestamp );
459
+ txnTimeoutMs , ImmutableSet . of (), txnStartTimestamp , updateTimestamp );
459
460
}
460
461
461
462
public TxnTransitMetadata prepareDead () {
462
463
return prepareTransitionTo (TransactionState .DEAD , producerId , producerEpoch , lastProducerEpoch , txnTimeoutMs ,
463
- Collections . emptySet (), txnStartTimestamp , txnLastUpdateTimestamp );
464
+ ImmutableSet . of (), txnStartTimestamp , txnLastUpdateTimestamp );
464
465
}
465
466
466
467
private void throwStateTransitionFailure (TxnTransitMetadata txnTransitMetadata ) throws IllegalStateException {
@@ -484,27 +485,11 @@ public void removePartition(TopicPartition topicPartition) {
484
485
+ "trying to remove partitions whose txn marker has been sent, this is not expected" ,
485
486
state , pendingState ));
486
487
}
487
- Set <TopicPartition > newTopicPartitions = new HashSet <>(topicPartitions );
488
- newTopicPartitions .remove (topicPartition );
489
- this .topicPartitions = Collections .unmodifiableSet (newTopicPartitions );
490
- }
491
-
492
- public void removePartitions (Set <TopicPartition > topicPartitions ) {
493
- if (state != TransactionState .PREPARE_COMMIT && state != TransactionState .PREPARE_ABORT ) {
494
- throw new IllegalStateException (
495
- String .format ("Transaction metadata's current state is %s, and its pending state is %s while "
496
- + "trying to remove partitions whose txn marker has been sent, this is not expected" ,
497
- state , pendingState ));
498
- }
499
- Set <TopicPartition > newTopicPartitions = new HashSet <>(topicPartitions );
500
- newTopicPartitions .removeAll (topicPartitions );
501
- this .topicPartitions = Collections .unmodifiableSet (newTopicPartitions );
488
+ topicPartitions .remove (topicPartition );
502
489
}
503
490
504
491
public void addPartitions (Set <TopicPartition > partitions ) {
505
- Set <TopicPartition > newTopicPartitions = new HashSet <>(topicPartitions );
506
- newTopicPartitions .addAll (partitions );
507
- this .topicPartitions = Collections .unmodifiableSet (newTopicPartitions );
492
+ topicPartitions .addAll (partitions );
508
493
}
509
494
510
495
public boolean pendingTransitionInProgress () {
0 commit comments