Skip to content

Commit a1416b2

Browse files
committed
[HUDI-3123] review fix
1 parent 1c11fcf commit a1416b2

12 files changed

Lines changed: 183 additions & 212 deletions

File tree

hudi-client/hudi-client-common/src/main/java/org/apache/hudi/client/utils/LazyIterableIterator.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,9 @@ public LazyIterableIterator(Iterator<I> in) {
4545
/**
4646
* Called once, before any elements are processed.
4747
*/
48-
protected abstract void start();
48+
protected void start() {
49+
50+
}
4951

5052
/**
5153
* Block computation to be overwritten by sub classes.
@@ -55,7 +57,9 @@ public LazyIterableIterator(Iterator<I> in) {
5557
/**
5658
* Called once, after all elements are processed.
5759
*/
58-
protected abstract void end();
60+
protected void end() {
61+
62+
}
5963

6064
//////////////////
6165
// iterable implementation

hudi-client/hudi-client-common/src/main/java/org/apache/hudi/index/bucket/BucketIdentifier.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@
3131
import java.util.stream.Collectors;
3232

3333
public class BucketIdentifier implements Serializable {
34-
// compatible with the spark bucket name
34+
// Compatible with the spark bucket name
3535
private static final Pattern BUCKET_NAME = Pattern.compile(".*_(\\d+)(?:\\..*)?$");
3636

3737
public static int getBucketId(HoodieRecord record, String indexKeyFields, int numBuckets) {
@@ -45,7 +45,7 @@ public static int getBucketId(HoodieKey hoodieKey, String indexKeyFields, int nu
4545
protected static List<String> getHashKeys(HoodieKey hoodieKey, String indexKeyFields) {
4646
List<String> hashKeys;
4747
if (!hoodieKey.getRecordKey().contains(":")) {
48-
hashKeyFields = Collections.singletonList(hoodieKey.getRecordKey());
48+
hashKeys = Collections.singletonList(hoodieKey.getRecordKey());
4949
} else {
5050
Map<String, String> recordKeyPairs = Arrays.stream(hoodieKey.getRecordKey().split(","))
5151
.map(p -> p.split(":"))
@@ -57,7 +57,7 @@ protected static List<String> getHashKeys(HoodieKey hoodieKey, String indexKeyFi
5757
return hashKeys;
5858
}
5959

60-
// only for test
60+
// Only for test
6161
public static int getBucketId(List<String> hashKeyFields, int numBuckets) {
6262
return hashKeyFields.hashCode() % numBuckets;
6363
}

hudi-client/hudi-client-common/src/main/java/org/apache/hudi/index/bucket/ConsistentBucketIdentifier.java

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,18 @@ public class ConsistentBucketIdentifier extends BucketIdentifier {
3838
*/
3939
private final HoodieConsistentHashingMetadata metadata;
4040
/**
41-
* in-memory structure to speed up rang mapping (hashing value -> hashing node)
41+
* In-memory structure to speed up ring mapping (hashing value -> hashing node)
4242
*/
43-
private TreeMap<Integer, ConsistentHashingNode> ring;
43+
private final TreeMap<Integer, ConsistentHashingNode> ring;
4444
/**
45-
* mapping from fileId -> hashing node
45+
* Mapping from fileId -> hashing node
4646
*/
47-
private Map<String, ConsistentHashingNode> fileIdToBucket;
47+
private final Map<String, ConsistentHashingNode> fileIdToBucket;
4848

4949
public ConsistentBucketIdentifier(HoodieConsistentHashingMetadata metadata) {
5050
this.metadata = metadata;
51+
this.fileIdToBucket = new HashMap<>();
52+
this.ring = new TreeMap<>();
5153
initialize();
5254
}
5355

@@ -82,7 +84,7 @@ protected ConsistentHashingNode getBucket(List<String> hashKeys) {
8284
for (int i = 0; i < hashKeys.size(); ++i) {
8385
hashValue = HashID.getXXHash32(hashKeys.get(i), hashValue);
8486
}
85-
return getBucket(hashValue & HoodieConsistentHashingMetadata.MAX_HASH_VALUE);
87+
return getBucket(hashValue & HoodieConsistentHashingMetadata.HASH_VALUE_MASK);
8688
}
8789

8890
protected ConsistentHashingNode getBucket(int hashValue) {
@@ -93,15 +95,13 @@ protected ConsistentHashingNode getBucket(int hashValue) {
9395
/**
9496
* Initialize necessary data structure to facilitate bucket identifying.
9597
* Specifically, we construct:
96-
* - a in-memory tree (ring) to speed up range mapping searching.
97-
* - a hash table (fileIdToBucket) to allow lookup of bucket using fileId.
98+
* - An in-memory tree (ring) to speed up range mapping searching.
99+
* - A hash table (fileIdToBucket) to allow lookup of bucket using fileId.
98100
*/
99101
private void initialize() {
100-
this.fileIdToBucket = new HashMap<>();
101-
this.ring = new TreeMap<>();
102102
for (ConsistentHashingNode p : metadata.getNodes()) {
103103
ring.put(p.getValue(), p);
104-
// one bucket has only one file group, so append 0 directly
104+
// One bucket has only one file group, so append 0 directly
105105
fileIdToBucket.put(FSUtils.createNewFileId(p.getFileIdPfx(), 0), p);
106106
}
107107
}

hudi-client/hudi-client-common/src/main/java/org/apache/hudi/index/bucket/HoodieBucketIndex.java

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,15 @@ public abstract class HoodieBucketIndex extends HoodieIndex<Object, Object> {
4545

4646
private static final Logger LOG = LogManager.getLogger(HoodieBucketIndex.class);
4747

48-
protected int numBuckets;
49-
protected String indexKeyFields;
48+
protected final int numBuckets;
49+
protected final String indexKeyFields;
5050

5151
public HoodieBucketIndex(HoodieWriteConfig config) {
5252
super(config);
5353

5454
this.numBuckets = config.getBucketIndexNumBuckets();
5555
this.indexKeyFields = config.getBucketIndexHashField();
56-
LOG.info("use bucket index, numBuckets = " + numBuckets + ", indexFields: " + indexKeyFields);
56+
LOG.info("Use bucket index, numBuckets = " + numBuckets + ", indexFields: " + indexKeyFields);
5757
}
5858

5959
@Override
@@ -69,31 +69,20 @@ public <R> HoodieData<HoodieRecord<R>> tagLocation(
6969
HoodieData<HoodieRecord<R>> records, HoodieEngineContext context,
7070
HoodieTable hoodieTable)
7171
throws HoodieIndexException {
72-
// initialize necessary information before tagging. e.g., hashing metadata
72+
// Initialize necessary information before tagging. e.g., hashing metadata
7373
List<String> partitions = records.map(HoodieRecord::getPartitionPath).distinct().collectAsList();
7474
LOG.info("Initializing hashing metadata for partitions: " + partitions);
7575
initialize(hoodieTable, partitions);
7676

7777
return records.mapPartitions(iterator ->
7878
new LazyIterableIterator<HoodieRecord<R>, HoodieRecord<R>>(iterator) {
79-
80-
@Override
81-
protected void start() {
82-
83-
}
84-
8579
@Override
8680
protected HoodieRecord<R> computeNext() {
8781
// TODO maybe batch the operation to improve performance
8882
HoodieRecord record = inputItr.next();
8983
HoodieRecordLocation loc = getBucket(record.getKey(), record.getPartitionPath());
9084
return HoodieIndexUtils.getTaggedRecord(record, Option.ofNullable(loc));
9185
}
92-
93-
@Override
94-
protected void end() {
95-
96-
}
9786
}
9887
);
9988
}
@@ -137,13 +126,15 @@ public int getNumBuckets() {
137126

138127
/**
139128
* Initialize necessary fields
129+
*
140130
* @param table
141131
* @param partitions
142132
*/
143133
protected abstract void initialize(HoodieTable table, List<String> partitions);
144134

145135
/**
146136
* Get record location given the record key and its partition
137+
*
147138
* @param key
148139
* @param partitionPath
149140
* @return

hudi-client/hudi-client-common/src/main/java/org/apache/hudi/index/bucket/HoodieSimpleBucketIndex.java

Lines changed: 11 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020

2121
import org.apache.hudi.common.model.HoodieKey;
2222
import org.apache.hudi.common.model.HoodieRecordLocation;
23-
import org.apache.hudi.common.util.collection.Pair;
2423
import org.apache.hudi.config.HoodieWriteConfig;
2524
import org.apache.hudi.exception.HoodieIOException;
2625
import org.apache.hudi.index.HoodieIndexUtils;
@@ -38,38 +37,38 @@
3837
*/
3938
public class HoodieSimpleBucketIndex extends HoodieBucketIndex {
4039

41-
private static final Logger LOG = LogManager.getLogger(HoodieSimpleBucketIndex.class);
40+
private static final Logger LOG = LogManager.getLogger(HoodieSimpleBucketIndex.class);
4241

4342
/**
44-
* partitionPath -> bucketId -> fileInfo
43+
* Mapping from partitionPath -> bucketId -> fileInfo
4544
*/
46-
Map<String, Map<Integer, Pair<String, String>>> partitionPathFileIDList;
45+
private Map<String, Map<Integer, HoodieRecordLocation>> partitionPathFileIDList;
4746

4847
public HoodieSimpleBucketIndex(HoodieWriteConfig config) {
4948
super(config);
5049
}
5150

52-
private Map<Integer, Pair<String, String>> loadPartitionBucketIdFileIdMapping(
51+
private Map<Integer, HoodieRecordLocation> loadPartitionBucketIdFileIdMapping(
5352
HoodieTable hoodieTable,
5453
String partition) {
5554
// bucketId -> fileIds
56-
Map<Integer, Pair<String, String>> fileIDList = new HashMap<>();
55+
Map<Integer, HoodieRecordLocation> bucketIdToFileIdMapping = new HashMap<>();
5756
hoodieTable.getMetaClient().reloadActiveTimeline();
5857
HoodieIndexUtils
5958
.getLatestBaseFilesForPartition(partition, hoodieTable)
6059
.forEach(file -> {
6160
String fileId = file.getFileId();
6261
String commitTime = file.getCommitTime();
6362
int bucketId = BucketIdentifier.bucketIdFromFileId(fileId);
64-
if (!fileIDList.containsKey(bucketId)) {
65-
fileIDList.put(bucketId, Pair.of(fileId, commitTime));
63+
if (!bucketIdToFileIdMapping.containsKey(bucketId)) {
64+
bucketIdToFileIdMapping.put(bucketId, new HoodieRecordLocation(commitTime, fileId));
6665
} else {
67-
// check if bucket data is valid
66+
// Check if bucket data is valid
6867
throw new HoodieIOException("Find multiple files at partition path="
6968
+ partition + " belongs to the same bucket id = " + bucketId);
7069
}
7170
});
72-
return fileIDList;
71+
return bucketIdToFileIdMapping;
7372
}
7473

7574
@Override
@@ -86,10 +85,7 @@ protected void initialize(HoodieTable table, List<String> partitions) {
8685
@Override
8786
protected HoodieRecordLocation getBucket(HoodieKey key, String partitionPath) {
8887
int bucketId = BucketIdentifier.getBucketId(key, config.getBucketIndexHashField(), numBuckets);
89-
if (partitionPathFileIDList.get(partitionPath).containsKey(bucketId)) {
90-
Pair<String, String> fileInfo = partitionPathFileIDList.get(partitionPath).get(bucketId);
91-
return new HoodieRecordLocation(fileInfo.getRight(), fileInfo.getLeft());
92-
}
93-
return null;
88+
Map<Integer, HoodieRecordLocation> bucketIdToFileIdMapping = partitionPathFileIDList.get(partitionPath);
89+
return bucketIdToFileIdMapping.getOrDefault(bucketId, null);
9490
}
9591
}

hudi-client/hudi-client-common/src/main/java/org/apache/hudi/table/storage/HoodieConsistentBucketLayout.java

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,28 +19,26 @@
1919
package org.apache.hudi.table.storage;
2020

2121
import org.apache.hudi.common.model.WriteOperationType;
22+
import org.apache.hudi.common.util.CollectionUtils;
2223
import org.apache.hudi.common.util.Option;
2324
import org.apache.hudi.config.HoodieWriteConfig;
2425

25-
import java.util.HashSet;
2626
import java.util.Set;
2727

2828
/**
2929
* Storage layout when using consistent hashing bucket index.
3030
*/
3131
public class HoodieConsistentBucketLayout extends HoodieStorageLayout {
32-
public static final Set<WriteOperationType> SUPPORTED_OPERATIONS = new HashSet<WriteOperationType>() {
33-
{
34-
add(WriteOperationType.INSERT);
35-
add(WriteOperationType.INSERT_PREPPED);
36-
add(WriteOperationType.UPSERT);
37-
add(WriteOperationType.UPSERT_PREPPED);
38-
add(WriteOperationType.INSERT_OVERWRITE);
39-
add(WriteOperationType.DELETE);
40-
add(WriteOperationType.COMPACT);
41-
add(WriteOperationType.DELETE_PARTITION);
42-
}
43-
};
32+
public static final Set<WriteOperationType> SUPPORTED_OPERATIONS = CollectionUtils.createImmutableSet(
33+
WriteOperationType.INSERT,
34+
WriteOperationType.INSERT_PREPPED,
35+
WriteOperationType.UPSERT,
36+
WriteOperationType.UPSERT_PREPPED,
37+
WriteOperationType.INSERT_OVERWRITE,
38+
WriteOperationType.DELETE,
39+
WriteOperationType.COMPACT,
40+
WriteOperationType.DELETE_PARTITION
41+
);
4442

4543
public HoodieConsistentBucketLayout(HoodieWriteConfig config) {
4644
super(config);
@@ -56,6 +54,7 @@ public boolean determinesNumFileGroups() {
5654

5755
/**
5856
* Consistent hashing will tag all incoming records, so we could go ahead reusing an existing Partitioner
57+
*
5958
* @return
6059
*/
6160
@Override

hudi-client/hudi-client-common/src/main/java/org/apache/hudi/table/storage/HoodieSimpleBucketLayout.java

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,28 @@
1919
package org.apache.hudi.table.storage;
2020

2121
import org.apache.hudi.common.model.WriteOperationType;
22+
import org.apache.hudi.common.util.CollectionUtils;
2223
import org.apache.hudi.common.util.Option;
2324
import org.apache.hudi.config.HoodieLayoutConfig;
2425
import org.apache.hudi.config.HoodieWriteConfig;
2526

26-
import java.util.HashSet;
2727
import java.util.Set;
2828

2929
/**
3030
* Storage layout when using bucket index. Data distribution and files organization are in a specific way.
3131
*/
3232
public class HoodieSimpleBucketLayout extends HoodieStorageLayout {
3333

34-
public static final Set<WriteOperationType> SUPPORTED_OPERATIONS = new HashSet<WriteOperationType>() {{
35-
add(WriteOperationType.INSERT);
36-
add(WriteOperationType.INSERT_PREPPED);
37-
add(WriteOperationType.UPSERT);
38-
add(WriteOperationType.UPSERT_PREPPED);
39-
add(WriteOperationType.INSERT_OVERWRITE);
40-
add(WriteOperationType.DELETE);
41-
add(WriteOperationType.COMPACT);
42-
add(WriteOperationType.DELETE_PARTITION);
43-
}
44-
};
34+
public static final Set<WriteOperationType> SUPPORTED_OPERATIONS = CollectionUtils.createImmutableSet(
35+
WriteOperationType.INSERT,
36+
WriteOperationType.INSERT_PREPPED,
37+
WriteOperationType.UPSERT,
38+
WriteOperationType.UPSERT_PREPPED,
39+
WriteOperationType.INSERT_OVERWRITE,
40+
WriteOperationType.DELETE,
41+
WriteOperationType.COMPACT,
42+
WriteOperationType.DELETE_PARTITION
43+
);
4544

4645
public HoodieSimpleBucketLayout(HoodieWriteConfig config) {
4746
super(config);

0 commit comments

Comments
 (0)