-
Notifications
You must be signed in to change notification settings - Fork 0
/
netAgent.go
566 lines (444 loc) · 11.8 KB
/
netAgent.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
// Embedded net Agent
package netAgent
import (
"bytes"
b64 "encoding/base64"
"encoding/json"
"errors"
"flag"
"fmt"
"github.com/WIZARD-CXY/netAgent/Godeps/_workspace/src/github.com/golang/glog"
"github.com/WIZARD-CXY/netAgent/Godeps/_workspace/src/github.com/hashicorp/consul/api"
"github.com/WIZARD-CXY/netAgent/Godeps/_workspace/src/github.com/hashicorp/consul/command"
"github.com/WIZARD-CXY/netAgent/Godeps/_workspace/src/github.com/hashicorp/consul/watch"
"github.com/WIZARD-CXY/netAgent/Godeps/_workspace/src/github.com/mitchellh/cli"
"io/ioutil"
"net"
"net/http"
"os"
"reflect"
"strconv"
"time"
)
func StartAgent(serverMode bool, bootstrap bool, bindInterface string, dataDir string) error {
bindAddr := ""
if bindInterface != "" {
netInterface, err := net.InterfaceByName(bindInterface)
if err != nil {
glog.Fatalf("Error : %v", err)
return err
}
addrs, err := netInterface.Addrs()
if err == nil {
for _, addr := range addrs {
ip, _, _ := net.ParseCIDR(addr.String())
if ip != nil && ip.To4() != nil {
bindAddr = ip.To4().String()
}
}
}
}
errChan := make(chan int)
watchForExistingRegisteredUpdates()
//go RegisterForNodeUpdates()
go startConsul(serverMode, bootstrap, bindAddr, dataDir, errChan)
select {
case <-errChan:
return errors.New("Error start consul agent")
case <-time.After(time.Second * 5):
}
return nil
}
func startConsul(serverMode bool, bootstrap bool, bindAddress string, dataDir string, eCh chan int) {
args := []string{"agent", "-data-dir", dataDir}
if serverMode {
args = append(args, "-server")
}
if bootstrap {
args = append(args, "-bootstrap-expect=1")
}
if bindAddress != "" {
args = append(args, "-bind")
args = append(args, bindAddress)
args = append(args, "-advertise")
args = append(args, bindAddress)
}
args = append(args)
ret := Execute(args...)
eCh <- ret
}
// Execute function is borrowed from Consul's main.go
func Execute(args ...string) int {
cli := &cli.CLI{
Args: args,
Commands: Commands,
HelpFunc: cli.BasicHelpFunc("consul"),
}
exitCode, err := cli.Run()
if err != nil {
fmt.Fprintf(os.Stderr, "Error executing CLI: %s\n", err.Error())
return 1
}
return exitCode
}
// Node operation related
type Node struct {
Name string `json:"Name,omitempty"`
Address string `json:"Addr,ommitempty"`
}
const CONSUL_CATALOG_BASE_URL = "http://localhost:8500/v1/catalog/"
func Join(addr string) error {
ret := Execute("join", addr)
if ret != 0 {
glog.Errorf("Error (%d) joining %s with consul peers", ret, addr)
return errors.New("Error joining the cluster")
}
return nil
}
func Leave() error {
ret := Execute("leave")
if ret != 0 {
glog.Errorf("Error leaving consul cluster")
return errors.New("Error leaving consul cluster")
}
return nil
}
func getNodes() ([]Node, error) {
url := CONSUL_CATALOG_BASE_URL + "nodes"
resp, err := http.Get(url)
if err != nil {
glog.Errorf("Error (%v) get %s", err, url)
return nil, errors.New("Get nodes failed")
}
defer resp.Body.Close()
glog.Infof("Get %s for %s\n", resp.Status, url)
var nodes []Node
err = json.NewDecoder(resp.Body).Decode(nodes)
if err != nil {
glog.Errorf("getNodes failed error decoding")
return nil, errors.New("Decode error in getNodes")
}
return nodes, nil
}
// K/V store
const CONSUL_KV_BASE_URL = "http://localhost:8500/v1/kv/"
type KVRespBody struct {
CreateIndex int `json:"CreateIndex,omitempty"`
ModifyIndex int `json:"ModifyIndex,omitempty"`
Key string `json:"Key,omitempty"`
Flags int `json:Flags,omitempty"`
Value string `json:Value,omitempty"`
}
// 1st return value as []byte
// 2nd return its ModifyIndex
// 3rd return ok or not
func Get(store string, key string) ([]byte, int, bool) {
url := CONSUL_KV_BASE_URL + store + "/" + key
resp, err := http.Get(url)
if err != nil {
glog.Errorf("Error (%v) in Get for %s\n", err, url)
return nil, 0, false
}
defer resp.Body.Close()
glog.Infof("Status of Get %s for %s", resp.Status, url)
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
var jsonBody []KVRespBody
err = json.NewDecoder(resp.Body).Decode(&jsonBody)
existingValue, err := b64.StdEncoding.DecodeString(jsonBody[0].Value)
if err != nil {
return nil, jsonBody[0].ModifyIndex, false
}
return existingValue, jsonBody[0].ModifyIndex, true
} else {
return nil, 0, false
}
}
// get all key-value pairs in one store from backend
func GetAll(store string) ([][]byte, []int, bool) {
url := CONSUL_KV_BASE_URL + store + "?recursive"
resp, err := http.Get(url)
defer resp.Body.Close()
if err != nil {
glog.Infof("Error in Get all KV %v", store)
}
fmt.Printf("Status of Get %s for %s\n", resp.Status, url)
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
var jsonBody []KVRespBody
values := make([][]byte, 0)
indexes := make([]int, 0)
err = json.NewDecoder(resp.Body).Decode(&jsonBody)
for _, body := range jsonBody {
existingVal, _ := b64.StdEncoding.DecodeString(body.Value)
values = append(values, existingVal)
indexes = append(indexes, body.ModifyIndex)
}
return values, indexes, true
} else {
return nil, nil, false
}
}
const (
OK = iota
OUTDATED
ERROR
)
// return val indicate the error type
// need old val as 4-th param
func Put(store string, key string, value []byte, oldVal []byte) int {
existingVal, casIndex, ok := Get(store, key)
if ok && !bytes.Equal(oldVal, existingVal) {
return OUTDATED
}
url := CONSUL_KV_BASE_URL + store + "/" + key + "?cas=" + strconv.Itoa(casIndex)
glog.Infof("Updating KV pair for %s %s %s %d", url, key, value, casIndex)
req, err := http.NewRequest("PUT", url, bytes.NewBuffer(value))
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
glog.Errorf("Error creating KV pair for %s", key)
return ERROR
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
if string(body) == "false" {
return ERROR
}
return OK
}
// return val indicate the error type
func Delete(store string, key string) int {
url := fmt.Sprintf("%s%s/%s", CONSUL_KV_BASE_URL, store, key)
glog.Infof("Deleting KV pair for %s", url)
req, err := http.NewRequest("DELETE", url, nil)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
glog.Errorf("Error deleting KV pair %d", key)
return ERROR
}
defer resp.Body.Close()
return OK
}
// Watch related
const (
NOTIFY_UPDATE_ADD = iota
NOTIFY_UPDATE_MODIFY
NOTIFY_UPDATE_DELETE
)
type NotifyUpdateType int
const (
WATCH_TYPE_NODE = iota
WATCH_TYPE_KEY
WATCH_TYPE_STORE
WATCH_TYPE_EVENT
)
type WatchType int
type watchData struct {
listeners map[string][]Listener
watchPlans []*watch.WatchPlan
}
var watches map[WatchType]watchData = make(map[WatchType]watchData)
type Listener interface {
NotifyNodeUpdate(NotifyUpdateType, string)
NotifyKeyUpdate(NotifyUpdateType, string, []byte)
NotifyStoreUpdate(NotifyUpdateType, string, map[string][]byte)
}
func contains(wType WatchType, key string, elem interface{}) bool {
ws, ok := watches[wType]
if !ok {
return false
}
list, ok := ws.listeners[key]
if !ok {
return false
}
v := reflect.ValueOf(list)
for i := 0; i < v.Len(); i++ {
if v.Index(i).Interface() == elem {
return true
}
}
return false
}
type watchconsul bool
func addListener(wtype WatchType, key string, listener Listener) watchconsul {
var wc watchconsul = false
if !contains(WATCH_TYPE_NODE, key, listener) {
ws, ok := watches[wtype]
if !ok {
watches[wtype] = watchData{make(map[string][]Listener), make([]*watch.WatchPlan, 0)}
ws = watches[wtype]
}
listeners, ok := ws.listeners[key]
if !ok {
listeners = make([]Listener, 0)
wc = true
}
ws.listeners[key] = append(listeners, listener)
}
return wc
}
func getListeners(wtype WatchType, key string) []Listener {
ws, ok := watches[wtype]
if !ok {
return nil
}
list, ok := ws.listeners[key]
if ok {
return list
}
return nil
}
func addWatchPlan(wtype WatchType, wp *watch.WatchPlan) {
ws, ok := watches[wtype]
if !ok {
return
}
ws.watchPlans = append(ws.watchPlans, wp)
watches[wtype] = ws
}
func stopWatches() {
for _, ws := range watches {
for _, wp := range ws.watchPlans {
wp.Stop()
}
ws.watchPlans = ws.watchPlans[:0]
}
}
func register(wtype WatchType, params map[string]interface{}, handler watch.HandlerFunc) {
// Create the watch
wp, err := watch.Parse(params)
if err != nil {
fmt.Printf("Register error : %s", err)
return
}
addWatchPlan(wtype, wp)
wp.Handler = handler
cmdFlags := flag.NewFlagSet("watch", flag.ContinueOnError)
httpAddr := command.HTTPAddrFlag(cmdFlags)
// Run the watch
if err := wp.Run(*httpAddr); err != nil {
fmt.Printf("Error querying Consul agent: %s", err)
}
}
var nodeCache []*api.Node
func compare(X, Y []*api.Node) []*api.Node {
m := make(map[string]bool)
for _, y := range Y {
m[y.Address] = true
}
var ret []*api.Node
for _, x := range X {
if m[x.Address] {
continue
}
ret = append(ret, x)
}
return ret
}
func updateNodeListeners(clusterNodes []*api.Node) {
toDelete := compare(nodeCache, clusterNodes)
toAdd := compare(clusterNodes, nodeCache)
nodeCache = clusterNodes
listeners := getListeners(WATCH_TYPE_NODE, "")
if listeners == nil {
return
}
for _, deleteNode := range toDelete {
for _, listener := range listeners {
listener.NotifyNodeUpdate(NOTIFY_UPDATE_DELETE, deleteNode.Address)
}
}
for _, addNode := range toAdd {
for _, listener := range listeners {
listener.NotifyNodeUpdate(NOTIFY_UPDATE_ADD, addNode.Address)
}
}
}
func updateKeyListeners(idx uint64, key string, data interface{}) {
listeners := getListeners(WATCH_TYPE_KEY, key)
if listeners == nil {
return
}
var kv *api.KVPair = nil
var val []byte = nil
updateType := NOTIFY_UPDATE_MODIFY
if data != nil {
kv = data.(*api.KVPair)
}
if kv == nil {
updateType = NOTIFY_UPDATE_DELETE
} else {
updateType = NOTIFY_UPDATE_MODIFY
if idx == kv.CreateIndex {
updateType = NOTIFY_UPDATE_ADD
}
val = kv.Value
}
for _, listener := range listeners {
listener.NotifyKeyUpdate(NotifyUpdateType(updateType), key, val)
}
}
func registerForNodeUpdates() {
// Compile the watch parameters
params := make(map[string]interface{})
params["type"] = "nodes"
handler := func(idx uint64, data interface{}) {
updateNodeListeners(data.([]*api.Node))
}
register(WATCH_TYPE_NODE, params, handler)
}
func RegisterForNodeUpdates(listener Listener) {
wc := addListener(WATCH_TYPE_NODE, "", listener)
if wc {
registerForNodeUpdates()
}
}
func registerForKeyUpdates(absKey string) {
params := make(map[string]interface{})
params["type"] = "key"
params["key"] = absKey
handler := func(idx uint64, data interface{}) {
updateKeyListeners(idx, absKey, data)
}
register(WATCH_TYPE_KEY, params, handler)
}
func RegisterForKeyUpdates(store string, key string, listener Listener) {
absKey := store + "/" + key
wc := addListener(WATCH_TYPE_KEY, absKey, listener)
if wc {
registerForKeyUpdates(absKey)
}
}
func registerForStoreUpdates(store string) {
// Compile the watch parameters
params := make(map[string]interface{})
params["type"] = "keyprefix"
params["prefix"] = store + "/"
handler := func(idx uint64, data interface{}) {
fmt.Println("NOT IMPLEMENTED Store Update :", idx, data)
}
register(WATCH_TYPE_STORE, params, handler)
}
func RegisterForStoreUpdates(store string, listener Listener) {
wc := addListener(WATCH_TYPE_STORE, store, listener)
if wc {
registerForStoreUpdates(store)
}
}
func watchForExistingRegisteredUpdates() {
for wType, ws := range watches {
glog.Infof("watchForExistingRegisteredUpdates : ", wType)
for key, _ := range ws.listeners {
glog.Infof("key : ", key)
switch wType {
case WATCH_TYPE_NODE:
go registerForNodeUpdates()
case WATCH_TYPE_KEY:
go registerForKeyUpdates(key)
case WATCH_TYPE_STORE:
go registerForStoreUpdates(key)
}
}
}
}