github.com/tsuna/gohbase@v0.0.0-20250731002811-4ffcadfba63e/hrpc/move.go (about)

     1  // Copyright (C) 2020  The GoHBase Authors.  All rights reserved.
     2  // This file is part of GoHBase.
     3  // Use of this source code is governed by the Apache License 2.0
     4  // that can be found in the COPYING file.
     5  
     6  package hrpc
     7  
     8  import (
     9  	"context"
    10  	"errors"
    11  	"fmt"
    12  	"strconv"
    13  	"strings"
    14  
    15  	"github.com/tsuna/gohbase/pb"
    16  	"google.golang.org/protobuf/proto"
    17  )
    18  
    19  // MoveRegion allows to move region to a different RegionServer.
    20  type MoveRegion struct {
    21  	base
    22  	req *pb.MoveRegionRequest
    23  }
    24  
    25  // WithDestinationRegionServer specifies destination RegionServer for MoveReqion request
    26  // A server name is its host, port plus startcode: host187.example.com,60020,1289493121758
    27  func WithDestinationRegionServer(serverName string) func(Call) error {
    28  	return func(c Call) error {
    29  		mr, ok := c.(*MoveRegion)
    30  		if !ok {
    31  			return errors.New("WithDestinationRegionServer option can only be used with MoveRegion")
    32  		}
    33  		out := strings.SplitN(serverName, ",", 3)
    34  		if len(out) != 3 {
    35  			return errors.New(
    36  				"invalid server name, needs to be of format <host>,<port>,<startcode>")
    37  		}
    38  
    39  		// parse port
    40  		port, err := strconv.ParseUint(out[1], 10, 32)
    41  		if err != nil {
    42  			return fmt.Errorf("failed to parse port: %w", err)
    43  		}
    44  
    45  		// parse startCode
    46  		startCode, err := strconv.ParseUint(out[2], 10, 64)
    47  		if err != nil {
    48  			return fmt.Errorf("failed to parse startcode: %w", err)
    49  		}
    50  		mr.req.DestServerName = &pb.ServerName{
    51  			HostName:  proto.String(out[0]),
    52  			Port:      proto.Uint32(uint32(port)),
    53  			StartCode: proto.Uint64(startCode),
    54  		}
    55  		return nil
    56  	}
    57  }
    58  
    59  // NewMoveRegion creates an hrpc to move region to a different RegionServer.
    60  // Specify encoded region name.
    61  func NewMoveRegion(ctx context.Context, regionName []byte,
    62  	opts ...func(Call) error) (*MoveRegion, error) {
    63  	mr := &MoveRegion{
    64  		base: base{
    65  			ctx:      ctx,
    66  			resultch: make(chan RPCResult, 1),
    67  		},
    68  		req: &pb.MoveRegionRequest{
    69  			Region: &pb.RegionSpecifier{
    70  				Type:  pb.RegionSpecifier_ENCODED_REGION_NAME.Enum(),
    71  				Value: regionName,
    72  			},
    73  		},
    74  	}
    75  	if err := applyOptions(mr, opts...); err != nil {
    76  		return nil, err
    77  	}
    78  	return mr, nil
    79  }
    80  
    81  // Name returns the name of this RPC call.
    82  func (mr *MoveRegion) Name() string {
    83  	return "MoveRegion"
    84  }
    85  
    86  // Description returns the description of this RPC call.
    87  func (mr *MoveRegion) Description() string {
    88  	return mr.Name()
    89  }
    90  
    91  // ToProto converts the RPC into a protobuf message.
    92  func (mr *MoveRegion) ToProto() proto.Message {
    93  	return mr.req
    94  }
    95  
    96  // NewResponse creates an empty protobuf message to read the response of this RPC.
    97  func (mr *MoveRegion) NewResponse() proto.Message {
    98  	return &pb.MoveRegionResponse{}
    99  }