-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathantidote-connect.go
203 lines (182 loc) · 4.61 KB
/
antidote-connect.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
package main
import (
"flag"
"fmt"
antidote "github.com/AntidoteDB/antidote-go-client"
"net"
"os"
"strconv"
"strings"
"sync"
"time"
)
func parseHost(hostport string) (host antidote.Host, err error) {
name, port, err := net.SplitHostPort(hostport)
if err != nil {
err = fmt.Errorf("error parsing host %v: %v", hostport, err)
return
}
portI, err := strconv.Atoi(port)
if err != nil {
err = fmt.Errorf("error parsing host %v: port must be an integer", hostport)
return
}
host = antidote.Host{Name: name, Port: portI}
return
}
func parseHosts(hostports []string) (hosts []antidote.Host, err error) {
hosts = make([]antidote.Host, len(hostports))
for i, v := range hostports {
host, err := parseHost(v)
if err != nil {
return nil, err
}
hosts[i] = host
}
return hosts, nil
}
var quiet = flag.Bool("quiet", false, "Turn off status messages")
func debug(str string, vals ...interface{}) {
if !*quiet {
fmt.Printf(str+"\n", vals...)
}
}
func retry(retries int, f func() error) (err error) {
delay := 100 * time.Millisecond
tries := 0
for retries <= 0 || tries < retries {
err = f()
if err == nil {
return
}
fmt.Printf("ERROR %v\n", err)
debug("Retrying in %s", delay.String())
time.Sleep(delay)
delay *= 2
tries++
}
return
}
func main() {
createDc := flag.String("createDc", "", "Connects nodes in a DC. The first entry it the Antidote instance to connect to, the following arguments are Erlang node names to connect. For example:\n"+
" antidote-connect --createDc server1:8087 antidote@server2 antidote@server3")
connectDcs := flag.Bool("connectDcs", false, "Connects several DCs. Takes a list of 'hostname:port' where each entry is a node from a different DC. For example:\n"+
" antidote-connect --connectDcs server1:8087 server4:8087")
maxRetries := flag.Int("retries", 0, "Maximum number of retries for connections.")
flag.Parse()
tail := flag.Args()
work := false
if len(*createDc) > 0 {
host, err := parseHost(*createDc)
if err != nil {
fmt.Println("Error parsing host: ", err)
os.Exit(1)
}
err = retry(*maxRetries, func() error {
return runCreateDc(host, tail)
})
if err != nil {
os.Exit(1)
}
work = true
}
if *connectDcs {
hosts, err := parseHosts(tail)
if err != nil {
fmt.Println("Error parsing hosts: ", err)
os.Exit(1)
}
err = retry(*maxRetries, func() error {
return runConnectDcs(hosts)
})
if err != nil {
os.Exit(1)
}
work = true
}
if !work {
flag.Usage()
}
}
func runCreateDc(host antidote.Host, nodes []string) (err error) {
debug("Connecting to %s", host.Name)
client, err := antidote.NewClient(host)
if err != nil {
return fmt.Errorf("Could not connect to Antidote at %s:%d", host.Name, host.Port)
}
defer client.Close()
debug("Connecting DCs %s", strings.Join(nodes, ", "))
err = client.CreateDc(nodes)
if err != nil {
return fmt.Errorf("Could not create DC: %v", err)
}
debug("Done.")
return
}
func runConnectDcs(connectDcs []antidote.Host) (err error) {
var clients = make([]*antidote.Client, len(connectDcs))
defer func() {
for _, client := range clients {
if client != nil {
client.Close()
}
}
}()
var descriptors = make([][]byte, len(connectDcs))
debug("Getting connection descriptors")
var wg sync.WaitGroup
var lock = sync.Mutex{}
errors := make(chan error, len(connectDcs))
for i, host := range connectDcs {
wg.Add(1)
go func(i int, host antidote.Host) {
defer wg.Done()
client, err := antidote.NewClient(host)
if err != nil {
debug("%s", err.Error())
errors <- err
return
}
debug("Connection to %s:%d established. Getting connection descriptor.", host.Name, host.Port)
descriptor, err := client.GetConnectionDescriptor()
lock.Lock()
debug("Got connection descriptor from %s:%d", host.Name, host.Port)
clients[i] = client
descriptors[i] = descriptor
lock.Unlock()
}(i, host)
}
wg.Wait()
close(errors)
ok := true
for err := range errors {
ok = false
fmt.Println("Error connecting DCs:", err)
}
if !ok {
return fmt.Errorf("Could not connect DCs")
}
errors = make(chan error, len(connectDcs))
for i := range connectDcs {
wg.Add(1)
go func(i int) {
err := clients[i].ConnectToDCs(descriptors)
if err != nil {
errors <- fmt.Errorf("Could not connect %s:%d, %v", connectDcs[i].Name, connectDcs[i].Port, err)
}
wg.Done()
debug("Connection done on %s:%d", connectDcs[i].Name, connectDcs[i].Port)
}(i)
}
wg.Wait()
close(errors)
for err := range errors {
ok = false
fmt.Println("Error connecting DCs:", err)
}
if !ok {
return fmt.Errorf("Could not connect DCs")
}
debug("DCs connected")
return
}