forked from tsuna/gohbase
/
client.go
309 lines (262 loc) · 7.98 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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
// Copyright (C) 2015 The GoHBase Authors. All rights reserved.
// This file is part of GoHBase.
// Use of this source code is governed by the Apache License 2.0
// that can be found in the COPYING file.
package gohbase
import (
"bytes"
"encoding/binary"
"fmt"
"sync"
"time"
log "github.com/Sirupsen/logrus"
"github.com/cznic/b"
"github.com/tsuna/gohbase/hrpc"
"github.com/tsuna/gohbase/internal/pb"
"github.com/tsuna/gohbase/region"
"github.com/tsuna/gohbase/zk"
)
const (
standardClient = iota
adminClient
defaultRPCQueueSize = 100
defaultFlushInterval = 20 * time.Millisecond
)
// Client a regular HBase client
type Client interface {
Scan(s *hrpc.Scan) ([]*hrpc.Result, error)
Get(g *hrpc.Get) (*hrpc.Result, error)
Put(p *hrpc.Mutate) (*hrpc.Result, error)
Delete(d *hrpc.Mutate) (*hrpc.Result, error)
Append(a *hrpc.Mutate) (*hrpc.Result, error)
Increment(i *hrpc.Mutate) (int64, error)
CheckAndPut(p *hrpc.Mutate, family string, qualifier string,
expectedValue []byte) (bool, error)
Close()
}
type Option func(*client)
// A Client provides access to an HBase cluster.
type client struct {
clientType int
regions keyRegionCache
// TODO: document what this protects.
regionsLock sync.Mutex
// Maps a hrpc.RegionInfo to the *region.Client that we think currently
// serves it.
clients clientRegionCache
metaRegionInfo hrpc.RegionInfo
adminRegionInfo hrpc.RegionInfo
// The maximum size of the RPC queue in the region client
rpcQueueSize int
// zkClient is zookeeper for retrieving meta and admin information
zkClient zk.Client
// The timeout before flushing the RPC queue in the region client
flushInterval time.Duration
// Master and Meta resource names
master zk.ResourceName
meta zk.ResourceName
}
// NewClient creates a new HBase client.
func NewClient(zkquorum string, options ...Option) Client {
return newClient(zkquorum, options...)
}
func newClient(zkquorum string, options ...Option) *client {
log.WithFields(log.Fields{
"Host": zkquorum,
}).Debug("Creating new client.")
c := &client{
clientType: standardClient,
regions: keyRegionCache{regions: b.TreeNew(region.CompareGeneric)},
clients: clientRegionCache{
regions: make(map[hrpc.RegionClient][]hrpc.RegionInfo),
},
rpcQueueSize: defaultRPCQueueSize,
flushInterval: defaultFlushInterval,
metaRegionInfo: region.NewInfo(
[]byte("hbase:meta"),
[]byte("hbase:meta,,1"),
nil,
nil),
zkClient: zk.NewClient(zkquorum),
master: zk.Master,
meta: zk.Meta,
}
for _, option := range options {
option(c)
}
return c
}
// RpcQueueSize will return an option that will set the size of the RPC queues
// used in a given client
func RpcQueueSize(size int) Option {
return func(c *client) {
c.rpcQueueSize = size
}
}
// FlushInterval will return an option that will set the timeout for flushing
// the RPC queues used in a given client
func FlushInterval(interval time.Duration) Option {
return func(c *client) {
c.flushInterval = interval
}
}
// ZKPath will return an option that will set the ZooKeeper resources to
// reference for the HBase master and meta region server
func ZKPath(zkRootPath string) Option {
return func(c *client) {
c.master = (zk.ResourceName)(zkRootPath + zk.MasterPath)
c.meta = (zk.ResourceName)(zkRootPath + zk.MetaPath)
}
}
// Close closes connections to hbase master and regionservers
func (c *client) Close() {
// TODO: do we need a lock for metaRegionInfo and adminRegionInfo
if c.clientType == adminClient {
if ac := c.adminRegionInfo.Client(); ac != nil {
ac.Close()
}
} else {
if mc := c.metaRegionInfo.Client(); mc != nil {
mc.Close()
}
}
c.clients.closeAll()
}
// Scan retrieves the values specified in families from the given range.
func (c *client) Scan(s *hrpc.Scan) ([]*hrpc.Result, error) {
var results []*pb.Result
var scanres *pb.ScanResponse
var rpc *hrpc.Scan
ctx := s.Context()
table := s.Table()
families := s.Families()
filters := s.Filter()
startRow := s.StartRow()
stopRow := s.StopRow()
fromTs, toTs := s.TimeRange()
maxVersions := s.MaxVersions()
numberOfRows := s.NumberOfRows()
for {
if rpc != nil {
// If it's not the first region, we want to start at whatever the
// last region's StopKey was
startRow = rpc.RegionStop()
}
// TODO: would be nicer to clone it in some way
var err error
rpc, err = hrpc.NewScanRange(ctx, table, startRow, stopRow,
hrpc.Families(families), hrpc.Filters(filters),
hrpc.TimeRangeUint64(fromTs, toTs),
hrpc.MaxVersions(maxVersions),
hrpc.NumberOfRows(numberOfRows))
if err != nil {
return nil, err
}
res, err := c.sendRPC(rpc)
if err != nil {
return nil, err
}
scanres = res.(*pb.ScanResponse)
results = append(results, scanres.Results...)
// TODO: The more_results field of the ScanResponse object was always
// true, so we should figure out if there's a better way to know when
// to move on to the next region than making an extra request and
// seeing if there were no results
for len(scanres.Results) != 0 && (numberOfRows == 0 || uint32(len(results)) < numberOfRows) {
rpc = hrpc.NewScanFromID(ctx, table, *scanres.ScannerId, rpc.Key())
res, err = c.sendRPC(rpc)
if err != nil {
return nil, err
}
scanres = res.(*pb.ScanResponse)
results = append(results, scanres.Results...)
}
rpc = hrpc.NewCloseFromID(ctx, table, *scanres.ScannerId, rpc.Key())
if err != nil {
return nil, err
}
res, err = c.sendRPC(rpc)
// Check to see if this region is the last we should scan (either
// because:
// (0) we got the number of rows user asked for
// (1) it's the last region or
// (3) because its stop_key is greater than or equal to the stop_key of this scanner provided
// that (2) we're not trying to scan until the end of the table).
if uint32(len(results)) >= numberOfRows ||
// (1)
len(rpc.RegionStop()) == 0 ||
// (2) (3)
(len(stopRow) != 0 && stopRow != nil && bytes.Compare(stopRow, rpc.RegionStop()) <= 0) {
// Do we want to be returning a slice of Result objects or should we just
// put all the Cells into the same Result object?
localResults := make([]*hrpc.Result, len(results))
for idx, result := range results {
localResults[idx] = hrpc.ToLocalResult(result)
}
return localResults, nil
}
}
}
func (c *client) Get(g *hrpc.Get) (*hrpc.Result, error) {
pbmsg, err := c.sendRPC(g)
if err != nil {
return nil, err
}
r, ok := pbmsg.(*pb.GetResponse)
if !ok {
return nil, fmt.Errorf("sendRPC returned not a GetResponse")
}
return hrpc.ToLocalResult(r.Result), nil
}
func (c *client) Put(p *hrpc.Mutate) (*hrpc.Result, error) {
return c.mutate(p)
}
func (c *client) Delete(d *hrpc.Mutate) (*hrpc.Result, error) {
return c.mutate(d)
}
func (c *client) Append(a *hrpc.Mutate) (*hrpc.Result, error) {
return c.mutate(a)
}
func (c *client) Increment(i *hrpc.Mutate) (int64, error) {
r, err := c.mutate(i)
if err != nil {
return 0, err
}
if len(r.Cells) != 1 {
return 0, fmt.Errorf("increment returned %d cells, but we expected exactly one",
len(r.Cells))
}
val := binary.BigEndian.Uint64(r.Cells[0].Value)
return int64(val), nil
}
func (c *client) mutate(m *hrpc.Mutate) (*hrpc.Result, error) {
pbmsg, err := c.sendRPC(m)
if err != nil {
return nil, err
}
r, ok := pbmsg.(*pb.MutateResponse)
if !ok {
return nil, fmt.Errorf("sendRPC returned not a MutateResponse")
}
return hrpc.ToLocalResult(r.Result), nil
}
func (c *client) CheckAndPut(p *hrpc.Mutate, family string,
qualifier string, expectedValue []byte) (bool, error) {
cas, err := hrpc.NewCheckAndPut(p, family, qualifier, expectedValue)
if err != nil {
return false, err
}
pbmsg, err := c.sendRPC(cas)
if err != nil {
return false, err
}
r, ok := pbmsg.(*pb.MutateResponse)
if !ok {
return false, fmt.Errorf("sendRPC returned a %T instead of MutateResponse", pbmsg)
}
if r.Processed == nil {
return false, fmt.Errorf("protobuf in the response didn't contain the field "+
"indicating whether the CheckAndPut was successful or not: %s", r)
}
return r.GetProcessed(), nil
}