-
Notifications
You must be signed in to change notification settings - Fork 51
/
connect.go
180 lines (151 loc) · 3.13 KB
/
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
package main
import (
"fmt"
"sync"
"sync/atomic"
"time"
"github.com/reconquest/hierr-go"
)
const (
longConnectionWarningTimeout = 2 * time.Second
)
// connectToCluster tries to acquire atomic file lock on each of
// specified remote nodes. lockFile is used to specify target lock file, it
// must exist on every node. runnerFactory will be used to make connection
// to remote node. If noLockFail is given, then only warning will be printed
// if lock process has been failed.
func connectToCluster(
lockFile string,
runnerFactory runnerFactory,
addresses []address,
noLock bool,
noLockFail bool,
noConnFail bool,
heartbeat func(*distributedLockNode),
) (*distributedLock, error) {
var (
cluster = &distributedLock{}
errors = make(chan error, 0)
nodeAddMutex = &sync.Mutex{}
)
status := &struct {
Phase string
Total int64
Fails int64
Success int64
}{
Phase: `lock`,
Total: int64(len(addresses)),
}
if noLock {
status.Phase = `connect`
}
setStatus(status)
for _, nodeAddress := range addresses {
go func(nodeAddress address) {
pool.run(func() {
failed := false
node, err := connectToNode(cluster, runnerFactory, nodeAddress)
if err != nil {
atomic.AddInt64(&status.Fails, 1)
atomic.AddInt64(&status.Total, -1)
if noConnFail {
failed = true
warningln(err)
} else {
errors <- err
return
}
} else {
if !noLock {
err = node.lock(lockFile)
if err != nil {
if noLockFail {
warningln(err)
} else {
errors <- err
return
}
} else {
go heartbeat(node)
}
}
}
textStatus := "established"
if failed {
textStatus = "failed"
} else {
atomic.AddInt64(&status.Success, 1)
nodeAddMutex.Lock()
defer nodeAddMutex.Unlock()
cluster.nodes = append(cluster.nodes, node)
}
debugf(
`%4d/%d (%d failed) connection %s: %s`,
status.Success,
status.Total,
status.Fails,
textStatus,
nodeAddress,
)
errors <- nil
})
}(nodeAddress)
}
erronous := 0
topError := hierr.Push(`can't connect to nodes`)
for range addresses {
err := <-errors
if err != nil {
erronous++
topError = hierr.Push(topError, err)
}
}
if erronous > 0 {
return nil, hierr.Push(
fmt.Errorf(
`connection to %d of %d nodes failed`,
erronous,
len(addresses),
),
topError,
)
}
return cluster, nil
}
func connectToNode(
cluster *distributedLock,
runnerFactory runnerFactory,
address address,
) (*distributedLockNode, error) {
tracef(`connecting to address: '%s'`, address)
done := make(chan struct{}, 0)
go func() {
select {
case <-done:
return
case <-time.After(longConnectionWarningTimeout):
warningf(
"still connecting to address after %s: %s",
longConnectionWarningTimeout,
address,
)
<-done
}
}()
defer func() {
done <- struct{}{}
}()
runner, err := runnerFactory(address)
if err != nil {
return nil, hierr.Errorf(
err,
`can't connect to address: %s`,
address,
)
}
return &distributedLockNode{
address: address,
runner: runner,
}, nil
}