-
Notifications
You must be signed in to change notification settings - Fork 10
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
database: add the ability to remove old job records from jobs
table
#459
Merged
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think a test for the default (no args == 26 weeks) is missing? Maybe should stick one more test record in there for something > 6 months old.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
also, probably should cover the path argument too.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good point, thanks. I just pushed up a test that does both - specifying a path to the DB as well as the default for
scrub-old-jobs
(which should remove a simulated job that "finished" over six months ago).