-
Notifications
You must be signed in to change notification settings - Fork 0
/
zkdao.go
387 lines (359 loc) · 11.2 KB
/
zkdao.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
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package data
import (
"github.com/samuel/go-zookeeper/zk"
"path"
"time"
"strconv"
"log"
"errors"
"bytes"
"encoding/base64"
)
type ZkDAO struct {
client *zk.Conn
}
// #### CONSTRUCTOR ####
func NewZkDAO(zookeeper []string) (*ZkDAO, error) {
client, _, err := zk.Connect(zookeeper, time.Second)
zkdao := new(ZkDAO)
zkdao.client = client
return zkdao, err
}
// #### PUBLIC METHODS ####
func (zkdao *ZkDAO) LoadDomains(key string, recursive bool) ([]Domain, error) {
nodepath := KeyToPath(key)
var domains[] Domain
domainNodes,_,_ := zkdao.client.Children(nodepath)
for _,domainNode := range domainNodes {
domain, err := zkdao.LoadDomain(PathToKey(nodepath + "/" + domainNode), recursive)
if err != nil { return domains, err }
domains = append(domains, domain)
}
return domains, nil
}
func (zkdao *ZkDAO) LoadDomain(key string, recursive bool) (Domain, error) {
nodepath := KeyToPath(key)
var domain Domain
domain.Key = key
domain.Name = path.Base(nodepath)
exists,_,_ := zkdao.client.Exists(nodepath)
if exists {
config, err := zkdao.LoadStaticConfig(PathToKey(nodepath + "/config"), recursive)
if err != nil { return domain, err }
domain.Config = config
runtime, err := zkdao.LoadRuntimeConfig(PathToKey(nodepath + "/runtime"), recursive)
if err != nil { return domain, err }
domain.Runtime = runtime
} else {
log.Println("Domain node does not exist: " + string(key))
}
return domain, nil
}
func (zkdao *ZkDAO) LoadStaticConfig(key string, recursive bool) (StaticConfig, error) {
nodepath := KeyToPath(key)
var config StaticConfig
exists,_,_ := zkdao.client.Exists(nodepath)
if exists {
agentsNode,_,_ := zkdao.client.Children(nodepath + "/agents")
for _,agentNode := range agentsNode {
agent, err := zkdao.LoadAgent(PathToKey(nodepath + "/agents/" + agentNode), recursive)
if err != nil { return config, err }
config.Agents = append(config.Agents, agent)
}
processesNode,_,_ := zkdao.client.Children(nodepath + "/processes")
for _,processNode := range processesNode {
process, err := zkdao.LoadProcess(PathToKey(nodepath + "/processes/" + processNode), recursive)
if err != nil { return config, err }
config.Processes = append(config.Processes, process)
}
} else {
log.Println("Static config node does not exist: " + nodepath)
}
return config, nil
}
func (zkdao *ZkDAO) LoadRuntimeConfig(key string, recursive bool) (RuntimeConfig, error) {
nodepath := KeyToPath(key)
var runtime RuntimeConfig
exists,_,_ := zkdao.client.Exists(nodepath)
if exists {
agentsNode,_,_ := zkdao.client.Children(nodepath + "/agents")
for _,agentNode := range agentsNode {
agent, err := zkdao.LoadAgent(PathToKey(nodepath + "/agents/" + agentNode), recursive)
if err != nil { return runtime, err }
runtime.Agents = append(runtime.Agents, agent)
}
} else {
log.Println("Runtime config node does not exist: " + nodepath)
}
return runtime, nil
}
func (zkdao *ZkDAO) LoadAgent(key string, recursive bool) (Agent, error) {
nodepath := KeyToPath(key)
var agent Agent
agent.Key = key
agent.Name = path.Base(nodepath)
exists,_,_ := zkdao.client.Exists(nodepath)
if exists {
processesNode,_,_ := zkdao.client.Children(nodepath + "/processes")
for _,processNode := range processesNode {
process, err := zkdao.LoadProcess(PathToKey(nodepath + "/processes/" + processNode), recursive)
if err != nil { return agent, err }
agent.Processes = append(agent.Processes, process)
}
exists,_,_ = zkdao.client.Exists(nodepath + "/eph")
if exists {
data,_,err := zkdao.client.Get(nodepath + "/eph")
if err == nil {
agent.Eph = string(data)
}
}
} else {
log.Println("Agent node does not exist: " + nodepath)
}
return agent, nil
}
func (zkdao *ZkDAO) LoadProcess(key string, recursive bool) (Process, error) {
nodepath := KeyToPath(key)
process := Process {Pid : -1}
process.Key = key
process.Name = path.Base(nodepath)
exists,_,_ := zkdao.client.Exists(nodepath)
if exists {
exists,_,_ = zkdao.client.Exists(nodepath + "/command")
if exists {
data,_,err := zkdao.client.Get(nodepath + "/command")
if err == nil {
process.Command = string(data)
}
log.Println("Reading command " + process.Command)
}
exists,_,_ = zkdao.client.Exists(nodepath + "/arguments")
if exists {
data,_,err := zkdao.client.Get(nodepath + "/arguments")
if err == nil {
process.Arguments = string(data)
}
}
exists,_,_ = zkdao.client.Exists(nodepath + "/process_class")
if exists {
data,_,err := zkdao.client.Get(nodepath + "/process_class")
if err == nil {
process.ProcessClass = string(data)
}
}
exists,_,_ = zkdao.client.Exists(nodepath + "/admin_state")
if exists {
data,_,err := zkdao.client.Get(nodepath + "/admin_state")
if err == nil {
process.AdminState = string(data)
}
}
exists,_,_ = zkdao.client.Exists(nodepath + "/oper_state")
if exists {
data,_,err := zkdao.client.Get(nodepath + "/oper_state")
if err == nil {
process.OperState = string(data)
}
}
exists,_,_ = zkdao.client.Exists(nodepath + "/pid")
if exists {
data,_,err := zkdao.client.Get(nodepath + "/pid")
if err == nil {
tempPid, err := strconv.ParseInt(string(data), 10, 32)
if err != nil {
log.Printf("Failed to parse pid '%s':\n%s", nodepath + "/pid", err)
} else {
process.Pid = int(tempPid)
}
}
}
} else {
log.Println("Process node does not exist: " + nodepath)
}
return process, nil
}
func (zkdao *ZkDAO) UpdateDomain(key string, domain Domain, recursive bool) error {
nodepath := KeyToPath(key)
exists,_,_ := zkdao.client.Exists(nodepath)
if !exists {
_, err := zkdao.createWithParents(nodepath, []byte{}, 0, zk.WorldACL(zk.PermAll))
if err != nil { return err }
}
if recursive {
err := zkdao.UpdateStaticConfig(PathToKey(nodepath + "/config"), domain.Config, recursive)
if err != nil { return err }
err = zkdao.UpdateRuntimeConfig(PathToKey(nodepath + "/runtime"), domain.Runtime, recursive)
if err != nil { return err }
}
return nil
}
func (zkdao *ZkDAO) UpdateStaticConfig(key string, config StaticConfig, recursive bool) error {
nodepath := KeyToPath(key)
exists,_,_ := zkdao.client.Exists(nodepath)
if !exists {
_, err := zkdao.createWithParents(nodepath, []byte{}, 0, zk.WorldACL(zk.PermAll))
if err != nil { return err }
}
if recursive {
for _, value := range config.Agents {
err := zkdao.UpdateAgent(PathToKey(nodepath + "/agents/" + value.Name), value, recursive)
if err != nil { return err }
}
for _, value := range config.Processes {
err := zkdao.UpdateProcess(PathToKey(nodepath + "/processes/" + value.Name), value, recursive)
if err != nil { return err }
}
}
return nil
}
func (zkdao *ZkDAO) UpdateRuntimeConfig(key string, runtime RuntimeConfig, recursive bool) error {
return nil
}
func (zkdao *ZkDAO) UpdateAgent(key string, agent Agent, recursive bool) error {
nodepath := KeyToPath(key)
log.Println("Updating agent: " + nodepath)
exists,_,_ := zkdao.client.Exists(nodepath)
if !exists {
_, err := zkdao.createWithParents(nodepath, []byte{}, 0, zk.WorldACL(zk.PermAll))
if err != nil { return err }
}
if recursive {
for _, value := range agent.Processes {
err := zkdao.UpdateProcess(PathToKey(nodepath + "/processes/" + value.Name), value, recursive)
if err != nil { return err }
}
}
return nil
}
func (zkdao *ZkDAO) UpdateProcess(key string, process Process, recursive bool) error {
nodepath := KeyToPath(key)
log.Println("Updating process: " + nodepath)
exists,_,_ := zkdao.client.Exists(nodepath)
if !exists {
_, err := zkdao.createWithParents(nodepath, []byte{}, 0, zk.WorldACL(zk.PermAll))
if err != nil { return err }
}
if process.Command != "" {
_, err := zkdao.createOrSet(nodepath + "/command", []byte(process.Command), 0, zk.WorldACL(zk.PermAll))
if err != nil { return err }
}
if process.Arguments != "" {
_, err := zkdao.createOrSet(nodepath + "/arguments", []byte(process.Arguments), 0, zk.WorldACL(zk.PermAll))
if err != nil { return err }
}
if process.ProcessClass != "" {
_, err := zkdao.createOrSet(nodepath + "/process_class", []byte(process.ProcessClass), 0, zk.WorldACL(zk.PermAll))
if err != nil { return err }
}
if process.AdminState != "" {
_, err := zkdao.createOrSet(nodepath + "/admin_state", []byte(process.AdminState), 0, zk.WorldACL(zk.PermAll))
if err != nil { return err }
}
if process.OperState != "" {
_, err := zkdao.createOrSet(nodepath + "/oper_state", []byte(process.OperState), 0, zk.WorldACL(zk.PermAll))
if err != nil { return err }
}
if process.Pid != -1 {
_, err := zkdao.createOrSet(nodepath + "/pid", []byte(strconv.FormatInt(int64(process.Pid), 10)), 0, zk.WorldACL(zk.PermAll))
if err != nil { return err }
}
return nil
}
func (zkdao *ZkDAO) Watch(path string, watchChannel chan<- zk.Event) (error) {
log.Println("Adding watch: " + path)
exists,_,eventChan,err := zkdao.client.ExistsW(path)
if err != nil {
return err
}
if !exists {
return errors.New("The path '" + path + "' does not exist" )
}
go func(to chan<- zk.Event, from <-chan zk.Event) {
e := <-from
to <-e
zkdao.Watch(path, to)
}(watchChannel, eventChan)
return nil
}
func (zkdao *ZkDAO) GetValue(path string) ([]byte, error) {
data,_,err := zkdao.client.Get(path)
return data,err;
}
func (zkdao *ZkDAO) SetValue(path string, data []byte) (error) {
exists,stat,err := zkdao.client.Exists(path)
if err != nil {
return err
}
if exists {
_, err = zkdao.client.Set(path, data, stat.Version)
if err != nil {
return err
}
}
return nil
}
func (zkdao *ZkDAO) RemoveRecursive(path string) (error) {
exists,stat,err := zkdao.client.Exists(path)
if exists && err != nil {
return err
}
if exists {
children,_,_ := zkdao.client.Children(path)
for _,child := range children {
err := zkdao.RemoveRecursive(path + "/" + child)
if err != nil {
return err
}
}
return zkdao.client.Delete(path, stat.Version)
}
return nil
}
// Creates an ephemeral node for the given path
func (zkdao ZkDAO) CreateEphemeral(path string, data []byte) (string, error) {
return zkdao.client.Create(path,data,zk.FlagEphemeral, zk.WorldACL(zk.PermAll))
}
// Converts a key to a ZK path
func KeyToPath(key string) (string) {
path, err := base64.StdEncoding.DecodeString(key)
if err != nil {
return ""
} else {
return string(path)
}
}
// Converts a ZK path to a key
func PathToKey(path string) (string) {
return base64.StdEncoding.EncodeToString([]byte(path))
}
// #### PRIVATE METHODS ####
func (zkdao *ZkDAO) createOrSet(nodepath string, data []byte, flags int32, acl []zk.ACL) (string, error) {
exists,stat,_ := zkdao.client.Exists(nodepath)
if exists {
oldData,_,_ := zkdao.client.Get(nodepath)
if bytes.Compare(oldData, data) != 0 {
_, err2 := zkdao.client.Set(nodepath, data, stat.Version)
return "", err2
}
} else {
return zkdao.client.Create(nodepath, data, flags, acl)
}
return "", nil
}
func (zkdao *ZkDAO) createWithParents(nodepath string, data []byte, flags int32, acl []zk.ACL) (string, error) {
parent := path.Dir(nodepath)
if parent == "." {
return "", nil
}
exists, _, err := zkdao.client.Exists(parent)
if err != nil {
return "", err
}
if !exists {
s, err := zkdao.createWithParents(parent, data, flags, acl)
if err != nil {
return s, err
}
}
return zkdao.client.Create(nodepath, data, flags, acl)
}