forked from tsuna/gohbase
/
client.go
117 lines (102 loc) · 3.27 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
// 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 zk encapsulates our interactions with ZooKeeper.
package zk
import (
"encoding/binary"
"fmt"
"net"
"path"
"strings"
"time"
"github.com/go-zookeeper/zk"
"github.com/loveuer/gohbase/log"
"github.com/loveuer/gohbase/pb"
"google.golang.org/protobuf/proto"
)
type logger struct{}
func (l *logger) Printf(format string, args ...interface{}) {
log.Logger.Debugf(format, args...)
}
func init() {
zk.DefaultLogger = &logger{}
}
// ResourceName is a type alias that is used to represent different resources
// in ZooKeeper
type ResourceName string
// Prepend creates a new ResourceName with prefix prepended to the former ResourceName.
func (r ResourceName) Prepend(prefix string) ResourceName {
return ResourceName(path.Join(prefix, string(r)))
}
const (
// Meta is a ResourceName that indicates that the location of the Meta
// table is what will be fetched
Meta = ResourceName("/meta-region-server")
// Master is a ResourceName that indicates that the location of the Master
// server is what will be fetched
Master = ResourceName("/master")
)
// Client is an interface of client that retrieves meta infomation from zookeeper
type Client interface {
LocateResource(ResourceName) (string, error)
}
type client struct {
zks []string
sessionTimeout time.Duration
}
// NewClient establishes connection to zookeeper and returns the client
func NewClient(zkquorum string, st time.Duration) Client {
return &client{
zks: strings.Split(zkquorum, ","),
sessionTimeout: st,
}
}
// LocateResource returns address of the server for the specified resource.
func (c *client) LocateResource(resource ResourceName) (string, error) {
conn, _, err := zk.Connect(c.zks, c.sessionTimeout)
if err != nil {
return "", fmt.Errorf("error connecting to ZooKeeper at %v: %s", c.zks, err)
}
defer conn.Close()
buf, _, err := conn.Get(string(resource))
if err != nil {
return "", fmt.Errorf("failed to read the %s znode: %s", resource, err)
}
if len(buf) == 0 {
log.Logger.Fatalf("%s was empty!", resource)
} else if buf[0] != 0xFF {
return "", fmt.Errorf("the first byte of %s was 0x%x, not 0xFF", resource, buf[0])
}
metadataLen := binary.BigEndian.Uint32(buf[1:])
if metadataLen < 1 || metadataLen > 65000 {
return "", fmt.Errorf("invalid metadata length for %s: %d", resource, metadataLen)
}
buf = buf[1+4+metadataLen:]
magic := binary.BigEndian.Uint32(buf)
const pbufMagic = 1346524486 // 4 bytes: "PBUF"
if magic != pbufMagic {
return "", fmt.Errorf("invalid magic number for %s: %d", resource, magic)
}
buf = buf[4:]
var server *pb.ServerName
if resource == Meta {
meta := &pb.MetaRegionServer{}
err = proto.Unmarshal(buf, meta)
if err != nil {
return "",
fmt.Errorf("failed to deserialize the MetaRegionServer entry from ZK: %s", err)
}
server = meta.Server
} else {
master := &pb.Master{}
err = proto.Unmarshal(buf, master)
if err != nil {
return "",
fmt.Errorf("failed to deserialize the Master entry from ZK: %s", err)
}
server = master.Master
}
return net.JoinHostPort(*server.HostName, fmt.Sprint(*server.Port)), nil
}