20
20
21
21
import java .util .ArrayList ;
22
22
import java .util .Arrays ;
23
- import java .util .Iterator ;
24
23
import java .util .List ;
25
24
import java .util .Map ;
26
25
import java .util .Objects ;
@@ -133,7 +132,7 @@ protected static class ShardHolder
133
132
this .store = store ;
134
133
}
135
134
136
- private ShardHolder (CommandStore store , RangesForEpoch ranges )
135
+ public ShardHolder (CommandStore store , RangesForEpoch ranges )
137
136
{
138
137
this .store = store ;
139
138
this .ranges = ranges ;
@@ -144,7 +143,7 @@ public ShardHolder withStoreUnsafe(CommandStore store)
144
143
return new ShardHolder (store , ranges );
145
144
}
146
145
147
- RangesForEpoch ranges ()
146
+ public RangesForEpoch ranges ()
148
147
{
149
148
return ranges ;
150
149
}
@@ -381,43 +380,6 @@ public Ranges removed(long presentIn, long removedByInclusive)
381
380
}
382
381
}
383
382
384
- // This method should only be used on node startup.
385
- // "Unsafe" because it relies on user to synchronise and sequence the call properly.
386
- public void restoreShardStateUnsafe (Consumer <Topology > reportTopology )
387
- {
388
- Iterator <Journal .TopologyUpdate > iter = journal .replayTopologies ();
389
- // First boot
390
- if (!iter .hasNext ())
391
- return ;
392
-
393
- Journal .TopologyUpdate lastUpdate = null ;
394
- while (iter .hasNext ())
395
- {
396
- Journal .TopologyUpdate update = iter .next ();
397
- reportTopology .accept (update .global );
398
- if (lastUpdate == null || update .global .epoch () > lastUpdate .global .epoch ())
399
- lastUpdate = update ;
400
- }
401
-
402
- ShardHolder [] shards = new ShardHolder [lastUpdate .commandStores .size ()];
403
- int i = 0 ;
404
- for (Map .Entry <Integer , RangesForEpoch > e : lastUpdate .commandStores .entrySet ())
405
- {
406
- RangesForEpoch ranges = e .getValue ();
407
- CommandStore commandStore = null ;
408
- for (ShardHolder shard : current .shards )
409
- {
410
- if (shard .ranges .equals (ranges ))
411
- commandStore = shard .store ;
412
- }
413
- Invariants .nonNull (commandStore , "Command store should have been reloaded" ).restore ();
414
- ShardHolder shard = new ShardHolder (commandStore , e .getValue ());
415
- shards [i ++] = shard ;
416
- }
417
-
418
- loadSnapshot (new Snapshot (shards , lastUpdate .local , lastUpdate .global ));
419
- }
420
-
421
383
protected void loadSnapshot (Snapshot toLoad )
422
384
{
423
385
current = toLoad ;
@@ -428,7 +390,7 @@ protected static class Snapshot extends Journal.TopologyUpdate
428
390
public final ShardHolder [] shards ;
429
391
public final Int2ObjectHashMap <CommandStore > byId ;
430
392
431
- Snapshot (ShardHolder [] shards , Topology local , Topology global )
393
+ public Snapshot (ShardHolder [] shards , Topology local , Topology global )
432
394
{
433
395
super (asMap (shards ), local , global );
434
396
this .shards = shards ;
@@ -771,6 +733,20 @@ protected <O> AsyncChain<O> mapReduce(PreLoadContext context, MapReduce<? super
771
733
return chain == null ? AsyncChains .success (null ) : chain ;
772
734
}
773
735
736
+ /**
737
+ * Initialize topology from snapshot on boot.
738
+ */
739
+ public synchronized void initializeTopologyUnsafe (Journal .TopologyUpdate update )
740
+ {
741
+ Invariants .require (current .global .epoch () == 0 );
742
+ ShardHolder [] shards = new ShardHolder [update .commandStores .size ()];
743
+ int i = 0 ;
744
+ for (Map .Entry <Integer , RangesForEpoch > e : update .commandStores .entrySet ())
745
+ shards [i ++] = new ShardHolder (supplier .create (e .getKey (), new EpochUpdateHolder ()), e .getValue ());
746
+
747
+ loadSnapshot (new Snapshot (shards , update .local , update .global ));
748
+ }
749
+
774
750
public synchronized Supplier <EpochReady > updateTopology (Node node , Topology newTopology , boolean startSync )
775
751
{
776
752
TopologyUpdate update = updateTopology (node , current , newTopology , startSync );
0 commit comments