-
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathasyncPersistence.js
More file actions
642 lines (564 loc) · 18 KB
/
asyncPersistence.js
File metadata and controls
642 lines (564 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
'use strict'
const regEscape = require('escape-string-regexp')
const Packet = require('aedes-persistence').Packet
const BroadcastPersistence = require('aedes-persistence/broadcastPersistence.js')
const { MongoClient } = require('mongodb')
const { Qlobber } = require('qlobber')
const QlobberSub = require('qlobber/aedes/qlobber-sub')
const QLOBBER_OPTIONS = {
separator: '/',
wildcard_one: '+',
wildcard_some: '#',
match_empty_levels: true
}
// Batching limits for retained message pattern queries
// MongoDB has a BSON document size limit of 16MB, but regex patterns hit
// practical compilation/execution limits around 32KB. These values are tuned
// to prevent "regular expression is too large" errors while maintaining good
// query performance.
//
// Why two limits?
// - MAX_TOTAL_PATTERN_LENGTH: Prevents MongoDB regex size errors (~32KB limit)
// Cumulative pattern length is tracked to stay well under MongoDB's practical
// regex limit, providing a safety margin for regex escaping and construction.
//
// - MAX_PATTERNS_PER_BATCH: Optimizes query performance by reducing network overhead
// Larger batches mean fewer MongoDB queries, which typically improves performance
// more than smaller batches with simpler regex patterns.
//
// These values balance safety (preventing regex errors) with performance (minimizing
// the number of database queries needed to process subscription patterns).
const MAX_PATTERNS_PER_BATCH = 200
const MAX_TOTAL_PATTERN_LENGTH = 15000
class AsyncMongoPersistence {
// private class members start with #
#trie
#destroyed
#broker
#opts
#db
#mongoDBclient
#cl
#broadcast
#retainedBulkQueue
#executing
constructor (opts = {}) {
this.#trie = new QlobberSub(QLOBBER_OPTIONS) // used to match packets
opts.ttl = opts.ttl || {}
if (typeof opts.ttl.packets === 'number') {
const ttl = opts.ttl.packets
opts.ttl.packets = {
retained: ttl,
will: ttl,
outgoing: ttl,
incoming: ttl
}
}
this.#opts = opts
this.#db = null
this.#cl = null
this.#destroyed = false
this.#retainedBulkQueue = [] // used for storing retained packets with ordered bulks
this.#executing = false // used as lock while a bulk is executing
}
#addTTLIndexes (indexes) {
const addTTLIndex = (collection, key, expireAfterSeconds) => {
if (expireAfterSeconds >= 0) {
indexes.push({ collection, key, name: 'ttl', expireAfterSeconds })
}
}
if (this.#opts.ttl.subscriptions >= 0) {
addTTLIndex(
'subscriptions',
this.#opts.ttlAfterDisconnected ? 'disconnected' : 'added',
this.#opts.ttl.subscriptions
)
}
if (this.#opts.ttl.packets) {
addTTLIndex('retained', 'added', this.#opts.ttl.packets.retained)
addTTLIndex('will', 'packet.added', this.#opts.ttl.packets.will)
addTTLIndex('outgoing', 'packet.added', this.#opts.ttl.packets.outgoing)
addTTLIndex('incoming', 'packet.added', this.#opts.ttl.packets.incoming)
}
}
// access #broker, only for testing
get broker () {
return this.#broker
}
// access #db, only for testing
get _db () {
return this.#db
}
// access #mongoDBclient, only for testing
get _mongoDBclient () {
return this.#mongoDBclient
}
// setup is called by aedes-persistence/callbackPersistence.js
async setup (broker) {
this.#broker = broker
// database already connected
if (this.#db) {
return
}
// database already provided in the options
if (this.#opts.db) {
this.#db = this.#opts.db
} else {
// connect to the database
const conn = this.#opts.url || 'mongodb://127.0.0.1/aedes'
const options = this.#opts.mongoOptions
const mongoDBclient = new MongoClient(conn, options)
this.#mongoDBclient = mongoDBclient
const urlParsed = URL.parse(this.#opts.url)
// skip the first / of the pathname if it exists
const pathname = urlParsed.pathname ? urlParsed.pathname.substring(1) : undefined
const databaseName = this.#opts.database || pathname
this.#db = mongoDBclient.db(databaseName)
}
const collectionPrefix = `${this.#opts.collectionPrefix || ''}`
const db = this.#db
const subscriptions = db.collection(`${collectionPrefix}subscriptions`)
const retained = db.collection(`${collectionPrefix}retained`)
const will = db.collection(`${collectionPrefix}will`)
const outgoing = db.collection(`${collectionPrefix}outgoing`)
const incoming = db.collection(`${collectionPrefix}incoming`)
this.#cl = {
subscriptions,
retained,
will,
outgoing,
incoming
}
// drop existing TTL indexes (if exist)
if (this.#opts.dropExistingIndexes) {
const collections = await db.collections()
for (const collection of collections) {
const exists = await collection.indexExists('ttl')
if (exists) {
await collection.dropIndex('ttl')
}
}
}
// create indexes
const createIndex = async (idx) => {
const indexOpts = { name: idx.name }
if (typeof idx.expireAfterSeconds === 'number') {
indexOpts.expireAfterSeconds = idx.expireAfterSeconds
}
await this.#cl[idx.collection].createIndex(idx.key, indexOpts)
}
const indexes = [
{
collection: 'outgoing',
key: { clientId: 1, 'packet.brokerId': 1, 'packet.brokerCounter': 1 },
name: 'query_clientId_brokerId'
},
{
collection: 'outgoing',
key: { clientId: 1, 'packet.messageId': 1 },
name: 'query_clientId_messageId'
},
{
collection: 'incoming',
key: { clientId: 1, 'packet.brokerId': 1, 'packet.brokerCounter': 1 },
name: 'query_clientId_brokerId'
},
{
collection: 'incoming',
key: { clientId: 1, 'packet.messageId': 1 },
name: 'query_clientId_messageId'
}
]
// Add TTL indexes
this.#addTTLIndexes(indexes)
// create all indexes in parallel
await Promise.all(indexes.map(createIndex))
if (this.#opts.ttlAfterDisconnected) {
// To avoid stale subscriptions that might be left behind by broker shutting
// down while clients were connected, set all to disconnected on startup.
await this.#cl.subscriptions.updateMany({ disconnected: { $exists: false } }, { $currentDate: { disconnected: true } })
// Handlers for setting and clearing the disconnected timestamp on subscriptions
this.#broker.on('clientReady', (client) => {
this.#cl.subscriptions.updateMany({ clientId: client.id }, { $unset: { disconnected: true } })
})
this.#broker.on('clientDisconnect', (client) => {
this.#cl.subscriptions.updateMany({ clientId: client.id }, { $currentDate: { disconnected: true } })
})
}
// add subscriptions to Trie
for await (const subscription of subscriptions.find({
qos: { $gte: 0 }
})) {
this.#trie.add(subscription.topic, subscription)
}
// subscribe to the broker for subscription updates
this.#broadcast = new BroadcastPersistence(broker, this.#trie)
await this.#broadcast.brokerSubscribe()
// setup is done
}
async processRetainedBulk () {
if (!this.#executing && !this.#destroyed && this.#retainedBulkQueue.length > 0) {
this.#executing = true
const operations = []
const onEnd = []
while (this.#retainedBulkQueue.length) {
const { operation, resolve } = this.#retainedBulkQueue.shift()
operations.push(operation)
onEnd.push(resolve)
}
// execute operations and ignore the error
await this.#cl.retained.bulkWrite(operations).catch(() => { })
// resolve all promises
while (onEnd.length) onEnd.shift().call()
// check if we have new packets in queue
this.#executing = false
// do not await as we run this in background and ignore errors
this.processRetainedBulk()
}
if (this.#destroyed) {
// cleanup dangling promises
while (this.#retainedBulkQueue.length) {
const { resolve } = this.#retainedBulkQueue.shift()
resolve() // resolve all promises
}
}
}
async storeRetained (packet) {
const { promise, resolve } = promiseWithResolvers()
const queue = this.#retainedBulkQueue
const filter = { topic: packet.topic }
if (packet.payload.length > 0) {
queue.push({
operation: {
updateOne: {
filter,
update: { $set: decoratePacket(packet, this.#opts.ttl.packets) },
upsert: true
}
},
resolve
})
} else {
queue.push({
operation: {
deleteOne: {
filter
}
},
resolve
})
}
this.processRetainedBulk()
return promise
}
createRetainedStream (pattern) {
return this.createRetainedStreamCombi([pattern])
}
async * createRetainedStreamCombi (patterns) {
const matcher = new Qlobber(QLOBBER_OPTIONS)
for (let i = 0; i < patterns.length; i++) {
matcher.add(patterns[i], true)
}
// Calculate total pattern length
const totalLength = patterns.reduce((sum, p) => sum + p.length, 0)
// Determine if we need to batch
const needsBatching =
patterns.length > MAX_PATTERNS_PER_BATCH ||
totalLength > MAX_TOTAL_PATTERN_LENGTH
if (needsBatching) {
// Process patterns in batches to avoid creating regex that's too large
// Use dynamic batching based on cumulative length
const seenTopics = new Set() // Track yielded packets to avoid duplicates
const batches = []
let currentBatch = []
let currentLength = 0
for (const pattern of patterns) {
const patternLength = pattern.length
// Edge case: if a single pattern exceeds MAX_TOTAL_PATTERN_LENGTH,
// it will be placed in its own batch. This is intentional behavior
// to ensure the pattern is still processed (MongoDB will handle it
// or fail with a clear error). Very long patterns (>32KB after escaping)
// may still cause MongoDB "regular expression is too large" errors.
// Start a new batch if adding this pattern would exceed limits
if (currentBatch.length >= MAX_PATTERNS_PER_BATCH ||
(currentLength + patternLength > MAX_TOTAL_PATTERN_LENGTH && currentBatch.length > 0)) {
batches.push(currentBatch)
currentBatch = []
currentLength = 0
}
currentBatch.push(pattern)
currentLength += patternLength
}
// Add the last batch if not empty
if (currentBatch.length > 0) {
batches.push(currentBatch)
}
for (const batch of batches) {
for await (const packet of this.#queryRetainedByPatterns(batch, matcher)) {
// Avoid duplicates across batches
if (!seenTopics.has(packet.topic)) {
seenTopics.add(packet.topic)
yield packet
}
}
}
} else {
// Original logic for small pattern sets
for await (const packet of this.#queryRetainedByPatterns(patterns, matcher)) {
yield packet
}
}
}
async * #queryRetainedByPatterns (patterns, matcher) {
// Early return for empty patterns to avoid creating an empty regex
// that would match all documents in the collection
if (patterns.length === 0) {
return
}
const regexes = patterns.map(pattern =>
regEscape(pattern).replace(/(\/*#|\\\+).*$/, '')
)
const topic = new RegExp(regexes.join('|'))
const filter = { topic }
const exclude = { _id: 0 }
for await (const result of this.#cl.retained.find(filter).project(exclude)) {
const packet = asPacket(result)
if (matcher.match(packet.topic).length > 0) {
yield packet
}
}
}
async addSubscriptions (client, subs) {
const subscriptions = []
const operations = subs.map(sub => {
const subscription = { ...sub, clientId: client.id }
subscriptions.push(subscription)
return {
updateOne: {
filter: {
clientId: client.id,
topic: sub.topic
},
update: {
$set: decorateSubscription(subscription, this.#opts)
},
upsert: true
}
}
})
await this.#cl.subscriptions.bulkWrite(operations)
// inform the broker
await this.#broadcast.addedSubscriptions(client, subs)
}
async removeSubscriptions (client, subs) {
const operations = subs.map(topic => ({
deleteOne: {
filter: {
clientId: client.id,
topic
}
}
}))
await this.#cl.subscriptions.bulkWrite(operations)
// inform the broker
await this.#broadcast.removedSubscriptions(client, subs)
}
async subscriptionsByClient (client) {
const filter = { clientId: client.id }
const exclude = { clientId: false, _id: false } // exclude these fields
const subs = await this.#cl.subscriptions.find(filter).project(exclude).toArray()
return subs
}
async countOffline () {
const subsCount = this.#trie.subscriptionsCount
const result = await this.#cl.subscriptions.aggregate([
{
$group: {
_id: '$clientId'
}
}, {
$count: 'clientsCount'
}]).toArray()
const clientsCount = result[0]?.clientsCount || 0
return { subsCount, clientsCount }
}
async destroy () {
if (this.#destroyed) {
throw new Error('destroyed called twice!')
}
this.#destroyed = true
// stop listening to subscription updates
await this.#broadcast.brokerUnsubscribe()
if (this.#opts.db) {
return
}
await this.#mongoDBclient.close()
}
async subscriptionsByTopic (topic) {
return this.#trie.match(topic)
}
async cleanSubscriptions (client) {
const subs = await this.subscriptionsByClient(client)
if (subs.length > 0) {
const remSubs = subs.map(sub => sub.topic)
await this.removeSubscriptions(client, remSubs)
}
}
async outgoingEnqueue (sub, packet) {
return await this.outgoingEnqueueCombi([sub], packet)
}
async outgoingEnqueueCombi (subs, packet) {
if (subs?.length === 0) {
return packet
}
const newPacket = new Packet(packet)
const decoratedPacket = decoratePacket(newPacket, this.#opts.ttl.packets)
const packets = subs.map(sub => ({
clientId: sub.clientId,
packet: decoratedPacket
}))
await this.#cl.outgoing.insertMany(packets)
}
async * outgoingStream (client) {
for await (const result of this.#cl.outgoing.find({ clientId: client.id })) {
yield asPacket(result)
}
}
async outgoingUpdate (client, packet) {
if (packet.brokerId) {
await updateWithMessageId(this.#cl, client, packet)
} else {
await updatePacket(this.#cl, client, packet)
}
}
async outgoingClearMessageId (client, packet) {
const result = await this.#cl.outgoing.findOneAndDelete({
clientId: client.id,
'packet.messageId': packet.messageId
})
return result ? asPacket(result) : null
}
async incomingStorePacket (client, packet) {
const newPacket = new Packet(packet)
newPacket.messageId = packet.messageId
await this.#cl.incoming.insertOne({
clientId: client.id,
packet: decoratePacket(newPacket, this.#opts.ttl.packets)
})
}
async incomingGetPacket (client, packet) {
const result = await this.#cl.incoming.findOne({
clientId: client.id,
'packet.messageId': packet.messageId
})
if (!result) {
throw new Error(`packet not found for: ${client}`)
}
return asPacket(result)
}
async incomingDelPacket (client, packet) {
await this.#cl.incoming.deleteOne({
clientId: client.id,
'packet.messageId': packet.messageId
})
}
async putWill (client, packet) {
packet.clientId = client.id
packet.brokerId = this.#broker.id
await this.#cl.will.insertOne({
clientId: client.id,
packet: decoratePacket(packet, this.#opts.ttl.packets)
})
}
async getWill (client) {
const result = await this.#cl.will.findOne({
clientId: client.id
})
if (!result) {
return null // packet not found
}
return asPacket(result)
}
async delWill (client) {
const result = await this.#cl.will.findOneAndDelete({
clientId: client.id
})
if (!result) {
return null // packet not found
}
return asPacket(result)
}
async * streamWill (brokers) {
const filter = {}
if (brokers) {
filter['packet.brokerId'] = { $nin: Object.keys(brokers) }
}
for await (const will of this.#cl.will.find(filter)) {
yield asPacket(will)
}
}
async * getClientList (topic) {
const filter = {}
if (topic) {
filter.topic = topic
}
for await (const sub of this.#cl.subscriptions.find(filter)) {
yield sub.clientId
}
}
}
function decoratePacket (packet, setTTL) {
if (setTTL) {
packet.added = new Date()
}
return packet
}
function decorateSubscription (sub, opts) {
if (opts.ttl.subscriptions) {
sub.added = new Date()
}
return sub
}
function asPacket (obj) {
const packet = obj?.packet || obj
if (!packet) {
throw new Error('Invalid packet')
}
if (Buffer.isBuffer(packet?.payload?.buffer)) {
packet.payload = packet.payload.buffer
}
return packet
}
async function updateWithMessageId (db, client, packet) {
await db.outgoing.updateOne({
clientId: client.id,
'packet.brokerCounter': packet.brokerCounter,
'packet.brokerId': packet.brokerId
}, {
$set: {
'packet.messageId': packet.messageId
}
})
}
async function updatePacket (db, client, packet) {
await db.outgoing.updateOne({
clientId: client.id,
'packet.messageId': packet.messageId
}, {
$set: {
clientId: client.id,
packet
}
})
}
function promiseWithResolvers () {
// this can be replaced by Promise.withResolvers()in NodeJS >= 22
let res
let rej
const promise = new Promise((resolve, reject) => {
res = resolve
rej = reject
})
return { promise, resolve: res, reject: rej }
}
module.exports = AsyncMongoPersistence