Replace SELECT 1 warmup with full database file read to warm OS page cache

Reading the entire DB file into OS page cache eliminates the 8-14s
cold-start penalty on the first ORM query (users/templates/api_keys).
Previous SELECT 1 only warmed the connection pool, not the file cache.
This commit is contained in:
YueGuobin 2026-06-15 23:43:41 +08:00
parent 5502b8f8e5
commit 3c41dea48f
No known key found for this signature in database

View File

@ -99,14 +99,17 @@ async def connect_to_db(app: FastAPI) -> None:
return row[0] if row else "unknown" return row[0] if row else "unknown"
wal_mode = await _verify_conn.run_sync(_check_wal) wal_mode = await _verify_conn.run_sync(_check_wal)
log.info(f"SQLite journal mode: {wal_mode} {'✅' if wal_mode and wal_mode.upper() == 'WAL' else '❌ will cause database contention'}") log.info(f"SQLite journal mode: {wal_mode} {'✅' if wal_mode and wal_mode.upper() == 'WAL' else '❌ will cause database contention'}")
# Warm up the connection pool and ORM cache so the first API request # Warm up the OS page cache by reading the entire database file.
# doesn't pay the cold-start penalty (observed: 6-8s first DB query) # The first API query suffers 8-14s cold-start penalty when the file
async with engine.connect() as _warmup: # isn't in cache (SQLite + OS page cache combined).
def _warmup_query(conn): try:
from sqlalchemy import text db_file_size = os.path.getsize(db_path)
conn.execute(text("SELECT 1")) with open(db_path, "rb") as f:
await _warmup.run_sync(_warmup_query) while f.read(1024 * 1024): # 1MB chunks
log.info("Database connection pool warmed up") pass
log.info(f"Database warmed up ({db_file_size / 1024:.0f} KB)")
except OSError as e:
log.warning(f"Could not warm up database file: {e}")
alembic_cfg = config.Config() alembic_cfg = config.Config()
alembic_cfg.set_main_option("script_location", "gns3server:db_migrations") alembic_cfg.set_main_option("script_location", "gns3server:db_migrations")
#alembic_cfg.set_main_option('sqlalchemy.url', db_url) #alembic_cfg.set_main_option('sqlalchemy.url', db_url)