Skip to content
Closed
Show file tree
Hide file tree
Changes from 6 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
6 changes: 3 additions & 3 deletions downstreamadapter/sink/blackhole/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import (
"github.com/pingcap/log"
"github.com/pingcap/ticdc/pkg/common"
commonEvent "github.com/pingcap/ticdc/pkg/common/event"
"github.com/pingcap/ticdc/pkg/metrics"
"github.com/pingcap/ticdc/pkg/statistics"
"github.com/pingcap/ticdc/utils/chann"
"go.uber.org/zap"
)
Expand All @@ -28,13 +28,13 @@ import (
// Including DDL and DML.
type Sink struct {
eventCh *chann.UnlimitedChannel[*commonEvent.DMLEvent, any]
statistics *metrics.Statistics
statistics *statistics.Statistics
}

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

Expand Down
9 changes: 6 additions & 3 deletions downstreamadapter/sink/cloudstorage/buffer_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,9 +177,10 @@ type tableBatches struct {
}

type tableBatch struct {
size uint64
tableInfo *common.TableInfo
entries []*spool.Entry
size uint64
approximateSize int64
tableInfo *common.TableInfo
entries []*spool.Entry
}

func newTableBatches() tableBatches {
Expand All @@ -203,6 +204,7 @@ func (t *tableBatches) addEntry(event *task, entry *spool.Entry) {

tableTask := t.tables[table]
tableTask.size += entry.FileBytes()
tableTask.approximateSize += event.approximateSize
tableTask.entries = append(tableTask.entries, entry)
t.nBytes += entry.FileBytes()
}
Expand Down Expand Up @@ -285,6 +287,7 @@ func (b *payloadBuilder) Build() *payload {
tableInfo: b.batch.tableInfo,
data: b.buf.Bytes(),
rowsCount: b.rowsCount,
approximateSize: b.batch.approximateSize,
nBytes: b.nBytes,
entries: b.batch.entries,
postFlushCallbacks: b.postFlushCallbacks,
Expand Down
6 changes: 3 additions & 3 deletions downstreamadapter/sink/cloudstorage/dml_writers.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ import (
"github.com/pingcap/ticdc/pkg/cloudstorage"
commonType "github.com/pingcap/ticdc/pkg/common"
commonEvent "github.com/pingcap/ticdc/pkg/common/event"
"github.com/pingcap/ticdc/pkg/metrics"
"github.com/pingcap/ticdc/pkg/sink/codec/common"
"github.com/pingcap/ticdc/pkg/statistics"
"github.com/pingcap/ticdc/utils/chann"
"github.com/pingcap/tidb/pkg/objstore/storeapi"
"go.uber.org/atomic"
Expand All @@ -34,7 +34,7 @@ import (
// dmlWriters coordinates encoding and output shard writers.
type dmlWriters struct {
changefeedID commonType.ChangeFeedID
statistics *metrics.Statistics
statistics *statistics.Statistics

// msgCh is the only unbounded queue in the storage sink pipeline.
// External callers push tasks into it, addTasks consumes it, and
Expand All @@ -55,7 +55,7 @@ func newDMLWriters(
config *cloudstorage.Config,
encoderConfig *common.Config,
extension string,
statistics *metrics.Statistics,
statistics *statistics.Statistics,
columnSelector *columnselector.ColumnSelectors,
) (*dmlWriters, error) {
messageCh := chann.NewUnlimitedChannelDefault[*task]()
Expand Down
5 changes: 3 additions & 2 deletions downstreamadapter/sink/cloudstorage/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import (
"github.com/pingcap/ticdc/pkg/config"
"github.com/pingcap/ticdc/pkg/errors"
"github.com/pingcap/ticdc/pkg/metrics"
"github.com/pingcap/ticdc/pkg/statistics"
"github.com/pingcap/ticdc/pkg/util"
"github.com/pingcap/tidb/pkg/meta/model"
"github.com/pingcap/tidb/pkg/objstore/storeapi"
Expand Down Expand Up @@ -65,7 +66,7 @@ type sink struct {
lastSendCheckpointTsTime time.Time

cron *cron.Cron
statistics *metrics.Statistics
statistics *statistics.Statistics

isNormal *atomic.Bool
cleanupJobs []func() /* only for test */
Expand Down Expand Up @@ -135,7 +136,7 @@ func New(
if err != nil {
return nil, err
}
statistics := metrics.NewStatistics(changefeedID, keyspaceID, "cloudstorage")
statistics := statistics.New(changefeedID, keyspaceID)
defer func() {
if err != nil {
statistics.Close()
Expand Down
20 changes: 11 additions & 9 deletions downstreamadapter/sink/cloudstorage/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ type task struct {
dispatcherID commonType.DispatcherID

// DML-only fields.
postEnqueue func() // Transaction enqueue callback.
tableInfo *commonType.TableInfo // Table info used after event is released.
versionedTable cloudstorage.VersionedTableName // Versioned output identity for the DML event.
rowEvents []*commonEvent.RowEvent // Row events to encode and flush.
encodedMsgs []*common.Message // Encoded result built from event.
postEnqueue func() // Transaction enqueue callback.
tableInfo *commonType.TableInfo // Table info used after event is released.
versionedTable cloudstorage.VersionedTableName // Versioned output identity for the DML event.
approximateSize int64 // Approximate size of the original DML event.
rowEvents []*commonEvent.RowEvent // Row events to encode and flush.
encodedMsgs []*common.Message // Encoded result built from event.

// Flush-only field.
marker *flushMarker // Barrier marker used by FlushDMLBeforeBlock.
Expand All @@ -55,10 +56,11 @@ func newDMLTask(
) *task {
postEnqueue, postFlush := event.DetachPostCallbacks()
return &task{
kind: taskKindDML,
postEnqueue: postEnqueue,
tableInfo: event.TableInfo,
versionedTable: version,
kind: taskKindDML,
postEnqueue: postEnqueue,
tableInfo: event.TableInfo,
versionedTable: version,
approximateSize: event.GetSize(),
// Storage txn encoders attach only the last row callback to the built
// batch message, so the callback is triggered once per encoded txn
// message. Kafka uses row-level callbacks and counts all rows before
Expand Down
11 changes: 6 additions & 5 deletions downstreamadapter/sink/cloudstorage/writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import (
"github.com/pingcap/ticdc/pkg/cloudstorage"
"github.com/pingcap/ticdc/pkg/common"
"github.com/pingcap/ticdc/pkg/errors"
pmetrics "github.com/pingcap/ticdc/pkg/metrics"
"github.com/pingcap/ticdc/pkg/statistics"
"github.com/pingcap/tidb/pkg/objstore/storeapi"
"github.com/prometheus/client_golang/prometheus"
"go.uber.org/zap"
Expand All @@ -45,7 +45,7 @@ type writer struct {
// the channel does not need to be closed explicitly.
flushCh chan flushTask

statistics *pmetrics.Statistics
statistics *statistics.Statistics
filePathGenerator *cloudstorage.FilePathGenerator

metricFlushBytes prometheus.Observer
Expand All @@ -63,6 +63,7 @@ type payload struct {
tableInfo *common.TableInfo
data []byte
rowsCount int
approximateSize int64
nBytes int64
entries []*spool.Entry
postFlushCallbacks []func()
Expand All @@ -74,7 +75,7 @@ func newWriter(
storage storeapi.Storage,
config *cloudstorage.Config,
extension string,
statistics *pmetrics.Statistics,
statistics *statistics.Statistics,
spoolBuffer *spool.Spool,
) *writer {
var (
Expand Down Expand Up @@ -229,7 +230,7 @@ func (d *writer) writeDataFile(ctx context.Context, dataFilePath, indexFilePath
if err != nil {
return 0, 0, err
}
return payload.rowsCount, payload.nBytes, nil
return payload.rowsCount, payload.approximateSize, nil
}

writer, err := d.storage.Create(ctx, dataFilePath, &storeapi.WriterOption{
Expand All @@ -256,7 +257,7 @@ func (d *writer) writeDataFile(ctx context.Context, dataFilePath, indexFilePath
zap.String("path", dataFilePath), zap.Error(err))
return 0, 0, err
}
return payload.rowsCount, payload.nBytes, nil
return payload.rowsCount, payload.approximateSize, nil
})
if err != nil {
return err
Expand Down
16 changes: 10 additions & 6 deletions downstreamadapter/sink/cloudstorage/writer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ import (
commonType "github.com/pingcap/ticdc/pkg/common"
commonEvent "github.com/pingcap/ticdc/pkg/common/event"
"github.com/pingcap/ticdc/pkg/config"
"github.com/pingcap/ticdc/pkg/metrics"
"github.com/pingcap/ticdc/pkg/pdutil"
"github.com/pingcap/ticdc/pkg/sink/codec/common"
"github.com/pingcap/ticdc/pkg/statistics"
"github.com/pingcap/ticdc/pkg/util"
"github.com/pingcap/tidb/pkg/meta/model"
"github.com/pingcap/tidb/pkg/objstore/objectio"
Expand All @@ -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, commonType.DefaultKeyspaceID, t.Name())
statistics := statistics.New(changefeedID, commonType.DefaultKeyspaceID)
spoolBuffer := newTestSpool(t, changefeedID, cfg)
d := newWriter(1, changefeedID, storage,
cfg, ".json", statistics, spoolBuffer)
Expand Down Expand Up @@ -426,8 +426,12 @@ func TestWriterPostFlushDoesNotRunPausedPostEnqueue(t *testing.T) {
defer spoolBuffer.Release(secondEntry)
require.Equal(t, int64(0), secondEnqueued.Load())

payload, err := buildPayload(spoolBuffer, &tableBatch{entries: []*spool.Entry{secondEntry}})
payload, err := buildPayload(spoolBuffer, &tableBatch{
approximateSize: 123,
entries: []*spool.Entry{secondEntry},
})
require.NoError(t, err)
require.Equal(t, int64(123), payload.approximateSize)
require.Len(t, payload.postFlushCallbacks, 1)

for _, postFlushCallback := range payload.postFlushCallbacks {
Expand Down Expand Up @@ -479,7 +483,7 @@ func TestWriterStoresPendingMessagesInSpoolBeforeFlush(t *testing.T) {
cfg.FlushInterval = time.Hour

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

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

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

changefeedID := commonType.NewChangefeedID4Test("test", "writer-close-error")
statistics := metrics.NewStatistics(changefeedID, commonType.DefaultKeyspaceID, t.Name())
statistics := statistics.New(changefeedID, commonType.DefaultKeyspaceID)
setPDClockForTest(t, pdutil.NewClock4Test())
spoolBuffer := newTestSpool(t, changefeedID, cfg)
d := newWriter(1, changefeedID, storage, cfg, ".json", statistics, spoolBuffer)
Expand Down
3 changes: 3 additions & 0 deletions downstreamadapter/sink/helper/mq_row_event.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func NewMQRowEvents(
) ([]*commonEvent.MQRowEvent, error) {
callback := NewPostFlushRowCallback(event, uint64(event.Len()))
events := make([]*commonEvent.MQRowEvent, 0, event.Len())
approximateSize := event.GetSize()
if selector == nil {
selector = columnselector.NewDefaultColumnSelector()
}
Expand Down Expand Up @@ -57,12 +58,14 @@ func NewMQRowEvents(
TableInfo: event.TableInfo,
StartTs: event.StartTs,
CommitTs: event.CommitTs,
ApproximateSize: approximateSize,
Event: row,
Callback: callback,
ColumnSelector: selector,
Checksum: row.Checksum,
},
})
approximateSize = 0
}
return events, nil
}
Expand Down
50 changes: 31 additions & 19 deletions downstreamadapter/sink/kafka/sink.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ import (
codecCommon "github.com/pingcap/ticdc/pkg/sink/codec/common"
"github.com/pingcap/ticdc/pkg/sink/kafka"
"github.com/pingcap/ticdc/pkg/sink/kafka/claimcheck"
"github.com/pingcap/ticdc/pkg/statistics"
"github.com/pingcap/ticdc/pkg/util"
"github.com/pingcap/ticdc/utils/chann"
"go.uber.org/atomic"
Expand All @@ -51,7 +52,7 @@ type sink struct {
metricsCollector kafka.MetricsCollector

comp components
statistics *metrics.Statistics
statistics *statistics.Statistics

protocol config.Protocol
partitionRule helper.DDLDispatchRule
Expand Down Expand Up @@ -170,7 +171,7 @@ func newWithComponents(
protocol config.Protocol,
comp components,
) (*sink, error) {
statistics := metrics.NewStatistics(changefeedID, keyspaceID, "sink")
statistics := statistics.New(changefeedID, keyspaceID)
var (
err error
asyncProducer kafka.AsyncProducer
Expand Down Expand Up @@ -443,25 +444,36 @@ func (s *sink) sendMessages(ctx context.Context) error {
if err = future.Ready(ctx); err != nil {
return err
}
for _, message := range future.Messages {
for i, message := range future.Messages {
start := time.Now()
if err = s.statistics.RecordBatchExecution(func() (int, int64, error) {
message.SetPartitionKey(future.Key.PartitionKey)
log.Debug("send message to kafka", zap.String("messageKey", util.RedactBytes(message.Key)), zap.String("messageValue", util.RedactBytes(message.Value)))
if err = s.dmlProducer.AsyncSend(
ctx,
future.Key.Topic,
future.Key.Partition,
message); err != nil {
log.Error("kafka sink send message failed",
zap.String("keyspace", s.changefeedID.Keyspace()),
zap.String("changefeed", s.changefeedID.Name()),
zap.Error(err))
return 0, 0, err
rows, writeBytes := message.GetRowsCount(), int64(0)
if i == 0 {
writeBytes = future.ApproximateSize
}
callback := message.Callback
message.Callback = func() {
_ = s.statistics.RecordBatchExecution(func() (int, int64, error) {
return rows, writeBytes, nil
})
if callback != nil {
callback()
}
return message.GetRowsCount(), int64(message.Length()), nil
}); err != nil {
return err
}

message.SetPartitionKey(future.Key.PartitionKey)
log.Debug("send message to kafka", zap.String("messageKey", util.RedactBytes(message.Key)), zap.String("messageValue", util.RedactBytes(message.Value)))
if err = s.dmlProducer.AsyncSend(
ctx,
future.Key.Topic,
future.Key.Partition,
message); err != nil {
log.Error("kafka sink send message failed",
zap.String("keyspace", s.changefeedID.Keyspace()),
zap.String("changefeed", s.changefeedID.Name()),
zap.Error(err))
return s.statistics.RecordBatchExecution(func() (int, int64, error) {
return 0, 0, err
})
}
metricSendMessageDuration.Observe(time.Since(start).Seconds())
}
Expand Down
Loading
Loading