-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #459 from cmoussa1/job.archive.purge
database: add the ability to remove old job records from `jobs` table
- Loading branch information
Showing
8 changed files
with
275 additions
and
0 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
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,97 @@ | ||
#!/usr/bin/env python3 | ||
############################################################### | ||
# Copyright 2024 Lawrence Livermore National Security, LLC | ||
# (c.f. AUTHORS, NOTICE.LLNS, COPYING) | ||
# | ||
# This file is part of the Flux resource manager framework. | ||
# For details, see https://github.com/flux-framework. | ||
# | ||
# SPDX-License-Identifier: LGPL-3.0 | ||
############################################################### | ||
import sqlite3 | ||
import sqlite3 | ||
import sys | ||
import time | ||
|
||
|
||
def main(): | ||
if len(sys.argv) < 2: | ||
sys.exit(f"Usage: insert_jobs DATABASE_PATH") | ||
|
||
db_uri = sys.argv[1] | ||
|
||
try: | ||
conn = sqlite3.connect(db_uri, uri=True) | ||
cur = conn.cursor() | ||
except sqlite3.OperationalError as exc: | ||
print(f"Unable to open database file: {db_uri}", file=sys.stderr) | ||
print(exc) | ||
sys.exit(1) | ||
|
||
userid = 9999 | ||
t_submit = t_run = 0 | ||
t_inactive_recent = time.time() # job that just finished | ||
t_inactive_two_weeks = time.time() - (604861 * 2) # more than 2 weeks old | ||
t_inactive_old = time.time() - (604861 * 27) # more than six months old | ||
ranks = r = jobspec = "" | ||
insert_stmt = "INSERT INTO jobs VALUES (?, ?, ?, ?, ?, ?, ?, ?)" | ||
|
||
cur.execute( | ||
insert_stmt, | ||
( | ||
"1", | ||
userid, | ||
t_submit, | ||
t_run, | ||
t_inactive_recent, | ||
ranks, | ||
r, | ||
jobspec, | ||
), | ||
) | ||
cur.execute( | ||
insert_stmt, | ||
( | ||
"2", | ||
userid, | ||
t_submit, | ||
t_run, | ||
t_inactive_two_weeks, | ||
ranks, | ||
r, | ||
jobspec, | ||
), | ||
) | ||
cur.execute( | ||
insert_stmt, | ||
( | ||
"3", | ||
userid, | ||
t_submit, | ||
t_run, | ||
t_inactive_two_weeks, | ||
ranks, | ||
r, | ||
jobspec, | ||
), | ||
) | ||
cur.execute( | ||
insert_stmt, | ||
( | ||
"4", | ||
userid, | ||
t_submit, | ||
t_run, | ||
t_inactive_old, | ||
ranks, | ||
r, | ||
jobspec, | ||
), | ||
) | ||
|
||
conn.commit() | ||
conn.close() | ||
|
||
|
||
if __name__ == "__main__": | ||
main() |
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,83 @@ | ||
#!/bin/bash | ||
|
||
test_description='test removing old job records from the flux-accounting database' | ||
|
||
. `dirname $0`/sharness.sh | ||
DB_PATH=$(pwd)/FluxAccountingTest.db | ||
QUERYCMD="flux python ${SHARNESS_TEST_SRCDIR}/scripts/query.py" | ||
INSERT_JOBS="flux python ${SHARNESS_TEST_SRCDIR}/scripts/insert_jobs.py" | ||
|
||
export TEST_UNDER_FLUX_NO_JOB_EXEC=y | ||
export TEST_UNDER_FLUX_SCHED_SIMPLE_MODE="limited=1" | ||
test_under_flux 1 job | ||
|
||
flux setattr log-stderr-level 1 | ||
|
||
# get job records from jobs table | ||
# arg1 - database path | ||
get_job_records() { | ||
local dbpath=$1 | ||
local i=0 | ||
local row_count=0 | ||
query="select count(*) from jobs;" | ||
|
||
row_count=$(${QUERYCMD} -t 100 ${dbpath} "${query}" | awk -F' = ' '{print $2}') | ||
echo $row_count | ||
} | ||
|
||
test_expect_success 'create flux-accounting DB' ' | ||
flux account -p $(pwd)/FluxAccountingTest.db create-db | ||
' | ||
|
||
test_expect_success 'start flux-accounting service' ' | ||
flux account-service -p ${DB_PATH} -t | ||
' | ||
|
||
# insert_jobs.py inserts three fake job records into the jobs table in the | ||
# flux-accounting database. Four total job records are added to the jobs table: | ||
# | ||
# Two of the jobs have a simulated time of finishing just over two weeks ago. | ||
# One of the jobs has a simulated time of finishing very recently. | ||
# One of the jobs has a simulated time of finishing over six months ago. | ||
test_expect_success 'populate DB with four job records' ' | ||
${INSERT_JOBS} ${DB_PATH} | ||
' | ||
|
||
test_expect_success 'ensure the jobs table has four records in it' ' | ||
get_job_records ${DB_PATH} > result.out && | ||
test $(cat result.out) -eq 4 | ||
' | ||
|
||
test_expect_success 'do not pass an argument to scrub-old-jobs (should remove the oldest job)' ' | ||
flux account -p ${DB_PATH} scrub-old-jobs && | ||
get_job_records ${DB_PATH} > result.out && | ||
test $(cat result.out) -eq 3 | ||
' | ||
|
||
# Passing 0 for num_weeks is saying "Remove all records older than 0 weeks | ||
# old," or rather, remove all jobs in the table. | ||
test_expect_success 'if we pass 0 for num_weeks, all jobs will be removed' ' | ||
flux account scrub-old-jobs 0 && | ||
get_job_records ${DB_PATH} > result.out && | ||
test $(cat result.out) -eq 0 | ||
' | ||
|
||
# If num_weeks == 2, all jobs that have finished more than 2 weeks ago will be | ||
# removed. In our testsuite, that should leave just the job that finished | ||
# "recently". | ||
test_expect_success 'only remove job records older than 2 weeks old' ' | ||
${INSERT_JOBS} ${DB_PATH} && | ||
flux account scrub-old-jobs 2 && | ||
get_job_records ${DB_PATH} > result.out && | ||
test $(cat result.out) -eq 1 | ||
' | ||
|
||
test_expect_success 'remove flux-accounting DB' ' | ||
rm $(pwd)/FluxAccountingTest.db | ||
' | ||
|
||
test_expect_success 'shut down flux-accounting service' ' | ||
flux python -c "import flux; flux.Flux().rpc(\"accounting.shutdown_service\").get()" | ||
' | ||
|
||
test_done |