-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcore.go
497 lines (454 loc) · 12.3 KB
/
core.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
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
package btrdb
import (
"context"
"fmt"
"strings"
"sync"
"sync/atomic"
"time"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
pb "github.com/BTrDB/btrdb/v5/v5api"
logging "github.com/op/go-logging"
"github.com/pborman/uuid"
)
var lg *logging.Logger
func init() {
lg = logging.MustGetLogger("log")
}
//ErrorDisconnected is returned when operations are attempted after Disconnect()
//is called.
var ErrorDisconnected = &CodedError{&pb.Status{Code: 421, Msg: "Driver is disconnected"}}
//ErrorClusterDegraded is returned when a write operation on an unmapped UUID is attempted.
//generally the same operation will succeed if attempted once the cluster has recovered.
var ErrorClusterDegraded = &CodedError{&pb.Status{Code: 419, Msg: "Cluster is degraded"}}
//ErrorWrongArgs is returned from API functions if the parameters are nonsensical
var ErrorWrongArgs = &CodedError{&pb.Status{Code: 421, Msg: "Invalid Arguments"}}
//ErrorNoSuchStream is returned if an operation is attempted on a stream when
//it does not exist.
//var ErrorNoSuchStream = &CodedError{&pb.Status{Code: 404, Msg: "No such stream"}}
//BTrDB is the main object you should use to interact with BTrDB.
type BTrDB struct {
//This covers the mash
mashwmu sync.Mutex
activeMash atomic.Value
isproxied bool
closed bool
//This covers the epcache
epmu sync.RWMutex
epcache map[uint32]*Endpoint
bootstraps []string
apikey string
//Must hold this when evaluating if an endpoint has failed and
//requires a resync
resyncMu sync.Mutex
//Incremented every time there is a resync
numResyncs int64
}
func newBTrDB() *BTrDB {
return &BTrDB{epcache: make(map[uint32]*Endpoint)}
}
//StatPoint represents a statistical summary of a window. The length of that
//window must be determined from context (e.g the parameters passed to AlignedWindow or Window methods)
type StatPoint struct {
//The time of the start of the window, in nanoseconds since the epoch UTC
Time int64
Min float64
Mean float64
Max float64
Count uint64
StdDev float64
}
//Connect takes a list of endpoints and returns a BTrDB handle.
//Note that only a single endpoint is technically required, but having
//more endpoints will make the initial connection more robust to cluster
//changes. Different addresses for the same endpoint are permitted
func Connect(ctx context.Context, endpoints ...string) (*BTrDB, error) {
return ConnectAuth(ctx, "", endpoints...)
}
//ConnectAuth takes an API key and a list of endpoints and returns a BTrDB handle.
//Note that only a single endpoint is technically required, but having
//more endpoints will make the initial connection more robust to cluster
//changes. Different addresses for the same endpoint are permitted
func ConnectAuth(ctx context.Context, apikey string, endpoints ...string) (*BTrDB, error) {
if len(endpoints) == 0 {
return nil, fmt.Errorf("No endpoints provided")
}
b := newBTrDB()
b.apikey = apikey
b.bootstraps = endpoints
for _, epa := range endpoints {
ep, err := ConnectEndpointAuth(ctx, apikey, epa)
if ctx.Err() != nil {
return nil, ctx.Err()
}
if err != nil {
lg.Warningf("attempt to connect to %s yielded %v", epa, err)
continue
}
mash, inf, err := ep.Info(ctx)
if err != nil {
lg.Warningf("attempt to obtain MASH from %s yielded %v", epa, err)
ep.Disconnect()
continue
}
if inf.GetProxy() != nil {
//This is a proxied BTrDB cluster
b.isproxied = true
} else {
b.activeMash.Store(mash)
}
ep.Disconnect()
break
}
if !b.isproxied && b.activeMash.Load() == nil {
return nil, fmt.Errorf("Could not connect to cluster via provided endpoints")
}
return b, nil
}
//Disconnect will close all active connections to the cluster. All future calls
//will return ErrorDisconnected
func (b *BTrDB) Disconnect() error {
b.epmu.Lock()
defer b.epmu.Unlock()
var gerr error
for _, ep := range b.epcache {
err := ep.Disconnect()
if err != nil {
gerr = err
}
}
b.closed = true
return gerr
}
//EndpointForHash is a low level function that returns a single endpoint for an
//endpoint hash.
func (b *BTrDB) EndpointForHash(ctx context.Context, hash uint32) (*Endpoint, error) {
if ctx.Err() != nil {
return nil, ctx.Err()
}
if b.isproxied {
return b.EndpointFor(ctx, nil)
}
m := b.activeMash.Load().(*MASH)
b.epmu.Lock()
ep, ok := b.epcache[hash]
if ok {
b.epmu.Unlock()
return ep, nil
}
var addrs []string
for _, ep := range m.eps {
if ep.hash == hash {
addrs = ep.grpc
}
}
//We need to connect to endpoint
nep, err := ConnectEndpointAuth(ctx, b.apikey, addrs...)
if err != nil {
b.epmu.Unlock()
return nil, err
}
b.epcache[hash] = nep
b.epmu.Unlock()
return nep, nil
}
//ReadEndpointFor returns the endpoint that should be used to read the given uuid
func (b *BTrDB) ReadEndpointFor(ctx context.Context, uuid uuid.UUID) (*Endpoint, error) {
//TODO do rpref
return b.EndpointFor(ctx, uuid)
}
//EndpointFor returns the endpoint that should be used to write the given uuid
func (b *BTrDB) EndpointFor(ctx context.Context, uuid uuid.UUID) (*Endpoint, error) {
if ctx.Err() != nil {
return nil, ctx.Err()
}
if b.isproxied {
b.epmu.Lock()
defer b.epmu.Unlock()
for _, ep := range b.epcache {
return ep, nil
}
//We need to connect to endpoint
nep, err := ConnectEndpointAuth(ctx, b.apikey, b.bootstraps...)
if err != nil {
return nil, err
}
b.epcache[0] = nep
return nep, nil
}
m := b.activeMash.Load().(*MASH)
ok, hash, addrs := m.EndpointFor(uuid)
if !ok {
b.ResyncMash()
return nil, ErrorClusterDegraded
}
b.epmu.Lock()
ep, ok := b.epcache[hash]
if ok {
b.epmu.Unlock()
return ep, nil
}
//We need to connect to endpoint
nep, err := ConnectEndpointAuth(ctx, b.apikey, addrs...) //XX
if err != nil {
b.epmu.Unlock()
return nil, err
}
b.epcache[hash] = nep
b.epmu.Unlock()
return nep, nil
}
func (b *BTrDB) dropEpcache() {
for _, e := range b.epcache {
e.Disconnect()
}
b.epcache = make(map[uint32]*Endpoint)
}
func (b *BTrDB) GetAnyEndpoint(ctx context.Context) (*Endpoint, error) {
b.epmu.RLock()
for _, ep := range b.epcache {
b.epmu.RUnlock()
return ep, nil
}
b.epmu.RUnlock()
//Nothing in cache
return b.EndpointFor(ctx, uuid.NewRandom())
}
func (b *BTrDB) ResyncMash() {
if b.isproxied {
b.resyncProxied()
} else {
b.resyncInternalMash()
}
}
func (b *BTrDB) resyncProxied() {
b.epmu.Lock()
defer b.epmu.Unlock()
b.dropEpcache()
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
ep, err := ConnectEndpointAuth(ctx, b.apikey, b.bootstraps...)
cancel()
if err != nil {
return
}
b.epcache[0] = ep
return
}
func (b *BTrDB) resyncInternalMash() {
b.epmu.Lock()
for _, ep := range b.epcache {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
mash, _, err := ep.Info(ctx)
cancel()
if err == nil {
b.activeMash.Store(mash)
b.dropEpcache()
b.epmu.Unlock()
return
} else {
lg.Warningf("attempt to obtain info from endpoint cache error: %v", err)
}
}
//Try bootstraps
for _, epa := range b.bootstraps {
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
ep, err := ConnectEndpointAuth(ctx, b.apikey, epa)
cancel()
if err != nil {
lg.Warningf("attempt to connect to %s yielded %v", epa, err)
continue
}
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Second)
mash, _, err := ep.Info(ctx)
cancel()
if err != nil {
ep.Disconnect()
lg.Warningf("attempt to obtain MASH from %s yielded %v", epa, err)
continue
}
b.activeMash.Store(mash)
b.dropEpcache()
ep.Disconnect()
b.epmu.Unlock()
return
}
b.epmu.Unlock()
//Try clients already in the MASH
cm := b.activeMash.Load().(*MASH)
for _, mbr := range cm.Members {
if !mbr.In {
continue
}
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
ep, err := b.EndpointForHash(ctx, mbr.Hash)
if err != nil {
lg.Warningf("obtaining endpoint for hash error: %v", err)
cancel()
continue
}
mash, _, err := ep.Info(ctx)
cancel()
if err == nil {
b.activeMash.Store(mash)
b.dropEpcache()
return
} else {
lg.Warningf("obtaining endpoint info error: %v", err)
}
}
lg.Warningf("failed to resync MASH, is BTrDB unavailable?")
}
//This returns true if you should redo your operation (and get new ep)
//and false if you should return the last value/error you got
func (b *BTrDB) TestEpError(ep *Endpoint, err error) bool {
startNumResyncs := atomic.LoadInt64(&b.numResyncs)
if ep == nil && err == nil {
return true
}
if err == forceEp {
return true
}
if err == nil {
return false
}
mustResync := false
if strings.Contains(err.Error(), "getsockopt: connection refused") {
mustResync = true
//why grpc no use proper code :(
lg.Warningf("Got conn refused, resyncing silently")
} else if strings.Contains(err.Error(), "Endpoint is unreachable on all addresses") {
mustResync = true
lg.Warningf("Got conn refused, resyncing silently")
} else if grpc.Code(err) == codes.Unavailable {
mustResync = true
lg.Warningf("Got unavailable, resyncing mash silently")
} else {
ce := ToCodedError(err)
if ce.Code == 405 {
mustResync = true
lg.Warningf("Got 405 (wrong endpoint) silently retrying")
}
if ce.Code == 419 {
mustResync = true
lg.Warningf("Got 419 (cluster degraded) silently retrying")
}
}
if mustResync {
//This is to avoid tight resync loops
time.Sleep(300 * time.Millisecond)
b.resyncMu.Lock()
//No other goroutine has done a resync since we started this testEpError
if b.numResyncs == startNumResyncs {
b.ResyncMash()
b.numResyncs++
}
b.resyncMu.Unlock()
return true
}
return false
}
//This should invalidate the endpoint if some kind of error occurs.
//Because some values may have already been delivered, async functions using
//snoopEpErr will not be able to mask cluster errors from the user
func (b *BTrDB) SnoopEpErr(ep *Endpoint, err chan error) chan error {
rv := make(chan error, 2)
go func() {
for e := range err {
//if e is special invalidate ep
b.TestEpError(ep, e)
rv <- e
}
close(rv)
}()
return rv
}
//Subscriptions represent a set of
//real time subscriptions to streaming data.
type Subscriptions struct {
err chan error
id []uuid.UUID
c chan SubRecord
}
//Data for a single stream returned from a Subscription.
type SubRecord struct {
ID uuid.UUID
Val []RawPoint
}
//An endpoint and its associated uuids.
type EPGroup struct {
*Endpoint
ID []uuid.UUID
}
//EndpointSplit takes a variadic list of uuids and organizes them by the endpoint
//responsible for them.
func (b *BTrDB) EndpointsSplit(ctx context.Context, id ...uuid.UUID) ([]EPGroup, error) {
var err error
var ep *Endpoint
who := make(map[string][]uuid.UUID)
for b.TestEpError(ep, err) {
ep, err = b.GetAnyEndpoint(ctx)
if err != nil {
continue
}
if b.isproxied {
who["proxy"] = id
} else {
m := b.activeMash.Load().(*MASH)
for i := range id {
_, _, addrs := m.EndpointFor(id[i])
if addrs == nil || len(addrs) != 1 {
return nil, fmt.Errorf("can't find endpoint for %s", id[i])
}
who[addrs[0]] = append(who[addrs[0]], id[i])
}
}
}
var out []EPGroup
i := 0
for _, v := range who {
ep, err := b.EndpointFor(ctx, v[0])
for b.TestEpError(ep, err) {
ep, err = b.EndpointFor(ctx, v[0])
}
if err != nil {
return nil, err
}
out = append(out, EPGroup{ep, v})
i++
}
return out, nil
}
//Subscribe takes a list of stream UUIDs to receive real time data points from.
//Connections are made to each relevant endpoints, and streams that belong to the
//same endpoint use the same connection. Data points are "raw", meaning they
//are given in the exact same sequence the database received them from the client.
func (b *BTrDB) Subscribe(ctx context.Context, id ...uuid.UUID) (*Subscriptions, error) {
if len(id) == 0 {
return nil, fmt.Errorf("no ids provided")
}
subs := &Subscriptions{
id: id,
err: make(chan error),
c: make(chan SubRecord),
}
eps, err := b.EndpointsSplit(ctx, id...)
if err != nil {
return nil, err
}
for _, ep := range eps {
ep.Endpoint.SubscribeTo(ctx, ep.ID, subs.c, subs.err)
}
return subs, nil
}
//Next gives either the most recent data for the set of subscriptions
//or an error regarding the connection state.
func (subs *Subscriptions) Next(ctx context.Context) (sr SubRecord, err error) {
select {
case <-ctx.Done():
err = ctx.Err()
case err = <-subs.err:
case sr = <-subs.c:
}
return
}