Skip to content

Commit

Permalink
Use SELECT ... FOR UPDATE row-level locking on checkpoint during se…
Browse files Browse the repository at this point in the history
…quencing
  • Loading branch information
roger2hk committed Jul 25, 2024
1 parent a36a173 commit cd38ea8
Showing 1 changed file with 9 additions and 8 deletions.
17 changes: 9 additions & 8 deletions storage/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ import (
)

const (
selectCheckpointByIDSQL = "SELECT `note` FROM `Checkpoint` WHERE `id` = ?"
replaceCheckpointSQL = "REPLACE INTO `Checkpoint` (`id`, `note`) VALUES (?, ?)"
selectSubtreeByLevelAndIndexSQL = "SELECT `nodes` FROM `Subtree` WHERE `level` = ? AND `index` = ?"
replaceSubtreeSQL = "REPLACE INTO `Subtree` (`level`, `index`, `nodes`) VALUES (?, ?, ?)"
selectTiledLeavesSQL = "SELECT `data` FROM `TiledLeaves` WHERE `tile_index` = ?"
replaceTiledLeavesSQL = "REPLACE INTO `TiledLeaves` (`tile_index`, `data`) VALUES (?, ?)"
selectCheckpointByIDSQL = "SELECT `note` FROM `Checkpoint` WHERE `id` = ?"
selectCheckpointByIDForUpdateSQL = selectCheckpointByIDSQL + " FOR UPDATE"
replaceCheckpointSQL = "REPLACE INTO `Checkpoint` (`id`, `note`) VALUES (?, ?)"
selectSubtreeByLevelAndIndexSQL = "SELECT `nodes` FROM `Subtree` WHERE `level` = ? AND `index` = ?"
replaceSubtreeSQL = "REPLACE INTO `Subtree` (`level`, `index`, `nodes`) VALUES (?, ?, ?)"
selectTiledLeavesSQL = "SELECT `data` FROM `TiledLeaves` WHERE `tile_index` = ?"
replaceTiledLeavesSQL = "REPLACE INTO `TiledLeaves` (`tile_index`, `data`) VALUES (?, ?)"

checkpointID = 0
defaultEntryBundleSize = 256
Expand Down Expand Up @@ -220,9 +221,9 @@ func (s *Storage) sequenceBatch(ctx context.Context, entries []*tessera.Entry) e
}
}()

// Get tree size from checkpoint.
// Get tree size from checkpoint. Note that "SELECT ... FOR UPDATE" is used for row-level locking.
// TODO(#21): Optimize how we get the tree size without parsing and verifying the checkpoints every time.
row := tx.QueryRowContext(ctx, selectCheckpointByIDSQL, checkpointID)
row := tx.QueryRowContext(ctx, selectCheckpointByIDForUpdateSQL, checkpointID)
if err := row.Err(); err != nil {
return err
}
Expand Down

0 comments on commit cd38ea8

Please sign in to comment.