-
Notifications
You must be signed in to change notification settings - Fork 41
/
Copy pathclient.go
294 lines (248 loc) · 8.16 KB
/
client.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
// Copyright 2020 MatrixOrigin.
//
// 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 client
import (
"context"
"sync"
"github.com/fagongzi/util/hack"
"github.com/fagongzi/util/protoc"
"github.com/matrixorigin/matrixcube/components/log"
"github.com/matrixorigin/matrixcube/pb/metapb"
"github.com/matrixorigin/matrixcube/pb/rpcpb"
"github.com/matrixorigin/matrixcube/pb/txnpb"
"github.com/matrixorigin/matrixcube/raftstore"
"github.com/matrixorigin/matrixcube/util/uuid"
"go.uber.org/zap"
)
var (
futurePool = sync.Pool{
New: func() interface{} {
return &Future{
c: make(chan struct{}, 1),
}
},
}
)
func acquireFuture() *Future {
f := futurePool.Get().(*Future)
f.mu.closed = false
return f
}
func releaseFuture(f *Future) {
f.reset()
futurePool.Put(f)
}
// Option client option
type Option func(*rpcpb.Request)
// WithShardGroup set shard group to execute the request
func WithShardGroup(group uint64) Option {
return func(req *rpcpb.Request) {
req.Group = group
}
}
// WithRouteKey use the specified key to route request
func WithRouteKey(key []byte) Option {
return func(req *rpcpb.Request) {
req.Key = key
}
}
// WithKeysRange If the current request operates on multiple Keys, set the range [from, to) of Keys
// operated by the current request. The client needs to split the request again if it wants
// to re-route according to KeysRange after the data management scope of the Shard has
// changed, or if it returns the specified error.
func WithKeysRange(from, to []byte) Option {
return func(req *rpcpb.Request) {
req.KeysRange = &rpcpb.Range{From: from, To: to}
}
}
// WithShard use the specified shard to route request
func WithShard(shard uint64) Option {
return func(req *rpcpb.Request) {
req.ToShard = shard
}
}
// WithReplicaSelectPolicy set the ReplicaSelectPolicy for request, default is SelectLeader
func WithReplicaSelectPolicy(policy rpcpb.ReplicaSelectPolicy) Option {
return func(req *rpcpb.Request) {
req.ReplicaSelectPolicy = policy
}
}
// WithLease set the Lease for request
func WithLease(lease *metapb.EpochLease) Option {
return func(req *rpcpb.Request) {
req.Lease = lease
}
}
// Client is a cube client, providing read and write access to the external.
type Client interface {
// Start start the cube client
Start() error
// Stop stop the cube client
Stop() error
// Router returns a Router with real-time updated routing table information
// inside for custom message routing
Router() raftstore.Router
// Admin exec the admin request, and use the `Future` to get the response.
Admin(ctx context.Context, requestType uint64, payload []byte, opts ...Option) *Future
// Write exec the write request, and use the `Future` to get the response.
Write(ctx context.Context, requestType uint64, payload []byte, opts ...Option) *Future
// Read exec the read request, and use the `Future` to get the response
Read(ctx context.Context, requestType uint64, payload []byte, opts ...Option) *Future
// Txn exec the transaction request, and use the `Future` to get the response
Txn(ctx context.Context, request txnpb.TxnBatchRequest, opts ...Option) *Future
// AddLabelToShard add lable to shard, and use the `Future` to get the response
AddLabelToShard(ctx context.Context, name, value string, shard uint64) *Future
}
var _ Client = (*client)(nil)
// client a tcp application server
type client struct {
logger *zap.Logger
shardsProxy raftstore.ShardsProxy
mu struct {
sync.RWMutex
inflights map[string]*Future
}
}
// NewClient creates and return a cube client
func NewClient(cfg Cfg) Client {
return NewClientWithOptions(CreateWithLogger(cfg.Store.GetConfig().Logger.Named("cube-client")),
CreateWithShardsProxy(cfg.Store.GetShardsProxy()))
}
// NewClientWithOptions create client with options
func NewClientWithOptions(options ...CreateOption) Client {
c := &client{}
for _, opt := range options {
opt(c)
}
c.adjust()
c.mu.inflights = make(map[string]*Future, 1024)
return c
}
func (s *client) adjust() {
if s.logger == nil {
s.logger = log.Adjust(nil).Named("cube-client")
}
if s.shardsProxy == nil {
s.logger.Fatal("ShardsProxy not set")
}
}
func (s *client) Start() error {
s.logger.Info("begin to start cube client")
s.shardsProxy.SetCallback(s.done, s.doneError)
s.shardsProxy.SetRetryController(s)
s.logger.Info("cube client started")
return nil
}
func (s *client) Stop() error {
s.logger.Info("cube client stopped")
return nil
}
func (s *client) Router() raftstore.Router {
return s.shardsProxy.Router()
}
func (s *client) Write(ctx context.Context, requestType uint64, payload []byte, opts ...Option) *Future {
return s.exec(ctx, requestType, payload, rpcpb.Write, nil, opts...)
}
func (s *client) Read(ctx context.Context, requestType uint64, payload []byte, opts ...Option) *Future {
return s.exec(ctx, requestType, payload, rpcpb.Read, nil, opts...)
}
func (s *client) Admin(ctx context.Context, requestType uint64, payload []byte, opts ...Option) *Future {
return s.exec(ctx, requestType, payload, rpcpb.Admin, nil, opts...)
}
func (s *client) Txn(ctx context.Context, request txnpb.TxnBatchRequest, opts ...Option) *Future {
return s.exec(ctx, 0, nil, rpcpb.Txn, &request, opts...)
}
func (s *client) AddLabelToShard(ctx context.Context, name, value string, shard uint64) *Future {
payload := protoc.MustMarshal(&rpcpb.UpdateLabelsRequest{
Labels: []metapb.Label{{Key: name, Value: value}},
Policy: rpcpb.Add,
})
return s.exec(ctx, uint64(rpcpb.CmdUpdateLabels), payload, rpcpb.Admin, nil, WithShard(shard))
}
func (s *client) exec(ctx context.Context, requestType uint64, payload []byte, cmdType rpcpb.CmdType, txnRequest *txnpb.TxnBatchRequest, opts ...Option) *Future {
f := newFuture(ctx)
f.req.ID = uuid.NewV4().Bytes()
f.req.Type = cmdType
f.req.CustomType = requestType
f.req.Cmd = payload
f.req.TxnBatchRequest = txnRequest
for _, opt := range opts {
opt(&f.req)
}
id := hack.SliceToString(f.req.ID)
s.addInfight(id, f)
f.cancel = func() {
s.deleteInfight(id)
}
if len(f.req.Key) > 0 && f.req.ToShard > 0 {
s.logger.Fatal("route with key and route with shard cannot be set at the same time")
}
if _, ok := ctx.Deadline(); !ok {
s.logger.Fatal("cube client must use timeout context")
}
if ce := s.logger.Check(zap.DebugLevel, "begin to send request"); ce != nil {
ce.Write(log.RequestIDField(f.req.ID))
}
if err := s.shardsProxy.Dispatch(f.req); err != nil {
f.done(nil, nil, err)
}
return f
}
func (s *client) Retry(requestID []byte) (rpcpb.Request, bool) {
if f, ok := s.getInfight(hack.SliceToString(requestID)); ok {
if f.canRetry() {
return f.req, true
}
}
return rpcpb.Request{}, false
}
func (s *client) done(resp rpcpb.Response) {
if ce := s.logger.Check(zap.DebugLevel, "response received"); ce != nil {
ce.Write(log.RequestIDField(resp.ID))
}
id := hack.SliceToString(resp.ID)
if f, ok := s.getInfight(id); ok {
s.deleteInfight(id)
f.done(resp.Value, resp.TxnBatchResponse, nil)
} else {
if ce := s.logger.Check(zap.DebugLevel, "response skipped"); ce != nil {
ce.Write(log.RequestIDField(resp.ID), log.ReasonField("missing ctx"))
}
}
}
func (s *client) doneError(requestID []byte, err error) {
if ce := s.logger.Check(zap.DebugLevel, "error response received"); ce != nil {
ce.Write(log.RequestIDField(requestID), zap.Error(err))
}
id := hack.SliceToString(requestID)
if f, ok := s.getInfight(id); ok {
s.deleteInfight(id)
f.done(nil, nil, err)
}
}
func (s *client) addInfight(id string, f *Future) {
s.mu.Lock()
defer s.mu.Unlock()
s.mu.inflights[hack.SliceToString(f.req.ID)] = f
}
func (s *client) deleteInfight(id string) {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.mu.inflights, id)
}
func (s *client) getInfight(id string) (*Future, bool) {
s.mu.RLock()
defer s.mu.RUnlock()
v, ok := s.mu.inflights[id]
return v, ok
}