cshuo commented on code in PR #18372:
URL: https://github.com/apache/hudi/pull/18372#discussion_r3542052753
##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/metadata/HoodieBackedTableMetadataWriter.java:
##########
@@ -432,109 +415,80 @@ private boolean initializeFromFilesystem(String
dataTableInstantTime, List<Metad
partitionInfoList = Collections.emptyList();
}
}
- Map<String, Map<String, Long>> partitionIdToAllFilesMap =
partitionInfoList.stream()
- .map(p -> {
- String partitionName =
HoodieTableMetadataUtil.getPartitionIdentifierForFilesPartition(p.getRelativePath());
- return Pair.of(partitionName, p.getFilenameToSizeMap());
- })
- .collect(Collectors.toMap(Pair::getKey, Pair::getValue));
-
- // validate that each index is eligible to be initialized
- Iterator<MetadataPartitionType> iterator = partitionsToInit.iterator();
- while (iterator.hasNext()) {
- MetadataPartitionType partitionType = iterator.next();
- if (partitionType == PARTITION_STATS &&
!dataMetaClient.getTableConfig().isTablePartitioned()) {
- // Partition stats index cannot be enabled for a non-partitioned table
- iterator.remove();
- this.enabledPartitionTypes.remove(partitionType);
- }
+ Map<String, List<FileInfo>> partitionIdToAllFilesMap =
DirectoryInfo.getPartitionToFileInfo(partitionInfoList);
+ Lazy<List<FileSliceAndPartition>> lazyLatestMergedPartitionFileSliceList =
getLazyLatestMergedPartitionFileSliceList();
+
+ // FILES partition should always be initialized first if enabled
+ if (!filesPartitionAvailable) {
+ initializeMetadataPartition(FILES,
indexerMapForPartitionsToInit.get(FILES),
+ dataTableInstantTime, partitionIdToAllFilesMap,
lazyLatestMergedPartitionFileSliceList);
+ hasPartitionsStateChanged = true;
}
- // For a fresh table, defer RLI initialization
- if (dataWriteConfig.getMetadataConfig().shouldDeferRliInitForFreshTable()
&& this.enabledPartitionTypes.contains(RECORD_INDEX)
- &&
dataMetaClient.getActiveTimeline().filterCompletedInstants().countInstants() ==
0) {
- this.enabledPartitionTypes.remove(RECORD_INDEX);
- partitionsToInit.remove(RECORD_INDEX);
- }
-
- Lazy<List<Pair<String, FileSlice>>> lazyLatestMergedPartitionFileSliceList
= getLazyLatestMergedPartitionFileSliceList();
- for (MetadataPartitionType partitionType : partitionsToInit) {
- // Find the commit timestamp to use for this partition. Each
initialization should use its own unique commit time.
- String instantTimeForPartition =
generateUniqueInstantTime(dataTableInstantTime);
- String partitionTypeName = partitionType.name();
- LOG.info("Initializing MDT partition {} at instant {}",
partitionTypeName, instantTimeForPartition);
- String relativePartitionPath;
- Pair<Integer, HoodieData<HoodieRecord>> fileGroupCountAndRecordsPair;
- Lazy<Option<HoodieSchema>> tableSchema = Lazy.lazily(() ->
HoodieTableMetadataUtil.tryResolveSchemaForTable(dataMetaClient));
- try {
- switch (partitionType) {
- case FILES:
- fileGroupCountAndRecordsPair =
initializeFilesPartition(partitionIdToAllFilesMap);
- initializeFilegroupsAndCommit(partitionType,
FILES.getPartitionPath(), fileGroupCountAndRecordsPair,
instantTimeForPartition);
- break;
- case BLOOM_FILTERS:
- fileGroupCountAndRecordsPair =
initializeBloomFiltersPartition(dataTableInstantTime, partitionIdToAllFilesMap);
- initializeFilegroupsAndCommit(partitionType,
BLOOM_FILTERS.getPartitionPath(), fileGroupCountAndRecordsPair,
instantTimeForPartition);
- break;
- case COLUMN_STATS:
- Pair<List<String>, Pair<Integer, HoodieData<HoodieRecord>>>
colStatsColumnsAndRecord =
initializeColumnStatsPartition(partitionIdToAllFilesMap, tableSchema);
- fileGroupCountAndRecordsPair = colStatsColumnsAndRecord.getValue();
- initializeFilegroupsAndCommit(partitionType,
COLUMN_STATS.getPartitionPath(), fileGroupCountAndRecordsPair,
instantTimeForPartition, colStatsColumnsAndRecord.getKey());
- break;
- case RECORD_INDEX:
- boolean isPartitionedRLI =
dataWriteConfig.isRecordLevelIndexEnabled();
-
initializeFilegroupsAndCommitToRecordIndexPartition(instantTimeForPartition,
lazyLatestMergedPartitionFileSliceList, isPartitionedRLI);
- break;
- case EXPRESSION_INDEX:
- Set<String> expressionIndexPartitionsToInit =
getExpressionIndexPartitionsToInit(partitionType,
dataWriteConfig.getMetadataConfig(), dataMetaClient);
- if (expressionIndexPartitionsToInit.size() != 1) {
- if (expressionIndexPartitionsToInit.size() > 1) {
- LOG.warn("Skipping expression index initialization as only one
expression index bootstrap at a time is supported for now. Provided: {}",
expressionIndexPartitionsToInit);
- }
- continue;
- }
- relativePartitionPath =
expressionIndexPartitionsToInit.iterator().next();
- fileGroupCountAndRecordsPair =
initializeExpressionIndexPartition(relativePartitionPath, dataTableInstantTime,
lazyLatestMergedPartitionFileSliceList, tableSchema);
- initializeFilegroupsAndCommit(partitionType,
relativePartitionPath, fileGroupCountAndRecordsPair, instantTimeForPartition);
- break;
- case PARTITION_STATS:
- // For PARTITION_STATS, COLUMN_STATS should also be enabled
- if (!dataWriteConfig.isMetadataColumnStatsIndexEnabled()) {
- LOG.debug("Skipping partition stats initialization as column
stats index is not enabled. Please enable {}",
-
HoodieMetadataConfig.ENABLE_METADATA_INDEX_COLUMN_STATS.key());
- continue;
- }
- fileGroupCountAndRecordsPair =
initializePartitionStatsIndex(lazyLatestMergedPartitionFileSliceList,
tableSchema);
- initializeFilegroupsAndCommit(partitionType,
PARTITION_STATS.getPartitionPath(), fileGroupCountAndRecordsPair,
instantTimeForPartition);
- break;
- case SECONDARY_INDEX:
- Set<String> secondaryIndexPartitionsToInit =
getSecondaryIndexPartitionsToInit(partitionType,
dataWriteConfig.getMetadataConfig(), dataMetaClient);
- if (secondaryIndexPartitionsToInit.size() != 1) {
- if (secondaryIndexPartitionsToInit.size() > 1) {
- LOG.warn("Skipping secondary index initialization as only one
secondary index bootstrap at a time is supported for now. Provided: {}",
secondaryIndexPartitionsToInit);
- }
- continue;
- }
- relativePartitionPath =
secondaryIndexPartitionsToInit.iterator().next();
- fileGroupCountAndRecordsPair =
initializeSecondaryIndexPartition(relativePartitionPath,
lazyLatestMergedPartitionFileSliceList);
- initializeFilegroupsAndCommit(partitionType,
relativePartitionPath, fileGroupCountAndRecordsPair, instantTimeForPartition);
- break;
- default:
- throw new HoodieMetadataException(String.format("Unsupported MDT
partition type: %s", partitionType));
- }
- } catch (Exception e) {
- String metricKey = partitionType.getPartitionPath() + "_" +
HoodieMetadataMetrics.BOOTSTRAP_ERR_STR;
- metrics.ifPresent(m -> m.setMetric(metricKey, 1));
- String errMsg = String.format("Bootstrap on %s partition failed for
%s",
- partitionType.getPartitionPath(),
metadataMetaClient.getBasePath());
- LOG.error(errMsg, e);
- throw new HoodieMetadataException(errMsg, e);
- }
+ for (Map.Entry<MetadataPartitionType, Indexer> entry :
+ indexerMapForPartitionsToInit.entrySet().stream()
+ .filter(e -> e.getKey() != FILES).collect(Collectors.toList())) {
+ initializeMetadataPartition(entry.getKey(), entry.getValue(),
+ dataTableInstantTime, partitionIdToAllFilesMap,
lazyLatestMergedPartitionFileSliceList);
hasPartitionsStateChanged = true;
}
return true;
}
+ @SneakyThrows
+ private void initializeMetadataPartition(
+ MetadataPartitionType partitionType,
+ Indexer indexer,
+ String dataTableInstantTime,
+ Map<String, List<FileInfo>> partitionToAllFilesMap,
+ Lazy<List<FileSliceAndPartition>>
lazyLatestMergedPartitionFileSliceList) {
+ String instantTimeForPartition =
generateUniqueInstantTime(dataTableInstantTime);
+ // initialize metadata partitions
+ List<IndexPartitionInitialization> initializationList;
+ try {
+ initializationList = indexer.buildInitialization(
+ dataTableInstantTime, instantTimeForPartition,
partitionToAllFilesMap, lazyLatestMergedPartitionFileSliceList);
+ } catch (Exception e) {
+ String metricKey = partitionType.getPartitionPath() + "_" +
HoodieMetadataMetrics.BOOTSTRAP_ERR_STR;
+ metrics.ifPresent(m -> m.setMetric(metricKey, 1));
+ String errMsg = String.format("Bootstrap on %s partition failed for %s",
+ partitionType.getPartitionPath(), metadataMetaClient.getBasePath());
+ LOG.error(errMsg, e);
+ throw new HoodieMetadataException(errMsg, e);
+ }
+
+ if (initializationList.isEmpty()) {
+ LOG.info("Skip building {} index in metadata table",
partitionType.getPartitionPath());
+ return;
+ }
+
+ ValidationUtils.checkArgument(initializationList.size() == 1,
+ "Only support the initialization of one partition per index type "
+ + "(HUDI-9358 for the feature support)");
+
+ IndexPartitionInitialization initialIndexPartitionData =
initializationList.get(0);
+ final int numFileGroup = initialIndexPartitionData.totalFileGroups();
+ String relativePartitionPath =
initialIndexPartitionData.indexPartitionName();
+ LOG.info("Initializing {} index with {} file groups",
relativePartitionPath, numFileGroup);
+
+ HoodieTimer partitionInitTimer = HoodieTimer.start();
+ clearExistingMetadataPartition(relativePartitionPath);
+ HoodieData<HoodieRecord> records = engineContext.emptyHoodieData();
+ for (DataPartitionAndRecords dataPartitionAndRecords:
initialIndexPartitionData.dataPartitionAndRecords()) {
+ initializeFileGroups(dataMetaClient, partitionType,
instantTimeForPartition,
+ dataPartitionAndRecords.numFileGroups(), relativePartitionPath,
dataPartitionAndRecords.dataPartition());
+ records = records.union(dataPartitionAndRecords.indexRecords());
+ }
+
+ bulkCommit(instantTimeForPartition, relativePartitionPath, records,
initialIndexPartitionData.indexParser());
+
+ indexer.postInitialization(metadataMetaClient, records, numFileGroup,
relativePartitionPath);
+ // initialize metadata reader
+ initMetadataReader();
+ long totalInitTime = partitionInitTimer.endTimer();
+ LOG.info("Initializing {} index in metadata table took {} in ms",
partitionType, totalInitTime);
+ }
+
/**
* Updates the list of columns to index with col stats index.
* @param columnsToIndex list of columns to index.
Review Comment:
Fixed.
##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/metadata/HoodieBackedTableMetadataWriter.java:
##########
@@ -432,109 +415,80 @@ private boolean initializeFromFilesystem(String
dataTableInstantTime, List<Metad
partitionInfoList = Collections.emptyList();
}
}
- Map<String, Map<String, Long>> partitionIdToAllFilesMap =
partitionInfoList.stream()
- .map(p -> {
- String partitionName =
HoodieTableMetadataUtil.getPartitionIdentifierForFilesPartition(p.getRelativePath());
- return Pair.of(partitionName, p.getFilenameToSizeMap());
- })
- .collect(Collectors.toMap(Pair::getKey, Pair::getValue));
-
- // validate that each index is eligible to be initialized
- Iterator<MetadataPartitionType> iterator = partitionsToInit.iterator();
- while (iterator.hasNext()) {
- MetadataPartitionType partitionType = iterator.next();
- if (partitionType == PARTITION_STATS &&
!dataMetaClient.getTableConfig().isTablePartitioned()) {
- // Partition stats index cannot be enabled for a non-partitioned table
- iterator.remove();
- this.enabledPartitionTypes.remove(partitionType);
- }
+ Map<String, List<FileInfo>> partitionIdToAllFilesMap =
DirectoryInfo.getPartitionToFileInfo(partitionInfoList);
+ Lazy<List<FileSliceAndPartition>> lazyLatestMergedPartitionFileSliceList =
getLazyLatestMergedPartitionFileSliceList();
+
+ // FILES partition should always be initialized first if enabled
+ if (!filesPartitionAvailable) {
+ initializeMetadataPartition(FILES,
indexerMapForPartitionsToInit.get(FILES),
+ dataTableInstantTime, partitionIdToAllFilesMap,
lazyLatestMergedPartitionFileSliceList);
+ hasPartitionsStateChanged = true;
}
- // For a fresh table, defer RLI initialization
- if (dataWriteConfig.getMetadataConfig().shouldDeferRliInitForFreshTable()
&& this.enabledPartitionTypes.contains(RECORD_INDEX)
- &&
dataMetaClient.getActiveTimeline().filterCompletedInstants().countInstants() ==
0) {
- this.enabledPartitionTypes.remove(RECORD_INDEX);
- partitionsToInit.remove(RECORD_INDEX);
- }
-
- Lazy<List<Pair<String, FileSlice>>> lazyLatestMergedPartitionFileSliceList
= getLazyLatestMergedPartitionFileSliceList();
- for (MetadataPartitionType partitionType : partitionsToInit) {
- // Find the commit timestamp to use for this partition. Each
initialization should use its own unique commit time.
- String instantTimeForPartition =
generateUniqueInstantTime(dataTableInstantTime);
- String partitionTypeName = partitionType.name();
- LOG.info("Initializing MDT partition {} at instant {}",
partitionTypeName, instantTimeForPartition);
- String relativePartitionPath;
- Pair<Integer, HoodieData<HoodieRecord>> fileGroupCountAndRecordsPair;
- Lazy<Option<HoodieSchema>> tableSchema = Lazy.lazily(() ->
HoodieTableMetadataUtil.tryResolveSchemaForTable(dataMetaClient));
- try {
- switch (partitionType) {
- case FILES:
- fileGroupCountAndRecordsPair =
initializeFilesPartition(partitionIdToAllFilesMap);
- initializeFilegroupsAndCommit(partitionType,
FILES.getPartitionPath(), fileGroupCountAndRecordsPair,
instantTimeForPartition);
- break;
- case BLOOM_FILTERS:
- fileGroupCountAndRecordsPair =
initializeBloomFiltersPartition(dataTableInstantTime, partitionIdToAllFilesMap);
- initializeFilegroupsAndCommit(partitionType,
BLOOM_FILTERS.getPartitionPath(), fileGroupCountAndRecordsPair,
instantTimeForPartition);
- break;
- case COLUMN_STATS:
- Pair<List<String>, Pair<Integer, HoodieData<HoodieRecord>>>
colStatsColumnsAndRecord =
initializeColumnStatsPartition(partitionIdToAllFilesMap, tableSchema);
- fileGroupCountAndRecordsPair = colStatsColumnsAndRecord.getValue();
- initializeFilegroupsAndCommit(partitionType,
COLUMN_STATS.getPartitionPath(), fileGroupCountAndRecordsPair,
instantTimeForPartition, colStatsColumnsAndRecord.getKey());
- break;
- case RECORD_INDEX:
- boolean isPartitionedRLI =
dataWriteConfig.isRecordLevelIndexEnabled();
-
initializeFilegroupsAndCommitToRecordIndexPartition(instantTimeForPartition,
lazyLatestMergedPartitionFileSliceList, isPartitionedRLI);
- break;
- case EXPRESSION_INDEX:
- Set<String> expressionIndexPartitionsToInit =
getExpressionIndexPartitionsToInit(partitionType,
dataWriteConfig.getMetadataConfig(), dataMetaClient);
- if (expressionIndexPartitionsToInit.size() != 1) {
- if (expressionIndexPartitionsToInit.size() > 1) {
- LOG.warn("Skipping expression index initialization as only one
expression index bootstrap at a time is supported for now. Provided: {}",
expressionIndexPartitionsToInit);
- }
- continue;
- }
- relativePartitionPath =
expressionIndexPartitionsToInit.iterator().next();
- fileGroupCountAndRecordsPair =
initializeExpressionIndexPartition(relativePartitionPath, dataTableInstantTime,
lazyLatestMergedPartitionFileSliceList, tableSchema);
- initializeFilegroupsAndCommit(partitionType,
relativePartitionPath, fileGroupCountAndRecordsPair, instantTimeForPartition);
- break;
- case PARTITION_STATS:
- // For PARTITION_STATS, COLUMN_STATS should also be enabled
- if (!dataWriteConfig.isMetadataColumnStatsIndexEnabled()) {
- LOG.debug("Skipping partition stats initialization as column
stats index is not enabled. Please enable {}",
-
HoodieMetadataConfig.ENABLE_METADATA_INDEX_COLUMN_STATS.key());
- continue;
- }
- fileGroupCountAndRecordsPair =
initializePartitionStatsIndex(lazyLatestMergedPartitionFileSliceList,
tableSchema);
- initializeFilegroupsAndCommit(partitionType,
PARTITION_STATS.getPartitionPath(), fileGroupCountAndRecordsPair,
instantTimeForPartition);
- break;
- case SECONDARY_INDEX:
- Set<String> secondaryIndexPartitionsToInit =
getSecondaryIndexPartitionsToInit(partitionType,
dataWriteConfig.getMetadataConfig(), dataMetaClient);
- if (secondaryIndexPartitionsToInit.size() != 1) {
- if (secondaryIndexPartitionsToInit.size() > 1) {
- LOG.warn("Skipping secondary index initialization as only one
secondary index bootstrap at a time is supported for now. Provided: {}",
secondaryIndexPartitionsToInit);
- }
- continue;
- }
- relativePartitionPath =
secondaryIndexPartitionsToInit.iterator().next();
- fileGroupCountAndRecordsPair =
initializeSecondaryIndexPartition(relativePartitionPath,
lazyLatestMergedPartitionFileSliceList);
- initializeFilegroupsAndCommit(partitionType,
relativePartitionPath, fileGroupCountAndRecordsPair, instantTimeForPartition);
- break;
- default:
- throw new HoodieMetadataException(String.format("Unsupported MDT
partition type: %s", partitionType));
- }
- } catch (Exception e) {
- String metricKey = partitionType.getPartitionPath() + "_" +
HoodieMetadataMetrics.BOOTSTRAP_ERR_STR;
- metrics.ifPresent(m -> m.setMetric(metricKey, 1));
- String errMsg = String.format("Bootstrap on %s partition failed for
%s",
- partitionType.getPartitionPath(),
metadataMetaClient.getBasePath());
- LOG.error(errMsg, e);
- throw new HoodieMetadataException(errMsg, e);
- }
+ for (Map.Entry<MetadataPartitionType, Indexer> entry :
+ indexerMapForPartitionsToInit.entrySet().stream()
+ .filter(e -> e.getKey() != FILES).collect(Collectors.toList())) {
+ initializeMetadataPartition(entry.getKey(), entry.getValue(),
+ dataTableInstantTime, partitionIdToAllFilesMap,
lazyLatestMergedPartitionFileSliceList);
hasPartitionsStateChanged = true;
}
return true;
}
+ @SneakyThrows
+ private void initializeMetadataPartition(
+ MetadataPartitionType partitionType,
+ Indexer indexer,
+ String dataTableInstantTime,
+ Map<String, List<FileInfo>> partitionToAllFilesMap,
+ Lazy<List<FileSliceAndPartition>>
lazyLatestMergedPartitionFileSliceList) {
+ String instantTimeForPartition =
generateUniqueInstantTime(dataTableInstantTime);
+ // initialize metadata partitions
+ List<IndexPartitionInitialization> initializationList;
+ try {
+ initializationList = indexer.buildInitialization(
+ dataTableInstantTime, instantTimeForPartition,
partitionToAllFilesMap, lazyLatestMergedPartitionFileSliceList);
+ } catch (Exception e) {
+ String metricKey = partitionType.getPartitionPath() + "_" +
HoodieMetadataMetrics.BOOTSTRAP_ERR_STR;
+ metrics.ifPresent(m -> m.setMetric(metricKey, 1));
+ String errMsg = String.format("Bootstrap on %s partition failed for %s",
+ partitionType.getPartitionPath(), metadataMetaClient.getBasePath());
+ LOG.error(errMsg, e);
+ throw new HoodieMetadataException(errMsg, e);
+ }
+
+ if (initializationList.isEmpty()) {
+ LOG.info("Skip building {} index in metadata table",
partitionType.getPartitionPath());
+ return;
+ }
+
+ ValidationUtils.checkArgument(initializationList.size() == 1,
+ "Only support the initialization of one partition per index type "
+ + "(HUDI-9358 for the feature support)");
+
+ IndexPartitionInitialization initialIndexPartitionData =
initializationList.get(0);
+ final int numFileGroup = initialIndexPartitionData.totalFileGroups();
+ String relativePartitionPath =
initialIndexPartitionData.indexPartitionName();
+ LOG.info("Initializing {} index with {} file groups",
relativePartitionPath, numFileGroup);
+
+ HoodieTimer partitionInitTimer = HoodieTimer.start();
+ clearExistingMetadataPartition(relativePartitionPath);
+ HoodieData<HoodieRecord> records = engineContext.emptyHoodieData();
+ for (DataPartitionAndRecords dataPartitionAndRecords:
initialIndexPartitionData.dataPartitionAndRecords()) {
+ initializeFileGroups(dataMetaClient, partitionType,
instantTimeForPartition,
+ dataPartitionAndRecords.numFileGroups(), relativePartitionPath,
dataPartitionAndRecords.dataPartition());
+ records = records.union(dataPartitionAndRecords.indexRecords());
+ }
+
+ bulkCommit(instantTimeForPartition, relativePartitionPath, records,
initialIndexPartitionData.indexParser());
+
+ indexer.postInitialization(metadataMetaClient, records, numFileGroup,
relativePartitionPath);
+ // initialize metadata reader
+ initMetadataReader();
+ long totalInitTime = partitionInitTimer.endTimer();
+ LOG.info("Initializing {} index in metadata table took {} in ms",
partitionType, totalInitTime);
Review Comment:
Fixed.
##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/metadata/HoodieBackedTableMetadataWriter.java:
##########
@@ -1692,9 +1126,15 @@ private HoodieData<HoodieRecord>
getSecondaryIndexUpdates(HoodieCommitMetadata c
@Override
public void update(HoodieCleanMetadata cleanMetadata, String instantTime) {
mayBeReinitMetadataReader();
- processAndCommit(instantTime, () ->
HoodieTableMetadataUtil.convertMetadataToRecords(engineContext,
- cleanMetadata, instantTime, dataMetaClient,
dataWriteConfig.getMetadataConfig(), enabledPartitionTypes,
- dataWriteConfig.getBloomIndexParallelism(),
Option.of(dataWriteConfig.getRecordMerger().getRecordType())));
+ processAndCommit(instantTime, new ConvertMetadataFunction() {
Review Comment:
Fixed.
##########
hudi-client/hudi-client-common/src/main/java/org/apache/hudi/metadata/index/record/BaseRecordIndexer.java:
##########
@@ -0,0 +1,484 @@
+/*
+ * 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.hudi.metadata.index.record;
+
+import org.apache.hudi.avro.model.HoodieCleanMetadata;
+import org.apache.hudi.common.config.HoodieConfig;
+import org.apache.hudi.common.config.HoodieMetadataConfig;
+import org.apache.hudi.common.data.HoodieData;
+import org.apache.hudi.common.engine.HoodieEngineContext;
+import org.apache.hudi.common.engine.HoodieReaderContext;
+import org.apache.hudi.common.engine.ReaderContextFactory;
+import org.apache.hudi.common.fs.FSUtils;
+import org.apache.hudi.common.model.FileSlice;
+import org.apache.hudi.common.model.HoodieBaseFile;
+import org.apache.hudi.common.model.HoodieCommitMetadata;
+import org.apache.hudi.common.model.HoodieDeltaWriteStat;
+import org.apache.hudi.common.model.HoodieFileFormat;
+import org.apache.hudi.common.model.HoodieReplaceCommitMetadata;
+import org.apache.hudi.common.model.HoodieWriteStat;
+import org.apache.hudi.common.model.WriteOperationType;
+import org.apache.hudi.common.table.view.HoodieTableFileSystemView;
+import org.apache.hudi.common.util.CollectionUtils;
+import org.apache.hudi.common.util.ValidationUtils;
+import org.apache.hudi.common.util.VisibleForTesting;
+import org.apache.hudi.common.util.collection.Pair;
+import org.apache.hudi.exception.HoodieException;
+import org.apache.hudi.exception.HoodieIOException;
+import org.apache.hudi.io.storage.HoodieAvroFileReader;
+import org.apache.hudi.io.storage.HoodieIOFactory;
+import org.apache.hudi.metadata.BaseFileRecordParsingUtils;
+import org.apache.hudi.metadata.HoodieBackedTableMetadata;
+import org.apache.hudi.metadata.index.model.DataPartitionAndRecords;
+import org.apache.hudi.metadata.index.model.IndexPartitionAndRecords;
+import org.apache.hudi.metadata.model.FileSliceAndPartition;
+import org.apache.hudi.common.model.HoodieRecord;
+import org.apache.hudi.common.schema.HoodieSchema;
+import org.apache.hudi.common.schema.HoodieSchemaCache;
+import org.apache.hudi.common.schema.HoodieSchemaUtils;
+import org.apache.hudi.common.table.HoodieTableMetaClient;
+import org.apache.hudi.common.table.read.HoodieFileGroupReader;
+import org.apache.hudi.common.table.timeline.HoodieInstant;
+import org.apache.hudi.common.util.Option;
+import org.apache.hudi.common.util.collection.CloseableMappingIterator;
+import org.apache.hudi.config.HoodieWriteConfig;
+import org.apache.hudi.internal.schema.InternalSchema;
+import org.apache.hudi.internal.schema.utils.SerDeHelper;
+import org.apache.hudi.metadata.HoodieMetadataPayload;
+import org.apache.hudi.metadata.HoodieTableMetadataUtil;
+import org.apache.hudi.metadata.MetadataPartitionType;
+import org.apache.hudi.metadata.index.BaseIndexer;
+import org.apache.hudi.storage.HoodieStorage;
+import org.apache.hudi.storage.HoodieStorageUtils;
+import org.apache.hudi.storage.StorageConfiguration;
+import org.apache.hudi.storage.StoragePath;
+import org.apache.hudi.util.Lazy;
+
+import lombok.extern.slf4j.Slf4j;
+
+import java.io.IOException;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Supplier;
+import java.util.stream.Collectors;
+
+import static
org.apache.hudi.common.schema.HoodieSchemaUtils.getRecordKeySchema;
+import static org.apache.hudi.common.util.ValidationUtils.checkState;
+import static
org.apache.hudi.metadata.HoodieTableMetadataUtil.readRecordKeysFromBaseFiles;
+import static org.apache.hudi.metadata.MetadataPartitionType.RECORD_INDEX;
+
+/**
+ * Base implementation for record-index.
+ */
+@Slf4j
+public abstract class BaseRecordIndexer extends BaseIndexer {
+
+ private static final int RECORD_INDEX_AVERAGE_RECORD_SIZE = 48;
+
+ protected BaseRecordIndexer(HoodieEngineContext engineContext,
HoodieWriteConfig dataTableWriteConfig, HoodieTableMetaClient
dataTableMetaClient) {
+ super(engineContext, dataTableWriteConfig, dataTableMetaClient);
+ }
+
+ protected DataPartitionAndRecords initializeRecordIndexPartition(
+ List<FileSliceAndPartition> latestMergedPartitionFileSliceList,
+ int recordIndexMaxParallelism) {
+ return initializeRecordIndexPartition(null,
latestMergedPartitionFileSliceList, recordIndexMaxParallelism);
+ }
+
+ protected DataPartitionAndRecords initializeRecordIndexPartition(
+ String dataPartition,
+ List<FileSliceAndPartition> latestMergedPartitionFileSliceList,
+ int recordIndexMaxParallelism) {
+ log.info("Initializing record index from {} file slices",
latestMergedPartitionFileSliceList.size());
+ HoodieData<HoodieRecord> records = readRecordKeysFromFileSliceSnapshot(
+ engineContext,
+ latestMergedPartitionFileSliceList,
+ recordIndexMaxParallelism,
+ this.getClass().getSimpleName(),
+ dataTableMetaClient,
+ dataTableWriteConfig);
+
+ // Initialize the file groups
+ final int fileGroupCount = estimateFileGroupCount(records);
+ log.info("Initializing record index with {} file groups.", fileGroupCount);
+ return new DataPartitionAndRecords(fileGroupCount,
Option.ofNullable(dataPartition), records);
+ }
+
+ @Override
+ public void postInitialization(HoodieTableMetaClient metadataMetaClient,
HoodieData<HoodieRecord> records, int fileGroupCount, String
relativePartitionPath) {
+ super.postInitialization(metadataMetaClient, records, fileGroupCount,
relativePartitionPath);
+ // Validate record index after commit if validation is enabled
+ if
(dataTableWriteConfig.getMetadataConfig().isRecordIndexInitializationValidationEnabled())
{
+ validateRecordIndex(records, fileGroupCount, metadataMetaClient);
+ }
+ records.unpersist();
+ }
+
+ @Override
+ public List<IndexPartitionAndRecords> buildClean(String instantTime,
HoodieCleanMetadata cleanMetadata) {
+ return Collections.emptyList();
+ }
+
+ @Override
+ public List<IndexPartitionAndRecords> buildUpdate(
+ String instantTime,
+ HoodieBackedTableMetadata tableMetadata,
+ Lazy<HoodieTableFileSystemView> lazyFileSystemView,
+ HoodieCommitMetadata commitMetadata) {
+ HoodieData<HoodieRecord> updatesFromWriteStatuses =
convertMetadataToRecordIndexRecords(engineContext, commitMetadata,
+ dataTableWriteConfig.getMetadataConfig(), dataTableMetaClient,
dataTableWriteConfig.getWritesFileIdEncoding(), instantTime);
+ HoodieData<HoodieRecord> additionalUpdates =
getRecordIndexAdditionalUpserts(updatesFromWriteStatuses, commitMetadata,
lazyFileSystemView);
+ return
Collections.singletonList(IndexPartitionAndRecords.of(RECORD_INDEX.getPartitionPath(),
updatesFromWriteStatuses.union(additionalUpdates)));
+ }
+
+ /**
+ * Validates the record index after bootstrap by comparing the expected
record count with the actual
+ * record count stored in the metadata table. The validation is performed in
a distributed manner
+ * using the engine context to count records from HFiles in parallel.
+ *
+ * @param recordIndexRecords the HoodieData containing the expected records
+ * @param fileGroupCount the expected number of file groups
+ * @param metadataMetaClient meta client for the metadata table
+ */
+ protected void validateRecordIndex(HoodieData<HoodieRecord>
recordIndexRecords, int fileGroupCount, HoodieTableMetaClient
metadataMetaClient) {
+ String partitionName =
MetadataPartitionType.RECORD_INDEX.getPartitionPath();
+ HoodieTableFileSystemView fsView =
HoodieTableMetadataUtil.getFileSystemViewForMetadataTable(metadataMetaClient);
+ try {
+ // Use merged file slices to handle cases with pending compactions
+ List<FileSlice> fileSlices =
HoodieTableMetadataUtil.getPartitionLatestMergedFileSlices(metadataMetaClient,
fsView, partitionName);
+
+ // Filter to only file slices with base files and extract their storage
paths
+ List<StoragePath> baseFilePaths = fileSlices.stream()
+ .filter(fs -> fs.getBaseFile().isPresent())
+ .map(fs -> fs.getBaseFile().get().getStoragePath())
Review Comment:
Fixed.
--
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]