github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/redo/writer/writer.go (about)

     1  //  Copyright 2021 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 writer
    15  
    16  import (
    17  	"context"
    18  	"fmt"
    19  	"net/url"
    20  
    21  	"github.com/pingcap/tiflow/cdc/model"
    22  	"github.com/pingcap/tiflow/pkg/config"
    23  	"github.com/pingcap/tiflow/pkg/uuid"
    24  )
    25  
    26  var (
    27  	_ RedoEvent = (*model.RowChangedEvent)(nil)
    28  	_ RedoEvent = (*model.DDLEvent)(nil)
    29  )
    30  
    31  // RedoEvent is the interface for redo event.
    32  type RedoEvent interface {
    33  	ToRedoLog() *model.RedoLog
    34  }
    35  
    36  // RedoLogWriter defines the interfaces used to write redo log, all operations are thread-safe.
    37  type RedoLogWriter interface {
    38  	// WriteEvents writes DDL or DML events to the redo log.
    39  	WriteEvents(ctx context.Context, events ...RedoEvent) error
    40  
    41  	// FlushLog flushes all events written by `WriteEvents` into redo storage.
    42  	FlushLog(ctx context.Context) error
    43  
    44  	// Close is used to close the writer.
    45  	Close() error
    46  }
    47  
    48  // LogWriterConfig is the config for redo log writer.
    49  type LogWriterConfig struct {
    50  	config.ConsistentConfig
    51  	LogType      string
    52  	CaptureID    model.CaptureID
    53  	ChangeFeedID model.ChangeFeedID
    54  
    55  	URI                *url.URL
    56  	UseExternalStorage bool
    57  	Dir                string
    58  	MaxLogSizeInBytes  int64
    59  }
    60  
    61  func (cfg LogWriterConfig) String() string {
    62  	return fmt.Sprintf("%s:%s:%s:%s:%d:%s:%t",
    63  		cfg.ChangeFeedID.Namespace, cfg.ChangeFeedID.ID, cfg.CaptureID,
    64  		cfg.Dir, cfg.MaxLogSize, cfg.URI.String(), cfg.UseExternalStorage)
    65  }
    66  
    67  // Option define the writerOptions
    68  type Option func(writer *LogWriterOptions)
    69  
    70  // LogWriterOptions is the options for writer
    71  type LogWriterOptions struct {
    72  	GetLogFileName   func() string
    73  	GetUUIDGenerator func() uuid.Generator
    74  }
    75  
    76  // WithLogFileName provide the Option for fileName
    77  func WithLogFileName(f func() string) Option {
    78  	return func(o *LogWriterOptions) {
    79  		if f != nil {
    80  			o.GetLogFileName = f
    81  		}
    82  	}
    83  }
    84  
    85  // WithUUIDGenerator provides the Option for uuid generator
    86  func WithUUIDGenerator(f func() uuid.Generator) Option {
    87  	return func(o *LogWriterOptions) {
    88  		if f != nil {
    89  			o.GetUUIDGenerator = f
    90  		}
    91  	}
    92  }
    93  
    94  // EncodeFrameSize encodes the frame size for etcd wal which uses code
    95  // from etcd wal/encoder.go. Ref: https://github.com/etcd-io/etcd/pull/5250
    96  func EncodeFrameSize(dataBytes int) (lenField uint64, padBytes int) {
    97  	lenField = uint64(dataBytes)
    98  	// force 8 byte alignment so length never gets a torn write
    99  	padBytes = (8 - (dataBytes % 8)) % 8
   100  	if padBytes != 0 {
   101  		lenField |= uint64(0x80|padBytes) << 56
   102  	}
   103  	return lenField, padBytes
   104  }