Merge pull request #4342 from overleaf/jpa-fix-clsi-server-id-handling

[misc] get current clsi server id directly from compile response

GitOrigin-RevId: 403e7ca35270a8937a0066fe2c8daf52ed71531d
This commit is contained in:
Jakob Ackermann 2021-07-26 15:13:03 +02:00 committed by Copybot
parent b03a4cf7fc
commit 63fb7a022a
3 changed files with 135 additions and 115 deletions

View file

@ -99,7 +99,7 @@ module.exports = function (backendGroup) {
return rclient.expire(
this.buildKey(project_id),
Settings.clsiCookie.ttl,
callback
err => callback(err, undefined)
)
}
if (rclient_secondary != null) {
@ -134,10 +134,10 @@ module.exports = function (backendGroup) {
getCookieJar(project_id, callback) {
if (callback == null) {
callback = function (err, jar) {}
callback = function (err, jar, clsiServerId) {}
}
if (!clsiCookiesEnabled) {
return callback(null, request.jar())
return callback(null, request.jar(), undefined)
}
return this._getServerId(project_id, (err, serverId) => {
if (err != null) {
@ -151,7 +151,7 @@ module.exports = function (backendGroup) {
)
const jar = request.jar()
jar.setCookie(serverCookie, Settings.apis.clsi.url)
return callback(null, jar)
return callback(null, jar, serverId)
})
},
}

View file

@ -237,7 +237,7 @@ const ClsiManager = {
userId,
req,
options.compileGroup,
(err, response) => {
(err, response, clsiServerId) => {
if (err != null) {
return callback(
OError.tag(err, 'error sending request to clsi', {
@ -246,12 +246,6 @@ const ClsiManager = {
})
)
}
ClsiCookieManager._getServerId(projectId, (err, clsiServerId) => {
if (err != null) {
return callback(
OError.tag(err, 'error getting server id', { projectId })
)
}
const outputFiles = ClsiManager._parseOutputFiles(
projectId,
response && response.compile && response.compile.outputFiles
@ -270,7 +264,6 @@ const ClsiManager = {
stats,
timings
)
})
}
)
}
@ -299,7 +292,9 @@ const ClsiManager = {
{
currentBackend(cb) {
const startTime = new Date()
ClsiCookieManager.getCookieJar(projectId, (err, jar) => {
ClsiCookieManager.getCookieJar(
projectId,
(err, jar, clsiServerId) => {
if (err != null) {
return callback(
OError.tag(err, 'error getting cookie jar for CLSI request', {
@ -312,30 +307,44 @@ const ClsiManager = {
request(opts, (err, response, body) => {
if (err != null) {
return callback(
OError.tag(err, 'error making request to CLSI', { projectId })
OError.tag(err, 'error making request to CLSI', {
projectId,
})
)
}
timer.done()
Metrics.inc(
`compile.currentBackend.response.${response.statusCode}`
)
ClsiCookieManager.setServerId(projectId, response, err => {
ClsiCookieManager.setServerId(
projectId,
response,
(err, newClsiServerId) => {
if (err != null) {
callback(
OError.tag(err, 'error setting server id', { projectId })
OError.tag(err, 'error setting server id', {
projectId,
})
)
} else {
// return as soon as the standard compile has returned
callback(null, response, body)
callback(
null,
response,
body,
newClsiServerId || clsiServerId
)
}
cb(err, {
response,
body,
finishTime: new Date() - startTime,
})
}
)
})
})
})
}
)
},
newBackend(cb) {
const startTime = new Date()
@ -460,7 +469,10 @@ const ClsiManager = {
json: req,
method: 'POST',
}
ClsiManager._makeRequest(projectId, opts, (err, response, body) => {
ClsiManager._makeRequest(
projectId,
opts,
(err, response, body, clsiServerId) => {
if (err != null) {
return callback(
new OError('failed to make request to CLSI', {
@ -472,7 +484,7 @@ const ClsiManager = {
)
}
if (response.statusCode >= 200 && response.statusCode < 300) {
callback(null, body)
callback(null, body, clsiServerId)
} else if (response.statusCode === 413) {
callback(null, { compile: { status: 'project-too-large' } })
} else if (response.statusCode === 409) {
@ -483,17 +495,21 @@ const ClsiManager = {
callback(null, { compile: { status: 'unavailable' } })
} else {
callback(
new OError(`CLSI returned non-success code: ${response.statusCode}`, {
new OError(
`CLSI returned non-success code: ${response.statusCode}`,
{
projectId,
userId,
compileOptions: req.compile.options,
rootResourcePath: req.compile.rootResourcePath,
clsiResponse: body,
statusCode: response.statusCode,
})
}
)
)
}
})
}
)
},
_parseOutputFiles(projectId, rawOutputFiles = []) {

View file

@ -148,7 +148,9 @@ describe('ClsiManager', function () {
describe('with ranges on the pdf and stats/timings details', function () {
beforeEach(function () {
this.ClsiManager._postToClsi = sinon.stub().yields(null, {
this.ClsiManager._postToClsi = sinon.stub().yields(
null,
{
compile: {
status: 'success',
stats: { fooStat: 1 },
@ -171,7 +173,9 @@ describe('ClsiManager', function () {
},
],
},
})
},
'clsi-server-id-43'
)
this.ClsiCookieManager._getServerId.yields(null, 'clsi-server-id-42')
this.ClsiManager.sendRequest(
this.project_id,
@ -208,7 +212,7 @@ describe('ClsiManager', function () {
null,
'success',
outputFiles,
'clsi-server-id-42',
'clsi-server-id-43',
validationError,
{ fooStat: 1 },
{ barTiming: 2 }