forked from facebookarchive/cassandra-aws-benchmark
-
Notifications
You must be signed in to change notification settings - Fork 0
/
cloudformation.yaml
512 lines (490 loc) · 17.9 KB
/
cloudformation.yaml
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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
#
# Copyright 2018-present, Facebook, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
AWSTemplateFormatVersion: '2010-09-09'
Description: CF template for provision single DC cassandra cluster for benchmark purpose
Parameters:
KeyName:
Description: Name of an existing EC2 KeyPair to enable SSH access to the instance
Type: AWS::EC2::KeyPair::KeyName
ConstraintDescription: must be the name of an existing EC2 KeyPair.
Default: sshkey
NoneSeedFleetSize:
Type: Number
Description: Number of nodes that is not seed. ClusterSize=NoneSeedFleetSize+1
Default: '2'
InstanceType:
Description: Node EC2 instance type
Type: String
Default: i3.xlarge
AllowedValues:
- i3.xlarge
- i3.2xlarge
- i3.4xlarge
- i3.8xlarge
- i3.16xlarge
ConstraintDescription: must be a valid EC2 instance type.
AWSRegion:
Type: String
Description: Which aws region we should use
Default: us-west-2
CassandraAMI:
Type: String
Description: Cassandra AMI ID. Make sure you use the one corresponding to the
region setting
Default: ami-9fd06fe7
BencherAMI:
Type: String
Description: NDBnech AMI ID. Make sure you use the one corresponding to the region
setting
Default: ami-4aab1432
BencherInstanceType:
Description: EC2 instance type for Bencher node
Type: String
Default: c5.2xlarge
BencherFleetSize:
Type: Number
Description: Number of bencher nodes for generating load
Default: '4'
VPCSubnetId:
Type: String
Description: VPC subnet id you want to all machines located to
ResourceGroup:
Type: String
Description: Add group tag for your resources for easier to manage
Default: ig_benchmark
InstanceProfile:
Type: String
Description: Name of a precreated instance profile that has permission to ec2::Describ* and autoscalegroup::Describe*
Mappings:
Scripts:
Cassandra:
DiskMount: |
#!/bin/bash
set -x -e
mount_options=rw,noatime,attr2,inode64,allocsize=64k,noquota
disk_count=$(find /dev/ -name "nvme*n1" -type b | wc -l)
disk_list=$(find /dev/ -name "nvme*n1" -type b | xargs)
if [ $disk_count == "1" ];
then
# i3.xlarge, i3.2xlarge only have one disk, so just format and mount it
mkfs.xfs -s size=4096 $disk_list
mount -o $mount_options $disk_list /var/lib/cassandra
else
# on i3.4xlarge and above setup raid0 to max flash throughput
mdadm --create --verbose --level=0 /dev/md0 --name=DATA --raid-devices=$disk_count $disk_list
set +e
mdadm --wait /dev/md0
set -e
mkfs.xfs -s size=4096 /dev/md0
mount -o $mount_options /dev/md0 /var/lib/cassandra
fi
mkdir /var/lib/cassandra/{data,commitlog,hints,saved_caches}
chown -R cassandra:cassandra /var/lib/cassandra
SetupSchema: |
#!/bin/bash
set -x -e
/usr/bin/cqlsh -e \
"CREATE KEYSPACE IF NOT EXISTS dev1 \
WITH replication = {'class': 'NetworkTopologyStrategy', 'dc1': '1'} \
AND durable_writes = true;"
# skip C* WAL for rocksandra since rocks db already have one.
if grep -q cassandra.rocksdb /etc/cassandra/default.conf/jvm.options
then
/usr/bin/cqlsh -e "ALTER KEYSPACE dev1 with durable_writes = false;"
fi
/usr/bin/cqlsh -e \
"CREATE TABLE IF NOT EXISTS dev1.emp ( \
emp_uname text PRIMARY KEY, \
emp_dept text, \
emp_first text, \
emp_last text \
);"
ConfigLocalIp: >-
sed -i "s|__REPLACE_WITH_LOCAL_IP__|`curl -s http://169.254.169.254/latest/meta-data/local-ipv4`|g" /etc/cassandra/default.conf/cassandra.yaml
ConfigSeedIpsOnSeedItself: >-
sed -i "s|__REPLACE_WITH_SEED_IPS__|`curl -s http://169.254.169.254/latest/meta-data/local-ipv4`|g" /etc/cassandra/default.conf/cassandra.yaml
ConfigNumTokens: >-
sed -i "s|__REPLACE_WITH_NUM_TOKENS__|256|g" /etc/cassandra/default.conf/cassandra.yaml
ConfigHeapSize: |
#!/bin/bash
set -x -e
# heap_cap is max memory we can use. for rocksandar we set it to 12g since
# memtables are hold in rocksdb
if grep -q cassandra.rocksdb /etc/cassandra/default.conf/jvm.options
then
heap_cap=12288
else
heap_cap=65532
fi
# set heap size to 1/4 of system memory, cap to $heap_cap
system_memory_in_mb=`free -m | awk '/:/ {print $2;exit}'`
heap_size_in_mb=`expr $system_memory_in_mb / 4`
if [ "$heap_size_in_mb" -gt "$heap_cap" ]
then
heap_size_in_mb="$heap_cap"
fi
# write new setting to config
echo "\n" >> /etc/cassandra/default.conf/jvm.options
echo "-Xms${heap_size_in_mb}M" >> /etc/cassandra/default.conf/jvm.options
echo "-Xmx${heap_size_in_mb}M" >> /etc/cassandra/default.conf/jvm.options
Outputs:
CassandraSeedNode:
Description: Public IP of cassandra seed node
Value:
Fn::GetAtt:
- SeedNodeInstance
- PublicIp
Resources:
# setup single ec2 instance as seed node
SeedNodeInstance:
Type: AWS::EC2::Instance
Properties:
InstanceType: !Ref InstanceType
EbsOptimized: 'true'
KeyName: !Ref KeyName
ImageId: !Ref CassandraAMI
SubnetId: !Ref VPCSubnetId
Tags:
- Key: Name
Value:
Fn::Sub: >-
${AWS::StackName}/cassandra
- Key: group
Value: !Ref ResourceGroup
UserData:
Fn::Base64:
Fn::Sub: |
#! /bin/bash -v
# Helper function
function error_exit
{
/opt/aws/bin/cfn-signal -e 1 -r "$1" '${CassandraSeedWaitHandle}'
exit 1
}
# Install packages
/opt/aws/bin/cfn-init -s ${AWS::StackId} -r SeedNodeInstance --region ${AWS::Region}
# All is well so signal success
/opt/aws/bin/cfn-signal -e $? -r "SeedNodeInstance setup complete" '${CassandraSeedWaitHandle}'
Metadata:
AWS::CloudFormation::Init:
configSets:
default:
- configure_hosts
- configure_cassandra
- start_service
- setup_schema
configure_hosts:
commands:
01-create_hosts_entry-on_boot:
command: echo "`curl -s http://169.254.169.254/latest/meta-data/local-ipv4` `hostname`" >>/etc/hosts
test: test ! -f .create_hosts_entry-semaphore
02-signal_startup_complete:
command: touch .create_hosts_entry-semaphore
files:
/etc/cfn/cfn-hup.conf:
content:
Fn::Sub: |
[main]
stack=${AWS::StackId}
region=${AWS::Region}
interval=1
mode: '000400'
owner: root
group: root
/etc/cfn/hooks.d/cfn-auto-reloader.conf:
content:
Fn::Sub: |
[cfn-auto-reloader-hook]
triggers=post.update
path=Resources.SeedNodeInstance.Metadata.AWS::CloudFormation::Init
action=/opt/aws/bin/cfn-init -v -s ${AWS::StackId} --resource SeedNodeInstance --configsets default --region ${AWS::Region}
runas=root
mode: '000400'
owner: root
group: root
configure_cassandra:
commands:
01-config_local_ip:
command: !FindInMap [ Scripts, Cassandra, ConfigLocalIp ]
02-config_seed_ips:
command: !FindInMap [ Scripts, Cassandra, ConfigSeedIpsOnSeedItself ]
03-config_tokens:
command: !FindInMap [ Scripts, Cassandra, ConfigNumTokens ]
04_config_heap:
command: !FindInMap [ Scripts, Cassandra, ConfigHeapSize ]
05_mount_data_disk:
command: !FindInMap [ Scripts, Cassandra, DiskMount ]
start_service:
services:
sysvinit:
cfn-hup:
enabled: 'true'
ensureRunning: 'true'
files:
- /etc/cfn/cfn-hup.conf
- /etc/cfn/hooks.d/cfn-auto-reloader.conf
commands:
01-restart-cassandra:
command: /sbin/service cassandra restart
setup_schema:
commands:
01-wait-for-serverup:
command: /bin/sleep 120
02-schema:
command: !FindInMap [ Scripts, Cassandra, SetupSchema ]
CassandraSeedWaitHandle:
Type: AWS::CloudFormation::WaitConditionHandle
CassandraSeedWaitCondition:
Type: AWS::CloudFormation::WaitCondition
DependsOn: SeedNodeInstance
Properties:
Handle:
Ref: CassandraSeedWaitHandle
Timeout: '3600'
# setup auto scaling group for none seed nodes
CassandraNonSeedFleet:
Type: AWS::AutoScaling::AutoScalingGroup
DependsOn: CassandraSeedWaitCondition
UpdatePolicy:
AutoScalingRollingUpdate:
MaxBatchSize: '1'
MinInstancesInService: '0'
PauseTime: PT0M30S
Properties:
LaunchConfigurationName: !Ref CassandraNonSeedLaunchConfig
MinSize: !Ref NoneSeedFleetSize
MaxSize: !Ref NoneSeedFleetSize
DesiredCapacity: !Ref NoneSeedFleetSize
VPCZoneIdentifier: [ !Ref VPCSubnetId ]
Tags:
- Key: Name
Value:
Fn::Sub: >-
${AWS::StackName}/cassandra
PropagateAtLaunch: 'true'
- Key: group
Value: !Ref ResourceGroup
PropagateAtLaunch: 'true'
CassandraNonSeedLaunchConfig:
Type: AWS::AutoScaling::LaunchConfiguration
Properties:
InstanceType: !Ref InstanceType
EbsOptimized: 'true'
KeyName: !Ref KeyName
ImageId: !Ref CassandraAMI
UserData:
Fn::Base64:
Fn::Sub: |
#! /bin/bash -v
yum update -y
# Helper function
function error_exit
{
/opt/aws/bin/cfn-signal -e 1 -r "$1" '${CassandraNonSeedWaitHandle}'
exit 1
}
# Install packages
/opt/aws/bin/cfn-init -s ${AWS::StackId} -r CassandraNonSeedLaunchConfig --region ${AWS::Region}
# All is well so signal success
/opt/aws/bin/cfn-signal -e $? -r "Cassandra instance setup complete" '${CassandraNonSeedWaitHandle}'
Metadata:
AWS::CloudFormation::Init:
configSets:
default:
- configure_hosts
- configure_cassandra
- start_service
configure_hosts:
commands:
01-create_hosts_entry-on_boot:
command: echo "`curl -s http://169.254.169.254/latest/meta-data/local-ipv4` `hostname`" >>/etc/hosts
test: test ! -f .create_hosts_entry-semaphore
02-signal_startup_complete:
command: touch .create_hosts_entry-semaphore
files:
/etc/cfn/cfn-hup.conf:
content:
Fn::Sub: |
[main]
stack=${AWS::StackId}
region=${AWS::Region}
interval=1
mode: '000400'
owner: root
group: root
/etc/cfn/hooks.d/cfn-auto-reloader.conf:
content:
Fn::Sub: |
[cfn-auto-reloader-hook]
triggers=post.update
path=Resources.CassandraNonSeedLaunchConfig.Metadata.AWS::CloudFormation::Init
action=/opt/aws/bin/cfn-init -v -s ${AWS::StackId} --resource CassandraNonSeedLaunchConfig --configsets default --region ${AWS::Region}
runas=root
mode: '000400'
owner: root
group: root
configure_cassandra:
commands:
01-config_local_ip:
command: !FindInMap [ Scripts, Cassandra, ConfigLocalIp ]
02-config_seed_ips:
command:
Fn::Sub: >-
sed -i "s|__REPLACE_WITH_SEED_IPS__|${SeedNodeInstance.PrivateIp}|g" /etc/cassandra/default.conf/cassandra.yaml
03-config_tokens:
command: !FindInMap [ Scripts, Cassandra, ConfigNumTokens ]
04_config_heap:
command: !FindInMap [ Scripts, Cassandra, ConfigHeapSize ]
05_mount_data_disk:
command: !FindInMap [ Scripts, Cassandra, DiskMount ]
start_service:
services:
sysvinit:
cfn-hup:
enabled: 'true'
ensureRunning: 'true'
files:
- /etc/cfn/cfn-hup.conf
- /etc/cfn/hooks.d/cfn-auto-reloader.conf
commands:
01-restart-cassandra:
command: /sbin/service cassandra restart
CassandraNonSeedWaitHandle:
Type: AWS::CloudFormation::WaitConditionHandle
CassandraNonSeedWaitCondition:
Type: AWS::CloudFormation::WaitCondition
DependsOn: CassandraNonSeedFleet
Properties:
Handle:
Ref: CassandraNonSeedWaitHandle
Timeout: '3600'
Count: '1'
# setup auto scaling group for multiple bencher nodes with ndbench
BencherFleet:
Type: AWS::AutoScaling::AutoScalingGroup
DependsOn: CassandraNonSeedWaitCondition
UpdatePolicy:
AutoScalingRollingUpdate:
MaxBatchSize: "2"
MinInstancesInService: '0'
PauseTime: PT5S
Properties:
VPCZoneIdentifier: [ !Ref VPCSubnetId ]
LaunchConfigurationName: !Ref BencherLaunchConfig
MinSize: !Ref BencherFleetSize
MaxSize: !Ref BencherFleetSize
DesiredCapacity: !Ref BencherFleetSize
Tags:
- Key: Name
Value:
Fn::Sub: >-
${AWS::StackName}/bencher
PropagateAtLaunch: 'true'
- Key: group
Value: !Ref ResourceGroup
PropagateAtLaunch: 'true'
BencherLaunchConfig:
Type: AWS::AutoScaling::LaunchConfiguration
Properties:
InstanceType: !Ref BencherInstanceType
IamInstanceProfile: !Ref InstanceProfile
KeyName: !Ref KeyName
ImageId: !Ref BencherAMI
UserData:
Fn::Base64:
Fn::Sub: |
#! /bin/bash -v
# Helper function
function error_exit
{
/opt/aws/bin/cfn-signal -e 1 -r "$1" '${BencherWaitHandle}'
exit 1
}
# Install packages
/opt/aws/bin/cfn-init -s ${AWS::StackId} -r BencherLaunchConfig --region ${AWS::Region}
# All is well so signal success
/opt/aws/bin/cfn-signal -e $? -r "bencher setup complete. Last error: $(grep ERROR /var/log/cfn-init.log | tail -n 1)" '${BencherWaitHandle}'
Metadata:
AWS::CloudFormation::Init:
configSets:
default:
- configure_hosts
- configure_ndbench
- start_service
configure_hosts:
files:
/etc/cfn/cfn-hup.conf:
content:
Fn::Sub: |-
[main]
stack=${AWS::StackId}
region=${AWS::Region}
interval=1
mode: '000400'
owner: root
group: root
/etc/cfn/hooks.d/cfn-auto-reloader.conf:
content:
Fn::Sub: |
[cfn-auto-reloader-hook]
triggers=post.update
path=Resources.BencherLaunchConfig.Metadata.AWS::CloudFormation::Init
action=/opt/aws/bin/cfn-init -v -s ${AWS::StackId} --resource BencherLaunchConfig --configsets default --region ${AWS::Region}
runas=root
mode: '000400'
owner: root
group: root
configure_ndbench:
files:
/etc/sysconfig/tomcat8:
content:
Fn::Sub: |
export DISCOVERY_ENV=AWS_ASG
export JAVA_OPTS="$JAVA_OPTS -Dndbench.config.dataSize=2000"
export JAVA_OPTS="$JAVA_OPTS -Dndbench.config.numBackfill=64"
export JAVA_OPTS="$JAVA_OPTS -Dndbench.config.numKeys=250000000"
export JAVA_OPTS="$JAVA_OPTS -Dndbench.config.numReaders=64"
export JAVA_OPTS="$JAVA_OPTS -Dndbench.config.numValues=100"
export JAVA_OPTS="$JAVA_OPTS -Dndbench.config.numWriters=64"
export JAVA_OPTS="$JAVA_OPTS -Dndbench.config.readRateLimit=10000"
export JAVA_OPTS="$JAVA_OPTS -Dndbench.config.writeRateLimit=10000"
export JAVA_OPTS="$JAVA_OPTS -Dndbench.config.cass.cluster=benchmark"
export JAVA_OPTS="$JAVA_OPTS -Dndbench.config.cass.host=${SeedNodeInstance.PrivateIp}"
mode: '000644'
owner: root
group: root
start_service:
services:
sysvinit:
cfn-hup:
enabled: 'true'
ensureRunning: 'true'
files:
- /etc/cfn/cfn-hup.conf
- /etc/cfn/hooks.d/cfn-auto-reloader.conf
commands:
01-restart-tomcat:
command: /sbin/service tomcat8 restart
BencherWaitHandle:
Type: AWS::CloudFormation::WaitConditionHandle
BencherWaitCondition:
Type: AWS::CloudFormation::WaitCondition
DependsOn: BencherFleet
Properties:
Handle:
Ref: BencherWaitHandle
Timeout: '3600'
Count: '1'