-
Notifications
You must be signed in to change notification settings - Fork 3
/
discovery.go
326 lines (263 loc) · 10.4 KB
/
discovery.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
package vshard_router //nolint:revive
import (
"context"
"errors"
"fmt"
"runtime/debug"
"time"
"golang.org/x/sync/errgroup"
"github.com/google/uuid"
"github.com/tarantool/go-tarantool/v2"
)
// --------------------------------------------------------------------------------
// -- Discovery
// --------------------------------------------------------------------------------
type DiscoveryMode int
const (
// DiscoveryModeOn is cron discovery with cron timeout
DiscoveryModeOn DiscoveryMode = iota
DiscoveryModeOnce
)
// BucketsSearchMode a type, that used to define policy for BucketDiscovery method.
// See type Config for further details.
type BucketsSearchMode int
const (
// BucketsSearchLegacy implements the same logic as lua router:
// send bucket_stat request to every replicaset,
// return a response immediately if any of them succeed.
BucketsSearchLegacy BucketsSearchMode = iota
// BucketsSearchBatchedQuick and BucketsSearchBatchedFull implement another logic:
// send buckets_discovery request to every replicaset with from=bucketID,
// seek our bucketID in their responses.
// Additionally, store other bucketIDs in the route map.
// BucketsSearchBatchedQuick stops iterating over replicasets responses as soon as our bucketID is found.
BucketsSearchBatchedQuick
// BucketsSearchBatchedFull implements the same logic as BucketsSearchBatchedQuick,
// but doesn't stop iterating over replicasets responses as soon as our bucketID is found.
// Instead, it always iterates over all replicasets responses even bucketID is found.
BucketsSearchBatchedFull
)
// BucketDiscovery search bucket in whole cluster
func (r *Router) BucketDiscovery(ctx context.Context, bucketID uint64) (*Replicaset, error) {
if bucketID < 1 || r.cfg.TotalBucketCount < bucketID {
return nil, fmt.Errorf("bucket id is out of range: %d (total %d)", bucketID, r.cfg.TotalBucketCount)
}
view := r.getConsistentView()
rs := view.routeMap[bucketID].Load()
if rs != nil {
return rs, nil
}
// it`s ok if in the same time we have few active searches
r.log().Infof(ctx, "Discovering bucket %d", bucketID)
if r.cfg.BucketsSearchMode == BucketsSearchLegacy {
return r.bucketSearchLegacy(ctx, bucketID)
}
return r.bucketSearchBatched(ctx, bucketID)
}
func (r *Router) bucketSearchLegacy(ctx context.Context, bucketID uint64) (*Replicaset, error) {
idToReplicasetRef := r.getIDToReplicaset()
type rsFuture struct {
rsID uuid.UUID
future *tarantool.Future
}
var rsFutures = make([]rsFuture, 0, len(idToReplicasetRef))
// Send a bunch of parallel requests
for rsID, rs := range idToReplicasetRef {
rsFutures = append(rsFutures, rsFuture{
rsID: rsID,
future: rs.bucketStatAsync(ctx, bucketID),
})
}
for _, rsFuture := range rsFutures {
if _, err := bucketStatWait(rsFuture.future); err != nil {
var bsError bucketStatError
if !errors.As(err, &bsError) {
r.log().Errorf(ctx, "bucketSearchLegacy: bucketStatWait call error for %v: %v", rsFuture.rsID, err)
}
// just skip, bucket may not belong to this replicaset
continue
}
// It's ok if several replicasets return ok to bucket_stat command for the same bucketID, just pick any of them.
rs, err := r.BucketSet(bucketID, rsFuture.rsID)
if err != nil {
r.log().Errorf(ctx, "bucketSearchLegacy: can't set rsID %v for bucketID %d: %v", rsFuture.rsID, bucketID, err)
return nil, newVShardErrorNoRouteToBucket(bucketID)
}
// TODO: should we release resources for unhandled futures?
return rs, nil
}
/*
-- All replicasets were scanned, but a bucket was not
-- found anywhere, so most likely it does not exist. It
-- can be wrong, if rebalancing is in progress, and a
-- bucket was found to be RECEIVING on one replicaset, and
-- was not found on other replicasets (it was sent during
-- discovery).
*/
return nil, newVShardErrorNoRouteToBucket(bucketID)
}
// The approach in bucketSearchLegacy is very ineffective because
// we use N vshard.storage.bucket_stat requests over the network
// to find out the location of a single bucket. So, we use here vshard.storage.buckets_discovery request instead.
// As a result, we find the location for N * 1000 buckets while paying almost the same cost (CPU, network).
// P.S. 1000 is a batch size in response of buckets_discovery, see:
// https://github.com/tarantool/vshard/blob/dfa2cc8a2aff221d5f421298851a9a229b2e0434/vshard/storage/init.lua#L1700
// https://github.com/tarantool/vshard/blob/dfa2cc8a2aff221d5f421298851a9a229b2e0434/vshard/consts.lua#L37
func (r *Router) bucketSearchBatched(ctx context.Context, bucketIDToFind uint64) (*Replicaset, error) {
idToReplicasetRef := r.getIDToReplicaset()
view := r.getConsistentView()
type rsFuture struct {
rs *Replicaset
rsID uuid.UUID
future *tarantool.Future
}
var rsFutures = make([]rsFuture, 0, len(idToReplicasetRef))
// Send a bunch of parallel requests
for rsID, rs := range idToReplicasetRef {
rsFutures = append(rsFutures, rsFuture{
rs: rs,
rsID: rsID,
future: rs.bucketsDiscoveryAsync(ctx, bucketIDToFind),
})
}
var rs *Replicaset
for _, rsFuture := range rsFutures {
resp, err := bucketsDiscoveryWait(rsFuture.future)
if err != nil {
r.log().Errorf(ctx, "bucketSearchBatched: bucketsDiscoveryWait error for %v: %v", rsFuture.rsID, err)
// just skip, we still may find our bucket in another replicaset
continue
}
for _, bucketID := range resp.Buckets {
if bucketID == bucketIDToFind {
// We found where bucketIDToFind is located
rs = rsFuture.rs
}
if old := view.routeMap[bucketID].Swap(rs); old == nil {
view.knownBucketCount.Add(1)
}
}
if bucketIDWasFound := rs != nil; !bucketIDWasFound {
continue
}
if r.cfg.BucketsSearchMode == BucketsSearchBatchedQuick {
return rs, nil
}
}
if rs == nil {
return nil, newVShardErrorNoRouteToBucket(bucketIDToFind)
}
return rs, nil
}
// BucketResolve resolve bucket id to replicaset
func (r *Router) BucketResolve(ctx context.Context, bucketID uint64) (*Replicaset, error) {
return r.BucketDiscovery(ctx, bucketID)
}
// DiscoveryHandleBuckets arrange downloaded buckets to the route map so as they reference a given replicaset.
func (r *Router) DiscoveryHandleBuckets(ctx context.Context, rs *Replicaset, buckets []uint64) {
view := r.getConsistentView()
removedFrom := make(map[*Replicaset]int)
for _, bucketID := range buckets {
oldRs := view.routeMap[bucketID].Swap(rs)
if oldRs == rs {
continue
}
if oldRs == nil {
view.knownBucketCount.Add(1)
}
// We don't check oldRs for nil here, because it's a valid key too (if rs == nil, it means removed from unknown buckets set)
removedFrom[oldRs]++
}
var addedToRs int
for rs, removedFromRs := range removedFrom {
addedToRs += removedFromRs
switch rs {
case nil:
r.log().Debugf(ctx, "Added new %d buckets to the cluster map", removedFromRs)
default:
r.log().Debugf(ctx, "Removed %d buckets from replicaset %s", removedFromRs, rs.info.Name)
}
}
r.log().Infof(ctx, "Added %d buckets to replicaset %s", addedToRs, rs.info.Name)
}
func (r *Router) DiscoveryAllBuckets(ctx context.Context) error {
t := time.Now()
r.log().Infof(ctx, "Start discovery all buckets")
errGr, ctx := errgroup.WithContext(ctx)
view := r.getConsistentView()
idToReplicasetRef := r.getIDToReplicaset()
for _, rs := range idToReplicasetRef {
rs := rs
errGr.Go(func() error {
var bucketsDiscoveryPaginationFrom uint64
for {
resp, err := rs.bucketsDiscovery(ctx, bucketsDiscoveryPaginationFrom)
if err != nil {
return err
}
for _, bucketID := range resp.Buckets {
// We could check here that bucketID is in range [1, TotalBucketCnt], but it seems to be redundant.
if old := view.routeMap[bucketID].Swap(rs); old == nil {
view.knownBucketCount.Add(1)
}
}
// There are no more buckets
// https://github.com/tarantool/vshard/blob/8d299bfe/vshard/storage/init.lua#L1730
// vshard.storage returns { buckets = [], next_from = nil } if there are no more buckets.
// Since next_from is always > 0. NextFrom = 0 means that we got next_from = nil, that has not been decoded.
if resp.NextFrom == 0 {
return nil
}
bucketsDiscoveryPaginationFrom = resp.NextFrom
// Don't spam many requests at once. Give storages time to handle them and other requests.
// https://github.com/tarantool/vshard/blob/b6fdbe950a2e4557f05b83bd8b846b126ec3724e/vshard/router/init.lua#L308
time.Sleep(r.cfg.DiscoveryWorkStep)
}
})
}
err := errGr.Wait()
if err != nil {
return fmt.Errorf("errGr.Wait() err: %w", err)
}
r.log().Infof(ctx, "Discovery done since: %s", time.Since(t))
return nil
}
// cronDiscovery is discovery_service_f analog with goroutines instead fibers
func (r *Router) cronDiscovery(ctx context.Context) {
var iterationCount uint64
for {
select {
case <-ctx.Done():
r.metrics().CronDiscoveryEvent(false, 0, "ctx-cancel")
r.log().Infof(ctx, "[DISCOVERY] cron discovery has been stopped after %d iterations", iterationCount)
return
case <-time.After(r.cfg.DiscoveryTimeout):
iterationCount++
}
// Since the current for loop should not stop until ctx->Done() event fires,
// we should be able to continue execution even a panic occures.
// Therefore, we should wrap everything into anonymous function that recovers after panic.
// (Similar to pcall in lua/tarantool)
func() {
defer func() {
if recovered := recover(); recovered != nil {
// Another one panic may happen due to log function below (e.g. bug in log().Errorf), in this case we have two options:
// 1. recover again and log nothing: panic will be muted and lost
// 2. don't try to recover, we hope that the second panic will be logged somehow by go runtime
// So, we choose the second behavior
r.log().Errorf(ctx, "[DISCOVERY] something unexpected has happened in cronDiscovery(%d): panic %v, stackstrace: %s",
iterationCount, recovered, string(debug.Stack()))
}
}()
r.log().Infof(ctx, "[DISCOVERY] started cron discovery iteration %d", iterationCount)
tStartDiscovery := time.Now()
if err := r.DiscoveryAllBuckets(ctx); err != nil {
r.metrics().CronDiscoveryEvent(false, time.Since(tStartDiscovery), "discovery-error")
r.log().Errorf(ctx, "[DISCOVERY] cant do cron discovery iteration %d with error: %s", iterationCount, err)
return
}
r.log().Infof(ctx, "[DISCOVERY] finished cron discovery iteration %d", iterationCount)
r.metrics().CronDiscoveryEvent(true, time.Since(tStartDiscovery), "ok")
}()
}
}