deniskuzZ commented on code in PR #6595:
URL: https://github.com/apache/hive/pull/6595#discussion_r4284518079


##########
standalone-metastore/metastore-search/src/main/java/org/apache/hive/search/metastore/MetastoreIndexer.java:
##########
@@ -0,0 +1,359 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.hive.search.metastore;
+
+import java.io.Closeable;
+import java.io.IOException;
+import java.util.List;
+import java.util.Optional;
+
+import org.apache.hadoop.conf.Configuration;
+import org.apache.hadoop.hive.common.DatabaseName;
+import org.apache.hadoop.hive.metastore.IMetaStoreClient;
+import org.apache.hadoop.hive.metastore.RetryingMetaStoreClient;
+import org.apache.hadoop.hive.metastore.api.NotificationEventRequest;
+import org.apache.hadoop.hive.metastore.leader.LeaderElection;
+import org.apache.hive.search.mapping.TableDocument;
+import org.apache.hive.search.exception.IndexNotHealthyException;
+import org.apache.hive.search.config.IndexOptions;
+import org.apache.hive.search.index.Indexer;
+import org.apache.hive.search.index.IndexManager;
+import org.apache.hive.search.index.store.IndexManifest;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public final class MetastoreIndexer implements Closeable {
+  private static final Logger LOG = 
LoggerFactory.getLogger(MetastoreIndexer.class);
+
+  private final IMetaStoreClient client;
+  private final long lastEventId;
+  private final Indexer indexer;
+  private final MetastoreCluster cluster;
+  private final MetastoreEventHandler handler;
+  private final IndexManager indexManager;
+  private final FlushIndexListener flushIndexListener;
+  private final boolean shareBootstrapFetchClient;
+
+  public MetastoreIndexer(Configuration configuration, IndexManager 
indexManager, Indexer indexer)
+      throws Exception {
+    this(configuration, indexManager, indexer,
+        RetryingMetaStoreClient.getProxy(configuration, true), false);
+  }
+
+  MetastoreIndexer(Configuration configuration, IndexManager indexManager, 
Indexer indexer,
+      IMetaStoreClient client) throws Exception {
+    this(configuration, indexManager, indexer, client, true);
+  }
+
+  MetastoreIndexer(Configuration configuration, IndexManager indexManager, 
Indexer indexer,
+      IMetaStoreClient client, boolean shareBootstrapFetchClient) throws 
Exception {
+    this.client = client;
+    this.shareBootstrapFetchClient = shareBootstrapFetchClient;
+    this.indexer = indexer;
+    this.indexManager = indexManager;
+    this.flushIndexListener = new FlushIndexListener(configuration);
+    this.handler = new MetastoreEventHandler(configuration, client);
+    this.handler.addListeners(flushIndexListener, indexManager);
+    this.cluster = new MetastoreCluster(configuration, flushIndexListener);
+    this.lastEventId = initialize();
+    this.indexManager.setNid(lastEventId);
+  }
+
+  private boolean rebuildIndex() throws Exception {
+    indexManager.resolveInterruptedRestore();
+    if (isIndexValid(indexManager.readLocalManifest().orElse(null))) {
+      return false;
+    }
+    if (isIndexValid(indexManager.readRemoteManifest().orElse(null))) {
+      if (indexManager.restoreBackup()) {
+        return false;
+      }
+      LOG.warn("Failed to restore index from remote backup; clearing partial 
local index");
+      indexManager.clearLocalIndex();
+      return true;
+    }
+    indexManager.clearLocalIndex();
+    return true;
+  }
+
+  private boolean isIndexValid(IndexManifest indexManifest)

Review Comment:
   The design doc (§3, §3.3) defines the index mapping as a versioned contract, 
which is the main value of having a mapping contract. The implementation 
doesn’t version it, though. isIndexValid() only compares the embedder name, and 
Indexer.flush() only records nid, embedder and commit_time in the commit user 
data.
   
   As a result, any of these changes will silently produce a mixed-shape index 
after an upgrade or config change, because the local or remote snapshot is 
still considered valid:
   - adding, removing or renaming a field, or changing its 
lexical/filter/stored flags;
   - changing an analyzer;
   - changing semanticSegmentMax / semanticSegmentMaxChars (old docs keep the 
old segment layout);
   - changing what goes into the segment text (SearchTextSegment.build), or the 
embedder pooling/prompt, while the model name stays the same.
   
   Suggestion:
   
   - Introduce an explicit MAPPING_VERSION constant that is bumped on any 
change in §3.3’s “mapping revision” category, and/or a fingerprint computed 
from the effective mapping (field names and flags, analyzer class, segment 
config, embedder name, pooling, dimension).
   - Write it into the commit user data in Indexer.flush() and into 
IndexManifest.
   - In isIndexValid() and waitForRemoteBackup(), treat a mismatch the same as 
an embedder mismatch: don’t restore, rebuild instead.
   - Optionally, follow §3.3 and distinguish a stored-only change (backward 
compatible, no rebuild) from a searchable or analyzer change (rebuild 
required). A version with a “compatible since” field would cover that.
   
   It would also be good to have a test that bumps the version and asserts that 
the restore path is rejected and a bootstrap runs.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to