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

     1  // Copyright 2022 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 factory
    15  
    16  import (
    17  	"context"
    18  	"net/url"
    19  
    20  	"github.com/pingcap/tiflow/cdc/model"
    21  	"github.com/pingcap/tiflow/cdc/sink/ddlsink"
    22  	"github.com/pingcap/tiflow/cdc/sink/ddlsink/blackhole"
    23  	"github.com/pingcap/tiflow/cdc/sink/ddlsink/cloudstorage"
    24  	"github.com/pingcap/tiflow/cdc/sink/ddlsink/mq"
    25  	"github.com/pingcap/tiflow/cdc/sink/ddlsink/mq/ddlproducer"
    26  	"github.com/pingcap/tiflow/cdc/sink/ddlsink/mysql"
    27  	"github.com/pingcap/tiflow/cdc/sink/dmlsink/mq/manager"
    28  	"github.com/pingcap/tiflow/pkg/config"
    29  	cerror "github.com/pingcap/tiflow/pkg/errors"
    30  	"github.com/pingcap/tiflow/pkg/sink"
    31  	"github.com/pingcap/tiflow/pkg/sink/kafka"
    32  	kafkav2 "github.com/pingcap/tiflow/pkg/sink/kafka/v2"
    33  	pulsarConfig "github.com/pingcap/tiflow/pkg/sink/pulsar"
    34  	"github.com/pingcap/tiflow/pkg/util"
    35  )
    36  
    37  // New creates a new ddlsink.Sink by scheme.
    38  func New(
    39  	ctx context.Context,
    40  	changefeedID model.ChangeFeedID,
    41  	sinkURIStr string,
    42  	cfg *config.ReplicaConfig,
    43  ) (ddlsink.Sink, error) {
    44  	sinkURI, err := url.Parse(sinkURIStr)
    45  	if err != nil {
    46  		return nil, cerror.WrapError(cerror.ErrSinkURIInvalid, err)
    47  	}
    48  	scheme := sink.GetScheme(sinkURI)
    49  	switch scheme {
    50  	case sink.KafkaScheme, sink.KafkaSSLScheme:
    51  		factoryCreator := kafka.NewSaramaFactory
    52  		if util.GetOrZero(cfg.Sink.EnableKafkaSinkV2) {
    53  			factoryCreator = kafkav2.NewFactory
    54  		}
    55  		return mq.NewKafkaDDLSink(ctx, changefeedID, sinkURI, cfg,
    56  			factoryCreator, ddlproducer.NewKafkaDDLProducer)
    57  	case sink.BlackHoleScheme:
    58  		return blackhole.NewDDLSink(), nil
    59  	case sink.MySQLSSLScheme, sink.MySQLScheme, sink.TiDBScheme, sink.TiDBSSLScheme:
    60  		return mysql.NewDDLSink(ctx, changefeedID, sinkURI, cfg)
    61  	case sink.S3Scheme, sink.FileScheme, sink.GCSScheme, sink.GSScheme, sink.AzblobScheme, sink.AzureScheme, sink.CloudStorageNoopScheme:
    62  		return cloudstorage.NewDDLSink(ctx, changefeedID, sinkURI, cfg)
    63  	case sink.PulsarScheme, sink.PulsarSSLScheme:
    64  		return mq.NewPulsarDDLSink(ctx, changefeedID, sinkURI, cfg, manager.NewPulsarTopicManager,
    65  			pulsarConfig.NewCreatorFactory, ddlproducer.NewPulsarProducer)
    66  	default:
    67  		return nil,
    68  			cerror.ErrSinkURIInvalid.GenWithStack("the sink scheme (%s) is not supported", scheme)
    69  	}
    70  }