Skip to content

Commit f37e6b1

Browse files
committed
simplify the code further
1 parent 7d1a6a6 commit f37e6b1

9 files changed

Lines changed: 47 additions & 74 deletions

File tree

‎cmd/consumer/conductor/conductor.go‎

Lines changed: 19 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ type Conductor struct {
112112
partitionPendingBytes []int64
113113
pausedPartitions []bool
114114
pausedCount int
115-
pendingDMLBytes int64
115+
queuedDMLBytes int64
116116
partitionPauseBytes int64
117117
partitionResumeBytes int64
118118
watermark uint64
@@ -134,7 +134,11 @@ type Conductor struct {
134134
// pending-DML assembly.
135135
func New(reader source.Reader, schemas *codecCommon.TableInfoProvider, decoder *decode.Group) *Conductor {
136136
partitions := reader.Partitions()
137-
pauseBytes, resumeBytes := partitionBufferLimits(partitions)
137+
var pauseBytes, resumeBytes int64
138+
if partitions > 1 {
139+
pauseBytes = min(maxPartitionPauseBytes, max(minPartitionPauseBytes, maxTotalPendingBytes/int64(partitions)))
140+
resumeBytes = pauseBytes / 2
141+
}
138142
c := &Conductor{
139143
schemas: schemas,
140144
decoder: decoder,
@@ -240,7 +244,7 @@ func (c *Conductor) logProgress(w *writer.Writer, inBytes int64, lastLogTime *ti
240244
zap.Uint64("effectiveWatermark", c.effectiveWatermark()),
241245
zap.Uint64("checkpoint", checkpoint),
242246
zap.Int("pendingDMLTables", len(c.pendingDML)),
243-
zap.Int64("pendingDMLBytes", c.pendingDMLBytes),
247+
zap.Int64("queuedDMLBytes", c.queuedDMLBytes),
244248
zap.Int("pendingDDL", len(c.pendingDDL)),
245249
zap.Int("pendingDecode", c.waitingCount),
246250
}
@@ -299,12 +303,7 @@ func (c *Conductor) acceptPending(items []*simple.PendingMessage, payload *sourc
299303
var (
300304
marker uint64
301305
perItemBytes int64
302-
offset int64
303306
)
304-
partition := payloadPartition(payload)
305-
if payload != nil {
306-
offset = payload.Offset
307-
}
308307
if len(items) > 0 {
309308
perItemBytes = max(payloadBytes/int64(len(items)), dmlEventOverheadBytes)
310309
}
@@ -315,7 +314,7 @@ func (c *Conductor) acceptPending(items []*simple.PendingMessage, payload *sourc
315314
marker = max(marker, pending.CommitTs)
316315

317316
if tableInfo := c.schemas.Read(pending.Schema, pending.Table, pending.SchemaVersion); tableInfo != nil {
318-
c.queueDML(c.decoder.DecodePending(pending, tableInfo, partition, offset), partition)
317+
c.queueDML(c.decoder.DecodePending(pending, tableInfo, payload.Partition, payload.Offset), payload.Partition)
319318
continue
320319
}
321320

@@ -326,11 +325,11 @@ func (c *Conductor) acceptPending(items []*simple.PendingMessage, payload *sourc
326325
}
327326
c.waiting[key] = append(c.waiting[key], waitingDML{
328327
pending: pending,
329-
partition: partition,
330-
offset: offset,
328+
partition: payload.Partition,
329+
offset: payload.Offset,
331330
bytes: perItemBytes,
332331
})
333-
c.addPartitionBytes(partition, perItemBytes)
332+
c.addPartitionBytes(payload.Partition, perItemBytes)
334333
c.waitingCount++
335334
if pending.CommitTs != 0 && (c.minWaitingTs == 0 || pending.CommitTs < c.minWaitingTs) {
336335
c.minWaitingTs = pending.CommitTs
@@ -379,10 +378,10 @@ func (c *Conductor) recomputeMinWaiting() {
379378
func (c *Conductor) acceptEvent(e event.Event, payload *source.Payload) uint64 {
380379
switch e := e.(type) {
381380
case *event.DMLEvent:
382-
c.queueDML(e, payloadPartition(payload))
381+
c.queueDML(e, payload.Partition)
383382
return e.CommitTs
384383
case *event.ResolvedEvent:
385-
c.advanceWatermark(e.ResolvedTs, payloadPartition(payload))
384+
c.advanceWatermark(e.ResolvedTs, payload.Partition)
386385
case *event.DDLEvent:
387386
c.wakeWaiting(e.TableInfo)
388387
for _, info := range e.MultipleTableInfos {
@@ -550,7 +549,7 @@ func (c *Conductor) syncTable(q *tableQueue) {
550549
// queueDML records a decoded row until the writer can accept it.
551550
func (c *Conductor) queueDML(item *event.DMLEvent, partition int32) {
552551
bytes := dmlMemoryBytes(item)
553-
c.pendingDMLBytes += bytes
552+
c.queuedDMLBytes += bytes
554553
c.addPartitionBytes(partition, bytes)
555554

556555
entry := queuedDML{
@@ -642,7 +641,7 @@ func (c *Conductor) popDML(row *event.DMLEvent) {
642641
q.rows[0] = queuedDML{}
643642
q.rows = q.rows[1:]
644643
q.dropped++
645-
c.pendingDMLBytes -= entry.bytes
644+
c.queuedDMLBytes -= entry.bytes
646645
c.subPartitionBytes(entry.partition, entry.bytes)
647646

648647
if len(q.rows) == 0 {
@@ -770,7 +769,7 @@ func (c *Conductor) advanceWatermark(ts uint64, partition int32) {
770769
return
771770
}
772771

773-
if partition < 0 || int(partition) >= len(c.partitionWatermarks) {
772+
if int(partition) >= len(c.partitionWatermarks) {
774773
return
775774
}
776775

@@ -802,39 +801,20 @@ func (c *Conductor) advanceWriter(w *writer.Writer) {
802801
w.Advance(c.effectiveWatermark(), pending)
803802
}
804803

805-
func partitionBufferLimits(partitions int32) (pauseBytes, resumeBytes int64) {
806-
if partitions <= 1 {
807-
return maxPartitionPauseBytes, maxPartitionPauseBytes / 2
808-
}
809-
pauseBytes = min(maxPartitionPauseBytes, max(minPartitionPauseBytes, maxTotalPendingBytes/int64(partitions)))
810-
return pauseBytes, pauseBytes / 2
811-
}
812-
813-
func payloadPartition(payload *source.Payload) int32 {
814-
if payload == nil {
815-
return -1
816-
}
817-
return payload.Partition
818-
}
819-
820804
func dmlMemoryBytes(item *event.DMLEvent) int64 {
821-
if item == nil || item.Rows == nil {
822-
return dmlEventOverheadBytes
823-
}
824805
return dmlEventOverheadBytes + item.Rows.MemoryUsage()
825806
}
826807

827808
func (c *Conductor) addPartitionBytes(partition int32, bytes int64) {
828-
if partition >= 0 && int(partition) < len(c.partitionPendingBytes) {
809+
if int(partition) < len(c.partitionPendingBytes) {
829810
c.partitionPendingBytes[partition] += bytes
830811
}
831812
}
832813

833814
func (c *Conductor) subPartitionBytes(partition int32, bytes int64) {
834-
if partition < 0 || int(partition) >= len(c.partitionPendingBytes) {
835-
return
815+
if int(partition) < len(c.partitionPendingBytes) {
816+
c.partitionPendingBytes[partition] -= bytes
836817
}
837-
c.partitionPendingBytes[partition] = max(0, c.partitionPendingBytes[partition]-bytes)
838818
}
839819

840820
// syncPartitionPacing pauses partitions whose resolved progress leads the slowest

‎pkg/sink/codec/avro/decoder.go‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import (
3737
"github.com/pingcap/tidb/pkg/parser/ast"
3838
"github.com/pingcap/tidb/pkg/parser/mysql"
3939
"github.com/pingcap/tidb/pkg/types"
40-
"github.com/pingcap/tidb/pkg/util/chunk"
4140
"go.uber.org/zap"
4241
)
4342

@@ -339,10 +338,9 @@ func assembleEvent(
339338
if !isDelete && (hasBefore || updateWithoutBefore) {
340339
rowCap = 2
341340
}
342-
event.Rows = chunk.NewChunkFromPoolWithCapacity(event.TableInfo.GetFieldSlice(), rowCap)
343-
event.AddPostFlushFunc(func() {
344-
event.Rows.Destroy(rowCap, event.TableInfo.GetFieldSlice())
345-
})
341+
var release func()
342+
event.Rows, release = common.NewRowsChunk(event.TableInfo.GetFieldSlice(), rowCap)
343+
event.AddPostFlushFunc(release)
346344
event.Length++
347345
if isDelete {
348346
if hasValue {

‎pkg/sink/codec/canal/canal_json_decoder.go‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ import (
3838
"github.com/pingcap/tidb/pkg/parser/mysql"
3939
"github.com/pingcap/tidb/pkg/parser/types"
4040
tiTypes "github.com/pingcap/tidb/pkg/types"
41-
"github.com/pingcap/tidb/pkg/util/chunk"
4241
canal "github.com/pingcap/tiflow/proto/canal"
4342
"go.uber.org/zap"
4443
"golang.org/x/text/encoding/charmap"
@@ -330,10 +329,9 @@ func (d *decoder) canalJSONMessage2DMLEvent(
330329
if msg.eventType() == canal.EventType_UPDATE {
331330
rowCap = 2
332331
}
333-
result.Rows = chunk.NewChunkFromPoolWithCapacity(tableInfo.GetFieldSlice(), rowCap)
334-
result.AddPostFlushFunc(func() {
335-
result.Rows.Destroy(rowCap, tableInfo.GetFieldSlice())
336-
})
332+
var release func()
333+
result.Rows, release = common.NewRowsChunk(tableInfo.GetFieldSlice(), rowCap)
334+
result.AddPostFlushFunc(release)
337335
result.Length++
338336

339337
columns := tableInfo.GetColumns()

‎pkg/sink/codec/canal/canal_json_txn_decoder.go‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import (
2323
commonEvent "github.com/pingcap/ticdc/pkg/common/event"
2424
"github.com/pingcap/ticdc/pkg/sink/codec/common"
2525
"github.com/pingcap/ticdc/pkg/util"
26-
"github.com/pingcap/tidb/pkg/util/chunk"
2726
canal "github.com/pingcap/tiflow/proto/canal"
2827
"go.uber.org/zap"
2928
)
@@ -140,10 +139,8 @@ func (d *txnDecoder) canalJSONMessage2RowChange(msg canalJSONMessageInterface) *
140139
if msg.eventType() == canal.EventType_UPDATE {
141140
rowCap = 2
142141
}
143-
chk := chunk.NewChunkFromPoolWithCapacity(tableInfo.GetFieldSlice(), rowCap)
144-
result.AddPostFlushFunc(func() {
145-
chk.Destroy(rowCap, tableInfo.GetFieldSlice())
146-
})
142+
chk, release := common.NewRowsChunk(tableInfo.GetFieldSlice(), rowCap)
143+
result.AddPostFlushFunc(release)
147144

148145
columns := tableInfo.GetColumns()
149146
switch msg.eventType() {

‎pkg/sink/codec/common/decoder.go‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,19 @@ package common
1515

1616
import (
1717
commonEvent "github.com/pingcap/ticdc/pkg/common/event"
18+
"github.com/pingcap/tidb/pkg/types"
19+
"github.com/pingcap/tidb/pkg/util/chunk"
1820
)
1921

22+
// NewRowsChunk allocates a pooled chunk sized to capacity rows and returns its
23+
// matching destroy callback for DMLEvent.AddPostFlushFunc.
24+
func NewRowsChunk(fields []*types.FieldType, capacity int) (*chunk.Chunk, func()) {
25+
chk := chunk.NewChunkFromPoolWithCapacity(fields, capacity)
26+
return chk, func() {
27+
chk.Destroy(capacity, fields)
28+
}
29+
}
30+
2031
// Decoder is an abstraction for events decoder.
2132
type Decoder interface {
2233
// AddKeyValue add the received key and values to the decoder,

‎pkg/sink/codec/csv/csv_decoder.go‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
"github.com/pingcap/tidb/pkg/parser/charset"
3636
"github.com/pingcap/tidb/pkg/parser/mysql"
3737
"github.com/pingcap/tidb/pkg/types"
38-
"github.com/pingcap/tidb/pkg/util/chunk"
3938
"go.uber.org/zap"
4039
)
4140

@@ -333,10 +332,8 @@ func csvMsg2RowChangedEvent(csvConfig *common.Config, csvMsg *csvMessage, tableI
333332
if deleteData != nil {
334333
rowCap = 2
335334
}
336-
chk := chunk.NewChunkFromPoolWithCapacity(tableInfo.GetFieldSlice(), rowCap)
337-
e.AddPostFlushFunc(func() {
338-
chk.Destroy(rowCap, tableInfo.GetFieldSlice())
339-
})
335+
chk, release := common.NewRowsChunk(tableInfo.GetFieldSlice(), rowCap)
336+
e.AddPostFlushFunc(release)
340337
if deleteData != nil {
341338
// Handle-key changes are split upstream. Keep the keyed delete and
342339
// after-image insert in one event, as for Avro updates without before values.

‎pkg/sink/codec/debezium/decoder.go‎

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import (
3737
"github.com/pingcap/tidb/pkg/parser/mysql"
3838
ptypes "github.com/pingcap/tidb/pkg/parser/types"
3939
"github.com/pingcap/tidb/pkg/types"
40-
"github.com/pingcap/tidb/pkg/util/chunk"
4140
"go.uber.org/zap"
4241
)
4342

@@ -198,17 +197,16 @@ func (d *decoder) assembleDMLEventFromPayload(
198197
if ok1 && ok2 {
199198
rowCap = 2
200199
}
200+
rows, release := common.NewRowsChunk(tableInfo.GetFieldSlice(), rowCap)
201201
event := &commonEvent.DMLEvent{
202-
Rows: chunk.NewChunkFromPoolWithCapacity(tableInfo.GetFieldSlice(), rowCap),
202+
Rows: rows,
203203
StartTs: startTs,
204204
CommitTs: commitTs,
205205
TableInfo: tableInfo,
206206
PhysicalTableID: tableInfo.TableName.TableID,
207207
Length: 1,
208208
}
209-
event.AddPostFlushFunc(func() {
210-
event.Rows.Destroy(rowCap, tableInfo.GetFieldSlice())
211-
})
209+
event.AddPostFlushFunc(release)
212210
columns := tableInfo.GetColumns()
213211
binaryHandlingMode := d.config.DebeziumBinaryHandlingMode
214212
if d.config.Protocol == config.ProtocolDebeziumAvro {

‎pkg/sink/codec/open/decoder.go‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ import (
3737
"github.com/pingcap/tidb/pkg/parser/mysql"
3838
"github.com/pingcap/tidb/pkg/parser/types"
3939
tiTypes "github.com/pingcap/tidb/pkg/types"
40-
"github.com/pingcap/tidb/pkg/util/chunk"
4140
"go.uber.org/zap"
4241
)
4342

@@ -562,10 +561,8 @@ func (b *decoder) assembleDMLEvent(key *messageKey, value *messageRow) *commonEv
562561
if len(value.Delete) == 0 && len(value.Update) != 0 && len(value.PreColumns) != 0 {
563562
rowCap = 2
564563
}
565-
chk := chunk.NewChunkFromPoolWithCapacity(tableInfo.GetFieldSlice(), rowCap)
566-
result.AddPostFlushFunc(func() {
567-
chk.Destroy(rowCap, tableInfo.GetFieldSlice())
568-
})
564+
chk, release := common.NewRowsChunk(tableInfo.GetFieldSlice(), rowCap)
565+
result.AddPostFlushFunc(release)
569566
columns := tableInfo.GetColumns()
570567
if len(value.Delete) != 0 {
571568
data := collectAllColumnsValue(value.Delete, columns)

‎pkg/sink/codec/simple/decoder.go‎

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ import (
3535
"github.com/pingcap/tidb/pkg/parser/mysql"
3636
ptypes "github.com/pingcap/tidb/pkg/parser/types"
3737
"github.com/pingcap/tidb/pkg/types"
38-
"github.com/pingcap/tidb/pkg/util/chunk"
3938
"go.uber.org/zap"
4039
)
4140

@@ -547,10 +546,8 @@ func buildDMLEvent(msg *message, tableInfo *commonType.TableInfo, enableRowCheck
547546
if msg.Type == DMLTypeUpdate {
548547
rowCap = 2
549548
}
550-
chk := chunk.NewChunkFromPoolWithCapacity(tableInfo.GetFieldSlice(), rowCap)
551-
result.AddPostFlushFunc(func() {
552-
chk.Destroy(rowCap, tableInfo.GetFieldSlice())
553-
})
549+
chk, release := common.NewRowsChunk(tableInfo.GetFieldSlice(), rowCap)
550+
result.AddPostFlushFunc(release)
554551
columns := tableInfo.GetColumns()
555552
switch msg.Type {
556553
case DMLTypeDelete:

0 commit comments

Comments
 (0)