Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

9169 idb log durability and timeout changes #9177

Merged
merged 2 commits into from
Sep 19, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 26 additions & 4 deletions src/telemetry/logging.ts
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ async function openLoggingDB() {
},
blocked(currentVersion: number, blockedVersion: number) {
console.debug("Database blocked.", { currentVersion, blockedVersion });
// This should never happen, since we immediately close connections if blocking below,
// but just in case, close the connection here so it doesn't block openLoggingDB from
// resolving
database?.close();
},
blocking(currentVersion: number, blockedVersion: number) {
// Don't block closing/upgrading the database
Expand Down Expand Up @@ -249,7 +253,9 @@ export async function clearLog(context: MessageContext = {}): Promise<void> {
const db = await openLoggingDB();

try {
const tx = db.transaction(ENTRY_OBJECT_STORE, "readwrite");
const tx = db.transaction(ENTRY_OBJECT_STORE, "readwrite", {
durability: "relaxed",
});

if (isEmpty(context)) {
await tx.store.clear();
Expand Down Expand Up @@ -278,7 +284,9 @@ export async function getLogEntries(

try {
const objectStore = db
.transaction(ENTRY_OBJECT_STORE, "readonly")
.transaction(ENTRY_OBJECT_STORE, "readonly", {
durability: "relaxed",
})
.objectStore(ENTRY_OBJECT_STORE);

let indexKey: IndexKey | undefined;
Expand Down Expand Up @@ -515,7 +523,9 @@ export async function clearModComponentDebugLogs(
const db = await openLoggingDB();

try {
const tx = db.transaction(ENTRY_OBJECT_STORE, "readwrite");
const tx = db.transaction(ENTRY_OBJECT_STORE, "readwrite", {
durability: "relaxed",
});
const index = tx.store.index("modComponentId");
for await (const cursor of index.iterate(modComponentId)) {
if (cursor.value.level === "debug" || cursor.value.level === "trace") {
Expand Down Expand Up @@ -546,15 +556,26 @@ async function _sweepLogs(): Promise<void> {
});

const db = await openLoggingDB();
const abortController = new AbortController();
// Ensure in cases where the sweep is taking too long, we abort the operation to reduce the likelihood
// of blocking other db transactions.
const timeoutId = setTimeout(abortController.abort, 30_000);

try {
const tx = db.transaction(ENTRY_OBJECT_STORE, "readwrite");
const tx = db.transaction(ENTRY_OBJECT_STORE, "readwrite", {
durability: "relaxed",
});

let deletedCount = 0;

// Ideally this would be ordered by timestamp to delete the oldest records, but timestamp is not an index.
// This might mostly "just work" if the cursor happens to iterate in insertion order
for await (const cursor of tx.store) {
if (abortController.signal.aborted) {
console.warn("Log sweep aborted due to timeout");
break;
}

await cursor.delete();
deletedCount++;

Expand All @@ -563,6 +584,7 @@ async function _sweepLogs(): Promise<void> {
}
}
} finally {
clearTimeout(timeoutId);
db.close();
}
}
Expand Down
Loading