/
scaler.go
252 lines (199 loc) · 7.35 KB
/
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
package scaler
import (
"context"
"fmt"
"math"
"time"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/service/cloudwatch"
"github.com/aws/aws-sdk-go-v2/service/dynamodb"
dynamodbTypes "github.com/aws/aws-sdk-go-v2/service/dynamodb/types"
log "github.com/sirupsen/logrus"
)
type capacityType string
const readCapacity = "read"
const writeCapacity = "write"
type Scaler struct {
Config Config
cwClient *cloudwatch.Client
ddbClient *dynamodb.Client
}
func New(cfg Config) *Scaler {
return &Scaler{
Config: cfg,
cwClient: cloudwatch.NewFromConfig(cfg.AWSConfig),
ddbClient: dynamodb.NewFromConfig(cfg.AWSConfig),
}
}
func (s *Scaler) Run(ctx context.Context) error {
log.Debugf("config: %v", s.Config.ScalingTargets)
if err := s.Config.Check(); err != nil {
return fmt.Errorf("config check failed: %w", err)
}
ticker := time.NewTicker(s.Config.Interval)
s.scaleTargets(ctx)
for {
select {
case <-ctx.Done():
log.Infof("stopping execution: %s", ctx.Err())
return nil
case <-ticker.C:
s.scaleTargets(ctx)
}
}
}
func (s *Scaler) scaleTargets(ctx context.Context) {
for _, target := range s.Config.ScalingTargets {
err := s.scaleTarget(ctx, target)
if err != nil {
log.WithField("tableName", target.TableName).Warnf("skipping scaling action: %s", err)
}
}
}
func (s *Scaler) scaleTarget(ctx context.Context, target *TableScalingConfig) error {
table, err := s.ddbClient.DescribeTable(ctx, &dynamodb.DescribeTableInput{TableName: aws.String(target.TableName)})
if err != nil {
return fmt.Errorf("failed to get table status: %w", err)
}
if table.Table.TableStatus != "ACTIVE" {
return fmt.Errorf("table is currently in status: %s", table.Table.TableStatus)
}
tableCap, err := s.getTableCapacity(ctx, target.TableName)
if err != nil {
return fmt.Errorf("failed to get table capacity: %w", err)
}
tableCap.ReadCapacity.Provisioned = table.Table.ProvisionedThroughput.ReadCapacityUnits
tableCap.WriteCapacity.Provisioned = table.Table.ProvisionedThroughput.WriteCapacityUnits
log.WithField("tableName", target.TableName).Debugf("table capacity: %v", tableCap)
if !tableCap.isSafe() {
return fmt.Errorf("table capacity is not safe: %+v", *tableCap)
}
newReadCap, newWriteCap, err := s.calculateNewCapacity(tableCap, target)
if err != nil {
return fmt.Errorf("failed to calculate new capacity: %w", err)
}
if (newReadCap >= 1 || newWriteCap >= 1) &&
(newReadCap != *table.Table.ProvisionedThroughput.ReadCapacityUnits || newWriteCap != *table.Table.ProvisionedThroughput.WriteCapacityUnits) {
if newReadCap < *table.Table.ProvisionedThroughput.ReadCapacityUnits && newReadCap > 0 {
if time.Now().Sub(target.lastScaleTime) < target.ScaleDownDelay {
log.WithField("tableName", target.TableName).Debugf("skipping read scale down, next allowed at %s", target.lastScaleTime.Add(target.ScaleDownDelay).Format(time.RFC3339))
newReadCap = 0
}
}
if newWriteCap < *table.Table.ProvisionedThroughput.WriteCapacityUnits && newWriteCap > 0 {
if time.Now().Sub(target.lastScaleTime) < target.ScaleDownDelay {
log.WithField("tableName", target.TableName).Debugf("skipping write scale down, next allowed at %s", target.lastScaleTime.Add(target.ScaleDownDelay).Format(time.RFC3339))
newWriteCap = 0
}
}
if newReadCap != 0 && newWriteCap != 0 {
target.lastScaleTime = time.Now()
err = s.updateTableCapacity(ctx, table.Table, newReadCap, newWriteCap)
if err != nil {
return fmt.Errorf("failed to scale table: %w", err)
}
return nil
}
}
log.WithField("tableName", target.TableName).Debug("no scaling necessary")
return nil
}
func (s *Scaler) calculateNewCapacity(tableCap *tableCapacity, target *TableScalingConfig) (int64, int64, error) {
var newReadCap, newWriteCap int64
var err error
if tableCap.ReadCapacity.Throttles > 0 && tableCap.ReadCapacity.Provisioned != nil {
newReadCap, err = makeThrottlingScalingDecision(tableCap.ReadCapacity, target)
if err != nil {
return 0, 0, fmt.Errorf("failed to make read throttle scaling decision: %w", err)
}
}
if tableCap.WriteCapacity.Throttles > 0 && tableCap.WriteCapacity.Provisioned != nil {
newWriteCap, err = makeThrottlingScalingDecision(tableCap.WriteCapacity, target)
if err != nil {
return 0, 0, fmt.Errorf("failed to make write throttle scaling decision: %w", err)
}
}
if newReadCap == 0 {
newReadCap, err = makeConsumptionScalingDecision(tableCap.ReadCapacity, target)
if err != nil {
return 0, 0, fmt.Errorf("failed to make read consumption scaling decision: %w", err)
}
}
if newWriteCap == 0 {
newWriteCap, err = makeConsumptionScalingDecision(tableCap.WriteCapacity, target)
if err != nil {
return 0, 0, fmt.Errorf("failed to make write consumption scaling decision: %w", err)
}
}
return newReadCap, newWriteCap, nil
}
func (s *Scaler) updateTableCapacity(ctx context.Context, table *dynamodbTypes.TableDescription, newReadCap int64, newWriteCap int64) error {
provisionThroughput := dynamodbTypes.ProvisionedThroughput{
ReadCapacityUnits: table.ProvisionedThroughput.ReadCapacityUnits,
WriteCapacityUnits: table.ProvisionedThroughput.WriteCapacityUnits,
}
if newReadCap > 0 {
provisionThroughput.ReadCapacityUnits = aws.Int64(newReadCap)
}
if newWriteCap > 0 {
provisionThroughput.WriteCapacityUnits = aws.Int64(newWriteCap)
}
_, err := s.ddbClient.UpdateTable(ctx, &dynamodb.UpdateTableInput{
TableName: table.TableName,
ProvisionedThroughput: &provisionThroughput,
})
if err != nil {
return fmt.Errorf("failed to update table: %w", err)
}
log.WithField("tableName", *table.TableName).Infof("updated table capacity: read=%d,write=%d", newReadCap, newWriteCap)
return nil
}
func makeThrottlingScalingDecision(c capacity, target *TableScalingConfig) (int64, error) {
switch c.CapType {
case readCapacity:
result := (c.Throttles + float64(*c.Provisioned)) * (1 + target.ReadBufferCapacity)
if result > 40000 {
result = 40000
}
if result > target.ReadUpperBound {
result = target.ReadUpperBound
}
return int64(math.Round(result)), nil
case writeCapacity:
result := (c.Throttles + float64(*c.Provisioned)) * (1 + target.WriteBufferCapacity)
if result > 40000 {
result = 40000
}
if result > target.WriteUpperBound {
result = target.WriteUpperBound
}
return int64(math.Round(result)), nil
default:
return 0, fmt.Errorf("unknown capacity type: %s", c.CapType)
}
}
func makeConsumptionScalingDecision(c capacity, target *TableScalingConfig) (int64, error) {
switch c.CapType {
case readCapacity:
return calculateNewBoundary(float64(*c.Provisioned), *c.Consumed, target.ReadBufferCapacity, target.ReadLowerBound, target.ReadUpperBound), nil
case writeCapacity:
return calculateNewBoundary(float64(*c.Provisioned), *c.Consumed, target.ReadBufferCapacity, target.ReadLowerBound, target.ReadUpperBound), nil
default:
return 0, fmt.Errorf("unknown capacity type: %s", c.CapType)
}
}
func calculateNewBoundary(provisioned, consumed, buffer, lowerBound, upperBound float64) int64 {
scaleUpBoundary := provisioned * buffer
if consumed > scaleUpBoundary {
newBoundary := consumed * (1 + buffer)
if newBoundary > upperBound {
newBoundary = upperBound
}
return int64(math.Round(newBoundary))
}
newBoundary := consumed * (1 + buffer)
if newBoundary < lowerBound {
newBoundary = lowerBound
}
return int64(math.Round(newBoundary))
}