github.com/KinWaiYuen/client-go/v2@v2.5.4/internal/locate/pd_codec.go (about)

     1  // Copyright 2021 TiKV Authors
     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  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  // NOTE: The code in this file is based on code from the
    16  // TiDB project, licensed under the Apache License v 2.0
    17  //
    18  // https://github.com/pingcap/tidb/tree/cc5e161ac06827589c4966674597c137cc9e809c/store/tikv/locate/pd_codec.go
    19  //
    20  
    21  // Copyright 2016 PingCAP, Inc.
    22  //
    23  // Licensed under the Apache License, Version 2.0 (the "License");
    24  // you may not use this file except in compliance with the License.
    25  // You may obtain a copy of the License at
    26  //
    27  //     http://www.apache.org/licenses/LICENSE-2.0
    28  //
    29  // Unless required by applicable law or agreed to in writing, software
    30  // distributed under the License is distributed on an "AS IS" BASIS,
    31  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    32  // See the License for the specific language governing permissions and
    33  // limitations under the License.
    34  
    35  package locate
    36  
    37  import (
    38  	"context"
    39  
    40  	"github.com/KinWaiYuen/client-go/v2/util/codec"
    41  	"github.com/pingcap/errors"
    42  	"github.com/pingcap/kvproto/pkg/metapb"
    43  	pd "github.com/tikv/pd/client"
    44  )
    45  
    46  // CodecPDClient wraps a PD Client to decode the encoded keys in region meta.
    47  type CodecPDClient struct {
    48  	pd.Client
    49  }
    50  
    51  // NewCodeCPDClient creates a CodecPDClient.
    52  func NewCodeCPDClient(client pd.Client) *CodecPDClient {
    53  	return &CodecPDClient{client}
    54  }
    55  
    56  // GetRegion encodes the key before send requests to pd-server and decodes the
    57  // returned StartKey && EndKey from pd-server.
    58  func (c *CodecPDClient) GetRegion(ctx context.Context, key []byte) (*pd.Region, error) {
    59  	encodedKey := codec.EncodeBytes([]byte(nil), key)
    60  	region, err := c.Client.GetRegion(ctx, encodedKey)
    61  	return processRegionResult(region, err)
    62  }
    63  
    64  // GetPrevRegion encodes the key before send requests to pd-server and decodes the
    65  // returned StartKey && EndKey from pd-server.
    66  func (c *CodecPDClient) GetPrevRegion(ctx context.Context, key []byte) (*pd.Region, error) {
    67  	encodedKey := codec.EncodeBytes([]byte(nil), key)
    68  	region, err := c.Client.GetPrevRegion(ctx, encodedKey)
    69  	return processRegionResult(region, err)
    70  }
    71  
    72  // GetRegionByID encodes the key before send requests to pd-server and decodes the
    73  // returned StartKey && EndKey from pd-server.
    74  func (c *CodecPDClient) GetRegionByID(ctx context.Context, regionID uint64) (*pd.Region, error) {
    75  	region, err := c.Client.GetRegionByID(ctx, regionID)
    76  	return processRegionResult(region, err)
    77  }
    78  
    79  // ScanRegions encodes the key before send requests to pd-server and decodes the
    80  // returned StartKey && EndKey from pd-server.
    81  func (c *CodecPDClient) ScanRegions(ctx context.Context, startKey []byte, endKey []byte, limit int) ([]*pd.Region, error) {
    82  	startKey = codec.EncodeBytes([]byte(nil), startKey)
    83  	if len(endKey) > 0 {
    84  		endKey = codec.EncodeBytes([]byte(nil), endKey)
    85  	}
    86  
    87  	regions, err := c.Client.ScanRegions(ctx, startKey, endKey, limit)
    88  	if err != nil {
    89  		return nil, errors.Trace(err)
    90  	}
    91  	for _, region := range regions {
    92  		if region != nil {
    93  			err = decodeRegionMetaKeyInPlace(region.Meta)
    94  			if err != nil {
    95  				return nil, errors.Trace(err)
    96  			}
    97  		}
    98  	}
    99  	return regions, nil
   100  }
   101  
   102  func processRegionResult(region *pd.Region, err error) (*pd.Region, error) {
   103  	if err != nil {
   104  		return nil, errors.Trace(err)
   105  	}
   106  	if region == nil || region.Meta == nil {
   107  		return nil, nil
   108  	}
   109  	err = decodeRegionMetaKeyInPlace(region.Meta)
   110  	if err != nil {
   111  		return nil, errors.Trace(err)
   112  	}
   113  	return region, nil
   114  }
   115  
   116  // decodeError happens if the region range key is not well-formed.
   117  // It indicates TiKV has bugs and the client can't handle such a case,
   118  // so it should report the error to users soon.
   119  type decodeError struct {
   120  	error
   121  }
   122  
   123  func isDecodeError(err error) bool {
   124  	_, ok := errors.Cause(err).(*decodeError)
   125  	if !ok {
   126  		_, ok = errors.Cause(err).(decodeError)
   127  	}
   128  	return ok
   129  }
   130  
   131  func decodeRegionMetaKeyInPlace(r *metapb.Region) error {
   132  	if len(r.StartKey) != 0 {
   133  		_, decoded, err := codec.DecodeBytes(r.StartKey, nil)
   134  		if err != nil {
   135  			return &decodeError{err}
   136  		}
   137  		r.StartKey = decoded
   138  	}
   139  	if len(r.EndKey) != 0 {
   140  		_, decoded, err := codec.DecodeBytes(r.EndKey, nil)
   141  		if err != nil {
   142  			return &decodeError{err}
   143  		}
   144  		r.EndKey = decoded
   145  	}
   146  	return nil
   147  }
   148  
   149  func decodeRegionMetaKeyWithShallowCopy(r *metapb.Region) (*metapb.Region, error) {
   150  	nr := *r
   151  	if len(r.StartKey) != 0 {
   152  		_, decoded, err := codec.DecodeBytes(r.StartKey, nil)
   153  		if err != nil {
   154  			return nil, errors.Trace(err)
   155  		}
   156  		nr.StartKey = decoded
   157  	}
   158  	if len(r.EndKey) != 0 {
   159  		_, decoded, err := codec.DecodeBytes(r.EndKey, nil)
   160  		if err != nil {
   161  			return nil, errors.Trace(err)
   162  		}
   163  		nr.EndKey = decoded
   164  	}
   165  	return &nr, nil
   166  }