Proxy ranges between doc updater and docstore

This commit is contained in:
James Allen 2016-12-08 14:09:06 +00:00
parent a5d2183441
commit 1830d41eba
8 changed files with 36 additions and 24 deletions

View file

@ -45,13 +45,13 @@ module.exports = DocstoreManager =
return callback(error) if error? return callback(error) if error?
if 200 <= res.statusCode < 300 if 200 <= res.statusCode < 300
logger.log doc_id: doc_id, project_id: project_id, version: doc.version, rev: doc.rev, "got doc from docstore api" logger.log doc_id: doc_id, project_id: project_id, version: doc.version, rev: doc.rev, "got doc from docstore api"
callback(null, doc.lines, doc.rev, doc.version) callback(null, doc.lines, doc.rev, doc.version, doc.ranges)
else else
error = new Error("docstore api responded with non-success code: #{res.statusCode}") error = new Error("docstore api responded with non-success code: #{res.statusCode}")
logger.error err: error, project_id: project_id, doc_id: doc_id, "error getting doc from docstore" logger.error err: error, project_id: project_id, doc_id: doc_id, "error getting doc from docstore"
callback(error) callback(error)
updateDoc: (project_id, doc_id, lines, version, callback = (error, modified, rev) ->) -> updateDoc: (project_id, doc_id, lines, version, ranges, callback = (error, modified, rev) ->) ->
logger.log project_id: project_id, doc_id: doc_id, "updating doc in docstore api" logger.log project_id: project_id, doc_id: doc_id, "updating doc in docstore api"
url = "#{settings.apis.docstore.url}/project/#{project_id}/doc/#{doc_id}" url = "#{settings.apis.docstore.url}/project/#{project_id}/doc/#{doc_id}"
request.post { request.post {
@ -59,6 +59,7 @@ module.exports = DocstoreManager =
json: json:
lines: lines lines: lines
version: version version: version
ranges: ranges
}, (error, res, result) -> }, (error, res, result) ->
return callback(error) if error? return callback(error) if error?
if 200 <= res.statusCode < 300 if 200 <= res.statusCode < 300

View file

@ -95,7 +95,7 @@ module.exports = DocumentUpdaterHandler =
logger.error err: error, project_id: project_id, doc_id: doc_id, "document updater returned failure status code: #{res.statusCode}" logger.error err: error, project_id: project_id, doc_id: doc_id, "document updater returned failure status code: #{res.statusCode}"
return callback(error) return callback(error)
getDocument: (project_id, doc_id, fromVersion, callback = (error, exists, doclines, version) ->) -> getDocument: (project_id, doc_id, fromVersion, callback = (error, doclines, version, ranges, ops) ->) ->
timer = new metrics.Timer("get-document") timer = new metrics.Timer("get-document")
url = "#{settings.apis.documentupdater.url}/project/#{project_id}/doc/#{doc_id}?fromVersion=#{fromVersion}" url = "#{settings.apis.documentupdater.url}/project/#{project_id}/doc/#{doc_id}?fromVersion=#{fromVersion}"
logger.log project_id:project_id, doc_id: doc_id, "getting doc from document updater" logger.log project_id:project_id, doc_id: doc_id, "getting doc from document updater"
@ -110,7 +110,7 @@ module.exports = DocumentUpdaterHandler =
body = JSON.parse(body) body = JSON.parse(body)
catch error catch error
return callback(error) return callback(error)
callback null, body.lines, body.version, body.ops callback null, body.lines, body.version, body.ranges, body.ops
else else
logger.error project_id:project_id, doc_id:doc_id, url: url, "doc updater returned a non-success status code: #{res.statusCode}" logger.error project_id:project_id, doc_id:doc_id, url: url, "doc updater returned a non-success status code: #{res.statusCode}"
callback new Error("doc updater returned a non-success status code: #{res.statusCode}") callback new Error("doc updater returned a non-success status code: #{res.statusCode}")

View file

@ -7,7 +7,7 @@ module.exports =
doc_id = req.params.doc_id doc_id = req.params.doc_id
plain = req?.query?.plain == 'true' plain = req?.query?.plain == 'true'
logger.log doc_id:doc_id, project_id:project_id, "receiving get document request from api (docupdater)" logger.log doc_id:doc_id, project_id:project_id, "receiving get document request from api (docupdater)"
ProjectEntityHandler.getDoc project_id, doc_id, (error, lines, rev, version) -> ProjectEntityHandler.getDoc project_id, doc_id, (error, lines, rev, version, ranges) ->
if error? if error?
logger.err err:error, doc_id:doc_id, project_id:project_id, "error finding element for getDocument" logger.err err:error, doc_id:doc_id, project_id:project_id, "error finding element for getDocument"
return next(error) return next(error)
@ -19,14 +19,15 @@ module.exports =
res.send JSON.stringify { res.send JSON.stringify {
lines: lines lines: lines
version: version version: version
ranges: ranges
} }
setDocument: (req, res, next = (error) ->) -> setDocument: (req, res, next = (error) ->) ->
project_id = req.params.Project_id project_id = req.params.Project_id
doc_id = req.params.doc_id doc_id = req.params.doc_id
{lines, version} = req.body {lines, version, ranges} = req.body
logger.log doc_id:doc_id, project_id:project_id, "receiving set document request from api (docupdater)" logger.log doc_id:doc_id, project_id:project_id, "receiving set document request from api (docupdater)"
ProjectEntityHandler.updateDocLines project_id, doc_id, lines, version, (error) -> ProjectEntityHandler.updateDocLines project_id, doc_id, lines, version, ranges, (error) ->
if error? if error?
logger.err err:error, doc_id:doc_id, project_id:project_id, "error finding element for getDocument" logger.err err:error, doc_id:doc_id, project_id:project_id, "error finding element for getDocument"
return next(error) return next(error)

View file

@ -126,7 +126,7 @@ module.exports = ProjectEntityHandler =
doc = new Doc name: docName doc = new Doc name: docName
# Put doc in docstore first, so that if it errors, we don't have a doc_id in the project # Put doc in docstore first, so that if it errors, we don't have a doc_id in the project
# which hasn't been created in docstore. # which hasn't been created in docstore.
DocstoreManager.updateDoc project_id.toString(), doc._id.toString(), docLines, 0, (err, modified, rev) -> DocstoreManager.updateDoc project_id.toString(), doc._id.toString(), docLines, 0, {}, (err, modified, rev) ->
return callback(err) if err? return callback(err) if err?
ProjectEntityHandler._putElement project, folder_id, doc, "doc", (err, result)=> ProjectEntityHandler._putElement project, folder_id, doc, "doc", (err, result)=>
@ -292,7 +292,7 @@ module.exports = ProjectEntityHandler =
return callback(err) return callback(err)
callback(err, folder, parentFolder_id) callback(err, folder, parentFolder_id)
updateDocLines : (project_id, doc_id, lines, version, callback = (error) ->)-> updateDocLines : (project_id, doc_id, lines, version, ranges, callback = (error) ->)->
ProjectGetter.getProjectWithoutDocLines project_id, (err, project)-> ProjectGetter.getProjectWithoutDocLines project_id, (err, project)->
return callback(err) if err? return callback(err) if err?
return callback(new Errors.NotFoundError("project not found")) if !project? return callback(new Errors.NotFoundError("project not found")) if !project?
@ -307,7 +307,7 @@ module.exports = ProjectEntityHandler =
return callback(error) return callback(error)
logger.log project_id: project_id, doc_id: doc_id, "telling docstore manager to update doc" logger.log project_id: project_id, doc_id: doc_id, "telling docstore manager to update doc"
DocstoreManager.updateDoc project_id, doc_id, lines, version, (err, modified, rev) -> DocstoreManager.updateDoc project_id, doc_id, lines, version, ranges, (err, modified, rev) ->
if err? if err?
logger.error err: err, doc_id: doc_id, project_id:project_id, lines: lines, "error sending doc to docstore" logger.error err: err, doc_id: doc_id, project_id:project_id, lines: lines, "error sending doc to docstore"
return callback(err) return callback(err)

View file

@ -57,12 +57,13 @@ describe "DocstoreManager", ->
@lines = ["mock", "doc", "lines"] @lines = ["mock", "doc", "lines"]
@rev = 5 @rev = 5
@version = 42 @version = 42
@ranges = { "mock": "ranges" }
@modified = true @modified = true
describe "with a successful response code", -> describe "with a successful response code", ->
beforeEach -> beforeEach ->
@request.post = sinon.stub().callsArgWith(1, null, statusCode: 204, { modified: @modified, rev: @rev }) @request.post = sinon.stub().callsArgWith(1, null, statusCode: 204, { modified: @modified, rev: @rev })
@DocstoreManager.updateDoc @project_id, @doc_id, @lines, @version, @callback @DocstoreManager.updateDoc @project_id, @doc_id, @lines, @version, @ranges, @callback
it "should update the doc in the docstore api", -> it "should update the doc in the docstore api", ->
@request.post @request.post
@ -71,6 +72,7 @@ describe "DocstoreManager", ->
json: json:
lines: @lines lines: @lines
version: @version version: @version
ranges: @ranges
}) })
.should.equal true .should.equal true
@ -80,7 +82,7 @@ describe "DocstoreManager", ->
describe "with a failed response code", -> describe "with a failed response code", ->
beforeEach -> beforeEach ->
@request.post = sinon.stub().callsArgWith(1, null, statusCode: 500, "") @request.post = sinon.stub().callsArgWith(1, null, statusCode: 500, "")
@DocstoreManager.updateDoc @project_id, @doc_id, @lines, @version, @callback @DocstoreManager.updateDoc @project_id, @doc_id, @lines, @version, @ranges, @callback
it "should call the callback with an error", -> it "should call the callback with an error", ->
@callback.calledWith(new Error("docstore api responded with non-success code: 500")).should.equal true @callback.calledWith(new Error("docstore api responded with non-success code: 500")).should.equal true
@ -100,6 +102,7 @@ describe "DocstoreManager", ->
lines: @lines = ["mock", "doc", "lines"] lines: @lines = ["mock", "doc", "lines"]
rev: @rev = 5 rev: @rev = 5
version: @version = 42 version: @version = 42
ranges: @ranges = { "mock": "ranges" }
describe "with a successful response code", -> describe "with a successful response code", ->
beforeEach -> beforeEach ->
@ -115,7 +118,7 @@ describe "DocstoreManager", ->
.should.equal true .should.equal true
it "should call the callback with the lines, version and rev", -> it "should call the callback with the lines, version and rev", ->
@callback.calledWith(null, @lines, @rev, @version).should.equal true @callback.calledWith(null, @lines, @rev, @version, @ranges).should.equal true
describe "with a failed response code", -> describe "with a failed response code", ->
beforeEach -> beforeEach ->
@ -148,7 +151,7 @@ describe "DocstoreManager", ->
.should.equal true .should.equal true
it "should call the callback with the lines, version and rev", -> it "should call the callback with the lines, version and rev", ->
@callback.calledWith(null, @lines, @rev, @version).should.equal true @callback.calledWith(null, @lines, @rev, @version, @ranges).should.equal true
describe "getAllDocs", -> describe "getAllDocs", ->
describe "with a successful response code", -> describe "with a successful response code", ->

View file

@ -267,6 +267,7 @@ describe 'DocumentUpdaterHandler - Flushing documents :', ->
lines: @lines lines: @lines
version: @version version: @version
ops: @ops = ["mock-op-1", "mock-op-2"] ops: @ops = ["mock-op-1", "mock-op-2"]
ranges: @ranges = {"mock":"ranges"}
@fromVersion = 2 @fromVersion = 2
@request.get = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body) @request.get = sinon.stub().callsArgWith(1, null, {statusCode: 200}, @body)
@handler.getDocument @project_id, @doc_id, @fromVersion, @callback @handler.getDocument @project_id, @doc_id, @fromVersion, @callback
@ -276,7 +277,7 @@ describe 'DocumentUpdaterHandler - Flushing documents :', ->
@request.get.calledWith(url).should.equal true @request.get.calledWith(url).should.equal true
it "should call the callback with the lines and version", -> it "should call the callback with the lines and version", ->
@callback.calledWith(null, @lines, @version, @ops).should.equal true @callback.calledWith(null, @lines, @version, @ranges, @ops).should.equal true
describe "when the document updater API returns an error", -> describe "when the document updater API returns an error", ->
beforeEach -> beforeEach ->

View file

@ -23,6 +23,7 @@ describe "DocumentController", ->
@doc_id = "doc-id-123" @doc_id = "doc-id-123"
@doc_lines = ["one", "two", "three"] @doc_lines = ["one", "two", "three"]
@version = 42 @version = 42
@ranges = {"mock": "ranges"}
@rev = 5 @rev = 5
describe "getDocument", -> describe "getDocument", ->
@ -33,7 +34,7 @@ describe "DocumentController", ->
describe "when the document exists", -> describe "when the document exists", ->
beforeEach -> beforeEach ->
@ProjectEntityHandler.getDoc = sinon.stub().callsArgWith(2, null, @doc_lines, @rev, @version) @ProjectEntityHandler.getDoc = sinon.stub().callsArgWith(2, null, @doc_lines, @rev, @version, @ranges)
@DocumentController.getDocument(@req, @res, @next) @DocumentController.getDocument(@req, @res, @next)
it "should get the document from Mongo", -> it "should get the document from Mongo", ->
@ -46,6 +47,7 @@ describe "DocumentController", ->
@res.body.should.equal JSON.stringify @res.body.should.equal JSON.stringify
lines: @doc_lines lines: @doc_lines
version: @version version: @version
ranges: @ranges
describe "when the document doesn't exist", -> describe "when the document doesn't exist", ->
beforeEach -> beforeEach ->
@ -68,11 +70,12 @@ describe "DocumentController", ->
@req.body = @req.body =
lines: @doc_lines lines: @doc_lines
version: @version version: @version
ranges: @ranges
@DocumentController.setDocument(@req, @res, @next) @DocumentController.setDocument(@req, @res, @next)
it "should update the document in Mongo", -> it "should update the document in Mongo", ->
@ProjectEntityHandler.updateDocLines @ProjectEntityHandler.updateDocLines
.calledWith(@project_id, @doc_id, @doc_lines, @version) .calledWith(@project_id, @doc_id, @doc_lines, @version, @ranges)
.should.equal true .should.equal true
it "should return a successful response", -> it "should return a successful response", ->

View file

@ -382,7 +382,9 @@ describe 'ProjectEntityHandler', ->
beforeEach -> beforeEach ->
@lines = ["mock", "doc", "lines"] @lines = ["mock", "doc", "lines"]
@rev = 5 @rev = 5
@DocstoreManager.getDoc = sinon.stub().callsArgWith(3, null, @lines, @rev) @version = 42
@ranges = {"mock": "ranges"}
@DocstoreManager.getDoc = sinon.stub().callsArgWith(3, null, @lines, @rev, @version, @ranges)
@ProjectEntityHandler.getDoc project_id, doc_id, @callback @ProjectEntityHandler.getDoc project_id, doc_id, @callback
it "should call the docstore", -> it "should call the docstore", ->
@ -391,7 +393,7 @@ describe 'ProjectEntityHandler', ->
.should.equal true .should.equal true
it "should call the callback with the lines, version and rev", -> it "should call the callback with the lines, version and rev", ->
@callback.calledWith(null, @lines, @rev).should.equal true @callback.calledWith(null, @lines, @rev, @version, @ranges).should.equal true
describe 'addDoc', -> describe 'addDoc', ->
beforeEach -> beforeEach ->
@ -590,6 +592,7 @@ describe 'ProjectEntityHandler', ->
_id: doc_id _id: doc_id
} }
@version = 42 @version = 42
@ranges = {"mock":"ranges"}
@ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, @project) @ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, @project)
@projectLocator.findElement = sinon.stub().callsArgWith(1, null, @doc, {fileSystem: @path}) @projectLocator.findElement = sinon.stub().callsArgWith(1, null, @doc, {fileSystem: @path})
@tpdsUpdateSender.addDoc = sinon.stub().callsArg(1) @tpdsUpdateSender.addDoc = sinon.stub().callsArg(1)
@ -599,7 +602,7 @@ describe 'ProjectEntityHandler', ->
describe "when the doc has been modified", -> describe "when the doc has been modified", ->
beforeEach -> beforeEach ->
@DocstoreManager.updateDoc = sinon.stub().yields(null, true, @rev = 5) @DocstoreManager.updateDoc = sinon.stub().yields(null, true, @rev = 5)
@ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @version, @callback @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @version, @ranges, @callback
it "should get the project without doc lines", -> it "should get the project without doc lines", ->
@ProjectGetter.getProjectWithoutDocLines @ProjectGetter.getProjectWithoutDocLines
@ -617,7 +620,7 @@ describe 'ProjectEntityHandler', ->
it "should update the doc in the docstore", -> it "should update the doc in the docstore", ->
@DocstoreManager.updateDoc @DocstoreManager.updateDoc
.calledWith(project_id, doc_id, @lines, @version) .calledWith(project_id, doc_id, @lines, @version, @ranges)
.should.equal true .should.equal true
it "should mark the project as updated", -> it "should mark the project as updated", ->
@ -642,7 +645,7 @@ describe 'ProjectEntityHandler', ->
describe "when the doc has not been modified", -> describe "when the doc has not been modified", ->
beforeEach -> beforeEach ->
@DocstoreManager.updateDoc = sinon.stub().yields(null, false, @rev = 5) @DocstoreManager.updateDoc = sinon.stub().yields(null, false, @rev = 5)
@ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @version, @callback @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @version, @ranges, @callback
it "should not mark the project as updated", -> it "should not mark the project as updated", ->
@projectUpdater.markAsUpdated.called.should.equal false @projectUpdater.markAsUpdated.called.should.equal false
@ -656,7 +659,7 @@ describe 'ProjectEntityHandler', ->
describe "when the project is not found", -> describe "when the project is not found", ->
beforeEach -> beforeEach ->
@ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, null) @ProjectGetter.getProjectWithoutDocLines = sinon.stub().callsArgWith(1, null, null)
@ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @version, @callback @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @ranges, @version, @callback
it "should return a not found error", -> it "should return a not found error", ->
@callback.calledWith(new Errors.NotFoundError()).should.equal true @callback.calledWith(new Errors.NotFoundError()).should.equal true
@ -664,7 +667,7 @@ describe 'ProjectEntityHandler', ->
describe "when the doc is not found", -> describe "when the doc is not found", ->
beforeEach -> beforeEach ->
@projectLocator.findElement = sinon.stub().callsArgWith(1, null, null, null) @projectLocator.findElement = sinon.stub().callsArgWith(1, null, null, null)
@ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @version, @callback @ProjectEntityHandler.updateDocLines project_id, doc_id, @lines, @ranges, @version, @callback
it "should log out the error", -> it "should log out the error", ->
@logger.error @logger.error