@@ -12,6 +12,7 @@ import (
12
12
"github.com/cockroachdb/cockroach/pkg/kv/kvpb"
13
13
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvstorage"
14
14
"github.com/cockroachdb/cockroach/pkg/roachpb"
15
+ "github.com/cockroachdb/cockroach/pkg/storage"
15
16
"github.com/cockroachdb/cockroach/pkg/util/log"
16
17
"github.com/cockroachdb/cockroach/pkg/util/retry"
17
18
"github.com/cockroachdb/errors"
@@ -51,13 +52,10 @@ var errRetry = errors.New("retry: orphaned replica")
51
52
//
52
53
// The caller must not hold the store's lock.
53
54
func (s * Store ) getOrCreateReplica (
54
- ctx context.Context ,
55
- rangeID roachpb.RangeID ,
56
- replicaID roachpb.ReplicaID ,
57
- creatingReplica * roachpb.ReplicaDescriptor ,
55
+ ctx context.Context , id storage.FullReplicaID , creatingReplica * roachpb.ReplicaDescriptor ,
58
56
) (_ * Replica , created bool , _ error ) {
59
- if replicaID == 0 {
60
- log .Fatalf (ctx , "cannot construct a Replica for range %d with 0 id" , rangeID )
57
+ if id . ReplicaID == 0 {
58
+ log .Fatalf (ctx , "cannot construct a Replica for range %d with 0 id" , id . RangeID )
61
59
}
62
60
// We need a retry loop as the replica we find in the map may be in the
63
61
// process of being removed or may need to be removed. Retries in the loop
@@ -71,12 +69,7 @@ func (s *Store) getOrCreateReplica(
71
69
})
72
70
for {
73
71
r .Next ()
74
- r , created , err := s .tryGetOrCreateReplica (
75
- ctx ,
76
- rangeID ,
77
- replicaID ,
78
- creatingReplica ,
79
- )
72
+ r , created , err := s .tryGetOrCreateReplica (ctx , id , creatingReplica )
80
73
if errors .Is (err , errRetry ) {
81
74
continue
82
75
}
@@ -92,12 +85,9 @@ func (s *Store) getOrCreateReplica(
92
85
// removed. Returns errRetry error if the replica is in a transitional state and
93
86
// its retrieval needs to be retried. Other errors are permanent.
94
87
func (s * Store ) tryGetReplica (
95
- ctx context.Context ,
96
- rangeID roachpb.RangeID ,
97
- replicaID roachpb.ReplicaID ,
98
- creatingReplica * roachpb.ReplicaDescriptor ,
88
+ ctx context.Context , id storage.FullReplicaID , creatingReplica * roachpb.ReplicaDescriptor ,
99
89
) (* Replica , error ) {
100
- repl , found := s .mu .replicasByRangeID .Load (rangeID )
90
+ repl , found := s .mu .replicasByRangeID .Load (id . RangeID )
101
91
if ! found {
102
92
return nil , nil
103
93
}
@@ -120,14 +110,14 @@ func (s *Store) tryGetReplica(
120
110
}
121
111
122
112
// The current replica needs to be removed, remove it and go back around.
123
- if toTooOld := repl .replicaID < replicaID ; toTooOld {
113
+ if toTooOld := repl .replicaID < id . ReplicaID ; toTooOld {
124
114
if shouldLog := log .V (1 ); shouldLog {
125
115
log .Infof (ctx , "found message for replica ID %d which is newer than %v" ,
126
- replicaID , repl )
116
+ id . ReplicaID , repl )
127
117
}
128
118
129
119
repl .mu .RUnlock ()
130
- if err := s .removeReplicaRaftMuLocked (ctx , repl , replicaID , "superseded by newer Replica" , RemoveOptions {
120
+ if err := s .removeReplicaRaftMuLocked (ctx , repl , id . ReplicaID , "superseded by newer Replica" , RemoveOptions {
131
121
DestroyData : true ,
132
122
}); err != nil {
133
123
log .Fatalf (ctx , "failed to remove replica: %v" , err )
@@ -137,17 +127,17 @@ func (s *Store) tryGetReplica(
137
127
}
138
128
defer repl .mu .RUnlock ()
139
129
140
- if repl .replicaID > replicaID {
130
+ if repl .replicaID > id . ReplicaID {
141
131
// The sender is behind and is sending to an old replica.
142
132
// We could silently drop this message but this way we'll inform the
143
133
// sender that they may no longer exist.
144
134
repl .raftMu .Unlock ()
145
135
return nil , & kvpb.RaftGroupDeletedError {}
146
136
}
147
- if repl .replicaID != replicaID {
137
+ if repl .replicaID != id . ReplicaID {
148
138
// This case should have been caught by handleToReplicaTooOld.
149
139
log .Fatalf (ctx , "intended replica id %d unexpectedly does not match the current replica %v" ,
150
- replicaID , repl )
140
+ id . ReplicaID , repl )
151
141
}
152
142
return repl , nil
153
143
}
@@ -159,13 +149,10 @@ func (s *Store) tryGetReplica(
159
149
// tryGetOrCreateReplica will likely succeed, hence the loop in
160
150
// getOrCreateReplica.
161
151
func (s * Store ) tryGetOrCreateReplica (
162
- ctx context.Context ,
163
- rangeID roachpb.RangeID ,
164
- replicaID roachpb.ReplicaID ,
165
- creatingReplica * roachpb.ReplicaDescriptor ,
152
+ ctx context.Context , id storage.FullReplicaID , creatingReplica * roachpb.ReplicaDescriptor ,
166
153
) (_ * Replica , created bool , _ error ) {
167
154
// The common case: look up an existing replica.
168
- if repl , err := s .tryGetReplica (ctx , rangeID , replicaID , creatingReplica ); err != nil {
155
+ if repl , err := s .tryGetReplica (ctx , id , creatingReplica ); err != nil {
169
156
return nil , false , err
170
157
} else if repl != nil {
171
158
return repl , false , nil
@@ -175,24 +162,24 @@ func (s *Store) tryGetOrCreateReplica(
175
162
// be racing at this point, so grab a "lock" over this rangeID (represented by
176
163
// s.mu.creatingReplicas[rangeID]) by one goroutine, and retry others.
177
164
s .mu .Lock ()
178
- if _ , ok := s .mu .creatingReplicas [rangeID ]; ok {
165
+ if _ , ok := s .mu .creatingReplicas [id . RangeID ]; ok {
179
166
// Lost the race - another goroutine is currently creating that replica. Let
180
167
// the caller retry so that they can eventually see it.
181
168
s .mu .Unlock ()
182
169
return nil , false , errRetry
183
170
}
184
- s .mu .creatingReplicas [rangeID ] = struct {}{}
171
+ s .mu .creatingReplicas [id . RangeID ] = struct {}{}
185
172
s .mu .Unlock ()
186
173
defer func () {
187
174
s .mu .Lock ()
188
- delete (s .mu .creatingReplicas , rangeID )
175
+ delete (s .mu .creatingReplicas , id . RangeID )
189
176
s .mu .Unlock ()
190
177
}()
191
178
// Now we are the only goroutine trying to create a replica for this rangeID.
192
179
193
180
// Repeat the quick path in case someone has overtaken us while we were
194
181
// grabbing the "lock".
195
- if repl , err := s .tryGetReplica (ctx , rangeID , replicaID , creatingReplica ); err != nil {
182
+ if repl , err := s .tryGetReplica (ctx , id , creatingReplica ); err != nil {
196
183
return nil , false , err
197
184
} else if repl != nil {
198
185
return repl , false , nil
@@ -204,16 +191,16 @@ func (s *Store) tryGetOrCreateReplica(
204
191
// Replica for this rangeID, and that's us.
205
192
206
193
_ = kvstorage .CreateUninitReplicaTODO
194
+ // TODO(sep-raft-log): needs both engines due to tombstone (which lives on
195
+ // statemachine).
207
196
if err := kvstorage .CreateUninitializedReplica (
208
- // TODO(sep-raft-log): needs both engines due to tombstone (which lives on
209
- // statemachine).
210
- ctx , s .TODOEngine (), s .StoreID (), rangeID , replicaID ,
197
+ ctx , s .TODOEngine (), s .StoreID (), id ,
211
198
); err != nil {
212
199
return nil , false , err
213
200
}
214
201
215
202
// Create a new uninitialized replica and lock it for raft processing.
216
- repl , err := newUninitializedReplica (s , rangeID , replicaID )
203
+ repl , err := newUninitializedReplica (s , id )
217
204
if err != nil {
218
205
return nil , false , err
219
206
}
0 commit comments