@koralabs/kora-labs-common 6.14.2 → 6.14.3
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.
- package/cronLock/cqlCronLock.js +22 -7
- package/package.json +1 -1
package/cronLock/cqlCronLock.js
CHANGED
|
@@ -148,26 +148,41 @@ const acquireWithExecutor = async (executor, name, offline, topology, token = cr
|
|
|
148
148
|
// The LWT timed out: it may have been applied WITH OUR TOKEN, in which case nobody (not even
|
|
149
149
|
// us) could run until the lease expired. A read at the same serial consistency completes any
|
|
150
150
|
// in-flight Paxos round, so it tells us the real owner.
|
|
151
|
-
const
|
|
152
|
-
if (owner === token)
|
|
151
|
+
const read = await readOwner(executor, key, serialConsistency);
|
|
152
|
+
if (read.owner === token)
|
|
153
153
|
return { status: 'acquired', token };
|
|
154
|
-
if (owner)
|
|
154
|
+
if (read.owner)
|
|
155
155
|
return { status: 'held' };
|
|
156
|
+
if (read.error) {
|
|
157
|
+
// Still unknown and we will not run: retract our possibly-applied proposal so it cannot
|
|
158
|
+
// block every box for the whole lease. Conditional on our token, so it never frees another's lock.
|
|
159
|
+
const retracted = await deleteIfOwner(executor, key, token, serialConsistency, consistency);
|
|
160
|
+
return { status: 'unavailable', message: `${message}; owner read failed (${read.error}); ${retracted ? 'retracted our proposal' : 'retract failed, the lease will reclaim it'}` };
|
|
161
|
+
}
|
|
156
162
|
}
|
|
157
163
|
return { status: 'unavailable', message };
|
|
158
164
|
}
|
|
159
165
|
};
|
|
160
166
|
exports.acquireWithExecutor = acquireWithExecutor;
|
|
161
|
-
/** The lock's owner as a linearizable (serial) read sees it
|
|
167
|
+
/** The lock's owner as a linearizable (serial) read sees it. */
|
|
162
168
|
const readOwner = async (executor, key, serialConsistency) => {
|
|
163
169
|
var _a;
|
|
164
170
|
try {
|
|
165
171
|
const result = await executor.execute(`SELECT owner FROM ${KEYSPACE}.${LOCK_TABLE} WHERE name = ?`, [key], { prepare: true, consistency: serialConsistency });
|
|
166
172
|
const owner = (_a = result.rows[0]) === null || _a === void 0 ? void 0 : _a.owner;
|
|
167
|
-
return typeof owner === 'string' ? owner :
|
|
173
|
+
return typeof owner === 'string' ? { owner } : {};
|
|
168
174
|
}
|
|
169
|
-
catch (
|
|
170
|
-
return
|
|
175
|
+
catch (error) {
|
|
176
|
+
return { error: error instanceof Error ? error.message : String(error) };
|
|
177
|
+
}
|
|
178
|
+
};
|
|
179
|
+
const deleteIfOwner = async (executor, key, token, serialConsistency, consistency) => {
|
|
180
|
+
try {
|
|
181
|
+
await executor.execute(`DELETE FROM ${KEYSPACE}.${LOCK_TABLE} WHERE name = ? IF owner = ?`, [key, token], { prepare: true, serialConsistency, consistency });
|
|
182
|
+
return true;
|
|
183
|
+
}
|
|
184
|
+
catch (_a) {
|
|
185
|
+
return false;
|
|
171
186
|
}
|
|
172
187
|
};
|
|
173
188
|
const acquire = async (name, offline, options = {}) => {
|