-
Notifications
You must be signed in to change notification settings - Fork 35
/
meta.go
149 lines (128 loc) · 2.83 KB
/
meta.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
package beehive
import (
"encoding/gob"
"os"
"path"
"time"
"github.com/kandoo/beehive/Godeps/_workspace/src/github.com/golang/glog"
)
type HiveInfo struct {
ID uint64 `json:"id"`
Addr string `json:"addr"`
}
type hiveMeta struct {
Hive HiveInfo
Peers map[uint64]HiveInfo
}
func peersInfo(addrs []string) map[uint64]HiveInfo {
if len(addrs) == 0 {
return nil
}
ch := make(chan []HiveInfo, len(addrs))
for _, a := range addrs {
go func(a string) {
s, err := getHiveState(a)
if err != nil {
glog.Errorf("cannot communicate with %v: %v", a, err)
return
}
ch <- s.Peers
}(a)
}
// Return the first one.
hives := <-ch
glog.V(2).Infof("found live hives: %v", hives)
infos := make(map[uint64]HiveInfo)
for _, h := range hives {
infos[h.ID] = h
}
return infos
}
func hiveIDFromPeers(addr string, paddrs []string) uint64 {
if len(paddrs) == 0 {
return 1
}
ch := make(chan uint64, len(paddrs))
for _, paddr := range paddrs {
glog.Infof("requesting hive ID from %v", paddr)
go func(paddr string) {
c, err := newRPCClient(paddr)
if err != nil {
glog.Error(err)
return
}
defer c.stop()
id, err := c.sendCmd(cmd{Data: cmdNewHiveID{}})
if err != nil {
glog.Error(err)
return
}
if id == Nil {
glog.Fatalf("invalid ID from peer")
}
_, err = c.sendCmd(cmd{
Data: cmdAddHive{
Hive: HiveInfo{
ID: id.(uint64),
Addr: addr,
},
},
})
if err != nil {
glog.Error(err)
return
}
ch <- id.(uint64)
}(paddr)
select {
case id := <-ch:
return id
case <-time.After(1 * time.Second):
glog.Infof("cannot get id from %v", paddr)
continue
}
}
glog.Fatalf("cannot get a new hive ID from peers")
return 1
}
func meta(cfg HiveConfig) hiveMeta {
m := hiveMeta{}
var dec *gob.Decoder
metapath := path.Join(cfg.StatePath, "meta")
f, err := os.Open(metapath)
if err != nil {
// TODO(soheil): We should also update our peer addresses when we have an
// existing meta.
m.Peers = peersInfo(cfg.PeerAddrs)
m.Hive.Addr = cfg.Addr
if len(cfg.PeerAddrs) == 0 {
// The initial ID is 1. There is no raft node up yet to allocate an ID. So
// we must do this when the hive starts.
m.Hive.ID = 1
goto save
}
m.Hive.ID = hiveIDFromPeers(cfg.Addr, cfg.PeerAddrs)
goto save
}
dec = gob.NewDecoder(f)
if err = dec.Decode(&m); err != nil {
glog.Fatalf("Cannot decode meta: %v", err)
}
m.Hive.Addr = cfg.Addr
f.Close()
save:
saveMeta(m, cfg)
return m
}
func saveMeta(m hiveMeta, cfg HiveConfig) {
metafile := path.Join(cfg.StatePath, "meta")
f, err := os.OpenFile(metafile, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0700)
if err != nil {
glog.Fatalf("cannot open meta file: %v", err)
}
enc := gob.NewEncoder(f)
if err := enc.Encode(&m); err != nil {
glog.Fatalf("cannot encode meta: %v", err)
}
f.Close()
}