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

add onDemand capacity to backup and restore functions #92

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
35 changes: 19 additions & 16 deletions lib/dynamo-backup.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ function DynamoBackup(options) {
this.awsSecretKey = options.awsSecretKey;
this.awsRegion = options.awsRegion;
this.debug = Boolean(options.debug);
this.onDemandReadCapacity = options.onDemandReadCapacity || 25;

if (this.awsRegion) {
params.region = this.awsRegion;
Expand Down Expand Up @@ -54,21 +55,21 @@ DynamoBackup.prototype.backupTable = function (tableName, backupPath, callback)
backupPath = self._getBackupPath();
}

var params = {};
if (self.awsRegion) {
params.region = self.awsRegion;
}
if (self.awsAccessKey && self.awsSecretKey) {
var params = {};
if (self.awsRegion) {
params.region = self.awsRegion;
}
if (self.awsAccessKey && self.awsSecretKey) {
params.accessKey = self.awsAccessKey;
params.secretKey = self.awsSecretKey;
}

params.bucket = self.bucket;
params.objectName = path.join(backupPath, tableName + '.json');
params.stream = stream;
params.debug = self.debug;
params.bucket = self.bucket;
params.objectName = path.join(backupPath, tableName + '.json');
params.stream = stream;
params.debug = self.debug;

var upload = new Uploader(params);
var upload = new Uploader(params);

var startTime = moment.utc();
self.emit('start-backup', tableName, startTime);
Expand All @@ -87,7 +88,7 @@ DynamoBackup.prototype.backupTable = function (tableName, backupPath, callback)

self._copyTable(
tableName,
function (items) {
function (items) { //itemsReceived
items.forEach(function (item) {
if (self.base64Binary) {
_.each(item, function (value, key) {
Expand All @@ -114,7 +115,7 @@ DynamoBackup.prototype.backupTable = function (tableName, backupPath, callback)
});
}
}
);
);
};

DynamoBackup.prototype.backupAllTables = function (callback) {
Expand All @@ -141,7 +142,7 @@ DynamoBackup.prototype.backupAllTables = function (callback) {
})
},
callback
);
);
});
};

Expand All @@ -161,7 +162,9 @@ DynamoBackup.prototype._copyTable = function (tableName, itemsReceived, callback

var readPercentage = self.readPercentage;
var limit = Math.max((data.Table.ProvisionedThroughput.ReadCapacityUnits * readPercentage) | 0, 1);

if (data.Table.BillingModeSummary.BillingMode === 'PAY_PER_REQUEST') {
limit = self.onDemandReadCapacity;
}
self._streamItems(tableName, null, limit, itemsReceived, callback);
});
};
Expand Down Expand Up @@ -229,8 +232,8 @@ DynamoBackup.prototype._formatForDataPipeline = function (item) {
value[dataPipelineValueKey] = v;
value[k] = undefined;
// for MAps and Lists, recurse until the elements are created with the correct case
if(k === 'M' || k === 'L') {
self._formatForDataPipeline(v);
if (k === 'M' || k === 'L') {
self._formatForDataPipeline(v);
}
});
});
Expand Down
16 changes: 11 additions & 5 deletions lib/dynamo-restore.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ function DynamoRestore(options) {
options.awsKey = options.awsKey || process.env.AWS_ACCESS_KEY_ID;
options.awsSecret = options.awsSecret || process.env.AWS_SECRET_ACCESS_KEY;
options.awsRegion = options.awsRegion || process.env.AWS_DEFAULT_REGION || 'ap-southeast-2';
options.onDemandTable = options.onDemandTable || false;

AWS.config.update({
accessKeyId: options.awsKey,
Expand Down Expand Up @@ -244,12 +245,17 @@ DynamoRestore.prototype._createTable = function(callback) {
KeySchema: [{
AttributeName: options.partitionkey,
KeyType: 'HASH'
}],
ProvisionedThroughput: {
}],
};
if (options.onDemandTable) {
params['BillingMode'] = 'PAY_PER_REQUEST';
} else {
params['ProvisionedThroughput'] = {
ReadCapacityUnits: options.readcapacity,
WriteCapacityUnits: options.concurrency // Need this high for pumping data, but will reduce it later.
}
};
}

if (options.sortkey) {
params.AttributeDefinitions.push({
AttributeName: options.sortkey,
Expand Down Expand Up @@ -291,8 +297,8 @@ DynamoRestore.prototype._sendBatch = function() {
// Prepare
var params = { RequestItems: {} },
dynamo = this.dynamodb,
options = this.options;
batch = this.batches.shift();
options = this.options,
batch = this.batches.shift();
params.RequestItems[options.table] = batch.items;

// Send
Expand Down