Skip to content

Instantly share code, notes, and snippets.

@btmills
Created March 23, 2013 23:26
Show Gist options
  • Save btmills/5229742 to your computer and use it in GitHub Desktop.
Save btmills/5229742 to your computer and use it in GitHub Desktop.
Rapidly insert a large number of records in a RethinkDB database. Noticing failure for large number of inserts.
var r = require('rethinkdb');
// Insert this many rows
// 2547 will fail on connection with
// "(libuv) Failed to create kqueue (24)"
var COUNT = 2546;
// Database to use for the test
var DB = 'test'
// Table to use for the test
var TABLE = 'insertTest';
// Connection info
var CONNINFO = {
host: 'localhost',
port: 28015
};
// Get a database connection
// Sometimes a connection attempt fails on first try. Why?
// callback is function (err, res)
var connect = function (callback, retries) {
var MAX = 3;
retries = retries ? retries : MAX;
r.connect(CONNINFO, function (err, conn) {
if (retries <= 0) {
console.error('Reached max retries. Giving up connecting.');
return callback(err, conn);
}
if (err) {
return connect(callback, retries - 1);
}
callback(null, conn);
});
};
// Insert a whole bunch
// callback is function (err, iteration)
var insert = function (count, callback) {
console.time('Insert');
for (var i = 1; i <= count; i++) {
(function (i) {
// Must use new connection due to ~250ms sequential inserts
// https://github.com/rethinkdb/rethinkdb/issues/520
connect(function (err, conn) {
if (err) {
console.error('Error connecting for insertion #%d', i, err);
return callback(err, i);
}
r.db(DB)
.table(TABLE)
.insert({ number: i })
.run(conn, function (err, res) {
conn.close();
if (err) {
console.error('Error inserting #%d', i, err);
return callback(err, i);
}
callback(null, i);
});
});
})(i);
}
}
// Insert callback maintains success and fail counts
var inserted = (function() {
var success = 0;
var fail = 0;
// callback is function (success count, fail count)
return function (err, row, callback) {
if (err) {
fail++;
} else {
success++;
}
console.log('Processed %d of %d rows', success + fail, COUNT);
if (success + fail == COUNT) {
callback (success, fail);
}
};
})();
// Everything's done. Let's show some stats.
var done = function (success, fail) {
console.timeEnd('Insert');
console.log('Attempted %d of %d inserts with %d successes and %d failures.',
success + fail, COUNT, success, fail);
}
insert(COUNT, function (err, row) {
// Don't check err here - pass it through
inserted(err, row, done);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment