@@ -2308,14 +2308,20 @@ func (s *Store) MergeRange(
23082308 leftRepl .raftMu .AssertHeld ()
23092309 rightRepl .raftMu .AssertHeld ()
23102310
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- AllowPlaceholders : true ,
2317- }); err != nil {
2318- return errors .Errorf ("cannot remove range: %s" , err )
2311+ if rightRepl .IsInitialized () {
2312+ // Note that we were called (indirectly) from raft processing so we must
2313+ // call removeReplicaImpl directly to avoid deadlocking on the right-hand
2314+ // replica's raftMu.
2315+ if err := s .removeReplicaImpl (ctx , rightRepl , rightDesc , RemoveOptions {
2316+ DestroyData : false ,
2317+ }); err != nil {
2318+ return errors .Errorf ("cannot remove range: %s" , err )
2319+ }
2320+ } else {
2321+ s .mu .Lock ()
2322+ s .unlinkReplicaByRangeIDLocked (rightRepl .RangeID )
2323+ _ = s .removePlaceholderLocked (ctx , rightRepl .RangeID )
2324+ s .mu .Unlock ()
23192325 }
23202326
23212327 if leftRepl .leaseholderStats != nil {
@@ -2365,8 +2371,16 @@ func (s *Store) addReplicaInternalLocked(repl *Replica) error {
23652371 return nil
23662372}
23672373
2374+ // addPlaceholderLocked adds the specified placeholder. Requires that the
2375+ // raftMu of the replica whose place is being held is locked.
2376+ func (s * Store ) addPlaceholder (placeholder * ReplicaPlaceholder ) error {
2377+ s .mu .Lock ()
2378+ defer s .mu .Unlock ()
2379+ return s .addPlaceholderLocked (placeholder )
2380+ }
2381+
23682382// addPlaceholderLocked adds the specified placeholder. Requires that Store.mu
2369- // and Replica. raftMu are held.
2383+ // and the raftMu of the replica whose place is being held are locked .
23702384func (s * Store ) addPlaceholderLocked (placeholder * ReplicaPlaceholder ) error {
23712385 rangeID := placeholder .Desc ().RangeID
23722386 if exRng := s .mu .replicasByKey .ReplaceOrInsert (placeholder ); exRng != nil {
@@ -2381,15 +2395,16 @@ func (s *Store) addPlaceholderLocked(placeholder *ReplicaPlaceholder) error {
23812395
23822396// removePlaceholder removes a placeholder for the specified range if it
23832397// exists, returning true if a placeholder was present and removed and false
2384- // otherwise. Requires that Replica.raftMu is held.
2398+ // otherwise. Requires that the raftMu of the replica whose place is being held
2399+ // is locked.
23852400func (s * Store ) removePlaceholder (ctx context.Context , rngID roachpb.RangeID ) bool {
23862401 s .mu .Lock ()
23872402 defer s .mu .Unlock ()
23882403 return s .removePlaceholderLocked (ctx , rngID )
23892404}
23902405
23912406// removePlaceholderLocked removes the specified placeholder. Requires that
2392- // Store.mu and Replica. raftMu are held.
2407+ // Store.mu and the raftMu of the replica whose place is being held are locked .
23932408func (s * Store ) removePlaceholderLocked (ctx context.Context , rngID roachpb.RangeID ) bool {
23942409 placeholder , ok := s .mu .replicaPlaceholders [rngID ]
23952410 if ! ok {
@@ -2421,8 +2436,7 @@ func (s *Store) addReplicaToRangeMapLocked(repl *Replica) error {
24212436
24222437// RemoveOptions bundles boolean parameters for Store.RemoveReplica.
24232438type RemoveOptions struct {
2424- DestroyData bool
2425- AllowPlaceholders bool
2439+ DestroyData bool
24262440}
24272441
24282442// RemoveReplica removes the replica from the store's replica map and
@@ -2434,9 +2448,6 @@ type RemoveOptions struct {
24342448// If opts.DestroyData is true, data in all of the range's keyspaces
24352449// is deleted. Otherwise, only data in the range-ID local keyspace is
24362450// deleted. In either case a tombstone record is written.
2437- //
2438- // If opts.AllowPlaceholders is true, it is not an error if the replica
2439- // targeted for removal is an uninitialized placeholder.
24402451func (s * Store ) RemoveReplica (
24412452 ctx context.Context , rep * Replica , consistentDesc roachpb.RangeDescriptor , opts RemoveOptions ,
24422453) error {
@@ -2493,14 +2504,12 @@ func (s *Store) removeReplicaImpl(
24932504 s .mu .Unlock ()
24942505 return err
24952506 }
2496- if ! opts .AllowPlaceholders {
2497- if placeholder := s .getOverlappingKeyRangeLocked (desc ); placeholder != rep {
2498- // This is a fatal error because uninitialized replicas shouldn't make it
2499- // this far. This method will need some changes when we introduce GC of
2500- // uninitialized replicas.
2501- s .mu .Unlock ()
2502- log .Fatalf (ctx , "replica %+v unexpectedly overlapped by %+v" , rep , placeholder )
2503- }
2507+ if placeholder := s .getOverlappingKeyRangeLocked (desc ); placeholder != rep {
2508+ // This is a fatal error because uninitialized replicas shouldn't make it
2509+ // this far. This method will need some changes when we introduce GC of
2510+ // uninitialized replicas.
2511+ s .mu .Unlock ()
2512+ log .Fatalf (ctx , "replica %+v unexpectedly overlapped by %+v" , rep , placeholder )
25042513 }
25052514 // Adjust stats before calling Destroy. This can be called before or after
25062515 // Destroy, but this configuration helps avoid races in stat verification
@@ -2529,12 +2538,10 @@ func (s *Store) removeReplicaImpl(
25292538 s .mu .Lock ()
25302539 defer s .mu .Unlock ()
25312540 s .unlinkReplicaByRangeIDLocked (rep .RangeID )
2532- if ! opts .AllowPlaceholders {
2533- if placeholder := s .mu .replicasByKey .Delete (rep ); placeholder != rep {
2534- // We already checked that our replica was present in replicasByKey
2535- // above. Nothing should have been able to change that.
2536- log .Fatalf (ctx , "replica %+v unexpectedly overlapped by %+v" , rep , placeholder )
2537- }
2541+ if placeholder := s .mu .replicasByKey .Delete (rep ); placeholder != rep {
2542+ // We already checked that our replica was present in replicasByKey
2543+ // above. Nothing should have been able to change that.
2544+ log .Fatalf (ctx , "replica %+v unexpectedly overlapped by %+v" , rep , placeholder )
25382545 }
25392546 delete (s .mu .replicaPlaceholders , rep .RangeID )
25402547 // TODO(peter): Could release s.mu.Lock() here.
0 commit comments