[HUDI-4200] Fixing sorting of keys fetched from metadata table (#5773)
- Key fetched from metadata table especially from base file reader is not sorted. and hence may result in throwing NPE (key prefix search) or unnecessary seeks to starting of Hfile (full key look ups). Fixing the same in this patch. This is not an issue with log blocks, since sorting is taking care within HoodieHfileDataBlock. - Commit where the sorting was mistakenly reverted [HUDI-3760] Adding capability to fetch Metadata Records by prefix #5208
This commit is contained in:
committed by
GitHub
parent
4f5cad8029
commit
f85cd9b16d
@@ -259,11 +259,9 @@ public class HoodieHFileReader<R extends IndexedRecord> implements HoodieFileRea
|
||||
return Collections.emptyIterator();
|
||||
}
|
||||
} else if (val == -1) {
|
||||
// If scanner is aleady on the top of hfile. avoid trigger seekTo again.
|
||||
Option<Cell> headerCell = Option.fromJavaOptional(scanner.getReader().getFirstKey());
|
||||
if (headerCell.isPresent() && !headerCell.get().equals(scanner.getCell())) {
|
||||
scanner.seekTo();
|
||||
}
|
||||
// Whenever val == -1 HFile reader will place the pointer right before the first record. We have to advance it to the first record
|
||||
// of the file to validate whether it matches our search criteria
|
||||
scanner.seekTo();
|
||||
}
|
||||
|
||||
class KeyPrefixIterator implements Iterator<GenericRecord> {
|
||||
|
||||
@@ -144,6 +144,10 @@ public class HoodieBackedTableMetadata extends BaseTableMetadata {
|
||||
@Override
|
||||
public HoodieData<HoodieRecord<HoodieMetadataPayload>> getRecordsByKeyPrefixes(List<String> keyPrefixes,
|
||||
String partitionName) {
|
||||
// Sort the columns so that keys are looked up in order
|
||||
List<String> sortedkeyPrefixes = new ArrayList<>(keyPrefixes);
|
||||
Collections.sort(sortedkeyPrefixes);
|
||||
|
||||
// NOTE: Since we partition records to a particular file-group by full key, we will have
|
||||
// to scan all file-groups for all key-prefixes as each of these might contain some
|
||||
// records matching the key-prefix
|
||||
@@ -171,17 +175,17 @@ public class HoodieBackedTableMetadata extends BaseTableMetadata {
|
||||
boolean fullKeys = false;
|
||||
|
||||
Map<String, Option<HoodieRecord<HoodieMetadataPayload>>> logRecords =
|
||||
readLogRecords(logRecordScanner, keyPrefixes, fullKeys, timings);
|
||||
readLogRecords(logRecordScanner, sortedkeyPrefixes, fullKeys, timings);
|
||||
|
||||
List<Pair<String, Option<HoodieRecord<HoodieMetadataPayload>>>> mergedRecords =
|
||||
readFromBaseAndMergeWithLogRecords(baseFileReader, keyPrefixes, fullKeys, logRecords, timings, partitionName);
|
||||
readFromBaseAndMergeWithLogRecords(baseFileReader, sortedkeyPrefixes, fullKeys, logRecords, timings, partitionName);
|
||||
|
||||
LOG.debug(String.format("Metadata read for %s keys took [baseFileRead, logMerge] %s ms",
|
||||
keyPrefixes.size(), timings));
|
||||
sortedkeyPrefixes.size(), timings));
|
||||
|
||||
return mergedRecords.iterator();
|
||||
} catch (IOException ioe) {
|
||||
throw new HoodieIOException("Error merging records from metadata table for " + keyPrefixes.size() + " key : ", ioe);
|
||||
throw new HoodieIOException("Error merging records from metadata table for " + sortedkeyPrefixes.size() + " key : ", ioe);
|
||||
} finally {
|
||||
closeReader(readers);
|
||||
}
|
||||
@@ -194,7 +198,10 @@ public class HoodieBackedTableMetadata extends BaseTableMetadata {
|
||||
@Override
|
||||
public List<Pair<String, Option<HoodieRecord<HoodieMetadataPayload>>>> getRecordsByKeys(List<String> keys,
|
||||
String partitionName) {
|
||||
Map<Pair<String, FileSlice>, List<String>> partitionFileSliceToKeysMap = getPartitionFileSliceToKeysMapping(partitionName, keys);
|
||||
// Sort the columns so that keys are looked up in order
|
||||
List<String> sortedKeys = new ArrayList<>(keys);
|
||||
Collections.sort(sortedKeys);
|
||||
Map<Pair<String, FileSlice>, List<String>> partitionFileSliceToKeysMap = getPartitionFileSliceToKeysMapping(partitionName, sortedKeys);
|
||||
List<Pair<String, Option<HoodieRecord<HoodieMetadataPayload>>>> result = new ArrayList<>();
|
||||
AtomicInteger fileSlicesKeysCount = new AtomicInteger();
|
||||
partitionFileSliceToKeysMap.forEach((partitionFileSlicePair, fileSliceKeys) -> {
|
||||
@@ -219,7 +226,7 @@ public class HoodieBackedTableMetadata extends BaseTableMetadata {
|
||||
fileSliceKeys.size(), timings));
|
||||
fileSlicesKeysCount.addAndGet(fileSliceKeys.size());
|
||||
} catch (IOException ioe) {
|
||||
throw new HoodieIOException("Error merging records from metadata table for " + keys.size() + " key : ", ioe);
|
||||
throw new HoodieIOException("Error merging records from metadata table for " + sortedKeys.size() + " key : ", ioe);
|
||||
} finally {
|
||||
if (!reuse) {
|
||||
close(Pair.of(partitionFileSlicePair.getLeft(), partitionFileSlicePair.getRight().getFileId()));
|
||||
|
||||
Reference in New Issue
Block a user