LP1844418 Support index activation
authorBill Erickson <berickxx@gmail.com>
Mon, 3 Feb 2020 22:42:39 +0000 (17:42 -0500)
committerBill Erickson <berickxx@gmail.com>
Fri, 21 Feb 2020 21:20:33 +0000 (16:20 -0500)
Signed-off-by: Bill Erickson <berickxx@gmail.com>
Open-ILS/examples/fm_IDL.xml
Open-ILS/src/perlmods/lib/OpenILS/Elastic.pm
Open-ILS/src/support-scripts/elastic-index.pl

index b5e19c3..0a62b37 100644 (file)
@@ -12970,6 +12970,7 @@ SELECT  usr,
                oils_persist:tablename="elastic.index"
                reporter:label="Elastic Index">
                <fields oils_persist:primary="id" oils_persist:sequence="elastic.index_id_seq">
+                       <field name="id" reporter:selector="name" reporter:datatype="id"/>
                        <field reporter:label="Cluster" name="cluster" reporter:datatype="link"/>
                        <field reporter:label="Index Class" name="index_class" reporter:datatype="text"/>
                        <field reporter:label="Index Name" name="name" reporter:datatype="text"/>
index dfd501a..9462500 100644 (file)
@@ -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) = @_;
 
index e2c9753..307e7d4 100755 (executable)
@@ -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 <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 <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 <number>
                 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 = {