-
Notifications
You must be signed in to change notification settings - Fork 7
/
node3.go
82 lines (72 loc) · 1.59 KB
/
node3.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
package main
import (
"context"
"fmt"
"log"
"os"
"time"
"github.com/hashicorp/memberlist"
"gopkg.in/urfave/cli.v1"
)
func action(c *cli.Context) {
conf := memberlist.DefaultLocalConfig()
conf.Name = "node3"
conf.BindPort = 7949 // avoid port confliction
conf.AdvertisePort = conf.BindPort
conf.Events = new(MyEventDelegate)
list, err := memberlist.Create(conf)
if err != nil {
log.Fatal(err)
}
local := list.LocalNode()
list.Join([]string{
fmt.Sprintf("%s:%d", local.Addr.To4().String(), local.Port),
})
join := c.String("join")
log.Printf("cluster join to %s", join)
if _, err := list.Join([]string{join}); err != nil {
log.Fatal(err)
}
stopCtx, cancel := context.WithCancel(context.TODO())
go wait_signal(cancel)
tick := time.NewTicker(3 * time.Second)
run := true
for run {
select {
case <-tick.C:
devt := conf.Events.(*MyEventDelegate)
if devt == nil {
log.Printf("consistent isnt initialized")
continue
}
log.Printf("current node size: %d", devt.consistent.Size())
keys := []string{"foo", "world"}
for _, key := range keys {
node, ok := devt.consistent.GetNode(key)
if ok == true {
log.Printf("node3 search %s => %s", key, node)
} else {
log.Printf("no node available")
}
}
case <-stopCtx.Done():
log.Printf("stop called")
run = false
}
}
tick.Stop()
log.Printf("bye.")
}
func main() {
app := cli.NewApp()
app.Action = action
app.Flags = []cli.Flag{
cli.StringFlag{
Name: "join, j",
Usage: "cluster join address",
Value: "127.0.0.1:xxxx",
EnvVar: "JOIN_ADDR",
},
}
app.Run(os.Args)
}