return error when out-of-order ops detected

This commit is contained in:
Brian Gough 2017-03-30 11:48:26 +01:00
parent 70bb95fb36
commit d29141d583
2 changed files with 23 additions and 0 deletions

View file

@ -16,6 +16,15 @@ module.exports = UpdatesManager =
if length == 0
return callback()
# check that ops are in the correct order
for op, i in rawUpdates when i > 0
thisVersion = op?.v
prevVersion = rawUpdates[i-1]?.v
if not (prevVersion < thisVersion)
logger.error project_id: project_id, doc_id: doc_id, rawUpdates:rawUpdates, temporary: temporary, thisVersion:thisVersion, prevVersion:prevVersion, "op versions out of order"
# TODO try to recover by sorting the ops
return callback(new Error("incoming op versions out of order"))
# FIXME: we no longer need the lastCompressedUpdate, so change functions not to need it
# CORRECTION: we do use it to log the time in case of error
MongoManager.peekLastCompressedUpdate doc_id, (error, lastCompressedUpdate, lastVersion) ->

View file

@ -137,6 +137,20 @@ describe "UpdatesManager", ->
it "should not insert any update into mongo", ->
@PackManager.insertCompressedUpdates.called.should.equal false
describe "when the raw ops are out of order", ->
beforeEach ->
@rawUpdates = [{ v: 13, op: "mock-op-13" }, { v: 12, op: "mock-op-12" }]
@UpdatesManager.compressAndSaveRawUpdates @project_id, @doc_id, @rawUpdates, @temporary, @callback
it "should call the callback with an error", ->
@callback
.calledWith(new Error("incoming op versions out of order"))
.should.equal true
it "should not insert any update into mongo", ->
@PackManager.insertCompressedUpdates.called.should.equal false
describe "when the raw ops need appending to existing history which is in S3", ->
beforeEach ->
@lastCompressedUpdate = null