pi-cache-optimizer 2.10.0 → 2.10.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/index.ts +79 -10
- package/package.json +1 -1
package/index.ts
CHANGED
|
@@ -1008,8 +1008,18 @@ export default function (pi: ExtensionAPI) {
|
|
|
1008
1008
|
// Shards folded into the archive are counted there. Ignore their raw file
|
|
1009
1009
|
// (a crash may leave it behind) and this process's in-memory retired copy.
|
|
1010
1010
|
for (const id of archive.absorbedIds) retiredShards.delete(id);
|
|
1011
|
-
|
|
1012
|
-
|
|
1011
|
+
// A retired copy gives way to its file once the file is at least as new
|
|
1012
|
+
// (its closing write landed). An older file (the id's last active write,
|
|
1013
|
+
// when the closing write failed or is still queued) is ignored instead:
|
|
1014
|
+
// it may hold fewer counters than the retired copy.
|
|
1015
|
+
const persistedShards = rawShards.filter((shard) => {
|
|
1016
|
+
if (archive.absorbedIds.has(shard.instanceId)) return false;
|
|
1017
|
+
const retired = retiredShards.get(shard.instanceId);
|
|
1018
|
+
if (!retired) return true;
|
|
1019
|
+
if (shard.lifecycle.updatedAt < retired.lifecycle.updatedAt) return false;
|
|
1020
|
+
retiredShards.delete(shard.instanceId);
|
|
1021
|
+
return true;
|
|
1022
|
+
});
|
|
1013
1023
|
// This process's own file is not even read by the tiered index (its
|
|
1014
1024
|
// in-memory copy, the trailing shard, replaces it); the kill-switch path
|
|
1015
1025
|
// reads it and drops it here. If a day rollover changed the id while the
|
|
@@ -1350,19 +1360,18 @@ export default function (pi: ExtensionAPI) {
|
|
|
1350
1360
|
clearTimeout(persistTimer);
|
|
1351
1361
|
persistTimer = null;
|
|
1352
1362
|
}
|
|
1363
|
+
// Adopt other processes' epoch resets before the snapshot below.
|
|
1364
|
+
await refreshShardAggregate();
|
|
1365
|
+
// A session_start on this instance may have retired the shard (and
|
|
1366
|
+
// moved to today's day) while that refresh ran: nothing left to roll.
|
|
1367
|
+
if (shardDay === day) return;
|
|
1353
1368
|
// Preserve yesterday under its original UUID/day before clearing daily
|
|
1354
1369
|
// counters. Each day owns a new shard so total never includes yesterday.
|
|
1355
|
-
|
|
1356
|
-
await persistCacheStats(ctx, "closed");
|
|
1357
|
-
retiredShards.set(instanceId, buildCurrentStatsShard("closed"));
|
|
1358
|
-
instanceId = randomUUID();
|
|
1359
|
-
instanceShardPath = join(SHARD_FILES_DIR, `${instanceId}.json`);
|
|
1360
|
-
persistedOwnShards = [];
|
|
1361
|
-
shardDay = day;
|
|
1362
|
-
shardCreatedAt = Date.now();
|
|
1370
|
+
const closing = retireCurrentShard(day, ctx);
|
|
1363
1371
|
cacheStatsProcessByModel = {};
|
|
1364
1372
|
cacheStatsLegacyFamily = emptyAllCacheStats();
|
|
1365
1373
|
lastStatusText = undefined;
|
|
1374
|
+
await closing;
|
|
1366
1375
|
await persistCacheStats(ctx);
|
|
1367
1376
|
await refreshShardAggregate();
|
|
1368
1377
|
})();
|
|
@@ -1370,6 +1379,65 @@ export default function (pi: ExtensionAPI) {
|
|
|
1370
1379
|
finally { rolloverPromise = undefined; }
|
|
1371
1380
|
}
|
|
1372
1381
|
|
|
1382
|
+
/**
|
|
1383
|
+
* Retire the current shard: snapshot it closed, keep that snapshot as its
|
|
1384
|
+
* retired copy, continue under a new id on `day`, and queue the snapshot's
|
|
1385
|
+
* write (a failure only warns: the retired copy keeps counting it). The
|
|
1386
|
+
* written and the retired copy are one object, so once its write lands
|
|
1387
|
+
* the file (same updatedAt) replaces the retired copy. Synchronous up to
|
|
1388
|
+
* the queued write, so usage recorded after this goes to the new id.
|
|
1389
|
+
*/
|
|
1390
|
+
function retireCurrentShard(day: string, ctx?: ExtensionContext): Promise<void> {
|
|
1391
|
+
if (persistTimer !== null) {
|
|
1392
|
+
clearTimeout(persistTimer);
|
|
1393
|
+
persistTimer = null;
|
|
1394
|
+
}
|
|
1395
|
+
const closed = buildCurrentStatsShard("closed");
|
|
1396
|
+
const closedPath = instanceShardPath;
|
|
1397
|
+
retiredShards.set(instanceId, closed);
|
|
1398
|
+
instanceId = randomUUID();
|
|
1399
|
+
instanceShardPath = join(SHARD_FILES_DIR, `${instanceId}.json`);
|
|
1400
|
+
persistedOwnShards = [];
|
|
1401
|
+
shardCreatedAt = Date.now();
|
|
1402
|
+
shardDay = day;
|
|
1403
|
+
return enqueuePersist(async () => {
|
|
1404
|
+
try {
|
|
1405
|
+
await writeStatsShardV7(closedPath, closed);
|
|
1406
|
+
} catch (error) {
|
|
1407
|
+
console.warn(`${LOG_PREFIX}: failed to persist a retired stats shard`, error);
|
|
1408
|
+
if (!persistenceWarningShown) {
|
|
1409
|
+
persistenceWarningShown = true;
|
|
1410
|
+
ctx?.ui.notify(
|
|
1411
|
+
`${LOG_PREFIX}: failed to persist footer stats; using in-memory stats for this process.`,
|
|
1412
|
+
"warning",
|
|
1413
|
+
);
|
|
1414
|
+
}
|
|
1415
|
+
}
|
|
1416
|
+
});
|
|
1417
|
+
}
|
|
1418
|
+
|
|
1419
|
+
/**
|
|
1420
|
+
* session_start calls this instance has handled. Pi 1.3 builds a new
|
|
1421
|
+
* extension instance for every session (new/resume/fork/reload), but a
|
|
1422
|
+
* host that keeps one instance across sessions sends it a second
|
|
1423
|
+
* session_start, which clears the in-memory counters.
|
|
1424
|
+
*/
|
|
1425
|
+
let sessionStartsHandled = 0;
|
|
1426
|
+
|
|
1427
|
+
/**
|
|
1428
|
+
* Before a second session_start on the same instance clears the counters,
|
|
1429
|
+
* retire the shard holding them and continue under a new id, as the day
|
|
1430
|
+
* rollover does. Without this, the next persist rewrites the old id with
|
|
1431
|
+
* the cleared counters and the earlier session's usage for the day is lost
|
|
1432
|
+
* (the shutdown flush closed it, but the id was reused). See
|
|
1433
|
+
* retireCurrentShard.
|
|
1434
|
+
*/
|
|
1435
|
+
function retireShardForSessionStart(): void {
|
|
1436
|
+
// The retired shard keeps its own day; the new id starts on today's, so
|
|
1437
|
+
// the restore does not roll over again into an empty extra shard.
|
|
1438
|
+
void retireCurrentShard(currentLocalDay());
|
|
1439
|
+
}
|
|
1440
|
+
|
|
1373
1441
|
/**
|
|
1374
1442
|
* Synchronous part of a session restore: reset in-memory state immediately
|
|
1375
1443
|
* at session_start, so usage recorded before the background disk restore
|
|
@@ -1571,6 +1639,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
1571
1639
|
refreshScheduler.open();
|
|
1572
1640
|
latestStatsCtx = ctx;
|
|
1573
1641
|
const tui = ctx.mode === "tui";
|
|
1642
|
+
if (sessionStartsHandled++ > 0) retireShardForSessionStart();
|
|
1574
1643
|
resetStatsForSessionStart(event.reason, ctx);
|
|
1575
1644
|
const previousRestore = statsRestorePromise;
|
|
1576
1645
|
statsRestorePromise = previousRestore
|
package/package.json
CHANGED