github.com/pingcap/ticdc@v0.0.0-20220526033649-485a10ef2652/cdc/sink/black_hole.go (about)

     1  // Copyright 2020 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package sink
    15  
    16  import (
    17  	"context"
    18  	"sync/atomic"
    19  
    20  	"github.com/pingcap/log"
    21  	"github.com/pingcap/ticdc/cdc/model"
    22  	"go.uber.org/zap"
    23  )
    24  
    25  // newBlackHoleSink creates a block hole sink
    26  func newBlackHoleSink(ctx context.Context, opts map[string]string) *blackHoleSink {
    27  	return &blackHoleSink{
    28  		statistics: NewStatistics(ctx, "blackhole", opts),
    29  	}
    30  }
    31  
    32  type blackHoleSink struct {
    33  	statistics      *Statistics
    34  	checkpointTs    uint64
    35  	accumulated     uint64
    36  	lastAccumulated uint64
    37  }
    38  
    39  func (b *blackHoleSink) EmitRowChangedEvents(ctx context.Context, rows ...*model.RowChangedEvent) error {
    40  	for _, row := range rows {
    41  		log.Debug("BlockHoleSink: EmitRowChangedEvents", zap.Any("row", row))
    42  	}
    43  	rowsCount := len(rows)
    44  	atomic.AddUint64(&b.accumulated, uint64(rowsCount))
    45  	b.statistics.AddRowsCount(rowsCount)
    46  	return nil
    47  }
    48  
    49  func (b *blackHoleSink) FlushRowChangedEvents(ctx context.Context, resolvedTs uint64) (uint64, error) {
    50  	log.Debug("BlockHoleSink: FlushRowChangedEvents", zap.Uint64("resolvedTs", resolvedTs))
    51  	err := b.statistics.RecordBatchExecution(func() (int, error) {
    52  		// TODO: add some random replication latency
    53  		accumulated := atomic.LoadUint64(&b.accumulated)
    54  		batchSize := accumulated - b.lastAccumulated
    55  		b.lastAccumulated = accumulated
    56  		return int(batchSize), nil
    57  	})
    58  	b.statistics.PrintStatus(ctx)
    59  	atomic.StoreUint64(&b.checkpointTs, resolvedTs)
    60  	return resolvedTs, err
    61  }
    62  
    63  func (b *blackHoleSink) EmitCheckpointTs(ctx context.Context, ts uint64) error {
    64  	log.Debug("BlockHoleSink: Checkpoint Event", zap.Uint64("ts", ts))
    65  	return nil
    66  }
    67  
    68  func (b *blackHoleSink) EmitDDLEvent(ctx context.Context, ddl *model.DDLEvent) error {
    69  	log.Debug("BlockHoleSink: DDL Event", zap.Any("ddl", ddl))
    70  	return nil
    71  }
    72  
    73  // Initialize is no-op for blackhole
    74  func (b *blackHoleSink) Initialize(ctx context.Context, tableInfo []*model.SimpleTableInfo) error {
    75  	return nil
    76  }
    77  
    78  func (b *blackHoleSink) Close(ctx context.Context) error {
    79  	return nil
    80  }
    81  
    82  func (b *blackHoleSink) Barrier(ctx context.Context) error {
    83  	return nil
    84  }