/
gsi.py
724 lines (637 loc) · 29 KB
/
gsi.py
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
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
# -*- coding: utf-8 -*-
""" Core components """
from boto.exception import JSONResponseError, BotoServerError
from dynamic_dynamodb import calculators
from dynamic_dynamodb.aws import dynamodb, sns
from dynamic_dynamodb.core import circuit_breaker
from dynamic_dynamodb.statistics import gsi as gsi_stats
from dynamic_dynamodb.log_handler import LOGGER as logger
from dynamic_dynamodb.config_handler import get_global_option, get_gsi_option
def ensure_provisioning(
table_name, table_key, gsi_name, gsi_key,
num_consec_read_checks, num_consec_write_checks):
""" Ensure that provisioning is correct for Global Secondary Indexes
:type table_name: str
:param table_name: Name of the DynamoDB table
:type table_key: str
:param table_key: Table configuration option key name
:type gsi_name: str
:param gsi_name: Name of the GSI
:type gsi_key: str
:param gsi_key: GSI configuration option key name
:type num_consec_read_checks: int
:param num_consec_read_checks: How many consecutive checks have we had
:type num_consec_write_checks: int
:param num_consec_write_checks: How many consecutive checks have we had
:returns: (int, int) -- num_consec_read_checks, num_consec_write_checks
"""
if get_global_option('circuit_breaker_url'):
if circuit_breaker.is_open():
logger.warning('Circuit breaker is OPEN!')
return (0, 0)
logger.info(
'{0} - Will ensure provisioning for global secondary index {1}'.format(
table_name, gsi_name))
# Handle throughput alarm checks
__ensure_provisioning_alarm(table_name, table_key, gsi_name, gsi_key)
try:
read_update_needed, updated_read_units, num_consec_read_checks = \
__ensure_provisioning_reads(
table_name,
table_key,
gsi_name,
gsi_key,
num_consec_read_checks)
write_update_needed, updated_write_units, num_consec_write_checks = \
__ensure_provisioning_writes(
table_name,
table_key,
gsi_name,
gsi_key,
num_consec_write_checks)
if read_update_needed:
num_consec_read_checks = 0
if write_update_needed:
num_consec_write_checks = 0
# Handle throughput updates
if read_update_needed or write_update_needed:
logger.info(
'{0} - GSI: {1} - Changing provisioning to {2:d} '
'read units and {3:d} write units'.format(
table_name,
gsi_name,
int(updated_read_units),
int(updated_write_units)))
__update_throughput(
table_name,
table_key,
gsi_name,
gsi_key,
updated_read_units,
updated_write_units)
else:
logger.info(
'{0} - GSI: {1} - No need to change provisioning'.format(
table_name,
gsi_name))
except JSONResponseError:
raise
except BotoServerError:
raise
return num_consec_read_checks, num_consec_write_checks
def __calculate_always_decrease_rw_values(
table_name, gsi_name, read_units, provisioned_reads,
write_units, provisioned_writes):
""" Calculate values for always-decrease-rw-together
This will only return reads and writes decreases if both reads and writes
are lower than the current provisioning
:type table_name: str
:param table_name: Name of the DynamoDB table
:type gsi_name: str
:param gsi_name: Name of the GSI
:type read_units: int
:param read_units: New read unit provisioning
:type provisioned_reads: int
:param provisioned_reads: Currently provisioned reads
:type write_units: int
:param write_units: New write unit provisioning
:type provisioned_writes: int
:param provisioned_writes: Currently provisioned writes
:returns: (int, int) -- (reads, writes)
"""
if read_units < provisioned_reads and write_units < provisioned_writes:
return (read_units, write_units)
if read_units < provisioned_reads:
logger.info(
'{0} - GSI: {1} - Reads could be decreased, '
'but we are waiting for writes to get lower than the threshold '
'before scaling down'.format(table_name, gsi_name))
read_units = provisioned_reads
elif write_units < provisioned_writes:
logger.info(
'{0} - GSI: {1} - Writes could be decreased, '
'but we are waiting for reads to get lower than the threshold '
'before scaling down'.format(table_name, gsi_name))
write_units = provisioned_writes
return (read_units, write_units)
def __ensure_provisioning_reads(
table_name, table_key, gsi_name, gsi_key, num_consec_read_checks):
""" Ensure that provisioning is correct
:type table_name: str
:param table_name: Name of the DynamoDB table
:type table_key: str
:param table_key: Table configuration option key name
:type gsi_name: str
:param gsi_name: Name of the GSI
:type gsi_key: str
:param gsi_key: Configuration option key name
:type num_consec_read_checks: int
:param num_consec_read_checks: How many consecutive checks have we had
:returns: (bool, int, int)
update_needed, updated_read_units, num_consec_read_checks
"""
if not get_gsi_option(table_key, gsi_key, 'enable_reads_autoscaling'):
logger.info(
'{0} - GSI: {1} - '
'Autoscaling of reads has been disabled'.format(
table_name, gsi_name))
return False, dynamodb.get_provisioned_gsi_read_units(
table_name, gsi_name), 0
update_needed = False
try:
lookback_window_start = get_gsi_option(
table_key, gsi_key, 'lookback-window-start')
current_read_units = dynamodb.get_provisioned_gsi_read_units(
table_name, gsi_name)
consumed_read_units_percent = \
gsi_stats.get_consumed_read_units_percent(
table_name, gsi_name, lookback_window_start)
throttled_read_count = \
gsi_stats.get_throttled_read_event_count(
table_name, gsi_name, lookback_window_start)
reads_upper_threshold = \
get_gsi_option(table_key, gsi_key, 'reads_upper_threshold')
reads_lower_threshold = \
get_gsi_option(table_key, gsi_key, 'reads_lower_threshold')
increase_reads_unit = \
get_gsi_option(table_key, gsi_key, 'increase_reads_unit')
decrease_reads_unit = \
get_gsi_option(table_key, gsi_key, 'decrease_reads_unit')
increase_reads_with = \
get_gsi_option(table_key, gsi_key, 'increase_reads_with')
decrease_reads_with = \
get_gsi_option(table_key, gsi_key, 'decrease_reads_with')
throttled_reads_upper_threshold = \
get_gsi_option(
table_key, gsi_key, 'throttled_reads_upper_threshold')
max_provisioned_reads = \
get_gsi_option(table_key, gsi_key, 'max_provisioned_reads')
num_read_checks_before_scale_down = \
get_gsi_option(
table_key, gsi_key, 'num_read_checks_before_scale_down')
num_read_checks_reset_percent = \
get_gsi_option(table_key, gsi_key, 'num_read_checks_reset_percent')
except JSONResponseError:
raise
except BotoServerError:
raise
# Set the updated units to the current read unit value
updated_read_units = current_read_units
# Reset consecutive reads if num_read_checks_reset_percent is reached
if num_read_checks_reset_percent:
if consumed_read_units_percent >= num_read_checks_reset_percent:
logger.info(
'{0} - GSI: {1} - Resetting the number of consecutive '
'read checks. Reason: Consumed percent {2} is '
'greater than reset percent: {3}'.format(
table_name,
gsi_name,
consumed_read_units_percent,
num_read_checks_reset_percent))
num_consec_read_checks = 0
if (consumed_read_units_percent == 0 and not
get_gsi_option(
table_key,
gsi_key,
'allow_scaling_down_reads_on_0_percent')):
logger.info(
'{0} - GSI: {1} - '
'Scaling down reads is not done when usage is at 0%'.format(
table_name, gsi_name))
# Increase needed due to high CU consumption
elif consumed_read_units_percent >= reads_upper_threshold:
# Exit if up scaling has been disabled
if not get_gsi_option(table_key, gsi_key, 'enable_reads_up_scaling'):
logger.debug(
'{0} - GSI: {1} - Up scaling event detected. '
'No action taken as scaling '
'up reads has been disabled in the configuration'.format(
table_name, gsi_name))
else:
if increase_reads_unit == 'percent':
calculated_provisioning = calculators.increase_reads_in_percent(
current_read_units,
increase_reads_with,
get_gsi_option(table_key, gsi_key, 'max_provisioned_reads'),
'{0} - GSI: {1}'.format(table_name, gsi_name))
else:
calculated_provisioning = calculators.increase_reads_in_units(
current_read_units,
increase_reads_with,
get_gsi_option(table_key, gsi_key, 'max_provisioned_reads'),
'{0} - GSI: {1}'.format(table_name, gsi_name))
if current_read_units != calculated_provisioning:
logger.info(
'{0} - Resetting the number of consecutive '
'read checks. Reason: scale up event detected'.format(
table_name))
num_consec_read_checks = 0
update_needed = True
updated_read_units = calculated_provisioning
# Decrease needed due to low CU consumption
elif consumed_read_units_percent <= reads_lower_threshold:
# Exit if down scaling has been disabled
if not get_gsi_option(table_key, gsi_key, 'enable_reads_down_scaling'):
logger.debug(
'{0} - GSI: {1} - Down scaling event detected. '
'No action taken as scaling '
'down reads has been disabled in the configuration'.format(
table_name, gsi_name))
else:
if decrease_reads_unit == 'percent':
calculated_provisioning = calculators.decrease_reads_in_percent(
current_read_units,
decrease_reads_with,
get_gsi_option(table_key, gsi_key, 'min_provisioned_reads'),
'{0} - GSI: {1}'.format(table_name, gsi_name))
else:
calculated_provisioning = calculators.decrease_reads_in_units(
current_read_units,
decrease_reads_with,
get_gsi_option(table_key, gsi_key, 'min_provisioned_reads'),
'{0} - GSI: {1}'.format(table_name, gsi_name))
if current_read_units != calculated_provisioning:
# We need to look at how many times the num_consec_read_checks
# integer has incremented and Compare to config file value
num_consec_read_checks = num_consec_read_checks + 1
if num_consec_read_checks >= num_read_checks_before_scale_down:
update_needed = True
updated_read_units = calculated_provisioning
# Increase needed due to high throttling
elif throttled_read_count > throttled_reads_upper_threshold:
if throttled_reads_upper_threshold > 0:
if increase_reads_unit == 'percent':
calculated_provisioning = calculators.increase_reads_in_percent(
current_read_units,
increase_reads_with,
get_gsi_option(table_key, gsi_key, 'max_provisioned_reads'),
'{0} - GSI: {1}'.format(table_name, gsi_name))
else:
calculated_provisioning = calculators.increase_reads_in_units(
current_read_units,
increase_reads_with,
get_gsi_option(table_key, gsi_key, 'max_provisioned_reads'),
'{0} - GSI: {1}'.format(table_name, gsi_name))
if current_read_units != calculated_provisioning:
logger.info(
'{0} - GSI: {1} - Resetting the number of consecutive '
'read checks. Reason: scale up event detected'.format(
table_name, gsi_name))
num_consec_read_checks = 0
update_needed = True
updated_read_units = calculated_provisioning
# Never go over the configured max provisioning
if max_provisioned_reads:
if int(updated_read_units) > int(max_provisioned_reads):
update_needed = True
updated_read_units = int(max_provisioned_reads)
logger.info(
'Will not increase writes over gsi-max-provisioned-reads '
'limit ({0} writes)'.format(updated_read_units))
logger.info('{0} - GSI: {1} - Consecutive read checks {2}/{3}'.format(
table_name,
gsi_name,
num_consec_read_checks,
num_read_checks_before_scale_down))
return update_needed, updated_read_units, num_consec_read_checks
def __ensure_provisioning_writes(
table_name, table_key, gsi_name, gsi_key, num_consec_write_checks):
""" Ensure that provisioning of writes is correct
:type table_name: str
:param table_name: Name of the DynamoDB table
:type table_key: str
:param table_key: Table configuration option key name
:type gsi_name: str
:param gsi_name: Name of the GSI
:type gsi_key: str
:param gsi_key: Configuration option key name
:type num_consec_write_checks: int
:param num_consec_write_checks: How many consecutive checks have we had
:returns: (bool, int, int)
update_needed, updated_write_units, num_consec_write_checks
"""
if not get_gsi_option(table_key, gsi_key, 'enable_writes_autoscaling'):
logger.info(
'{0} - GSI: {1} - '
'Autoscaling of writes has been disabled'.format(
table_name, gsi_name))
return False, dynamodb.get_provisioned_gsi_write_units(
table_name, gsi_name), 0
update_needed = False
try:
lookback_window_start = get_gsi_option(
table_key, gsi_key, 'lookback-window-start')
current_write_units = dynamodb.get_provisioned_gsi_write_units(
table_name, gsi_name)
consumed_write_units_percent = \
gsi_stats.get_consumed_write_units_percent(
table_name, gsi_name, lookback_window_start)
throttled_write_count = \
gsi_stats.get_throttled_write_event_count(
table_name, gsi_name, lookback_window_start)
writes_upper_threshold = \
get_gsi_option(table_key, gsi_key, 'writes_upper_threshold')
writes_lower_threshold = \
get_gsi_option(table_key, gsi_key, 'writes_lower_threshold')
throttled_writes_upper_threshold = \
get_gsi_option(
table_key, gsi_key, 'throttled_writes_upper_threshold')
increase_writes_unit = \
get_gsi_option(table_key, gsi_key, 'increase_writes_unit')
increase_writes_with = \
get_gsi_option(table_key, gsi_key, 'increase_writes_with')
decrease_writes_unit = \
get_gsi_option(table_key, gsi_key, 'decrease_writes_unit')
decrease_writes_with = \
get_gsi_option(table_key, gsi_key, 'decrease_writes_with')
max_provisioned_writes = \
get_gsi_option(table_key, gsi_key, 'max_provisioned_writes')
num_write_checks_before_scale_down = \
get_gsi_option(
table_key, gsi_key, 'num_write_checks_before_scale_down')
num_write_checks_reset_percent = \
get_gsi_option(table_key, gsi_key, 'num_write_checks_reset_percent')
except JSONResponseError:
raise
except BotoServerError:
raise
# Set the updated units to the current write unit value
updated_write_units = current_write_units
# Reset write consecutive count if num_write_checks_reset_percent is reached
if num_write_checks_reset_percent:
if consumed_write_units_percent >= num_write_checks_reset_percent:
logger.info(
'{0} - GSI: {1} - Resetting the number of consecutive '
'write checks. Reason: Consumed percent {2} is '
'greater than reset percent: {3}'.format(
table_name,
gsi_name,
consumed_write_units_percent,
num_write_checks_reset_percent))
num_consec_write_checks = 0
# Check if we should update write provisioning
if (consumed_write_units_percent == 0 and not get_gsi_option(
table_key, gsi_key, 'allow_scaling_down_writes_on_0_percent')):
logger.info(
'{0} - GSI: {1} - '
'Scaling down writes is not done when usage is at 0%'.format(
table_name, gsi_name))
# Increase needed due to high CU consumption
elif consumed_write_units_percent >= writes_upper_threshold:
# Exit if up scaling has been disabled
if not get_gsi_option(table_key, gsi_key, 'enable_writes_up_scaling'):
logger.debug(
'{0} - GSI: {1} - Up scaling event detected. '
'No action taken as scaling '
'up writes has been disabled in the configuration'.format(
table_name, gsi_name))
else:
if increase_writes_unit == 'percent':
calculated_provisioning = \
calculators.increase_writes_in_percent(
current_write_units,
increase_writes_with,
get_gsi_option(
table_key, gsi_key, 'max_provisioned_reads'),
'{0} - GSI: {1}'.format(table_name, gsi_name))
else:
calculated_provisioning = calculators.increase_writes_in_units(
current_write_units,
increase_writes_with,
get_gsi_option(table_key, gsi_key, 'max_provisioned_reads'),
'{0} - GSI: {1}'.format(table_name, gsi_name))
if current_write_units != calculated_provisioning:
logger.info(
'{0} - GSI: {1} - Resetting the number of consecutive '
'write checks. Reason: scale up event detected'.format(
table_name, gsi_name))
num_consec_write_checks = 0
update_needed = True
updated_write_units = calculated_provisioning
# Decrease needed due to low CU consumption
elif consumed_write_units_percent <= writes_lower_threshold:
# Exit if down scaling has been disabled
if not get_gsi_option(table_key, gsi_key, 'enable_writes_down_scaling'):
logger.debug(
'{0} - GSI: {1} - Down scaling event detected. '
'No action taken as scaling '
'down writes has been disabled in the configuration'.format(
table_name, gsi_name))
else:
if decrease_writes_unit == 'percent':
calculated_provisioning = \
calculators.decrease_writes_in_percent(
current_write_units,
decrease_writes_with,
get_gsi_option(
table_key, gsi_key, 'min_provisioned_writes'),
'{0} - GSI: {1}'.format(table_name, gsi_name))
else:
calculated_provisioning = calculators.decrease_writes_in_units(
current_write_units,
decrease_writes_with,
get_gsi_option(table_key, gsi_key, 'min_provisioned_reads'),
'{0} - GSI: {1}'.format(table_name, gsi_name))
if current_write_units != calculated_provisioning:
num_consec_write_checks = num_consec_write_checks + 1
if (num_consec_write_checks >=
num_write_checks_before_scale_down):
update_needed = True
updated_write_units = calculated_provisioning
# Increase needed due to high throttling
elif throttled_write_count > throttled_writes_upper_threshold:
if throttled_writes_upper_threshold > 0:
if increase_writes_unit == 'percent':
calculated_provisioning = \
calculators.increase_writes_in_percent(
current_write_units,
increase_writes_with,
get_gsi_option(
table_key, gsi_key, 'max_provisioned_reads'),
'{0} - GSI: {1}'.format(table_name, gsi_name))
else:
calculated_provisioning = calculators.increase_writes_in_units(
current_write_units,
increase_writes_with,
get_gsi_option(table_key, gsi_key, 'max_provisioned_reads'),
'{0} - GSI: {1}'.format(table_name, gsi_name))
if current_write_units != calculated_provisioning:
logger.info(
'{0} - GSI: {1} - Resetting the number of consecutive '
'write checks. Reason: scale up event detected'.format(
table_name, gsi_name))
num_consec_write_checks = 0
update_needed = True
updated_write_units = calculated_provisioning
# Never go over the configured max provisioning
if max_provisioned_writes:
if int(updated_write_units) > int(max_provisioned_writes):
update_needed = True
updated_write_units = int(max_provisioned_writes)
logger.info(
'{0} - GSI: {1} - '
'Will not increase writes over gsi-max-provisioned-writes '
'limit ({2} writes)'.format(
table_name,
gsi_name,
updated_write_units))
logger.info('{0} - GSI: {1} - Consecutive write checks {2}/{3}'.format(
table_name,
gsi_name,
num_consec_write_checks,
num_write_checks_before_scale_down))
return update_needed, updated_write_units, num_consec_write_checks
def __update_throughput(
table_name, table_key, gsi_name, gsi_key, read_units, write_units):
""" Update throughput on the GSI
:type table_name: str
:param table_name: Name of the DynamoDB table
:type table_key: str
:param table_key: Table configuration option key name
:type gsi_name: str
:param gsi_name: Name of the GSI
:type gsi_key: str
:param gsi_key: Configuration option key name
:type read_units: int
:param read_units: New read unit provisioning
:type write_units: int
:param write_units: New write unit provisioning
"""
try:
current_ru = dynamodb.get_provisioned_gsi_read_units(
table_name, gsi_name)
current_wu = dynamodb.get_provisioned_gsi_write_units(
table_name, gsi_name)
except JSONResponseError:
raise
# Check table status
try:
gsi_status = dynamodb.get_gsi_status(table_name, gsi_name)
except JSONResponseError:
raise
logger.debug('{0} - GSI: {1} - GSI status is {2}'.format(
table_name, gsi_name, gsi_status))
if gsi_status != 'ACTIVE':
logger.warning(
'{0} - GSI: {1} - Not performing throughput changes when GSI '
'status is {2}'.format(table_name, gsi_name, gsi_status))
return
# If this setting is True, we will only scale down when
# BOTH reads AND writes are low
if get_gsi_option(table_key, gsi_key, 'always_decrease_rw_together'):
read_units, write_units = __calculate_always_decrease_rw_values(
table_name,
gsi_name,
read_units,
current_ru,
write_units,
current_wu)
if read_units == current_ru and write_units == current_wu:
logger.info('{0} - GSI: {1} - No changes to perform'.format(
table_name, gsi_name))
return
dynamodb.update_gsi_provisioning(
table_name,
table_key,
gsi_name,
gsi_key,
int(read_units),
int(write_units))
def __ensure_provisioning_alarm(table_name, table_key, gsi_name, gsi_key):
""" Ensure that provisioning alarm threshold is not exceeded
:type table_name: str
:param table_name: Name of the DynamoDB table
:type table_key: str
:param table_key: Table configuration option key name
:type gsi_name: str
:param gsi_name: Name of the GSI
:type gsi_key: str
:param gsi_key: Configuration option key name
"""
consumed_read_units_percent = \
gsi_stats.get_consumed_read_units_percent(table_name, gsi_name)
consumed_write_units_percent = \
gsi_stats.get_consumed_write_units_percent(table_name, gsi_name)
reads_upper_alarm_threshold = \
get_gsi_option(table_key, gsi_key, 'reads-upper-alarm-threshold')
reads_lower_alarm_threshold = \
get_gsi_option(table_key, gsi_key, 'reads-lower-alarm-threshold')
writes_upper_alarm_threshold = \
get_gsi_option(table_key, gsi_key, 'writes-upper-alarm-threshold')
writes_lower_alarm_threshold = \
get_gsi_option(table_key, gsi_key, 'writes-lower-alarm-threshold')
# Check upper alarm thresholds
upper_alert_triggered = False
upper_alert_message = []
if (reads_upper_alarm_threshold > 0 and
consumed_read_units_percent >= reads_upper_alarm_threshold):
upper_alert_triggered = True
upper_alert_message.append(
'{0} - GSI: {1} - Consumed Read Capacity {2:d}% '
'was greater than or equal to the upper alarm '
'threshold {3:d}%\n'.format(
table_name,
gsi_name,
consumed_read_units_percent,
reads_upper_alarm_threshold))
if (writes_upper_alarm_threshold > 0 and
consumed_write_units_percent >= writes_upper_alarm_threshold):
upper_alert_triggered = True
upper_alert_message.append(
'{0} - GSI: {1} - Consumed Write Capacity {2:d}% '
'was greater than or equal to the upper alarm '
'threshold {3:d}%\n'.format(
table_name,
gsi_name,
consumed_write_units_percent,
writes_upper_alarm_threshold))
# Check lower alarm thresholds
lower_alert_triggered = False
lower_alert_message = []
if (reads_lower_alarm_threshold > 0 and
consumed_read_units_percent < reads_lower_alarm_threshold):
lower_alert_triggered = True
lower_alert_message.append(
'{0} - GSI: {1} - Consumed Read Capacity {2:d}% '
'was below the lower alarm threshold {3:d}%\n'.format(
table_name,
gsi_name,
consumed_read_units_percent,
reads_lower_alarm_threshold))
if (writes_lower_alarm_threshold > 0 and
consumed_write_units_percent < writes_lower_alarm_threshold):
lower_alert_triggered = True
lower_alert_message.append(
'{0} - GSI: {1} - Consumed Write Capacity {2:d}% '
'was below the lower alarm threshold {3:d}%\n'.format(
table_name,
gsi_name,
consumed_write_units_percent,
writes_lower_alarm_threshold))
# Send alert if needed
if upper_alert_triggered:
logger.info(
'{0} - GSI: {1} - Will send high provisioning alert'.format(
table_name, gsi_name))
sns.publish_gsi_notification(
table_key,
gsi_key,
''.join(upper_alert_message),
['high-throughput-alarm'],
subject='ALARM: High Throughput for Table {0} - GSI: {1}'.format(
table_name, gsi_name))
elif lower_alert_triggered:
logger.info(
'{0} - GSI: {1} - Will send low provisioning alert'.format(
table_name, gsi_name))
sns.publish_gsi_notification(
table_key,
gsi_key,
''.join(lower_alert_message),
['low-throughput-alarm'],
subject='ALARM: Low Throughput for Table {0} - GSI: {1}'.format(
table_name, gsi_name))
else:
logger.debug(
'{0} - GSI: {1} - Throughput alarm thresholds not crossed'.format(
table_name, gsi_name))