Skip to content

Commit

Permalink
fix(mu): delete rows from trace db based on overflow
Browse files Browse the repository at this point in the history
  • Loading branch information
jfrain99 committed Aug 21, 2024
1 parent 9ea8e3d commit 3937e67
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 19 deletions.
36 changes: 18 additions & 18 deletions servers/mu/src/domain/clients/cron.js
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,15 @@ export function updateCronProcessCursorWith ({ db }) {

export function deleteOldTracesWith ({ db }) {
return async () => {
function createQuery ({ pageSize, pageCount }) {
function createQuery ({ overflow }) {
/**
* Check if the database has grown to greater than 1GB.
* If it has, delete the least recent 10% of traces.
*/
return {
sql: `
WITH page_info AS (
SELECT
? * ? AS db_size_in_bytes
),
delete_entries AS (
SELECT COUNT(*) AS total_rows, COUNT(*) / 10 AS rows_to_delete
WITH delete_entries AS (
SELECT COUNT(*) AS total_rows, CEILING(COUNT(*) * ?) AS rows_to_delete
FROM ${TRACES_TABLE}
)
DELETE FROM ${TRACES_TABLE}
Expand All @@ -112,19 +108,23 @@ export function deleteOldTracesWith ({ db }) {
ORDER BY timestamp ASC
LIMIT (SELECT rows_to_delete FROM delete_entries)
)
AND EXISTS (
SELECT 1
FROM page_info
WHERE db_size_in_bytes > 1024 * 1024 * 1024
);
`,
parameters: [pageSize, pageCount]
parameters: [overflow]
}
}
const pageSize = await db.pragma('page_size', { simple: true }) // db.query(createPageSizeQuery())
const pageCount = await db.pragma('page_count', { simple: true }) // query(createPageCountQuery())
console.log({ pageSize, pageCount })
db.run(createQuery({ pageSize, pageCount }))
const pageSize = await db.pragma('page_size', { simple: true })
const pageCount = await db.pragma('page_count', { simple: true })
/**
* Calculate if we are over the maximum amount of bytes allocated
* to the trace database. If we are, we will remove the oldest traces
* such that we will return to below the maximum amount.
*/
const totalBytes = pageSize * pageCount
const maxBytes = 1024 * 1024 * 1024
const overflow = maxBytes / totalBytes
if (overflow >= 1) return
await db.run(createQuery({ overflow: 1 - overflow }))
db.run({ sql: 'VACUUM;', parameters: [] })
}
}

Expand Down Expand Up @@ -190,7 +190,7 @@ function startMonitoredProcessWith ({ fetch, histogram, logger, CU_URL, fetchCro
)
.fork(
e => console.log(e),
_success => { console.log('success', _success) }
_success => console.log('success', _success)
)
ct.start() // resume cron when done getting messages
}
Expand Down
2 changes: 1 addition & 1 deletion servers/mu/src/domain/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ export const createApis = async (ctx) => {
/**
* Create cron to clear out traces, each hour
*/
cron.schedule('0 * * * *', async () => {
cron.schedule('* * * * *', async () => {
await deleteOldTraces()
})

Expand Down

0 comments on commit 3937e67

Please sign in to comment.