Skip to content

Commit 08e6cec

Browse files
authored
factor: check the metric interval for CPU and memory-based balance (#613)
1 parent 0d7661a commit 08e6cec

File tree

5 files changed

+85
-13
lines changed

5 files changed

+85
-13
lines changed

pkg/balance/factor/factor_cpu.go

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -50,17 +50,21 @@ var (
5050
if len(pairs) < 2 {
5151
return model.SampleValue(math.NaN())
5252
}
53-
pair1 := pairs[len(pairs)-2]
54-
pair2 := pairs[len(pairs)-1]
55-
timeDiff := float64(pair2.Timestamp-pair1.Timestamp) / 1000.0
56-
if timeDiff < 1e-4 {
57-
return model.SampleValue(math.NaN())
58-
}
59-
// Maybe the backend just rebooted.
60-
if pair1.Value > pair2.Value {
61-
return model.SampleValue(math.NaN())
53+
lastPair := pairs[len(pairs)-1]
54+
for i := len(pairs) - 2; i >= 0; i-- {
55+
pair := pairs[i]
56+
// Skip this one if the interval is too short.
57+
seconds := float64(lastPair.Timestamp-pair.Timestamp) / 1000.0
58+
if seconds < 1 {
59+
continue
60+
}
61+
// Maybe the backend just rebooted.
62+
if pair.Value > lastPair.Value {
63+
return model.SampleValue(math.NaN())
64+
}
65+
return (lastPair.Value - pair.Value) / model.SampleValue(seconds)
6266
}
63-
return (pair2.Value - pair1.Value) / model.SampleValue(timeDiff)
67+
return model.SampleValue(math.NaN())
6468
},
6569
ResultType: model.ValMatrix,
6670
}

pkg/balance/factor/factor_cpu_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,14 @@ tidb_server_maxprocs 2
338338
curValue: 1,
339339
finalValue: model.SampleValue(math.NaN()),
340340
},
341+
{
342+
text: `process_cpu_seconds_total 3
343+
tidb_server_maxprocs 2
344+
`,
345+
timestamp: model.Time(3500),
346+
curValue: 1.5,
347+
finalValue: model.SampleValue(math.NaN()),
348+
},
341349
}
342350

343351
historyPair := make([]model.SamplePair, 0)

pkg/balance/factor/factor_memory.go

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -204,9 +204,14 @@ func calcMemUsage(usageHistory []model.SamplePair) (latestUsage float64, timeToO
204204
latestUsage = value
205205
latestTime = usageHistory[i].Timestamp
206206
} else {
207-
diff := latestUsage - value
208-
if diff > 0.0001 {
209-
timeToOOM = time.Duration(float64(latestTime-usageHistory[i].Timestamp)*(oomMemoryUsage-latestUsage)/diff) * time.Millisecond
207+
timeDiff := latestTime.Sub(usageHistory[i].Timestamp)
208+
if timeDiff < 10*time.Second {
209+
// Skip this one is the interval is too short.
210+
continue
211+
}
212+
usageDiff := latestUsage - value
213+
if usageDiff > 1e-4 {
214+
timeToOOM = timeDiff * time.Duration((oomMemoryUsage-latestUsage)/usageDiff)
210215
}
211216
break
212217
}

pkg/balance/factor/factor_memory_test.go

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,53 @@ func TestMemoryScore(t *testing.T) {
109109
}
110110
}
111111

112+
func TestMemoryUsage(t *testing.T) {
113+
tests := []struct {
114+
memory []float64
115+
ts []model.Time
116+
lastUsage float64
117+
timeToOOM time.Duration
118+
}{
119+
{
120+
memory: []float64{0.2, 0.3},
121+
ts: []model.Time{model.Time(15000), model.Time(30000)},
122+
lastUsage: 0.3,
123+
timeToOOM: 90 * time.Second,
124+
},
125+
{
126+
memory: []float64{0.2, 0.3, 0.3},
127+
ts: []model.Time{model.Time(15000), model.Time(30000), model.Time(31000)},
128+
lastUsage: 0.3,
129+
timeToOOM: 96 * time.Second,
130+
},
131+
{
132+
memory: []float64{0.3, 0.3},
133+
ts: []model.Time{model.Time(30000), model.Time(31000)},
134+
lastUsage: 0.3,
135+
timeToOOM: time.Duration(math.MaxInt),
136+
},
137+
{
138+
memory: []float64{0.3, 0.3},
139+
ts: []model.Time{model.Time(30000), model.Time(45000)},
140+
lastUsage: 0.3,
141+
timeToOOM: time.Duration(math.MaxInt),
142+
},
143+
{
144+
memory: []float64{0.3, 0.2},
145+
ts: []model.Time{model.Time(30000), model.Time(45000)},
146+
lastUsage: 0.2,
147+
timeToOOM: time.Duration(math.MaxInt),
148+
},
149+
}
150+
151+
for i, test := range tests {
152+
pairs := createPairs(test.memory, test.ts)
153+
latestUsage, timeToOOM := calcMemUsage(pairs)
154+
require.Equal(t, test.lastUsage, latestUsage, "test index %d", i)
155+
require.Equal(t, test.timeToOOM, timeToOOM, "test index %d", i)
156+
}
157+
}
158+
112159
func TestMemoryBalance(t *testing.T) {
113160
tests := []struct {
114161
memory [][]float64

pkg/balance/factor/mock_test.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,14 @@ func createSampleStream(values []float64, backendIdx int, curTime model.Time) *m
150150
return &model.SampleStream{Metric: labelSet, Values: pairs}
151151
}
152152

153+
func createPairs(values []float64, ts []model.Time) []model.SamplePair {
154+
pairs := make([]model.SamplePair, 0, len(values))
155+
for i, value := range values {
156+
pairs = append(pairs, model.SamplePair{Timestamp: ts[i], Value: model.SampleValue(value)})
157+
}
158+
return pairs
159+
}
160+
153161
func createSample(value float64, backendIdx int) *model.Sample {
154162
host := strconv.Itoa(backendIdx)
155163
labelSet := model.Metric{metricsreader.LabelNameInstance: model.LabelValue(host + ":10080")}

0 commit comments

Comments
 (0)