forked from cockroachdb/cockroach
-
Notifications
You must be signed in to change notification settings - Fork 0
/
raft_snapshot_queue.go
129 lines (116 loc) · 4.07 KB
/
raft_snapshot_queue.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
// Copyright 2015 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 (
"time"
"github.com/coreos/etcd/raft"
"github.com/pkg/errors"
"golang.org/x/net/context"
"github.com/cockroachdb/cockroach/pkg/config"
"github.com/cockroachdb/cockroach/pkg/gossip"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"github.com/cockroachdb/cockroach/pkg/util/log"
)
const (
// raftSnapshotQueueTimerDuration is the duration between Raft snapshot of
// queued replicas.
raftSnapshotQueueTimerDuration = 0 // zero duration to process Raft snapshots greedily
raftSnapshotPriority float64 = 0
)
// raftSnapshotQueue manages a queue of replicas which may need to catch a
// replica up with a snapshot to their range.
type raftSnapshotQueue struct {
*baseQueue
clock *hlc.Clock
}
// newRaftSnapshotQueue returns a new instance of raftSnapshotQueue.
func newRaftSnapshotQueue(store *Store, g *gossip.Gossip, clock *hlc.Clock) *raftSnapshotQueue {
rq := &raftSnapshotQueue{
clock: clock,
}
rq.baseQueue = newBaseQueue(
"raftsnapshot", rq, store, g,
queueConfig{
maxSize: defaultQueueMaxSize,
// The Raft leader (which sends Raft snapshots) may not be the
// leaseholder. Operating on a replica without holding the lease is the
// reason Raft snapshots cannot be performed by the replicateQueue.
needsLease: false,
needsSystemConfig: false,
acceptsUnsplitRanges: true,
successes: store.metrics.RaftSnapshotQueueSuccesses,
failures: store.metrics.RaftSnapshotQueueFailures,
pending: store.metrics.RaftSnapshotQueuePending,
processingNanos: store.metrics.RaftSnapshotQueueProcessingNanos,
},
)
return rq
}
func (rq *raftSnapshotQueue) shouldQueue(
ctx context.Context, now hlc.Timestamp, repl *Replica, _ config.SystemConfig,
) (shouldQ bool, priority float64) {
// If a follower needs a snapshot, enqueue at the highest priority.
if status := repl.RaftStatus(); status != nil {
// raft.Status.Progress is only populated on the Raft group leader.
for _, p := range status.Progress {
if p.State == raft.ProgressStateSnapshot {
if log.V(2) {
log.Infof(ctx, "raft snapshot needed, enqueuing")
}
return true, raftSnapshotPriority
}
}
}
return false, 0
}
func (rq *raftSnapshotQueue) process(
ctx context.Context, repl *Replica, _ config.SystemConfig,
) error {
// If a follower requires a Raft snapshot, perform it.
if status := repl.RaftStatus(); status != nil {
// raft.Status.Progress is only populated on the Raft group leader.
for id, p := range status.Progress {
if p.State == raft.ProgressStateSnapshot {
if log.V(1) {
log.Infof(ctx, "sending raft snapshot")
}
if err := rq.processRaftSnapshot(ctx, repl, roachpb.ReplicaID(id)); err != nil {
return err
}
}
}
}
return nil
}
func (rq *raftSnapshotQueue) processRaftSnapshot(
ctx context.Context, repl *Replica, id roachpb.ReplicaID,
) error {
desc := repl.Desc()
repDesc, ok := desc.GetReplicaDescriptorByID(id)
if !ok {
return errors.Errorf("%s: replica %d not present in %v", repl, id, desc.Replicas)
}
err := repl.sendSnapshot(ctx, repDesc, snapTypeRaft, SnapshotRequest_RECOVERY)
// Report the snapshot status to Raft, which expects us to do this once
// we finish sending the snapshot.
repl.reportSnapshotStatus(uint64(id), err)
return err
}
func (*raftSnapshotQueue) timer(_ time.Duration) time.Duration {
return raftSnapshotQueueTimerDuration
}
func (rq *raftSnapshotQueue) purgatoryChan() <-chan struct{} {
return nil
}