-
Notifications
You must be signed in to change notification settings - Fork 39.1k
/
guestbook.go
306 lines (259 loc) · 8.89 KB
/
guestbook.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
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package guestbook
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
"strings"
"time"
"github.com/spf13/cobra"
utilnet "k8s.io/apimachinery/pkg/util/net"
)
// CmdGuestbook is used by agnhost Cobra.
var CmdGuestbook = &cobra.Command{
Use: "guestbook",
Short: "Creates a HTTP server with various endpoints representing a guestbook app",
Long: `Starts a HTTP server on the given --http-port (default: 80), serving various endpoints representing a guestbook app. The endpoints and their purpose are:
- /register: A guestbook replica will subscribe to a primary, to its given --replicaof endpoint. The primary will then push any updates it receives to its registered replicas through the --backend-port.
- /get: Returns '{"data": value}', where the value is the stored value for the given key if non-empty, or the entire store.
- /set: Will set the given key-value pair in its own store and propagate it to its replicas, if any. Will return '{"data": "Updated"}' to the caller on success.
- /guestbook: Will proxy the request to agnhost-primary if the given cmd is 'set', or agnhost-replica if the given cmd is 'get'.`,
Args: cobra.MaximumNArgs(0),
Run: main,
}
var (
httpPort string
backendPort string
replicaOf string
replicas []string
store map[string]interface{}
)
const (
timeout = time.Duration(15) * time.Second
sleep = time.Duration(1) * time.Second
)
func init() {
CmdGuestbook.Flags().StringVar(&httpPort, "http-port", "80", "HTTP Listen Port")
CmdGuestbook.Flags().StringVar(&backendPort, "backend-port", "6379", "Backend's HTTP Listen Port")
CmdGuestbook.Flags().StringVar(&replicaOf, "replicaof", "", "The host's name to register to")
store = make(map[string]interface{})
}
func main(cmd *cobra.Command, args []string) {
go registerNode(replicaOf, backendPort)
startHTTPServer(httpPort)
}
func registerNode(registerTo, port string) {
if registerTo == "" {
return
}
hostPort := net.JoinHostPort(registerTo, backendPort)
start := time.Now()
for time.Since(start) < timeout {
host, err := getIP(hostPort)
if err != nil {
log.Printf("unable to get IP %s: %v. Retrying in %s.", hostPort, err, sleep)
time.Sleep(sleep)
continue
}
request := fmt.Sprintf("register?host=%s", host.String())
log.Printf("Registering to primary: %s/%s", hostPort, request)
_, err = net.ResolveTCPAddr("tcp", hostPort)
if err != nil {
log.Printf("unable to resolve %s, --replicaof param and/or --backend-port param are invalid: %v. Retrying in %s.", hostPort, err, sleep)
time.Sleep(sleep)
continue
}
response, err := dialHTTP(request, hostPort)
if err != nil {
log.Printf("encountered error while registering to primary: %v. Retrying in %s.", err, sleep)
time.Sleep(sleep)
continue
}
responseJSON := make(map[string]interface{})
err = json.Unmarshal([]byte(response), &responseJSON)
if err != nil {
log.Fatalf("Error while unmarshaling primary's response: %v", err)
}
var ok bool
store, ok = responseJSON["data"].(map[string]interface{})
if !ok {
log.Fatalf("Could not cast responseJSON: %s", responseJSON["data"])
}
log.Printf("Registered to node: %s", registerTo)
return
}
log.Fatal("Timed out while registering to primary.")
}
func startHTTPServer(port string) {
http.HandleFunc("/register", registerHandler)
http.HandleFunc("/get", getHandler)
http.HandleFunc("/set", setHandler)
http.HandleFunc("/guestbook", guestbookHandler)
log.Fatal(http.ListenAndServe(fmt.Sprintf(":%s", port), nil))
}
// registerHandler will register the caller in this server's list of replicas.
// /set requests will be propagated to replicas, if any.
func registerHandler(w http.ResponseWriter, r *http.Request) {
values, err := url.Parse(r.URL.RequestURI())
if err != nil {
http.Error(w, fmt.Sprintf("%v", err), http.StatusBadRequest)
return
}
ip := values.Query().Get("host")
log.Printf("GET /register?host=%s", ip)
// send all the store to the replica as well.
output := make(map[string]interface{})
output["data"] = store
bytes, err := json.Marshal(output)
if err != nil {
http.Error(w, fmt.Sprintf("response could not be serialized. %v", err), http.StatusExpectationFailed)
return
}
fmt.Fprint(w, string(bytes))
replicas = append(replicas, ip)
log.Printf("Node '%s' registered.", ip)
}
// getHandler will return '{"data": value}', where value is the stored value for
// the given key if non-empty, or entire store.
func getHandler(w http.ResponseWriter, r *http.Request) {
values, err := url.Parse(r.URL.RequestURI())
if err != nil {
http.Error(w, fmt.Sprintf("%v", err), http.StatusBadRequest)
return
}
key := values.Query().Get("key")
log.Printf("GET /get?key=%s", key)
output := make(map[string]interface{})
if key == "" {
output["data"] = store
} else {
value, found := store[key]
if !found {
value = ""
}
output["data"] = value
}
bytes, err := json.Marshal(output)
if err == nil {
fmt.Fprint(w, string(bytes))
} else {
http.Error(w, fmt.Sprintf("response could not be serialized. %v", err), http.StatusExpectationFailed)
}
}
// setHandler will set the given key-value pair in its own store and propagate
// it to its replicas, if any. Will return '{"message": "Updated"}' to the caller on success.
func setHandler(w http.ResponseWriter, r *http.Request) {
values, err := url.Parse(r.URL.RequestURI())
if err != nil {
http.Error(w, fmt.Sprintf("%v", err), http.StatusBadRequest)
return
}
key := values.Query().Get("key")
value := values.Query().Get("value")
log.Printf("GET /set?key=%s&value=%s", key, value)
if key == "" {
http.Error(w, "cannot set with empty key.", http.StatusBadRequest)
return
}
store[key] = value
request := fmt.Sprintf("set?key=%s&value=%s", key, value)
for _, replica := range replicas {
hostPort := net.JoinHostPort(replica, backendPort)
_, err = dialHTTP(request, hostPort)
if err != nil {
http.Error(w, fmt.Sprintf("encountered error while propagating to replica '%s': %v", replica, err), http.StatusExpectationFailed)
return
}
}
output := map[string]string{}
output["message"] = "Updated"
bytes, err := json.Marshal(output)
if err == nil {
fmt.Fprint(w, string(bytes))
} else {
http.Error(w, fmt.Sprintf("response could not be serialized. %v", err), http.StatusExpectationFailed)
}
}
// guestbookHandler will proxy the request to agnhost-primary if the given cmd is
// 'set' or agnhost-replica if the given cmd is 'get'.
func guestbookHandler(w http.ResponseWriter, r *http.Request) {
values, err := url.Parse(r.URL.RequestURI())
if err != nil {
http.Error(w, fmt.Sprintf("%v", err), http.StatusBadRequest)
return
}
cmd := strings.ToLower(values.Query().Get("cmd"))
key := values.Query().Get("key")
value := values.Query().Get("value")
log.Printf("GET /guestbook?cmd=%s&key=%s&value=%s", cmd, key, value)
if cmd != "get" && cmd != "set" {
http.Error(w, fmt.Sprintf("unsupported cmd: '%s'", cmd), http.StatusBadRequest)
return
}
if cmd == "set" && key == "" {
http.Error(w, "cannot set with empty key.", http.StatusBadRequest)
return
}
host := "agnhost-primary"
if cmd == "get" {
host = "agnhost-replica"
}
hostPort := net.JoinHostPort(host, backendPort)
_, err = net.ResolveTCPAddr("tcp", hostPort)
if err != nil {
http.Error(w, fmt.Sprintf("host and/or port param are invalid. %v", err), http.StatusBadRequest)
return
}
request := fmt.Sprintf("%s?key=%s&value=%s", cmd, key, value)
response, err := dialHTTP(request, hostPort)
if err == nil {
fmt.Fprint(w, response)
} else {
http.Error(w, fmt.Sprintf("encountered error: %v", err), http.StatusExpectationFailed)
}
}
func dialHTTP(request, hostPort string) (string, error) {
transport := utilnet.SetTransportDefaults(&http.Transport{})
httpClient := createHTTPClient(transport)
resp, err := httpClient.Get(fmt.Sprintf("http://%s/%s", hostPort, request))
defer transport.CloseIdleConnections()
if err == nil {
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err == nil {
return string(body), nil
}
}
return "", err
}
func createHTTPClient(transport *http.Transport) *http.Client {
client := &http.Client{
Transport: transport,
Timeout: 5 * time.Second,
}
return client
}
func getIP(hostPort string) (net.IP, error) {
conn, err := net.Dial("udp", hostPort)
if err != nil {
return []byte{}, err
}
defer conn.Close()
localAddr := conn.LocalAddr().(*net.UDPAddr)
return localAddr.IP, nil
}