-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathexplorer.go
272 lines (226 loc) · 5.66 KB
/
explorer.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
package zoidberg
import (
"encoding/json"
"fmt"
"log"
"net/http"
"path"
"reflect"
"strings"
"sync"
"time"
"github.com/bobrik/zoidberg/application"
"github.com/bobrik/zoidberg/balancer"
"github.com/bobrik/zoidberg/state"
"github.com/samuel/go-zookeeper/zk"
)
// Explorer constantly updates cluster state and notifies Balancers
type Explorer struct {
name string
af application.Finder
bf balancer.Finder
zookeeper *zk.Conn
zp string
state state.State
updated map[string]update
interval time.Duration
laziness time.Duration
mutex sync.Mutex
}
// NewExplorer creates a new Explorer instance with a name,
// application and balancer finders and zookeeper connection
// to persist versioning information
func NewExplorer(name string, af application.Finder, bf balancer.Finder, zc *zk.Conn, zp string, interval, laziness time.Duration) (*Explorer, error) {
s := state.State{}
ss, _, err := zc.Get(zp)
if err != nil {
if err != zk.ErrNoNode {
return nil, err
}
s.Versions = map[string]state.Versions{}
} else {
err := json.Unmarshal(ss, &s)
if err != nil {
return nil, err
}
}
return &Explorer{
name: name,
af: af,
bf: bf,
zookeeper: zc,
zp: zp,
state: s,
updated: map[string]update{},
interval: interval,
laziness: laziness,
mutex: sync.Mutex{},
}, nil
}
// Run launches explorer's main loop that fetches state
// and updates load balancers' state
func (e *Explorer) Run() error {
for {
time.Sleep(e.interval)
d, err := e.discover()
if err != nil {
return err
}
e.updateBalancers(d)
}
}
// discover returns the current view of the world
func (e *Explorer) discover() (*Discovery, error) {
a, err := e.af.Apps()
if err != nil {
return nil, err
}
b, err := e.bf.Balancers()
if err != nil {
return nil, err
}
return &Discovery{
Balancers: b,
Apps: a,
}, nil
}
// updateBalancers updates state of all load balancers
// in parallel with the specified discovery information
func (e *Explorer) updateBalancers(discovery *Discovery) {
state := e.getState()
now := time.Now()
updates := []balancer.Balancer{}
for _, b := range discovery.Balancers {
bs := b.String()
if reflect.DeepEqual(e.updated[bs].apps, discovery.Apps) {
if now.Sub(e.updated[bs].time) < e.laziness {
continue
}
}
updates = append(updates, b)
}
wg := sync.WaitGroup{}
wg.Add(len(updates))
for _, b := range updates {
go func(b balancer.Balancer) {
defer wg.Done()
err := b.Update(e.name, discovery.Apps, state)
if err != nil {
log.Printf("error updating state on %s: %s", b, err)
return
}
e.mutex.Lock()
e.updated[b.String()] = update{
time: now,
apps: discovery.Apps,
}
e.mutex.Unlock()
}(b)
}
wg.Wait()
}
// getState returns the current state of the world
func (e *Explorer) getState() state.State {
e.mutex.Lock()
s := e.state
e.mutex.Unlock()
return s
}
// setVersions sets version information for the specified application
func (e *Explorer) setVersions(app string, versions state.Versions) {
e.mutex.Lock()
e.state.Versions[app] = versions
e.mutex.Unlock()
}
// persistState persists version state in zookeeper
func (e *Explorer) persistState() error {
s := e.getState()
b, err := json.Marshal(s)
if err != nil {
return err
}
_, err = e.zookeeper.Set(e.zp, b, -1)
if err == zk.ErrNoNode {
err = e.setUpZkPath(path.Dir(e.zp))
if err != nil {
return err
}
_, err = e.zookeeper.Create(e.zp, b, 0, zk.WorldACL(zk.PermAll))
}
return err
}
// setUpZkPath initializes zookeeper if needed
func (e *Explorer) setUpZkPath(p string) error {
if p == "/" {
return nil
}
err := e.setUpZkPath(path.Dir(p))
if err != nil {
return nil
}
_, err = e.zookeeper.Create(p, []byte{}, 0, zk.WorldACL(zk.PermAll))
if err == zk.ErrNodeExists {
return nil
}
return err
}
// ServeMux returns explorer's api server
func (e *Explorer) ServeMux() *http.ServeMux {
mux := http.NewServeMux()
mux.HandleFunc("/_health", func(w http.ResponseWriter, req *http.Request) {
w.WriteHeader(http.StatusNoContent)
})
mux.HandleFunc("/state", func(w http.ResponseWriter, req *http.Request) {
if req.Method != "GET" {
http.Error(w, "expected GET", http.StatusBadRequest)
return
}
w.Header().Add("Content-type", "application/json")
err := json.NewEncoder(w).Encode(e.getState())
if err != nil {
log.Println("error sending state:", err)
}
})
mux.HandleFunc("/versions/", func(w http.ResponseWriter, req *http.Request) {
if req.Method != "POST" && req.Method != "PUT" {
http.Error(w, "expected POST or PUT", http.StatusBadRequest)
return
}
d := json.NewDecoder(req.Body)
v := state.Versions{}
err := d.Decode(&v)
if err != nil {
http.Error(w, fmt.Sprintf("version decoding failed: %s", err), http.StatusBadRequest)
return
}
a := strings.TrimPrefix(req.URL.Path, "/versions/")
if a == "" {
http.Error(w, "application is not specified", http.StatusBadRequest)
return
}
e.setVersions(a, v)
err = e.persistState()
if err != nil {
http.Error(w, fmt.Sprintf("state set successfully, but persisting failed: %s", err), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
})
mux.HandleFunc("/discovery", func(w http.ResponseWriter, req *http.Request) {
d, err := e.discover()
if err != nil {
http.Error(w, fmt.Sprintf("error getting servers: %s", err), http.StatusInternalServerError)
return
}
w.Header().Add("Content-type", "application/json")
err = json.NewEncoder(w).Encode(d)
if err != nil {
log.Println("error sending discovery:", err)
}
})
return mux
}
type update struct {
time time.Time
apps application.Apps
}