@@ -46,8 +46,10 @@ type CompositeCommitStore struct {
4646 // Manages routing of traffic between the memiavl and flatkv backends.
4747 // Built (and rebuilt) inside LoadVersion against the just-opened
4848 // backends so that lazily-eager constructors like
49- // NewMemiavlMigrationIterator see a non-nil memiavl DB.
50- router migration.Router
49+ // NewMemiavlMigrationIterator see a non-nil memiavl DB. SetWriteMode
50+ // replaces it while views from GetChildStoreByName may be reading it, so
51+ // access it only through loadRouter and storeRouter.
52+ router atomic.Pointer [migration.Router ]
5153
5254 // ctx is the constructor's context. Each invocation of buildRouter
5355 // derives a per-router child context from it and stores the
@@ -412,7 +414,7 @@ func (cs *CompositeCommitStore) LoadVersionReadOnly(targetVersion int64) (_ type
412414 }
413415
414416 // Build a per-handle composite with its own router. Without this the read-only handle has
415- // cs.router == nil and every read-side method nil-dereferences on first call. The new composite
417+ // a nil router and every read-side method nil-dereferences on first call. The new composite
416418 // inherits cs.ctx so cancellation of the parent context cascades, but buildRouter installs its own
417419 // child cancel so closing this handle does not affect the parent.
418420 ro := & CompositeCommitStore {
@@ -475,8 +477,25 @@ func (cs *CompositeCommitStore) resolveCurrentWriteMode(closeIdleFlatKV bool) er
475477 return nil
476478}
477479
480+ // loadRouter returns the installed router, or nil before LoadVersion and
481+ // after Close.
482+ func (cs * CompositeCommitStore ) loadRouter () migration.Router {
483+ if router := cs .router .Load (); router != nil {
484+ return * router
485+ }
486+ return nil
487+ }
488+
489+ func (cs * CompositeCommitStore ) storeRouter (router migration.Router ) {
490+ if router == nil {
491+ cs .router .Store (nil )
492+ return
493+ }
494+ cs .router .Store (& router )
495+ }
496+
478497// buildRouter constructs the migration router against the currently-opened
479- // backends and assigns it to cs.router . Must be called after memIAVL and
498+ // backends and installs it with storeRouter . Must be called after memIAVL and
480499// flatKV (if any) have been opened via LoadVersion and after the effective
481500// mode has been resolved.
482501func (cs * CompositeCommitStore ) buildRouter () error {
@@ -496,7 +515,7 @@ func (cs *CompositeCommitStore) buildRouter() error {
496515 if cs .routerCancel != nil {
497516 cs .routerCancel ()
498517 }
499- cs .router = router
518+ cs .storeRouter ( router )
500519 cs .routerCancel = cancel
501520 return nil
502521}
@@ -519,8 +538,8 @@ func (cs *CompositeCommitStore) SetMigrationBatchSize(batchSize int) error {
519538 batchSize = 0
520539 }
521540 cs .migrationBatchSize .Store (int64 (batchSize ))
522- if cs .router != nil {
523- cs . router .SetMigrationBatchSize (batchSize )
541+ if router := cs .loadRouter (); router != nil {
542+ router .SetMigrationBatchSize (batchSize )
524543 }
525544 return nil
526545}
@@ -596,7 +615,7 @@ func (cs *CompositeCommitStore) SetWriteMode(targetWriteMode types.WriteMode) er
596615 "write mode is fixed at %q by configuration; runtime switching requires write mode %q" ,
597616 cs .config .WriteMode , types .Auto )
598617 }
599- if cs .router == nil {
618+ if cs .loadRouter () == nil {
600619 return errors .New ("SetWriteMode called before LoadVersion" )
601620 }
602621 if targetWriteMode == cs .currentWriteMode {
@@ -718,7 +737,7 @@ func (cs *CompositeCommitStore) materializeFlatKV() error {
718737func (cs * CompositeCommitStore ) ApplyChangeSets (changesets []* proto.NamedChangeSet ) error {
719738 if cs .currentWriteMode .IsMigrationMode () {
720739 firstBatchInBlock := ! cs .migrationAdvancedThisCommit
721- if err := cs .router .ApplyChangeSets (changesets , firstBatchInBlock ); err != nil {
740+ if err := cs .loadRouter () .ApplyChangeSets (changesets , firstBatchInBlock ); err != nil {
722741 return fmt .Errorf ("failed to apply changesets: %w" , err )
723742 }
724743 cs .migrationAdvancedThisCommit = true
@@ -727,7 +746,7 @@ func (cs *CompositeCommitStore) ApplyChangeSets(changesets []*proto.NamedChangeS
727746 return nil
728747 }
729748
730- err := cs .router .ApplyChangeSets (changesets , false )
749+ err := cs .loadRouter () .ApplyChangeSets (changesets , false )
731750 if err != nil {
732751 return fmt .Errorf ("failed to apply changesets: %w" , err )
733752 }
@@ -1114,11 +1133,11 @@ func (cs *CompositeCommitStore) GetChildStoreByName(name string) types.CommitKVS
11141133 }
11151134 }
11161135
1117- // The provider resolves cs. router at call time: SetWriteMode replaces
1136+ // The provider resolves the router at call time: SetWriteMode replaces
11181137 // the router while views vended here stay cached by rootmulti, and a
11191138 // captured router value would keep serving the pre-transition mode.
11201139 return migration .NewRouterCommitKVStore (
1121- func () migration. Router { return cs .router } ,
1140+ cs .loadRouter ,
11221141 name ,
11231142 cs .Version ,
11241143 func (start , end []byte , ascending bool ) (db.Iterator , error ) {
@@ -1166,7 +1185,7 @@ func (cs *CompositeCommitStore) ReleaseSnapshotRefs() error {
11661185 cs .routerCancel ()
11671186 cs .routerCancel = nil
11681187 }
1169- cs .router = nil
1188+ cs .storeRouter ( nil )
11701189 if cs .memIAVL == nil {
11711190 return nil
11721191 }
@@ -1412,7 +1431,7 @@ func (cs *CompositeCommitStore) Close() error {
14121431 cs .routerCancel ()
14131432 cs .routerCancel = nil
14141433 }
1415- cs .router = nil
1434+ cs .storeRouter ( nil )
14161435
14171436 if cs .memIAVL != nil {
14181437 if err := cs .memIAVL .Close (); err != nil {
@@ -1437,7 +1456,7 @@ func (cs *CompositeCommitStore) Get(store string, key []byte) (value []byte, ok
14371456 return nil , false , fmt .Errorf ("key cannot be nil" )
14381457 }
14391458
1440- value , ok , err = cs .router .Read (store , key )
1459+ value , ok , err = cs .loadRouter () .Read (store , key )
14411460 if err != nil {
14421461 return nil , false , fmt .Errorf ("failed to read value: %w" , err )
14431462 }
@@ -1452,7 +1471,7 @@ func (cs *CompositeCommitStore) GetProof(store string, key []byte) (*ics23.Commi
14521471 return nil , fmt .Errorf ("key cannot be nil" )
14531472 }
14541473
1455- proof , err := cs .router .GetProof (store , key )
1474+ proof , err := cs .loadRouter () .GetProof (store , key )
14561475 if err != nil {
14571476 return nil , fmt .Errorf ("failed to get proof: %w" , err )
14581477 }
0 commit comments