Skip to content

Commit

Permalink
Use select-for-update to make sure board compaction is not run while …
Browse files Browse the repository at this point in the history
…loading board

No strong reason to suppose this concurrency would be harmful
but the opposite isn't proven either so better safe than sorry.
  • Loading branch information
raimohanska committed Jan 30, 2024
1 parent 7894d4d commit c213a49
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
2 changes: 1 addition & 1 deletion backend/src/board-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ with allow_lists as (
from board_access a
where a.board_id = b.id
) as allow_list
from board b
from board b for update
)
select id,
jsonb_set (content - 'accessPolicy', '{accessPolicy}', cast(case when allow_list is null then 'null' else (json_build_object('allowList', allow_list, 'publicRead', public_read, 'publicWrite', public_write)) end as jsonb)) as content
Expand Down
3 changes: 3 additions & 0 deletions backend/src/compact-history.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@ import {
verifyContinuityFromMetas,
} from "./board-store"
import { inTransaction } from "./db"
import { sleep } from "../../common/src/sleep"

export async function quickCompactBoardHistory(id: Id) {
let fallback = false
const result = inTransaction(async (client) => {
// Lock the board to prevent loading the board while compacting
await client.query("select 1 from board where id=$1 for update", [id])
const bundleMetas = await getBoardHistoryBundleMetas(client, id)
if (bundleMetas.length === 0) return
const consistent = verifyContinuityFromMetas(id, 0, bundleMetas)
Expand Down

0 comments on commit c213a49

Please sign in to comment.