Skip to content
This repository was archived by the owner on Dec 1, 2022. It is now read-only.

Commit

Permalink
Merge pull request #29 from tylfin/throttleMetric
Browse files Browse the repository at this point in the history
Add throttle metrics to describeTableConsumedCapacityAsync
  • Loading branch information
tmitchel2 authored Nov 7, 2016
2 parents 6665b89 + e025d1d commit f4a6da2
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 2 deletions.
65 changes: 65 additions & 0 deletions src/capacity/CapacityCalculatorBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,21 +54,42 @@ export default class CapacityCalculatorBase {
let gsiWrites = (params.GlobalSecondaryIndexes || [])
.map(gsi => this.getConsumedCapacityAsync(false, params.TableName, gsi.IndexName));

let tableTRead = this.getThrottledEventsAsync(true, params.TableName, null)
let tableTWrites = this.getThrottledEventsAsync(false, params.TableName, null)

let gsiTReads = (params.GlobalSecondaryIndexes || [])
.map(gsi => this.getThrottledEventsAsync(true, params.TableName, gsi.IndexName));

let gsiTWrites = (params.GlobalSecondaryIndexes || [])
.map(gsi => this.getThrottledEventsAsync(false, params.TableName, gsi.IndexName));

// Await on the results
let tableConsumedRead = await tableRead;
let tableConsumedWrite = await tableWrite;
let gsiConsumedReads = await Promise.all(gsiReads);
let gsiConsumedWrites = await Promise.all(gsiWrites);

// Await on throttled info
let tableThrottledRead = await tableTRead;
let tableThrottledWrite = await tableTWrites;
let gsiThrottledReads = await Promise.all(gsiTReads);
let gsiThrottledWrites = await Promise.all(gsiTWrites);

// Format results
let gsis = gsiConsumedReads.map((read, i) => {
let write = gsiConsumedWrites[i];
let throttledWrite = gsiThrottledWrites[i];
let throttledRead = gsiThrottledReads[i]
return {
// $FlowIgnore: The indexName is not null in this case
IndexName: read.globalSecondaryIndexName,
ConsumedThroughput: {
ReadCapacityUnits: read.value,
WriteCapacityUnits: write.value
},
ThrottledEvents: {
ThrottledReadEvents: throttledRead,
ThrottledWriteEvents: throttledRead
}
};
});
Expand All @@ -79,6 +100,10 @@ export default class CapacityCalculatorBase {
ReadCapacityUnits: tableConsumedRead.value,
WriteCapacityUnits: tableConsumedWrite.value
},
ThrottledEvents: {
ThrottledReadEvents: tableThrottledRead,
ThrottledWriteEvents: tableThrottledWrite
},
GlobalSecondaryIndexes: gsis
};
} catch (ex) {
Expand Down Expand Up @@ -138,6 +163,46 @@ export default class CapacityCalculatorBase {
}
}

async getThrottledEventsAsync(
isRead: boolean, tableName: string, globalSecondaryIndexName: ?string):
Promise<number> {
try {
invariant(isRead != null, 'Parameter \'isRead\' is not set');
invariant(tableName != null, 'Parameter \'tableName\' is not set');

let settings = this.getStatisticSettings();

let EndTime = new Date();
let StartTime = new Date();
StartTime.setTime(EndTime - (60000 * settings.spanMinutes * settings.count));
let MetricName = isRead ? 'ReadThrottleEvents' : 'WriteThrottleEvents';
let Dimensions = this.getDimensions(tableName, globalSecondaryIndexName);
let period = (settings.spanMinutes * 60);
let params = {
Namespace: 'AWS/DynamoDB',
MetricName,
Dimensions,
StartTime,
EndTime,
Period: period,
Statistics: [ settings.type ],
Unit: 'Count'
};

let statistics = await this.cw.getMetricStatisticsAsync(params);
let value = this.getProjectedValue(settings, statistics);

return value;
} catch (ex) {
warning(JSON.stringify({
class: 'CapacityCalculator',
function: 'getThrottledEventsAsync',
isRead, tableName, globalSecondaryIndexName,
}, null, json.padding));
throw ex;
}
}

getDimensions(tableName: string, globalSecondaryIndexName: ?string): Dimension[] {
if (globalSecondaryIndexName) {
return [
Expand Down
7 changes: 7 additions & 0 deletions src/flow/FlowTypes.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
/* @flow */
import type { ProvisionedThroughput, Throughput } from 'aws-sdk-promise';

export type ThrottledEventsDescription = {
ThrottledReadEvents: number,
ThrottledWriteEvents: number
}

export type TableProvisionedAndConsumedThroughput = {
TableName: string,
IndexName?: string,
ProvisionedThroughput: ProvisionedThroughput,
ConsumedThroughput: Throughput,
ThrottledEvents: ThrottledEventsDescription
};

export type GlobalSecondaryIndexConsumedThroughput = {
Expand All @@ -17,6 +23,7 @@ export type TableConsumedCapacityDescription = {
TableName: string,
ConsumedThroughput: Throughput,
GlobalSecondaryIndexes: GlobalSecondaryIndexConsumedThroughput[],
ThrottledEvents: ThrottledEventsDescription
};

export type ConsumedCapacityDesc = {
Expand Down
6 changes: 4 additions & 2 deletions src/provisioning/ProvisionerConfigurableBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,8 @@ export default class ProvisionerConfigurableBase extends ProvisionerBase {
let tableData = {
TableName: tableDescription.TableName,
ProvisionedThroughput: tableDescription.ProvisionedThroughput,
ConsumedThroughput: tableConsumedCapacityDescription.ConsumedThroughput
ConsumedThroughput: tableConsumedCapacityDescription.ConsumedThroughput,
ThrottledEvents: tableConsumedCapacityDescription.ThrottledEvents
};

let provisionedThroughput = this.getUpdatedProvisionedThroughput(tableData);
Expand Down Expand Up @@ -177,7 +178,8 @@ export default class ProvisionerConfigurableBase extends ProvisionerBase {
TableName: tableDescription.TableName,
IndexName: gsicc.IndexName,
ProvisionedThroughput: gsi.ProvisionedThroughput,
ConsumedThroughput: gsicc.ConsumedThroughput
ConsumedThroughput: gsicc.ConsumedThroughput,
ThrottledEvents: gsicc.ThrottledEvents
});

// eslint-disable-next-line eqeqeq
Expand Down

0 comments on commit f4a6da2

Please sign in to comment.