From 21cb30f60f47fa95139f36ae2b2ce0bb8b3537d8 Mon Sep 17 00:00:00 2001 From: Bill Erickson Date: Mon, 3 Feb 2020 17:42:39 -0500 Subject: [PATCH] LP1844418 Support index activation Signed-off-by: Bill Erickson --- Open-ILS/examples/fm_IDL.xml | 1 + Open-ILS/src/perlmods/lib/OpenILS/Elastic.pm | 73 ++++++++++++++++++++++++--- Open-ILS/src/support-scripts/elastic-index.pl | 14 ++++- 3 files changed, 80 insertions(+), 8 deletions(-) diff --git a/Open-ILS/examples/fm_IDL.xml b/Open-ILS/examples/fm_IDL.xml index b5e19c315d..0a62b37870 100644 --- a/Open-ILS/examples/fm_IDL.xml +++ b/Open-ILS/examples/fm_IDL.xml @@ -12970,6 +12970,7 @@ SELECT usr, oils_persist:tablename="elastic.index" reporter:label="Elastic Index"> + diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Elastic.pm b/Open-ILS/src/perlmods/lib/OpenILS/Elastic.pm index dfd501a7e3..9462500f88 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Elastic.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Elastic.pm @@ -120,7 +120,7 @@ sub get_db_rows { # load the config via cstore. sub load_config { - my $self = @_; + my ($self) = @_; my $e = new_editor(); my $cluster = $self->cluster; @@ -142,18 +142,25 @@ sub load_config { } } -sub find_or_create_index_config { +sub find_index_config { my $self = shift; my ($conf) = grep { $_->name eq $self->index_name && - $_->type eq $self->index_class + $_->index_class eq $self->index_class } @{$self->indices}; + return $conf; +} + +sub find_or_create_index_config { + my $self = shift; + + my $conf = $self->find_index_config; return $conf if $conf; $logger->info("ES creating new index configuration for ". - sprintf("cluster=%s type=%s name=%s", + sprintf("cluster=%s index_class=%s name=%s", $self->cluster, $self->index_class, $self->index_name)); my $e = new_editor(xact => 1); @@ -161,13 +168,12 @@ sub find_or_create_index_config { $conf->cluster($self->cluster); $conf->index_class($self->index_class); - $conf->index_name($self->index_name); + $conf->name($self->index_name); # Created by default with active=false and num_shards=1 unless ($e->create_elastic_index($conf)) { - $logger->error("ES failed creating index " . - $self->index_name . ": " . $e->die_event); + $logger->error("ES failed creating index ".$self->index_name); return undef; } @@ -203,6 +209,59 @@ sub connect { } } +# Activates the currently loaded index while deactivating any active +# index with the same cluster and index_class. +sub activate_index { + my ($self) = @_; + + my $index = $self->index_name; + + if (!$self->es->indices->exists(index => $index)) { + $logger->warn("ES cannot activate index '$index' which does not exist"); + return; + } + + my ($active) = grep { + $_->index_class eq $self->index_class && + $_->cluster eq $self->cluster && + $_->active eq 't' && + $_->name ne $index + } @{$self->indices}; + + my $e = new_editor(xact => 1); + + if ($active) { + $logger->info( + "ES deactivating index ".$active->name." before activating $index"); + + $active->active('f'); + unless ($e->update_elastic_index($active)) { + $logger->error("ES failed deactivating index ".$active->name); + $e->rollback; + return 0; + } + } + + my $conf = $self->find_index_config; + + if (!$conf) { + $logger->error("ES no such index to activate: $index"); + $e->rollback; + return 0; + } + + $conf->active('t'); + unless ($e->update_elastic_index($conf)) { + $logger->error("ES failed deactivating index: $index"); + $e->rollback; + return 0; + } + + $e->commit; + + return 1; +} + sub delete_index { my ($self) = @_; diff --git a/Open-ILS/src/support-scripts/elastic-index.pl b/Open-ILS/src/support-scripts/elastic-index.pl index e2c9753dff..307e7d4d33 100755 --- a/Open-ILS/src/support-scripts/elastic-index.pl +++ b/Open-ILS/src/support-scripts/elastic-index.pl @@ -14,6 +14,7 @@ my $create_index; my $delete_index; my $index_class = 'bib-search'; my $index_name; +my $activate_index; my $populate; my $index_record; my $start_record; @@ -39,6 +40,7 @@ GetOptions( 'index-name=s' => \$index_name, 'index-class=s' => \$index_class, 'index-record=s' => \$index_record, + 'activate-index' => \$activate_index, 'start-record=s' => \$start_record, 'stop-record=s' => \$stop_record, 'modified-since=s' => \$modified_since, @@ -80,9 +82,11 @@ sub help { --index-class Specifies which data set the current index manages (e.g. bib-search) + Must match a well-known index class with backing code. --index-name - Specify an index name. Defaults to --index-class. + Specify an index name. Can be any value ES index name. + Defaults to --index-class. --delete-index Delete the specified index and all of its data. @@ -90,6 +94,10 @@ sub help { --create-index Create an index whose name equals --index-name. + --activate-index + Activate the selected index while deactivating all other + indexes of the same index_class and cluster. + --batch-size Index at most this many records per batch. Default is 500. @@ -154,6 +162,10 @@ if ($create_index) { $es->create_index or die "Index create failed.\n"; } +if ($activate_index) { + $es->activate_index or die "Index activation failed.\n"; +} + if ($populate) { my $settings = { -- 2.11.0