-
Notifications
You must be signed in to change notification settings - Fork 213
/
admin_client.go
297 lines (252 loc) · 7.33 KB
/
admin_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
// Copyright (C) 2016 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 (
"context"
"errors"
"fmt"
"log/slog"
"time"
"github.com/tsuna/gohbase/hrpc"
"github.com/tsuna/gohbase/pb"
"github.com/tsuna/gohbase/region"
"github.com/tsuna/gohbase/zk"
)
const (
// snapshotValidateInterval specifies the amount of time to wait before
// polling the hbase server about the status of a snapshot operation.
snaphotValidateInterval time.Duration = time.Second / 2
)
// AdminClient to perform administrative operations with HMaster
type AdminClient interface {
CreateTable(t *hrpc.CreateTable) error
DeleteTable(t *hrpc.DeleteTable) error
EnableTable(t *hrpc.EnableTable) error
DisableTable(t *hrpc.DisableTable) error
CreateSnapshot(t *hrpc.Snapshot) error
DeleteSnapshot(t *hrpc.Snapshot) error
ListSnapshots(t *hrpc.ListSnapshots) ([]*pb.SnapshotDescription, error)
RestoreSnapshot(t *hrpc.Snapshot) error
ClusterStatus() (*pb.ClusterStatus, error)
ListTableNames(t *hrpc.ListTableNames) ([]*pb.TableName, error)
// SetBalancer sets balancer state and returns previous state
SetBalancer(sb *hrpc.SetBalancer) (bool, error)
// MoveRegion moves a region to a different RegionServer
MoveRegion(mr *hrpc.MoveRegion) error
}
// NewAdminClient creates an admin HBase client.
func NewAdminClient(zkquorum string, options ...Option) AdminClient {
return newAdminClient(zkquorum, options...)
}
func newAdminClient(zkquorum string, options ...Option) AdminClient {
c := &client{
clientType: region.MasterClient,
rpcQueueSize: defaultRPCQueueSize,
flushInterval: defaultFlushInterval,
// empty region in order to be able to set client to it
adminRegionInfo: region.NewInfo(0, nil, nil, nil, nil, nil),
zkTimeout: defaultZkTimeout,
zkRoot: defaultZkRoot,
effectiveUser: defaultEffectiveUser,
regionLookupTimeout: region.DefaultLookupTimeout,
regionReadTimeout: region.DefaultReadTimeout,
newRegionClientFn: region.NewClient,
logger: slog.Default(),
}
for _, option := range options {
option(c)
}
c.logger.Debug("Creating new admin client.", "Host", slog.StringValue(zkquorum))
c.zkClient = zk.NewClient(zkquorum, c.zkTimeout, c.zkDialer, c.logger)
return c
}
// Get the status of the cluster
func (c *client) ClusterStatus() (*pb.ClusterStatus, error) {
pbmsg, err := c.SendRPC(hrpc.NewClusterStatus())
if err != nil {
return nil, err
}
r, ok := pbmsg.(*pb.GetClusterStatusResponse)
if !ok {
return nil, fmt.Errorf("sendRPC returned not a ClusterStatusResponse")
}
return r.GetClusterStatus(), nil
}
func (c *client) CreateTable(t *hrpc.CreateTable) error {
pbmsg, err := c.SendRPC(t)
if err != nil {
return err
}
r, ok := pbmsg.(*pb.CreateTableResponse)
if !ok {
return fmt.Errorf("sendRPC returned not a CreateTableResponse")
}
return c.checkProcedureWithBackoff(t.Context(), r.GetProcId())
}
func (c *client) DeleteTable(t *hrpc.DeleteTable) error {
pbmsg, err := c.SendRPC(t)
if err != nil {
return err
}
r, ok := pbmsg.(*pb.DeleteTableResponse)
if !ok {
return fmt.Errorf("sendRPC returned not a DeleteTableResponse")
}
return c.checkProcedureWithBackoff(t.Context(), r.GetProcId())
}
func (c *client) EnableTable(t *hrpc.EnableTable) error {
pbmsg, err := c.SendRPC(t)
if err != nil {
return err
}
r, ok := pbmsg.(*pb.EnableTableResponse)
if !ok {
return fmt.Errorf("sendRPC returned not a EnableTableResponse")
}
return c.checkProcedureWithBackoff(t.Context(), r.GetProcId())
}
func (c *client) DisableTable(t *hrpc.DisableTable) error {
pbmsg, err := c.SendRPC(t)
if err != nil {
return err
}
r, ok := pbmsg.(*pb.DisableTableResponse)
if !ok {
return fmt.Errorf("sendRPC returned not a DisableTableResponse")
}
return c.checkProcedureWithBackoff(t.Context(), r.GetProcId())
}
func (c *client) checkProcedureWithBackoff(ctx context.Context, procID uint64) error {
backoff := backoffStart
for {
pbmsg, err := c.SendRPC(hrpc.NewGetProcedureState(ctx, procID))
if err != nil {
return err
}
res := pbmsg.(*pb.GetProcedureResultResponse)
switch res.GetState() {
case pb.GetProcedureResultResponse_NOT_FOUND:
return fmt.Errorf("procedure not found")
case pb.GetProcedureResultResponse_FINISHED:
if fe := res.Exception; fe != nil {
ge := fe.GenericException
if ge == nil {
return errors.New("got unexpected empty exception")
}
return fmt.Errorf("procedure exception: %s: %s", ge.GetClassName(), ge.GetMessage())
}
return nil
default:
backoff, err = sleepAndIncreaseBackoff(ctx, backoff)
if err != nil {
return err
}
}
}
}
// CreateSnapshot creates a snapshot in HBase.
//
// If a context happens during creation, no cleanup is done.
func (c *client) CreateSnapshot(t *hrpc.Snapshot) error {
pbmsg, err := c.SendRPC(t)
if err != nil {
return err
}
_, ok := pbmsg.(*pb.SnapshotResponse)
if !ok {
return errors.New("sendPRC returned not a SnapshotResponse")
}
ticker := time.NewTicker(snaphotValidateInterval)
defer ticker.Stop()
check := hrpc.NewSnapshotDone(t)
ctx := t.Context()
for {
select {
case <-ticker.C:
pbmsgs, err := c.SendRPC(check)
if err != nil {
return err
}
r, ok := pbmsgs.(*pb.IsSnapshotDoneResponse)
if !ok {
return errors.New("sendPRC returned not a IsSnapshotDoneResponse")
}
if r.GetDone() {
return nil
}
case <-ctx.Done():
return ctx.Err()
}
}
}
// DeleteSnapshot deletes a snapshot in HBase.
func (c *client) DeleteSnapshot(t *hrpc.Snapshot) error {
rt := hrpc.NewDeleteSnapshot(t)
pbmsg, err := c.SendRPC(rt)
if err != nil {
return err
}
_, ok := pbmsg.(*pb.DeleteSnapshotResponse)
if !ok {
return errors.New("sendPRC returned not a DeleteSnapshotResponse")
}
return nil
}
func (c *client) ListSnapshots(t *hrpc.ListSnapshots) ([]*pb.SnapshotDescription, error) {
pbmsg, err := c.SendRPC(t)
if err != nil {
return nil, err
}
r, ok := pbmsg.(*pb.GetCompletedSnapshotsResponse)
if !ok {
return nil, errors.New("sendPRC returned not a GetCompletedSnapshotsResponse")
}
return r.GetSnapshots(), nil
}
func (c *client) RestoreSnapshot(t *hrpc.Snapshot) error {
rt := hrpc.NewRestoreSnapshot(t)
pbmsg, err := c.SendRPC(rt)
if err != nil {
return err
}
_, ok := pbmsg.(*pb.RestoreSnapshotResponse)
if !ok {
return errors.New("sendPRC returned not a RestoreSnapshotResponse")
}
return nil
}
func (c *client) ListTableNames(t *hrpc.ListTableNames) ([]*pb.TableName, error) {
pbmsg, err := c.SendRPC(t)
if err != nil {
return nil, err
}
res, ok := pbmsg.(*pb.GetTableNamesResponse)
if !ok {
return nil, errors.New("sendPRC returned not a GetTableNamesResponse")
}
return res.GetTableNames(), nil
}
func (c *client) SetBalancer(sb *hrpc.SetBalancer) (bool, error) {
pbmsg, err := c.SendRPC(sb)
if err != nil {
return false, err
}
res, ok := pbmsg.(*pb.SetBalancerRunningResponse)
if !ok {
return false, errors.New("SendPRC returned not a SetBalancerRunningResponse")
}
return res.GetPrevBalanceValue(), nil
}
func (c *client) MoveRegion(mr *hrpc.MoveRegion) error {
pbmsg, err := c.SendRPC(mr)
if err != nil {
return err
}
_, ok := pbmsg.(*pb.MoveRegionResponse)
if !ok {
return errors.New("SendPRC returned not a MoveRegionResponse")
}
return nil
}