Skip to content

Commit 8e2a106

Browse files
balance: fix TiDB regionMiss backoff causes TiProxy to incorrectly migrate connections (#832) (#834)
Co-authored-by: djshow832 <[email protected]>
1 parent efb4014 commit 8e2a106

File tree

7 files changed

+362
-248
lines changed

7 files changed

+362
-248
lines changed

pkg/balance/factor/factor_balance.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -279,18 +279,17 @@ func (fbb *FactorBasedBalance) BackendsToBalance(backends []policy.BackendCtx) (
279279
if score1 >= score2 {
280280
// The factors with higher priorities are ordered, so this factor shouldn't violate them.
281281
// E.g. if the CPU usage of A is higher than B, don't migrate from B to A even if A is preferred in location.
282-
var advice BalanceAdvice
283-
var fields []zap.Field
284-
advice, balanceCount, fields = factor.BalanceCount(scoredBackends[i], scoredBackends[0])
282+
advice, count, fields := factor.BalanceCount(scoredBackends[i], scoredBackends[0])
285283
if advice == AdviceNegtive {
286284
// If the factor will be unbalanced after migration, skip the rest factors.
287285
// E.g. if the CPU usage of A will be much higher than B after migration,
288286
// don't migrate from B to A even if A is preferred in location.
289287
break
290288
}
291289
logFields = append(logFields, fields...)
292-
if score1 > score2 && advice == AdvicePositive && balanceCount > 0.0001 {
290+
if score1 > score2 && advice == AdvicePositive && count > 0.0001 {
293291
from, to = scoredBackends[i].BackendCtx, scoredBackends[0].BackendCtx
292+
balanceCount = count
294293
reason = factor.Name()
295294
logFields = append(logFields, zap.String("factor", reason),
296295
zap.String("from_total_score", strconv.FormatUint(scoredBackends[i].scoreBits, 16)),

pkg/balance/factor/factor_cpu.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,12 @@ var (
4141
Names: []string{"process_cpu_seconds_total", "tidb_server_maxprocs"},
4242
Retention: 1 * time.Minute,
4343
Metric2Value: func(mfs map[string]*dto.MetricFamily) model.SampleValue {
44+
if mfs["process_cpu_seconds_total"] == nil || len(mfs["process_cpu_seconds_total"].Metric) == 0 {
45+
return model.SampleValue(math.NaN())
46+
}
47+
if mfs["tidb_server_maxprocs"] == nil || len(mfs["tidb_server_maxprocs"].Metric) == 0 {
48+
return model.SampleValue(math.NaN())
49+
}
4450
cpuTotal := mfs["process_cpu_seconds_total"].Metric[0].Untyped
4551
maxProcs := mfs["tidb_server_maxprocs"].Metric[0].Untyped
4652
if cpuTotal == nil || maxProcs == nil {

0 commit comments

Comments
 (0)