Skip to content
Merged
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
2 changes: 1 addition & 1 deletion cmd/kafka-consumer/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,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 @@ -138,7 +138,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
14 changes: 12 additions & 2 deletions coordinator/changefeed/changefeed_db.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,18 @@ func (db *ChangefeedDB) StopByChangefeedID(cfID common.ChangeFeedID, remove bool
db.stopped[cfID] = cf
}

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

return nodeID
}
Expand Down
37 changes: 37 additions & 0 deletions coordinator/changefeed/changefeed_db_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ import (
"github.com/pingcap/ticdc/pkg/common"
"github.com/pingcap/ticdc/pkg/config"
"github.com/pingcap/ticdc/pkg/errors"
"github.com/pingcap/ticdc/pkg/metrics"
"github.com/pingcap/ticdc/pkg/node"
"github.com/prometheus/client_golang/prometheus/testutil"
"github.com/stretchr/testify/require"
"go.uber.org/atomic"
)
Expand Down Expand Up @@ -133,6 +135,41 @@ func TestRemoveChangefeed(t *testing.T) {
require.False(t, ok)
}

func TestStopByChangefeedIDDeletesCheckpointMetrics(t *testing.T) {
metrics.ChangefeedStatusGauge.Reset()
metrics.ChangefeedCheckpointTsGauge.Reset()
metrics.ChangefeedCheckpointTsLagGauge.Reset()
t.Cleanup(func() {
metrics.ChangefeedStatusGauge.Reset()
metrics.ChangefeedCheckpointTsGauge.Reset()
metrics.ChangefeedCheckpointTsLagGauge.Reset()
})

db := NewChangefeedDB(1216)
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(), "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))
}

func TestGetByID(t *testing.T) {
db := NewChangefeedDB(1216)
cf := &Changefeed{ID: common.NewChangeFeedIDWithName("test", common.DefaultKeyspaceName)}
Expand Down
12 changes: 10 additions & 2 deletions coordinator/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,11 @@ func (c *Controller) collectMetrics(ctx context.Context) error {
info := cf.GetInfo()
keyspace := info.ChangefeedID.Keyspace()
name := info.ChangefeedID.Name()
metrics.ChangefeedStatusGauge.WithLabelValues(keyspace, name).Set(float64(info.State.ToInt()))
metrics.ChangefeedStatusGauge.WithLabelValues(
keyspace,
name,
metrics.FormatKeyspaceID(info.KeyspaceID),
).Set(float64(info.State.ToInt()))

// don't update checkpoint ts and checkpoint ts lag for stopped changefeed
if info.State == config.StateStopped {
Expand All @@ -215,7 +219,11 @@ func (c *Controller) collectMetrics(ctx context.Context) error {
phyCkpTs := oracle.ExtractPhysical(cf.GetLastSavedCheckPointTs())
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(info.KeyspaceID),
).Set(lag)

// sync changefeed error metrics
currentChangefeeds[cf.ID] = struct{}{}
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 @@ -291,7 +291,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
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ func TestTryCloseRemovedRequestAfterClosedReturnsImmediatelyAndTriggersCleanup(t
false,
false,
time.Minute,
common.DefaultKeyspaceID,
)
manager := &DispatcherManager{
changefeedID: changefeedID,
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 @@ -44,7 +44,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 @@ -535,7 +535,7 @@ func TestCloseBeforeRunDoesNotPanicAndCleansSpool(t *testing.T) {
appcontext.SetService(appcontext.DefaultPDClock, mockPDClock)

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 @@ -61,7 +61,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())
pdlock := pdutil.NewMonotonicClock(clock.New())
appcontext.SetService(appcontext.DefaultPDClock, pdlock)
mockPDClock := pdutil.NewClock4Test()
Expand Down Expand Up @@ -486,7 +486,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())
pdlock := pdutil.NewMonotonicClock(clock.New())
appcontext.SetService(appcontext.DefaultPDClock, pdlock)
mockPDClock := pdutil.NewClock4Test()
Expand Down Expand Up @@ -655,7 +655,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())
pdlock := pdutil.NewMonotonicClock(clock.New())
appcontext.SetService(appcontext.DefaultPDClock, pdlock)
mockPDClock := pdutil.NewClock4Test()
Expand Down Expand Up @@ -721,7 +721,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())
pdlock := pdutil.NewMonotonicClock(clock.New())
appcontext.SetService(appcontext.DefaultPDClock, pdlock)
mockPDClock := pdutil.NewClock4Test()
Expand Down
4 changes: 2 additions & 2 deletions downstreamadapter/sink/kafka/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ 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 {
Expand All @@ -86,7 +86,7 @@ func New(
}
}()

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 @@ -50,7 +50,7 @@ func newKafkaSinkForTestWithProducers(ctx context.Context,
if err != nil {
return nil, errors.Trace(err)
}
statistics := metrics.NewStatistics(changefeedID, "sink")
statistics := metrics.NewStatistics(changefeedID, common.DefaultKeyspaceID, "sink")
comp, protocol, err := newKafkaSinkComponentForTest(ctx, changefeedID, sinkURI, sinkConfig)
if err != nil {
return nil, errors.Trace(err)
Expand Down
6 changes: 4 additions & 2 deletions downstreamadapter/sink/mysql/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,12 +91,13 @@ func New(
changefeedID common.ChangeFeedID,
config *config.ChangefeedConfig,
sinkURI *url.URL,
keyspaceID uint32,
) (*Sink, error) {
cfg, db, err := mysql.NewMysqlConfigAndDB(ctx, changefeedID, sinkURI, config)
if err != nil {
return nil, err
}
return NewMySQLSink(ctx, changefeedID, cfg, db, config.BDRMode, config.EnableActiveActive, config.ActiveActiveProgressInterval), nil
return NewMySQLSink(ctx, changefeedID, cfg, db, config.BDRMode, config.EnableActiveActive, config.ActiveActiveProgressInterval, keyspaceID), nil
}

func NewMySQLSink(
Expand All @@ -107,8 +108,9 @@ func NewMySQLSink(
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
4 changes: 2 additions & 2 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 Down
6 changes: 4 additions & 2 deletions downstreamadapter/sink/pulsar/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ 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 := newPulsarSinkComponent(ctx, changefeedID, sinkURI, sinkConfig)
if err != nil {
Expand All @@ -86,6 +86,7 @@ func New(
return newWithComponent(
ctx,
changefeedID,
keyspaceID,
sinkConfig,
comp,
protocol,
Expand Down Expand Up @@ -121,6 +122,7 @@ func newPulsarDDLProducer(
func newWithComponent(
ctx context.Context,
changefeedID commonType.ChangeFeedID,
keyspaceID uint32,
sinkConfig *config.SinkConfig,
comp component,
protocol config.Protocol,
Expand Down Expand Up @@ -148,7 +150,7 @@ func newWithComponent(
}()

failpointCh := make(chan error, 1)
statistics = metrics.NewStatistics(changefeedID, "pulsar")
statistics = metrics.NewStatistics(changefeedID, keyspaceID, "pulsar")
dmlProducer, err = newDMLProducer(changefeedID, comp, failpointCh)
if err != nil {
return nil, err
Expand Down
4 changes: 3 additions & 1 deletion downstreamadapter/sink/pulsar/sink_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ func newPulsarSinkForTest(t *testing.T) (*sink, error) {
comp, protocol, err := newPulsarSinkComponentForTest(ctx, changefeedID, sinkURI, replicaConfig.Sink)
require.NoError(t, err)

statistics := metrics.NewStatistics(changefeedID, "sink")
statistics := metrics.NewStatistics(changefeedID, common.DefaultKeyspaceID, "sink")
pulsarSink := &sink{
changefeedID: changefeedID,
dmlProducer: newMockDMLProducer(),
Expand Down Expand Up @@ -144,6 +144,7 @@ func TestPulsarSinkNewWithComponentReturnsDMLProducerError(t *testing.T) {
_, err = newWithComponent(
context.Background(),
changefeedID,
common.DefaultKeyspaceID,
&config.SinkConfig{},
component{},
config.ProtocolCanalJSON,
Expand Down Expand Up @@ -172,6 +173,7 @@ func TestPulsarSinkNewWithComponentReturnsDDLProducerError(t *testing.T) {
_, err = newWithComponent(
context.Background(),
changefeedID,
common.DefaultKeyspaceID,
&config.SinkConfig{},
component{},
config.ProtocolCanalJSON,
Expand Down
10 changes: 5 additions & 5 deletions downstreamadapter/sink/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,21 @@ type Sink interface {
Run(ctx context.Context) error
}

func New(ctx context.Context, cfg *config.ChangefeedConfig, changefeedID common.ChangeFeedID) (Sink, error) {
func New(ctx context.Context, cfg *config.ChangefeedConfig, changefeedID common.ChangeFeedID, keyspaceID uint32) (Sink, error) {
sinkURI, err := url.Parse(cfg.SinkURI)
if err != nil {
return nil, errors.WrapError(errors.ErrSinkURIInvalid, err)
}
scheme := config.GetScheme(sinkURI)
switch scheme {
case config.MySQLScheme, config.MySQLSSLScheme, config.TiDBScheme, config.TiDBSSLScheme:
return mysql.New(ctx, changefeedID, cfg, sinkURI)
return mysql.New(ctx, changefeedID, cfg, sinkURI, keyspaceID)
case config.KafkaScheme, config.KafkaSSLScheme:
return kafka.New(ctx, changefeedID, sinkURI, cfg.SinkConfig)
return kafka.New(ctx, changefeedID, sinkURI, cfg.SinkConfig, keyspaceID)
case config.PulsarScheme, config.PulsarSSLScheme, config.PulsarHTTPScheme, config.PulsarHTTPSScheme:
return pulsar.New(ctx, changefeedID, sinkURI, cfg.SinkConfig)
return pulsar.New(ctx, changefeedID, sinkURI, cfg.SinkConfig, keyspaceID)
case config.S3Scheme, config.FileScheme, config.GCSScheme, config.GSScheme, config.AzblobScheme, config.AzureScheme, config.CloudStorageNoopScheme:
return cloudstorage.New(ctx, changefeedID, sinkURI, cfg.SinkConfig, cfg.EnableTableAcrossNodes, nil)
return cloudstorage.New(ctx, changefeedID, sinkURI, cfg.SinkConfig, cfg.EnableTableAcrossNodes, nil, keyspaceID)
case config.BlackHoleScheme:
return blackhole.New()
}
Expand Down
8 changes: 7 additions & 1 deletion pkg/applier/redo.go
Original file line number Diff line number Diff line change
Expand Up @@ -464,7 +464,13 @@ func (ra *RedoApplier) Apply(egCtx context.Context) (err error) {
SinkConfig: &config.SinkConfig{},
}
if ra.mysqlSink == nil {
ra.mysqlSink, err = mysql.New(egCtx, ra.rd.GetChangefeedID(), replicaConfig, sinkURI)
ra.mysqlSink, err = mysql.New(
egCtx,
ra.rd.GetChangefeedID(),
replicaConfig,
sinkURI,
commonType.DefaultKeyspaceID,
)
if err != nil {
return err
}
Expand Down
Loading
Loading