-
Notifications
You must be signed in to change notification settings - Fork 7
/
ecc.go
627 lines (541 loc) · 14.3 KB
/
ecc.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
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
package ecc
import (
"bytes"
b64 "encoding/base64"
"encoding/json"
"errors"
"flag"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"reflect"
"time"
"github.com/socketplane/ecc/Godeps/_workspace/src/github.com/hashicorp/consul/api"
"github.com/socketplane/ecc/Godeps/_workspace/src/github.com/hashicorp/consul/command"
"github.com/socketplane/ecc/Godeps/_workspace/src/github.com/hashicorp/consul/watch"
"github.com/socketplane/ecc/Godeps/_workspace/src/github.com/mitchellh/cli"
)
// Embedded Consul Client
// Quick and Dirty way to embed Consul with any golang based application without the
// additional step of installing the Consul application in the host system
// Consul Agent related functions
var started bool
var OfflineSupport bool = true
var listener eccListener
func Start(serverMode bool, bootstrap bool, bindInterface string, dataDir string) error {
bindAddress := ""
if bindInterface != "" {
intf, err := net.InterfaceByName(bindInterface)
if err != nil {
log.Printf("Error : %v", err)
return err
}
addrs, err := intf.Addrs()
if err == nil {
for i := 0; i < len(addrs); i++ {
addr := addrs[i].String()
ip, _, _ := net.ParseCIDR(addr)
if ip != nil && ip.To4() != nil {
bindAddress = ip.To4().String()
}
}
}
}
errCh := make(chan int)
watchForExistingRegisteredUpdates()
go RegisterForNodeUpdates(listener)
go startConsul(serverMode, bootstrap, bindAddress, dataDir, errCh)
select {
case <-errCh:
return errors.New("Error starting 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")
}
if bindAddress != "" {
args = append(args, "-bind")
args = append(args, bindAddress)
args = append(args, "-advertise")
args = append(args, bindAddress)
}
ret := Execute(args...)
eCh <- ret
}
func HasStarted() bool {
return started
}
func Join(address string) error {
ret := Execute("join", address)
if ret != 0 {
log.Println("Error (%d) joining %s with Consul peers", ret, address)
return errors.New("Error adding member")
}
return nil
}
func Leave() error {
stopWatches()
ret := Execute("leave")
if ret != 0 {
log.Println("Error Leaving Consul membership")
return errors.New("Error leaving Consul cluster")
}
return nil
}
// Execute function is borrowed from Consul's main.go
func Execute(args ...string) int {
for _, arg := range args {
if arg == "-v" || arg == "--version" {
newArgs := make([]string, len(args)+1)
newArgs[0] = "version"
copy(newArgs[1:], args)
args = newArgs
break
}
}
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
}
const CONSUL_BASE_URL = "http://localhost:8500/v1/"
func ConsulGet(url string) (string, bool) {
resp, err := http.Get(url)
if err != nil {
log.Printf("Error (%v) in Get for %s\n", err, url)
return "", false
}
defer resp.Body.Close()
log.Printf("Status of Get %s %d for %s", resp.Status, resp.StatusCode, url)
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
var jsonBody []consulBody
body, err := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(body, &jsonBody)
existingValue, err := b64.StdEncoding.DecodeString(jsonBody[0].Value)
if err != nil {
return "", false
}
return string(existingValue[:]), true
} else {
return "", false
}
}
// Consul KV Store related
const CONSUL_KV_BASE_URL = "http://localhost:8500/v1/kv/"
type consulBody 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"`
}
func GetAll(store string) ([][]byte, []int, bool) {
if OfflineSupport && !started {
return getAllFromCache(store)
}
url := CONSUL_KV_BASE_URL + store + "?recurse"
resp, err := http.Get(url)
if err != nil {
log.Printf("Error (%v) in Get for %s\n", err, url)
return nil, nil, false
}
defer resp.Body.Close()
log.Printf("Status of Get %s %d for %s", resp.Status, resp.StatusCode, url)
if resp.StatusCode >= 400 && resp.StatusCode < 500 {
return nil, nil, false
} else if resp.StatusCode >= 200 && resp.StatusCode < 300 {
var jsonBody []consulBody
valueArr := make([][]byte, 0)
indexArr := make([]int, 0)
body, _ := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(body, &jsonBody)
for _, body := range jsonBody {
existingValue, _ := b64.StdEncoding.DecodeString(body.Value)
valueArr = append(valueArr, existingValue)
indexArr = append(indexArr, body.ModifyIndex)
}
return valueArr, indexArr, true
} else {
return nil, nil, false
}
}
func Get(store string, key string) ([]byte, int, bool) {
if OfflineSupport && !started {
return getFromCache(store, key)
}
url := CONSUL_KV_BASE_URL + store + "/" + key
resp, err := http.Get(url)
if err != nil {
log.Printf("Error (%v) in Get for %s\n", err, url)
return nil, 0, false
}
defer resp.Body.Close()
log.Printf("Status of Get %s %d for %s", resp.Status, resp.StatusCode, url)
if resp.StatusCode >= 400 && resp.StatusCode < 500 {
return nil, 0, false
} else if resp.StatusCode >= 200 && resp.StatusCode < 300 {
var jsonBody []consulBody
body, err := ioutil.ReadAll(resp.Body)
err = json.Unmarshal(body, &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
}
}
const (
OK = iota
OUTDATED
ERROR
)
type eccerror int
func Put(store string, key string, value []byte, oldValue []byte) eccerror {
if OfflineSupport && !started {
return putInCache(store, key, value, oldValue)
}
existingValue, casIndex, ok := Get(store, key)
if ok && !bytes.Equal(oldValue, existingValue) {
return OUTDATED
}
url := fmt.Sprintf("%s%s/%s?cas=%d", CONSUL_KV_BASE_URL, store, key, casIndex)
log.Printf("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 {
log.Println("Error creating KV pair for ", url, err)
return ERROR
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
if string(body) == "false" {
// Let the application retry based on return value
// return Put(store, key, value, oldValue)
return OUTDATED
}
return OK
}
func Delete(store string, key string) eccerror {
if OfflineSupport && !started {
return deleteFromCache(store, key)
}
url := fmt.Sprintf("%s%s/%s", CONSUL_KV_BASE_URL, store, key)
log.Printf("Deleting KV pair for %s", url)
req, err := http.NewRequest("DELETE", url, nil)
client := &http.Client{}
resp, err := client.Do(req)
if err != nil {
log.Println("Error creating KV pair for ", url, err)
return ERROR
}
defer resp.Body.Close()
body, _ := ioutil.ReadAll(resp.Body)
log.Println(string(body))
return OK
}
type Store struct {
cache map[string][]byte
}
// Local KV store cache for bootstrap node consul connection issues
var cache map[string]Store = make(map[string]Store)
func getAllFromCache(storeName string) ([][]byte, []int, bool) {
store, ok := cache[storeName]
if !ok {
return nil, nil, false
}
vals := make([][]byte, 0)
for _, val := range store.cache {
vals = append(vals, val)
}
return vals, nil, true
}
func getFromCache(storeName string, key string) ([]byte, int, bool) {
store, ok := cache[storeName]
if !ok {
return nil, 0, false
}
val, ok := store.cache[key]
return val, 0, ok
}
func putInCache(storeName string, key string, value []byte, oldValue []byte) eccerror {
store, ok := cache[storeName]
if !ok {
store = Store{make(map[string][]byte)}
cache[storeName] = store
}
store.cache[key] = value
return OK
}
func deleteFromCache(storeName string, key string) eccerror {
store, ok := cache[storeName]
if ok {
delete(store.cache, key)
}
return OK
}
func populateKVStoreFromCache() {
if !OfflineSupport || started {
return
}
started = true
for storeName, store := range cache {
for key, val := range store.cache {
go Put(storeName, key, val, nil)
}
delete(cache, storeName)
}
}
// 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)
}
type eccListener struct {
}
func (e eccListener) NotifyNodeUpdate(nType NotifyUpdateType, nodeAddress string) {
if nType == NOTIFY_UPDATE_ADD && !started {
populateKVStoreFromCache()
}
}
func (e eccListener) NotifyKeyUpdate(nType NotifyUpdateType, key string, data []byte) {
}
func (e eccListener) NotifyStoreUpdate(nType NotifyUpdateType, store string, data 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 {
log.Println("watchForExistingRegisteredUpdates : ", wType)
for key, _ := range ws.listeners {
log.Println("key : ", key)
switch wType {
case WATCH_TYPE_NODE:
go registerForNodeUpdates()
case WATCH_TYPE_KEY:
go registerForKeyUpdates(key)
case WATCH_TYPE_STORE:
go registerForStoreUpdates(key)
}
}
}
}