forked from pingcap/tidb
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
store/tikv: add raw kv api. (pingcap#2101)
* store/tikv: add raw kv api.
- Loading branch information
Showing
11 changed files
with
2,583 additions
and
938 deletions.
There are no files selected for viewing
3,055 changes: 2,197 additions & 858 deletions
3,055
_vendor/src/github.com/pingcap/kvproto/pkg/kvrpcpb/kvrpcpb.pb.go
Large diffs are not rendered by default.
Oops, something went wrong.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
// Copyright 2016 PingCAP, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
package tikv | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/juju/errors" | ||
"github.com/pingcap/kvproto/pkg/kvrpcpb" | ||
"github.com/pingcap/pd/pd-client" | ||
) | ||
|
||
// RawKVClient is a client of TiKV server which is used as a key-value storage, | ||
// only GET/PUT/DELETE commands are supported. | ||
type RawKVClient struct { | ||
clusterID uint64 | ||
regionCache *RegionCache | ||
rpcClient Client | ||
} | ||
|
||
// NewRawKVClient creates a client with PD cluster addrs. | ||
func NewRawKVClient(pdAddrs []string) (*RawKVClient, error) { | ||
pdCli, err := pd.NewClient(pdAddrs) | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
return &RawKVClient{ | ||
clusterID: pdCli.GetClusterID(), | ||
regionCache: NewRegionCache(pdCli), | ||
rpcClient: newRPCClient(), | ||
}, nil | ||
} | ||
|
||
// ClusterID returns the TiKV cluster ID. | ||
func (c *RawKVClient) ClusterID() uint64 { | ||
return c.clusterID | ||
} | ||
|
||
// Get queries value with the key. When the key does not exist, it returns | ||
// `nil, nil`, while `[]byte{}, nil` means an empty value. | ||
func (c *RawKVClient) Get(key []byte) ([]byte, error) { | ||
req := &kvrpcpb.Request{ | ||
Type: kvrpcpb.MessageType_CmdRawGet, | ||
CmdRawGetReq: &kvrpcpb.CmdRawGetRequest{ | ||
Key: key, | ||
}, | ||
} | ||
resp, err := c.sendKVReq(key, req) | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
cmdResp := resp.GetCmdRawGetResp() | ||
if cmdResp == nil { | ||
return nil, errors.Trace(errBodyMissing) | ||
} | ||
if cmdResp.GetError() != "" { | ||
return nil, errors.New(cmdResp.GetError()) | ||
} | ||
return cmdResp.Value, nil | ||
} | ||
|
||
// Put stores a key-value pair to TiKV. | ||
func (c *RawKVClient) Put(key, value []byte) error { | ||
req := &kvrpcpb.Request{ | ||
Type: kvrpcpb.MessageType_CmdRawPut, | ||
CmdRawPutReq: &kvrpcpb.CmdRawPutRequest{ | ||
Key: key, | ||
Value: value, | ||
}, | ||
} | ||
resp, err := c.sendKVReq(key, req) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
cmdResp := resp.GetCmdRawPutResp() | ||
if cmdResp == nil { | ||
return errors.Trace(errBodyMissing) | ||
} | ||
if cmdResp.GetError() != "" { | ||
return errors.New(cmdResp.GetError()) | ||
} | ||
return nil | ||
} | ||
|
||
// Delete deletes a key-value pair from TiKV. | ||
func (c *RawKVClient) Delete(key []byte) error { | ||
req := &kvrpcpb.Request{ | ||
Type: kvrpcpb.MessageType_CmdRawDelete, | ||
CmdRawDeleteReq: &kvrpcpb.CmdRawDeleteRequest{ | ||
Key: key, | ||
}, | ||
} | ||
resp, err := c.sendKVReq(key, req) | ||
if err != nil { | ||
return errors.Trace(err) | ||
} | ||
cmdResp := resp.GetCmdRawDeleteResp() | ||
if cmdResp == nil { | ||
return errors.Trace(errBodyMissing) | ||
} | ||
if cmdResp.GetError() != "" { | ||
return errors.New(cmdResp.GetError()) | ||
} | ||
return nil | ||
} | ||
|
||
func (c *RawKVClient) sendKVReq(key []byte, req *kvrpcpb.Request) (*kvrpcpb.Response, error) { | ||
bo := NewBackoffer(rawkvMaxBackoff, context.Background()) | ||
for { | ||
region, err := c.regionCache.GetRegion(bo, key) | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
resp, err := sendKVReq(c.regionCache, c.rpcClient, bo, req, region.VerID(), readTimeoutShort) | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
if regionErr := resp.GetRegionError(); regionErr != nil { | ||
err := bo.Backoff(boRegionMiss, errors.New(regionErr.String())) | ||
if err != nil { | ||
return nil, errors.Trace(err) | ||
} | ||
continue | ||
} | ||
return resp, nil | ||
} | ||
} |
Oops, something went wrong.