@empiricalrun/playwright-utils 0.71.0 → 0.71.1
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/CHANGELOG.md +7 -0
- package/dist/generated/api-worker-openapi.d.ts +304 -173
- package/dist/generated/api-worker-openapi.d.ts.map +1 -1
- package/dist/test/cross-shard-locks.d.ts +1 -1
- package/dist/test/cross-shard-locks.d.ts.map +1 -1
- package/dist/test/cross-shard-locks.js +71 -25
- package/package.json +1 -1
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"cross-shard-locks.d.ts","sourceRoot":"","sources":["../../src/test/cross-shard-locks.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,6CAA6C,CAAC;AACrF,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAEjD,OAAO,EAAE,KAAK,QAAQ,EAA6B,MAAM,OAAO,CAAC;AAGjE,KAAK,MAAM,GAAG,IAAI,CAAC,QAAQ,EAAE,KAAK,GAAG,SAAS,GAAG,QAAQ,CAAC,CAAC;
|
|
1
|
+
{"version":3,"file":"cross-shard-locks.d.ts","sourceRoot":"","sources":["../../src/test/cross-shard-locks.ts"],"names":[],"mappings":"AAGA,OAAO,KAAK,EAAE,iBAAiB,EAAE,MAAM,6CAA6C,CAAC;AACrF,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,kBAAkB,CAAC;AAEjD,OAAO,EAAE,KAAK,QAAQ,EAA6B,MAAM,OAAO,CAAC;AAGjE,KAAK,MAAM,GAAG,IAAI,CAAC,QAAQ,EAAE,KAAK,GAAG,SAAS,GAAG,QAAQ,CAAC,CAAC;AAM3D,eAAO,MAAM,kBAAkB,KAAK,CAAC;AAkBrC,wBAAgB,YAAY,CAC1B,MAAM,EAAE,MAAM,EACd,KAAK,GAAE,iBACa,GACnB,MAAM,EAAE,CAEV;AAED,wBAAgB,OAAO,CAAC,KAAK,EAAE,MAAM,EAAE,IAAI,EAAE,MAAM,GAAG,MAAM,CAI3D;AAmCD,wBAAsB,SAAS,CAAC,EAC9B,EAAE,EACF,KAAK,EACL,KAAK,EACL,MAAM,EACN,QAAQ,EACR,GAAc,EACd,KAAiE,GAClE,EAAE;IACD,EAAE,EAAE,MAAM,CAAC;IACX,KAAK,EAAE,MAAM,CAAC;IACd,KAAK,EAAE,MAAM,EAAE,CAAC;IAChB,MAAM,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAChC,QAAQ,EAAE,CAAC,UAAU,EAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,WAAW,EAAE,MAAM,CAAA;KAAE,KAAK,IAAI,CAAC;IACtE,GAAG,CAAC,EAAE,MAAM,MAAM,CAAC;IACnB,KAAK,CAAC,EAAE,CAAC,EAAE,EAAE,MAAM,KAAK,OAAO,CAAC,IAAI,CAAC,CAAC;CACvC,GAAG,OAAO,CAAC,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,CA2E/B;AAED,wBAAsB,mBAAmB,CACvC,QAAQ,EAAE,QAAQ,EAClB,QAAQ,EAAE,MAAM,MAAM,GACrB,OAAO,CAAC,MAAM,OAAO,CAAC,IAAI,CAAC,CAAC,CAmB9B"}
|
|
@@ -37,15 +37,26 @@ function lockKey(runId, name) {
|
|
|
37
37
|
const digest = (0, node_crypto_1.createHash)("sha256").update(name).digest("hex").slice(0, 12);
|
|
38
38
|
return `pw-lock:${runId}:${readable}:${digest}`;
|
|
39
39
|
}
|
|
40
|
-
|
|
40
|
+
function ownVersionAfterConflict(error, lease) {
|
|
41
|
+
if (!(error instanceof kv_1.KvPreconditionFailedError) || !error.current) {
|
|
42
|
+
return null;
|
|
43
|
+
}
|
|
44
|
+
const value = error.current.value;
|
|
45
|
+
return value?.lease === lease ? error.current.version : null;
|
|
46
|
+
}
|
|
47
|
+
async function acquire(kv, key, value, sleep) {
|
|
41
48
|
for (let attempt = 0;; attempt++) {
|
|
42
49
|
try {
|
|
43
|
-
const entry = await kv.set(key,
|
|
50
|
+
const entry = await kv.set(key, value, exports.LOCK_LEASE_SECONDS, {
|
|
44
51
|
ifAbsent: true,
|
|
45
52
|
});
|
|
46
53
|
return entry.version;
|
|
47
54
|
}
|
|
48
55
|
catch (error) {
|
|
56
|
+
const ownVersion = ownVersionAfterConflict(error, value.lease);
|
|
57
|
+
if (ownVersion !== null) {
|
|
58
|
+
return ownVersion;
|
|
59
|
+
}
|
|
49
60
|
if (!(error instanceof kv_1.KvPreconditionFailedError)) {
|
|
50
61
|
throw error;
|
|
51
62
|
}
|
|
@@ -54,39 +65,74 @@ async function acquire(kv, key, holder, sleep) {
|
|
|
54
65
|
}
|
|
55
66
|
}
|
|
56
67
|
async function holdLocks({ kv, runId, names, holder, annotate, now = Date.now, sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms)), }) {
|
|
68
|
+
const value = { ...holder, lease: (0, node_crypto_1.randomUUID)() };
|
|
57
69
|
const held = [];
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
type: "lock",
|
|
65
|
-
description: `${name}: waited ${Math.round((now() - startedAt) / 1000)}s across shards`,
|
|
66
|
-
});
|
|
67
|
-
}
|
|
68
|
-
const leaseLost = (lock) => annotate({ type: "lock", description: `${lock.name}: lease lost` });
|
|
69
|
-
let renewing = Promise.resolve();
|
|
70
|
-
const renewal = setInterval(() => {
|
|
71
|
-
renewing = Promise.allSettled(held.map((lock) => kv
|
|
72
|
-
.refresh(lock.key, exports.LOCK_LEASE_SECONDS, { ifVersion: lock.version })
|
|
73
|
-
.then((entry) => {
|
|
70
|
+
const note = (lock, what) => annotate({ type: "lock", description: `${lock.name}: ${what}` });
|
|
71
|
+
const renew = async (lock) => {
|
|
72
|
+
try {
|
|
73
|
+
const entry = await kv.refresh(lock.key, exports.LOCK_LEASE_SECONDS, {
|
|
74
|
+
ifVersion: lock.version,
|
|
75
|
+
});
|
|
74
76
|
if (entry) {
|
|
75
77
|
lock.version = entry.version;
|
|
78
|
+
return;
|
|
79
|
+
}
|
|
80
|
+
}
|
|
81
|
+
catch (error) {
|
|
82
|
+
const ownVersion = ownVersionAfterConflict(error, value.lease);
|
|
83
|
+
if (ownVersion !== null) {
|
|
84
|
+
lock.version = ownVersion;
|
|
85
|
+
return;
|
|
76
86
|
}
|
|
77
|
-
|
|
78
|
-
|
|
87
|
+
if (!(error instanceof kv_1.KvPreconditionFailedError)) {
|
|
88
|
+
note(lock, `lease renewal failed, retrying in ${RENEW_EVERY_MS / 1000}s`);
|
|
89
|
+
return;
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
lock.lost = true;
|
|
93
|
+
note(lock, "lease lost");
|
|
94
|
+
};
|
|
95
|
+
const releaseLock = async (lock) => {
|
|
96
|
+
if (lock.lost) {
|
|
97
|
+
return;
|
|
98
|
+
}
|
|
99
|
+
try {
|
|
100
|
+
await kv.delete(lock.key, { ifVersion: lock.version });
|
|
101
|
+
}
|
|
102
|
+
catch (error) {
|
|
103
|
+
const ownVersion = ownVersionAfterConflict(error, value.lease);
|
|
104
|
+
if (ownVersion !== null) {
|
|
105
|
+
await kv.delete(lock.key, { ifVersion: ownVersion });
|
|
79
106
|
}
|
|
80
|
-
}
|
|
107
|
+
}
|
|
108
|
+
};
|
|
109
|
+
let renewing = Promise.resolve();
|
|
110
|
+
const renewal = setInterval(() => {
|
|
111
|
+
renewing = Promise.allSettled(held.filter((lock) => !lock.lost).map(renew));
|
|
81
112
|
}, RENEW_EVERY_MS);
|
|
82
113
|
renewal.unref();
|
|
83
|
-
|
|
114
|
+
const release = async () => {
|
|
84
115
|
clearInterval(renewal);
|
|
85
116
|
await renewing;
|
|
86
|
-
await Promise.allSettled(held
|
|
87
|
-
.reverse()
|
|
88
|
-
.map((lock) => kv.delete(lock.key, { ifVersion: lock.version })));
|
|
117
|
+
await Promise.allSettled([...held].reverse().map(releaseLock));
|
|
89
118
|
};
|
|
119
|
+
try {
|
|
120
|
+
for (const name of names) {
|
|
121
|
+
const key = lockKey(runId, name);
|
|
122
|
+
const startedAt = now();
|
|
123
|
+
const version = await acquire(kv, key, value, sleep);
|
|
124
|
+
held.push({ name, key, version });
|
|
125
|
+
annotate({
|
|
126
|
+
type: "lock",
|
|
127
|
+
description: `${name}: waited ${Math.round((now() - startedAt) / 1000)}s across shards`,
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
catch (error) {
|
|
132
|
+
await release();
|
|
133
|
+
throw error;
|
|
134
|
+
}
|
|
135
|
+
return release;
|
|
90
136
|
}
|
|
91
137
|
async function holdCrossShardLocks(testInfo, createKv) {
|
|
92
138
|
const names = lockNamesFor(testInfo.testId);
|