Skip to content

Commit 241863e

Browse files
committed
fix double counting acks
1 parent 3881cb4 commit 241863e

File tree

4 files changed

+25
-22
lines changed

4 files changed

+25
-22
lines changed

lib/client/doc.js

+20-5
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,7 @@ Doc.prototype._opAcknowledged = function(message) {
978978
// The op was committed successfully. Increment the version number
979979
this.version++;
980980

981-
this._clearInflight();
981+
this._clearInflight(null, message.seq);
982982
};
983983

984984
Doc.prototype._rollback = function(err) {
@@ -1051,6 +1051,7 @@ Doc.prototype._hardRollback = function(err) {
10511051
// Cancel all pending ops and reset if we can't invert
10521052
this._setType(null);
10531053
this.version = null;
1054+
// TODO: Clear transaction
10541055
this.inflightOp = null;
10551056
this.pendingOps = [];
10561057

@@ -1107,15 +1108,29 @@ Doc.prototype._hasInflight = function() {
11071108
return !!(this.inflightOp || this._transaction);
11081109
};
11091110

1110-
Doc.prototype._clearInflight = function(err) {
1111+
Doc.prototype._clearInflight = function(err, seq) {
11111112
var callbacks = [];
11121113
if (this.inflightOp) callbacks = this._clearInflightOp();
1113-
else if (this._transaction) callbacks = this._clearTransaction();
1114+
else if (this._transaction) {
1115+
if (!seq) {
1116+
callbacks = this._clearTransaction();
1117+
} else {
1118+
var i = this.pendingOps.findIndex(function(pendingOp) {
1119+
return pendingOp.seq === seq;
1120+
});
1121+
if (i >= 0) {
1122+
const op = this.pendingOps.splice(i, 1)[0];
1123+
callbacks = callbacks.concat(op.callbacks);
1124+
}
1125+
}
1126+
}
11141127

11151128
var called = util.callEach(callbacks, err);
11161129

1117-
this.flush();
1118-
this._emitNothingPending();
1130+
if (!this._transaction) {
1131+
this.flush();
1132+
this._emitNothingPending();
1133+
}
11191134

11201135
if (err && !called) return this.emit('error', err);
11211136
};

lib/submit-request.js

-5
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,6 @@ SubmitRequest.prototype.submit = function(callback) {
9090
var collection = this.collection;
9191
var id = this.id;
9292
var op = this.op;
93-
console.log('submit', op);
9493
// Send a special projection so that getSnapshot knows to return all fields.
9594
// With a null projection, it strips document metadata
9695
var fields = {$submit: true};
@@ -109,7 +108,6 @@ SubmitRequest.prototype.submit = function(callback) {
109108
getSnapshot = function(collection, id, fields, snapshotOptions, callback) {
110109
transaction.getSnapshotAndOps(request, function(error, snapshot, ops) {
111110
if (!snapshot) return getSnapshotFromDb(collection, id, fields, snapshotOptions, callback);
112-
console.log('>> got ops', request.op, ops);
113111
var type = snapshot.type;
114112
// TODO: Use this._transformOp()? Get a version mismatch, and don't want ops on this.ops though
115113
for (var op of ops) {
@@ -122,7 +120,6 @@ SubmitRequest.prototype.submit = function(callback) {
122120
}
123121

124122
getSnapshot(collection, id, fields, snapshotOptions, function(err, snapshot) {
125-
console.log('got snapshot', request.op, snapshot);
126123
if (err) return callback(err);
127124

128125
request.snapshot = snapshot;
@@ -170,7 +167,6 @@ SubmitRequest.prototype.submit = function(callback) {
170167

171168
// Transform the op up to the current snapshot version, then apply
172169
var from = op.v;
173-
console.log('get ops to snapshot', from, snapshot.v);
174170
backend.db.getOpsToSnapshot(collection, id, from, snapshot, {metadata: true}, function(err, ops) {
175171
if (err) return callback(err);
176172

@@ -298,7 +294,6 @@ SubmitRequest.prototype.retry = function(callback) {
298294
};
299295

300296
SubmitRequest.prototype._transformOp = function(ops) {
301-
console.log('transform op', this.op, ops);
302297
var type = this.snapshot.type;
303298
for (var i = 0; i < ops.length; i++) {
304299
var op = ops[i];

lib/transaction/transaction.js

+4-11
Original file line numberDiff line numberDiff line change
@@ -34,12 +34,8 @@ Transaction.prototype.submit = function(callback) {
3434
// TODO: Handle multiple calls?
3535
this._callback = callback;
3636

37-
for (var collection in this._docOps) {
38-
for (var id in this._docOps[collection]) {
39-
for (var op of this._docOps[collection][id]) {
40-
this._submitOp(op);
41-
}
42-
}
37+
for (var op of this._ops) {
38+
this._submitOp(op);
4339
}
4440
};
4541

@@ -51,7 +47,6 @@ Transaction.prototype.ready = function(request, callback) {
5147
docRequests.push(request);
5248
this._requestCallbacks.push(callback);
5349

54-
console.log('> REQ READY', request.op, request.snapshot);
5550
this.emit('requestReady', request);
5651

5752
if (this._isReady()) return this._commitTransaction();
@@ -64,19 +59,18 @@ Transaction.prototype.getSnapshotAndOps = function(request, callback) {
6459
this._waitForPreviousOpRequest(op, function(req) {
6560
if (!req) return callback();
6661
var versionDiff = req.snapshot.v - request.op.v;
67-
var ops = req.ops.slice(-versionDiff);
62+
var ops = util.clone(req.ops.slice(-versionDiff));
6863
if (ops.length) {
6964
var offset = request.op.v - ops[0].v;
7065
for (var op of ops) {
7166
op.v = op.v + offset;
7267
}
7368
}
74-
callback(null, util.clone(req.snapshot), util.clone(ops));
69+
callback(null, util.clone(req.snapshot), ops);
7570
});
7671
};
7772

7873
Transaction.prototype._waitForPreviousOpRequest = function(op, callback) {
79-
console.log('wait for previous', op);
8074
var collection = op.c;
8175
var id = op.d;
8276

@@ -87,7 +81,6 @@ Transaction.prototype._waitForPreviousOpRequest = function(op, callback) {
8781
previousOp = docOp;
8882
}
8983

90-
console.log('previous op', previousOp);
9184
if (!previousOp) return callback();
9285

9386
var requests = util.dig(this._readyRequests, collection, id) || [];

test/client/transaction.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ module.exports = function() {
164164
], done);
165165
});
166166

167-
it.only('transaction is behind remote', function(done) {
167+
it('transaction is behind remote', function(done) {
168168
async.series([
169169
doc.create.bind(doc, {tricks: ['fetch']}),
170170
remoteDoc.fetch.bind(remoteDoc),

0 commit comments

Comments
 (0)