github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/engine/pkg/client/discovery_client.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 client
    15  
    16  import (
    17  	"context"
    18  
    19  	pb "github.com/pingcap/tiflow/engine/enginepb"
    20  	"github.com/pingcap/tiflow/engine/model"
    21  	"github.com/pingcap/tiflow/engine/pkg/client/internal"
    22  	"github.com/pingcap/tiflow/pkg/errors"
    23  )
    24  
    25  // DiscoveryClient is a client to the Discovery service on the server master.
    26  type DiscoveryClient interface {
    27  	// RegisterExecutor registers an executor. The server
    28  	// will allocate and records a UUID.
    29  	RegisterExecutor(
    30  		ctx context.Context,
    31  		request *pb.RegisterExecutorRequest,
    32  	) (model.ExecutorID, error)
    33  
    34  	// ListExecutors lists all executors.
    35  	ListExecutors(ctx context.Context) ([]*pb.Executor, error)
    36  
    37  	// ListMasters lists all masters.
    38  	ListMasters(ctx context.Context) ([]*pb.Master, error)
    39  
    40  	// Heartbeat sends a heartbeat message to the server.
    41  	Heartbeat(
    42  		ctx context.Context,
    43  		request *pb.HeartbeatRequest,
    44  	) (*pb.HeartbeatResponse, error)
    45  
    46  	// QueryMetaStore queries the details of a metastore.
    47  	QueryMetaStore(
    48  		ctx context.Context,
    49  		request *pb.QueryMetaStoreRequest,
    50  	) (*pb.QueryMetaStoreResponse, error)
    51  
    52  	// QueryStorageConfig queries the storage config.
    53  	QueryStorageConfig(
    54  		ctx context.Context,
    55  		in *pb.QueryStorageConfigRequest,
    56  	) (*pb.QueryStorageConfigResponse, error)
    57  }
    58  
    59  var _ DiscoveryClient = &discoveryClient{}
    60  
    61  type discoveryClient struct {
    62  	cli pb.DiscoveryClient
    63  }
    64  
    65  // NewDiscoveryClient returns a DiscoveryClient.
    66  func NewDiscoveryClient(cli pb.DiscoveryClient) DiscoveryClient {
    67  	return &discoveryClient{cli: cli}
    68  }
    69  
    70  func (c *discoveryClient) RegisterExecutor(
    71  	ctx context.Context,
    72  	request *pb.RegisterExecutorRequest,
    73  ) (model.ExecutorID, error) {
    74  	var ret model.ExecutorID
    75  	call := internal.NewCall(
    76  		c.cli.RegisterExecutor,
    77  		request,
    78  		// RegisterExecutor is not idempotent in general
    79  		// TODO review idempotency
    80  		// internal.WithForceNoRetry()
    81  	)
    82  	executor, err := call.Do(ctx)
    83  	if err != nil {
    84  		return "", errors.Trace(err)
    85  	}
    86  	ret = model.ExecutorID(executor.Id)
    87  	return ret, nil
    88  }
    89  
    90  func (c *discoveryClient) ListExecutors(ctx context.Context) ([]*pb.Executor, error) {
    91  	call := internal.NewCall(c.cli.ListExecutors, &pb.ListExecutorsRequest{})
    92  	resp, err := call.Do(ctx)
    93  	if err != nil {
    94  		return nil, err
    95  	}
    96  	return resp.Executors, nil
    97  }
    98  
    99  func (c *discoveryClient) ListMasters(ctx context.Context) ([]*pb.Master, error) {
   100  	call := internal.NewCall(c.cli.ListMasters, &pb.ListMastersRequest{})
   101  	resp, err := call.Do(ctx)
   102  	if err != nil {
   103  		return nil, err
   104  	}
   105  	return resp.Masters, nil
   106  }
   107  
   108  // Heartbeat sends a heartbeat to the DiscoveryService.
   109  // Note: HeartbeatResponse contains Leader & Addr, which gives the call
   110  // "Heartbeat" double responsibilities, i.e., keep-alive and get-members.
   111  // TODO refactor this.
   112  func (c *discoveryClient) Heartbeat(
   113  	ctx context.Context,
   114  	request *pb.HeartbeatRequest,
   115  ) (*pb.HeartbeatResponse, error) {
   116  	call := internal.NewCall(
   117  		c.cli.Heartbeat,
   118  		request,
   119  		// No need to retry heartbeats
   120  		internal.WithForceNoRetry())
   121  	return call.Do(ctx)
   122  }
   123  
   124  func (c *discoveryClient) QueryMetaStore(
   125  	ctx context.Context,
   126  	request *pb.QueryMetaStoreRequest,
   127  ) (*pb.QueryMetaStoreResponse, error) {
   128  	call := internal.NewCall(
   129  		c.cli.QueryMetaStore,
   130  		request)
   131  	return call.Do(ctx)
   132  }
   133  
   134  func (c *discoveryClient) QueryStorageConfig(
   135  	ctx context.Context,
   136  	request *pb.QueryStorageConfigRequest,
   137  ) (*pb.QueryStorageConfigResponse, error) {
   138  	call := internal.NewCall(
   139  		c.cli.QueryStorageConfig,
   140  		request)
   141  	return call.Do(ctx)
   142  }