From ad315ce893a77435567acf85295e0ad1063af994 Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Mon, 3 Jun 2024 10:55:45 -0400 Subject: [PATCH] Bug 28833: Speed up holds queue builder via parallel processing The holds queue builder can take a very long time to run on systems with many holds. For example, a partner with 124,784 unfilled ( not found ) holds, is taking about 64 minutes to run. If we run that same number of holds in 5 parallel chunks ( splitting the number of records as evenly as possible, but *not* taking into account the holds per bib ), it takes 21.5 minutes. If we use 10 loops, it takes less then 14 minutes. Test Plan: 0) Install the Perl library Parallel::ForkManager 1) Generate a huge number of holds ( a few thousand at the minimum ) 2) Run the holds queue builder, use the `time` utility to track how much time it took to run 3) Set HoldsQueueParallelLoopsCount to 10 4) Repeat step 2, note the improvement in speed 5) Experiment with other values for HoldsQueueParallelLoopsCount 6) prove t/db_dependent/HoldsQueue.t --- C4/HoldsQueue.pm | 67 +++++++++++++++++------- debian/templates/koha-conf-site.xml.in | 4 ++ etc/koha-conf.xml | 4 ++ misc/cronjobs/holds/build_holds_queue.pl | 3 +- 4 files changed, 59 insertions(+), 19 deletions(-) diff --git a/C4/HoldsQueue.pm b/C4/HoldsQueue.pm index 957d40b9912..119f143ce68 100644 --- a/C4/HoldsQueue.pm +++ b/C4/HoldsQueue.pm @@ -30,8 +30,10 @@ use Koha::Items; use Koha::Libraries; use Koha::Patrons; -use List::Util qw( shuffle ); use List::MoreUtils qw( any ); +use List::Util qw( shuffle ); +use POSIX qw(ceil); +use Parallel::ForkManager; our (@ISA, @EXPORT_OK); BEGIN { @@ -166,16 +168,13 @@ Top level function that turns reserves into tmp_holdsqueue and hold_fill_targets =cut sub CreateQueue { + my $loops = shift || 1; + my $dbh = C4::Context->dbh; $dbh->do("DELETE FROM tmp_holdsqueue"); # clear the old table for new info $dbh->do("DELETE FROM hold_fill_targets"); - my $total_bibs = 0; - my $total_requests = 0; - my $total_available_items = 0; - my $num_items_mapped = 0; - my $branches_to_use; my $transport_cost_matrix; my $use_transport_cost_matrix = C4::Context->preference("UseTransportCostMatrix"); @@ -191,9 +190,42 @@ sub CreateQueue { my $bibs_with_pending_requests = GetBibsWithPendingHoldRequests(); - foreach my $biblionumber (@$bibs_with_pending_requests) { + # Split the list of bibs into chunks to run in parallel + my @chunks; + if ( $loops > 1 ) { + my $i = 0; + while (@$bibs_with_pending_requests) { + push( @{ $chunks[$i] }, pop(@$bibs_with_pending_requests) ); + + $i++; + $i = 0 if $i >= $loops; + } + + my $pm = Parallel::ForkManager->new($loops); + + DATA_LOOP: + foreach my $chunk (@chunks) { + my $pid = $pm->start and next DATA_LOOP; + _ProcessBiblios( $chunk, $branches_to_use, $transport_cost_matrix ); + $pm->finish; + } - $total_bibs++; + $pm->wait_all_children; + } else { + _ProcessBiblios( $bibs_with_pending_requests, $branches_to_use, $transport_cost_matrix ); + } +} + +=head2 _ProcessBiblios + +=cut + +sub _ProcessBiblios { + my $bibs_with_pending_requests = shift; + my $branches_to_use = shift; + my $transport_cost_matrix = shift; + + foreach my $biblionumber (@$bibs_with_pending_requests) { my $result = update_queue_for_biblio( { biblio_id => $biblionumber, @@ -201,10 +233,6 @@ sub CreateQueue { transport_cost_matrix => $transport_cost_matrix, } ); - - $total_requests += $result->{requests}; - $total_available_items += $result->{available_items}; - $num_items_mapped += $result->{mapped_items}; } } @@ -220,12 +248,15 @@ that have one or more unfilled hold requests. sub GetBibsWithPendingHoldRequests { my $dbh = C4::Context->dbh; - my $bib_query = "SELECT DISTINCT biblionumber - FROM reserves - WHERE found IS NULL - AND priority > 0 - AND reservedate <= CURRENT_DATE() - AND suspend = 0 + my $bib_query = "SELECT biblionumber, + COUNT(biblionumber) AS holds_count + FROM reserves + WHERE found IS NULL + AND priority > 0 + AND reservedate <= CURRENT_DATE() + AND suspend = 0 + GROUP BY biblionumber + ORDER BY biblionumber DESC "; my $sth = $dbh->prepare($bib_query); diff --git a/debian/templates/koha-conf-site.xml.in b/debian/templates/koha-conf-site.xml.in index bb53ce3c83b..3d2cd2f3b1f 100644 --- a/debian/templates/koha-conf-site.xml.in +++ b/debian/templates/koha-conf-site.xml.in @@ -443,6 +443,10 @@ __END_SRU_PUBLICSERVER__ --> + + 1 + + __SMTP_HOST__ __SMTP_PORT__ diff --git a/etc/koha-conf.xml b/etc/koha-conf.xml index 0272c110a9e..f3715d75084 100644 --- a/etc/koha-conf.xml +++ b/etc/koha-conf.xml @@ -255,6 +255,10 @@ --> + + 1 + + __SMTP_HOST__ __SMTP_PORT__ diff --git a/misc/cronjobs/holds/build_holds_queue.pl b/misc/cronjobs/holds/build_holds_queue.pl index 76c61a8e943..1b6bc650830 100755 --- a/misc/cronjobs/holds/build_holds_queue.pl +++ b/misc/cronjobs/holds/build_holds_queue.pl @@ -77,6 +77,7 @@ if ( $rthq && !$force ) { cronlogaction( { info => $command_line_options } ); -CreateQueue(); +my $loops = C4::Context->config('holds_queue_builder')->{parallel_loops_count}; +CreateQueue($loops); cronlogaction( { action => 'End', info => "COMPLETED" } ); -- 2.30.2