-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathreplica_metrics.go
299 lines (271 loc) · 10.2 KB
/
replica_metrics.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
// Copyright 2019 The Cockroach Authors.
//
// Use of this software is governed by the Business Source License
// included in the file licenses/BSL.txt.
//
// As of the Change Date specified in that file, in accordance with
// the Business Source License, use of this software will be governed
// by the Apache License, Version 2.0, included in the file
// licenses/APL.txt.
package kvserver
import (
"context"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/config/zonepb"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/kvserverpb"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/liveness"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"go.etcd.io/etcd/raft/v3"
)
// ReplicaMetrics contains details on the current status of the replica.
type ReplicaMetrics struct {
Leader bool
LeaseValid bool
Leaseholder bool
LeaseType roachpb.LeaseType
LeaseStatus kvserverpb.LeaseStatus
// Quiescent indicates whether the replica believes itself to be quiesced.
Quiescent bool
// Ticking indicates whether the store is ticking the replica. It should be
// the opposite of Quiescent.
Ticking bool
// Is this the replica which collects per-range metrics? This is done either
// on the leader or, if there is no leader, on the largest live replica ID.
RangeCounter bool
Unavailable bool
Underreplicated bool
Overreplicated bool
BehindCount int64
LatchInfoLocal kvserverpb.LatchManagerInfo
LatchInfoGlobal kvserverpb.LatchManagerInfo
RaftLogTooLarge bool
}
// Metrics returns the current metrics for the replica.
func (r *Replica) Metrics(
ctx context.Context, now hlc.ClockTimestamp, livenessMap liveness.IsLiveMap, clusterNodes int,
) ReplicaMetrics {
r.mu.RLock()
raftStatus := r.raftStatusRLocked()
leaseStatus := r.leaseStatusAtRLocked(ctx, now)
quiescent := r.mu.quiescent || r.mu.internalRaftGroup == nil
desc := r.mu.state.Desc
zone := r.mu.zone
raftLogSize := r.mu.raftLogSize
raftLogSizeTrusted := r.mu.raftLogSizeTrusted
r.mu.RUnlock()
r.store.unquiescedReplicas.Lock()
_, ticking := r.store.unquiescedReplicas.m[r.RangeID]
r.store.unquiescedReplicas.Unlock()
latchInfoGlobal, latchInfoLocal := r.concMgr.LatchMetrics()
return calcReplicaMetrics(
ctx,
now.ToTimestamp(),
&r.store.cfg.RaftConfig,
zone,
livenessMap,
clusterNodes,
desc,
raftStatus,
leaseStatus,
r.store.StoreID(),
quiescent,
ticking,
latchInfoLocal,
latchInfoGlobal,
raftLogSize,
raftLogSizeTrusted,
)
}
func calcReplicaMetrics(
_ context.Context,
_ hlc.Timestamp,
raftCfg *base.RaftConfig,
zone *zonepb.ZoneConfig,
livenessMap liveness.IsLiveMap,
clusterNodes int,
desc *roachpb.RangeDescriptor,
raftStatus *raft.Status,
leaseStatus kvserverpb.LeaseStatus,
storeID roachpb.StoreID,
quiescent bool,
ticking bool,
latchInfoLocal kvserverpb.LatchManagerInfo,
latchInfoGlobal kvserverpb.LatchManagerInfo,
raftLogSize int64,
raftLogSizeTrusted bool,
) ReplicaMetrics {
var m ReplicaMetrics
var leaseOwner bool
m.LeaseStatus = leaseStatus
if leaseStatus.IsValid() {
m.LeaseValid = true
leaseOwner = leaseStatus.Lease.OwnedBy(storeID)
m.LeaseType = leaseStatus.Lease.Type()
}
m.Leaseholder = m.LeaseValid && leaseOwner
m.Leader = isRaftLeader(raftStatus)
m.Quiescent = quiescent
m.Ticking = ticking
m.RangeCounter, m.Unavailable, m.Underreplicated, m.Overreplicated =
calcRangeCounter(storeID, desc, livenessMap, zone.GetNumVoters(), *zone.NumReplicas, clusterNodes)
// The raft leader computes the number of raft entries that replicas are
// behind.
if m.Leader {
m.BehindCount = calcBehindCount(raftStatus, desc, livenessMap)
}
m.LatchInfoLocal = latchInfoLocal
m.LatchInfoGlobal = latchInfoGlobal
const raftLogTooLargeMultiple = 4
m.RaftLogTooLarge = raftLogSize > (raftLogTooLargeMultiple*raftCfg.RaftLogTruncationThreshold) &&
raftLogSizeTrusted
return m
}
// calcRangeCounter returns whether this replica is designated as the replica in
// the range responsible for range-level metrics, whether the range doesn't have
// a quorum of live voting replicas, and whether the range is currently
// under-replicated (with regards to either the number of voting replicas or the
// number of non-voting replicas).
//
// Note: we compute an estimated range count across the cluster by counting the
// first live replica in each descriptor. Note that the first live replica is
// an arbitrary choice. We want to select one live replica to do the counting
// that all replicas can agree on.
//
// Note that this heuristic can double count. If the first live replica is on
// a node that is partitioned from the other replicas in the range, there may
// be multiple nodes which believe they are the first live replica. This
// scenario seems rare as it requires the partitioned node to be alive enough
// to be performing liveness heartbeats.
func calcRangeCounter(
storeID roachpb.StoreID,
desc *roachpb.RangeDescriptor,
livenessMap liveness.IsLiveMap,
numVoters, numReplicas int32,
clusterNodes int,
) (rangeCounter, unavailable, underreplicated, overreplicated bool) {
// It seems unlikely that a learner replica would be the first live one, but
// there's no particular reason to exclude them.
for _, rd := range desc.Replicas().Descriptors() {
if livenessMap[rd.NodeID].IsLive {
rangeCounter = rd.StoreID == storeID
break
}
}
// We also compute an estimated per-range count of under-replicated and
// unavailable ranges for each range based on the liveness table.
if rangeCounter {
unavailable = !desc.Replicas().CanMakeProgress(func(rDesc roachpb.ReplicaDescriptor) bool {
return livenessMap[rDesc.NodeID].IsLive
})
neededVoters := GetNeededVoters(numVoters, clusterNodes)
liveVoters := calcLiveVoterReplicas(desc, livenessMap)
neededNonVoters := GetNeededNonVoters(int(numVoters), int(numReplicas-numVoters), clusterNodes)
liveNonVoters := calcLiveNonVoterReplicas(desc, livenessMap)
if neededVoters > liveVoters || neededNonVoters > liveNonVoters {
underreplicated = true
} else if neededVoters < liveVoters || neededNonVoters < liveNonVoters {
overreplicated = true
}
}
return
}
// calcLiveVoterReplicas returns a count of the live voter replicas; a live
// replica is determined by checking its node in the provided liveness map. This
// method is used when indicating under-replication so only voter replicas are
// considered.
func calcLiveVoterReplicas(desc *roachpb.RangeDescriptor, livenessMap liveness.IsLiveMap) int {
var live int
for _, rd := range desc.Replicas().VoterDescriptors() {
if livenessMap[rd.NodeID].IsLive {
live++
}
}
return live
}
// calcLiveNonVoterReplicas returns a count of the live non-voter replicas; a live
// replica is determined by checking its node in the provided liveness map.
func calcLiveNonVoterReplicas(desc *roachpb.RangeDescriptor, livenessMap liveness.IsLiveMap) int {
var live int
for _, rd := range desc.Replicas().NonVoterDescriptors() {
if livenessMap[rd.NodeID].IsLive {
live++
}
}
return live
}
// calcBehindCount returns a total count of log entries that follower replicas
// are behind. This can only be computed on the raft leader.
func calcBehindCount(
raftStatus *raft.Status, desc *roachpb.RangeDescriptor, livenessMap liveness.IsLiveMap,
) int64 {
var behindCount int64
for _, rd := range desc.Replicas().Descriptors() {
if progress, ok := raftStatus.Progress[uint64(rd.ReplicaID)]; ok {
if progress.Match > 0 &&
progress.Match < raftStatus.Commit {
behindCount += int64(raftStatus.Commit) - int64(progress.Match)
}
}
}
return behindCount
}
// QueriesPerSecond returns the range's average QPS if it is the current
// leaseholder. If it isn't, this will return 0 because the replica does not
// know about the reads that the leaseholder is serving.
//
// A "Query" is a BatchRequest (regardless of its contents) arriving at the
// leaseholder with a gateway node set in the header (i.e. excluding requests
// that weren't sent through a DistSender, which in practice should be
// practically none).
func (r *Replica) QueriesPerSecond() float64 {
qps, _ := r.leaseholderStats.avgQPS()
return qps
}
// WritesPerSecond returns the range's average keys written per second. A
// "Write" is a mutation applied by Raft as measured by
// engine.RocksDBBatchCount(writeBatch). This corresponds roughly to the number
// of keys mutated by a write. For example, writing 12 intents would count as 24
// writes (12 for the metadata, 12 for the versions). A DeleteRange that
// ultimately only removes one key counts as one (or two if it's transactional).
func (r *Replica) WritesPerSecond() float64 {
wps, _ := r.writeStats.avgQPS()
return wps
}
func (r *Replica) needsSplitBySizeRLocked() bool {
exceeded, _ := r.exceedsMultipleOfSplitSizeRLocked(1)
return exceeded
}
func (r *Replica) needsMergeBySizeRLocked() bool {
return r.mu.state.Stats.Total() < *r.mu.zone.RangeMinBytes
}
func (r *Replica) needsRaftLogTruncationLocked() bool {
// We don't want to check the Raft log for truncation on every write
// operation or even every operation which occurs after the Raft log exceeds
// RaftLogQueueStaleSize. The logic below queues the replica for possible
// Raft log truncation whenever an additional RaftLogQueueStaleSize bytes
// have been written to the Raft log.
checkRaftLog := r.mu.raftLogSize-r.mu.raftLogLastCheckSize >= RaftLogQueueStaleSize
if checkRaftLog {
r.mu.raftLogLastCheckSize = r.mu.raftLogSize
}
return checkRaftLog
}
// exceedsMultipleOfSplitSizeRLocked returns whether the current size of the
// range exceeds the max size times mult. If so, the bytes overage is also
// returned. Note that the max size is determined by either the current maximum
// size as dictated by the zone config or a previous max size indicating that
// the max size has changed relatively recently and thus we should not
// backpressure for being over.
func (r *Replica) exceedsMultipleOfSplitSizeRLocked(mult float64) (exceeded bool, bytesOver int64) {
maxBytes := *r.mu.zone.RangeMaxBytes
if r.mu.largestPreviousMaxRangeSizeBytes > maxBytes {
maxBytes = r.mu.largestPreviousMaxRangeSizeBytes
}
size := r.mu.state.Stats.Total()
maxSize := int64(float64(maxBytes)*mult) + 1
if maxBytes <= 0 || size <= maxSize {
return false, 0
}
return true, size - maxSize
}