Merge pull request #13510 from overleaf/em-promisify-clsi-manager

Clean up of ClsiManager and ClsiCookieManager

GitOrigin-RevId: e5047b253613e87fd6cb4f12855b821028fcaf8e
This commit is contained in:
Eric Mc Sween 2023-06-26 13:42:11 +01:00 committed by Copybot
parent 8cdfffa6f9
commit 1fc13b8cbf
5 changed files with 1723 additions and 2088 deletions

View file

@ -1,32 +1,19 @@
/* eslint-disable
n/handle-callback-err,
max-len,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* DS207: Consider shorter variations of null checks
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
let rclientSecondary
const { URL, URLSearchParams } = require('url')
const OError = require('@overleaf/o-error')
const Settings = require('@overleaf/settings')
const request = require('request').defaults({ timeout: 30 * 1000 })
const RedisWrapper = require('../../infrastructure/RedisWrapper')
const rclient = RedisWrapper.client('clsi_cookie')
if (Settings.redis.clsi_cookie_secondary != null) {
rclientSecondary = RedisWrapper.client('clsi_cookie_secondary')
}
const Cookie = require('cookie')
const logger = require('@overleaf/logger')
const Metrics = require('@overleaf/metrics')
const clsiCookiesEnabled =
(Settings.clsiCookie != null ? Settings.clsiCookie.key : undefined) != null &&
Settings.clsiCookie.key.length !== 0
const clsiCookiesEnabled = (Settings.clsiCookie?.key ?? '') !== ''
const rclient = RedisWrapper.client('clsi_cookie')
let rclientSecondary
if (Settings.redis.clsi_cookie_secondary != null) {
rclientSecondary = RedisWrapper.client('clsi_cookie_secondary')
}
module.exports = function (backendGroup) {
return {
@ -45,15 +32,12 @@ module.exports = function (backendGroup) {
compileBackendClass,
callback
) {
if (callback == null) {
callback = function () {}
}
return rclient.get(this.buildKey(projectId, userId), (err, serverId) => {
if (err != null) {
rclient.get(this.buildKey(projectId, userId), (err, serverId) => {
if (err) {
return callback(err)
}
if (serverId == null || serverId === '') {
return this._populateServerIdViaRequest(
this._populateServerIdViaRequest(
projectId,
userId,
compileGroup,
@ -61,7 +45,7 @@ module.exports = function (backendGroup) {
callback
)
} else {
return callback(null, serverId)
callback(null, serverId)
}
})
},
@ -73,16 +57,13 @@ module.exports = function (backendGroup) {
compileBackendClass,
callback
) {
if (callback == null) {
callback = function () {}
}
const u = new URL(`${Settings.apis.clsi.url}/project/${projectId}/status`)
u.search = new URLSearchParams({
compileGroup,
compileBackendClass,
}).toString()
request.post(u.href, (err, res, body) => {
if (err != null) {
if (err) {
OError.tag(err, 'error getting initial server id for project', {
project_id: projectId,
})
@ -96,25 +77,21 @@ module.exports = function (backendGroup) {
res,
null,
function (err, serverId) {
if (err != null) {
if (err) {
logger.warn(
{ err, projectId },
'error setting server id via populate request'
)
}
return callback(err, serverId)
callback(err, serverId)
}
)
})
},
_parseServerIdFromResponse(response) {
const cookies = Cookie.parse(
(response.headers['set-cookie'] != null
? response.headers['set-cookie'][0]
: undefined) || ''
)
return cookies != null ? cookies[Settings.clsiCookie.key] : undefined
const cookies = Cookie.parse(response.headers['set-cookie']?.[0] || '')
return cookies?.[Settings.clsiCookie.key]
},
checkIsLoadSheddingEvent(clsiserverid, compileGroup, compileBackendClass) {
@ -155,9 +132,6 @@ module.exports = function (backendGroup) {
previous,
callback
) {
if (callback == null) {
callback = function () {}
}
if (!clsiCookiesEnabled) {
return callback()
}
@ -195,9 +169,6 @@ module.exports = function (backendGroup) {
},
_setServerIdInRedis(rclient, projectId, userId, serverId, callback) {
if (callback == null) {
callback = function () {}
}
rclient.setex(
this.buildKey(projectId, userId),
this._getTTLInSeconds(serverId),
@ -207,13 +178,10 @@ module.exports = function (backendGroup) {
},
clearServerId(projectId, userId, callback) {
if (callback == null) {
callback = function () {}
}
if (!clsiCookiesEnabled) {
return callback()
}
return rclient.del(this.buildKey(projectId, userId), callback)
rclient.del(this.buildKey(projectId, userId), callback)
},
getCookieJar(
@ -223,13 +191,10 @@ module.exports = function (backendGroup) {
compileBackendClass,
callback
) {
if (callback == null) {
callback = function () {}
}
if (!clsiCookiesEnabled) {
return callback(null, request.jar(), undefined)
}
return this._getServerId(
this._getServerId(
projectId,
userId,
compileGroup,
@ -246,7 +211,7 @@ module.exports = function (backendGroup) {
)
const jar = request.jar()
jar.setCookie(serverCookie, Settings.apis.clsi.url)
return callback(null, jar, serverId)
callback(null, jar, serverId)
}
)
},

File diff suppressed because it is too large Load diff

View file

@ -1272,7 +1272,7 @@ function initialize(webRouter, privateApiRouter, publicApiRouter) {
const sendRes = _.once(function (statusCode, message) {
res.status(statusCode)
plainTextResponse(res, message)
ClsiCookieManager.clearServerId(projectId)
ClsiCookieManager.clearServerId(projectId, () => {})
}) // force every compile to a new server
// set a timeout
let handler = setTimeout(function () {

View file

@ -1,16 +1,3 @@
/* eslint-disable
n/handle-callback-err,
max-len,
no-return-assign,
no-unused-vars,
*/
// TODO: This file was created by bulk-decaffeinate.
// Fix any style issues and re-enable lint.
/*
* decaffeinate suggestions:
* DS102: Remove unnecessary code created because of implicit returns
* Full docs: https://github.com/decaffeinate/decaffeinate/blob/master/docs/suggestions.md
*/
const sinon = require('sinon')
const { assert, expect } = require('chai')
const modulePath = '../../../../app/src/Features/Compile/ClsiCookieManager.js'
@ -19,7 +6,6 @@ const realRequst = require('request')
describe('ClsiCookieManager', function () {
beforeEach(function () {
const self = this
this.redis = {
auth() {},
get: sinon.stub(),
@ -57,25 +43,28 @@ describe('ClsiCookieManager', function () {
'@overleaf/settings': this.settings,
request: this.request,
}
return (this.ClsiCookieManager = SandboxedModule.require(modulePath, {
this.ClsiCookieManager = SandboxedModule.require(modulePath, {
requires: this.requires,
})())
})()
})
describe('getServerId', function () {
it('should call get for the key', function (done) {
this.redis.get.callsArgWith(1, null, 'clsi-7')
return this.ClsiCookieManager._getServerId(
this.ClsiCookieManager._getServerId(
this.project_id,
this.user_id,
'',
'e2',
(err, serverId) => {
if (err) {
return done(err)
}
this.redis.get
.calledWith(`clsiserver:${this.project_id}:${this.user_id}`)
.should.equal(true)
serverId.should.equal('clsi-7')
return done()
done()
}
)
})
@ -85,15 +74,18 @@ describe('ClsiCookieManager', function () {
.stub()
.yields(null)
this.redis.get.callsArgWith(1, null)
return this.ClsiCookieManager._getServerId(
this.ClsiCookieManager._getServerId(
this.project_id,
this.user_id,
'',
(err, serverId) => {
if (err) {
return done(err)
}
this.ClsiCookieManager._populateServerIdViaRequest
.calledWith(this.project_id, this.user_id)
.should.equal(true)
return done()
done()
}
)
})
@ -103,16 +95,19 @@ describe('ClsiCookieManager', function () {
.stub()
.yields(null)
this.redis.get.callsArgWith(1, null, '')
return this.ClsiCookieManager._getServerId(
this.ClsiCookieManager._getServerId(
this.project_id,
this.user_id,
'',
'e2',
(err, serverId) => {
if (err) {
return done(err)
}
this.ClsiCookieManager._populateServerIdViaRequest
.calledWith(this.project_id, this.user_id)
.should.equal(true)
return done()
done()
}
)
})
@ -122,38 +117,42 @@ describe('ClsiCookieManager', function () {
beforeEach(function () {
this.response = 'some data'
this.request.post.callsArgWith(1, null, this.response)
return (this.ClsiCookieManager.setServerId = sinon
.stub()
.yields(null, 'clsi-9'))
this.ClsiCookieManager.setServerId = sinon.stub().yields(null, 'clsi-9')
})
it('should make a request to the clsi', function (done) {
return this.ClsiCookieManager._populateServerIdViaRequest(
this.ClsiCookieManager._populateServerIdViaRequest(
this.project_id,
this.user_id,
'standard',
'e2',
(err, serverId) => {
if (err) {
return done(err)
}
const args = this.ClsiCookieManager.setServerId.args[0]
args[0].should.equal(this.project_id)
args[1].should.equal(this.user_id)
args[2].should.equal('standard')
args[3].should.equal('e2')
args[4].should.deep.equal(this.response)
return done()
done()
}
)
})
it('should return the server id', function (done) {
return this.ClsiCookieManager._populateServerIdViaRequest(
this.ClsiCookieManager._populateServerIdViaRequest(
this.project_id,
this.user_id,
'',
'e2',
(err, serverId) => {
if (err) {
return done(err)
}
serverId.should.equal('clsi-9')
return done()
done()
}
)
})
@ -168,7 +167,7 @@ describe('ClsiCookieManager', function () {
})
it('should set the server id with a ttl', function (done) {
return this.ClsiCookieManager.setServerId(
this.ClsiCookieManager.setServerId(
this.project_id,
this.user_id,
'standard',
@ -176,6 +175,9 @@ describe('ClsiCookieManager', function () {
this.response,
null,
err => {
if (err) {
return done(err)
}
this.redis.setex
.calledWith(
`clsiserver:${this.project_id}:${this.user_id}`,
@ -183,7 +185,7 @@ describe('ClsiCookieManager', function () {
'clsi-8'
)
.should.equal(true)
return done()
done()
}
)
})
@ -200,6 +202,9 @@ describe('ClsiCookieManager', function () {
this.response,
null,
err => {
if (err) {
return done(err)
}
expect(this.redis.setex).to.have.been.calledWith(
`clsiserver:${this.project_id}:${this.user_id}`,
this.settings.clsiCookie.ttlInSecondsRegular,
@ -211,7 +216,7 @@ describe('ClsiCookieManager', function () {
})
it('should return the server id', function (done) {
return this.ClsiCookieManager.setServerId(
this.ClsiCookieManager.setServerId(
this.project_id,
this.user_id,
'standard',
@ -219,8 +224,11 @@ describe('ClsiCookieManager', function () {
this.response,
null,
(err, serverId) => {
if (err) {
return done(err)
}
serverId.should.equal('clsi-8')
return done()
done()
}
)
})
@ -233,7 +241,7 @@ describe('ClsiCookieManager', function () {
},
requires: this.requires,
})()
return this.ClsiCookieManager.setServerId(
this.ClsiCookieManager.setServerId(
this.project_id,
this.user_id,
'standard',
@ -241,8 +249,11 @@ describe('ClsiCookieManager', function () {
this.response,
null,
(err, serverId) => {
if (err) {
return done(err)
}
this.redis.setex.called.should.equal(false)
return done()
done()
}
)
})
@ -251,7 +262,7 @@ describe('ClsiCookieManager', function () {
this.ClsiCookieManager._parseServerIdFromResponse = sinon
.stub()
.returns(null)
return this.ClsiCookieManager.setServerId(
this.ClsiCookieManager.setServerId(
this.project_id,
this.user_id,
'standard',
@ -259,8 +270,11 @@ describe('ClsiCookieManager', function () {
this.response,
null,
(err, serverId) => {
if (err) {
return done(err)
}
this.redis.setex.called.should.equal(false)
return done()
done()
}
)
})
@ -282,7 +296,7 @@ describe('ClsiCookieManager', function () {
this.ClsiCookieManager._parseServerIdFromResponse = sinon
.stub()
.returns('clsi-8')
return this.ClsiCookieManager.setServerId(
this.ClsiCookieManager.setServerId(
this.project_id,
this.user_id,
'standard',
@ -290,6 +304,9 @@ describe('ClsiCookieManager', function () {
this.response,
null,
(err, serverId) => {
if (err) {
return done(err)
}
this.redis_secondary.setex
.calledWith(
`clsiserver:${this.project_id}:${this.user_id}`,
@ -297,7 +314,7 @@ describe('ClsiCookieManager', function () {
'clsi-8'
)
.should.equal(true)
return done()
done()
}
)
})
@ -305,25 +322,26 @@ describe('ClsiCookieManager', function () {
describe('getCookieJar', function () {
beforeEach(function () {
return (this.ClsiCookieManager._getServerId = sinon
.stub()
.yields(null, 'clsi-11'))
this.ClsiCookieManager._getServerId = sinon.stub().yields(null, 'clsi-11')
})
it('should return a jar with the cookie set populated from redis', function (done) {
return this.ClsiCookieManager.getCookieJar(
this.ClsiCookieManager.getCookieJar(
this.project_id,
this.user_id,
'',
'e2',
(err, jar) => {
if (err) {
return done(err)
}
jar._jar.store.idx['clsi.example.com']['/'][
this.settings.clsiCookie.key
].key.should.equal
jar._jar.store.idx['clsi.example.com']['/'][
this.settings.clsiCookie.key
].value.should.equal('clsi-11')
return done()
done()
}
)
})
@ -336,14 +354,17 @@ describe('ClsiCookieManager', function () {
},
requires: this.requires,
})()
return this.ClsiCookieManager.getCookieJar(
this.ClsiCookieManager.getCookieJar(
this.project_id,
this.user_id,
'',
'e2',
(err, jar) => {
if (err) {
return done(err)
}
assert.deepEqual(jar, realRequst.jar())
return done()
done()
}
)
})

File diff suppressed because it is too large Load diff