Skip to content

Commit

Permalink
mongodb deadlock tweaks
Browse files Browse the repository at this point in the history
  • Loading branch information
alecgibson committed Jan 20, 2025
1 parent b064753 commit fcbbd3f
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 19 deletions.
26 changes: 15 additions & 11 deletions lib/db/memory.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ MemoryDB.prototype.commit = function(collection, id, op, snapshot, options, call

var transaction;
if (options.transaction) {
transaction = db._getTransaction(options.transaction);
transaction = db._transactions[options.transaction];
if (!transaction) return callback(null, false);
version = version + transaction.commits.length;
}

Expand Down Expand Up @@ -149,14 +150,23 @@ MemoryDB.prototype.query = function(collection, query, fields, options, callback
});
};

MemoryDB.prototype.startTransaction = function(id, callback) {
if (this._transactions[id]) callback(new Error('Transaction already started'));
this._transactions[id] = this._transactions[id] || {};
this._transactions[id].commits = [];
callback();
};

MemoryDB.prototype.restartTransaction = function(id, callback) {
delete this._transactions[id];
this.startTransaction(id, callback);
};

MemoryDB.prototype.commitTransaction = function(id, callback) {
var transaction = this._transactions[id];
if (!transaction) {
throw new Error('No transaction found');
}
if (!transaction) return callback();

delete this._transactions[id];
// transaction.callbacks.push(callback);

// Heavy-handed but effective approach to synchronous transaction writing:
// let's just save the DB state before and restore if the transaction fails
Expand Down Expand Up @@ -235,9 +245,3 @@ MemoryDB.prototype._getVersionSync = function(collection, id) {
var collectionOps = this.ops[collection];
return (collectionOps && collectionOps[id] && collectionOps[id].length) || 0;
};

MemoryDB.prototype._getTransaction = function(id) {
var transaction = this._transactions[id] = this._transactions[id] || {};
transaction.commits = transaction.commits || [];
return transaction;
}
25 changes: 17 additions & 8 deletions lib/transaction/pending-transaction.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ PendingTransaction.prototype.abort = function() {
};

PendingTransaction.prototype.registerSubmitRequest = function(request) {
if (!this._transaction._requests.length) {
this._transaction.backend.db.startTransaction(this._transaction.id, function(error) {
// TODO: Handle errors / wait for callback
});
}
this._transaction._requests.push(request);
this.update();
};
Expand All @@ -34,16 +39,20 @@ PendingTransaction.prototype.retry = function(callback) {
}

var transaction = this._transaction;
transaction.backend.db.abortTransaction(this._transaction.id, function(error) {
var db = transaction.backend.db;
db.abortTransaction(this._transaction.id, function(error) {
if (error) return cb(error);
var requests = transaction._requests.slice();
var retryNext = function(error) {
db.restartTransaction(transaction.id, function(error) {
if (error) return cb(error);
var request = requests.shift();
if (!request) return cb();
request.retry(retryNext);
}
retryNext();
var requests = transaction._requests.slice();
var retryNext = function(error) {
if (error) return cb(error);
var request = requests.shift();
if (!request) return cb();
request.retry(retryNext);
}
retryNext();
});
});
};

Expand Down

0 comments on commit fcbbd3f

Please sign in to comment.