-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathreplica_metrics.go
254 lines (228 loc) · 8.09 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
// 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 storage
import (
"context"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/config"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/storage/storagepb"
"github.com/cockroachdb/cockroach/pkg/util/hlc"
"go.etcd.io/etcd/raft"
)
// ReplicaMetrics contains details on the current status of the replica.
type ReplicaMetrics struct {
Leader bool
LeaseValid bool
Leaseholder bool
LeaseType roachpb.LeaseType
LeaseStatus storagepb.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 storagepb.LatchManagerInfo
LatchInfoGlobal storagepb.LatchManagerInfo
RaftLogTooLarge bool
}
// Metrics returns the current metrics for the replica.
func (r *Replica) Metrics(
ctx context.Context, now hlc.Timestamp, livenessMap IsLiveMap, clusterNodes int,
) ReplicaMetrics {
r.mu.RLock()
raftStatus := r.raftStatusRLocked()
leaseStatus := r.leaseStatus(*r.mu.state.Lease, now, r.mu.minLeaseProposedTS)
quiescent := r.mu.quiescent || r.mu.internalRaftGroup == nil
desc := r.mu.state.Desc
zone := r.mu.zone
raftLogSize := r.mu.raftLogSize
r.mu.RUnlock()
r.store.unquiescedReplicas.Lock()
_, ticking := r.store.unquiescedReplicas.m[r.RangeID]
r.store.unquiescedReplicas.Unlock()
latchInfoGlobal, latchInfoLocal := r.latchMgr.Info()
return calcReplicaMetrics(
ctx,
now,
&r.store.cfg.RaftConfig,
zone,
livenessMap,
clusterNodes,
desc,
raftStatus,
leaseStatus,
r.store.StoreID(),
quiescent,
ticking,
latchInfoLocal,
latchInfoGlobal,
raftLogSize,
)
}
func calcReplicaMetrics(
_ context.Context,
_ hlc.Timestamp,
raftCfg *base.RaftConfig,
zone *config.ZoneConfig,
livenessMap IsLiveMap,
clusterNodes int,
desc *roachpb.RangeDescriptor,
raftStatus *raft.Status,
leaseStatus storagepb.LeaseStatus,
storeID roachpb.StoreID,
quiescent bool,
ticking bool,
latchInfoLocal storagepb.LatchManagerInfo,
latchInfoGlobal storagepb.LatchManagerInfo,
raftLogSize int64,
) ReplicaMetrics {
var m ReplicaMetrics
var leaseOwner bool
m.LeaseStatus = leaseStatus
if leaseStatus.State == storagepb.LeaseState_VALID {
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.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)
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 replicas, and whether the
// range is currently under-replicated.
//
// 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 IsLiveMap,
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. Note that `All` returns the
// voters first.
for _, rd := range desc.Replicas().All() {
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 {
_, live := livenessMap[rDesc.NodeID]
return live
})
needed := GetNeededReplicas(numReplicas, clusterNodes)
liveVoterReplicas := calcLiveVoterReplicas(desc, livenessMap)
if needed > liveVoterReplicas {
underreplicated = true
} else if needed < liveVoterReplicas {
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 IsLiveMap) int {
var live int
for _, rd := range desc.Replicas().Voters() {
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 IsLiveMap,
) int64 {
var behindCount int64
for _, rd := range desc.Replicas().All() {
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 {
return r.exceedsMultipleOfSplitSizeRLocked(1)
}
func (r *Replica) needsMergeBySizeRLocked() bool {
return r.mu.state.Stats.Total() < *r.mu.zone.RangeMinBytes
}
func (r *Replica) exceedsMultipleOfSplitSizeRLocked(mult float64) bool {
maxBytes := *r.mu.zone.RangeMaxBytes
size := r.mu.state.Stats.Total()
return maxBytes > 0 && float64(size) > float64(maxBytes)*mult
}