-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
cratedb-wtf: Add recorder for outcomes of
info
and job-info
- Loading branch information
Showing
5 changed files
with
83 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
# Copyright (c) 2023-2024, Crate.io Inc. | ||
# Distributed under the terms of the AGPLv3 license, see LICENSE. | ||
import logging | ||
import threading | ||
import time | ||
|
||
import sqlalchemy as sa | ||
|
||
from cratedb_toolkit.util import DatabaseAdapter | ||
from cratedb_toolkit.wtf.core import InfoContainer, JobInfoContainer | ||
|
||
logger = logging.getLogger(__name__) | ||
|
||
|
||
class InfoRecorder: | ||
""" | ||
Record complete outcomes of `info` and `job-info`. | ||
""" | ||
|
||
clusterinfo_table = "ext.clusterinfo" | ||
jobinfo_table = "ext.jobinfo" | ||
interval_seconds = 10 | ||
|
||
def __init__(self, adapter: DatabaseAdapter, scrub: bool = False): | ||
self.adapter = adapter | ||
self.scrub = scrub | ||
|
||
def record_once(self): | ||
logger.info("Recording information snapshot") | ||
clusterinfo_sample = InfoContainer(adapter=self.adapter, scrub=self.scrub) | ||
jobinfo_sample = JobInfoContainer(adapter=self.adapter, scrub=self.scrub) | ||
|
||
for table, sample in ((self.clusterinfo_table, clusterinfo_sample), (self.jobinfo_table, jobinfo_sample)): | ||
self.adapter.connection.execute( | ||
sa.text( | ||
f""" | ||
CREATE TABLE IF NOT EXISTS {table} | ||
(time TIMESTAMP DEFAULT NOW(), info OBJECT) | ||
""" | ||
) | ||
) | ||
self.adapter.connection.execute( | ||
sa.text(f"INSERT INTO {table} (info) VALUES (:info)"), {"info": sample.to_dict()["data"]} # noqa: S608 | ||
) | ||
|
||
def record_forever(self): | ||
logger.info(f"Starting to record information snapshot each {self.interval_seconds} seconds") | ||
thread = threading.Thread(target=self.do_record_forever) | ||
thread.start() | ||
|
||
def do_record_forever(self): | ||
while True: | ||
try: | ||
self.record_once() | ||
except Exception: | ||
logger.exception("Failed to record information snapshot") | ||
time.sleep(self.interval_seconds) | ||