/
lbclient.go
445 lines (376 loc) · 11.8 KB
/
lbclient.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
package client
import (
"context"
"sync/atomic"
cid "github.com/ipfs/go-cid"
shell "github.com/ipfs/go-ipfs-api"
files "github.com/ipfs/go-ipfs-files"
"github.com/kebohan1/ipfs-cluster/api"
peer "github.com/libp2p/go-libp2p-core/peer"
)
// loadBalancingClient is a client to interact with IPFS Cluster APIs
// that balances the load by distributing requests among peers.
type loadBalancingClient struct {
strategy LBStrategy
retries int
}
// LBStrategy is a strategy to load balance requests among clients.
type LBStrategy interface {
Next(count int) Client
SetClients(clients []Client)
}
// RoundRobin is a load balancing strategy that would use clients in a sequence
// for all methods, throughout the lifetime of the lb client.
type RoundRobin struct {
clients []Client
counter uint32
length uint32
}
// Next return the next client to be used.
func (r *RoundRobin) Next(count int) Client {
i := atomic.AddUint32(&r.counter, 1) % r.length
return r.clients[i]
}
// SetClients sets a list of clients for this strategy.
func (r *RoundRobin) SetClients(cl []Client) {
r.clients = cl
r.length = uint32(len(cl))
}
// Failover is a load balancing strategy that would try the first cluster peer
// first. If the first call fails it would try other clients for that call in a
// round robin fashion.
type Failover struct {
clients []Client
}
// Next returns the next client to be used.
func (f *Failover) Next(count int) Client {
return f.clients[count%len(f.clients)]
}
// SetClients sets a list of clients for this strategy.
func (f *Failover) SetClients(cl []Client) {
f.clients = cl
}
// NewLBClient returns a new client that would load balance requests among
// clients.
func NewLBClient(strategy LBStrategy, cfgs []*Config, retries int) (Client, error) {
var clients []Client
for _, cfg := range cfgs {
defaultClient, err := NewDefaultClient(cfg)
if err != nil {
return nil, err
}
clients = append(clients, defaultClient)
}
strategy.SetClients(clients)
return &loadBalancingClient{strategy: strategy, retries: retries}, nil
}
// retry tries the request until it is successful or tries `lc.retries` times.
func (lc *loadBalancingClient) retry(count int, call func(Client) error) error {
logger.Debugf("retrying %d times", count+1)
err := call(lc.strategy.Next(count))
count++
// successful request
if err == nil {
return nil
}
// It is a safety check. This error should never occur.
// All errors returned by client methods are of type `api.Error`.
apiErr, ok := err.(*api.Error)
if !ok {
logger.Error("could not cast error into api.Error")
return err
}
if apiErr.Code != 0 {
return err
}
if count == lc.retries {
logger.Errorf("reached maximum number of retries without success, retries: %d", lc.retries)
return err
}
return lc.retry(count, call)
}
// ID returns information about the cluster Peer.
func (lc *loadBalancingClient) ID(ctx context.Context) (*api.ID, error) {
var id *api.ID
call := func(c Client) error {
var err error
id, err = c.ID(ctx)
return err
}
err := lc.retry(0, call)
return id, err
}
// Peers requests ID information for all cluster peers.
func (lc *loadBalancingClient) Peers(ctx context.Context) ([]*api.ID, error) {
var peers []*api.ID
call := func(c Client) error {
var err error
peers, err = c.Peers(ctx)
return err
}
err := lc.retry(0, call)
return peers, err
}
// PeerAdd adds a new peer to the cluster.
func (lc *loadBalancingClient) PeerAdd(ctx context.Context, pid peer.ID) (*api.ID, error) {
var id *api.ID
call := func(c Client) error {
var err error
id, err = c.PeerAdd(ctx, pid)
return err
}
err := lc.retry(0, call)
return id, err
}
// PeerRm removes a current peer from the cluster.
func (lc *loadBalancingClient) PeerRm(ctx context.Context, id peer.ID) error {
call := func(c Client) error {
return c.PeerRm(ctx, id)
}
return lc.retry(0, call)
}
// Pin tracks a Cid with the given replication factor and a name for
// human-friendliness.
func (lc *loadBalancingClient) Pin(ctx context.Context, ci cid.Cid, opts api.PinOptions) (*api.Pin, error) {
var pin *api.Pin
call := func(c Client) error {
var err error
pin, err = c.Pin(ctx, ci, opts)
return err
}
err := lc.retry(0, call)
return pin, err
}
// Unpin untracks a Cid from cluster.
func (lc *loadBalancingClient) Unpin(ctx context.Context, ci cid.Cid) (*api.Pin, error) {
var pin *api.Pin
call := func(c Client) error {
var err error
pin, err = c.Unpin(ctx, ci)
return err
}
err := lc.retry(0, call)
return pin, err
}
// PinPath allows to pin an element by the given IPFS path.
func (lc *loadBalancingClient) PinPath(ctx context.Context, path string, opts api.PinOptions) (*api.Pin, error) {
var pin *api.Pin
call := func(c Client) error {
var err error
pin, err = c.PinPath(ctx, path, opts)
return err
}
err := lc.retry(0, call)
return pin, err
}
// UnpinPath allows to unpin an item by providing its IPFS path.
// It returns the unpinned api.Pin information of the resolved Cid.
func (lc *loadBalancingClient) UnpinPath(ctx context.Context, p string) (*api.Pin, error) {
var pin *api.Pin
call := func(c Client) error {
var err error
pin, err = c.UnpinPath(ctx, p)
return err
}
err := lc.retry(0, call)
return pin, err
}
func (lc *loadBalancingClient) Sync(ctx context.Context, ci cid.Cid, local bool) (*api.GlobalPinInfo, error) {
var pinInfo *api.GlobalPinInfo
call := func(c Client) error {
var err error
pinInfo, err = c.Sync(ctx, ci, local)
return err
}
err := lc.retry(0, call)
return pinInfo, err
}
func (lc *loadBalancingClient) SyncAll(ctx context.Context, local bool) ([]*api.GlobalPinInfo, error) {
var pinInfos []*api.GlobalPinInfo
call := func(c Client) error {
var err error
pinInfos, err = c.SyncAll(ctx, local)
return err
}
err := lc.retry(0, call)
return pinInfos, err
}
// Allocations returns the consensus state listing all tracked items and
// the peers that should be pinning them.
func (lc *loadBalancingClient) Allocations(ctx context.Context, filter api.PinType) ([]*api.Pin, error) {
var pins []*api.Pin
call := func(c Client) error {
var err error
pins, err = c.Allocations(ctx, filter)
return err
}
err := lc.retry(0, call)
return pins, err
}
// Allocation returns the current allocations for a given Cid.
func (lc *loadBalancingClient) Allocation(ctx context.Context, ci cid.Cid) (*api.Pin, error) {
var pin *api.Pin
call := func(c Client) error {
var err error
pin, err = c.Allocation(ctx, ci)
return err
}
err := lc.retry(0, call)
return pin, err
}
// Status returns the current ipfs state for a given Cid. If local is true,
// the information affects only the current peer, otherwise the information
// is fetched from all cluster peers.
func (lc *loadBalancingClient) Status(ctx context.Context, ci cid.Cid, local bool) (*api.GlobalPinInfo, error) {
var pinInfo *api.GlobalPinInfo
call := func(c Client) error {
var err error
pinInfo, err = c.Status(ctx, ci, local)
return err
}
err := lc.retry(0, call)
return pinInfo, err
}
// StatusAll gathers Status() for all tracked items. If a filter is
// provided, only entries matching the given filter statuses
// will be returned. A filter can be built by merging TrackerStatuses with
// a bitwise OR operation (st1 | st2 | ...). A "0" filter value (or
// api.TrackerStatusUndefined), means all.
func (lc *loadBalancingClient) StatusAll(ctx context.Context, filter api.TrackerStatus, local bool) ([]*api.GlobalPinInfo, error) {
var pinInfos []*api.GlobalPinInfo
call := func(c Client) error {
var err error
pinInfos, err = c.StatusAll(ctx, filter, local)
return err
}
err := lc.retry(0, call)
return pinInfos, err
}
// Recover retriggers pin or unpin ipfs operations for a Cid in error state.
// If local is true, the operation is limited to the current peer, otherwise
// it happens on every cluster peer.
func (lc *loadBalancingClient) Recover(ctx context.Context, ci cid.Cid, local bool) (*api.GlobalPinInfo, error) {
var pinInfo *api.GlobalPinInfo
call := func(c Client) error {
var err error
pinInfo, err = c.Recover(ctx, ci, local)
return err
}
err := lc.retry(0, call)
return pinInfo, err
}
// RecoverAll triggers Recover() operations on all tracked items. If local is
// true, the operation is limited to the current peer. Otherwise, it happens
// everywhere.
func (lc *loadBalancingClient) RecoverAll(ctx context.Context, local bool) ([]*api.GlobalPinInfo, error) {
var pinInfos []*api.GlobalPinInfo
call := func(c Client) error {
var err error
pinInfos, err = c.RecoverAll(ctx, local)
return err
}
err := lc.retry(0, call)
return pinInfos, err
}
// Version returns the ipfs-cluster peer's version.
func (lc *loadBalancingClient) Version(ctx context.Context) (*api.Version, error) {
var v *api.Version
call := func(c Client) error {
var err error
v, err = c.Version(ctx)
return err
}
err := lc.retry(0, call)
return v, err
}
// GetConnectGraph returns an ipfs-cluster connection graph.
// The serialized version, strings instead of pids, is returned.
func (lc *loadBalancingClient) GetConnectGraph(ctx context.Context) (*api.ConnectGraph, error) {
var graph *api.ConnectGraph
call := func(c Client) error {
var err error
graph, err = c.GetConnectGraph(ctx)
return err
}
err := lc.retry(0, call)
return graph, err
}
// Metrics returns a map with the latest valid metrics of the given name
// for the current cluster peers.
func (lc *loadBalancingClient) Metrics(ctx context.Context, name string) ([]*api.Metric, error) {
var metrics []*api.Metric
call := func(c Client) error {
var err error
metrics, err = c.Metrics(ctx, name)
return err
}
err := lc.retry(0, call)
return metrics, err
}
// MetricNames returns the list of metric types.
func (lc *loadBalancingClient) MetricNames(ctx context.Context) ([]string, error) {
var metricNames []string
call := func(c Client) error {
var err error
metricNames, err = c.MetricNames(ctx)
return err
}
err := lc.retry(0, call)
return metricNames, err
}
// RepoGC runs garbage collection on IPFS daemons of cluster peers and
// returns collected CIDs. If local is true, it would garbage collect
// only on contacted peer, otherwise on all peers' IPFS daemons.
func (lc *loadBalancingClient) RepoGC(ctx context.Context, local bool) (*api.GlobalRepoGC, error) {
var repoGC *api.GlobalRepoGC
call := func(c Client) error {
var err error
repoGC, err = c.RepoGC(ctx, local)
return err
}
err := lc.retry(0, call)
return repoGC, err
}
// Add imports files to the cluster from the given paths. A path can
// either be a local filesystem location or an web url (http:// or https://).
// In the latter case, the destination will be downloaded with a GET request.
// The AddParams allow to control different options, like enabling the
// sharding the resulting DAG across the IPFS daemons of multiple cluster
// peers. The output channel will receive regular updates as the adding
// process progresses.
func (lc *loadBalancingClient) Add(
ctx context.Context,
paths []string,
params *api.AddParams,
out chan<- *api.AddedOutput,
) error {
call := func(c Client) error {
return c.Add(ctx, paths, params, out)
}
return lc.retry(0, call)
}
// AddMultiFile imports new files from a MultiFileReader. See Add().
func (lc *loadBalancingClient) AddMultiFile(
ctx context.Context,
multiFileR *files.MultiFileReader,
params *api.AddParams,
out chan<- *api.AddedOutput,
) error {
call := func(c Client) error {
return c.AddMultiFile(ctx, multiFileR, params, out)
}
return lc.retry(0, call)
}
// IPFS returns an instance of go-ipfs-api's Shell, pointing to the
// configured ProxyAddr (or to the default Cluster's IPFS proxy port).
// It re-uses this Client's HTTP client, thus will be constrained by
// the same configurations affecting it (timeouts...).
func (lc *loadBalancingClient) IPFS(ctx context.Context) *shell.Shell {
var s *shell.Shell
call := func(c Client) error {
s = c.IPFS(ctx)
return nil
}
lc.retry(0, call)
return s
}