-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
replication_stats_report.go
424 lines (380 loc) · 13.1 KB
/
replication_stats_report.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
// 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 reports
import (
"context"
"time"
"github.com/cockroachdb/cockroach/pkg/base"
"github.com/cockroachdb/cockroach/pkg/config"
"github.com/cockroachdb/cockroach/pkg/config/zonepb"
"github.com/cockroachdb/cockroach/pkg/keys"
"github.com/cockroachdb/cockroach/pkg/kv"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/sql/sem/tree"
"github.com/cockroachdb/cockroach/pkg/sql/sqlutil"
"github.com/cockroachdb/cockroach/pkg/util/log"
"github.com/cockroachdb/errors"
)
// replicationStatsReportID is the id of the row in the system. reports_meta
// table corresponding to the replication stats report (i.e. the
// system.replication_stats table).
const replicationStatsReportID reportID = 3
// RangeReport represents the system.zone_range_status report.
type RangeReport map[ZoneKey]zoneRangeStatus
// zoneRangeStatus is the leaf of the RangeReport.
type zoneRangeStatus struct {
numRanges int32
unavailable int32
underReplicated int32
overReplicated int32
}
// replicationStatsReportSaver deals with saving a RangeReport to the database.
// The idea is for it to be used to save new version of the report over and
// over. It maintains the previously-saved version of the report in order to
// speed-up the saving of the next one.
type replicationStatsReportSaver struct {
previousVersion RangeReport
lastGenerated time.Time
lastUpdatedRowCount int
}
// makeReplicationStatsReportSaver creates a new report saver.
func makeReplicationStatsReportSaver() replicationStatsReportSaver {
return replicationStatsReportSaver{}
}
// LastUpdatedRowCount is the count of the rows that were touched during the last save.
func (r *replicationStatsReportSaver) LastUpdatedRowCount() int {
return r.lastUpdatedRowCount
}
// EnsureEntry creates an entry for the given key if there is none.
func (r RangeReport) EnsureEntry(zKey ZoneKey) {
if _, ok := r[zKey]; !ok {
r[zKey] = zoneRangeStatus{}
}
}
// CountRange adds one range's info to the report. If there's no entry in the
// report for the range's zone, a new one is created.
func (r RangeReport) CountRange(zKey ZoneKey, status roachpb.RangeStatusReport) {
r.EnsureEntry(zKey)
rStat := r[zKey]
rStat.numRanges++
if !status.Available {
rStat.unavailable++
}
if status.UnderReplicated {
rStat.underReplicated++
}
if status.OverReplicated {
rStat.overReplicated++
}
r[zKey] = rStat
}
func (r *replicationStatsReportSaver) loadPreviousVersion(
ctx context.Context, ex sqlutil.InternalExecutor, txn *kv.Txn,
) error {
// The data for the previous save needs to be loaded if:
// - this is the first time that we call this method and lastUpdatedAt has never been set
// - in case that the lastUpdatedAt is set but is different than the timestamp in reports_meta
// this indicates that some other worker wrote after we did the write.
if !r.lastGenerated.IsZero() {
generated, err := getReportGenerationTime(ctx, replicationStatsReportID, ex, txn)
if err != nil {
return err
}
// If the report is missing, this is the first time we are running and the
// reload is needed. In that case, generated will be the zero value.
if generated == r.lastGenerated {
// We have the latest report; reload not needed.
return nil
}
}
const prevViolations = "select zone_id, subzone_id, total_ranges, " +
"unavailable_ranges, under_replicated_ranges, over_replicated_ranges " +
"from system.replication_stats"
it, err := ex.QueryIterator(
ctx, "get-previous-replication-stats", txn, prevViolations,
)
if err != nil {
return err
}
r.previousVersion = make(RangeReport)
var ok bool
for ok, err = it.Next(ctx); ok; ok, err = it.Next(ctx) {
row := it.Cur()
key := ZoneKey{}
key.ZoneID = (config.ObjectID)(*row[0].(*tree.DInt))
key.SubzoneID = base.SubzoneID(*row[1].(*tree.DInt))
r.previousVersion[key] = zoneRangeStatus{
(int32)(*row[2].(*tree.DInt)),
(int32)(*row[3].(*tree.DInt)),
(int32)(*row[4].(*tree.DInt)),
(int32)(*row[5].(*tree.DInt)),
}
}
return err
}
func (r *replicationStatsReportSaver) updateTimestamp(
ctx context.Context, ex sqlutil.InternalExecutor, txn *kv.Txn, reportTS time.Time,
) error {
if !r.lastGenerated.IsZero() && reportTS == r.lastGenerated {
return errors.Errorf(
"The new time %s is the same as the time of the last update %s",
reportTS.String(),
r.lastGenerated.String(),
)
}
_, err := ex.Exec(
ctx,
"timestamp-upsert-replication-stats",
txn,
"upsert into system.reports_meta(id, generated) values($1, $2)",
replicationStatsReportID,
reportTS,
)
return err
}
// Save a report in the database.
//
// report should not be used by the caller any more after this call; the callee
// takes ownership.
// reportTS is the time that will be set in the updated_at column for every row.
func (r *replicationStatsReportSaver) Save(
ctx context.Context,
report RangeReport,
reportTS time.Time,
db *kv.DB,
ex sqlutil.InternalExecutor,
) error {
r.lastUpdatedRowCount = 0
if err := db.Txn(ctx, func(ctx context.Context, txn *kv.Txn) error {
err := r.loadPreviousVersion(ctx, ex, txn)
if err != nil {
return err
}
err = r.updateTimestamp(ctx, ex, txn, reportTS)
if err != nil {
return err
}
for key, status := range report {
if err := r.upsertStats(ctx, txn, key, status, ex); err != nil {
return err
}
}
for key := range r.previousVersion {
if _, ok := report[key]; !ok {
_, err := ex.Exec(
ctx,
"delete-old-replication-stats",
txn,
"delete from system.replication_stats "+
"where zone_id = $1 and subzone_id = $2",
key.ZoneID,
key.SubzoneID,
)
if err != nil {
return err
}
r.lastUpdatedRowCount++
}
}
return nil
}); err != nil {
return err
}
r.lastGenerated = reportTS
r.previousVersion = report
return nil
}
// upsertStat upserts a row into system.replication_stats.
func (r *replicationStatsReportSaver) upsertStats(
ctx context.Context, txn *kv.Txn, key ZoneKey, stats zoneRangeStatus, ex sqlutil.InternalExecutor,
) error {
var err error
previousStats, hasOldVersion := r.previousVersion[key]
if hasOldVersion && previousStats == stats {
// No change in the stats so no update.
return nil
}
// Updating an old row.
_, err = ex.Exec(
ctx, "upsert-replication-stats", txn,
"upsert into system.replication_stats(report_id, zone_id, subzone_id, "+
"total_ranges, unavailable_ranges, under_replicated_ranges, "+
"over_replicated_ranges) values($1, $2, $3, $4, $5, $6, $7)",
replicationStatsReportID,
key.ZoneID, key.SubzoneID, stats.numRanges, stats.unavailable,
stats.underReplicated, stats.overReplicated,
)
if err != nil {
return err
}
r.lastUpdatedRowCount++
return nil
}
// replicationStatsVisitor is a visitor that builds a RangeReport.
type replicationStatsVisitor struct {
cfg *config.SystemConfig
nodeChecker nodeChecker
// report is the output of the visitor. visit*() methods populate it.
// After visiting all the ranges, it can be retrieved with Report().
report RangeReport
visitErr bool
// prevZoneKey and prevNumVoters maintain state from one range to the next.
// This state can be reused when a range is covered by the same zone config as
// the previous one. Reusing it speeds up the report generation.
prevZoneKey ZoneKey
prevNumVoters int
}
var _ rangeVisitor = &replicationStatsVisitor{}
func makeReplicationStatsVisitor(
ctx context.Context, cfg *config.SystemConfig, nodeChecker nodeChecker,
) replicationStatsVisitor {
v := replicationStatsVisitor{
cfg: cfg,
nodeChecker: nodeChecker,
report: make(RangeReport),
}
v.reset(ctx)
return v
}
// failed is part of the rangeVisitor interface.
func (v *replicationStatsVisitor) failed() bool {
return v.visitErr
}
// Report returns the RangeReport that was populated by previous visit*() calls.
func (v *replicationStatsVisitor) Report() RangeReport {
return v.report
}
// reset is part of the rangeVisitor interface.
func (v *replicationStatsVisitor) reset(ctx context.Context) {
*v = replicationStatsVisitor{
cfg: v.cfg,
nodeChecker: v.nodeChecker,
prevNumVoters: -1,
report: make(RangeReport, len(v.report)),
}
// Iterate through all the zone configs to create report entries for all the
// zones that have constraints. Otherwise, just iterating through the ranges
// wouldn't create entries for zones that don't apply to any ranges.
maxObjectID, err := v.cfg.GetLargestObjectID(
0 /* maxReservedDescID - return the largest ID in the config */, keys.PseudoTableIDs,
)
if err != nil {
log.Fatalf(ctx, "unexpected failure to compute max object id: %s", err)
}
for i := config.ObjectID(1); i <= maxObjectID; i++ {
zone, err := getZoneByID(i, v.cfg)
if err != nil {
log.Fatalf(ctx, "unexpected failure to compute max object id: %s", err)
}
if zone == nil {
continue
}
v.ensureEntries(MakeZoneKey(i, NoSubzone), zone)
}
}
func (v *replicationStatsVisitor) ensureEntries(key ZoneKey, zone *zonepb.ZoneConfig) {
if zoneChangesReplication(zone) {
v.report.EnsureEntry(key)
}
for i, sz := range zone.Subzones {
v.ensureEntries(MakeZoneKey(key.ZoneID, base.SubzoneIDFromIndex(i)), &sz.Config)
}
}
// visitNewZone is part of the rangeVisitor interface.
func (v *replicationStatsVisitor) visitNewZone(
ctx context.Context, r *roachpb.RangeDescriptor,
) (retErr error) {
defer func() {
v.visitErr = retErr != nil
}()
var zKey ZoneKey
var zConfig *zonepb.ZoneConfig
// NumReplicas and NumVoters are not necessarily set in tandem so
// if NumVoters is not found there should be continual traversal to
// find it and otherwise defer to NumReplicas.
// Based on the above, desiredNumVoters is set to NumVoters or NumReplicas
// if NumVoters is not set.
var desiredNumVoters int
// Figure out the zone config for whose report the current range is to be
// counted. This is the lowest-level zone config covering the range that
// changes replication settings. We also need to figure out the replication
// factor this zone is configured with; the replication factor might be
// inherited from a higher-level zone config.
_, err := visitZones(ctx, r, v.cfg, ignoreSubzonePlaceholders,
func(_ context.Context, zone *zonepb.ZoneConfig, key ZoneKey) bool {
if zConfig == nil {
if !zoneChangesReplication(zone) {
return false
}
zKey = key
zConfig = zone
if zone.NumVoters != nil {
desiredNumVoters = int(*zone.NumVoters)
return true
}
if zone.NumReplicas != nil && desiredNumVoters == 0 {
desiredNumVoters = int(*zone.NumReplicas)
}
// We need to continue upwards in search for NumVoters
// and, if the previous is not found, NumReplicas.
return false
}
if zone.NumVoters != nil {
desiredNumVoters = int(*zone.NumVoters)
return true
}
if zone.NumReplicas != nil && desiredNumVoters == 0 {
desiredNumVoters = int(*zone.NumReplicas)
}
// We had already found the zone to report to, but we're haven't found
// its NumVoters and NumReplicas yet.
return false
})
if err != nil {
return errors.NewAssertionErrorWithWrappedErrf(err, "unexpected error visiting zones for range %s", r)
}
if desiredNumVoters == 0 {
return errors.AssertionFailedf(
"no zone config with replication attributes found for range: %s", r)
}
v.prevZoneKey = zKey
v.prevNumVoters = desiredNumVoters
v.countRange(ctx, zKey, desiredNumVoters, r)
return nil
}
// visitSameZone is part of the rangeVisitor interface.
func (v *replicationStatsVisitor) visitSameZone(ctx context.Context, r *roachpb.RangeDescriptor) {
v.countRange(ctx, v.prevZoneKey, v.prevNumVoters, r)
}
func (v *replicationStatsVisitor) countRange(
ctx context.Context, key ZoneKey, replicationFactor int, r *roachpb.RangeDescriptor,
) {
status := r.Replicas().ReplicationStatus(func(rDesc roachpb.ReplicaDescriptor) bool {
return v.nodeChecker(rDesc.NodeID)
// NB: this reporting code was written before ReplicationStatus reported
// on non-voting replicas. This code will also soon be removed in favor
// of something that works with multi-tenancy (#89987).
}, replicationFactor, 0)
// Note that a range can be under-replicated and over-replicated at the same
// time if it has many replicas, but sufficiently many of them are on dead
// nodes.
v.report.CountRange(key, status)
}
// zoneChangesReplication determines whether a given zone config changes
// replication attributes: the replication factor or the replication
// constraints.
// This is used to determine which zone's report a range counts towards for the
// replication_stats and the critical_localities reports : it'll count towards
// the lowest ancestor for which this method returns true.
func zoneChangesReplication(zone *zonepb.ZoneConfig) bool {
return (zone.NumReplicas != nil && *zone.NumReplicas != 0) ||
(zone.NumVoters != nil && *zone.NumVoters != 0) ||
zone.Constraints != nil
}