Skip to content

Commit c6c0683

Browse files
committed
Improved EC logging related to SQLite housekeeping
SUP-25629 Change-Id: I14fd4bd683d3f06eec4e1a53a0ddb87b689c7ee0
1 parent 9941f7c commit c6c0683

File tree

1 file changed

+35
-12
lines changed

1 file changed

+35
-12
lines changed

cmk/ec/history_sqlite.py

Lines changed: 35 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,13 @@
1010
import time
1111
from collections.abc import Iterable, Sequence
1212
from dataclasses import dataclass
13-
from datetime import timedelta
13+
from datetime import datetime, timedelta
1414
from logging import Logger
1515
from pathlib import Path
1616
from typing import Final, Literal
1717

18+
from cmk.utils.log import VERBOSE
19+
1820
from .config import Config
1921
from .event import Event
2022
from .history import History, HistoryWhat
@@ -267,23 +269,44 @@ def housekeeping(self) -> None:
267269
And performs a vacuum to shrink the database file.
268270
"""
269271
now = time.time()
270-
if now - self._last_housekeeping > self._config["sqlite_housekeeping_interval"]:
271-
delta = now - timedelta(days=self._config["history_lifetime"]).total_seconds()
272-
with self.conn as connection:
273-
cur = connection.cursor()
274-
cur.execute("DELETE FROM history WHERE time <= ?;", (delta,))
275-
# should be executed outside of the transaction
276-
self._vacuum()
277-
self._last_housekeeping = now
272+
if now - self._last_housekeeping <= self._config["sqlite_housekeeping_interval"]:
273+
return
274+
delta = now - timedelta(days=self._config["history_lifetime"]).total_seconds()
275+
self._logger.log(
276+
VERBOSE,
277+
"SQLite history housekeeping: deleting events before %s",
278+
datetime.fromtimestamp(delta).isoformat(),
279+
)
280+
with self.conn as connection:
281+
cur = connection.cursor()
282+
cur.execute("DELETE FROM history WHERE time <= ?;", (delta,))
283+
# should be executed outside of the transaction
284+
self._vacuum()
285+
self._last_housekeeping = now
278286

279287
def _vacuum(self) -> None:
280-
"""Run VACUUM command only if the free pages in DB are greater than 50 Mb."""
288+
"""Run VACUUM command only if the free pages in DB are greater than the configured limit"""
281289
with self.conn as connection:
282290
freelist_count = connection.execute("PRAGMA freelist_count").fetchone()[0]
283291
freelist_size = freelist_count * self._page_size
284292

285-
if freelist_size > self._config["sqlite_freelist_size"]:
286-
self.conn.execute("VACUUM;")
293+
max_freelist_size = self._config["sqlite_freelist_size"]
294+
if freelist_size <= max_freelist_size:
295+
return
296+
self._logger.log(
297+
VERBOSE,
298+
"SQLite's freelist size of %d bytes is larger than the configured limit of %d bytes, cleanup needed",
299+
freelist_size,
300+
max_freelist_size,
301+
)
302+
self._logger.log(
303+
VERBOSE,
304+
"running VACUUM on event console history %s",
305+
f"at {self._settings.database}"
306+
if isinstance(self._settings.database, Path)
307+
else "in-memory DB",
308+
)
309+
self.conn.execute("VACUUM;")
287310

288311
def close(self) -> None:
289312
"""Explicitly close the connection to the sqlite database.

0 commit comments

Comments
 (0)