-
Notifications
You must be signed in to change notification settings - Fork 3.9k
/
Copy pathconfig_loader.go
497 lines (467 loc) · 12.8 KB
/
config_loader.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
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
// 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 state
import (
"fmt"
"strings"
"github.com/cockroachdb/cockroach/pkg/config/zonepb"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/asim/config"
"github.com/cockroachdb/cockroach/pkg/roachpb"
"google.golang.org/protobuf/proto"
)
var SingleRegionClusterOptions = [...]string{"single_region", "single_region_multi_store"}
var MultiRegionClusterOptions = [...]string{"multi_region", "complex"}
var AllClusterOptions = [...]string{"single_region", "single_region_multi_store", "multi_region", "complex"}
// TODO(kvoli): Add a loader/translator for the existing
// []*roachpb.StoreDescriptor configurations in kvserver/*_test.go and
// allocatorimpl/*_test.go.
// SingleRegionConfig is a simple cluster config with a single region and 3
// zones, all have the same number of nodes.
var SingleRegionConfig = ClusterInfo{
DiskCapacityGB: 1024,
Regions: []Region{
{
Name: "US",
Zones: []Zone{
{Name: "US_1", NodeCount: 5},
{Name: "US_2", NodeCount: 5},
{Name: "US_3", NodeCount: 5},
},
},
},
}
// SingleRegionMultiStoreConfig is a simple cluster config with a single region
// and 3 zones, all zones have 1 node and 6 stores per node.
var SingleRegionMultiStoreConfig = ClusterInfo{
DiskCapacityGB: 1024,
Regions: []Region{
{
Name: "US",
Zones: []Zone{
{Name: "US_1", NodeCount: 1, StoresPerNode: 5},
{Name: "US_2", NodeCount: 1, StoresPerNode: 5},
{Name: "US_3", NodeCount: 1, StoresPerNode: 5},
},
},
},
}
// MultiRegionConfig is a perfectly balanced cluster config with 3 regions.
var MultiRegionConfig = ClusterInfo{
DiskCapacityGB: 2048,
Regions: []Region{
{
Name: "US_East",
Zones: []Zone{
{Name: "US_East_1", NodeCount: 4},
{Name: "US_East_2", NodeCount: 4},
{Name: "US_East_3", NodeCount: 4},
},
},
{
Name: "US_West",
Zones: []Zone{
{Name: "US_West_1", NodeCount: 4},
{Name: "US_West_2", NodeCount: 4},
{Name: "US_West_3", NodeCount: 4},
},
},
{
Name: "EU",
Zones: []Zone{
{Name: "EU_1", NodeCount: 4},
{Name: "EU_2", NodeCount: 4},
{Name: "EU_3", NodeCount: 4},
},
},
},
}
// ComplexConfig is an imbalanced multi-region cluster config.
var ComplexConfig = ClusterInfo{
DiskCapacityGB: 2048,
Regions: []Region{
{
Name: "US_East",
Zones: []Zone{
{Name: "US_East_1", NodeCount: 1},
{Name: "US_East_2", NodeCount: 2},
{Name: "US_East_3", NodeCount: 3},
{Name: "US_East_4", NodeCount: 10},
},
},
{
Name: "US_West",
Zones: []Zone{
{Name: "US_West_1", NodeCount: 2},
},
},
{
Name: "EU",
Zones: []Zone{
{Name: "EU_1", NodeCount: 3},
{Name: "EU_2", NodeCount: 3},
{Name: "EU_3", NodeCount: 4},
},
},
},
}
// SingleRangeConfig is a single range config where there are 3 replicas on
// stores 1, 2 and 3. Store 1 is the leaseholder.
var SingleRangeConfig = []RangeInfo{
{
Descriptor: roachpb.RangeDescriptor{
StartKey: MinKey.ToRKey(),
EndKey: (MinKey + 1000).ToRKey(),
InternalReplicas: []roachpb.ReplicaDescriptor{
{
StoreID: 1,
},
{
StoreID: 2,
},
{
StoreID: 3,
},
},
},
Config: &defaultSpanConfig,
Leaseholder: 1,
},
}
// MultiRangeConfig is a ranges config where there are three ranges and stores
// 1,2,3 have replicas for each range. There is 1 leaseholder on each of store
// 1,2,3.
var MultiRangeConfig = []RangeInfo{
{
Descriptor: roachpb.RangeDescriptor{
StartKey: MinKey.ToRKey(),
InternalReplicas: []roachpb.ReplicaDescriptor{
{
StoreID: 1,
},
{
StoreID: 2,
},
{
StoreID: 3,
},
},
},
Config: &defaultSpanConfig,
Leaseholder: 1,
},
{
Descriptor: roachpb.RangeDescriptor{
StartKey: (MinKey + 1000).ToRKey(),
InternalReplicas: []roachpb.ReplicaDescriptor{
{
StoreID: 1,
},
{
StoreID: 2,
},
{
StoreID: 3,
},
},
},
Config: &defaultSpanConfig,
Leaseholder: 2,
},
{
Descriptor: roachpb.RangeDescriptor{
StartKey: (MinKey + 2000).ToRKey(),
InternalReplicas: []roachpb.ReplicaDescriptor{
{
StoreID: 1,
},
{
StoreID: 2,
},
{
StoreID: 3,
},
},
},
Config: &defaultSpanConfig,
Leaseholder: 3,
},
}
// GetClusterInfo returns ClusterInfo for a given configName and panics if no
// match is found in existing configurations.
func GetClusterInfo(configName string) ClusterInfo {
switch configName {
case "single_region":
return SingleRegionConfig
case "single_region_multi_store":
return SingleRegionMultiStoreConfig
case "multi_region":
return MultiRegionConfig
case "complex":
return ComplexConfig
default:
panic(fmt.Sprintf("no matching cluster info found for %s", configName))
}
}
// RangeInfoWithReplicas returns a new RangeInfo using the supplied arguments.
func RangeInfoWithReplicas(
startKey Key, voters, nonVoters []StoreID, leaseholder StoreID, config *roachpb.SpanConfig,
) RangeInfo {
desc := roachpb.RangeDescriptor{
StartKey: startKey.ToRKey(),
InternalReplicas: make([]roachpb.ReplicaDescriptor, len(voters)+len(nonVoters)),
}
for i, storeID := range voters {
desc.InternalReplicas[i] = roachpb.ReplicaDescriptor{
StoreID: roachpb.StoreID(storeID),
Type: roachpb.VOTER_FULL,
}
}
for i, storeID := range nonVoters {
desc.InternalReplicas[i+len(voters)] = roachpb.ReplicaDescriptor{
StoreID: roachpb.StoreID(storeID),
Type: roachpb.NON_VOTER,
}
}
return RangeInfo{Descriptor: desc, Leaseholder: leaseholder, Config: config}
}
// Zone is a simulated availability zone. When StoresPerNode is 0, a default
// value of 1 store per node is used instead.
type Zone struct {
Name string
NodeCount int
StoresPerNode int
}
// Region is a simulated region which contains one or more zones.
type Region struct {
Name string
Zones []Zone
}
// ClusterInfo contains cluster information needed for allocation decisions.
// TODO(lidor): add cross region network latencies.
type ClusterInfo struct {
DiskCapacityGB int
Regions []Region
}
func (c ClusterInfo) String() (s string) {
buf := &strings.Builder{}
for i, r := range c.Regions {
buf.WriteString(fmt.Sprintf("\t\tregion:%s [", r.Name))
if len(r.Zones) == 0 {
panic(fmt.Sprintf("number of zones within region %s is zero", r.Name))
}
for j, z := range r.Zones {
buf.WriteString(fmt.Sprintf("zone=%s(nodes=%d,stores=%d)", z.Name, z.NodeCount, z.StoresPerNode))
if j != len(r.Zones)-1 {
buf.WriteString(", ")
}
}
buf.WriteString("]")
if i != len(c.Regions)-1 {
buf.WriteString("\n")
}
}
return buf.String()
}
type RangeInfo struct {
Descriptor roachpb.RangeDescriptor
Config *roachpb.SpanConfig
Size int64
Leaseholder StoreID
}
type RangesInfo []RangeInfo
// LoadConfig loads a predefined configuration which contains cluster
// information, range info and initial replica/lease placement.
func LoadConfig(c ClusterInfo, r RangesInfo, settings *config.SimulationSettings) State {
s := LoadClusterInfo(c, settings)
LoadRangeInfo(s, r...)
return s
}
// LoadClusterInfo loads a predefined configuration which contains cluster
// information such as regions, zones, etc.
func LoadClusterInfo(c ClusterInfo, settings *config.SimulationSettings) State {
s := newState(settings)
// A new state has a single range - add the replica load for that range.
s.clusterinfo = c
for _, r := range c.Regions {
regionTier := roachpb.Tier{
Key: "region",
Value: r.Name,
}
for _, z := range r.Zones {
zoneTier := roachpb.Tier{
Key: "zone",
Value: z.Name,
}
locality := roachpb.Locality{
Tiers: []roachpb.Tier{regionTier, zoneTier},
}
for i := 0; i < z.NodeCount; i++ {
node := s.AddNode()
s.SetNodeLocality(node.NodeID(), locality)
storesRequired := z.StoresPerNode
if storesRequired < 1 {
storesRequired = 1
}
for store := 0; store < storesRequired; store++ {
if newStore, ok := s.AddStore(node.NodeID()); !ok {
panic(fmt.Sprintf(
"Unable to load config: cannot add store %d",
node.NodeID(),
))
} else {
s.SetStoreCapacity(newStore.StoreID(), int64(c.DiskCapacityGB)*1<<30)
}
}
}
}
}
return s
}
// LoadRangeInfo loads the ranges specified in RangesInfo into state. If any
// operation fails this function panics.
func LoadRangeInfo(s State, rangeInfos ...RangeInfo) {
for _, r := range rangeInfos {
var rng Range
var ok bool
// Use the default span config if not set in the configuration.
if r.Config == nil {
copiedDefaultConfig := defaultSpanConfig
r.Config = &copiedDefaultConfig
}
// When the state is initialized there will always be at least one
// range that spans the entire keyspace. All other ranges are split off
// of this one. If the range info has a start key that is equal to
// MinKey, then we assume that this refers to the start range - use
// that existing range rather than splitting.
startKey := ToKey(r.Descriptor.StartKey.AsRawKey())
if startKey == MinKey {
rng, ok = s.RangeFor(startKey), true
} else {
_, rng, ok = s.SplitRange(startKey)
}
if !ok {
panic(fmt.Sprintf(
"Unable to load config: failed create range %d",
startKey,
))
}
if !s.SetSpanConfigForRange(rng.RangeID(), *r.Config) {
panic(fmt.Sprintf(
"Unable to load config: cannot set span config for range %s",
rng,
))
}
}
// Create the replicas for each range and transfer the range lease to the
// specified leaseholder. If this were done in the above loop, it would be
// necessary to delete all existing replicas that were carried over from
// the lhs split, before adding the new replicas to the rhs.
for _, r := range rangeInfos {
startKey := ToKey(r.Descriptor.StartKey.AsRawKey())
rng := s.RangeFor(startKey)
s.SetRangeBytes(rng.RangeID(), r.Size)
for _, desc := range r.Descriptor.InternalReplicas {
if _, ok := s.AddReplica(rng.RangeID(), StoreID(desc.StoreID), desc.Type); !ok {
panic(fmt.Sprintf(
"Unable to load config: add replica to store %d failed at "+
"for range %s replicas %s",
desc.StoreID, rng, rng.Replicas()))
}
}
if store, _ := s.LeaseholderStore(rng.RangeID()); store.StoreID() != r.Leaseholder {
if !s.TransferLease(rng.RangeID(), r.Leaseholder) {
panic(fmt.Sprintf(
"Unable to load config: transfer lease to %d failed at for range %s",
r.Leaseholder, rng))
}
}
}
}
func GetRegionSurvivalConfig(
regionOne string, regionTwo string, regionThree string,
) zonepb.ZoneConfig {
zoneConfig := zonepb.DefaultZoneConfig()
zoneConfig.NumReplicas = proto.Int32(5)
zoneConfig.NumVoters = proto.Int32(5)
zoneConfig.LeasePreferences = []zonepb.LeasePreference{
{
Constraints: []zonepb.Constraint{
{Type: zonepb.Constraint_REQUIRED, Key: "region", Value: regionOne}},
},
}
zoneConfig.Constraints = []zonepb.ConstraintsConjunction{
{
NumReplicas: 1,
Constraints: []zonepb.Constraint{
{Type: zonepb.Constraint_REQUIRED, Key: "region", Value: regionOne},
},
},
{
NumReplicas: 1,
Constraints: []zonepb.Constraint{
{Type: zonepb.Constraint_REQUIRED, Key: "region", Value: regionTwo},
},
},
{
NumReplicas: 1,
Constraints: []zonepb.Constraint{
{Type: zonepb.Constraint_REQUIRED, Key: "region", Value: regionThree},
},
},
}
zoneConfig.VoterConstraints = []zonepb.ConstraintsConjunction{
{
NumReplicas: 2,
Constraints: []zonepb.Constraint{
{Type: zonepb.Constraint_REQUIRED, Key: "region", Value: regionOne},
},
},
}
return zoneConfig
}
func GetZoneSurvivalConfig(
regionOne string, regionTwo string, regionThree string,
) zonepb.ZoneConfig {
zoneConfig := zonepb.DefaultZoneConfig()
zoneConfig.NumReplicas = proto.Int32(5)
zoneConfig.NumVoters = proto.Int32(3)
zoneConfig.LeasePreferences = []zonepb.LeasePreference{{
Constraints: []zonepb.Constraint{
{Type: zonepb.Constraint_REQUIRED, Key: "region", Value: regionOne},
},
}}
zoneConfig.Constraints = []zonepb.ConstraintsConjunction{
{
NumReplicas: 1,
Constraints: []zonepb.Constraint{
{Type: zonepb.Constraint_REQUIRED, Key: "region", Value: regionOne},
},
},
{
NumReplicas: 1,
Constraints: []zonepb.Constraint{
{Type: zonepb.Constraint_REQUIRED, Key: "region", Value: regionTwo},
},
},
{
NumReplicas: 1,
Constraints: []zonepb.Constraint{
{Type: zonepb.Constraint_REQUIRED, Key: "region", Value: regionThree},
},
},
}
zoneConfig.VoterConstraints = []zonepb.ConstraintsConjunction{{
Constraints: []zonepb.Constraint{
{Type: zonepb.Constraint_REQUIRED, Key: "region", Value: regionOne},
},
},
}
return zoneConfig
}