-
Notifications
You must be signed in to change notification settings - Fork 24
Expand file tree
/
Copy pathservice.go
More file actions
431 lines (392 loc) · 16.3 KB
/
Copy pathservice.go
File metadata and controls
431 lines (392 loc) · 16.3 KB
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
package slave
import (
"context"
"errors"
"fmt"
"io"
"math/rand"
"strings"
"time"
"github.com/flipkart-incubator/dkv/pkg/health"
"github.com/flipkart-incubator/dkv/internal/discovery"
"github.com/flipkart-incubator/dkv/internal/hlc"
opts "github.com/flipkart-incubator/dkv/internal/opts"
"github.com/flipkart-incubator/dkv/internal/storage"
"github.com/flipkart-incubator/dkv/pkg/ctl"
"github.com/flipkart-incubator/dkv/pkg/serverpb"
"go.uber.org/zap"
"google.golang.org/protobuf/types/known/emptypb"
)
// A DKVService represents a service for serving key value data.
type DKVService interface {
io.Closer
serverpb.DKVServer
serverpb.DKVDiscoveryNodeServer
health.HealthServer
}
type ReplicationConfig struct {
// Max num changes to poll from master in a single replication call
MaxNumChngs uint32
// Interval to periodically poll changes from master
ReplPollInterval time.Duration
// Maximum allowed replication lag from master for the slave to be considered valid
MaxActiveReplLag uint64
// Maximum allowed replication time elapsed from master for the slave to be considered valid
// Applicable when replication requests are erroring out due to an issue with master / slave
MaxActiveReplElapsed uint64
// Listener address of the master node
ReplMasterAddr string
// Temporary flag to disable automatic master discovery until https://github.com/flipkart-incubator/dkv/issues/82 is fixed
// The above issue causes replication issues during master switch due to inconsistent change numbers
DisableAutoMasterDisc bool
}
type replInfo struct {
// can be nil only initially when trying to find a master to replicate from
replCli *ctl.DKVClient
// replActive can be used to avoid setting replCli to nil during master reelection
// which would otherwise require additional locks to prevent crashes due to intermediate null switches
replActive bool
replTckr *time.Ticker
replStop chan struct{}
replLag uint64
lastReplTime uint64
replConfig *ReplicationConfig
fromChngNum uint64
}
type slaveService struct {
store storage.KVStore
ca storage.ChangeApplier
regionInfo *serverpb.RegionInfo
clusterInfo discovery.ClusterInfoGetter
isClosed bool
replInfo *replInfo
serveropts *opts.ServerOpts
}
// NewService creates a slave DKVService that periodically polls
// for changes from master node and replicates them onto its local
// storage. As a result, it forbids changes to this local storage
// through any of the other key value mutators.
func NewService(store storage.KVStore, ca storage.ChangeApplier, regionInfo *serverpb.RegionInfo,
replConf *ReplicationConfig, clusterInfo discovery.ClusterInfoGetter,
serveropts *opts.ServerOpts) (DKVService, error) {
if store == nil || ca == nil {
return nil, errors.New("invalid args - params `store`, `ca` and `replPollInterval` are all mandatory")
}
return newSlaveService(store, ca, regionInfo, replConf, clusterInfo, serveropts), nil
}
func newSlaveService(store storage.KVStore, ca storage.ChangeApplier, info *serverpb.RegionInfo,
replConf *ReplicationConfig, clusterInfo discovery.ClusterInfoGetter, serveropts *opts.ServerOpts) *slaveService {
ri := &replInfo{replConfig: replConf}
ss := &slaveService{store: store, ca: ca, regionInfo: info, replInfo: ri, clusterInfo: clusterInfo, serveropts: serveropts}
ss.findAndConnectToMaster()
ss.startReplication()
return ss
}
func (ss *slaveService) Put(_ context.Context, _ *serverpb.PutRequest) (*serverpb.PutResponse, error) {
return nil, errors.New("DKV slave service does not support keyspace mutations")
}
func (ss *slaveService) MultiPut(_ context.Context, _ *serverpb.MultiPutRequest) (*serverpb.PutResponse, error) {
return nil, errors.New("DKV slave service does not support keyspace mutations")
}
func (ss *slaveService) Delete(_ context.Context, _ *serverpb.DeleteRequest) (*serverpb.DeleteResponse, error) {
return nil, errors.New("DKV slave service does not support keyspace mutations")
}
func (ss *slaveService) CompareAndSet(_ context.Context, _ *serverpb.CompareAndSetRequest) (*serverpb.CompareAndSetResponse, error) {
return nil, errors.New("DKV slave service does not support keyspace mutations")
}
func (ss *slaveService) GetKeySpaceSize(ctx context.Context, empty *emptypb.Empty) (*serverpb.KeySpaceSizeResponse, error) {
//TODO implement me
panic("implement me")
}
func (ss *slaveService) Get(ctx context.Context, getReq *serverpb.GetRequest) (*serverpb.GetResponse, error) {
readResults, err := ss.store.Get(getReq.Key)
res := &serverpb.GetResponse{Status: newEmptyStatus()}
if err != nil {
res.Status = newErrorStatus(err)
} else {
if len(readResults) == 1 {
res.Value = readResults[0].Value
}
}
return res, err
}
func (ss *slaveService) Check(ctx context.Context, healthCheckReq *health.HealthCheckRequest) (*health.HealthCheckResponse, error) {
if ss.isClosed {
return &health.HealthCheckResponse{Status: health.HealthCheckResponse_NOT_SERVING}, nil
}
if ss.replInfo.replLag > ss.replInfo.replConfig.MaxActiveReplLag {
return &health.HealthCheckResponse{Status: health.HealthCheckResponse_NOT_SERVING}, nil
}
// server has not started replicating yet or the server last replicated more than MaxActiveReplElapsed ago
if ss.replInfo.lastReplTime == 0 || hlc.GetTimeAgo(ss.replInfo.lastReplTime) > ss.replInfo.replConfig.MaxActiveReplElapsed {
return &health.HealthCheckResponse{Status: health.HealthCheckResponse_NOT_SERVING}, nil
}
return &health.HealthCheckResponse{Status: health.HealthCheckResponse_SERVING}, nil
}
func (ss *slaveService) Watch(req *health.HealthCheckRequest, watcher health.Health_WatchServer) error {
if ss.isClosed {
return watcher.Send(&health.HealthCheckResponse{Status: health.HealthCheckResponse_NOT_SERVING})
}
ticker := time.NewTicker(time.Duration(ss.serveropts.HealthCheckTickerInterval) * time.Second)
defer ticker.Stop()
for {
select {
case <-ticker.C:
checkResponse, err := ss.Check(context.Background(), req)
if err != nil {
return err
}
if err := watcher.Send(checkResponse); err != nil {
return err
}
case <-ss.replInfo.replStop:
return watcher.Send(&health.HealthCheckResponse{Status: health.HealthCheckResponse_NOT_SERVING})
}
}
}
func (ss *slaveService) MultiGet(ctx context.Context, multiGetReq *serverpb.MultiGetRequest) (*serverpb.MultiGetResponse, error) {
readResults, err := ss.store.Get(multiGetReq.Keys...)
res := &serverpb.MultiGetResponse{Status: newEmptyStatus()}
if err != nil {
res.Status = newErrorStatus(err)
} else {
res.KeyValues = readResults
}
return res, err
}
func (ss *slaveService) Iterate(iterReq *serverpb.IterateRequest, dkvIterSrvr serverpb.DKV_IterateServer) error {
iteration := storage.NewIteration(ss.store, iterReq)
err := iteration.ForEach(func(e *serverpb.KVPair) error {
itRes := &serverpb.IterateResponse{Status: newEmptyStatus(), Key: e.Key, Value: e.Value}
return dkvIterSrvr.Send(itRes)
})
if err != nil {
itRes := &serverpb.IterateResponse{Status: newErrorStatus(err)}
return dkvIterSrvr.Send(itRes)
}
return nil
}
func (ss *slaveService) Close() error {
ss.serveropts.Logger.Info("Closing the slave service")
ss.replInfo.replStop <- struct{}{}
ss.replInfo.replTckr.Stop()
if ss.replInfo.replCli != nil {
ss.replInfo.replCli.Close()
}
ss.store.Close()
ss.isClosed = true
return nil
}
func (ss *slaveService) startReplication() {
ss.replInfo.replTckr = time.NewTicker(ss.replInfo.replConfig.ReplPollInterval)
latestChngNum, _ := ss.ca.GetLatestAppliedChangeNumber()
ss.replInfo.fromChngNum = 1 + latestChngNum
ss.replInfo.replStop = make(chan struct{})
slg := ss.serveropts.Logger.Sugar()
slg.Infof("Replicating changes from change number: %d and polling interval: %s", ss.replInfo.fromChngNum, ss.replInfo.replConfig.ReplPollInterval.String())
slg.Sync()
go ss.pollAndApplyChanges()
}
func (ss *slaveService) pollAndApplyChanges() {
for {
select {
case <-ss.replInfo.replTckr.C:
ss.serveropts.Logger.Info("Current replication lag", zap.Uint64("ReplicationLag", ss.replInfo.replLag))
ss.serveropts.StatsCli.Gauge("replication.lag", int64(ss.replInfo.replLag))
if err := ss.applyChangesFromMaster(ss.replInfo.replConfig.MaxNumChngs); err != nil {
ss.serveropts.Logger.Error("Unable to retrieve changes from master", zap.Error(err))
if err := ss.replaceMasterIfInactive(); err != nil {
ss.serveropts.Logger.Error("Unable to replace master", zap.Error(err))
}
}
case <-ss.replInfo.replStop:
ss.serveropts.Logger.Info("Stopping the change poller")
break
}
}
}
func (ss *slaveService) applyChangesFromMaster(chngsPerBatch uint32) error {
defer ss.serveropts.StatsCli.Timing("slave.applyChangesFromMaster.latency.ms", time.Now())
if ss.replInfo.replCli == nil || !ss.replInfo.replActive {
return errors.New("can not replicate as unable to connect to an active master")
}
ss.serveropts.Logger.Info("Retrieving changes from master", zap.Uint64("FromChangeNumber", ss.replInfo.fromChngNum), zap.Uint32("ChangesPerBatch", chngsPerBatch))
res, err := ss.replInfo.replCli.GetChanges(ss.replInfo.fromChngNum, chngsPerBatch)
if err == nil {
if res.Status.Code != 0 {
// this is an error from DKV master's end
err = errors.New(res.Status.Message)
} else {
if res.MasterChangeNumber < (ss.replInfo.fromChngNum - 1) {
ss.serveropts.Logger.Error("change number of the master node can not be lesser than the change number of the slave node", zap.Uint64("MasterChangeNum", res.MasterChangeNumber), zap.Uint64("FromChangeNum", ss.replInfo.fromChngNum))
err = errors.New("change number of the master node can not be lesser than the change number of the slave node")
} else {
if err = ss.applyChanges(res); err == nil {
ss.replInfo.lastReplTime = hlc.UnixNow()
}
}
}
} else {
if strings.Contains(err.Error(), "ResourceExhausted") {
// This is an error from DKV slave's end where the GRPC
// receive buffer is exhausted. We now attempt to retrieve
// the changes by halving the batch size. We try this until
// the batch size can no longer be halved (= 0) and then
// give up with an error. In such cases, this method is
// invoked recursively utmost log2[ss.maxNumChngs] times.
ss.serveropts.Logger.Warn("GetChanges call exceeded resource limits", zap.Error(err))
if newMaxNumChngs := chngsPerBatch >> 1; newMaxNumChngs > 0 {
ss.serveropts.Logger.Warn("Retrieving smaller batches of changes", zap.Uint32("before", chngsPerBatch), zap.Uint32("after", newMaxNumChngs))
err = ss.applyChangesFromMaster(newMaxNumChngs)
} else {
err = errors.New("unable to retrieve changes from master due to GRPC resource exhaustion on slave")
}
}
}
return err
}
func (ss *slaveService) applyChanges(chngsRes *serverpb.GetChangesResponse) error {
if chngsRes.NumberOfChanges > 0 {
ss.serveropts.Logger.Info("Applying the changes received from master", zap.Uint32("NumberOfChanges", chngsRes.NumberOfChanges))
actChngNum, err := ss.ca.SaveChanges(chngsRes.Changes)
if err != nil {
return err
}
ss.replInfo.fromChngNum = actChngNum + 1
ss.serveropts.Logger.Info("Changes applied to local storage", zap.Uint64("FromChangeNumber", ss.replInfo.fromChngNum))
ss.replInfo.replLag = chngsRes.MasterChangeNumber - actChngNum
} else {
ss.serveropts.Logger.Info("Not received any changes from master")
}
return nil
}
func newErrorStatus(err error) *serverpb.Status {
return &serverpb.Status{Code: -1, Message: err.Error()}
}
func newEmptyStatus() *serverpb.Status {
return &serverpb.Status{Code: 0, Message: ""}
}
func (ss *slaveService) GetStatus(context context.Context, request *emptypb.Empty) (*serverpb.RegionInfo, error) {
if ss.replInfo.replLag > ss.replInfo.replConfig.MaxActiveReplLag {
ss.regionInfo.Status = serverpb.RegionStatus_INACTIVE
} else if ss.replInfo.lastReplTime == 0 || hlc.GetTimeAgo(ss.replInfo.lastReplTime) > ss.replInfo.replConfig.MaxActiveReplElapsed {
ss.regionInfo.Status = serverpb.RegionStatus_INACTIVE
} else if ss.isClosed {
ss.regionInfo.Status = serverpb.RegionStatus_INACTIVE
} else {
ss.regionInfo.Status = serverpb.RegionStatus_ACTIVE_SLAVE
}
ss.regionInfo.MasterHost = &ss.replInfo.replConfig.ReplMasterAddr
ss.serveropts.Logger.Debug("Current Info", zap.String("Status", ss.regionInfo.Status.String()),
zap.Uint64("Repl Lag", ss.replInfo.replLag), zap.Uint64("Last Repl time", ss.replInfo.lastReplTime))
return ss.regionInfo, nil
}
func (ss *slaveService) replaceMasterIfInactive() error {
if ss.replInfo.replConfig.DisableAutoMasterDisc {
return nil
}
if regions, err := ss.clusterInfo.GetClusterStatus(ss.regionInfo.GetDatabase(), ss.regionInfo.GetVBucket()); err == nil {
var currentMaster *serverpb.RegionInfo = nil
for _, region := range regions {
if region.NodeAddress == ss.replInfo.replConfig.ReplMasterAddr {
currentMaster = region
break
}
}
if currentMaster == nil {
// Current Master not found in cluster. implies current master is inactive
return ss.reconnectMaster()
} else if !isVBucketApplicableToBeMaster(currentMaster) {
return ss.reconnectMaster()
} else {
// current master is active. No action required as replication error could be temporary
// need to validate this assumption though
return nil
}
} else {
return err
}
}
func (ss *slaveService) reconnectMaster() error {
// This is so that replication doesn't happen from inactive master
// which could otherwise result in slave marking itself active if no errors in replication
ss.replInfo.replConfig.ReplMasterAddr = ""
ss.replInfo.replActive = false
return ss.findAndConnectToMaster()
}
func (ss *slaveService) findAndConnectToMaster() error {
if master, err := ss.findNewMaster(); err == nil {
// TODO: Check if authority override option is needed for slaves while they connect with masters
if replCli, err := ctl.NewInSecureDKVClient(*master, ""); err == nil {
if ss.replInfo.replCli != nil {
ss.replInfo.replCli.Close()
}
ss.replInfo.replCli = replCli
ss.replInfo.replConfig.ReplMasterAddr = *master
ss.replInfo.replActive = true
} else {
ss.serveropts.Logger.Warn("Unable to create a replication client", zap.Error(err))
return err
}
} else {
ss.serveropts.Logger.Warn("Unable to find a master for this slave to replicate from", zap.Error(err))
return err
}
return nil
}
// Finds a new active master for the region
// Prefers followers within the local DC first, followed by master within local DC
// followed by followers outside DC, followed by master outside DC
// TODO - rather than randomly selecting a master from applicable followers, load balance to distribute better
func (ss *slaveService) findNewMaster() (*string, error) {
if ss.replInfo.replConfig.DisableAutoMasterDisc {
return &ss.replInfo.replConfig.ReplMasterAddr, nil
}
// Get all active regions
if vBuckets, err := ss.clusterInfo.GetClusterStatus(ss.regionInfo.GetDatabase(), ss.regionInfo.GetVBucket()); err == nil {
// Filter regions applicable to become master for this slave
var filteredVBuckets []*serverpb.RegionInfo
for _, vBucket := range vBuckets {
if isVBucketApplicableToBeMaster(vBucket) {
filteredVBuckets = append(filteredVBuckets, vBucket)
}
}
if len(filteredVBuckets) == 0 {
return nil, fmt.Errorf("no active master found for database %s and vBucket %s",
ss.regionInfo.Database, ss.regionInfo.VBucket)
} else {
vBuckets = filteredVBuckets
}
// If any region exists within the DC, prefer those.
var localDCVBuckets []*serverpb.RegionInfo
for _, vBucket := range vBuckets {
if vBucket.DcID == ss.regionInfo.DcID {
localDCVBuckets = append(localDCVBuckets, vBucket)
}
}
if len(localDCVBuckets) > 0 {
vBuckets = localDCVBuckets
}
// If any non master region exists, prefer those.
var followers []*serverpb.RegionInfo
for _, vBucket := range vBuckets {
if vBucket.Status != serverpb.RegionStatus_LEADER {
followers = append(followers, vBucket)
}
}
if len(followers) > 0 {
vBuckets = followers
}
// Randomly select 1 region
idx := rand.Intn(len(vBuckets))
return &vBuckets[idx].NodeAddress, nil
} else {
return nil, err
}
}
// Assumption is slave can not replicate from any other slave
// if above assumption is incorrect, we should modify the filter conditions appropriately
func isVBucketApplicableToBeMaster(info *serverpb.RegionInfo) bool {
return info.Status == serverpb.RegionStatus_LEADER || info.Status == serverpb.RegionStatus_PRIMARY_FOLLOWER ||
info.Status == serverpb.RegionStatus_SECONDARY_FOLLOWER
}