Merge pull request #18583 from overleaf/em-multi-hash-key

Ensure that MULTI commands always go to a single node

GitOrigin-RevId: 223d29986766577df89c82983126dabca5d16eed
This commit is contained in:
Eric Mc Sween 2024-05-29 11:50:34 -04:00 committed by Copybot
parent 55e54ce875
commit 22257cf037
6 changed files with 172 additions and 126 deletions

View file

@ -23,6 +23,12 @@ const ProjectHistoryRedisManager = {
for (const op of ops) { for (const op of ops) {
metrics.summary('redis.projectHistoryOps', op.length, { status: 'push' }) metrics.summary('redis.projectHistoryOps', op.length, { status: 'push' })
} }
// Make sure that this MULTI operation only operates on project
// specific keys, i.e. keys that have the project id in curly braces.
// The curly braces identify a hash key for Redis and ensures that
// the MULTI's operations are all done on the same node in a
// cluster environment.
const multi = rclient.multi() const multi = rclient.multi()
// Push the ops onto the project history queue // Push the ops onto the project history queue
multi.rpush( multi.rpush(

View file

@ -32,6 +32,11 @@ const MAX_OPS_PER_ITERATION = 8 // process a limited number of ops for safety
const RealTimeRedisManager = { const RealTimeRedisManager = {
getPendingUpdatesForDoc(docId, callback) { getPendingUpdatesForDoc(docId, callback) {
// Make sure that this MULTI operation only operates on doc
// specific keys, i.e. keys that have the doc id in curly braces.
// The curly braces identify a hash key for Redis and ensures that
// the MULTI's operations are all done on the same node in a
// cluster environment.
const multi = rclient.multi() const multi = rclient.multi()
multi.lrange( multi.lrange(
Keys.pendingUpdates({ doc_id: docId }), Keys.pendingUpdates({ doc_id: docId }),

View file

@ -77,6 +77,10 @@ const RedisManager = {
logger.error({ err: error, docId, projectId }, error.message) logger.error({ err: error, docId, projectId }, error.message)
return callback(error) return callback(error)
} }
setHistoryRangesSupportFlag(docId, historyRangesSupport, err => {
if (err) {
return callback(err)
}
// update docsInProject set before writing doc contents // update docsInProject set before writing doc contents
rclient.sadd( rclient.sadd(
keys.docsInProject({ project_id: projectId }), keys.docsInProject({ project_id: projectId }),
@ -91,6 +95,11 @@ const RedisManager = {
}) })
} }
// Make sure that this MULTI operation only operates on doc
// specific keys, i.e. keys that have the doc id in curly braces.
// The curly braces identify a hash key for Redis and ensures that
// the MULTI's operations are all done on the same node in a
// cluster environment.
const multi = rclient.multi() const multi = rclient.multi()
multi.mset({ multi.mset({
[keys.docLines({ doc_id: docId })]: docLines, [keys.docLines({ doc_id: docId })]: docLines,
@ -102,7 +111,6 @@ const RedisManager = {
[keys.projectHistoryId({ doc_id: docId })]: projectHistoryId, [keys.projectHistoryId({ doc_id: docId })]: projectHistoryId,
}) })
if (historyRangesSupport) { if (historyRangesSupport) {
multi.sadd(keys.historyRangesSupport(), docId)
multi.del(keys.resolvedCommentIds({ doc_id: docId })) multi.del(keys.resolvedCommentIds({ doc_id: docId }))
if (resolvedCommentIds.length > 0) { if (resolvedCommentIds.length > 0) {
multi.sadd( multi.sadd(
@ -129,6 +137,7 @@ const RedisManager = {
} }
) )
}) })
})
}, },
removeDocFromMemory(projectId, docId, _callback) { removeDocFromMemory(projectId, docId, _callback) {
@ -143,6 +152,11 @@ const RedisManager = {
} }
} }
// Make sure that this MULTI operation only operates on doc
// specific keys, i.e. keys that have the doc id in curly braces.
// The curly braces identify a hash key for Redis and ensures that
// the MULTI's operations are all done on the same node in a
// cluster environment.
let multi = rclient.multi() let multi = rclient.multi()
multi.strlen(keys.docLines({ doc_id: docId })) multi.strlen(keys.docLines({ doc_id: docId }))
multi.del( multi.del(
@ -167,6 +181,12 @@ const RedisManager = {
// record bytes freed in redis // record bytes freed in redis
metrics.summary('redis.docLines', length, { status: 'del' }) metrics.summary('redis.docLines', length, { status: 'del' })
} }
// Make sure that this MULTI operation only operates on project
// specific keys, i.e. keys that have the project id in curly braces.
// The curly braces identify a hash key for Redis and ensures that
// the MULTI's operations are all done on the same node in a
// cluster environment.
multi = rclient.multi() multi = rclient.multi()
multi.srem(keys.docsInProject({ project_id: projectId }), docId) multi.srem(keys.docsInProject({ project_id: projectId }), docId)
multi.del(keys.projectState({ project_id: projectId })) multi.del(keys.projectState({ project_id: projectId }))
@ -180,6 +200,11 @@ const RedisManager = {
}, },
checkOrSetProjectState(projectId, newState, callback) { checkOrSetProjectState(projectId, newState, callback) {
// Make sure that this MULTI operation only operates on project
// specific keys, i.e. keys that have the project id in curly braces.
// The curly braces identify a hash key for Redis and ensures that
// the MULTI's operations are all done on the same node in a
// cluster environment.
const multi = rclient.multi() const multi = rclient.multi()
multi.getset(keys.projectState({ project_id: projectId }), newState) multi.getset(keys.projectState({ project_id: projectId }), newState)
multi.expire(keys.projectState({ project_id: projectId }), 30 * minutes) multi.expire(keys.projectState({ project_id: projectId }), 30 * minutes)
@ -488,6 +513,12 @@ const RedisManager = {
logger.error({ err: error, docId, ranges }, error.message) logger.error({ err: error, docId, ranges }, error.message)
return callback(error) return callback(error)
} }
// Make sure that this MULTI operation only operates on doc
// specific keys, i.e. keys that have the doc id in curly braces.
// The curly braces identify a hash key for Redis and ensures that
// the MULTI's operations are all done on the same node in a
// cluster environment.
const multi = rclient.multi() const multi = rclient.multi()
multi.mset({ multi.mset({
[keys.docLines({ doc_id: docId })]: newDocLines, [keys.docLines({ doc_id: docId })]: newDocLines,
@ -670,6 +701,14 @@ const RedisManager = {
}, },
} }
function setHistoryRangesSupportFlag(docId, historyRangesSupport, callback) {
if (historyRangesSupport) {
rclient.sadd(keys.historyRangesSupport(), docId, callback)
} else {
rclient.srem(keys.historyRangesSupport(), docId, callback)
}
}
module.exports = RedisManager module.exports = RedisManager
module.exports.promises = promisifyAll(RedisManager, { module.exports.promises = promisifyAll(RedisManager, {
without: ['_deserializeRanges', '_computeHash'], without: ['_deserializeRanges', '_computeHash'],

View file

@ -441,13 +441,10 @@ describe('Ranges', function () {
lines: ['aaa', 'bbb', 'ccc', 'ddd', 'eee'], lines: ['aaa', 'bbb', 'ccc', 'ddd', 'eee'],
} }
DocUpdaterClient.enableHistoryRangesSupport(this.doc.id, (error, res) => {
if (error) {
throw error
}
MockWebApi.insertDoc(this.project_id, this.doc.id, { MockWebApi.insertDoc(this.project_id, this.doc.id, {
lines: this.doc.lines, lines: this.doc.lines,
version: 0, version: 0,
historyRangesSupport: true,
}) })
DocUpdaterClient.preloadDoc(this.project_id, this.doc.id, error => { DocUpdaterClient.preloadDoc(this.project_id, this.doc.id, error => {
@ -522,7 +519,7 @@ describe('Ranges', function () {
) )
}) })
}) })
})
afterEach(function () { afterEach(function () {
sandbox.restore() sandbox.restore()
}) })

View file

@ -119,10 +119,6 @@ module.exports = DocUpdaterClient = {
) )
}, },
enableHistoryRangesSupport(docId, cb) {
rclient.sadd(keys.historyRangesSupport(), docId, cb)
},
preloadDoc(projectId, docId, callback) { preloadDoc(projectId, docId, callback) {
DocUpdaterClient.getDoc(projectId, docId, callback) DocUpdaterClient.getDoc(projectId, docId, callback)
}, },

View file

@ -834,8 +834,11 @@ describe('RedisManager', function () {
this.logger.error.calledWith().should.equal(false) this.logger.error.calledWith().should.equal(false)
}) })
it('should not add the document to the HistoryRangesSupport set in Redis', function () { it('should remove the document from the HistoryRangesSupport set in Redis', function () {
this.multi.sadd.should.not.have.been.calledWith('HistoryRangesSupport') this.rclient.srem.should.have.been.calledWith(
'HistoryRangesSupport',
this.docId
)
}) })
it('should not store the resolved comments in Redis', function () { it('should not store the resolved comments in Redis', function () {
@ -956,7 +959,7 @@ describe('RedisManager', function () {
}) })
it('should add the document to the HistoryRangesSupport set in Redis', function () { it('should add the document to the HistoryRangesSupport set in Redis', function () {
this.multi.sadd.should.have.been.calledWith( this.rclient.sadd.should.have.been.calledWith(
'HistoryRangesSupport', 'HistoryRangesSupport',
this.docId this.docId
) )