@@ -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 {
@@ -2492,14 +2503,12 @@ func (s *Store) removeReplicaImpl(
24922503 }
24932504
24942505 s .mu .Lock ()
2495- if ! opts .AllowPlaceholders {
2496- if placeholder := s .getOverlappingKeyRangeLocked (desc ); placeholder != rep {
2497- // This is a fatal error because uninitialized replicas shouldn't make it
2498- // this far. This method will need some changes when we introduce GC of
2499- // uninitialized replicas.
2500- s .mu .Unlock ()
2501- log .Fatalf (ctx , "replica %+v unexpectedly overlapped by %+v" , rep , placeholder )
2502- }
2506+ if placeholder := s .getOverlappingKeyRangeLocked (desc ); placeholder != rep {
2507+ // This is a fatal error because uninitialized replicas shouldn't make it
2508+ // this far. This method will need some changes when we introduce GC of
2509+ // uninitialized replicas.
2510+ s .mu .Unlock ()
2511+ log .Fatalf (ctx , "replica %+v unexpectedly overlapped by %+v" , rep , placeholder )
25032512 }
25042513 // Adjust stats before calling Destroy. This can be called before or after
25052514 // Destroy, but this configuration helps avoid races in stat verification
@@ -2528,12 +2537,10 @@ func (s *Store) removeReplicaImpl(
25282537 s .mu .Lock ()
25292538 defer s .mu .Unlock ()
25302539 s .unlinkReplicaByRangeIDLocked (rep .RangeID )
2531- if ! opts .AllowPlaceholders {
2532- if placeholder := s .mu .replicasByKey .Delete (rep ); placeholder != rep {
2533- // We already checked that our replica was present in replicasByKey
2534- // above. Nothing should have been able to change that.
2535- log .Fatalf (ctx , "replica %+v unexpectedly overlapped by %+v" , rep , placeholder )
2536- }
2540+ if placeholder := s .mu .replicasByKey .Delete (rep ); placeholder != rep {
2541+ // We already checked that our replica was present in replicasByKey
2542+ // above. Nothing should have been able to change that.
2543+ log .Fatalf (ctx , "replica %+v unexpectedly overlapped by %+v" , rep , placeholder )
25372544 }
25382545 delete (s .mu .replicaPlaceholders , rep .RangeID )
25392546 // TODO(peter): Could release s.mu.Lock() here.
0 commit comments