-
Notifications
You must be signed in to change notification settings - Fork 103
/
scalingSpec.js
207 lines (183 loc) · 7.21 KB
/
scalingSpec.js
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
'use strict';
const find = require('lodash.find');
const { autoscaling, ecs } = require('@cumulus/common/aws');
const {
buildAndStartWorkflow,
waitForCompletedExecution,
getClusterArn
} = require('@cumulus/integration-tests');
const { sleep } = require('@cumulus/common/util');
const { loadConfig, loadCloudformationTemplate } = require('../helpers/testUtils');
const workflowName = 'HelloWorldOnDemandWorkflow';
const config = loadConfig();
jasmine.DEFAULT_TIMEOUT_INTERVAL = 1000000;
let clusterArn;
let autoScalingGroupName;
async function getClusterStats({
statTypes = ['runningEC2TasksCount', 'pendingEC2TasksCount']
}) {
const stats = (await ecs().describeClusters({
clusters: [clusterArn],
include: ['STATISTICS']
}).promise()).clusters[0].statistics;
const returnedStats = {};
statTypes.forEach((statType) => {
returnedStats[statType] = parseInt(find(stats, ['name', statType]).value, 10);
});
return returnedStats;
}
async function getAutoScalingGroupName(stackName) {
const autoScalingGroups = (await autoscaling().describeAutoScalingGroups({}).promise()).AutoScalingGroups;
const asg = find(autoScalingGroups, (group) => group.AutoScalingGroupName.match(new RegExp(stackName, 'g')));
return asg.AutoScalingGroupName;
}
const waitPeriod = 30000;
async function getNewScalingActivity() {
const params = {
AutoScalingGroupName: autoScalingGroupName,
MaxRecords: 1
};
let activities = await autoscaling().describeScalingActivities(params).promise();
const startingActivity = activities.Activities[0];
let mostRecentActivity = Object.assign({}, startingActivity);
/* eslint-disable no-await-in-loop */
while (startingActivity.ActivityId === mostRecentActivity.ActivityId) {
activities = await autoscaling().describeScalingActivities(params).promise();
mostRecentActivity = activities.Activities[0];
console.log(`No new activity found. Sleeping for ${waitPeriod / 1000} seconds.`);
await sleep(waitPeriod);
}
/* eslint-enable no-await-in-loop */
return mostRecentActivity;
}
const numExecutions = 3;
let cloudformationResources;
let numActivityTasks;
let minInstancesCount;
describe('cloudformation template for scaling policies', () => {
beforeAll(async (done) => {
const cloudformationTemplate = await loadCloudformationTemplate(config);
console.log(`cloudformationTemplate ${cloudformationTemplate}`);
cloudformationResources = cloudformationTemplate.Resources;
numActivityTasks = Object.values(cloudformationResources).filter((resource) => resource.Type === 'AWS::StepFunctions::Activity').length;
minInstancesCount = cloudformationResources.CumulusECSAutoScalingGroup.UpdatePolicy.AutoScalingRollingUpdate.MinInstancesInService;
done();
});
describe('SimpleScaling Policies', () => {
const simpleScalingPolicy = {
Type: 'AWS::AutoScaling::ScalingPolicy',
Properties: {
AdjustmentType: 'ChangeInCapacity',
AutoScalingGroupName: {
Ref: 'CumulusECSAutoScalingGroup'
},
Cooldown: 60,
PolicyType: 'SimpleScaling'
}
};
it('ScaleOut has the expected values', () => {
const expectedScaleOutPolicy = Object.assign({}, simpleScalingPolicy);
expectedScaleOutPolicy.Properties.ScalingAdjustment = 1;
expect(cloudformationResources.ScaleOutScalingPolicy).toEqual(expectedScaleOutPolicy);
});
it('ScaleIn has the expected values', () => {
const expectedScaleInPolicy = Object.assign({}, simpleScalingPolicy);
expectedScaleInPolicy.Properties.ScalingAdjustment = -1;
expect(cloudformationResources.ScaleInScalingPolicy).toEqual(expectedScaleInPolicy);
});
});
describe('StepScaling Policy', () => {
const expectedStepScalingPolicy = {
Type: 'AWS::AutoScaling::ScalingPolicy',
Properties: {
PolicyType: 'StepScaling',
AdjustmentType: 'ChangeInCapacity',
EstimatedInstanceWarmup: 60,
MetricAggregationType: 'Average',
AutoScalingGroupName: {
Ref: 'CumulusECSAutoScalingGroup'
},
StepAdjustments: [{
MetricIntervalLowerBound: 0,
ScalingAdjustment: 1
},
{
MetricIntervalUpperBound: 0,
ScalingAdjustment: -1
}]
}
};
it('has the expected values', () => {
expect(cloudformationResources.StepScalingPolicy).toEqual(expectedStepScalingPolicy);
});
});
describe('TargetScalingPolicy', () => {
const expectedTargetScalingPolicy = {
Type: 'AWS::AutoScaling::ScalingPolicy',
Properties: {
PolicyType: 'TargetTrackingScaling',
AdjustmentType: 'ChangeInCapacity',
EstimatedInstanceWarmup: 60,
Cooldown: 60,
AutoScalingGroupName: {
Ref: 'CumulusECSAutoScalingGroup'
},
TargetTrackingConfiguration: {
DisableScaleIn: false,
TargetValue: 50,
PredefinedMetricSpecification: {
PredefinedMetricType: 'ASGAverageNetworkIn'
}
}
}
};
it('has the expected values', () => {
expect(cloudformationResources.TargetScalingPolicy).toEqual(expectedTargetScalingPolicy);
});
});
});
describe('When a task is configured to run in ECS', () => {
beforeAll(async () => {
clusterArn = await getClusterArn(config.stackName);
autoScalingGroupName = await getAutoScalingGroupName(config.stackName);
});
describe('the load on the system exceeds that which its resources can handle', () => {
let workflowExecutionArns = [];
beforeAll(async () => {
const workflowExecutionPromises = [];
for (let i = 0; i < numExecutions; i += 1) {
workflowExecutionPromises.push(buildAndStartWorkflow(
config.stackName,
config.bucket,
workflowName
));
}
workflowExecutionArns = await Promise.all(workflowExecutionPromises);
});
afterAll(async () => {
const completions = workflowExecutionArns.map((executionArn) => waitForCompletedExecution(executionArn));
await Promise.all(completions);
});
it('can handle the load', async () => {
await sleep(5000);
const stats = await getClusterStats({});
expect(stats.runningEC2TasksCount + stats.pendingEC2TasksCount).toEqual(numExecutions + numActivityTasks);
});
it('adds new resources', async () => {
console.log('Waiting for scale out policy to take affect.');
const mostRecentActivity = await getNewScalingActivity();
expect(mostRecentActivity.Description).toMatch(/Launching a new EC2 instance: i-*/);
});
});
describe('the load on the system is far below what its resources can handle', () => {
it('removes excessive resources but not all resources', async () => {
console.log('Waiting for scale in policy to take affect.');
const mostRecentActivity = await getNewScalingActivity();
expect(mostRecentActivity.Description).toMatch(/Terminating EC2 instance: i-*/);
const stats = await getClusterStats({});
expect(stats.runningEC2TasksCount + stats.pendingEC2TasksCount).toEqual(numActivityTasks);
const instances = await ecs().listContainerInstances({ cluster: clusterArn }).promise();
expect(instances.containerInstanceArns.length).toEqual(minInstancesCount);
});
});
});