/
main.go
254 lines (211 loc) · 5.47 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
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
package main
import (
"encoding/json"
"fmt"
"log"
"strconv"
"strings"
"sync"
"time"
"github.com/iron-io/iron_go3/api"
"github.com/iron-io/iron_go3/cache"
"github.com/iron-io/iron_go3/config"
"github.com/iron-io/iron_go3/mq"
"github.com/iron-io/iron_go3/worker"
)
const (
defaultInterval = 10 * time.Second
defaultRuntime = 30 * time.Minute
configFile = "scale.json"
)
var (
s = state{
queueSizes: make(map[string]int),
codeIds: make(map[string]string),
}
client = api.HttpClient
c *Config
qCache *cache.Cache
)
func main() {
worker.ParseFlags()
var err error
c, err = getConfig(configFile)
if err != nil {
log.Fatalln(err)
}
// Setup cache settings
cacheEnv, exists := c.getSettings("cache", c.CacheEnv)
if !exists {
log.Fatalln("No cache environment set")
return
}
qCache = &cache.Cache{Settings: *cacheEnv, Name: "autoscale-prevs"}
// Determine runtime
runtime := defaultRuntime
if c.Runtime != nil {
runtime = time.Duration(*c.Runtime) * time.Second
}
// Start watchers
wg := &sync.WaitGroup{}
stop := make(chan struct{})
for _, alert := range c.Alerts {
if len(alert.Triggers) == 0 {
continue
}
wg.Add(1)
go watchTriggers(alert, wg, stop)
}
// Main goroutine sleeps, stops, then exits
time.Sleep(runtime)
close(stop)
wg.Wait()
}
func watchTriggers(a QueueWorkerAlert, wg *sync.WaitGroup, stop chan struct{}) {
defer wg.Done()
// Determine trigger check interval
interval := defaultInterval
if a.Interval != nil && *a.Interval >= 1 {
interval = time.Duration(*a.Interval) * time.Second
}
// Queue Settings
queueEnv, exists := c.getSettings("mq", a.QueueEnv)
if !exists {
fmt.Printf("Environment %q is not defined for queue %q\n", a.QueueEnv, a.QueueName)
return
}
q := mq.ConfigNew(a.QueueName, queueEnv)
// Worker Settings
workerEnv, exists := c.getSettings("worker", a.WorkerEnv)
if !exists {
fmt.Printf("Environment %q is not defined for queue %q\n", a.QueueEnv, a.QueueName)
return
}
knowPrevious := a.needPreviousSize()
Loop:
for {
checkTriggers(a, q, workerEnv, a.WorkerName, knowPrevious)
// Decide to stop looping
select {
case <-stop:
break Loop
default:
}
time.Sleep(interval)
}
}
func checkTriggers(a QueueWorkerAlert, q mq.Queue, workerEnv *config.Settings, codeName string, knowPrevious bool) {
qKey := queueKey(a)
queueSizePrev, sizePrevExists := 0, false
if knowPrevious {
queueSizePrev, sizePrevExists = queuePrevSize(qKey)
}
queueSizeCurr, sizeCurrErr := queueCurrSize(qKey, q)
if sizeCurrErr != nil {
fmt.Printf("Could not get info about %s: %v\n", q.Name, sizeCurrErr)
return
} else if knowPrevious {
// Update cache value
go qCache.Set(qKey, queueSizeCurr, 900)
}
// Update previous value
if sizePrevExists {
s.setQueueSize(qKey, queueSizeCurr)
} else {
queueSizePrev = queueSizeCurr
}
queued, running, err := codeStats(workerEnv, codeName)
if err != nil {
fmt.Printf("Could not get code stats for %s, err: %v\n", codeName, err)
return
}
// Determine amount of tasks to launch
launch, maxTrigger := evalTriggers(queued, running, queueSizeCurr, queueSizePrev, a.Triggers)
if a.Min != nil && (launch+queued+running) < *a.Min {
launch = *a.Min - queued - running
maxTrigger = minTrigger(*a.Min)
}
if a.Max != nil && (launch+queued+running) > *a.Max {
launch = *a.Max - queued - running
}
launchStmt := ""
if launch > 0 {
launchStmt = " Launching " + strconv.Itoa(launch)
d, _ := json.Marshal(maxTrigger)
dstr := string(d)
w := &worker.Worker{Settings: *workerEnv}
tasks := make([]worker.Task, launch)
for x := 0; x < len(tasks); x++ {
tasks[x].CodeName = a.WorkerName
tasks[x].Cluster = a.Cluster
tasks[x].Priority = a.Priority
tasks[x].Payload = dstr
}
_, err = w.TaskQueue(tasks...)
if err != nil {
fmt.Println("Could not create tasks for", a.WorkerName)
return
}
}
prevStmt := ""
if sizePrevExists {
prevStmt = ", prev: " + strconv.Itoa(queueSizePrev)
}
fmt.Printf("Queue: %s (size: %d%s), CodeName: %s (queued: %d, running: %d)%s\n", q.Name, queueSizeCurr, prevStmt, a.WorkerName, queued, running, launchStmt)
}
func queueKey(qw QueueWorkerAlert) string {
return qw.QueueEnv + "|" + qw.QueueName
}
func queuePrevSize(key string) (int, bool) {
if v, exists := s.getQueueSize(key); exists {
return v, true
}
vCached, err := qCache.Get(key)
if err == nil {
return int(vCached.(float64)), true
}
if !strings.Contains(err.Error(), "not found") {
fmt.Println(err) // Print errors not associated with cache/key not found errors
}
return 0, false
}
func queueCurrSize(key string, q mq.Queue) (int, error) {
i, err := q.Info()
if err != nil {
return 0, err
}
return i.Size, err
}
func evalTriggers(queued, running, queueSize, prevQueueSize int, triggers []Trigger) (launch int, maxTrigger Trigger) {
for _, t := range triggers {
tlaunch := 0
switch t.Typ {
case "fixed":
if queueSize >= t.Value && (queued+running) == 0 {
tlaunch = 1
}
case "progressive":
if queueSize < t.Value {
continue
}
previous_level := prevQueueSize / t.Value
current_level := queueSize / t.Value
tlaunch = current_level - previous_level
case "ratio":
expected_runners := (queueSize + t.Value - 1) / t.Value // Only have 0 runners if qsize=0
diff := expected_runners - (queued + running)
tlaunch = diff
}
if tlaunch > launch {
launch = tlaunch
maxTrigger = t
}
}
return launch, maxTrigger
}
func minTrigger(i int) Trigger {
return Trigger{
Typ: "min",
Value: i,
}
}