-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
reporter.go
217 lines (202 loc) · 7.44 KB
/
reporter.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
// Copyright 2022 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 spanconfigreporter reports on whether ranges over the queried spans
// conform to the span configs that apply to them.
package spanconfigreporter
import (
"context"
"fmt"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/constraint"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/liveness/livenesspb"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/cockroach/pkg/settings/cluster"
"github.com/cockroachdb/cockroach/pkg/spanconfig"
"github.com/cockroachdb/cockroach/pkg/util/rangedesciter"
)
// rangeDescPageSize controls the page size when iterating through range
// descriptors. It's settable only by the system tenant.
var rangeDescPageSize = settings.RegisterIntSetting(
settings.SystemOnly,
"spanconfig.reporter.range_desc_page_size",
"pa",
100,
func(i int64) error {
if i < 5 || i > 25000 {
return fmt.Errorf("expected range_desc_page_size to be in range [5, 25000], got %d", i)
}
return nil
},
)
// Liveness is the subset of the interface satisfied by CRDB's node liveness
// component that the reporter relies on.
type Liveness interface {
GetIsLiveMap() livenesspb.IsLiveMap
}
// Reporter is used to figure out whether ranges backing specific spans conform
// to the span configs that apply over them. It's a concrete implementation of
// the spanconfig.Reporter interface.
type Reporter struct {
dep struct {
// NB: The data dependencies in the implementation are:
//
// i. point-in-time view over gossip-backed node liveness;
// ii. point-in-time view of range descriptors (done transactionally);
// iii. the store resolver resolving store IDs to store descriptors;
// iv. view over what span configs apply to what keyspans;
//
// TODO(irfansharif): For (iii) and (iv) we might not have a
// point-in-time snapshot of the data.
// - For (iii) it's possible that as we iterate through the set of range
// descriptors, a few of which refer to some store S, we're racing
// against that newly-added store's info not yet being available
// through gossip. This is exceedingly unlikely, but if we see it
// happen, we can expose some snapshot of the StoreResolver state like
// we have for liveness.
// - For (iv) too we're not grabbing a read lock over the backing
// spanconfig.KVSubscriber while reading off each span config, so it's
// possible we generate the report for two range descriptors with span
// configs from different points in time. If this too becomes a
// problem, we can explicitly generate a snapshot like we do for
// liveness.
Liveness
rangedesciter.Iterator
constraint.StoreResolver
spanconfig.StoreReader
}
settings *cluster.Settings
knobs *spanconfig.TestingKnobs
}
var _ spanconfig.Reporter = &Reporter{}
// New constructs and returns a Reporter.
func New(
liveness Liveness,
resolver constraint.StoreResolver,
reader spanconfig.StoreReader,
iterator rangedesciter.Iterator,
settings *cluster.Settings,
knobs *spanconfig.TestingKnobs,
) *Reporter {
r := &Reporter{
settings: settings,
knobs: knobs,
}
r.dep.Liveness = liveness
r.dep.StoreResolver = resolver
r.dep.Iterator = iterator
r.dep.StoreReader = reader
return r
}
// TODO(irfansharif): Support the equivalent of "critical localities", perhaps
// through a different API than the one below since it's not quite
// span-oriented.
//
// TODO(irfansharif): Once wired up the SQL code or exposed through an endpoint,
// write an end-to-end test using actual SQL and zone configs. Set configs on a
// table, disable replication, see conformance report. Enable repl, change
// configs, repeat. Do this for tenants as well.
// SpanConfigConformance implements the spanconfig.Reporter interface.
func (r *Reporter) SpanConfigConformance(
ctx context.Context, spans []roachpb.Span,
) (roachpb.SpanConfigConformanceReport, error) {
report := roachpb.SpanConfigConformanceReport{}
unavailableNodes := make(map[roachpb.NodeID]struct{})
isLiveMap := r.dep.Liveness.GetIsLiveMap()
for _, span := range spans {
if err := r.dep.Iterate(ctx, int(rangeDescPageSize.Get(&r.settings.SV)),
func() { report = roachpb.SpanConfigConformanceReport{} /* init */ },
span,
func(descriptors ...roachpb.RangeDescriptor) error {
for _, desc := range descriptors {
conf, err := r.dep.StoreReader.GetSpanConfigForKey(ctx, desc.StartKey)
if err != nil {
return err
}
status := desc.Replicas().ReplicationStatus(
func(rDesc roachpb.ReplicaDescriptor) bool {
isLive := isLiveMap[rDesc.NodeID].IsLive
if !isLive {
unavailableNodes[rDesc.NodeID] = struct{}{}
}
return isLive
}, int(conf.GetNumVoters()), int(conf.GetNumNonVoters()))
if !status.Available {
report.Unavailable = append(report.Unavailable,
roachpb.ConformanceReportedRange{
RangeDescriptor: desc,
Config: conf,
})
}
if status.UnderReplicated || status.UnderReplicatedNonVoters {
report.UnderReplicated = append(report.UnderReplicated,
roachpb.ConformanceReportedRange{
RangeDescriptor: desc,
Config: conf,
})
}
if status.OverReplicated || status.OverReplicatedNonVoters {
report.OverReplicated = append(report.OverReplicated,
roachpb.ConformanceReportedRange{
RangeDescriptor: desc,
Config: conf,
})
}
// Compute constraint violations for the overall (affecting voters
// and non-voters alike) and voter constraints.
overall := constraint.AnalyzeConstraints(
r.dep.StoreResolver,
desc.Replicas().Descriptors(),
conf.NumReplicas, conf.Constraints)
for i, c := range overall.Constraints {
if c.NumReplicas == 0 {
// NB: This is a weird artifact of
// constraint.NumReplicas, which if set to zero is
// used to imply that the constraint will applies to
// all replicas. Setting it explicitly makes the
// code below less fragile.
c.NumReplicas = conf.NumReplicas
}
if len(overall.SatisfiedBy[i]) < int(c.NumReplicas) {
report.ViolatingConstraints = append(report.ViolatingConstraints,
roachpb.ConformanceReportedRange{
RangeDescriptor: desc,
Config: conf,
})
break
}
}
voters := constraint.AnalyzeConstraints(
r.dep.StoreResolver,
desc.Replicas().Voters().Descriptors(),
conf.GetNumVoters(), conf.VoterConstraints)
for i, c := range voters.Constraints {
if c.NumReplicas == 0 {
c.NumReplicas = conf.GetNumVoters()
}
if len(voters.SatisfiedBy[i]) < int(c.NumReplicas) {
report.ViolatingConstraints = append(report.ViolatingConstraints,
roachpb.ConformanceReportedRange{
RangeDescriptor: desc,
Config: conf,
})
break
}
}
}
return nil
}); err != nil {
return roachpb.SpanConfigConformanceReport{}, err
}
}
for nid := range unavailableNodes {
report.UnavailableNodeIDs = append(report.UnavailableNodeIDs, int32(nid))
}
return report, nil
}