Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix read capacity calculation and wait between scans #63

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
69 changes: 45 additions & 24 deletions lib/dynamo-backup.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,38 +159,59 @@ DynamoBackup.prototype._copyTable = function (tableName, itemsReceived, callback
return callback(err);
}

var readPercentage = self.readPercentage;
var limit = Math.max((data.Table.ProvisionedThroughput.ReadCapacityUnits * readPercentage) | 0, 1);
const Table = data.Table;
var maxCapacityUnits = Math.max(Math.floor(Table.ProvisionedThroughput.ReadCapacityUnits * self.readPercentage), 1);
var averageItemSize = Table.TableSizeBytes / Table.ItemCount;

self._streamItems(tableName, null, limit, itemsReceived, callback);
self._streamItems(tableName, null, 0, maxCapacityUnits, averageItemSize, itemsReceived, callback);
});
};

DynamoBackup.prototype._streamItems = function fetchItems(tableName, startKey, limit, itemsReceived, callback) {
DynamoBackup.prototype._streamItems = function fetchItems(tableName, startKey, remainingCapacityUnits, maxCapacityUnits, averageItemSize, itemsReceived, callback) {
var self = this;
var ddb = new AWS.DynamoDB();
var params = {
Limit: limit,
ReturnConsumedCapacity: 'NONE',
TableName: tableName
};
if (startKey) {
params.ExclusiveStartKey = startKey;
}
ddb.scan(params, function (err, data) {
if (err) {
return callback(err);
}
var startOfSecond = new Date().getTime();

var usableCapacity = maxCapacityUnits + remainingCapacityUnits;
if (usableCapacity > 0) {
var limit = Math.max(Math.floor(((8 * 1024) / averageItemSize) * usableCapacity), 1);
var params = {
Limit: limit,
ReturnConsumedCapacity: 'TOTAL',
TableName: tableName
};

if (data.Items.length > 0) {
itemsReceived(data.Items);
if (startKey) {
params.ExclusiveStartKey = startKey;
}

if (!data.LastEvaluatedKey || _.keys(data.LastEvaluatedKey).length === 0) {
return callback();
}
self._streamItems(tableName, data.LastEvaluatedKey, limit, itemsReceived, callback);
});
ddb.scan(params, function (error, data) {
if (error) {
callback(error);
return;
}

if (data.Items.length > 0) {
itemsReceived(data.Items);
}

if (!data.LastEvaluatedKey || _.keys(data.LastEvaluatedKey).length === 0) {
callback();
return;
}

var nextRemainingCapacity = usableCapacity - data.ConsumedCapacity.CapacityUnits;
var timeout = Math.max(1000 - (new Date().getTime() - startOfSecond), 0);
setTimeout(function () {
self._streamItems(tableName, data.LastEvaluatedKey, nextRemainingCapacity, maxCapacityUnits, averageItemSize, itemsReceived, callback);
}, timeout);
});
} else {
// Wait until we have capacity again
setTimeout(() => {
self._streamItems(tableName, startKey, usableCapacity, maxCapacityUnits, averageItemSize, itemsReceived, callback);
}, 1000);
}
};

DynamoBackup.prototype._fetchTables = function (lastTable, tables, callback) {
Expand Down Expand Up @@ -259,4 +280,4 @@ DynamoBackup.prototype._getDataPipelineAttributeValueKey = function (type) {
}
};

module.exports = DynamoBackup;
module.exports = DynamoBackup;