Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save khamusa/74516887e04c70c3b11a to your computer and use it in GitHub Desktop.
Save khamusa/74516887e04c70c3b11a to your computer and use it in GitHub Desktop.
In-Progress MongoDB Index Creation Remaining Time Estimation
// Quick, dirty, but useful.
// This script will estimate the rime remaining for any index building operations
// currently in course.
// You can either call it by passing a known index building operation opid to the
// estimate function or call estimateIndexOps() with no arguments, that will
// find all index building operations and estimate each one of them.
//
// Usage:
// estimateIndexOps();
// estimate(opid);
var estimate = function(opid) {
var op = db.currentOp( { opid: opid })['inprog'][0];
var time_taken = op["secs_running"];
var message = op["msg"];
var state = message.match(/(\d+)\/(\d+)/);
var completed = state[1];
var total = state[2];
var completed_per_second = completed / time_taken;
var time_to_complete_all = total / completed_per_second;
var remaining_time = time_to_complete_all - time_taken;
print("-> " + message);
print(" Namespace: " + op["insert"]["ns"]);
print(" Index name: " + op["insert"]["name"] );
print(" " + remaining_time.toFixed(0) + " remaining seconds, or")
print(" " + remaining_time / (60*60) + ' hours, or');
print(" " + remaining_time / 60 + ' minutes');
return remaining_time;
}
function estimateIndexOps() {
var active_index = db.currentOp(
{
$or: [
{ op: "query", "query.createIndexes": { $exists: true } },
{ op: "insert", ns: /\.system\.indexes\b/ }
]
}
);
active_index["inprog"].forEach( function(op) {
print("In progress index operation found: " + op['opid']);
estimate(op['opid']);
print("");
});
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment