forked from vitessio/vitess
-
Notifications
You must be signed in to change notification settings - Fork 0
/
zkoccconn.go
121 lines (99 loc) · 3.62 KB
/
zkoccconn.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
// Copyright 2012, Google Inc. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zk
import (
"fmt"
"math/rand"
"strings"
"time"
log "github.com/golang/glog"
"github.com/youtube/vitess/go/rpcplus"
"github.com/youtube/vitess/go/rpcwrap/bsonrpc"
"launchpad.net/gozk/zookeeper"
)
func init() {
rand.Seed(time.Now().UnixNano())
}
type ZkoccUnimplementedError string
func (e ZkoccUnimplementedError) Error() string {
return string("ZkoccConn doesn't implement " + e)
}
// ZkoccConn is a client class that implements zk.Conn
// but uses a RPC client to talk to a zkocc process
type ZkoccConn struct {
rpcClient *rpcplus.Client
}
// From the addr (of the form server1:port1,server2:port2,server3:port3:...)
// splits it on commas, randomizes the list, and tries to connect
// to the servers, stopping at the first successful connection
func DialZkocc(addr string, connectTimeout time.Duration) (zkocc *ZkoccConn, err error) {
servers := strings.Split(addr, ",")
perm := rand.Perm(len(servers))
for _, index := range perm {
server := servers[index]
rpcClient, err := bsonrpc.DialHTTP("tcp", server, connectTimeout, nil)
if err == nil {
return &ZkoccConn{rpcClient: rpcClient}, nil
}
log.Infof("zk conn cache: zkocc connection to %v failed: %v", server, err)
}
return nil, fmt.Errorf("zkocc connect failed: %v", addr)
}
func (conn *ZkoccConn) Get(path string) (data string, stat Stat, err error) {
zkPath := &ZkPath{path}
zkNode := &ZkNode{}
if err := conn.rpcClient.Call("ZkReader.Get", zkPath, zkNode); err != nil {
return "", nil, err
}
return zkNode.Data, &zkNode.Stat, nil
}
func (conn *ZkoccConn) GetW(path string) (data string, stat Stat, watch <-chan zookeeper.Event, err error) {
panic(ZkoccUnimplementedError("GetW"))
}
func (conn *ZkoccConn) Children(path string) (children []string, stat Stat, err error) {
zkPath := &ZkPath{path}
zkNode := &ZkNode{}
if err := conn.rpcClient.Call("ZkReader.Children", zkPath, zkNode); err != nil {
return nil, nil, err
}
return zkNode.Children, &zkNode.Stat, nil
}
func (conn *ZkoccConn) ChildrenW(path string) (children []string, stat Stat, watch <-chan zookeeper.Event, err error) {
panic(ZkoccUnimplementedError("ChildrenW"))
}
// implement Exists using Get
// FIXME(alainjobart) Maybe we should add Exists in rpc API?
func (conn *ZkoccConn) Exists(path string) (stat Stat, err error) {
zkPath := &ZkPath{path}
zkNode := &ZkNode{}
if err := conn.rpcClient.Call("ZkReader.Get", zkPath, zkNode); err != nil {
return nil, err
}
return &zkNode.Stat, nil
}
func (conn *ZkoccConn) ExistsW(path string) (stat Stat, watch <-chan zookeeper.Event, err error) {
panic(ZkoccUnimplementedError("ExistsW"))
}
func (conn *ZkoccConn) Create(path, value string, flags int, aclv []zookeeper.ACL) (pathCreated string, err error) {
panic(ZkoccUnimplementedError("Create"))
}
func (conn *ZkoccConn) Set(path, value string, version int) (stat Stat, err error) {
panic(ZkoccUnimplementedError("Set"))
}
func (conn *ZkoccConn) Delete(path string, version int) (err error) {
panic(ZkoccUnimplementedError("Delete"))
}
func (conn *ZkoccConn) Close() error {
return conn.rpcClient.Close()
}
func (conn *ZkoccConn) RetryChange(path string, flags int, acl []zookeeper.ACL, changeFunc ChangeFunc) error {
panic(ZkoccUnimplementedError("RetryChange"))
}
// might want to add ACL in RPC code
func (conn *ZkoccConn) ACL(path string) ([]zookeeper.ACL, Stat, error) {
panic(ZkoccUnimplementedError("ACL"))
}
func (conn *ZkoccConn) SetACL(path string, aclv []zookeeper.ACL, version int) error {
panic(ZkoccUnimplementedError("SetACL"))
}