github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/pkg/meta/internal/etcdkv/builder.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 etcdkv
    15  
    16  import (
    17  	"github.com/pingcap/tiflow/engine/pkg/meta/internal/etcdkv/namespace"
    18  	metaModel "github.com/pingcap/tiflow/engine/pkg/meta/model"
    19  	"github.com/pingcap/tiflow/pkg/errors"
    20  	clientv3 "go.etcd.io/etcd/client/v3"
    21  )
    22  
    23  // ClientBuilderImpl is the etcd kvclient builder
    24  type ClientBuilderImpl struct{}
    25  
    26  // ClientType implements ClientType of clientBuilder
    27  func (b *ClientBuilderImpl) ClientType() metaModel.ClientType {
    28  	return metaModel.EtcdKVClientType
    29  }
    30  
    31  // NewKVClientWithNamespace implements NewKVClientWithNamespace of clientBuilder
    32  func (b *ClientBuilderImpl) NewKVClientWithNamespace(cc metaModel.ClientConn,
    33  	projectID metaModel.ProjectID, jobID metaModel.JobID,
    34  ) (metaModel.KVClient, error) {
    35  	cli, err := cc.GetConn()
    36  	if err != nil {
    37  		return nil, err
    38  	}
    39  
    40  	etcdCli, ok := cli.(*clientv3.Client)
    41  	if !ok {
    42  		return nil, errors.ErrMetaParamsInvalid.GenWithStack("invalid ClientConn type for etcd kvclient builder,"+
    43  			" conn type:%s", cc.StoreType())
    44  	}
    45  	impl, err := NewEtcdKVClientImpl(etcdCli)
    46  	if err != nil {
    47  		return nil, err
    48  	}
    49  	pfKV := namespace.NewPrefixKV(impl, namespace.MakeNamespacePrefix(projectID, jobID))
    50  	return &etcdKVClient{
    51  		Client: impl,
    52  		KV:     pfKV,
    53  	}, nil
    54  }
    55  
    56  // etcdKVClient is the implement of kv interface based on etcd
    57  // Support namespace isolation and all kv ability
    58  // etcdKVClientImpl -> kvPrefix+Closer -> etcdKVClient
    59  type etcdKVClient struct {
    60  	metaModel.Client
    61  	metaModel.KV
    62  }