-
Notifications
You must be signed in to change notification settings - Fork 2
/
api.go
449 lines (333 loc) · 10 KB
/
api.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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package vshard_router
import (
"context"
"fmt"
"sync/atomic"
"time"
"github.com/google/uuid"
"github.com/mitchellh/mapstructure"
"golang.org/x/sync/errgroup"
"github.com/tarantool/go-tarantool/v2"
"github.com/tarantool/go-tarantool/v2/pool"
)
// --------------------------------------------------------------------------------
// -- API
// --------------------------------------------------------------------------------
type VshardMode string
const (
ReadMode VshardMode = "read"
WriteMode VshardMode = "write"
)
func (c VshardMode) String() string {
return string(c)
}
type StorageCallAssertError struct {
Code int `msgpack:"code"`
BaseType string `msgpack:"base_type"`
Type string `msgpack:"type"`
Message string `msgpack:"message"`
Trace interface{} `msgpack:"trace"`
}
func (s StorageCallAssertError) Error() string {
return fmt.Sprintf("vshard.storage.call assert error code: %d, type:%s, message: %s", s.Code, s.Type, s.Message)
}
type StorageCallVShardError struct {
BucketID uint64 `msgpack:"bucket_id" mapstructure:"bucket_id"`
Reason string `msgpack:"reason"`
Code int `msgpack:"code"`
Type string `msgpack:"type"`
Message string `msgpack:"message"`
Name string `msgpack:"name"`
MasterUUID *string `msgpack:"master_uuid" mapstructure:"master_uuid"` // mapstructure cant decode to source uuid type
ReplicasetUUID *string `msgpack:"replicaset_uuid" mapstructure:"replicaset_uuid"` // mapstructure cant decode to source uuid type
}
func (s StorageCallVShardError) Error() string {
return fmt.Sprintf("vshard.storage.call bucket error bucket_id: %d, reason: %s, name: %s", s.BucketID, s.Reason, s.Name)
}
type StorageResultTypedFunc = func(result interface{}) error
type CallOpts struct {
VshardMode VshardMode // vshard mode in call
PoolMode pool.Mode
Timeout time.Duration
}
const CallTimeoutMin = time.Second / 2
// RouterCallImpl Perform shard operation function will restart operation
// after wrong bucket response until timeout is reached
func (r *Router) RouterCallImpl(ctx context.Context,
bucketID uint64,
opts CallOpts,
fnc string,
args interface{}) (interface{}, StorageResultTypedFunc, error) {
if bucketID > r.cfg.TotalBucketCount {
return nil, nil, fmt.Errorf("bucket is unreachable: bucket id is out of range")
}
if opts.Timeout == 0 {
opts.Timeout = CallTimeoutMin
}
timeout := opts.Timeout
timeStart := time.Now()
req := tarantool.NewCallRequest("vshard.storage.call")
req = req.Context(ctx)
req = req.Args([]interface{}{
bucketID,
opts.VshardMode.String(),
fnc,
args,
})
var err error
for {
if since := time.Since(timeStart); since > timeout {
r.metrics().RequestDuration(since, false, false)
r.log().Debug(ctx, fmt.Sprintf("return result on timeout; since %s of timeout %s", since, timeout))
if err == nil {
err = fmt.Errorf("cant get call cause call impl timeout")
}
return nil, nil, err
}
var rs *Replicaset
rs, err = r.BucketResolve(ctx, bucketID)
if err != nil {
r.log().Debug(ctx, fmt.Sprintf("cant resolve bucket %d with error: %s", bucketID, err.Error()))
r.metrics().RetryOnCall("bucket_resolve_error")
continue
}
r.log().Info(ctx, fmt.Sprintf("try call replicaset %s", rs.info.Name))
future := rs.conn.Do(req, opts.PoolMode)
respData, err := future.Get()
if err != nil {
r.log().Error(ctx, fmt.Sprintf("got future error: %s", err))
r.metrics().RetryOnCall("future_get_error")
continue
}
if len(respData) < 1 {
err = fmt.Errorf("invalid length of response data: must be >1, current: %d", len(respData))
r.log().Error(ctx, err.Error())
r.metrics().RetryOnCall("resp_data_error")
continue
}
if respData[0] == nil {
vshardErr := &StorageCallVShardError{}
err = mapstructure.Decode(respData[1], vshardErr)
if err != nil {
r.metrics().RetryOnCall("internal_error")
r.log().Error(ctx, fmt.Sprintf("cant decode vhsard err by trarantool with err: %s; continue try", err))
continue
}
err = vshardErr
r.log().Error(ctx, fmt.Sprintf("got vshard storage call error: %s", err))
if vshardErr.Name == "WRONG_BUCKET" ||
vshardErr.Name == "BUCKET_IS_LOCKED" ||
vshardErr.Name == "TRANSFER_IS_IN_PROGRESS" {
r.BucketReset(bucketID)
r.metrics().RetryOnCall("bucket_migrate")
continue
}
continue
}
isVShardRespOk := false
err = future.GetTyped(&[]interface{}{&isVShardRespOk})
if err != nil {
r.log().Debug(ctx, fmt.Sprintf("cant get typed with err: %s", err))
continue
}
if !isVShardRespOk { // error
errorResp := &StorageCallAssertError{}
err = future.GetTyped(&[]interface{}{&isVShardRespOk, errorResp})
if err != nil {
err = fmt.Errorf("cant get typed vshard err with err: %s", err)
}
err = errorResp
}
r.metrics().RequestDuration(time.Since(timeStart), true, false)
r.log().Debug(ctx, fmt.Sprintf("got call result response data %s", respData))
return respData[1:], func(result interface{}) error {
var stub interface{}
return future.GetTyped(&[]interface{}{&stub, result})
}, nil
}
}
// call function "storage_unref" if map_callrw is failed or successed
func (r *Router) callStorageUnref(refID int64) {
req := tarantool.NewCallRequest("vshard.storage._call")
req = req.Args([]interface{}{"storage_unref", refID})
for _, replicaset := range r.idToReplicaset {
conn := replicaset.conn
future := conn.Do(req, pool.RW)
future.SetError(nil)
}
}
type replicasetFuture struct {
id uuid.UUID
future *tarantool.Future
}
// RouterMapCallRWImpl perform call function on all masters in the cluster
// with a guarantee that in case of success it was executed with all
// buckets being accessible for reads and writes.
func (r *Router) RouterMapCallRWImpl(
ctx context.Context,
fnc string,
args interface{},
opts CallOpts,
) (map[uuid.UUID]interface{}, error) {
if opts.Timeout == 0 {
opts.Timeout = CallTimeoutMin
}
timeout := opts.Timeout
timeStart := time.Now()
refID := r.refID.Load()
r.refID.Add(1)
defer r.callStorageUnref(refID)
mapCallCtx, cancel := context.WithTimeout(ctx, timeout)
req := tarantool.NewCallRequest("vshard.storage._call")
req = req.Context(ctx)
// ref stage: send
req = req.Args([]interface{}{
"storage_ref",
refID,
timeout,
})
g, gctx := errgroup.WithContext(mapCallCtx)
rsFutures := make(chan replicasetFuture)
g.Go(func() error {
defer close(rsFutures)
for id, replicaset := range r.idToReplicaset {
conn := replicaset.conn
future := conn.Do(req, pool.RW)
if _, err := future.Get(); err != nil {
cancel()
return fmt.Errorf("rs {%s} storage_ref err: %s", id.String(), err.Error())
}
select {
case <-gctx.Done():
return gctx.Err()
case rsFutures <- replicasetFuture{
id: id,
future: future,
}:
}
}
return nil
})
// ref stage collect
totalBucketCount := int32(0)
for i := 0; i < int(r.nWorkers); i++ {
g.Go(func() error {
for rsFuture := range rsFutures {
future := rsFuture.future
respData, err := future.Get()
if err != nil {
cancel()
return err
}
if respData[0] == nil {
vshardErr := &StorageCallAssertError{}
err = mapstructure.Decode(respData[1], vshardErr)
if err != nil {
cancel()
return err
}
cancel()
return vshardErr
}
var bucketCount uint16
err = future.GetTyped(&[]interface{}{&bucketCount})
if err != nil {
cancel()
return err
}
atomic.AddInt32(&totalBucketCount, int32(bucketCount))
}
return nil
})
}
if err := g.Wait(); err != nil {
return nil, err
}
if totalBucketCount != r.knownBucketCount.Load() {
return nil, fmt.Errorf("unknown bucket counts %d", totalBucketCount)
}
// map stage: send
g, gctx = errgroup.WithContext(mapCallCtx)
rsFutures = make(chan replicasetFuture)
req = req.Args([]interface{}{"storage_map", refID, fnc, args})
g.Go(func() error {
defer close(rsFutures)
for id, replicaset := range r.idToReplicaset {
conn := replicaset.conn
future := conn.Do(req, pool.RW)
if _, err := future.Get(); err != nil {
cancel()
return fmt.Errorf("rs {%s} storage_map err: %s", id.String(), err.Error())
}
select {
case <-gctx.Done():
return gctx.Err()
case rsFutures <- replicasetFuture{
id: id,
future: future,
}:
}
}
return nil
})
// map stage: collect
idToResult := make(map[uuid.UUID]interface{})
for i := 0; i < int(r.nWorkers); i++ {
g.Go(func() error {
for rsFuture := range rsFutures {
future := rsFuture.future
respData, err := future.Get()
if err != nil {
cancel()
return err
}
if len(respData) != 2 {
err = fmt.Errorf("invalid length of response data: must be = 2, current: %d", len(respData))
cancel()
return err
}
if respData[0] == nil {
vshardErr := &StorageCallAssertError{}
err = mapstructure.Decode(respData[1], vshardErr)
if err != nil {
cancel()
return err
}
cancel()
return vshardErr
}
isVShardRespOk := false
err = future.GetTyped(&[]interface{}{&isVShardRespOk})
if err != nil {
cancel()
return err
}
if !isVShardRespOk { // error
errorResp := &StorageCallAssertError{}
err = future.GetTyped(&[]interface{}{&isVShardRespOk, errorResp})
if err != nil {
err = fmt.Errorf("cant get typed vshard err with err: %s", err)
}
cancel()
return err
}
idToResult[rsFuture.id] = respData[1]
}
return nil
})
}
if err := g.Wait(); err != nil {
return nil, err
}
r.metrics().RequestDuration(time.Since(timeStart), true, true)
return idToResult, nil
}
// RouterRoute get replicaset object by bucket identifier.
// alias to BucketResolve
func (r *Router) RouterRoute(ctx context.Context, bucketID uint64) (*Replicaset, error) {
return r.BucketResolve(ctx, bucketID)
}
// RouterRouteAll return map of all replicasets.
func (r *Router) RouterRouteAll() map[uuid.UUID]*Replicaset {
return r.idToReplicaset
}