Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion cmd/kafka-consumer/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func newWriter(ctx context.Context, o *option) *writer {
SinkURI: o.downstreamURI,
SinkConfig: o.sinkConfig,
}
w.mysqlSink, err = sink.New(ctx, cfg, changefeedID)
w.mysqlSink, err = sink.New(ctx, cfg, changefeedID, commonType.DefaultKeyspaceID)
if err != nil {
log.Panic("cannot create the mysql sink", zap.Error(err))
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/pulsar-consumer/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func newWriter(ctx context.Context, o *option) *writer {
SinkURI: o.downstreamURI,
SinkConfig: o.replicaConfig.Sink,
}
w.mysqlSink, err = sink.New(ctx, cfg, changefeedID)
w.mysqlSink, err = sink.New(ctx, cfg, changefeedID, commonType.DefaultKeyspaceID)
if err != nil {
log.Panic("cannot create the mysql sink", zap.Error(err))
}
Expand Down
7 changes: 6 additions & 1 deletion cmd/storage-consumer/consumer.go
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,12 @@ func newConsumer(ctx context.Context) (*consumer, error) {
SinkURI: downstreamURIStr,
SinkConfig: replicaConfig.Sink,
}
sink, err := sink.New(stdCtx, cfg, commonType.NewChangeFeedIDWithName(defaultChangefeedName, commonType.DefaultKeyspaceName))
sink, err := sink.New(
stdCtx,
cfg,
commonType.NewChangeFeedIDWithName(defaultChangefeedName, commonType.DefaultKeyspaceName),
commonType.DefaultKeyspaceID,
)
if err != nil {
log.Error("failed to create sink", zap.Error(err))
return nil, err
Expand Down
12 changes: 10 additions & 2 deletions coordinator/changefeed/changefeed_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,8 +150,16 @@ func (db *ChangefeedDB) StopByChangefeedID(cfID common.ChangeFeedID, remove bool
db.stopped[cfID] = cf
}

metrics.ChangefeedStatusGauge.DeleteLabelValues(cfID.Keyspace(), cfID.Name())
metrics.DeleteChangefeedCheckpointMetrics(cfID.Keyspace(), cfID.Name())
keyspaceID := common.DefaultKeyspaceID
if info := cf.GetInfo(); info != nil {
keyspaceID = info.KeyspaceID
}
metrics.ChangefeedStatusGauge.DeleteLabelValues(
cfID.Keyspace(),
cfID.Name(),
metrics.FormatKeyspaceID(keyspaceID),
)
Comment thread
asddongmen marked this conversation as resolved.
Resolved
metrics.DeleteChangefeedCheckpointMetrics(cfID.Keyspace(), cfID.Name(), keyspaceID)

return nodeID
}
Expand Down
14 changes: 12 additions & 2 deletions coordinator/changefeed/changefeed_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,26 @@ func TestStopByChangefeedIDDeletesCheckpointMetrics(t *testing.T) {
t.Cleanup(metrics.ResetOwnerChangefeedMetrics)

db := NewChangefeedDB(1216)
cf := &Changefeed{ID: common.NewChangeFeedIDWithName("test-metrics", common.DefaultKeyspaceName)}
cfID := common.NewChangeFeedIDWithName("test-metrics", common.DefaultKeyspaceName)
cf := NewChangefeed(cfID, &config.ChangeFeedInfo{
ChangefeedID: cfID,
KeyspaceID: 123,
Config: config.GetDefaultReplicaConfig(),
SinkURI: "blackhole://",
State: config.StateNormal,
}, 100, false)
db.AddReplicatingMaintainer(cf, "node1")

metrics.ChangefeedStatusGauge.WithLabelValues(cf.ID.Keyspace(), cf.ID.Name(), "123").Set(1)
metrics.ChangefeedCheckpointTsGauge.WithLabelValues(cf.ID.Keyspace(), cf.ID.Name()).Set(100)
metrics.ChangefeedCheckpointTsLagGauge.WithLabelValues(cf.ID.Keyspace(), cf.ID.Name()).Set(10)
metrics.ChangefeedCheckpointTsLagGauge.WithLabelValues(cf.ID.Keyspace(), cf.ID.Name(), "123").Set(10)
require.Equal(t, 1, testutil.CollectAndCount(metrics.ChangefeedStatusGauge))
require.Equal(t, 1, testutil.CollectAndCount(metrics.ChangefeedCheckpointTsGauge))
require.Equal(t, 1, testutil.CollectAndCount(metrics.ChangefeedCheckpointTsLagGauge))

require.Equal(t, node.ID("node1"), db.StopByChangefeedID(cf.ID, true))

require.Equal(t, 0, testutil.CollectAndCount(metrics.ChangefeedStatusGauge))
require.Equal(t, 0, testutil.CollectAndCount(metrics.ChangefeedCheckpointTsGauge))
require.Equal(t, 0, testutil.CollectAndCount(metrics.ChangefeedCheckpointTsLagGauge))
}
Expand Down
16 changes: 13 additions & 3 deletions coordinator/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -264,11 +264,16 @@ func (c *Controller) collectMetrics(ctx context.Context) error {
changefeedDownstreamTypeCache[displayName] = downstreamType
metrics.ChangefeedDownstreamInfoGauge.WithLabelValues(keyspace, name, downstreamType).Set(1)

metrics.ChangefeedStatusGauge.WithLabelValues(keyspace, name).Set(float64(info.State.ToInt()))
metrics.ChangefeedStatusGauge.WithLabelValues(
keyspace,
name,
metrics.FormatKeyspaceID(info.KeyspaceID),
).Set(float64(info.State.ToInt()))
Comment thread
asddongmen marked this conversation as resolved.
Resolved

if !updateChangefeedCheckpointMetrics(
keyspace,
name,
info.KeyspaceID,
info.State,
cf.GetLastSavedCheckPointTs(),
c.pdClock.CurrentTime(),
Expand Down Expand Up @@ -324,21 +329,26 @@ func (c *Controller) collectMetrics(ctx context.Context) error {
func updateChangefeedCheckpointMetrics(
keyspace string,
name string,
keyspaceID uint32,
state config.FeedState,
checkpointTs uint64,
pdTime time.Time,
) bool {
switch state {
case config.StateStopped, config.StateFinished, config.StateRemoved:
metrics.DeleteChangefeedCheckpointMetrics(keyspace, name)
metrics.DeleteChangefeedCheckpointMetrics(keyspace, name, keyspaceID)
return false
}

pdPhysicalTime := oracle.GetPhysical(pdTime)
phyCkpTs := oracle.ExtractPhysical(checkpointTs)
lag := float64(pdPhysicalTime-phyCkpTs) / 1e3
metrics.ChangefeedCheckpointTsGauge.WithLabelValues(keyspace, name).Set(float64(phyCkpTs))
metrics.ChangefeedCheckpointTsLagGauge.WithLabelValues(keyspace, name).Set(lag)
metrics.ChangefeedCheckpointTsLagGauge.WithLabelValues(
keyspace,
name,
metrics.FormatKeyspaceID(keyspaceID),
).Set(lag)
Comment thread
asddongmen marked this conversation as resolved.
Resolved
return true
}

Expand Down
3 changes: 3 additions & 0 deletions coordinator/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,14 @@ func TestUpdateChangefeedCheckpointMetricsDeletesFinishedLabels(t *testing.T) {

keyspace := common.DefaultKeyspaceName
name := "finished-metrics"
keyspaceID := uint32(123)
pdTime := time.UnixMilli(2000)
checkpointTs := oracle.ComposeTS(1000, 0)

require.True(t, updateChangefeedCheckpointMetrics(
keyspace,
name,
keyspaceID,
config.StateNormal,
checkpointTs,
pdTime,
Expand All @@ -75,6 +77,7 @@ func TestUpdateChangefeedCheckpointMetricsDeletesFinishedLabels(t *testing.T) {
require.False(t, updateChangefeedCheckpointMetrics(
keyspace,
name,
keyspaceID,
config.StateFinished,
checkpointTs,
pdTime,
Expand Down
2 changes: 1 addition & 1 deletion downstreamadapter/dispatchermanager/dispatcher_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -283,7 +283,7 @@ func NewDispatcherManager(
}
}

createdSink, err := sink.New(ctx, manager.config, manager.changefeedID)
createdSink, err := sink.New(ctx, manager.config, manager.changefeedID, manager.keyspaceID)
if err != nil {
return nil, errors.Trace(err)
}
Expand Down
4 changes: 2 additions & 2 deletions downstreamadapter/sink/blackhole/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ type Sink struct {
statistics *metrics.Statistics
}

func New(changefeedID common.ChangeFeedID) (*Sink, error) {
func New(changefeedID common.ChangeFeedID, keyspaceID uint32) (*Sink, error) {
return &Sink{
eventCh: chann.NewUnlimitedChannelDefault[*commonEvent.DMLEvent](),
statistics: metrics.NewStatistics(changefeedID, "sink"),
statistics: metrics.NewStatistics(changefeedID, keyspaceID, "sink"),
}, nil
}

Expand Down
4 changes: 2 additions & 2 deletions downstreamadapter/sink/blackhole/sink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import (

// Test callback and tableProgress works as expected after AddDMLEvent
func TestBlacHoleSinkBasicFunctionality(t *testing.T) {
sink, err := New(common.NewChangefeedID(common.DefaultKeyspaceName))
sink, err := New(common.NewChangefeedID(common.DefaultKeyspaceName), common.DefaultKeyspaceID)
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go sink.Run(ctx)
Expand Down Expand Up @@ -91,7 +91,7 @@ func TestBlacHoleSinkBasicFunctionality(t *testing.T) {
}

func TestBlackHoleSinkBatchConfig(t *testing.T) {
sink, err := New(common.NewChangefeedID(common.DefaultKeyspaceName))
sink, err := New(common.NewChangefeedID(common.DefaultKeyspaceName), common.DefaultKeyspaceID)
require.NoError(t, err)
require.Equal(t, 4096, sink.BatchCount())
require.Zero(t, sink.BatchBytes())
Expand Down
3 changes: 2 additions & 1 deletion downstreamadapter/sink/cloudstorage/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,7 @@ func Verify(ctx context.Context, changefeedID common.ChangeFeedID, sinkURI *url.
func New(
ctx context.Context, changefeedID common.ChangeFeedID, sinkURI *url.URL, sinkConfig *config.SinkConfig, enableTableAcrossNodes bool,
cleanupJobs []func(), /* only for test */
keyspaceID uint32,
) (*sink, error) {
// create cloud storage config and then apply the params of sinkURI to it.
cfg := cloudstorage.NewConfig()
Expand All @@ -126,7 +127,7 @@ func New(
if err != nil {
return nil, err
}
statistics := metrics.NewStatistics(changefeedID, "cloudstorage")
statistics := metrics.NewStatistics(changefeedID, keyspaceID, "cloudstorage")
defer func() {
if err != nil {
statistics.Close()
Expand Down
4 changes: 2 additions & 2 deletions downstreamadapter/sink/cloudstorage/sink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func newSinkForTest(
cleanUpJobs []func(),
) (*sink, error) {
changefeedID := common.NewChangefeedID4Test("test", "test")
result, err := New(ctx, changefeedID, sinkURI, replicaConfig.Sink, true, cleanUpJobs)
result, err := New(ctx, changefeedID, sinkURI, replicaConfig.Sink, true, cleanUpJobs, common.DefaultKeyspaceID)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -667,7 +667,7 @@ func TestCloseBeforeRunDoesNotPanicAndCleansSpool(t *testing.T) {
setPDClockForTest(t, pdutil.NewClock4Test())

changefeedID := common.NewChangefeedID4Test("test", "close-before-run")
cloudStorageSink, err := New(ctx, changefeedID, sinkURI, replicaConfig.Sink, true, nil)
cloudStorageSink, err := New(ctx, changefeedID, sinkURI, replicaConfig.Sink, true, nil, common.DefaultKeyspaceID)
require.NoError(t, err)

spoolDir := filepath.Join(spoolBaseDir, changefeedID.Keyspace(), changefeedID.Name())
Expand Down
8 changes: 4 additions & 4 deletions downstreamadapter/sink/cloudstorage/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func testWriter(ctx context.Context, t *testing.T, dir string) *writer {
require.NoError(t, err)

changefeedID := commonType.NewChangefeedID4Test("test", t.Name())
statistics := metrics.NewStatistics(changefeedID, t.Name())
statistics := metrics.NewStatistics(changefeedID, commonType.DefaultKeyspaceID, t.Name())
spoolBuffer := newTestSpool(t, changefeedID, cfg)
d := newWriter(1, changefeedID, storage,
cfg, ".json", statistics, spoolBuffer)
Expand Down Expand Up @@ -483,7 +483,7 @@ func TestWriterStoresPendingMessagesInSpoolBeforeFlush(t *testing.T) {
cfg.FlushInterval = time.Hour

changefeedID := commonType.NewChangefeedID4Test("test", "spool-pending")
statistics := metrics.NewStatistics(changefeedID, t.Name())
statistics := metrics.NewStatistics(changefeedID, commonType.DefaultKeyspaceID, t.Name())
setPDClockForTest(t, pdutil.NewClock4Test())

spoolBuffer := newTestSpool(t, changefeedID, cfg)
Expand Down Expand Up @@ -651,7 +651,7 @@ func TestWriterIndexWriteError(t *testing.T) {
cfg.FlushInterval = time.Hour

changefeedID := commonType.NewChangefeedID4Test("test", "writer-error-metric")
statistics := metrics.NewStatistics(changefeedID, t.Name())
statistics := metrics.NewStatistics(changefeedID, commonType.DefaultKeyspaceID, t.Name())
setPDClockForTest(t, pdutil.NewClock4Test())
spoolBuffer := newTestSpool(t, changefeedID, cfg)
d := newWriter(1, changefeedID, storage, cfg, ".json", statistics, spoolBuffer)
Expand Down Expand Up @@ -716,7 +716,7 @@ func TestWriterDataFileCloseError(t *testing.T) {
cfg.FlushInterval = time.Hour

changefeedID := commonType.NewChangefeedID4Test("test", "writer-close-error")
statistics := metrics.NewStatistics(changefeedID, t.Name())
statistics := metrics.NewStatistics(changefeedID, commonType.DefaultKeyspaceID, t.Name())
setPDClockForTest(t, pdutil.NewClock4Test())
spoolBuffer := newTestSpool(t, changefeedID, cfg)
d := newWriter(1, changefeedID, storage, cfg, ".json", statistics, spoolBuffer)
Expand Down
7 changes: 4 additions & 3 deletions downstreamadapter/sink/kafka/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,18 +74,19 @@ func Verify(ctx context.Context, changefeedID commonType.ChangeFeedID, uri *url.
}

func New(
ctx context.Context, changefeedID commonType.ChangeFeedID, sinkURI *url.URL, sinkConfig *config.SinkConfig,
ctx context.Context, changefeedID commonType.ChangeFeedID, sinkURI *url.URL, sinkConfig *config.SinkConfig, keyspaceID uint32,
) (*sink, error) {
comp, protocol, err := newKafkaSinkComponent(ctx, changefeedID, sinkURI, sinkConfig)
if err != nil {
return nil, errors.Trace(err)
}
return newWithComponents(ctx, changefeedID, protocol, comp)
return newWithComponents(ctx, changefeedID, keyspaceID, protocol, comp)
}

func newWithComponents(
ctx context.Context,
changefeedID commonType.ChangeFeedID,
keyspaceID uint32,
protocol config.Protocol,
comp components,
) (*sink, error) {
Expand All @@ -106,7 +107,7 @@ func newWithComponents(
}
}()

statistics := metrics.NewStatistics(changefeedID, "sink")
statistics := metrics.NewStatistics(changefeedID, keyspaceID, "sink")
asyncProducer, err = comp.factory.AsyncProducer(ctx)
if err != nil {
return nil, err
Expand Down
2 changes: 1 addition & 1 deletion downstreamadapter/sink/kafka/sink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ func newKafkaSinkForTestWithProducers(ctx context.Context,
}
}()

s, err := newWithComponents(ctx, changefeedID, protocol, comp)
s, err := newWithComponents(ctx, changefeedID, common.DefaultKeyspaceID, protocol, comp)
if err != nil {
return nil, err
}
Expand Down
12 changes: 8 additions & 4 deletions downstreamadapter/sink/mysql/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ func New(
changefeedID common.ChangeFeedID,
config *config.ChangefeedConfig,
sinkURI *url.URL,
keyspaceID uint32,
) (*Sink, error) {
cfg, dmlDB, controlDB, err := mysql.NewMysqlConfigAndDBs(ctx, changefeedID, sinkURI, config)
if err != nil {
Expand All @@ -112,7 +113,7 @@ func New(
metrics.ChangefeedDownstreamIsTiDBGauge.DeleteLabelValues(keyspace, name)
}

return newMySQLSinkWithControlDB(ctx, changefeedID, cfg, dmlDB, controlDB, config.BDRMode, config.EnableActiveActive, config.ActiveActiveProgressInterval), nil
return newMySQLSinkWithControlDB(ctx, changefeedID, cfg, dmlDB, controlDB, config.BDRMode, config.EnableActiveActive, config.ActiveActiveProgressInterval, keyspaceID), nil
}

func NewMySQLSink(
Expand All @@ -123,8 +124,9 @@ func NewMySQLSink(
bdrMode bool,
enableActiveActive bool,
progressInterval time.Duration,
keyspaceID uint32,
) *Sink {
return newMySQLSinkWithDBs(ctx, changefeedID, cfg, db, db, bdrMode, enableActiveActive, progressInterval)
return newMySQLSinkWithDBs(ctx, changefeedID, cfg, db, db, bdrMode, enableActiveActive, progressInterval, keyspaceID)
}

// newMySQLSinkWithControlDB creates a MySQL sink with separate pools for DML and
Expand All @@ -140,8 +142,9 @@ func newMySQLSinkWithControlDB(
bdrMode bool,
enableActiveActive bool,
progressInterval time.Duration,
keyspaceID uint32,
) *Sink {
return newMySQLSinkWithDBs(ctx, changefeedID, cfg, dmlDB, controlDB, bdrMode, enableActiveActive, progressInterval)
return newMySQLSinkWithDBs(ctx, changefeedID, cfg, dmlDB, controlDB, bdrMode, enableActiveActive, progressInterval, keyspaceID)
}

func newMySQLSinkWithDBs(
Expand All @@ -153,8 +156,9 @@ func newMySQLSinkWithDBs(
bdrMode bool,
enableActiveActive bool,
progressInterval time.Duration,
keyspaceID uint32,
) *Sink {
stat := metrics.NewStatistics(changefeedID, "TxnSink")
stat := metrics.NewStatistics(changefeedID, keyspaceID, "TxnSink")

var activeActiveSyncStatsCollector *mysql.ActiveActiveSyncStatsCollector
if enableActiveActive && cfg.IsTiDB && cfg.ActiveActiveSyncStatsInterval > 0 {
Expand Down
6 changes: 3 additions & 3 deletions downstreamadapter/sink/mysql/sink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func getMysqlSink() (context.Context, *Sink, sqlmock.Sqlmock) {
cfg.MaxAllowedPacket = int64(vardef.DefMaxAllowedPacket)
cfg.CachePrepStmts = false

sink := NewMySQLSink(ctx, changefeedID, cfg, db, false, false, time.Minute)
sink := NewMySQLSink(ctx, changefeedID, cfg, db, false, false, time.Minute, common.DefaultKeyspaceID)
return ctx, sink, mock
}

Expand All @@ -55,7 +55,7 @@ func getMysqlSinkWithDDLTs() (context.Context, *Sink, sqlmock.Sqlmock) {
cfg.CachePrepStmts = false
cfg.EnableDDLTs = true // Enable DDL-ts feature for testing

sink := NewMySQLSink(ctx, changefeedID, cfg, db, false, false, time.Minute)
sink := NewMySQLSink(ctx, changefeedID, cfg, db, false, false, time.Minute, common.DefaultKeyspaceID)
return ctx, sink, mock
}

Expand All @@ -75,7 +75,7 @@ func getMysqlSinkWithSeparateDBs(t *testing.T) (context.Context, *Sink, sqlmock.
cfg.MaxAllowedPacket = int64(vardef.DefMaxAllowedPacket)
cfg.CachePrepStmts = false

sink := newMySQLSinkWithControlDB(ctx, changefeedID, cfg, dmlDB, controlDB, false, false, time.Minute)
sink := newMySQLSinkWithControlDB(ctx, changefeedID, cfg, dmlDB, controlDB, false, false, time.Minute, common.DefaultKeyspaceID)
return ctx, sink, dmlMock, controlMock
}

Expand Down
Loading
Loading