This repository has been archived by the owner on Nov 19, 2020. It is now read-only.
/
app_auto_scaler.go
334 lines (255 loc) · 9.62 KB
/
app_auto_scaler.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
package main
import (
"code.google.com/p/gorest"
"fmt"
"net/http"
"os"
"strconv"
"sync"
"time"
cfutils "github.com/cf-platform-eng/autoscaler/cf_utils"
scalerutils "github.com/cf-platform-eng/autoscaler/scaler_utils"
)
var (
api_endpoint string
unit_increment = 1
DEBUG_LOCK = false
listen_port string
appDetailsMapMutex = &sync.RWMutex{}
appInstanceMapMutex = &sync.RWMutex{}
appDetailsMap map[string]scalerutils.AppDetail
appInstanceMap map[string]scalerutils.AppInstanceDetail
)
// Save app details with expiration of 60 seconds
// reload the app instance data if request comes for app details after the duration
const cache_expiration = 60
func init() {
if listen_port = os.Getenv("PORT"); listen_port == "" {
listen_port = "8080"
}
if debug_lock := os.Getenv("DEBUG_LOCK"); debug_lock != "" {
DEBUG_LOCK = true
}
fmt.Printf("App Auto Scaler Properties\n")
fmt.Printf(" Listen Port: %s !!\n", listen_port)
fmt.Printf(" Instance default increment set to: %d !!\n\n", unit_increment)
fmt.Printf(" Instance Cache Expiration set to: %d seconds !!\n", cache_expiration)
appDetailsMap = make(map[string]scalerutils.AppDetail)
appInstanceMap = make(map[string]scalerutils.AppInstanceDetail)
// Load persisted information into memory
// We are only persisting and reloading app data (org/space/app name and not instances)
// The app instance information can be stale and its better to get that from CF
if (scalerutils.IsDBEnabled()) {
appDetails := scalerutils.Load()
for _, appDetail := range appDetails {
register(appDetail, false)
}
}
}
func main() {
//var appDetailsMapMutex = &sync.Mutex{}
gorest.RegisterMarshaller("application/json", gorest.NewJSONMarshaller())
gorest.RegisterService(new(AppAutoScalerService)) //Register our service
http.Handle("/", gorest.Handle())
http.ListenAndServe(":"+listen_port, nil)
defer close()
}
func close() {
if (scalerutils.IsDBEnabled()) {
scalerutils.CloseDB()
}
}
//Service Definition
type AppAutoScalerService struct {
gorest.RestService `root:"/"`
load gorest.EndPoint `method:"POST" path:"/load/" postdata:"[]AppDetail"`
register gorest.EndPoint `method:"POST" path:"/register/" postdata:"AppDetail" `
deregister gorest.EndPoint `method:"DELETE" path:"/register/{destnName:string}"`
details gorest.EndPoint `method:"GET" path:"/details" output:"string"`
queueDetails gorest.EndPoint `method:"GET" path:"/details/{destnName:string}" output:"string"`
scaleUp gorest.EndPoint `method:"PUT" path:"/scale/{destnName:string}/up/{increments:int}" postdata:"string" `
scaleDown gorest.EndPoint `method:"PUT" path:"/scale/{destnName:string}/down/{increments:int}" postdata:"string" `
}
func lockDebug(lockType string, activity string, step string) {
if (DEBUG_LOCK) {
fmt.Printf("%s (%s)-Lock from %s\n", activity, lockType, step )
}
}
func (serv AppAutoScalerService) Register(appDetail scalerutils.AppDetail) {
register(appDetail, true)
}
// Private method to handle both new incoming registrations
// as well as loading up saved app details
// isNew is false for reloading previously persisted data
func register(appDetail scalerutils.AppDetail, isNew bool) {
fmt.Printf("\nRegistering App ... %#v", appDetail)
destnName := appDetail.Target
lockDebug("AD Write", "register", "Locking")
appDetailsMapMutex.Lock()
appDetailsMap[destnName] = appDetail
appDetailsMapMutex.Unlock()
lockDebug("AD Write", "register", "Released")
if (scalerutils.IsDBEnabled() && isNew) {
scalerutils.Insert(appDetail)
}
// Get the actual instance count... from CF
// A map of Guid and Instance count is returned by cfutils.FindApp,
// using 'guid' and 'instances' as keys
appGuidInstanceDetail := cfutils.FindApp(appDetail.Org, appDetail.Space, appDetail.AppName)
var instances int
var appGuid string
if appGuid = appGuidInstanceDetail["guid"]; appGuid != "" {
instanceCount, err := strconv.Atoi(appGuidInstanceDetail["instances"])
if err != nil {
fmt.Printf("Could not convert into int: %v\n", err)
} else {
instances = instanceCount
}
} else {
fmt.Printf("\nWarning!! Failed to locate App : %s in Org: %s, Space: %s\n", appDetail.AppName, appDetail.Org, appDetail.Space)
return
}
fmt.Printf("\nApp: %s under org: %s, space: %s, app guid: %s, instances: %d\n", appDetail.AppName, appDetail.Org, appDetail.Space, appGuid, instances)
appInstanceDetails := scalerutils.AppInstanceDetail{destnName, appDetail.AppName, appGuid, instances, time.Now().Unix()}
lockDebug("AI Write", "register", "Locking")
appInstanceMapMutex.Lock()
appInstanceMap[destnName] = appInstanceDetails
appInstanceMapMutex.Unlock()
lockDebug("AI Write", "register", "Released")
}
func (serv AppAutoScalerService) Load(appDetails []scalerutils.AppDetail) {
for _, appDetail := range appDetails {
serv.Register(appDetail)
}
}
func getDetail(target string) string {
var output string
curTime := time.Now().Unix()
lockDebug("AI Read", "detail", "Locking")
appInstanceMapMutex.RLock()
appInstance := appInstanceMap[target]
appInstanceMapMutex.RUnlock()
lockDebug("AI Read", "detail", "Released")
if (curTime - appInstance.LastFetchTime) < cache_expiration {
output = fmt.Sprintf("{ 'queue' : '%s', 'app' : '%s', 'instances': '%d'}\n", target, appInstance.AppName, appInstance.Instances)
return output
}
lockDebug("AD Read", "detail", "Locking")
appDetailsMapMutex.RLock()
appDetail := appDetailsMap[target]
appDetailsMapMutex.RUnlock()
lockDebug("AD Read", "detail", "Released")
appGuidInstanceDetail := cfutils.FindApp(appDetail.Org, appDetail.Space, appDetail.AppName)
if appGuid := appGuidInstanceDetail["guid"]; appGuid != "" {
instanceCount, err := strconv.Atoi(appGuidInstanceDetail["instances"])
if err != nil {
fmt.Printf("Could not convert into int: %v\n", err)
output = fmt.Sprintf("Error with Target: %s, could not covert instances count to int: %s", target, err)
} else {
appInstance.Instances = instanceCount
output = fmt.Sprintf("{ 'queue' : '%s', 'app' : '%s', 'instances': '%d'}\n", target, appInstance.AppName, appInstance.Instances)
}
} else {
appInstance.Instances = 0
fmt.Printf("\nError!! Failed to locate App : %s in Org: %s, Space: %s\n", appDetail.AppName, appDetail.Org, appDetail.Space)
output = fmt.Sprintf("\nError!! Failed to locate App : %s in Org: %s, Space: %s\n", appDetail.AppName, appDetail.Org, appDetail.Space)
// Acquire Lock before updating...
lockDebug("AI Write", "update as not-found", "Locking")
appInstanceMapMutex.Lock()
appInstanceMap[target] = appInstance
appInstanceMapMutex.Unlock()
lockDebug("AI Write", "update as not-found", "Released")
return output
}
// Update Last fetch time
appInstance.LastFetchTime = time.Now().Unix()
// Acquire Lock before updating...
lockDebug("AI Write", "update detail", "Locking")
appInstanceMapMutex.Lock()
appInstanceMap[target] = appInstance
appInstanceMapMutex.Unlock()
lockDebug("AI Write", "update detail", "Released")
return output
}
func (serv AppAutoScalerService) Details() string {
output := "["
for key := range appInstanceMap {
output += getDetail(key) + "\n"
}
output = output + "]"
return output
}
func (serv AppAutoScalerService) QueueDetails(target string) string {
return getDetail(target)
}
func findAppDetail(target string) scalerutils.AppDetail {
appDetail := appDetailsMap[target]
return appDetail
}
func deleteAppDetail(target string) {
delete(appDetailsMap, target)
}
func findAppInstanceDetail(target string) scalerutils.AppInstanceDetail {
appInstance := appInstanceMap[target]
return appInstance
}
func deleteAppInstanceDetail(target string) {
delete(appInstanceMap, target)
}
func isAppInstanceNil(appInstance scalerutils.AppInstanceDetail) bool {
return appInstance.AppName == ""
}
func isAppNil(app scalerutils.AppDetail) bool {
return app.AppName == ""
}
func (serv AppAutoScalerService) Deregister(target string) {
lockDebug("AI Write", "deregister", "Locking")
appInstanceMapMutex.Lock()
deleteAppInstanceDetail(target)
appInstanceMapMutex.Unlock()
lockDebug("AI Write", "deregister", "Released")
appDetail := appDetailsMap[target]
if (scalerutils.IsDBEnabled()) {
scalerutils.Delete(appDetail)
}
lockDebug("AD Write", "deregister", "Locking")
appDetailsMapMutex.Lock()
deleteAppDetail(target)
appDetailsMapMutex.Unlock()
lockDebug("AD Write", "deregister", "Released")
}
func (serv AppAutoScalerService) Scale(target string, increments int, up bool) {
lockDebug("AI Write", "scale", "Locking")
appInstanceMapMutex.Lock()
appInstanceDetails := appInstanceMap[target]
if isAppInstanceNil(appInstanceDetails) {
fmt.Printf("Error!! App Instance not found for %s\n", target)
appInstanceMapMutex.Unlock()
lockDebug("AI Write", "scale", "Released")
return
}
if increments == 0 {
increments = unit_increment
}
direction := "Up"
increment := increments
if !up {
increment = -increment
direction = "Down"
}
appInstanceDetails.Instances += increment
if appInstanceDetails.Instances < 0 {
appInstanceDetails.Instances = 0
}
appInstanceMap[target] = appInstanceDetails
appInstanceMapMutex.Unlock()
lockDebug("AI Write", "scale", "Released")
fmt.Printf("\nScaling[%s] App : { 'queue' : '%s', 'app' : '%s', 'instances': '%d'}\n", direction, target, appInstanceDetails.AppName, appInstanceDetails.Instances)
cfutils.ScaleApp(appInstanceDetails.AppName, appInstanceDetails.Instances)
}
func (serv AppAutoScalerService) ScaleUp(postdata string, target string, increments int) {
serv.Scale(target, increments, true)
}
func (serv AppAutoScalerService) ScaleDown(postdata string, target string, increments int) {
serv.Scale(target, increments, false)
}