@@ -2307,14 +2307,20 @@ func (s *Store) MergeRange(
23072307 leftRepl .raftMu .AssertHeld ()
23082308 rightRepl .raftMu .AssertHeld ()
23092309
2310- // Note that we were called (indirectly) from raft processing so we must
2311- // call removeReplicaImpl directly to avoid deadlocking on the right-hand
2312- // replica's raftMu.
2313- if err := s .removeReplicaImpl (ctx , rightRepl , rightDesc , RemoveOptions {
2314- DestroyData : false ,
2315- AllowPlaceholders : true ,
2316- }); err != nil {
2317- return errors .Errorf ("cannot remove range: %s" , err )
2310+ if rightRepl .IsInitialized () {
2311+ // Note that we were called (indirectly) from raft processing so we must
2312+ // call removeReplicaImpl directly to avoid deadlocking on the right-hand
2313+ // replica's raftMu.
2314+ if err := s .removeReplicaImpl (ctx , rightRepl , rightDesc , RemoveOptions {
2315+ DestroyData : false ,
2316+ }); err != nil {
2317+ return errors .Errorf ("cannot remove range: %s" , err )
2318+ }
2319+ } else {
2320+ s .mu .Lock ()
2321+ s .unlinkReplicaByRangeIDLocked (rightRepl .RangeID )
2322+ _ = s .removePlaceholderLocked (ctx , rightRepl .RangeID )
2323+ s .mu .Unlock ()
23182324 }
23192325
23202326 if leftRepl .leaseholderStats != nil {
@@ -2364,8 +2370,16 @@ func (s *Store) addReplicaInternalLocked(repl *Replica) error {
23642370 return nil
23652371}
23662372
2373+ // addPlaceholderLocked adds the specified placeholder. Requires that the
2374+ // raftMu of the replica whose place is being held is locked.
2375+ func (s * Store ) addPlaceholder (placeholder * ReplicaPlaceholder ) error {
2376+ s .mu .Lock ()
2377+ defer s .mu .Unlock ()
2378+ return s .addPlaceholderLocked (placeholder )
2379+ }
2380+
23672381// addPlaceholderLocked adds the specified placeholder. Requires that Store.mu
2368- // and Replica. raftMu are held.
2382+ // and the raftMu of the replica whose place is being held are locked .
23692383func (s * Store ) addPlaceholderLocked (placeholder * ReplicaPlaceholder ) error {
23702384 rangeID := placeholder .Desc ().RangeID
23712385 if exRng := s .mu .replicasByKey .ReplaceOrInsert (placeholder ); exRng != nil {
@@ -2380,15 +2394,16 @@ func (s *Store) addPlaceholderLocked(placeholder *ReplicaPlaceholder) error {
23802394
23812395// removePlaceholder removes a placeholder for the specified range if it
23822396// exists, returning true if a placeholder was present and removed and false
2383- // otherwise. Requires that Replica.raftMu is held.
2397+ // otherwise. Requires that the raftMu of the replica whose place is being held
2398+ // is locked.
23842399func (s * Store ) removePlaceholder (ctx context.Context , rngID roachpb.RangeID ) bool {
23852400 s .mu .Lock ()
23862401 defer s .mu .Unlock ()
23872402 return s .removePlaceholderLocked (ctx , rngID )
23882403}
23892404
23902405// removePlaceholderLocked removes the specified placeholder. Requires that
2391- // Store.mu and Replica. raftMu are held.
2406+ // Store.mu and the raftMu of the replica whose place is being held are locked .
23922407func (s * Store ) removePlaceholderLocked (ctx context.Context , rngID roachpb.RangeID ) bool {
23932408 placeholder , ok := s .mu .replicaPlaceholders [rngID ]
23942409 if ! ok {
@@ -2420,8 +2435,7 @@ func (s *Store) addReplicaToRangeMapLocked(repl *Replica) error {
24202435
24212436// RemoveOptions bundles boolean parameters for Store.RemoveReplica.
24222437type RemoveOptions struct {
2423- DestroyData bool
2424- AllowPlaceholders bool
2438+ DestroyData bool
24252439}
24262440
24272441// RemoveReplica removes the replica from the store's replica map and
@@ -2433,9 +2447,6 @@ type RemoveOptions struct {
24332447// If opts.DestroyData is true, data in all of the range's keyspaces
24342448// is deleted. Otherwise, only data in the range-ID local keyspace is
24352449// deleted. In either case a tombstone record is written.
2436- //
2437- // If opts.AllowPlaceholders is true, it is not an error if the replica
2438- // targeted for removal is an uninitialized placeholder.
24392450func (s * Store ) RemoveReplica (
24402451 ctx context.Context , rep * Replica , consistentDesc roachpb.RangeDescriptor , opts RemoveOptions ,
24412452) error {
@@ -2474,14 +2485,12 @@ func (s *Store) removeReplicaImpl(
24742485 }
24752486
24762487 s .mu .Lock ()
2477- if ! opts .AllowPlaceholders {
2478- if placeholder := s .getOverlappingKeyRangeLocked (desc ); placeholder != rep {
2479- // This is a fatal error because uninitialized replicas shouldn't make it
2480- // this far. This method will need some changes when we introduce GC of
2481- // uninitialized replicas.
2482- s .mu .Unlock ()
2483- log .Fatalf (ctx , "replica %+v unexpectedly overlapped by %+v" , rep , placeholder )
2484- }
2488+ if placeholder := s .getOverlappingKeyRangeLocked (desc ); placeholder != rep {
2489+ // This is a fatal error because uninitialized replicas shouldn't make it
2490+ // this far. This method will need some changes when we introduce GC of
2491+ // uninitialized replicas.
2492+ s .mu .Unlock ()
2493+ log .Fatalf (ctx , "replica %+v unexpectedly overlapped by %+v" , rep , placeholder )
24852494 }
24862495 // Adjust stats before calling Destroy. This can be called before or after
24872496 // Destroy, but this configuration helps avoid races in stat verification
@@ -2510,12 +2519,10 @@ func (s *Store) removeReplicaImpl(
25102519 s .mu .Lock ()
25112520 defer s .mu .Unlock ()
25122521 s .unlinkReplicaByRangeIDLocked (rep .RangeID )
2513- if ! opts .AllowPlaceholders {
2514- if placeholder := s .mu .replicasByKey .Delete (rep ); placeholder != rep {
2515- // We already checked that our replica was present in replicasByKey
2516- // above. Nothing should have been able to change that.
2517- log .Fatalf (ctx , "replica %+v unexpectedly overlapped by %+v" , rep , placeholder )
2518- }
2522+ if placeholder := s .mu .replicasByKey .Delete (rep ); placeholder != rep {
2523+ // We already checked that our replica was present in replicasByKey
2524+ // above. Nothing should have been able to change that.
2525+ log .Fatalf (ctx , "replica %+v unexpectedly overlapped by %+v" , rep , placeholder )
25192526 }
25202527 delete (s .mu .replicaPlaceholders , rep .RangeID )
25212528 // TODO(peter): Could release s.mu.Lock() here.
0 commit comments