-
Notifications
You must be signed in to change notification settings - Fork 26
/
proxy.go
196 lines (165 loc) · 4.84 KB
/
proxy.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
// Copyright 2014 Wandoujia Inc. All Rights Reserved.
// Licensed under the MIT (MIT-LICENSE.txt) license.
package models
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"path"
"github.com/ngaut/zkhelper"
"github.com/juju/errors"
"github.com/ngaut/go-zookeeper/zk"
log "github.com/ngaut/logging"
)
const (
PROXY_STATE_ONLINE = "online"
PROXY_STATE_OFFLINE = "offline"
PROXY_STATE_MARK_OFFLINE = "mark_offline"
)
type ProxyInfo struct {
Id string `json:"id"`
Addr string `json:"addr"`
LastEvent string `json:"last_event"`
LastEventTs int64 `json:"last_event_ts"`
State string `json:"state"`
Description string `json:"description"`
DebugVarAddr string `json:"debug_var_addr"`
}
func (p ProxyInfo) Ops() (int64, error) {
resp, err := http.Get("http://" + p.DebugVarAddr + "/debug/vars")
if err != nil {
return -1, errors.Trace(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return -1, errors.Trace(err)
}
m := make(map[string]interface{})
err = json.Unmarshal(body, &m)
if err != nil {
return -1, errors.Trace(err)
}
if v, ok := m["router"]; ok {
if vv, ok := v.(map[string]interface{})["ops"]; ok {
return int64(vv.(float64)), nil
}
}
return 0, nil
}
func (p ProxyInfo) DebugVars() (map[string]interface{}, error) {
resp, err := http.Get("http://" + p.DebugVarAddr + "/debug/vars")
if err != nil {
return nil, errors.Trace(err)
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return nil, errors.Trace(err)
}
m := make(map[string]interface{})
err = json.Unmarshal(body, &m)
if err != nil {
return nil, errors.Trace(err)
}
return m, nil
}
func GetProxyPath(productName string) string {
return fmt.Sprintf("/zk/codis/db_%s/proxy", productName)
}
func CreateProxyInfo(zkConn zkhelper.Conn, productName string, pi *ProxyInfo) (string, error) {
data, err := json.Marshal(pi)
if err != nil {
return "", errors.Trace(err)
}
zkhelper.CreateRecursive(zkConn, GetProxyPath(productName), string(data),
0, zkhelper.DefaultDirACLs())
return zkConn.Create(path.Join(GetProxyPath(productName), pi.Id), data,
zk.FlagEphemeral, zkhelper.DefaultDirACLs())
}
func ProxyList(zkConn zkhelper.Conn, productName string, filter func(*ProxyInfo) bool) ([]ProxyInfo, error) {
ret := make([]ProxyInfo, 0)
root := GetProxyPath(productName)
proxies, _, err := zkConn.Children(root)
if err != nil && !zkhelper.ZkErrorEqual(err, zk.ErrNoNode) {
return nil, errors.Trace(err)
}
for _, proxyName := range proxies {
pi, err := GetProxyInfo(zkConn, productName, proxyName)
if err != nil {
return nil, errors.Trace(err)
}
if filter == nil || filter(pi) == true {
ret = append(ret, *pi)
}
}
return ret, nil
}
var ErrUnknownProxyStatus = errors.New("unknown status, should be (online offline)")
func SetProxyStatus(zkConn zkhelper.Conn, productName string, proxyName string, status string) error {
p, err := GetProxyInfo(zkConn, productName, proxyName)
if err != nil {
return errors.Trace(err)
}
if status != PROXY_STATE_ONLINE && status != PROXY_STATE_MARK_OFFLINE && status != PROXY_STATE_OFFLINE {
return errors.Errorf("%v, %s", ErrUnknownProxyStatus, status)
}
// check slot status before setting proxy online
if status == PROXY_STATE_ONLINE {
slots, err := Slots(zkConn, productName)
if err != nil {
return errors.Trace(err)
}
for _, slot := range slots {
if slot.State.Status != SLOT_STATUS_ONLINE {
return errors.Errorf("slot %v is not online", slot)
}
if slot.GroupId == INVALID_ID {
return errors.Errorf("slot %v has invalid group id", slot)
}
}
}
p.State = status
b, _ := json.Marshal(p)
_, err = zkConn.Set(path.Join(GetProxyPath(productName), proxyName), b, -1)
if err != nil {
return errors.Trace(err)
}
if status == PROXY_STATE_MARK_OFFLINE {
// wait for the proxy down
for {
_, _, c, err := zkConn.GetW(path.Join(GetProxyPath(productName), proxyName))
if zkhelper.ZkErrorEqual(err, zk.ErrNoNode) {
return nil
} else if err != nil {
return errors.Trace(err)
}
<-c
info, err := GetProxyInfo(zkConn, productName, proxyName)
log.Info("mark_offline, check proxy status:", proxyName, info, err)
if zkhelper.ZkErrorEqual(err, zk.ErrNoNode) {
log.Info("shutdown proxy successful")
return nil
} else if err != nil {
return errors.Trace(err)
}
if info.State == PROXY_STATE_OFFLINE {
log.Info("proxy:", proxyName, "offline success!")
return nil
}
}
}
return nil
}
func GetProxyInfo(zkConn zkhelper.Conn, productName string, proxyName string) (*ProxyInfo, error) {
var pi ProxyInfo
data, _, err := zkConn.Get(path.Join(GetProxyPath(productName), proxyName))
if err != nil {
return nil, errors.Trace(err)
}
if err := json.Unmarshal(data, &pi); err != nil {
return nil, errors.Trace(err)
}
return &pi, nil
}