-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
store_snapshot.go
687 lines (629 loc) · 23.3 KB
/
store_snapshot.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
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
// Copyright 2018 The Cockroach Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
// implied. See the License for the specific language governing
// permissions and limitations under the License.
package storage
import (
"context"
"fmt"
"io"
"math"
"github.com/pkg/errors"
"go.etcd.io/etcd/raft/raftpb"
"golang.org/x/time/rate"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/storage/engine"
"github.com/cockroachdb/cockroach/pkg/util/envutil"
"github.com/cockroachdb/cockroach/pkg/util/humanizeutil"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/cockroach/pkg/util/protoutil"
"github.com/cockroachdb/cockroach/pkg/util/timeutil"
"github.com/cockroachdb/cockroach/pkg/util/uuid"
)
const (
// preemptiveSnapshotRaftGroupID is a bogus ID for which a Raft group is
// temporarily created during the application of a preemptive snapshot.
preemptiveSnapshotRaftGroupID = math.MaxUint64
// Messages that provide detail about why a preemptive snapshot was rejected.
snapshotStoreTooFullMsg = "store almost out of disk space"
snapshotApplySemBusyMsg = "store busy applying snapshots"
storeDrainingMsg = "store is draining"
// IntersectingSnapshotMsg is part of the error message returned from
// canApplySnapshotLocked and is exposed here so testing can rely on it.
IntersectingSnapshotMsg = "snapshot intersects existing range"
)
// incomingSnapshotStream is the minimal interface on a GRPC stream required
// to receive a snapshot over the network.
type incomingSnapshotStream interface {
Send(*SnapshotResponse) error
Recv() (*SnapshotRequest, error)
}
// outgoingSnapshotStream is the minimal interface on a GRPC stream required
// to send a snapshot over the network.
type outgoingSnapshotStream interface {
Send(*SnapshotRequest) error
Recv() (*SnapshotResponse, error)
}
// snapshotStrategy is an approach to sending and receiving Range snapshots.
// Each implementation corresponds to a SnapshotRequest_Strategy, and it is
// expected that the implementation that matches the Strategy specified in the
// snapshot header will always be used.
type snapshotStrategy interface {
// Receive streams SnapshotRequests in from the provided stream and
// constructs an IncomingSnapshot.
Receive(
context.Context, incomingSnapshotStream, SnapshotRequest_Header,
) (IncomingSnapshot, error)
// Send streams SnapshotRequests created from the OutgoingSnapshot in to the
// provided stream.
Send(
context.Context, outgoingSnapshotStream, SnapshotRequest_Header, *OutgoingSnapshot,
) error
// Status provides a status report on the work performed during the
// snapshot. Only valid if the strategy succeeded.
Status() string
}
func assertStrategy(
ctx context.Context, header SnapshotRequest_Header, expect SnapshotRequest_Strategy,
) {
if header.Strategy != expect {
log.Fatalf(ctx, "expected strategy %s, found strategy %s", expect, header.Strategy)
}
}
// kvBatchSnapshotStrategy is an implementation of snapshotStrategy that streams
// batches of KV pairs in the BatchRepr format.
type kvBatchSnapshotStrategy struct {
raftCfg *base.RaftConfig
status string
// Fields used when sending snapshots.
batchSize int64
limiter *rate.Limiter
newBatch func() engine.Batch
}
// Send implements the snapshotStrategy interface.
func (kvSS *kvBatchSnapshotStrategy) Receive(
ctx context.Context, stream incomingSnapshotStream, header SnapshotRequest_Header,
) (IncomingSnapshot, error) {
assertStrategy(ctx, header, SnapshotRequest_KV_BATCH)
var batches [][]byte
var logEntries [][]byte
for {
req, err := stream.Recv()
if err != nil {
return IncomingSnapshot{}, err
}
if req.Header != nil {
err := errors.New("client error: provided a header mid-stream")
return IncomingSnapshot{}, sendSnapshotError(stream, err)
}
if req.KVBatch != nil {
batches = append(batches, req.KVBatch)
}
if req.LogEntries != nil {
logEntries = append(logEntries, req.LogEntries...)
}
if req.Final {
snapUUID, err := uuid.FromBytes(header.RaftMessageRequest.Message.Snapshot.Data)
if err != nil {
err = errors.Wrap(err, "invalid snapshot")
return IncomingSnapshot{}, sendSnapshotError(stream, err)
}
inSnap := IncomingSnapshot{
SnapUUID: snapUUID,
Batches: batches,
LogEntries: logEntries,
State: &header.State,
snapType: snapTypeRaft,
}
if header.RaftMessageRequest.ToReplica.ReplicaID == 0 {
inSnap.snapType = snapTypePreemptive
}
kvSS.status = fmt.Sprintf("kv batches: %d, log entries: %d", len(batches), len(logEntries))
return inSnap, nil
}
}
}
// Send implements the snapshotStrategy interface.
func (kvSS *kvBatchSnapshotStrategy) Send(
ctx context.Context,
stream outgoingSnapshotStream,
header SnapshotRequest_Header,
snap *OutgoingSnapshot,
) error {
assertStrategy(ctx, header, SnapshotRequest_KV_BATCH)
// Iterate over all keys using the provided iterator and stream out batches
// of key-values.
n := 0
var b engine.Batch
for iter := snap.Iter; ; iter.Next() {
if ok, err := iter.Valid(); err != nil {
return err
} else if !ok {
break
}
key := iter.Key()
value := iter.Value()
n++
if b == nil {
b = kvSS.newBatch()
}
if err := b.Put(key, value); err != nil {
b.Close()
return err
}
if int64(len(b.Repr())) >= kvSS.batchSize {
if err := kvSS.limiter.WaitN(ctx, 1); err != nil {
return err
}
if err := kvSS.sendBatch(stream, b); err != nil {
return err
}
b = nil
// We no longer need the keys and values in the batch we just sent,
// so reset ReplicaDataIterator's allocator and allow its data to
// be garbage collected.
iter.ResetAllocator()
}
}
if b != nil {
if err := kvSS.limiter.WaitN(ctx, 1); err != nil {
return err
}
if err := kvSS.sendBatch(stream, b); err != nil {
return err
}
}
// Iterate over the specified range of Raft entries and send them all out
// together.
firstIndex := header.State.TruncatedState.Index + 1
endIndex := snap.RaftSnap.Metadata.Index + 1
preallocSize := endIndex - firstIndex
const maxPreallocSize = 1000
if preallocSize > maxPreallocSize {
// It's possible for the raft log to become enormous in certain
// sustained failure conditions. We may bail out of the snapshot
// process early in scanFunc, but in the worst case this
// preallocation is enough to run the server out of memory. Limit
// the size of the buffer we will preallocate.
preallocSize = maxPreallocSize
}
logEntries := make([][]byte, 0, preallocSize)
var raftLogBytes int64
scanFunc := func(kv roachpb.KeyValue) (bool, error) {
bytes, err := kv.Value.GetBytes()
if err == nil {
logEntries = append(logEntries, bytes)
raftLogBytes += int64(len(bytes))
if snap.snapType == snapTypePreemptive &&
raftLogBytes > 4*kvSS.raftCfg.RaftLogTruncationThreshold {
// If the raft log is too large, abort the snapshot instead of
// potentially running out of memory. However, if this is a
// raft-initiated snapshot (instead of a preemptive one), we
// have a dilemma. It may be impossible to truncate the raft
// log until we have caught up a peer with a snapshot. Since
// we don't know the exact size at which we will run out of
// memory, we err on the size of allowing the snapshot if it
// is raft-initiated, while aborting preemptive snapshots at a
// reasonable threshold. (Empirically, this is good enough:
// the situations that result in large raft logs have not been
// observed to result in raft-initiated snapshots).
//
// By aborting preemptive snapshots here, we disallow replica
// changes until the current replicas have caught up and
// truncated the log (either the range is available, in which
// case this will eventually happen, or it's not,in which case
// the preemptive snapshot would be wasted anyway because the
// change replicas transaction would be unable to commit).
return false, errors.Errorf(
"aborting snapshot because raft log is too large "+
"(%d bytes after processing %d of %d entries)",
raftLogBytes, len(logEntries), endIndex-firstIndex)
}
}
return false, err
}
rangeID := header.State.Desc.RangeID
if err := iterateEntries(ctx, snap.EngineSnap, rangeID, firstIndex, endIndex, scanFunc); err != nil {
return err
}
// Inline the payloads for all sideloaded proposals.
//
// TODO(tschottdorf): could also send slim proposals and attach sideloaded
// SSTables directly to the snapshot. Probably the better long-term
// solution, but let's see if it ever becomes relevant. Snapshots with
// inlined proposals are hopefully the exception.
{
var ent raftpb.Entry
for i := range logEntries {
if err := protoutil.Unmarshal(logEntries[i], &ent); err != nil {
return err
}
if !sniffSideloadedRaftCommand(ent.Data) {
continue
}
if err := snap.WithSideloaded(func(ss sideloadStorage) error {
newEnt, err := maybeInlineSideloadedRaftCommand(
ctx, rangeID, ent, ss, snap.RaftEntryCache,
)
if err != nil {
return err
}
if newEnt != nil {
ent = *newEnt
}
return nil
}); err != nil {
if errors.Cause(err) == errSideloadedFileNotFound {
// We're creating the Raft snapshot based on a snapshot of
// the engine, but the Raft log may since have been
// truncated and corresponding on-disk sideloaded payloads
// unlinked. Luckily, we can just abort this snapshot; the
// caller can retry.
//
// TODO(tschottdorf): check how callers handle this. They
// should simply retry. In some scenarios, perhaps this can
// happen repeatedly and prevent a snapshot; not sending the
// log entries wouldn't help, though, and so we'd really
// need to make sure the entries are always here, for
// instance by pre-loading them into memory. Or we can make
// log truncation less aggressive about removing sideloaded
// files, by delaying trailing file deletion for a bit.
return &errMustRetrySnapshotDueToTruncation{
index: ent.Index,
term: ent.Term,
}
}
return err
}
// TODO(tschottdorf): it should be possible to reuse `logEntries[i]` here.
var err error
if logEntries[i], err = protoutil.Marshal(&ent); err != nil {
return err
}
}
}
kvSS.status = fmt.Sprintf("kv pairs: %d, log entries: %d", n, len(logEntries))
return stream.Send(&SnapshotRequest{LogEntries: logEntries})
}
func (kvSS *kvBatchSnapshotStrategy) sendBatch(
stream outgoingSnapshotStream, batch engine.Batch,
) error {
repr := batch.Repr()
batch.Close()
return stream.Send(&SnapshotRequest{KVBatch: repr})
}
// Status implements the snapshotStrategy interface.
func (kvSS *kvBatchSnapshotStrategy) Status() string { return kvSS.status }
// reserveSnapshot throttles incoming snapshots. The returned closure is used
// to cleanup the reservation and release its resources. A nil cleanup function
// and a non-empty rejectionMessage indicates the reservation was declined.
func (s *Store) reserveSnapshot(
ctx context.Context, header *SnapshotRequest_Header,
) (_cleanup func(), _rejectionMsg string, _err error) {
if header.RangeSize == 0 {
// Empty snapshots are exempt from rate limits because they're so cheap to
// apply. This vastly speeds up rebalancing any empty ranges created by a
// RESTORE or manual SPLIT AT, since it prevents these empty snapshots from
// getting stuck behind large snapshots managed by the replicate queue.
} else if header.CanDecline {
storeDesc, ok := s.cfg.StorePool.getStoreDescriptor(s.StoreID())
if ok && (!maxCapacityCheck(storeDesc) || header.RangeSize > storeDesc.Capacity.Available) {
return nil, snapshotStoreTooFullMsg, nil
}
select {
case s.snapshotApplySem <- struct{}{}:
case <-ctx.Done():
return nil, "", ctx.Err()
case <-s.stopper.ShouldStop():
return nil, "", errors.Errorf("stopped")
default:
return nil, snapshotApplySemBusyMsg, nil
}
} else {
select {
case s.snapshotApplySem <- struct{}{}:
case <-ctx.Done():
return nil, "", ctx.Err()
case <-s.stopper.ShouldStop():
return nil, "", errors.Errorf("stopped")
}
}
s.metrics.ReservedReplicaCount.Inc(1)
s.metrics.Reserved.Inc(header.RangeSize)
return func() {
s.metrics.ReservedReplicaCount.Dec(1)
s.metrics.Reserved.Dec(header.RangeSize)
if header.RangeSize != 0 {
<-s.snapshotApplySem
}
}, "", nil
}
// canApplySnapshot returns (_, nil) if the snapshot can be applied to
// this store's replica (i.e. the snapshot is not from an older incarnation of
// the replica) and a placeholder can be added to the replicasByKey map (if
// necessary). If a placeholder is required, it is returned as the first value.
func (s *Store) canApplySnapshot(
ctx context.Context, snapHeader *SnapshotRequest_Header,
) (*ReplicaPlaceholder, error) {
s.mu.Lock()
defer s.mu.Unlock()
return s.canApplySnapshotLocked(ctx, snapHeader)
}
func (s *Store) canApplySnapshotLocked(
ctx context.Context, snapHeader *SnapshotRequest_Header,
) (*ReplicaPlaceholder, error) {
desc := *snapHeader.State.Desc
if v, ok := s.mu.replicas.Load(int64(desc.RangeID)); ok && (*Replica)(v).IsInitialized() {
// We have an initialized replica. Preemptive snapshots can be applied with
// no further checks if they do not widen the existing replica. Raft
// snapshots can be applied with no further checks even if they widen the
// existing replica—we can't reject them at this point—but see the comments
// in Replica.maybeAcquireSnapshotMergeLock for how this is made safe.
existingDesc := (*Replica)(v).Desc()
if !snapHeader.IsPreemptive() || !existingDesc.EndKey.Less(desc.EndKey) {
return nil, nil
}
// We have a preemptive snapshot that widens an existing replica. Proceed
// by checking the keyspace covered by the snapshot but not the existing
// replica.
desc.StartKey = existingDesc.EndKey
}
// We don't have the range, or we have an uninitialized placeholder, or the
// existing range is less wide. Will we be able to create/initialize it?
if exRng, ok := s.mu.replicaPlaceholders[desc.RangeID]; ok {
return nil, errors.Errorf("%s: canApplySnapshotLocked: cannot add placeholder, have an existing placeholder %s", s, exRng)
}
// TODO(benesch): consider discovering and GC'ing *all* overlapping ranges,
// not just the first one that getOverlappingKeyRangeLocked happens to return.
if exRange := s.getOverlappingKeyRangeLocked(&desc); exRange != nil {
// We have a conflicting range, so we must block the snapshot.
// When such a conflict exists, it will be resolved by one range
// either being split or garbage collected.
exReplica, err := s.GetReplica(exRange.Desc().RangeID)
msg := IntersectingSnapshotMsg
if err != nil {
log.Warning(ctx, errors.Wrapf(
err, "unable to look up overlapping replica on %s", exReplica))
} else {
inactive := func(r *Replica) bool {
if r.RaftStatus() == nil {
return true
}
lease, pendingLease := r.GetLease()
now := s.Clock().Now()
return !r.IsLeaseValid(lease, now) &&
(pendingLease == (roachpb.Lease{}) || !r.IsLeaseValid(pendingLease, now))
}
// If the existing range shows no signs of recent activity, give it a GC
// run.
if inactive(exReplica) {
if _, err := s.replicaGCQueue.Add(exReplica, replicaGCPriorityCandidate); err != nil {
log.Errorf(ctx, "%s: unable to add replica to GC queue: %s", exReplica, err)
} else {
msg += "; initiated GC:"
}
}
}
return nil, errors.Errorf("%s %v", msg, exReplica) // exReplica can be nil
}
placeholder := &ReplicaPlaceholder{
rangeDesc: desc,
}
return placeholder, nil
}
// receiveSnapshot receives an incoming snapshot via a pre-opened GRPC stream.
func (s *Store) receiveSnapshot(
ctx context.Context, header *SnapshotRequest_Header, stream incomingSnapshotStream,
) error {
cleanup, rejectionMsg, err := s.reserveSnapshot(ctx, header)
if err != nil {
return err
}
if cleanup == nil {
return stream.Send(&SnapshotResponse{
Status: SnapshotResponse_DECLINED,
Message: rejectionMsg,
})
}
defer cleanup()
// Check to see if the snapshot can be applied but don't attempt to add
// a placeholder here, because we're not holding the replica's raftMu.
// We'll perform this check again later after receiving the rest of the
// snapshot data - this is purely an optimization to prevent downloading
// a snapshot that we know we won't be able to apply.
if _, err := s.canApplySnapshot(ctx, header); err != nil {
return sendSnapshotError(stream,
errors.Wrapf(err, "%s,r%d: cannot apply snapshot", s, header.State.Desc.RangeID),
)
}
// Determine which snapshot strategy the sender is using to send this
// snapshot. If we don't know how to handle the specified strategy, return
// an error.
var ss snapshotStrategy
switch header.Strategy {
case SnapshotRequest_KV_BATCH:
ss = &kvBatchSnapshotStrategy{
raftCfg: &s.cfg.RaftConfig,
}
default:
return sendSnapshotError(stream,
errors.Errorf("%s,r%d: unknown snapshot strategy: %s",
s, header.State.Desc.RangeID, header.Strategy),
)
}
if err := stream.Send(&SnapshotResponse{Status: SnapshotResponse_ACCEPTED}); err != nil {
return err
}
if log.V(2) {
log.Infof(ctx, "accepted snapshot reservation for r%d", header.State.Desc.RangeID)
}
inSnap, err := ss.Receive(ctx, stream, *header)
if err != nil {
return err
}
if err := s.processRaftSnapshotRequest(ctx, header, inSnap); err != nil {
return sendSnapshotError(stream, errors.Wrap(err.GoError(), "failed to apply snapshot"))
}
return stream.Send(&SnapshotResponse{Status: SnapshotResponse_APPLIED})
}
func sendSnapshotError(stream incomingSnapshotStream, err error) error {
return stream.Send(&SnapshotResponse{
Status: SnapshotResponse_ERROR,
Message: err.Error(),
})
}
// SnapshotStorePool narrows StorePool to make sendSnapshot easier to test.
type SnapshotStorePool interface {
throttle(reason throttleReason, toStoreID roachpb.StoreID)
}
var rebalanceSnapshotRate = settings.RegisterByteSizeSetting(
"kv.snapshot_rebalance.max_rate",
"the rate limit (bytes/sec) to use for rebalance snapshots",
envutil.EnvOrDefaultBytes("COCKROACH_PREEMPTIVE_SNAPSHOT_RATE", 2<<20),
)
var recoverySnapshotRate = settings.RegisterByteSizeSetting(
"kv.snapshot_recovery.max_rate",
"the rate limit (bytes/sec) to use for recovery snapshots",
envutil.EnvOrDefaultBytes("COCKROACH_RAFT_SNAPSHOT_RATE", 8<<20),
)
func snapshotRateLimit(
st *cluster.Settings, priority SnapshotRequest_Priority,
) (rate.Limit, error) {
switch priority {
case SnapshotRequest_RECOVERY:
return rate.Limit(recoverySnapshotRate.Get(&st.SV)), nil
case SnapshotRequest_REBALANCE:
return rate.Limit(rebalanceSnapshotRate.Get(&st.SV)), nil
default:
return 0, errors.Errorf("unknown snapshot priority: %s", priority)
}
}
type errMustRetrySnapshotDueToTruncation struct {
index, term uint64
}
func (e *errMustRetrySnapshotDueToTruncation) Error() string {
return fmt.Sprintf(
"log truncation during snapshot removed sideloaded SSTable at index %d, term %d",
e.index, e.term,
)
}
// sendSnapshot sends an outgoing snapshot via a pre-opened GRPC stream.
func sendSnapshot(
ctx context.Context,
raftCfg *base.RaftConfig,
st *cluster.Settings,
stream outgoingSnapshotStream,
storePool SnapshotStorePool,
header SnapshotRequest_Header,
snap *OutgoingSnapshot,
newBatch func() engine.Batch,
sent func(),
) error {
start := timeutil.Now()
to := header.RaftMessageRequest.ToReplica
if err := stream.Send(&SnapshotRequest{Header: &header}); err != nil {
return err
}
// Wait until we get a response from the server.
resp, err := stream.Recv()
if err != nil {
storePool.throttle(throttleFailed, to.StoreID)
return err
}
switch resp.Status {
case SnapshotResponse_DECLINED:
if header.CanDecline {
storePool.throttle(throttleDeclined, to.StoreID)
declinedMsg := "reservation rejected"
if len(resp.Message) > 0 {
declinedMsg = resp.Message
}
return errors.Errorf("%s: remote declined %s: %s", to, snap, declinedMsg)
}
storePool.throttle(throttleFailed, to.StoreID)
return errors.Errorf("%s: programming error: remote declined required %s: %s",
to, snap, resp.Message)
case SnapshotResponse_ERROR:
storePool.throttle(throttleFailed, to.StoreID)
return errors.Errorf("%s: remote couldn't accept %s with error: %s",
to, snap, resp.Message)
case SnapshotResponse_ACCEPTED:
// This is the response we're expecting. Continue with snapshot sending.
default:
storePool.throttle(throttleFailed, to.StoreID)
return errors.Errorf("%s: server sent an invalid status while negotiating %s: %s",
to, snap, resp.Status)
}
log.Infof(ctx, "sending %s", snap)
// The size of batches to send. This is the granularity of rate limiting.
const batchSize = 256 << 10 // 256 KB
targetRate, err := snapshotRateLimit(st, header.Priority)
if err != nil {
return errors.Wrapf(err, "%s", to)
}
// Convert the bytes/sec rate limit to batches/sec.
//
// TODO(peter): Using bytes/sec for rate limiting seems more natural but has
// practical difficulties. We either need to use a very large burst size
// which seems to disable the rate limiting, or call WaitN in smaller than
// burst size chunks which caused excessive slowness in testing. Would be
// nice to figure this out, but the batches/sec rate limit works for now.
limiter := rate.NewLimiter(targetRate/batchSize, 1 /* burst size */)
// Create a snapshotStrategy based on the desired snapshot strategy.
var ss snapshotStrategy
switch header.Strategy {
case SnapshotRequest_KV_BATCH:
ss = &kvBatchSnapshotStrategy{
raftCfg: raftCfg,
batchSize: batchSize,
limiter: limiter,
newBatch: newBatch,
}
default:
log.Fatalf(ctx, "unknown snapshot strategy: %s", header.Strategy)
}
if err := ss.Send(ctx, stream, header, snap); err != nil {
return err
}
// Notify the sent callback before the final snapshot request is sent so that
// the snapshots generated metric gets incremented before the snapshot is
// applied.
sent()
if err := stream.Send(&SnapshotRequest{Final: true}); err != nil {
return err
}
log.Infof(ctx, "streamed snapshot to %s: %s, rate-limit: %s/sec, %0.0fms",
to, ss.Status(), humanizeutil.IBytes(int64(targetRate)),
timeutil.Since(start).Seconds()*1000)
resp, err = stream.Recv()
if err != nil {
return errors.Wrapf(err, "%s: remote failed to apply snapshot", to)
}
// NB: wait for EOF which ensures that all processing on the server side has
// completed (such as defers that might be run after the previous message was
// received).
if unexpectedResp, err := stream.Recv(); err != io.EOF {
return errors.Errorf("%s: expected EOF, got resp=%v err=%v", to, unexpectedResp, err)
}
switch resp.Status {
case SnapshotResponse_ERROR:
return errors.Errorf("%s: remote failed to apply snapshot for reason %s", to, resp.Message)
case SnapshotResponse_APPLIED:
return nil
default:
return errors.Errorf("%s: server sent an invalid status during finalization: %s",
to, resp.Status)
}
}