dengzhhu653 commented on code in PR #6595: URL: https://github.com/apache/hive/pull/6595#discussion_r4320761558
########## standalone-metastore/metastore-search/src/main/java/org/apache/hive/search/metastore/BootstrapIndexer.java: ########## @@ -0,0 +1,291 @@ +/* + * 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.IOException; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.ArrayBlockingQueue; +import java.util.concurrent.BlockingQueue; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.TimeUnit; +import java.util.concurrent.atomic.AtomicLong; +import java.util.concurrent.atomic.AtomicReference; + +import org.apache.hadoop.conf.Configuration; +import org.apache.hadoop.hive.metastore.IMetaStoreClient; +import org.apache.hadoop.hive.metastore.RetryingMetaStoreClient; +import org.apache.hadoop.hive.metastore.api.Table; +import org.apache.hive.search.exception.IndexIOException; +import org.apache.hive.search.index.IndexManager; +import org.apache.hive.search.mapping.TableDocument; +import org.apache.hive.search.config.IndexOptions; +import org.apache.hive.search.index.Indexer; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public final class BootstrapIndexer { + private static final Logger LOG = LoggerFactory.getLogger(BootstrapIndexer.class); + private static final List<TableDocument> END_OF_STREAM = List.of(); + + private final Configuration configuration; + private final IndexManager indexManager; + private final IndexOptions indexConfig; + private final Indexer indexer; + private final IMetaStoreClient client; + private final boolean shareFetchClient; + private Progress progress; + + /** Package-private for unit tests: fetch workers reuse the injected client. */ + BootstrapIndexer(IndexManager indexManager, + Indexer indexer, + IMetaStoreClient client, + boolean shareFetchClient) { + this.indexManager = indexManager; + this.configuration = indexManager.mapping().configuration(); + this.indexConfig = new IndexOptions(configuration); + this.indexer = indexer; + this.client = client; + this.shareFetchClient = shareFetchClient; + } + + void run(long notificationId) throws Exception { + long start = System.currentTimeMillis(); + List<String> databases = client.getAllDatabases(); + BatchPlan plan = planBatches(databases); + LOG.info("Bootstrap indexer planned {} table(s) in {} batch(es) across {} database(s)", + plan.plannedTableCount(), plan.batches().size(), databases.size()); + + BlockingQueue<List<TableDocument>> indexQueue = + new ArrayBlockingQueue<>(indexConfig.getBootstrapQueueDepth()); + AtomicReference<Exception> failure = new AtomicReference<>(); + AtomicLong indexedTables = new AtomicLong(); + CountDownLatch fetchDone = new CountDownLatch(plan.batches().size()); + progress = new Progress(indexedTables, plan.plannedTableCount, System.currentTimeMillis()); + indexManager.setBootstrapIndexer(this); + + Thread indexThread = startIndexConsumer(indexQueue, failure, indexedTables); + try (ExecutorService tableFetcher = Executors.newFixedThreadPool(indexConfig.getBootstrapFetchThreads(), + r -> { + Thread thread = new Thread(r, "Index-Bootstrap-Fetch"); + thread.setDaemon(true); + return thread; + })) { + for (TableBatch batch : plan.batches()) { + tableFetcher.submit(() -> fetchBatch(batch, indexQueue, failure, fetchDone)); + } + awaitFetchCompletion(fetchDone, failure); + indexQueue.put(END_OF_STREAM); + indexThread.join(); + rethrowFailure(failure); + indexer.flush(notificationId, true); + indexer.syncBackup(); + long elapsed = System.currentTimeMillis() - start; + LOG.info("Built index for {} tables in {} batch(es) over {}ms", + indexer.writer().getDocStats().numDocs, plan.batches().size(), elapsed); + } catch (InterruptedException e) { + Thread.currentThread().interrupt(); + throw new IndexIOException("Bootstrap indexing interrupted", e); + } finally { + indexThread.interrupt(); + indexThread.join(TimeUnit.SECONDS.toMillis(30)); + } + } + + private BatchPlan planBatches(List<String> databases) throws Exception { + int batchSize = indexConfig.getBootstrapBatchSize(); + List<TableBatch> planned = new ArrayList<>(); + int plannedTableCount = 0; + for (String db : databases) { + List<String> tableNames = client.getAllTables(db); + plannedTableCount += tableNames.size(); + for (int idx = 0; idx < tableNames.size(); idx += batchSize) { + int end = Math.min(idx + batchSize, tableNames.size()); + planned.add(new TableBatch(db, List.copyOf(tableNames.subList(idx, end)))); + } + } + return new BatchPlan(planned, plannedTableCount); + } + + private void fetchBatch(TableBatch batch, + BlockingQueue<List<TableDocument>> indexQueue, + AtomicReference<Exception> failure, + CountDownLatch fetchDone) { + try { + if (failure.get() != null) { + return; + } + if (shareFetchClient) { + fetchBatch(batch, indexQueue, failure, client); + return; + } + try (IMetaStoreClient fetchClient = RetryingMetaStoreClient.getProxy(configuration, true)) { + fetchBatch(batch, indexQueue, failure, fetchClient); + } + } catch (Exception e) { + recordFailure(failure, e); + } finally { + fetchDone.countDown(); + } + } + + private void fetchBatch(TableBatch batch, + BlockingQueue<List<TableDocument>> indexQueue, + AtomicReference<Exception> failure, + IMetaStoreClient client) throws Exception { + if (failure.get() != null) { + return; + } + List<Table> tables = + client.getTableObjectsByName(batch.database(), batch.tableNames()); + List<TableDocument> documents = new ArrayList<>(tables.size()); + for (Table table : tables) { + documents.add(MetastoreTableMapper.fromTable(table, indexManager.mapping())); + } + indexQueue.put(documents); Review Comment: The consumer will record the indexing failure, the `awaitFetchCompletion(fetchDone, failure)` sees the exception, and stops the bootstrap indexing early, this will trigger a shutdown message to the `tableFetcher`. -- 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]
