-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
threshold.go
135 lines (119 loc) · 3.97 KB
/
threshold.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
// 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 allocatorimpl
import (
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/allocator"
"github.com/cockroachdb/cockroach/pkg/kv/kvserver/allocator/load"
"github.com/cockroachdb/cockroach/pkg/settings"
"github.com/cockroachdb/errors"
)
const (
// disableRebalancingThreshold declares a rebalancing threshold that
// tolerates significant imbalance.
disableRebalancingThreshold = 0
)
func getLoadThreshold(dim load.Dimension, sv *settings.Values) float64 {
switch dim {
case load.Queries:
return allocator.QPSRebalanceThreshold.Get(sv)
case load.CPU:
return allocator.CPURebalanceThreshold.Get(sv)
default:
panic(errors.AssertionFailedf("Unkown load dimension %d", dim))
}
}
// LoadThresholds returns the load threshold values for the passed in
// dimensions.
func LoadThresholds(sv *settings.Values, dims ...load.Dimension) load.Load {
thresholds := load.Vector{}
// Initially set each threshold to be disabled.
for i := range thresholds {
thresholds[i] = disableRebalancingThreshold
}
for _, dim := range dims {
thresholds[dim] = getLoadThreshold(dim, sv)
}
return thresholds
}
func getLoadMinThreshold(dim load.Dimension) float64 {
switch dim {
case load.Queries:
return allocator.MinQPSThresholdDifference
case load.CPU:
return allocator.MinCPUThresholdDifference
default:
panic(errors.AssertionFailedf("Unkown load dimension %d", dim))
}
}
// LoadMinThresholds returns the minimum absoute load amounts by which a
// candidate must differ from the mean before it is considered under or
// overfull.
func LoadMinThresholds(dims ...load.Dimension) load.Load {
diffs := load.Vector{}
// Initially set each threshold to be disabled.
for i := range diffs {
diffs[i] = disableRebalancingThreshold
}
for _, dim := range dims {
diffs[dim] = getLoadMinThreshold(dim)
}
return diffs
}
func getLoadRebalanceMinRequiredDiff(dim load.Dimension, sv *settings.Values) float64 {
switch dim {
case load.Queries:
return allocator.MinQPSDifferenceForTransfers.Get(sv)
case load.CPU:
return allocator.MinCPUDifferenceForTransfers
default:
panic(errors.AssertionFailedf("Unkown load dimension %d", dim))
}
}
// LoadRebalanceRequiredMinDiff returns the minimum absolute difference between
// an existing (store) and another candidate that must be met before a
// rebalance or transfer is allowed. This setting is purposed to prevent
// thrashing.
func LoadRebalanceRequiredMinDiff(sv *settings.Values, dims ...load.Dimension) load.Load {
diffs := load.Vector{}
// Initially set each threshold to be disabled.
for i := range diffs {
diffs[i] = disableRebalancingThreshold
}
for _, dim := range dims {
diffs[dim] = getLoadRebalanceMinRequiredDiff(dim, sv)
}
return diffs
}
// OverfullLoadThresholds returns the overfull load threshold for each load
// dimension.
func OverfullLoadThresholds(means, thresholds, minThresholds load.Load) load.Load {
return load.Add(means, load.Max(load.ElementWiseProduct(means, thresholds), minThresholds))
}
// UnderfullLoadThresholds returns the underfull load threshold for each load
// dimension.
func UnderfullLoadThresholds(means, thresholds, minThresholds load.Load) load.Load {
return load.Sub(means, load.Max(load.ElementWiseProduct(means, thresholds), minThresholds))
}
// MakeQPSOnlyDim returns a load dimension with only QPS filled in with the
// value given.
func MakeQPSOnlyDim(v float64) load.Load {
dims := load.Vector{}
dims[load.Queries] = v
return dims
}
// SetAllDims returns a load vector with all dimensions filled in with the
// value given.
func SetAllDims(v float64) load.Load {
dims := load.Vector{}
for i := range dims {
dims[i] = v
}
return dims
}