kuzzle 2.61.3-beta.1 → 2.61.3-beta.2

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.
@@ -324,12 +324,11 @@ function inheritInput(target, source, data) {
324
324
  /**
325
325
  * Lock options of `auth:strategies:add` and `auth:strategies:remove`: wait
326
326
  * as long as it takes, as the deprecated `Mutex` did (no acquisition
327
- * timeout), with the same 30 s TTL — now refreshed while the strategy is
328
- * being added or removed.
327
+ * timeout). The lock is refreshed while the strategy is being added or
328
+ * removed.
329
329
  */
330
330
  const STRATEGY_LOCK = {
331
331
  retryAttempts: Number.POSITIVE_INFINITY,
332
- ttl: 30000,
333
332
  };
334
333
  /**
335
334
  * Returns a currified function of pluginsManager.registerStrategy
@@ -1067,16 +1067,27 @@ function getMethods(object) {
1067
1067
  }
1068
1068
  /**
1069
1069
  * A plugin's `init` waiting for a cluster-wide lock is the usual way to run
1070
- * out of time while the cluster starts: the message names the lock, and says
1071
- * that one left behind by a node that died is only freed when it expires
1072
- * (#3156).
1070
+ * out of time while the cluster starts: the message names the lock and who
1071
+ * holds it, and says that one left behind by a node that died is only freed
1072
+ * when it expires (#3156). A lock can also be held by another task of this
1073
+ * node, e.g. parallel collection creations.
1073
1074
  */
1074
1075
  function initTimeoutMessage(logPrefix, initTimeout, locks) {
1075
1076
  let waiting = "";
1076
1077
  if (locks.length > 0) {
1077
1078
  const plural = locks.length > 1 ? "s" : "";
1078
- const names = locks.map((key) => `"${key}"`).join(", ");
1079
- waiting = ` It was waiting for the cluster-wide lock${plural} ${names}, held by another node; a lock left by a node that stopped abruptly is only released when it expires.`;
1079
+ const remote = locks.filter((key) => !(0, distributedLock_1.isLockHeldHere)(key));
1080
+ const names = locks
1081
+ .map((key) => {
1082
+ const holder = remote.includes(key) ? "another node" : "this node";
1083
+ return `"${key}" (held by ${holder})`;
1084
+ })
1085
+ .join(", ");
1086
+ waiting = ` It was waiting for the cluster-wide lock${plural} ${names}.`;
1087
+ if (remote.length > 0) {
1088
+ waiting +=
1089
+ " A lock left by a node that stopped abruptly is only released when it expires.";
1090
+ }
1080
1091
  }
1081
1092
  return `${logPrefix} Initialization timed out after ${initTimeout}ms.${waiting} Try to increase the configuration "plugins.common.initTimeout".`;
1082
1093
  }
@@ -60,9 +60,8 @@ export declare class Store {
60
60
  * Initialize the index, and creates provided collections
61
61
  *
62
62
  * Every node creates the collections in turn, under the same lock, and
63
- * waits for it with no limit. The lock (`STORE_INIT_LOCK_TTL`) is refreshed
64
- * while the collections are created; if it is lost, this rejects with
65
- * `MutexLockLostError`.
63
+ * waits for it with no limit. The lock is refreshed while the collections
64
+ * are created; if it is lost, this rejects with `MutexLockLostError`.
66
65
  */
67
66
  init(collections?: StoreCollectionsDefinition): Promise<void>;
68
67
  /**
@@ -58,14 +58,6 @@ const kerror = __importStar(require("../../kerror"));
58
58
  const async_1 = require("../../util/async");
59
59
  const esRequest_1 = require("../../util/esRequest");
60
60
  const distributedLock_1 = require("../../util/distributedLock");
61
- /**
62
- * TTL of the `Store.init` lock, refreshed every 8 s while its holder is alive.
63
- * Plugins call `Store.init` from their `init`, which must finish within
64
- * `plugins.common.initTimeout` (10 s by default): a node that dies holding the
65
- * lock leaves it to expire, and with the 30 s default every node waiting on it
66
- * ran out of time (#3156).
67
- */
68
- const STORE_INIT_LOCK_TTL = 10000;
69
61
  /**
70
62
  * The store methods that are nothing but an ask on a
71
63
  * `core:storage:<scope>:<suffix>` event taking the index as its first
@@ -134,12 +126,11 @@ class Store {
134
126
  * Initialize the index, and creates provided collections
135
127
  *
136
128
  * Every node creates the collections in turn, under the same lock, and
137
- * waits for it with no limit. The lock (`STORE_INIT_LOCK_TTL`) is refreshed
138
- * while the collections are created; if it is lost, this rejects with
139
- * `MutexLockLostError`.
129
+ * waits for it with no limit. The lock is refreshed while the collections
130
+ * are created; if it is lost, this rejects with `MutexLockLostError`.
140
131
  */
141
132
  async init(collections = {}) {
142
- await (0, distributedLock_1.withLock)(`Store.init(${this.index})`, () => this.createCollections(collections, { indexCacheOnly: false }), { retryAttempts: Number.POSITIVE_INFINITY, ttl: STORE_INIT_LOCK_TTL });
133
+ await (0, distributedLock_1.withLock)(`Store.init(${this.index})`, () => this.createCollections(collections, { indexCacheOnly: false }), { retryAttempts: Number.POSITIVE_INFINITY });
143
134
  }
144
135
  /**
145
136
  * Creates collections with the provided mappings
@@ -1116,7 +1116,7 @@ class ClientAdapter {
1116
1116
  // Sequential by design: one index at a time under the lock
1117
1117
  await this._loadIndexMappings(index, collections, options); // NOSONAR (S9382)
1118
1118
  }
1119
- }, { retryAttempts: Number.POSITIVE_INFINITY, ttl: 60000 });
1119
+ }, { retryAttempts: Number.POSITIVE_INFINITY });
1120
1120
  }
1121
1121
  /**
1122
1122
  * Loads the mappings of an index's collections, one at a time
@@ -146,7 +146,7 @@ class InternalIndexHandler extends store_1.Store {
146
146
  }
147
147
  throw error;
148
148
  }
149
- }, { retryAttempts: Number.POSITIVE_INFINITY, ttl: 30000 });
149
+ }, { retryAttempts: Number.POSITIVE_INFINITY });
150
150
  }
151
151
  /**
152
152
  * @override
@@ -673,7 +673,6 @@ class Kuzzle extends KuzzleEventEmitter_1.default {
673
673
  // `MutexLockLostError` (the bookkeeping write stays idempotent, TD-69).
674
674
  await (0, distributedLock_1.withLock)(`backend:import:${type}`, importType, {
675
675
  retryAttempts: Number.POSITIVE_INFINITY,
676
- ttl: 60000,
677
676
  });
678
677
  };
679
678
  await importFrom(0);
@@ -13,7 +13,9 @@ export interface MutexConfig {
13
13
  */
14
14
  retryDelay?: number;
15
15
  /**
16
- * Lock TTL in milliseconds (default: 30000)
16
+ * Lock TTL in milliseconds (default: 10000). The lock is extended every
17
+ * third of it while the callback runs: the TTL only bounds how long a lock
18
+ * outlives a node that stopped extending it.
17
19
  */
18
20
  ttl?: number;
19
21
  }
@@ -62,6 +64,13 @@ export declare function trackLockWaits<T>(fn: () => Promise<T>): {
62
64
  * Internal to Kuzzle: not exported by the package.
63
65
  */
64
66
  export declare function releaseAllLocksBeforeExit(): Promise<void>;
67
+ /**
68
+ * Whether this process holds the lock on `key`, taken by `withLock`: a lock
69
+ * awaited by one task can be held by another task of the same node.
70
+ *
71
+ * Internal to Kuzzle: not exported by the package.
72
+ */
73
+ export declare function isLockHeldHere(key: string): boolean;
65
74
  /**
66
75
  * Runs `callback` while holding a distributed, reentrant lock on `key`.
67
76
  *
@@ -26,12 +26,22 @@ Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.MutexAcquireError = exports.MutexLockLostError = void 0;
27
27
  exports.trackLockWaits = trackLockWaits;
28
28
  exports.releaseAllLocksBeforeExit = releaseAllLocksBeforeExit;
29
+ exports.isLockHeldHere = isLockHeldHere;
29
30
  exports.withLock = withLock;
30
31
  const node_async_hooks_1 = require("node:async_hooks");
31
32
  const redis_semaphore_1 = require("redis-semaphore");
32
33
  const debug_1 = __importDefault(require("./debug"));
33
34
  require("../types/Global");
34
35
  const debug = (0, debug_1.default)("kuzzle:distributedLock");
36
+ /**
37
+ * Default `withLock` TTL. A node that dies holding a lock leaves it until it
38
+ * expires, and every node waiting for it waits that long: at startup, inside
39
+ * a plugin `init` that must end within `plugins.common.initTimeout` (10 s by
40
+ * default). A live holder extends it every third of the TTL, so it can stall
41
+ * for about 6.6 s before losing it, as with the former 30 s TTL extended
42
+ * every 24 s (#3156, #3207, #3208).
43
+ */
44
+ const DEFAULT_LOCK_TTL = 10000;
35
45
  /**
36
46
  * Thrown when a lock acquired by `withLock` is lost while its callback is
37
47
  * still running (e.g. TTL extension failed and another node may now hold
@@ -70,6 +80,7 @@ class ResourceMutex extends redis_semaphore_1.Mutex {
70
80
  constructor(client, key, options) {
71
81
  super(client, key, options);
72
82
  this._key = key;
83
+ this.resource = key;
73
84
  }
74
85
  async _refresh() {
75
86
  try {
@@ -157,6 +168,20 @@ async function releaseAllLocksBeforeExit() {
157
168
  // Best effort: a lock that cannot be freed still expires with its TTL.
158
169
  await Promise.allSettled([...held].map((mutex) => mutex.release()));
159
170
  }
171
+ /**
172
+ * Whether this process holds the lock on `key`, taken by `withLock`: a lock
173
+ * awaited by one task can be held by another task of the same node.
174
+ *
175
+ * Internal to Kuzzle: not exported by the package.
176
+ */
177
+ function isLockHeldHere(key) {
178
+ for (const mutex of held) {
179
+ if (mutex.resource === key) {
180
+ return true;
181
+ }
182
+ }
183
+ return false;
184
+ }
160
185
  /**
161
186
  * Lazily fetches and memoizes the internal cache's raw ioredis client.
162
187
  * Self-healing: if the very first call happens before the cache engine has
@@ -208,7 +233,7 @@ async function withLock(key, callback, config = {}) {
208
233
  if (ancestors?.has(key)) {
209
234
  return callback();
210
235
  }
211
- const { retryAttempts = 10, retryDelay = 200, ttl = 30000 } = config;
236
+ const { retryAttempts = 10, retryDelay = 200, ttl = DEFAULT_LOCK_TTL, } = config;
212
237
  const client = await getClient();
213
238
  const loss = new AbortController();
214
239
  const lockLost = new Promise((_resolve, reject) => {
@@ -222,6 +247,7 @@ async function withLock(key, callback, config = {}) {
222
247
  acquireAttemptsLimit: retryAttempts + 1,
223
248
  acquireTimeout: Number.POSITIVE_INFINITY,
224
249
  lockTimeout: ttl,
250
+ refreshInterval: Math.round(ttl / 3),
225
251
  onLockLost: (error) => {
226
252
  loss.abort(new MutexLockLostError(key, mutex.refreshError ?? error));
227
253
  },
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.3-beta.1",
4
+ "version": "2.61.3-beta.2",
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.3-beta.1",
4
+ "version": "2.61.3-beta.2",
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",