github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/cdc/kv/utils.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 kv
    15  
    16  import (
    17  	"fmt"
    18  
    19  	"github.com/pingcap/errors"
    20  	tidbkv "github.com/pingcap/tidb/pkg/kv"
    21  	"github.com/pingcap/tidb/pkg/meta"
    22  	"github.com/pingcap/tidb/pkg/store/driver"
    23  	cerror "github.com/pingcap/tiflow/pkg/errors"
    24  	"github.com/pingcap/tiflow/pkg/flags"
    25  	"github.com/pingcap/tiflow/pkg/security"
    26  	tikvconfig "github.com/tikv/client-go/v2/config"
    27  )
    28  
    29  // GetSnapshotMeta returns tidb meta information
    30  func GetSnapshotMeta(tiStore tidbkv.Storage, ts uint64) *meta.Meta {
    31  	snapshot := tiStore.GetSnapshot(tidbkv.NewVersion(ts))
    32  	return meta.NewSnapshotMeta(snapshot)
    33  }
    34  
    35  // CreateTiStore creates a tikv storage client
    36  // Note: It will return a same storage if the urls connect to a same pd cluster,
    37  // so must be careful when you call storage.Close().
    38  func CreateTiStore(urls string, credential *security.Credential) (tidbkv.Storage, error) {
    39  	urlv, err := flags.NewURLsValue(urls)
    40  	if err != nil {
    41  		return nil, errors.Trace(err)
    42  	}
    43  
    44  	tiPath := fmt.Sprintf("tikv://%s?disableGC=true", urlv.HostString())
    45  	securityCfg := tikvconfig.Security{
    46  		ClusterSSLCA:    credential.CAPath,
    47  		ClusterSSLCert:  credential.CertPath,
    48  		ClusterSSLKey:   credential.KeyPath,
    49  		ClusterVerifyCN: credential.CertAllowedCN,
    50  	}
    51  	d := driver.TiKVDriver{}
    52  	// we should use OpenWithOptions to open a storage to avoid modifying tidb's GlobalConfig
    53  	// so that we can create different storage in TiCDC by different urls and credential
    54  	tiStore, err := d.OpenWithOptions(tiPath, driver.WithSecurity(securityCfg))
    55  	if err != nil {
    56  		return nil, cerror.WrapError(cerror.ErrNewStore, err)
    57  	}
    58  	return tiStore, nil
    59  }