Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions dm/syncer/syncer.go
Original file line number Diff line number Diff line change
Expand Up @@ -545,11 +545,28 @@ func (s *Syncer) Init(ctx context.Context) (err error) {
metricProxies.Init(s.cfg.MetricsFactory)
}
s.metricsProxies = metricProxies.CacheForOneTask(s.cfg.Name, s.cfg.WorkerName, s.cfg.SourceID)
s.initSyncerBinlogMetrics(s.checkpoint.GlobalPoint())

s.ddlWorker = NewDDLWorker(&s.tctx.Logger, s)
return nil
}

func (s *Syncer) initSyncerBinlogMetrics(checkpoint binlog.Location) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 [Minor] Initializer name contradicts the helper's refresh behavior

Why
The helper is not limited to initialization: Run deliberately calls it again after the effective global checkpoint may change. Naming this repeated gauge update initSyncerBinlogMetrics obscures that it is safe and intended to overwrite existing metric values.

Scope
dm/syncer/syncer.go:554

Risk if unchanged
Future callers may treat the helper as a one-time setup operation, miss required refreshes after checkpoint changes, or add initialization-only work that is unsafe on the second call.

Evidence
The new comment at dm/syncer/syncer.go:1857 explicitly says to "Refresh" the metrics, but line 1860 invokes initSyncerBinlogMetrics; the same method is first called from Init at line 548, and the test name TestInitSyncerBinlogMetrics reinforces the one-time interpretation.

Change request
Prefer setSyncerBinlogMetrics or updateSyncerBinlogMetrics; the current name is confusing. Rename the helper and its test so both initialization and later refresh call sites describe the same repeated update semantics.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense, I will fix it.

s.metricsProxies.Metrics.BinlogSyncerPosGauge.Set(float64(checkpoint.Position.Pos))

index, err := utils.GetFilenameIndex(checkpoint.Position.Name)
if err != nil {
// An empty binlog filename is expected for a fresh or GTID-only checkpoint.
// Use NaN so the unknown file number is not exposed as a valid zero value.
s.metricsProxies.Metrics.BinlogSyncerFileGauge.Set(math.NaN())
if checkpoint.Position.Name != "" {
s.tctx.L().Warn("fail to get index number of checkpoint binlog file", log.ShortError(err))
}
return
}
s.metricsProxies.Metrics.BinlogSyncerFileGauge.Set(float64(index))
}

// buildLowerCaseTableNamesMap build a lower case schema map and lower case table map for all tables
// Input: map of schema --> list of tables
// Output: schema names map: lower_case_schema_name --> schema_name
Expand Down
92 changes: 92 additions & 0 deletions dm/syncer/syncer_metrics_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// Copyright 2026 PingCAP, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// See the License for the specific language governing permissions and
// limitations under the License.

package syncer

import (
"math"
"testing"

"github.com/go-mysql-org/go-mysql/mysql"
"github.com/pingcap/tiflow/dm/pkg/binlog"
"github.com/pingcap/tiflow/dm/pkg/gtid"
"github.com/pingcap/tiflow/dm/syncer/metrics"
"github.com/prometheus/client_golang/prometheus"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/require"
)

func TestInitSyncerBinlogMetrics(t *testing.T) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 [Minor] Checkpoint-to-metric lifecycle wiring is not covered

Why
The changed behavior depends on publishing the loaded checkpoint in Init and then republishing the final start checkpoint after metadata, start-time, and GTID adjustments in Run, but the new test validates only the conversion helper in isolation.

Scope
dm/syncer/syncer_metrics_test.go:30; dm/syncer/syncer.go:548; dm/syncer/syncer.go:1860

Risk if unchanged
A missing or misplaced lifecycle call can leave an idle or newly started task reporting zero, NaN, or a stale persisted checkpoint until its first binlog event, while this regression test still passes and the intended observability fix is lost.

Evidence
TestInitSyncerBinlogMetrics constructs standalone gauges and invokes s.initSyncerBinlogMetrics(tc.checkpoint) directly; it never exercises Syncer.Init, LoadMeta, setGlobalPointByTime, adjustGlobalPointGTID, or either production call site.

Change request
Please add an integration test for this path that asserts the exposed gauges after Init loads a persisted checkpoint and after Run selects a different metadata, start-time, or GTID-adjusted checkpoint before any binlog event is consumed.

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Make sense, I will fix it.

gtidSet, err := gtid.ParserGTID(
mysql.MySQLFlavor,
"3ccc475b-2343-11e7-be21-6c0b84d59f30:1-3",
)
require.NoError(t, err)

testCases := []struct {
name string
checkpoint binlog.Location
expectedFile float64
expectedPos float64
}{
{
name: "file position checkpoint",
checkpoint: binlog.NewLocation(mysql.Position{
Name: "binary-log.346652",
Pos: 560567,
}, nil),
expectedFile: 346652,
expectedPos: 560567,
},
{
name: "missing position",
checkpoint: binlog.MustZeroLocation(mysql.MySQLFlavor),
expectedFile: math.NaN(),
expectedPos: float64(binlog.MinPosition.Pos),
},
{
name: "GTID-only checkpoint",
checkpoint: binlog.NewLocation(
mysql.Position{},
gtidSet,
),
expectedFile: math.NaN(),
expectedPos: 0,
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
fileGauge := prometheus.NewGauge(prometheus.GaugeOpts{Name: "syncer_binlog_file"})
posGauge := prometheus.NewGauge(prometheus.GaugeOpts{Name: "syncer_binlog_pos"})
s := &Syncer{
metricsProxies: &metrics.Proxies{
Metrics: &metrics.Metrics{
BinlogSyncerFileGauge: fileGauge,
BinlogSyncerPosGauge: posGauge,
},
},
}

s.initSyncerBinlogMetrics(tc.checkpoint)

actualFile := testutil.ToFloat64(fileGauge)
if math.IsNaN(tc.expectedFile) {
require.True(t, math.IsNaN(actualFile))
} else {
require.Equal(t, tc.expectedFile, actualFile)
}
require.Equal(t, tc.expectedPos, testutil.ToFloat64(posGauge))
})
}
}
Loading