github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/sink/ddlsink/mq/ddlproducer/pulsar_ddl_mock_producer.go (about)

     1  // Copyright 2023 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 ddlproducer
    15  
    16  import (
    17  	"context"
    18  
    19  	"github.com/apache/pulsar-client-go/pulsar"
    20  	"github.com/pingcap/tiflow/cdc/model"
    21  	"github.com/pingcap/tiflow/pkg/config"
    22  	"github.com/pingcap/tiflow/pkg/sink/codec/common"
    23  )
    24  
    25  // Assert DDLEventSink implementation
    26  var _ DDLProducer = (*PulsarMockProducers)(nil)
    27  
    28  // PulsarMockProducers is a mock pulsar producer
    29  type PulsarMockProducers struct {
    30  	events map[string][]*pulsar.ProducerMessage
    31  }
    32  
    33  // SyncBroadcastMessage pulsar consume all partitions
    34  func (p *PulsarMockProducers) SyncBroadcastMessage(ctx context.Context, topic string,
    35  	totalPartitionsNum int32, message *common.Message,
    36  ) error {
    37  	return p.SyncSendMessage(ctx, topic, totalPartitionsNum, message)
    38  }
    39  
    40  // SyncSendMessage sends a message
    41  // partitionNum is not used,pulsar consume all partitions
    42  func (p *PulsarMockProducers) SyncSendMessage(ctx context.Context, topic string,
    43  	partitionNum int32, message *common.Message,
    44  ) error {
    45  	data := &pulsar.ProducerMessage{
    46  		Payload: message.Value,
    47  		Key:     message.GetPartitionKey(),
    48  	}
    49  	p.events[topic] = append(p.events[topic], data)
    50  
    51  	return nil
    52  }
    53  
    54  // NewMockPulsarProducer creates a pulsar producer
    55  func NewMockPulsarProducer(
    56  	ctx context.Context,
    57  	changefeedID model.ChangeFeedID,
    58  	pConfig *config.PulsarConfig,
    59  	client pulsar.Client,
    60  ) (*PulsarMockProducers, error) {
    61  	return &PulsarMockProducers{
    62  		events: map[string][]*pulsar.ProducerMessage{},
    63  	}, nil
    64  }
    65  
    66  // NewMockPulsarProducerDDL creates a pulsar producer for DDLProducer
    67  func NewMockPulsarProducerDDL(
    68  	ctx context.Context,
    69  	changefeedID model.ChangeFeedID,
    70  	pConfig *config.PulsarConfig,
    71  	client pulsar.Client,
    72  	sinkConfig *config.SinkConfig,
    73  ) (DDLProducer, error) {
    74  	return NewMockPulsarProducer(ctx, changefeedID, pConfig, client)
    75  }
    76  
    77  // GetProducerByTopic returns a producer by topic name
    78  func (p *PulsarMockProducers) GetProducerByTopic(topicName string) (producer pulsar.Producer, err error) {
    79  	return producer, nil
    80  }
    81  
    82  // Close close all producers
    83  func (p *PulsarMockProducers) Close() {
    84  	p.events = make(map[string][]*pulsar.ProducerMessage)
    85  }
    86  
    87  // Flush waits for all the messages in the async producer to be sent to Pulsar.
    88  // Notice: this method is not thread-safe.
    89  // Do not try to call AsyncSendMessage and Flush functions in different threads,
    90  // otherwise Flush will not work as expected. It may never finish or flush the wrong message.
    91  // Because inflight will be modified by mistake.
    92  func (p *PulsarMockProducers) Flush(ctx context.Context) error {
    93  	return nil
    94  }
    95  
    96  // GetAllEvents returns the events received by the mock producer.
    97  func (p *PulsarMockProducers) GetAllEvents() []*pulsar.ProducerMessage {
    98  	var events []*pulsar.ProducerMessage
    99  	for _, v := range p.events {
   100  		events = append(events, v...)
   101  	}
   102  	return events
   103  }
   104  
   105  // GetEvents returns the event filtered by the key.
   106  func (p *PulsarMockProducers) GetEvents(topic string) []*pulsar.ProducerMessage {
   107  	return p.events[topic]
   108  }