-
Notifications
You must be signed in to change notification settings - Fork 451
/
replicated_session.go
423 lines (373 loc) · 12.8 KB
/
replicated_session.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
// Copyright (c) 2019 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package client
import (
"context"
"fmt"
"time"
"github.com/uber-go/tally"
"go.uber.org/zap"
"github.com/m3db/m3/src/dbnode/encoding"
"github.com/m3db/m3/src/dbnode/generated/thrift/rpc"
"github.com/m3db/m3/src/dbnode/namespace"
"github.com/m3db/m3/src/dbnode/storage/block"
"github.com/m3db/m3/src/dbnode/storage/bootstrap/result"
"github.com/m3db/m3/src/dbnode/storage/index"
"github.com/m3db/m3/src/dbnode/topology"
"github.com/m3db/m3/src/x/ident"
m3sync "github.com/m3db/m3/src/x/sync"
xtime "github.com/m3db/m3/src/x/time"
)
type newSessionFn func(Options) (clientSession, error)
// replicatedSession is an implementation of clientSession which replicates
// session read/writes to a set of clusters asynchronously.
type replicatedSession struct {
session clientSession
asyncSessions []clientSession
newSessionFn newSessionFn
identifierPool ident.Pool
workerPool m3sync.PooledWorkerPool
replicationSemaphore chan struct{}
scope tally.Scope
log *zap.Logger
metrics replicatedSessionMetrics
outCh chan error
writeTimestampOffset time.Duration
}
type replicatedSessionMetrics struct {
replicateExecuted tally.Counter
replicateNotExecuted tally.Counter
replicateError tally.Counter
replicateSuccess tally.Counter
}
func newReplicatedSessionMetrics(scope tally.Scope) replicatedSessionMetrics {
return replicatedSessionMetrics{
replicateExecuted: scope.Counter("replicate.executed"),
replicateNotExecuted: scope.Counter("replicate.not-executed"),
replicateError: scope.Counter("replicate.error"),
replicateSuccess: scope.Counter("replicate.success"),
}
}
// Ensure replicatedSession implements the clientSession interface.
var _ clientSession = (*replicatedSession)(nil)
type replicatedSessionOption func(*replicatedSession)
func withNewSessionFn(fn newSessionFn) replicatedSessionOption {
return func(session *replicatedSession) {
session.newSessionFn = fn
}
}
func newReplicatedSession(
opts Options, asyncOpts []Options, options ...replicatedSessionOption,
) (clientSession, error) {
workerPool := opts.AsyncWriteWorkerPool()
scope := opts.InstrumentOptions().MetricsScope()
session := replicatedSession{
newSessionFn: newSession,
identifierPool: opts.IdentifierPool(),
workerPool: workerPool,
replicationSemaphore: make(chan struct{}, opts.AsyncWriteMaxConcurrency()),
scope: scope,
log: opts.InstrumentOptions().Logger(),
metrics: newReplicatedSessionMetrics(scope),
writeTimestampOffset: opts.WriteTimestampOffset(),
}
// Apply options
for _, option := range options {
option(&session)
}
if err := session.setSession(opts); err != nil {
return nil, err
}
if err := session.setAsyncSessions(asyncOpts); err != nil {
return nil, err
}
return &session, nil
}
func (s *replicatedSession) setSession(opts Options) error {
if opts.TopologyInitializer() == nil {
return nil
}
session, err := s.newSessionFn(opts)
if err != nil {
return err
}
s.session = session
return nil
}
func (s *replicatedSession) setAsyncSessions(opts []Options) error {
sessions := make([]clientSession, 0, len(opts))
for i, oo := range opts {
subscope := oo.InstrumentOptions().MetricsScope().SubScope(fmt.Sprintf("async-%d", i))
oo = oo.SetInstrumentOptions(oo.InstrumentOptions().SetMetricsScope(subscope))
session, err := s.newSessionFn(oo)
if err != nil {
return err
}
sessions = append(sessions, session)
}
s.asyncSessions = sessions
return nil
}
type replicatedParams struct {
namespace ident.ID
id ident.ID
t xtime.UnixNano
value float64
unit xtime.Unit
annotation []byte
tags ident.TagIterator
useTags bool
}
// NB(srobb): it would be a nicer to accept a lambda which is the fn to
// be performed on all sessions, however this causes an extra allocation.
func (s replicatedSession) replicate(params replicatedParams) error {
for _, asyncSession := range s.asyncSessions {
asyncSession := asyncSession // capture var
var (
clonedID = s.identifierPool.Clone(params.id)
clonedNS = s.identifierPool.Clone(params.namespace)
clonedTags ident.TagIterator
)
if params.useTags {
clonedTags = params.tags.Duplicate()
}
select {
case s.replicationSemaphore <- struct{}{}:
s.workerPool.Go(func() {
var err error
if params.useTags {
err = asyncSession.WriteTagged(
clonedNS, clonedID, clonedTags, params.t,
params.value, params.unit, params.annotation,
)
} else {
err = asyncSession.Write(
clonedNS, clonedID, params.t,
params.value, params.unit, params.annotation,
)
}
if err != nil {
s.metrics.replicateError.Inc(1)
s.log.Error("could not replicate write", zap.Error(err))
} else {
s.metrics.replicateSuccess.Inc(1)
}
if s.outCh != nil {
s.outCh <- err
}
<-s.replicationSemaphore
})
s.metrics.replicateExecuted.Inc(1)
default:
s.metrics.replicateNotExecuted.Inc(1)
}
}
if params.useTags {
return s.session.WriteTagged(
params.namespace, params.id, params.tags, params.t,
params.value, params.unit, params.annotation,
)
}
return s.session.Write(
params.namespace, params.id, params.t,
params.value, params.unit, params.annotation,
)
}
func (s *replicatedSession) ReadClusterAvailability() (bool, error) {
return s.session.ReadClusterAvailability()
}
func (s *replicatedSession) WriteClusterAvailability() (bool, error) {
return s.session.WriteClusterAvailability()
}
// Write value to the database for an ID.
func (s replicatedSession) Write(
namespace, id ident.ID, t xtime.UnixNano, value float64,
unit xtime.Unit, annotation []byte,
) error {
return s.replicate(replicatedParams{
namespace: namespace,
id: id,
t: t.Add(-s.writeTimestampOffset),
value: value,
unit: unit,
annotation: annotation,
})
}
// WriteTagged value to the database for an ID and given tags.
func (s replicatedSession) WriteTagged(
namespace, id ident.ID, tags ident.TagIterator, t xtime.UnixNano,
value float64, unit xtime.Unit, annotation []byte,
) error {
return s.replicate(replicatedParams{
namespace: namespace,
id: id,
t: t.Add(-s.writeTimestampOffset),
value: value,
unit: unit,
annotation: annotation,
tags: tags,
useTags: true,
})
}
// Fetch values from the database for an ID.
func (s replicatedSession) Fetch(
namespace, id ident.ID, startInclusive, endExclusive xtime.UnixNano,
) (encoding.SeriesIterator, error) {
return s.session.Fetch(namespace, id, startInclusive, endExclusive)
}
// FetchIDs values from the database for a set of IDs.
func (s replicatedSession) FetchIDs(
namespace ident.ID, ids ident.Iterator, startInclusive, endExclusive xtime.UnixNano,
) (encoding.SeriesIterators, error) {
return s.session.FetchIDs(namespace, ids, startInclusive, endExclusive)
}
// Aggregate aggregates values from the database for the given set of constraints.
func (s replicatedSession) Aggregate(
ctx context.Context,
ns ident.ID,
q index.Query,
opts index.AggregationOptions,
) (AggregatedTagsIterator, FetchResponseMetadata, error) {
return s.session.Aggregate(ctx, ns, q, opts)
}
// FetchTagged resolves the provided query to known IDs, and fetches the data for them.
func (s replicatedSession) FetchTagged(
ctx context.Context,
namespace ident.ID,
q index.Query,
opts index.QueryOptions,
) (encoding.SeriesIterators, FetchResponseMetadata, error) {
return s.session.FetchTagged(ctx, namespace, q, opts)
}
// FetchTaggedIDs resolves the provided query to known IDs.
func (s replicatedSession) FetchTaggedIDs(
ctx context.Context,
namespace ident.ID,
q index.Query,
opts index.QueryOptions,
) (TaggedIDsIterator, FetchResponseMetadata, error) {
return s.session.FetchTaggedIDs(ctx, namespace, q, opts)
}
// ShardID returns the given shard for an ID for callers
// to easily discern what shard is failing when operations
// for given IDs begin failing.
func (s replicatedSession) ShardID(id ident.ID) (uint32, error) {
return s.session.ShardID(id)
}
// IteratorPools exposes the internal iterator pools used by the session to clients.
func (s replicatedSession) IteratorPools() (encoding.IteratorPools, error) {
return s.session.IteratorPools()
}
// Close the session.
func (s replicatedSession) Close() error {
err := s.session.Close()
for _, as := range s.asyncSessions {
if err := as.Close(); err != nil {
s.log.Error("could not close async session: %v", zap.Error(err))
}
}
return err
}
// Origin returns the host that initiated the session.
func (s replicatedSession) Origin() topology.Host {
return s.session.Origin()
}
// Replicas returns the replication factor.
func (s replicatedSession) Replicas() int {
return s.session.Replicas()
}
// TopologyMap returns the current topology map. Note that the session
// has a separate topology watch than the database itself, so the two
// values can be out of sync and this method should not be relied upon
// if the current view of the topology as seen by the database is required.
func (s replicatedSession) TopologyMap() (topology.Map, error) {
return s.session.TopologyMap()
}
// Truncate will truncate the namespace for a given shard.
func (s replicatedSession) Truncate(namespace ident.ID) (int64, error) {
return s.session.Truncate(namespace)
}
// FetchBootstrapBlocksFromPeers will fetch the most fulfilled block
// for each series using the runtime configurable bootstrap level consistency.
func (s replicatedSession) FetchBootstrapBlocksFromPeers(
namespace namespace.Metadata,
shard uint32,
start, end xtime.UnixNano,
opts result.Options,
) (result.ShardResult, error) {
return s.session.FetchBootstrapBlocksFromPeers(namespace, shard, start, end, opts)
}
// FetchBootstrapBlocksMetadataFromPeers will fetch the blocks metadata from
// available peers using the runtime configurable bootstrap level consistency.
func (s replicatedSession) FetchBootstrapBlocksMetadataFromPeers(
namespace ident.ID,
shard uint32,
start, end xtime.UnixNano,
result result.Options,
) (PeerBlockMetadataIter, error) {
return s.session.FetchBootstrapBlocksMetadataFromPeers(namespace, shard, start, end, result)
}
// FetchBlocksMetadataFromPeers will fetch the blocks metadata from
// available peers.
func (s replicatedSession) FetchBlocksMetadataFromPeers(
namespace ident.ID,
shard uint32,
start, end xtime.UnixNano,
consistencyLevel topology.ReadConsistencyLevel,
result result.Options,
) (PeerBlockMetadataIter, error) {
return s.session.FetchBlocksMetadataFromPeers(namespace, shard, start, end, consistencyLevel, result)
}
// FetchBlocksFromPeers will fetch the required blocks from the
// peers specified.
func (s replicatedSession) FetchBlocksFromPeers(
namespace namespace.Metadata,
shard uint32,
consistencyLevel topology.ReadConsistencyLevel,
metadatas []block.ReplicaMetadata,
opts result.Options,
) (PeerBlocksIter, error) {
return s.session.FetchBlocksFromPeers(namespace, shard, consistencyLevel, metadatas, opts)
}
func (s *replicatedSession) BorrowConnections(
shardID uint32,
fn WithBorrowConnectionFn,
opts BorrowConnectionOptions,
) (BorrowConnectionsResult, error) {
return s.session.BorrowConnections(shardID, fn, opts)
}
func (s *replicatedSession) DedicatedConnection(
shardID uint32,
opts DedicatedConnectionOptions,
) (rpc.TChanNode, Channel, error) {
return s.session.DedicatedConnection(shardID, opts)
}
// Open the client session.
func (s replicatedSession) Open() error {
if err := s.session.Open(); err != nil {
return err
}
for _, asyncSession := range s.asyncSessions {
if err := asyncSession.Open(); err != nil {
s.log.Error("could not open session to async cluster: %v", zap.Error(err))
}
}
return nil
}