Commit 5df15c0e authored by Alexandre's avatar Alexandre
Browse files

Refactor the search for pre-existing test to reuse

New method to specifically perform the search for SQLite and MySQL.
parent 8563602c
......@@ -22,6 +22,7 @@ requires qw(
get_test_history
get_test_params
process_unfinished_tests_give_up
recent_test_hash_id
select_unfinished_tests
test_progress
test_results
......
......@@ -165,6 +165,18 @@ sub create_db {
) or die Zonemaster::Backend::Error::Internal->new( reason => "MySQL error, could not create 'users' table", data => $dbh->errstr() );
}
sub recent_test_hash_id {
my ( $self, $age_reuse_previous_test, $fingerprint ) = @_;
my $dbh = $self->dbh;
my ( $recent_hash_id ) = $dbh->selectrow_array(
"SELECT hash_id FROM test_results WHERE fingerprint = ? AND (TO_SECONDS(NOW()) - TO_SECONDS(creation_time)) < ?",
undef, $fingerprint, $age_reuse_previous_test
);
return $recent_hash_id;
}
sub create_new_batch_job {
my ( $self, $username ) = @_;
......@@ -202,29 +214,23 @@ sub create_new_test {
my $encoded_params = $self->encode_params( $test_params );
my $undelegated = $self->undelegated ( $test_params );
my $result_id;
my $hash_id;
my $priority = $test_params->{priority};
my $queue_label = $test_params->{queue};
eval {
$dbh->do( q[LOCK TABLES test_results WRITE] );
my ( $recent_hash_id ) = $dbh->selectrow_array(
q[
SELECT hash_id FROM test_results WHERE fingerprint = ? AND (TO_SECONDS(NOW()) - TO_SECONDS(creation_time)) < ?
],
undef, $fingerprint, $seconds_between_tests_with_same_params,
);
my $recent_hash_id = $self->recent_test_hash_id( $seconds_between_tests_with_same_params, $fingerprint );
if ( $recent_hash_id ) {
# A recent entry exists, so return its id
$result_id = $recent_hash_id;
$hash_id = $recent_hash_id;
}
else {
$dbh->do(
q[
INSERT INTO test_results (batch_id, priority, queue, fingerprint, params, domain, undelegated) VALUES (?,?,?,?,?,?,?)
],
"INSERT INTO test_results (batch_id, priority, queue, fingerprint, params, domain, undelegated) VALUES (?,?,?,?,?,?,?)",
undef,
$batch_id,
$priority,
......@@ -235,15 +241,13 @@ sub create_new_test {
$undelegated,
);
my ( undef, $hash_id ) = $dbh->selectrow_array(
( undef, $hash_id ) = $dbh->selectrow_array(
"SELECT id, hash_id FROM test_results WHERE fingerprint=? ORDER BY id DESC LIMIT 1", undef, $fingerprint);
$result_id = $hash_id;
}
};
$dbh->do( q[UNLOCK TABLES] );
return $result_id;
return $hash_id;
}
sub test_progress {
......
......@@ -123,6 +123,21 @@ sub create_db {
return 1;
}
# Search for recent test result with the test same parameters, where
# "age_reuse_previous_test" gives the time limit for how old test result that
# is accepted.
sub recent_test_hash_id {
my ( $self, $age_reuse_previous_test, $fingerprint ) = @_;
my $dbh = $self->dbh;
my ( $recent_hash_id ) = $dbh->selectrow_array(
"SELECT hash_id FROM test_results WHERE fingerprint = ? AND test_start_time > DATETIME('now', ?)",
undef, $fingerprint, "-$age_reuse_previous_test seconds"
);
return $recent_hash_id;
}
sub create_new_batch_job {
my ( $self, $username ) = @_;
......@@ -159,30 +174,23 @@ sub create_new_test {
my $encoded_params = $self->encode_params( $test_params );
my $undelegated = $self->undelegated ( $test_params );
my $result_id;
my $hash_id;
my $priority = $test_params->{priority};
my $queue_label = $test_params->{queue};
# Search for recent test result with the test same parameters, where "$seconds"
# gives the time limit for how old test result that is accepted.
my ( $recent_hash_id ) = $dbh->selectrow_array(
"SELECT hash_id FROM test_results WHERE fingerprint = ? AND creation_time > DATETIME('now', ?)",
undef,
$fingerprint,
"-$seconds seconds"
);
my $recent_hash_id = $self->recent_test_hash_id( $seconds, $fingerprint );
if ( $recent_hash_id ) {
# A recent entry exists, so return its id
$result_id = $recent_hash_id;
$hash_id = $recent_hash_id;
}
else {
# The SQLite database engine does not have support to create the "hash_id" by a
# database engine trigger. "hash_id" is assumed to hold a unique hash. Uniqueness
# cannot, however, be guaranteed. Same as with the other database engines.
my $hash_id = substr(md5_hex(time().rand()), 0, 16);
$hash_id = substr(md5_hex(time().rand()), 0, 16);
my $fields = 'hash_id, batch_id, priority, queue, fingerprint, params, domain, undelegated';
$dbh->do(
......@@ -197,10 +205,9 @@ sub create_new_test {
$test_params->{domain},
$undelegated,
);
$result_id = $hash_id;
}
return $result_id; # Return test ID, either test previously run or just created.
return $hash_id; # Return test ID, either test previously run or just created.
}
sub test_progress {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment