2021-12-14 08:00:35 -05:00
|
|
|
const logger = require('@overleaf/logger')
|
2021-07-12 12:47:18 -04:00
|
|
|
const settings = require('@overleaf/settings')
|
2020-06-23 13:29:44 -04:00
|
|
|
const RedisClientManager = require('./RedisClientManager')
|
|
|
|
const SafeJsonParse = require('./SafeJsonParse')
|
|
|
|
const EventLogger = require('./EventLogger')
|
|
|
|
const HealthCheckManager = require('./HealthCheckManager')
|
|
|
|
const RoomManager = require('./RoomManager')
|
|
|
|
const ChannelManager = require('./ChannelManager')
|
2020-11-25 06:57:22 -05:00
|
|
|
const metrics = require('@overleaf/metrics')
|
2014-11-14 10:30:18 -05:00
|
|
|
|
2020-07-07 06:06:02 -04:00
|
|
|
let DocumentUpdaterController
|
2020-06-23 13:29:44 -04:00
|
|
|
module.exports = DocumentUpdaterController = {
|
|
|
|
// DocumentUpdaterController is responsible for updates that come via Redis
|
|
|
|
// Pub/Sub from the document updater.
|
|
|
|
rclientList: RedisClientManager.createClientList(settings.redis.pubsub),
|
2014-11-14 10:30:18 -05:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
listenForUpdatesFromDocumentUpdater(io) {
|
2021-09-14 04:36:24 -04:00
|
|
|
logger.debug(
|
2020-06-23 13:29:44 -04:00
|
|
|
{ rclients: this.rclientList.length },
|
|
|
|
'listening for applied-ops events'
|
|
|
|
)
|
2020-07-07 06:06:02 -04:00
|
|
|
for (const rclient of this.rclientList) {
|
2020-06-23 13:29:44 -04:00
|
|
|
rclient.subscribe('applied-ops')
|
|
|
|
rclient.on('message', function (channel, message) {
|
|
|
|
metrics.inc('rclient', 0.001) // global event rate metric
|
|
|
|
if (settings.debugEvents > 0) {
|
|
|
|
EventLogger.debugEvent(channel, message)
|
|
|
|
}
|
2020-07-07 06:06:02 -04:00
|
|
|
DocumentUpdaterController._processMessageFromDocumentUpdater(
|
2020-06-23 13:29:44 -04:00
|
|
|
io,
|
|
|
|
channel,
|
|
|
|
message
|
|
|
|
)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
// create metrics for each redis instance only when we have multiple redis clients
|
|
|
|
if (this.rclientList.length > 1) {
|
2020-07-07 06:06:02 -04:00
|
|
|
this.rclientList.forEach((rclient, i) => {
|
|
|
|
// per client event rate metric
|
|
|
|
const metricName = `rclient-${i}`
|
|
|
|
rclient.on('message', () => metrics.inc(metricName, 0.001))
|
|
|
|
})
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
2020-07-07 06:06:02 -04:00
|
|
|
this.handleRoomUpdates(this.rclientList)
|
2020-06-23 13:29:44 -04:00
|
|
|
},
|
2019-07-18 06:25:10 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
handleRoomUpdates(rclientSubList) {
|
|
|
|
const roomEvents = RoomManager.eventSource()
|
2023-03-20 10:10:40 -04:00
|
|
|
roomEvents.on('doc-active', function (docId) {
|
2021-07-13 07:04:45 -04:00
|
|
|
const subscribePromises = rclientSubList.map(rclient =>
|
2023-03-20 10:10:40 -04:00
|
|
|
ChannelManager.subscribe(rclient, 'applied-ops', docId)
|
2020-06-23 13:29:44 -04:00
|
|
|
)
|
2023-03-20 10:10:40 -04:00
|
|
|
RoomManager.emitOnCompletion(subscribePromises, `doc-subscribed-${docId}`)
|
2020-06-23 13:29:44 -04:00
|
|
|
})
|
2023-03-20 10:10:40 -04:00
|
|
|
roomEvents.on('doc-empty', docId =>
|
2021-07-13 07:04:45 -04:00
|
|
|
rclientSubList.map(rclient =>
|
2023-03-20 10:10:40 -04:00
|
|
|
ChannelManager.unsubscribe(rclient, 'applied-ops', docId)
|
2020-06-23 13:29:44 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
},
|
2019-07-18 06:25:10 -04:00
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
_processMessageFromDocumentUpdater(io, channel, message) {
|
2020-07-07 06:06:02 -04:00
|
|
|
SafeJsonParse.parse(message, function (error, message) {
|
|
|
|
if (error) {
|
2020-06-23 13:29:44 -04:00
|
|
|
logger.error({ err: error, channel }, 'error parsing JSON')
|
|
|
|
return
|
|
|
|
}
|
2020-07-07 06:06:02 -04:00
|
|
|
if (message.op) {
|
|
|
|
if (message._id && settings.checkEventOrder) {
|
2020-06-23 13:29:44 -04:00
|
|
|
const status = EventLogger.checkEventOrder(
|
|
|
|
'applied-ops',
|
|
|
|
message._id,
|
|
|
|
message
|
|
|
|
)
|
|
|
|
if (status === 'duplicate') {
|
|
|
|
return // skip duplicate events
|
|
|
|
}
|
|
|
|
}
|
2020-07-07 06:06:02 -04:00
|
|
|
DocumentUpdaterController._applyUpdateFromDocumentUpdater(
|
2020-06-23 13:29:44 -04:00
|
|
|
io,
|
|
|
|
message.doc_id,
|
|
|
|
message.op
|
|
|
|
)
|
2020-07-07 06:06:02 -04:00
|
|
|
} else if (message.error) {
|
|
|
|
DocumentUpdaterController._processErrorFromDocumentUpdater(
|
2020-06-23 13:29:44 -04:00
|
|
|
io,
|
|
|
|
message.doc_id,
|
|
|
|
message.error,
|
|
|
|
message
|
|
|
|
)
|
2020-07-07 06:06:02 -04:00
|
|
|
} else if (message.health_check) {
|
2020-06-23 13:29:44 -04:00
|
|
|
logger.debug(
|
|
|
|
{ message },
|
|
|
|
'got health check message in applied ops channel'
|
|
|
|
)
|
2020-07-07 06:06:02 -04:00
|
|
|
HealthCheckManager.check(channel, message.key)
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|
|
|
|
})
|
|
|
|
},
|
2014-11-14 10:30:18 -05:00
|
|
|
|
2023-03-20 10:10:40 -04:00
|
|
|
_applyUpdateFromDocumentUpdater(io, docId, update) {
|
2020-06-23 13:29:44 -04:00
|
|
|
let client
|
2023-03-20 10:10:40 -04:00
|
|
|
const clientList = io.sockets.clients(docId)
|
2020-06-23 13:29:44 -04:00
|
|
|
// avoid unnecessary work if no clients are connected
|
|
|
|
if (clientList.length === 0) {
|
|
|
|
return
|
|
|
|
}
|
2024-07-18 09:01:09 -04:00
|
|
|
|
|
|
|
update.meta = update.meta || {}
|
|
|
|
const { tsRT: realTimeIngestionTime } = update.meta
|
|
|
|
delete update.meta.tsRT
|
|
|
|
|
2020-06-23 13:29:44 -04:00
|
|
|
// send updates to clients
|
2021-09-14 04:36:24 -04:00
|
|
|
logger.debug(
|
2020-06-23 13:29:44 -04:00
|
|
|
{
|
2023-03-20 10:10:40 -04:00
|
|
|
docId,
|
2020-06-23 13:29:44 -04:00
|
|
|
version: update.v,
|
2020-07-07 06:06:02 -04:00
|
|
|
source: update.meta && update.meta.source,
|
2021-07-13 07:04:45 -04:00
|
|
|
socketIoClients: clientList.map(client => client.id),
|
2020-06-23 13:29:44 -04:00
|
|
|
},
|
|
|
|
'distributing updates to clients'
|
|
|
|
)
|
|
|
|
const seen = {}
|
|
|
|
// send messages only to unique clients (due to duplicate entries in io.sockets.clients)
|
2020-07-07 06:06:02 -04:00
|
|
|
for (client of clientList) {
|
2020-06-23 13:29:44 -04:00
|
|
|
if (!seen[client.id]) {
|
|
|
|
seen[client.id] = true
|
|
|
|
if (client.publicId === update.meta.source) {
|
2021-09-14 04:36:24 -04:00
|
|
|
logger.debug(
|
2020-06-23 13:29:44 -04:00
|
|
|
{
|
2023-03-20 10:10:40 -04:00
|
|
|
docId,
|
2020-06-23 13:29:44 -04:00
|
|
|
version: update.v,
|
2021-07-13 07:04:45 -04:00
|
|
|
source: update.meta.source,
|
2020-06-23 13:29:44 -04:00
|
|
|
},
|
|
|
|
'distributing update to sender'
|
|
|
|
)
|
2024-07-18 09:01:09 -04:00
|
|
|
metrics.histogram(
|
|
|
|
'update-processing-time',
|
|
|
|
performance.now() - realTimeIngestionTime,
|
|
|
|
[
|
|
|
|
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 50, 100, 200, 500, 1000,
|
|
|
|
2000, 5000, 10000,
|
|
|
|
],
|
|
|
|
{ path: 'sharejs' }
|
|
|
|
)
|
2020-06-23 13:29:44 -04:00
|
|
|
client.emit('otUpdateApplied', { v: update.v, doc: update.doc })
|
|
|
|
} else if (!update.dup) {
|
|
|
|
// Duplicate ops should just be sent back to sending client for acknowledgement
|
2021-09-14 04:36:24 -04:00
|
|
|
logger.debug(
|
2020-06-23 13:29:44 -04:00
|
|
|
{
|
2023-03-20 10:10:40 -04:00
|
|
|
docId,
|
2020-06-23 13:29:44 -04:00
|
|
|
version: update.v,
|
2020-07-07 06:06:02 -04:00
|
|
|
source: update.meta.source,
|
2023-03-20 10:10:40 -04:00
|
|
|
clientId: client.id,
|
2020-06-23 13:29:44 -04:00
|
|
|
},
|
|
|
|
'distributing update to collaborator'
|
|
|
|
)
|
|
|
|
client.emit('otUpdateApplied', update)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (Object.keys(seen).length < clientList.length) {
|
|
|
|
metrics.inc('socket-io.duplicate-clients', 0.1)
|
2021-09-14 04:36:24 -04:00
|
|
|
logger.debug(
|
2020-06-23 13:29:44 -04:00
|
|
|
{
|
2023-03-20 10:10:40 -04:00
|
|
|
docId,
|
2021-07-13 07:04:45 -04:00
|
|
|
socketIoClients: clientList.map(client => client.id),
|
2020-06-23 13:29:44 -04:00
|
|
|
},
|
|
|
|
'discarded duplicate clients'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
},
|
2014-11-14 10:30:18 -05:00
|
|
|
|
2023-03-20 10:10:40 -04:00
|
|
|
_processErrorFromDocumentUpdater(io, docId, error, message) {
|
|
|
|
for (const client of io.sockets.clients(docId)) {
|
2020-07-07 06:06:02 -04:00
|
|
|
logger.warn(
|
2023-03-20 10:10:40 -04:00
|
|
|
{ err: error, docId, clientId: client.id },
|
2020-07-07 06:06:02 -04:00
|
|
|
'error from document updater, disconnecting client'
|
|
|
|
)
|
|
|
|
client.emit('otUpdateError', error, message)
|
|
|
|
client.disconnect()
|
|
|
|
}
|
2021-07-13 07:04:45 -04:00
|
|
|
},
|
2020-06-23 13:29:44 -04:00
|
|
|
}
|