50
50
import java .util .UUID ;
51
51
import java .util .concurrent .CompletableFuture ;
52
52
import java .util .concurrent .CompletionException ;
53
+ import java .util .concurrent .ConcurrentHashMap ;
53
54
import java .util .concurrent .ConcurrentLinkedDeque ;
54
55
import java .util .concurrent .ConcurrentLinkedQueue ;
55
56
import java .util .concurrent .ConcurrentSkipListMap ;
@@ -241,6 +242,7 @@ public class ManagedLedgerImpl implements ManagedLedger, CreateCallback {
241
242
242
243
protected static final int DEFAULT_LEDGER_DELETE_RETRIES = 3 ;
243
244
protected static final int DEFAULT_LEDGER_DELETE_BACKOFF_TIME_SEC = 60 ;
245
+ private static final String MIGRATION_STATE_PROPERTY = "migrated" ;
244
246
245
247
public enum State {
246
248
None , // Uninitialized
@@ -268,6 +270,7 @@ public enum PositionBound {
268
270
private static final AtomicReferenceFieldUpdater <ManagedLedgerImpl , State > STATE_UPDATER =
269
271
AtomicReferenceFieldUpdater .newUpdater (ManagedLedgerImpl .class , State .class , "state" );
270
272
protected volatile State state = null ;
273
+ private volatile boolean migrated = false ;
271
274
272
275
@ Getter
273
276
private final OrderedScheduler scheduledExecutor ;
@@ -343,7 +346,7 @@ public ManagedLedgerImpl(ManagedLedgerFactoryImpl factory, BookKeeper bookKeeper
343
346
// Get the next rollover time. Add a random value upto 5% to avoid rollover multiple ledgers at the same time
344
347
this .maximumRolloverTimeMs = getMaximumRolloverTimeMs (config );
345
348
this .mlOwnershipChecker = mlOwnershipChecker ;
346
- this .propertiesMap = new HashMap ();
349
+ this .propertiesMap = new ConcurrentHashMap <> ();
347
350
this .inactiveLedgerRollOverTimeMs = config .getInactiveLedgerRollOverTimeMs ();
348
351
if (config .getManagedLedgerInterceptor () != null ) {
349
352
this .managedLedgerInterceptor = config .getManagedLedgerInterceptor ();
@@ -367,7 +370,6 @@ public void operationComplete(ManagedLedgerInfo mlInfo, Stat stat) {
367
370
lastConfirmedEntry = new PositionImpl (mlInfo .getTerminatedPosition ());
368
371
log .info ("[{}] Recovering managed ledger terminated at {}" , name , lastConfirmedEntry );
369
372
}
370
-
371
373
for (LedgerInfo ls : mlInfo .getLedgerInfoList ()) {
372
374
ledgers .put (ls .getLedgerId (), ls );
373
375
}
@@ -379,6 +381,7 @@ public void operationComplete(ManagedLedgerInfo mlInfo, Stat stat) {
379
381
propertiesMap .put (property .getKey (), property .getValue ());
380
382
}
381
383
}
384
+ migrated = mlInfo .hasTerminatedPosition () && propertiesMap .containsKey (MIGRATION_STATE_PROPERTY );
382
385
if (managedLedgerInterceptor != null ) {
383
386
managedLedgerInterceptor .onManagedLedgerPropertiesInitialize (propertiesMap );
384
387
}
@@ -1271,6 +1274,27 @@ private long consumedLedgerSize(long ledgerSize, long ledgerEntries, long consum
1271
1274
}
1272
1275
}
1273
1276
1277
+ public CompletableFuture <Position > asyncMigrate () {
1278
+ propertiesMap .put (MIGRATION_STATE_PROPERTY , Boolean .TRUE .toString ());
1279
+ CompletableFuture <Position > result = new CompletableFuture <>();
1280
+ asyncTerminate (new TerminateCallback () {
1281
+
1282
+ @ Override
1283
+ public void terminateComplete (Position lastCommittedPosition , Object ctx ) {
1284
+ migrated = true ;
1285
+ log .info ("[{}] topic successfully terminated and migrated at {}" , name , lastCommittedPosition );
1286
+ result .complete (lastCommittedPosition );
1287
+ }
1288
+
1289
+ @ Override
1290
+ public void terminateFailed (ManagedLedgerException exception , Object ctx ) {
1291
+ log .info ("[{}] topic failed to terminate and migrate " , name , exception );
1292
+ result .completeExceptionally (exception );
1293
+ }
1294
+ }, null );
1295
+ return result ;
1296
+ }
1297
+
1274
1298
@ Override
1275
1299
public synchronized void asyncTerminate (TerminateCallback callback , Object ctx ) {
1276
1300
if (state == State .Fenced ) {
@@ -1363,6 +1387,11 @@ public boolean isTerminated() {
1363
1387
return state == State .Terminated ;
1364
1388
}
1365
1389
1390
+ @ Override
1391
+ public boolean isMigrated () {
1392
+ return migrated ;
1393
+ }
1394
+
1366
1395
@ Override
1367
1396
public void close () throws InterruptedException , ManagedLedgerException {
1368
1397
final CountDownLatch counter = new CountDownLatch (1 );
0 commit comments