github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/vars/vars.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 vars
    15  
    16  import (
    17  	"context"
    18  	"time"
    19  
    20  	"github.com/pingcap/tiflow/cdc/model"
    21  	"github.com/pingcap/tiflow/cdc/processor/sourcemanager/sorter/factory"
    22  	"github.com/pingcap/tiflow/pkg/config"
    23  	"github.com/pingcap/tiflow/pkg/etcd"
    24  	"github.com/pingcap/tiflow/pkg/p2p"
    25  	"github.com/pingcap/tiflow/pkg/workerpool"
    26  	"github.com/tikv/client-go/v2/oracle"
    27  )
    28  
    29  // GlobalVars contains some vars which can be used anywhere in a pipeline
    30  // the lifecycle of vars in the GlobalVars should be aligned with the ticdc server process.
    31  // All field in Vars should be READ-ONLY and THREAD-SAFE
    32  type GlobalVars struct {
    33  	CaptureInfo *model.CaptureInfo
    34  	EtcdClient  etcd.CDCEtcdClient
    35  
    36  	// SortEngineManager is introduced for pull-based sinks.
    37  	SortEngineFactory *factory.SortEngineFactory
    38  
    39  	// OwnerRevision is the Etcd revision when the owner got elected.
    40  	OwnerRevision int64
    41  
    42  	// MessageServer and MessageRouter are for peer-messaging
    43  	MessageServer *p2p.MessageServer
    44  	MessageRouter p2p.MessageRouter
    45  
    46  	// ChangefeedThreadPool is the thread pool for changefeed initialization
    47  	ChangefeedThreadPool workerpool.AsyncPool
    48  }
    49  
    50  // NewGlobalVars4Test returns a GlobalVars for test,
    51  func NewGlobalVars4Test() *GlobalVars {
    52  	return &GlobalVars{
    53  		CaptureInfo: &model.CaptureInfo{
    54  			ID:            "capture-test",
    55  			AdvertiseAddr: "127.0.0.1:0000",
    56  			// suppose the current version is `v6.3.0`
    57  			Version: "v6.3.0",
    58  		},
    59  		EtcdClient: &etcd.CDCEtcdClientImpl{
    60  			ClusterID: etcd.DefaultCDCClusterID,
    61  		},
    62  		ChangefeedThreadPool: &NonAsyncPool{},
    63  	}
    64  }
    65  
    66  // NewGlobalVarsAndChangefeedInfo4Test returns GlobalVars and model.ChangeFeedInfo for ut
    67  func NewGlobalVarsAndChangefeedInfo4Test() (*GlobalVars, *model.ChangeFeedInfo) {
    68  	return NewGlobalVars4Test(),
    69  		&model.ChangeFeedInfo{
    70  			ID:      "changefeed-id-test",
    71  			StartTs: oracle.GoTimeToTS(time.Now()),
    72  			Config:  config.GetDefaultReplicaConfig(),
    73  		}
    74  }
    75  
    76  // NonAsyncPool is a dummy implementation of workerpool.AsyncPool, which runs tasks synchronously.
    77  // It is used in tests to avoid the overhead of asynchronous task scheduling.
    78  type NonAsyncPool struct{}
    79  
    80  // Go runs the task synchronously.
    81  func (f *NonAsyncPool) Go(_ context.Context, fn func()) error {
    82  	fn()
    83  	return nil
    84  }
    85  
    86  // Run does nothing.
    87  func (f *NonAsyncPool) Run(_ context.Context) error {
    88  	return nil
    89  }