-
Notifications
You must be signed in to change notification settings - Fork 0
/
kvclient.go
149 lines (125 loc) · 3 KB
/
kvclient.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
package sdk
import (
"context"
"encoding/binary"
"errors"
"github.com/distributedio/kvproto/pkg/spannerpb"
"google.golang.org/grpc"
)
var (
ErrNotFound = errors.New("not found")
ErrRegionError = errors.New("region error")
)
type Type int
const (
OpPut Type = 0
OpDelete Type = 1
OpRead Type = 2
ErrorCode_NotFound = 1
ErrorCode_RegionError = 2
)
type Operation struct {
Type Type
Key []byte
Value []byte
}
type TiKVClient interface {
Get(ctx context.Context, txnID uint64, key []byte, version uint64, readOnly bool) ([]byte, error)
Commit(ctx context.Context, txnID uint64, operations []Operation, coordinatorId uint64, participants []uint64) (uint64, error)
HeartBeat(ctx context.Context, txnID uint64, ttl uint64) error
Close() error
}
type tikvClient struct {
conn *grpc.ClientConn
cli spannerpb.SpannerClient
}
func NewTiKV(address string) (TiKVClient, error) {
conn, err := grpc.Dial(address, grpc.WithInsecure())
if err != nil {
return nil, err
}
cli := spannerpb.NewSpannerClient(conn)
return &tikvClient{
conn: conn,
cli: cli,
}, nil
}
func (kv *tikvClient) Get(ctx context.Context, txnID uint64, key []byte, version uint64, readOnly bool) ([]byte, error) {
req := &spannerpb.GetRequest{
TxnId: txnID,
Key: key,
Version: version,
ReadOnly: readOnly,
}
var resp *spannerpb.GetResponse
resp, err := kv.cli.Get(ctx, req)
if err != nil {
return nil, err
}
if resp.GetErrorCode() != 0 {
// TODO
return nil, ErrRegionError
}
return resp.Value, nil
}
func (kv *tikvClient) Commit(ctx context.Context, txnID uint64, operations []Operation, coordinatorId uint64, participantIDs []uint64) (uint64, error) {
lid := make([]byte, 8)
binary.BigEndian.PutUint64(lid, coordinatorId)
ids := make([][]byte, 0, 0xF)
for _, v := range participantIDs {
id := make([]byte, 8)
binary.BigEndian.PutUint64(id, v)
ids = append(ids, id)
}
req := &spannerpb.CommitRequest{
TxnId: txnID,
CoordinatorId: lid,
ParticipantIds: ids,
}
for i := range operations {
op := &spannerpb.Operation{
Key: operations[i].Key,
Value: operations[i].Value,
}
switch operations[i].Type {
case OpPut:
op.Type = spannerpb.Type_Put
case OpDelete:
op.Type = spannerpb.Type_Delete
case OpRead:
op.Type = spannerpb.Type_Read
default:
return 0, errors.New("Unknown operation type")
}
req.Operations = append(req.Operations, op)
}
var resp *spannerpb.CommitResponse
resp, err := kv.cli.Commit(ctx, req)
if err != nil {
return 0, err
}
if resp.ErrorCode != 0 {
//TODO
return 0, ErrRegionError
}
return resp.Version, nil
}
func (kv *tikvClient) HeartBeat(ctx context.Context, txnID uint64, ttl uint64) error {
req := &spannerpb.HeartBeatRequest{
TxnId: txnID,
Ttl: ttl,
}
var resp *spannerpb.HeartBeatResponse
resp, err := kv.cli.HeartBeat(ctx, req)
if err != nil {
return err
}
if resp.ErrorCode != 0 {
//TODO
return ErrRegionError
}
return nil
}
func (kv *tikvClient) Close() error {
return kv.conn.Close()
}