-
Notifications
You must be signed in to change notification settings - Fork 9
/
main.go
216 lines (182 loc) · 5.26 KB
/
main.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
package main
import (
"fmt"
"os"
"strconv"
"time"
"github.com/mit-dci/pooldetective/logging"
"github.com/mit-dci/pooldetective/util"
"github.com/mit-dci/pooldetective/wire"
)
var locationID int
func main() {
logging.SetLogLevel(util.GetLoglevelFromEnv())
logging.Debugf("Starting coordinator...")
locationID64, err := strconv.ParseInt(os.Getenv("LOCATIONID"), 10, 32)
if err != nil {
panic(err)
}
locationID = int(locationID64)
initDocker()
err = initNetwork()
if err != nil {
panic(err)
}
err = pullLatestImages()
if err != nil {
panic(err)
}
go imageChangeDetector()
refresh := make(chan bool, 1)
restart := make(chan int, 10)
go subscribe(refresh, restart)
go func() {
for poID := range restart {
err := restartStratumClient(poID)
if err != nil {
logging.Errorf("Unable to restart pool observer %d: %v", poID, err)
}
}
}()
refresh <- true
for {
req, err := wire.NewClient(os.Getenv("HUBHOST"), wire.PortCoordinator)
if err != nil {
logging.Errorf("Could not connect to coordinator hub: %v", err)
time.Sleep(time.Second * 5)
continue
}
for {
<-refresh
logging.Infof("Refreshing config")
err = refreshConfig(req)
if err != nil {
logging.Errorf("Could not refresh config: %v", err)
time.Sleep(time.Second * 10)
break
}
}
req.Close()
time.Sleep(time.Second)
}
}
func imageChangeDetector() {
images := []string{"blockobserver", "stratumclient", "stratumserver"}
imageIDs := make([]string, len(images))
for i, img := range images {
imageIDs[i], _ = getImage(getPrefixedImageName(img))
}
for {
reapply := false
time.Sleep(time.Second * 60)
pullLatestImages()
for i, img := range images {
newImageID, _ := getImage(getPrefixedImageName(img))
if newImageID != imageIDs[i] {
imageIDs[i] = newImageID
reapply = true
}
}
if reapply {
logging.Debugf("Found changed images, reapplying config")
err := reapplyConfig()
if err != nil {
logging.Errorf("Could not reapply config: %v", err)
}
}
checkBlockObserver()
}
}
func refreshConfig(req *wire.PoolDetectiveConn) error {
err := req.Send(&wire.CoordinatorGetConfigRequestMsg{LocationID: locationID})
if err != nil {
return fmt.Errorf("Could not send CoordinatorGetConfigRequestMsg request to hub: %v", err)
}
msg, ok, err := req.Recv()
if err != nil || !ok {
return fmt.Errorf("Could not receive CoordinatorGetConfigResponseMsg response from hub: %v", err)
}
config, ok := msg.(*wire.CoordinatorGetConfigResponseMsg)
if !ok {
errMsg, ok := msg.(*wire.ErrorMsg)
if ok {
return fmt.Errorf("Error from hub: %v", errMsg.Error)
}
return fmt.Errorf("CoordinatorGetConfigResponseMsg response from hub is wrong type: %T", msg)
}
err = applyConfig(config)
if err != nil {
logging.Errorf("Could not apply config: %v", err)
}
return nil
}
var lastConfig *wire.CoordinatorGetConfigResponseMsg
func reapplyConfig() error {
return applyConfig(lastConfig)
}
func applyConfig(config *wire.CoordinatorGetConfigResponseMsg) error {
lastConfig = config
logging.Debugf("Starting %d stratum servers and %d stratum clients\n", len(config.StratumServers), len(config.StratumClientPoolObserverIDs))
err := checkBlockObserver()
if err != nil {
logging.Errorf("Failed to configure block observer: %v", err)
}
for _, i := range config.StratumClientPoolObserverIDs {
logging.Debugf("Checking stratum observer %d", i)
err := checkStratumClient(i)
if err != nil {
logging.Errorf("Failed to configure stratum client: %v", err)
}
}
algorithmIDs := make([]int, len(config.StratumServers))
for i, s := range config.StratumServers {
algorithmIDs[i] = s.AlgorithmID
logging.Debugf("Checking stratum server [Algorithm %d - Port %d - Protocol %d]", s.AlgorithmID, s.Port, s.Protocol)
err := checkStratumServer(s.AlgorithmID, s.Port, s.Protocol)
if err != nil {
logging.Errorf("Failed to configure stratum server: %v", err)
}
}
err = removeUnneededContainers("stratum-client", config.StratumClientPoolObserverIDs)
if err != nil {
logging.Errorf("Failed to remove unneeded stratum clients: %v", err)
}
err = removeUnneededContainers("stratum-server", algorithmIDs)
if err != nil {
logging.Errorf("Failed to remove unneeded stratum servers: %v", err)
}
return nil
}
func subscribe(refresh chan bool, restart chan int) {
for {
logging.Infof("Subscribing to Pub/Sub")
sub, err := wire.NewSubscriber(os.Getenv("HUBHOST"), wire.PortPubSubSubscribers)
if err != nil {
logging.Errorf("Could not connect to subscriber hub: %v", err)
time.Sleep(time.Second * 5)
continue
}
logging.Infof("Subscribed to Pub/Sub")
sub.Subscribe([]byte(fmt.Sprintf("co-%03d", locationID)))
for {
msg, _, _, err := sub.RecvSub()
if err != nil {
logging.Errorf("[Subscriber] Error receiving message: %v", err)
break
}
logging.Infof("Received message from Pub/Sub")
switch t := msg.(type) {
case *wire.CoordinatorRefreshConfigMsg:
logging.Infof("Received config refresh request from pubsub")
refresh <- true
case *wire.CoordinatorRestartPoolObserverMsg:
logging.Infof("Received restart pool observer request from pubsub")
restart <- t.PoolObserverID
default:
logging.Warnf("Received unknown message from Pub/Sub: %T", msg)
}
}
sub.Close()
time.Sleep(time.Second)
}
}