kuzzle 2.61.0-beta.3 → 2.61.0-beta.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -29,6 +29,8 @@ declare class ClientAdapter {
29
29
  capabilities: ReadonlySet<StorageCapability>;
30
30
  scope: storeScopeEnum;
31
31
  cache: IndexCache;
32
+ /** The storage read under way to catch the cache up, shared by its callers */
33
+ private catchingUp;
32
34
  constructor(scope: storeScopeEnum);
33
35
  init(): Promise<void>;
34
36
  createIndex(index: string, { indexCacheOnly, propagate }?: WriteScopeOptions): Promise<void>;
@@ -50,6 +52,27 @@ declare class ClientAdapter {
50
52
  * node joined the cluster must go too (kuzzle#3040).
51
53
  */
52
54
  reloadCache(): Promise<void>;
55
+ /**
56
+ * Caches the indexes and collections storage holds and this node misses.
57
+ *
58
+ * The cache is synchronized across the cluster by broadcasts that no request
59
+ * waits for: right after a peer creates an index or a collection, a request
60
+ * this node serves may not find it yet. Storage is asked before such a
61
+ * request fails, or is answered from the cache alone (kuzzle#3121).
62
+ */
63
+ private catchUpWithStorage;
64
+ /**
65
+ * @throws {services.storage.unknown_index} if neither the cache nor storage
66
+ * holds the index
67
+ */
68
+ private assertIndexExists;
69
+ /**
70
+ * @throws {services.storage.unknown_index | services.storage.unknown_collection}
71
+ * if neither the cache nor storage holds the collection
72
+ */
73
+ private assertCollectionExists;
74
+ /** Whether storage holds the index, whatever the cache says */
75
+ private isStored;
53
76
  /**
54
77
  * Replaces the cached collection write locks with the cluster-wide ones,
55
78
  * stored in Redis.
@@ -108,6 +108,8 @@ function assertSearchCapabilities(capabilities, searchBody) {
108
108
  */
109
109
  class ClientAdapter {
110
110
  constructor(scope) {
111
+ /** The storage read under way to catch the cache up, shared by its callers */
112
+ this.catchingUp = null;
111
113
  const config = global.kuzzle.config.services.storageEngine;
112
114
  const selected = (0, storageDriverLoader_1.selectStorageDriver)(config);
113
115
  this.service = new storageService_1.StorageService(selected.Driver, config, scope);
@@ -156,7 +158,13 @@ class ClientAdapter {
156
158
  }
157
159
  async createIndex(index, { indexCacheOnly = false, propagate = true } = {}) {
158
160
  if (this.cache.hasIndex(index)) {
159
- throw servicesError.get("index_already_exists", this.scope, index);
161
+ // A peer may have deleted it, and its broadcast not reached this node
162
+ // yet (kuzzle#3121). If storage still holds it after all, creating it
163
+ // below fails and caches it again.
164
+ if (indexCacheOnly || (await this.isStored(index))) {
165
+ throw servicesError.get("index_already_exists", this.scope, index);
166
+ }
167
+ this.cache.removeIndex(index);
160
168
  }
161
169
  if (!indexCacheOnly) {
162
170
  try {
@@ -198,7 +206,7 @@ class ClientAdapter {
198
206
  }
199
207
  }
200
208
  async deleteIndex(index) {
201
- this.cache.assertIndexExists(index);
209
+ await this.assertIndexExists(index);
202
210
  this.cache.assertIndexWritable(index);
203
211
  await this.driver.deleteIndex(index);
204
212
  this.cache.removeIndex(index);
@@ -208,6 +216,10 @@ class ClientAdapter {
208
216
  });
209
217
  }
210
218
  async deleteIndexes(indexes) {
219
+ // One storage read for all of them, if any is missing from the cache
220
+ if (indexes.some((index) => !this.cache.hasIndex(index))) {
221
+ await this.catchUpWithStorage();
222
+ }
211
223
  for (const index of indexes) {
212
224
  this.cache.assertIndexExists(index);
213
225
  this.cache.assertIndexWritable(index);
@@ -225,7 +237,7 @@ class ClientAdapter {
225
237
  return deleted;
226
238
  }
227
239
  async deleteCollection(index, collection) {
228
- this.cache.assertCollectionExists(index, collection);
240
+ await this.assertCollectionExists(index, collection);
229
241
  this.cache.assertCollectionWritable(index, collection);
230
242
  await this.driver.deleteCollection(index, collection);
231
243
  this.cache.removeCollection(index, collection);
@@ -261,6 +273,51 @@ class ClientAdapter {
261
273
  this.cache.replaceIndexes(schema, snapshot);
262
274
  await this.loadLocks();
263
275
  }
276
+ /**
277
+ * Caches the indexes and collections storage holds and this node misses.
278
+ *
279
+ * The cache is synchronized across the cluster by broadcasts that no request
280
+ * waits for: right after a peer creates an index or a collection, a request
281
+ * this node serves may not find it yet. Storage is asked before such a
282
+ * request fails, or is answered from the cache alone (kuzzle#3121).
283
+ */
284
+ catchUpWithStorage() {
285
+ // Concurrent cache misses share one read
286
+ this.catchingUp ?? (this.catchingUp = (async () => {
287
+ const removals = this.cache.removals;
288
+ try {
289
+ this.cache.learn(await this.driver.getSchema(), removals);
290
+ }
291
+ finally {
292
+ this.catchingUp = null;
293
+ }
294
+ })());
295
+ return this.catchingUp;
296
+ }
297
+ /**
298
+ * @throws {services.storage.unknown_index} if neither the cache nor storage
299
+ * holds the index
300
+ */
301
+ async assertIndexExists(index) {
302
+ if (!this.cache.hasIndex(index)) {
303
+ await this.catchUpWithStorage();
304
+ }
305
+ this.cache.assertIndexExists(index);
306
+ }
307
+ /**
308
+ * @throws {services.storage.unknown_index | services.storage.unknown_collection}
309
+ * if neither the cache nor storage holds the collection
310
+ */
311
+ async assertCollectionExists(index, collection) {
312
+ if (!this.cache.hasCollection(index, collection)) {
313
+ await this.catchUpWithStorage();
314
+ }
315
+ this.cache.assertCollectionExists(index, collection);
316
+ }
317
+ /** Whether storage holds the index, whatever the cache says */
318
+ async isStored(index) {
319
+ return Object.hasOwn(await this.driver.getSchema(), index);
320
+ }
264
321
  /**
265
322
  * Replaces the cached collection write locks with the cluster-wide ones,
266
323
  * stored in Redis.
@@ -292,7 +349,7 @@ class ClientAdapter {
292
349
  * @throws If the collection does not exist, or is locked by another owner
293
350
  */
294
351
  async lockCollection(index, collection, { owner, reason }) {
295
- this.cache.assertCollectionExists(index, collection);
352
+ await this.assertCollectionExists(index, collection);
296
353
  const field = JSON.stringify([index, collection]);
297
354
  const lock = { lockedAt: Date.now(), owner, reason };
298
355
  // Redis decides, not the local copy: two nodes locking the same collection
@@ -373,8 +430,8 @@ class ClientAdapter {
373
430
  * @param {string} index
374
431
  * @param {string} collection
375
432
  */
376
- global.kuzzle.onAsk(`core:storage:${this.scope}:collection:settings:get`, (index, collection) => {
377
- this.cache.assertCollectionExists(index, collection);
433
+ global.kuzzle.onAsk(`core:storage:${this.scope}:collection:settings:get`, async (index, collection) => {
434
+ await this.assertCollectionExists(index, collection);
378
435
  return this.driver.getSettings(index, collection);
379
436
  });
380
437
  /**
@@ -389,21 +446,30 @@ class ClientAdapter {
389
446
  * @param {string} index
390
447
  * @param {string} collection
391
448
  */
392
- global.kuzzle.onAsk(`core:storage:${this.scope}:collection:exist`, (index, collection) => this.cache.hasCollection(index, collection));
449
+ global.kuzzle.onAsk(`core:storage:${this.scope}:collection:exist`, async (index, collection) => {
450
+ if (this.cache.hasCollection(index, collection)) {
451
+ return true;
452
+ }
453
+ await this.catchUpWithStorage();
454
+ return this.cache.hasCollection(index, collection);
455
+ });
393
456
  /**
394
457
  * Return a list of an index' collections within this adapter's scope
395
458
  * @param {string} index
396
459
  * @returns {Promise.<string[]>}
397
460
  */
398
- global.kuzzle.onAsk(`core:storage:${this.scope}:collection:list`, (index) => this.cache.listCollections(index));
461
+ global.kuzzle.onAsk(`core:storage:${this.scope}:collection:list`, async (index) => {
462
+ await this.catchUpWithStorage();
463
+ return this.cache.listCollections(index);
464
+ });
399
465
  /**
400
466
  * Refresh a collection
401
467
  * @param {string} index
402
468
  * @param {string} collection
403
469
  * @returns {Promise}
404
470
  */
405
- global.kuzzle.onAsk(`core:storage:${this.scope}:collection:refresh`, (index, collection) => {
406
- this.cache.assertCollectionExists(index, collection);
471
+ global.kuzzle.onAsk(`core:storage:${this.scope}:collection:refresh`, async (index, collection) => {
472
+ await this.assertCollectionExists(index, collection);
407
473
  return this.driver.refreshCollection(index, collection);
408
474
  });
409
475
  /**
@@ -412,8 +478,8 @@ class ClientAdapter {
412
478
  * @param {string} collection
413
479
  * @returns {Promise}
414
480
  */
415
- global.kuzzle.onAsk(`core:storage:${this.scope}:collection:truncate`, (index, collection) => {
416
- this.cache.assertCollectionExists(index, collection);
481
+ global.kuzzle.onAsk(`core:storage:${this.scope}:collection:truncate`, async (index, collection) => {
482
+ await this.assertCollectionExists(index, collection);
417
483
  this.cache.assertCollectionWritable(index, collection);
418
484
  return this.driver.truncateCollection(index, collection);
419
485
  });
@@ -424,8 +490,8 @@ class ClientAdapter {
424
490
  * @param {Object} changes
425
491
  * @returns {Promise}
426
492
  */
427
- global.kuzzle.onAsk(`core:storage:${this.scope}:collection:update`, (index, collection, changes) => {
428
- this.cache.assertCollectionExists(index, collection);
493
+ global.kuzzle.onAsk(`core:storage:${this.scope}:collection:update`, async (index, collection, changes) => {
494
+ await this.assertCollectionExists(index, collection);
429
495
  this.cache.assertCollectionWritable(index, collection);
430
496
  assertCapability(this.capabilities, this.driver, "updateCollection");
431
497
  return this.driver.updateCollection(index, collection, changes);
@@ -452,12 +518,21 @@ class ClientAdapter {
452
518
  * @param {string} index
453
519
  * @return {Promise.<boolean>}
454
520
  */
455
- global.kuzzle.onAsk(`core:storage:${this.scope}:index:exist`, (index) => this.cache.hasIndex(index));
521
+ global.kuzzle.onAsk(`core:storage:${this.scope}:index:exist`, async (index) => {
522
+ if (this.cache.hasIndex(index)) {
523
+ return true;
524
+ }
525
+ await this.catchUpWithStorage();
526
+ return this.cache.hasIndex(index);
527
+ });
456
528
  /**
457
529
  * Return a list of all indexes within this adapter's scope
458
530
  * @returns {string[]}
459
531
  */
460
- global.kuzzle.onAsk(`core:storage:${this.scope}:index:list`, () => this.cache.listIndexes());
532
+ global.kuzzle.onAsk(`core:storage:${this.scope}:index:list`, async () => {
533
+ await this.catchUpWithStorage();
534
+ return this.cache.listIndexes();
535
+ });
461
536
  /**
462
537
  * Delete multiple indexes
463
538
  * @param {string[]} indexes
@@ -484,8 +559,8 @@ class ClientAdapter {
484
559
  * @param {Object} [opts] -- see Elasticsearch "import" options
485
560
  * @returns {Promise.<{ items, errors }>
486
561
  */
487
- global.kuzzle.onAsk(`core:storage:${this.scope}:document:bulk`, (index, collection, bulk, opts) => {
488
- this.cache.assertCollectionExists(index, collection);
562
+ global.kuzzle.onAsk(`core:storage:${this.scope}:document:bulk`, async (index, collection, bulk, opts) => {
563
+ await this.assertCollectionExists(index, collection);
489
564
  this.cache.assertCollectionWritable(index, collection);
490
565
  return this.driver.import(index, collection, bulk, opts);
491
566
  });
@@ -497,8 +572,8 @@ class ClientAdapter {
497
572
  * @param {Object} query -- search query
498
573
  * @return {Promise.<Number>}
499
574
  */
500
- global.kuzzle.onAsk(`core:storage:${this.scope}:document:count`, (index, collection, query) => {
501
- this.cache.assertCollectionExists(index, collection);
575
+ global.kuzzle.onAsk(`core:storage:${this.scope}:document:count`, async (index, collection, query) => {
576
+ await this.assertCollectionExists(index, collection);
502
577
  return this.driver.count(index, collection, query);
503
578
  });
504
579
  /**
@@ -510,8 +585,8 @@ class ClientAdapter {
510
585
  * @param {Object} [opts] -- see Elasticsearch "create" options
511
586
  * @returns {Promise.<{ _id, _version, _source }>}
512
587
  */
513
- global.kuzzle.onAsk(`core:storage:${this.scope}:document:create`, (index, collection, content, opts) => {
514
- this.cache.assertCollectionExists(index, collection);
588
+ global.kuzzle.onAsk(`core:storage:${this.scope}:document:create`, async (index, collection, content, opts) => {
589
+ await this.assertCollectionExists(index, collection);
515
590
  this.cache.assertCollectionWritable(index, collection);
516
591
  return this.driver.create(index, collection, content, opts);
517
592
  });
@@ -525,8 +600,8 @@ class ClientAdapter {
525
600
  * @param {Object} [opts] -- see Elasticsearch "createOrReplace" options
526
601
  * @returns {Promise.<{ _id, _version, _source, created }>}
527
602
  */
528
- global.kuzzle.onAsk(`core:storage:${this.scope}:document:createOrReplace`, (index, collection, id, content, opts) => {
529
- this.cache.assertCollectionExists(index, collection);
603
+ global.kuzzle.onAsk(`core:storage:${this.scope}:document:createOrReplace`, async (index, collection, id, content, opts) => {
604
+ await this.assertCollectionExists(index, collection);
530
605
  this.cache.assertCollectionWritable(index, collection);
531
606
  return this.driver.createOrReplace(index, collection, id, content, opts);
532
607
  });
@@ -539,8 +614,8 @@ class ClientAdapter {
539
614
  * @param {Object} [opts] -- see Elasticsearch "delete" options
540
615
  * @returns {Promise}
541
616
  */
542
- global.kuzzle.onAsk(`core:storage:${this.scope}:document:delete`, (index, collection, id, opts) => {
543
- this.cache.assertCollectionExists(index, collection);
617
+ global.kuzzle.onAsk(`core:storage:${this.scope}:document:delete`, async (index, collection, id, opts) => {
618
+ await this.assertCollectionExists(index, collection);
544
619
  this.cache.assertCollectionWritable(index, collection);
545
620
  return this.driver.delete(index, collection, id, opts);
546
621
  });
@@ -553,8 +628,8 @@ class ClientAdapter {
553
628
  * @param {Object} [opts] -- see Elasticsearch "deleteByQuery" options
554
629
  * @returns {Promise.<{ documents, total, deleted, failures: [ id, reason ] }>}
555
630
  */
556
- global.kuzzle.onAsk(`core:storage:${this.scope}:document:deleteByQuery`, (index, collection, query, opts) => {
557
- this.cache.assertCollectionExists(index, collection);
631
+ global.kuzzle.onAsk(`core:storage:${this.scope}:document:deleteByQuery`, async (index, collection, query, opts) => {
632
+ await this.assertCollectionExists(index, collection);
558
633
  this.cache.assertCollectionWritable(index, collection);
559
634
  return this.driver.deleteByQuery(index, collection, query, opts);
560
635
  });
@@ -568,8 +643,8 @@ class ClientAdapter {
568
643
  * @param {Object} [opts] -- see Elasticsearch "deleteFields" options
569
644
  * @returns {Promise.<{ _id, _version, _source }>}
570
645
  */
571
- global.kuzzle.onAsk(`core:storage:${this.scope}:document:deleteFields`, (index, collection, id, fields, opts) => {
572
- this.cache.assertCollectionExists(index, collection);
646
+ global.kuzzle.onAsk(`core:storage:${this.scope}:document:deleteFields`, async (index, collection, id, fields, opts) => {
647
+ await this.assertCollectionExists(index, collection);
573
648
  this.cache.assertCollectionWritable(index, collection);
574
649
  return this.driver.deleteFields(index, collection, id, fields, opts);
575
650
  });
@@ -581,8 +656,8 @@ class ClientAdapter {
581
656
  * @param {string} id -- document unique identifier
582
657
  * @returns {Promise.<boolean>}
583
658
  */
584
- global.kuzzle.onAsk(`core:storage:${this.scope}:document:exist`, (index, collection, id) => {
585
- this.cache.assertCollectionExists(index, collection);
659
+ global.kuzzle.onAsk(`core:storage:${this.scope}:document:exist`, async (index, collection, id) => {
660
+ await this.assertCollectionExists(index, collection);
586
661
  return this.driver.exists(index, collection, id);
587
662
  });
588
663
  /**
@@ -593,8 +668,8 @@ class ClientAdapter {
593
668
  * @param {string} id -- document unique identifier
594
669
  * @returns {Promise.<boolean>}
595
670
  */
596
- global.kuzzle.onAsk(`core:storage:${this.scope}:document:mExists`, (index, collection, ids) => {
597
- this.cache.assertCollectionExists(index, collection);
671
+ global.kuzzle.onAsk(`core:storage:${this.scope}:document:mExists`, async (index, collection, ids) => {
672
+ await this.assertCollectionExists(index, collection);
598
673
  return this.driver.mExists(index, collection, ids);
599
674
  });
600
675
  /**
@@ -605,8 +680,8 @@ class ClientAdapter {
605
680
  * @param {string} id -- document unique identifier
606
681
  * @returns {Promise.<{ _id, _version, _source }>}
607
682
  */
608
- global.kuzzle.onAsk(`core:storage:${this.scope}:document:get`, (index, collection, id) => {
609
- this.cache.assertCollectionExists(index, collection);
683
+ global.kuzzle.onAsk(`core:storage:${this.scope}:document:get`, async (index, collection, id) => {
684
+ await this.assertCollectionExists(index, collection);
610
685
  return this.driver.get(index, collection, id);
611
686
  });
612
687
  /**
@@ -624,8 +699,8 @@ class ClientAdapter {
624
699
  * @param {Object} [opts] -- see Elasticsearch "mCreate" options
625
700
  * @returns {Promise.<{ items, errors }>
626
701
  */
627
- global.kuzzle.onAsk(`core:storage:${this.scope}:document:mCreate`, (index, collection, documents, opts) => {
628
- this.cache.assertCollectionExists(index, collection);
702
+ global.kuzzle.onAsk(`core:storage:${this.scope}:document:mCreate`, async (index, collection, documents, opts) => {
703
+ await this.assertCollectionExists(index, collection);
629
704
  this.cache.assertCollectionWritable(index, collection);
630
705
  return this.driver.mCreate(index, collection, documents, opts);
631
706
  });
@@ -638,8 +713,8 @@ class ClientAdapter {
638
713
  * @param {Object} [opts] -- see Elasticsearch "mCreateOrReplace" options
639
714
  * @returns {Promise.<{ items, errors }>
640
715
  */
641
- global.kuzzle.onAsk(`core:storage:${this.scope}:document:mCreateOrReplace`, (index, collection, documents, opts) => {
642
- this.cache.assertCollectionExists(index, collection);
716
+ global.kuzzle.onAsk(`core:storage:${this.scope}:document:mCreateOrReplace`, async (index, collection, documents, opts) => {
717
+ await this.assertCollectionExists(index, collection);
643
718
  this.cache.assertCollectionWritable(index, collection);
644
719
  return this.driver.mCreateOrReplace(index, collection, documents, opts);
645
720
  });
@@ -652,8 +727,8 @@ class ClientAdapter {
652
727
  * @param {Object} [opts] -- see Elasticsearch "mDelete" options
653
728
  * @returns {Promise.<{ documents, errors }>
654
729
  */
655
- global.kuzzle.onAsk(`core:storage:${this.scope}:document:mDelete`, (index, collection, ids, opts) => {
656
- this.cache.assertCollectionExists(index, collection);
730
+ global.kuzzle.onAsk(`core:storage:${this.scope}:document:mDelete`, async (index, collection, ids, opts) => {
731
+ await this.assertCollectionExists(index, collection);
657
732
  this.cache.assertCollectionWritable(index, collection);
658
733
  return this.driver.mDelete(index, collection, ids, opts);
659
734
  });
@@ -666,8 +741,8 @@ class ClientAdapter {
666
741
  * @param {Object} [opts] -- see Elasticsearch "mReplace" options
667
742
  * @returns {Promise.<{ items, errors }>
668
743
  */
669
- global.kuzzle.onAsk(`core:storage:${this.scope}:document:mReplace`, (index, collection, documents, opts) => {
670
- this.cache.assertCollectionExists(index, collection);
744
+ global.kuzzle.onAsk(`core:storage:${this.scope}:document:mReplace`, async (index, collection, documents, opts) => {
745
+ await this.assertCollectionExists(index, collection);
671
746
  this.cache.assertCollectionWritable(index, collection);
672
747
  return this.driver.mReplace(index, collection, documents, opts);
673
748
  });
@@ -680,8 +755,8 @@ class ClientAdapter {
680
755
  * @param {Object} [opts] -- see Elasticsearch "mUpdate" options
681
756
  * @returns {Promise.<{ items, errors }>
682
757
  */
683
- global.kuzzle.onAsk(`core:storage:${this.scope}:document:mUpdate`, (index, collection, documents, opts) => {
684
- this.cache.assertCollectionExists(index, collection);
758
+ global.kuzzle.onAsk(`core:storage:${this.scope}:document:mUpdate`, async (index, collection, documents, opts) => {
759
+ await this.assertCollectionExists(index, collection);
685
760
  this.cache.assertCollectionWritable(index, collection);
686
761
  return this.driver.mUpdate(index, collection, documents, opts);
687
762
  });
@@ -695,8 +770,8 @@ class ClientAdapter {
695
770
  * @param {Object} [opts] -- see Elasticsearch "mUpsert" options
696
771
  * @returns {Promise.<{ items, errors }>
697
772
  */
698
- global.kuzzle.onAsk(`core:storage:${this.scope}:document:mUpsert`, (index, collection, documents, opts) => {
699
- this.cache.assertCollectionExists(index, collection);
773
+ global.kuzzle.onAsk(`core:storage:${this.scope}:document:mUpsert`, async (index, collection, documents, opts) => {
774
+ await this.assertCollectionExists(index, collection);
700
775
  this.cache.assertCollectionWritable(index, collection);
701
776
  return this.driver.mUpsert(index, collection, documents, opts);
702
777
  });
@@ -710,8 +785,8 @@ class ClientAdapter {
710
785
  * @param {Object} [opts] -- see Elasticsearch "mExecute" options
711
786
  * @returns {Promise.<any[]>} Array of results returned by the callback
712
787
  */
713
- global.kuzzle.onAsk(`core:storage:${this.scope}:document:mExecute`, (index, collection, query, callback, opts) => {
714
- this.cache.assertCollectionExists(index, collection);
788
+ global.kuzzle.onAsk(`core:storage:${this.scope}:document:mExecute`, async (index, collection, query, callback, opts) => {
789
+ await this.assertCollectionExists(index, collection);
715
790
  return this.driver.mExecute(index, collection, query, callback, opts);
716
791
  });
717
792
  /**
@@ -722,8 +797,8 @@ class ClientAdapter {
722
797
  * @param {string[]} ids
723
798
  * @returns {Promise.<{ items: [ _id, _source, _version ], errors }>}
724
799
  */
725
- global.kuzzle.onAsk(`core:storage:${this.scope}:document:mGet`, (index, collection, ids) => {
726
- this.cache.assertCollectionExists(index, collection);
800
+ global.kuzzle.onAsk(`core:storage:${this.scope}:document:mGet`, async (index, collection, ids) => {
801
+ await this.assertCollectionExists(index, collection);
727
802
  return this.driver.mGet(index, collection, ids);
728
803
  });
729
804
  /**
@@ -736,8 +811,8 @@ class ClientAdapter {
736
811
  * @param {Object} [opts] -- see Elasticsearch "replace" options
737
812
  * @returns {Promise.<{ _id, _version, _source }>}
738
813
  */
739
- global.kuzzle.onAsk(`core:storage:${this.scope}:document:replace`, (index, collection, id, content, opts) => {
740
- this.cache.assertCollectionExists(index, collection);
814
+ global.kuzzle.onAsk(`core:storage:${this.scope}:document:replace`, async (index, collection, id, content, opts) => {
815
+ await this.assertCollectionExists(index, collection);
741
816
  this.cache.assertCollectionWritable(index, collection);
742
817
  return this.driver.replace(index, collection, id, content, opts);
743
818
  });
@@ -758,8 +833,8 @@ class ClientAdapter {
758
833
  * @param {Object} [opts] -- see Elasticsearch "search" options
759
834
  * @returns {Promise.<{ scrollId, hits, aggregations, total }>}
760
835
  */
761
- global.kuzzle.onAsk(`core:storage:${this.scope}:document:search`, (index, collection, searchBody, opts) => {
762
- this.cache.assertCollectionExists(index, collection);
836
+ global.kuzzle.onAsk(`core:storage:${this.scope}:document:search`, async (index, collection, searchBody, opts) => {
837
+ await this.assertCollectionExists(index, collection);
763
838
  assertSearchCapabilities(this.capabilities, searchBody);
764
839
  return this.driver.search({ collection, index, searchBody }, opts);
765
840
  });
@@ -771,11 +846,14 @@ class ClientAdapter {
771
846
  * @param {Object} [opts] -- see Elasticsearch "search" options
772
847
  * @returns {Promise.<{ scrollId, hits, aggregations, total }>}
773
848
  */
774
- global.kuzzle.onAsk(`core:storage:${this.scope}:document:multiSearch`, (targets, searchBody, opts) => {
775
- for (const target of targets) {
776
- for (const collection of target.collections) {
777
- this.cache.assertCollectionExists(target.index, collection);
778
- }
849
+ global.kuzzle.onAsk(`core:storage:${this.scope}:document:multiSearch`, async (targets, searchBody, opts) => {
850
+ const searched = targets.flatMap(({ index, collections }) => collections.map((collection) => [index, collection]));
851
+ // One storage read for all of them, if any is missing from the cache
852
+ if (searched.some(([index, collection]) => !this.cache.hasCollection(index, collection))) {
853
+ await this.catchUpWithStorage();
854
+ }
855
+ for (const [index, collection] of searched) {
856
+ this.cache.assertCollectionExists(index, collection);
779
857
  }
780
858
  assertSearchCapabilities(this.capabilities, searchBody);
781
859
  return this.driver.search({ searchBody, targets }, opts);
@@ -790,8 +868,8 @@ class ClientAdapter {
790
868
  * @param {Object} [opts] -- see Elasticsearch "update" options
791
869
  * @returns {Promise.<{ _id, _version }>}
792
870
  */
793
- global.kuzzle.onAsk(`core:storage:${this.scope}:document:update`, (index, collection, id, content, opts) => {
794
- this.cache.assertCollectionExists(index, collection);
871
+ global.kuzzle.onAsk(`core:storage:${this.scope}:document:update`, async (index, collection, id, content, opts) => {
872
+ await this.assertCollectionExists(index, collection);
795
873
  this.cache.assertCollectionWritable(index, collection);
796
874
  return this.driver.update(index, collection, id, content, opts);
797
875
  });
@@ -806,8 +884,8 @@ class ClientAdapter {
806
884
  * @param {Object} [opts] -- see Elasticsearch "updateByQuery" options
807
885
  * @returns {Promise.<{ successes: [_id, _source, _status], errors: [ document, status, reason ] }>}
808
886
  */
809
- global.kuzzle.onAsk(`core:storage:${this.scope}:document:updateByQuery`, (index, collection, query, changes, opts) => {
810
- this.cache.assertCollectionExists(index, collection);
887
+ global.kuzzle.onAsk(`core:storage:${this.scope}:document:updateByQuery`, async (index, collection, query, changes, opts) => {
888
+ await this.assertCollectionExists(index, collection);
811
889
  this.cache.assertCollectionWritable(index, collection);
812
890
  assertCapability(this.capabilities, this.driver, "updateByQuery");
813
891
  return this.driver.updateByQuery(index, collection, query, changes, opts);
@@ -823,8 +901,8 @@ class ClientAdapter {
823
901
  * @param {Object} [opts] -- see Elasticsearch "updateByQuery" options
824
902
  * @returns {Promise.<{ successes: [_id, _source, _status], errors: [ document, status, reason ] }>}
825
903
  */
826
- global.kuzzle.onAsk(`core:storage:${this.scope}:bulk:updateByQuery`, (index, collection, query, changes, opts) => {
827
- this.cache.assertCollectionExists(index, collection);
904
+ global.kuzzle.onAsk(`core:storage:${this.scope}:bulk:updateByQuery`, async (index, collection, query, changes, opts) => {
905
+ await this.assertCollectionExists(index, collection);
828
906
  this.cache.assertCollectionWritable(index, collection);
829
907
  assertCapability(this.capabilities, this.driver, "bulkUpdateByQuery");
830
908
  return this.driver.bulkUpdateByQuery(index, collection, query, changes, opts);
@@ -840,8 +918,8 @@ class ClientAdapter {
840
918
  * @param {Object} [opts] -- see Elasticsearch "upsert" options
841
919
  * @returns {Promise.<{ _id, _version }>}
842
920
  */
843
- global.kuzzle.onAsk(`core:storage:${this.scope}:document:upsert`, (index, collection, id, content, opts) => {
844
- this.cache.assertCollectionExists(index, collection);
921
+ global.kuzzle.onAsk(`core:storage:${this.scope}:document:upsert`, async (index, collection, id, content, opts) => {
922
+ await this.assertCollectionExists(index, collection);
845
923
  this.cache.assertCollectionWritable(index, collection);
846
924
  return this.driver.upsert(index, collection, id, content, opts);
847
925
  });
@@ -856,8 +934,8 @@ class ClientAdapter {
856
934
  *
857
935
  * @returns {Promise.<{ dynamic, _meta, properties }>}
858
936
  */
859
- global.kuzzle.onAsk(`core:storage:${this.scope}:mappings:get`, (index, collection, opts) => {
860
- this.cache.assertCollectionExists(index, collection);
937
+ global.kuzzle.onAsk(`core:storage:${this.scope}:mappings:get`, async (index, collection, opts) => {
938
+ await this.assertCollectionExists(index, collection);
861
939
  return this.driver.getMapping(index, collection, opts);
862
940
  });
863
941
  /**
@@ -877,8 +955,8 @@ class ClientAdapter {
877
955
  * @param {Object} mappings
878
956
  * @returns {Promise.<{ dynamic, _meta, properties }>}
879
957
  */
880
- global.kuzzle.onAsk(`core:storage:${this.scope}:mappings:update`, (index, collection, mappings) => {
881
- this.cache.assertCollectionExists(index, collection);
958
+ global.kuzzle.onAsk(`core:storage:${this.scope}:mappings:update`, async (index, collection, mappings) => {
959
+ await this.assertCollectionExists(index, collection);
882
960
  this.cache.assertCollectionWritable(index, collection);
883
961
  assertCapability(this.capabilities, this.driver, "updateMapping");
884
962
  return this.driver.updateMapping(index, collection, mappings);
@@ -974,7 +1052,8 @@ class ClientAdapter {
974
1052
  throw kerror.get("api", "assert", "invalid_argument", collections, "object");
975
1053
  }
976
1054
  for (const [collection, payload] of Object.entries(collections)) {
977
- this.cache.assertCollectionExists(index, collection);
1055
+ // Checked as each collection comes, like its import below
1056
+ await this.assertCollectionExists(index, collection); // NOSONAR
978
1057
  this.cache.assertCollectionWritable(index, collection);
979
1058
  const imported = this.driver.import(index, collection, payload, {
980
1059
  refresh,
@@ -17,6 +17,12 @@ export declare class IndexCache {
17
17
  * Map<index, Set<collection>>
18
18
  */
19
19
  private readonly indexes;
20
+ /**
21
+ * How many times an index or a collection left the cache: `learn` compares
22
+ * it with the value read before storage was, to tell whether a removal landed
23
+ * during the read
24
+ */
25
+ private removalCount;
20
26
  /**
21
27
  * Collection write locks, a local copy of the cluster-wide state (see
22
28
  * `ClientAdapter`): checking a lock on every write must not cost a round
@@ -61,6 +67,22 @@ export declare class IndexCache {
61
67
  * Remove a collection from the cache
62
68
  */
63
69
  removeCollection(index: string, collection: string): void;
70
+ /**
71
+ * Removals so far, to pass to `learn` once storage has been read
72
+ */
73
+ get removals(): number;
74
+ /**
75
+ * Add the stored indexes and collections this cache misses, unless one was
76
+ * removed since `removals` was read: the read may then predate that removal,
77
+ * and would bring back what a peer deleted (kuzzle#3121).
78
+ *
79
+ * Never removes anything: unlike `replaceIndexes`, it only catches up with
80
+ * creations whose cluster broadcast has not reached this node yet.
81
+ *
82
+ * @param stored - Index name -> its collection names, as storage holds them
83
+ * @param removals - Read from `removals` before storage was
84
+ */
85
+ learn(stored: Record<string, string[]>, removals: number): void;
64
86
  /**
65
87
  * Return a copy of the cached indexes and collections, to pass to
66
88
  * `replaceIndexes` once storage has been read
@@ -84,6 +84,12 @@ class IndexCache {
84
84
  * Map<index, Set<collection>>
85
85
  */
86
86
  this.indexes = new Map();
87
+ /**
88
+ * How many times an index or a collection left the cache: `learn` compares
89
+ * it with the value read before storage was, to tell whether a removal landed
90
+ * during the read
91
+ */
92
+ this.removalCount = 0;
87
93
  /**
88
94
  * Collection write locks, a local copy of the cluster-wide state (see
89
95
  * `ClientAdapter`): checking a lock on every write must not cost a round
@@ -147,6 +153,7 @@ class IndexCache {
147
153
  */
148
154
  removeIndex(index) {
149
155
  this.indexes.delete(index);
156
+ this.removalCount++;
150
157
  }
151
158
  /**
152
159
  * Remove a collection from the cache
@@ -156,6 +163,35 @@ class IndexCache {
156
163
  if (collections) {
157
164
  collections.delete(collection);
158
165
  }
166
+ this.removalCount++;
167
+ }
168
+ /**
169
+ * Removals so far, to pass to `learn` once storage has been read
170
+ */
171
+ get removals() {
172
+ return this.removalCount;
173
+ }
174
+ /**
175
+ * Add the stored indexes and collections this cache misses, unless one was
176
+ * removed since `removals` was read: the read may then predate that removal,
177
+ * and would bring back what a peer deleted (kuzzle#3121).
178
+ *
179
+ * Never removes anything: unlike `replaceIndexes`, it only catches up with
180
+ * creations whose cluster broadcast has not reached this node yet.
181
+ *
182
+ * @param stored - Index name -> its collection names, as storage holds them
183
+ * @param removals - Read from `removals` before storage was
184
+ */
185
+ learn(stored, removals) {
186
+ if (removals !== this.removalCount) {
187
+ return;
188
+ }
189
+ for (const [index, collections] of Object.entries(stored)) {
190
+ this.addIndex(index);
191
+ for (const collection of collections) {
192
+ this.addCollection(index, collection);
193
+ }
194
+ }
159
195
  }
160
196
  /**
161
197
  * Return a copy of the cached indexes and collections, to pass to
@@ -183,6 +219,7 @@ class IndexCache {
183
219
  ...current.keys(),
184
220
  ]);
185
221
  this.indexes.clear();
222
+ this.removalCount++;
186
223
  for (const index of indexes) {
187
224
  const storedCollections = new Set(Object.hasOwn(stored, index) ? stored[index] : []);
188
225
  const was = snapshot.get(index);
package/dist/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "kuzzle",
3
3
  "author": "The Kuzzle Team <support@kuzzle.io>",
4
- "version": "2.61.0-beta.3",
4
+ "version": "2.61.0-beta.4",
5
5
  "description": "Kuzzle is an open-source solution that handles all the data management through a secured API, with a large choice of protocols.",
6
6
  "scripts": {
7
7
  "build": "rm -Rf ./dist && tsc && node -r ts-node/register/transpile-only ./bin/copy-binaries.ts",
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "kuzzle",
3
3
  "author": "The Kuzzle Team <support@kuzzle.io>",
4
- "version": "2.61.0-beta.3",
4
+ "version": "2.61.0-beta.4",
5
5
  "description": "Kuzzle is an open-source solution that handles all the data management through a secured API, with a large choice of protocols.",
6
6
  "scripts": {
7
7
  "build": "rm -Rf ./dist && tsc && node -r ts-node/register/transpile-only ./bin/copy-binaries.ts",