Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
7084612
First batch of code for a new Bucket Partitioning functionality
schongloo Mar 21, 2023
5eaf718
Updating code style.
schongloo Mar 22, 2023
4d84e49
Addressing PR comments
schongloo Apr 5, 2023
fe54373
Addressing PR comment: the BucketPartitioner should be provided a Par…
schongloo Apr 5, 2023
c7b86d7
Addressing build check problems
schongloo Apr 6, 2023
93d7c2e
Addressing PR comments:
schongloo Apr 17, 2023
75fad88
Addressing PR comments, pending:
schongloo Apr 23, 2023
264fc7d
Addressing PR comments, pending:
schongloo Apr 23, 2023
19fa9d6
Addressing PR comments, pending:
schongloo Apr 23, 2023
2afa7ad
Addressing PR comments, pending:
schongloo Apr 24, 2023
767a2da
- Creating the HadoopCatalogExtension.java for Junit5
schongloo Apr 24, 2023
4c40ee6
Removing the need for an additiona/external @TempDir for the HadoopCa…
schongloo Apr 25, 2023
7310f9b
- Splitting tests across TestBucketPartitionerFlinkIcebergSink (E2E),…
schongloo Apr 26, 2023
26dae02
Additional clean up
schongloo Apr 27, 2023
3bc5956
Checkstyle fixes
schongloo Apr 27, 2023
7c11879
Addressing PR comments
schongloo May 20, 2023
b44bd99
Using RowData as the main type across tests.
schongloo May 20, 2023
1651804
Refactoring the partitioning/schema utilities
schongloo May 24, 2023
836daa6
Latest round of PR comments
schongloo May 30, 2023
a488d12
Refactoring enum static methods
schongloo May 30, 2023
dc4b6f4
Latest round of PR comments
schongloo Jul 13, 2023
0cb7f37
Latest round of PR comments
schongloo Jul 14, 2023
c42ce13
Last PR comment
schongloo Aug 9, 2023
87e83b7
Merge branch 'master' into feature/bucket_partitioner
schongloo Aug 9, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* 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.iceberg.flink.sink;

import java.util.stream.IntStream;
import org.apache.flink.api.java.functions.KeySelector;
import org.apache.flink.table.data.RowData;
import org.apache.flink.table.types.logical.RowType;
import org.apache.iceberg.PartitionKey;
import org.apache.iceberg.PartitionSpec;
import org.apache.iceberg.Schema;
import org.apache.iceberg.flink.RowDataWrapper;

/**
* A {@link KeySelector} that extracts the bucketId from a data row's bucket partition as the key.
* To be used with the {@link BucketPartitioner}.
*/
class BucketPartitionKeySelector implements KeySelector<RowData, Integer> {

private final Schema schema;
private final PartitionKey partitionKey;
private final RowType flinkSchema;
private final int bucketFieldPosition;

private transient RowDataWrapper rowDataWrapper;

BucketPartitionKeySelector(PartitionSpec partitionSpec, Schema schema, RowType flinkSchema) {
Comment thread
kengtin marked this conversation as resolved.
this.schema = schema;
this.partitionKey = new PartitionKey(partitionSpec, schema);
this.flinkSchema = flinkSchema;
this.bucketFieldPosition = getBucketFieldPosition(partitionSpec);
}

private int getBucketFieldPosition(PartitionSpec partitionSpec) {
int bucketFieldId = BucketPartitionerUtil.getBucketFieldId(partitionSpec);
return IntStream.range(0, partitionSpec.fields().size())
.filter(i -> partitionSpec.fields().get(i).fieldId() == bucketFieldId)
.toArray()[0];
}

private RowDataWrapper lazyRowDataWrapper() {
if (rowDataWrapper == null) {
rowDataWrapper = new RowDataWrapper(flinkSchema, schema.asStruct());
}

return rowDataWrapper;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: iceberg style adds an empty line after the end of a control block }

}

@Override
public Integer getKey(RowData rowData) {
partitionKey.partition(lazyRowDataWrapper().wrap(rowData));
return partitionKey.get(bucketFieldPosition, Integer.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* 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.iceberg.flink.sink;

import org.apache.flink.api.common.functions.Partitioner;
import org.apache.iceberg.PartitionSpec;
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;

/**
* This partitioner will redirect records to writers deterministically based on the Bucket partition
* spec. It'll attempt to optimize the file size written depending on whether numPartitions is
* greater, less or equal than the maxNumBuckets. Note: The current implementation only supports ONE
* bucket in the partition spec.
*/
class BucketPartitioner implements Partitioner<Integer> {

static final String BUCKET_NULL_MESSAGE = "bucketId cannot be null";
static final String BUCKET_LESS_THAN_LOWER_BOUND_MESSAGE =
"Invalid bucket ID %s: must be non-negative.";
static final String BUCKET_GREATER_THAN_UPPER_BOUND_MESSAGE =
"Invalid bucket ID %s: must be less than bucket limit: %s.";

private final int maxNumBuckets;

// To hold the OFFSET of the next writer to use for any bucket, only used when writers > the
// number of buckets
private final int[] currentBucketWriterOffset;

BucketPartitioner(PartitionSpec partitionSpec) {
this.maxNumBuckets = BucketPartitionerUtil.getMaxNumBuckets(partitionSpec);
this.currentBucketWriterOffset = new int[maxNumBuckets];
}

/**
* Determine the partition id based on the following criteria: If the number of writers <= the
* number of buckets, an evenly distributed number of buckets will be assigned to each writer (one
* writer -> many buckets). Conversely, if the number of writers > the number of buckets the logic
* is handled by the {@link #getPartitionWithMoreWritersThanBuckets
* getPartitionWritersGreaterThanBuckets} method.
*
* @param bucketId the bucketId for each request
* @param numPartitions the total number of partitions
* @return the partition id (writer) to use for each request
*/
@Override
public int partition(Integer bucketId, int numPartitions) {
Comment thread
stevenzwu marked this conversation as resolved.
Preconditions.checkNotNull(bucketId, BUCKET_NULL_MESSAGE);
Preconditions.checkArgument(bucketId >= 0, BUCKET_LESS_THAN_LOWER_BOUND_MESSAGE, bucketId);
Preconditions.checkArgument(
bucketId < maxNumBuckets, BUCKET_GREATER_THAN_UPPER_BOUND_MESSAGE, bucketId, maxNumBuckets);

if (numPartitions <= maxNumBuckets) {
return bucketId % numPartitions;
} else {
return getPartitionWithMoreWritersThanBuckets(bucketId, numPartitions);
}
}

/*-
* If the number of writers > the number of buckets each partitioner will keep a state of multiple
Comment thread
stevenzwu marked this conversation as resolved.
* writers per bucket as evenly as possible, and will round-robin the requests across them, in this
* case each writer will target only one bucket at all times (many writers -> one bucket). Example:
* Configuration: numPartitions (writers) = 5, maxBuckets = 2
* Expected behavior:
* - Records for Bucket 0 will be "round robin" between Writers 0, 2 and 4
* - Records for Bucket 1 will always use Writer 1 and 3
* Notes:
* - maxNumWritersPerBucket determines when to reset the currentBucketWriterOffset to 0 for this bucketId
* - When numPartitions is not evenly divisible by maxBuckets, some buckets will have one more writer (extraWriter).
* In this example Bucket 0 has an "extra writer" to consider before resetting its offset to 0.
*
* @return the destination partition index (writer subtask id)
*/
private int getPartitionWithMoreWritersThanBuckets(int bucketId, int numPartitions) {
int currentOffset = currentBucketWriterOffset[bucketId];
// Determine if this bucket requires an "extra writer"
int extraWriter = bucketId < (numPartitions % maxNumBuckets) ? 1 : 0;
// The max number of writers this bucket can have
int maxNumWritersPerBucket = (numPartitions / maxNumBuckets) + extraWriter;

// Increment the writer offset or reset if it's reached the max for this bucket
int nextOffset = currentOffset == maxNumWritersPerBucket - 1 ? 0 : currentOffset + 1;
currentBucketWriterOffset[bucketId] = nextOffset;

return bucketId + (maxNumBuckets * currentOffset);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
* 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.iceberg.flink.sink;

import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;
import org.apache.flink.api.java.tuple.Tuple2;
import org.apache.iceberg.PartitionSpec;
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.apache.iceberg.transforms.PartitionSpecVisitor;

final class BucketPartitionerUtil {
static final String BAD_NUMBER_OF_BUCKETS_ERROR_MESSAGE =
"Invalid number of buckets: %s (must be 1)";

private BucketPartitionerUtil() {}

/**
* Determines whether the PartitionSpec has one and only one Bucket definition
*
* @param partitionSpec the partition spec in question
* @return whether the PartitionSpec has only one Bucket
*/
static boolean hasOneBucketField(PartitionSpec partitionSpec) {
List<Tuple2<Integer, Integer>> bucketFields = getBucketFields(partitionSpec);
return bucketFields != null && bucketFields.size() == 1;
}

/**
* Extracts the Bucket definition from a PartitionSpec.
*
* @param partitionSpec the partition spec in question
* @return the Bucket definition in the form of a tuple (fieldId, maxNumBuckets)
*/
private static Tuple2<Integer, Integer> getBucketFieldInfo(PartitionSpec partitionSpec) {
List<Tuple2<Integer, Integer>> bucketFields = getBucketFields(partitionSpec);
Preconditions.checkArgument(
bucketFields.size() == 1,
BucketPartitionerUtil.BAD_NUMBER_OF_BUCKETS_ERROR_MESSAGE,
bucketFields.size());
return bucketFields.get(0);
}

static int getBucketFieldId(PartitionSpec partitionSpec) {
return getBucketFieldInfo(partitionSpec).f0;
}

static int getMaxNumBuckets(PartitionSpec partitionSpec) {
return getBucketFieldInfo(partitionSpec).f1;
}

private static List<Tuple2<Integer, Integer>> getBucketFields(PartitionSpec spec) {
return PartitionSpecVisitor.visit(spec, new BucketPartitionSpecVisitor()).stream()
.filter(Objects::nonNull)
.collect(Collectors.toList());
}

private static class BucketPartitionSpecVisitor
implements PartitionSpecVisitor<Tuple2<Integer, Integer>> {
@Override
public Tuple2<Integer, Integer> identity(int fieldId, String sourceName, int sourceId) {
return null;
}

@Override
public Tuple2<Integer, Integer> bucket(
int fieldId, String sourceName, int sourceId, int numBuckets) {
return new Tuple2<>(fieldId, numBuckets);
}

@Override
public Tuple2<Integer, Integer> truncate(
int fieldId, String sourceName, int sourceId, int width) {
return null;
}

@Override
public Tuple2<Integer, Integer> year(int fieldId, String sourceName, int sourceId) {
return null;
}

@Override
public Tuple2<Integer, Integer> month(int fieldId, String sourceName, int sourceId) {
return null;
}

@Override
public Tuple2<Integer, Integer> day(int fieldId, String sourceName, int sourceId) {
return null;
}

@Override
public Tuple2<Integer, Integer> hour(int fieldId, String sourceName, int sourceId) {
return null;
}

@Override
public Tuple2<Integer, Integer> alwaysNull(int fieldId, String sourceName, int sourceId) {
return null;
}

@Override
public Tuple2<Integer, Integer> unknown(
int fieldId, String sourceName, int sourceId, String transform) {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -509,7 +509,13 @@ private DataStream<RowData> distributeDataStream(
+ "and table is unpartitioned");
return input;
} else {
return input.keyBy(new PartitionKeySelector(partitionSpec, iSchema, flinkRowType));
if (BucketPartitionerUtil.hasOneBucketField(partitionSpec)) {
return input.partitionCustom(
new BucketPartitioner(partitionSpec),
new BucketPartitionKeySelector(partitionSpec, iSchema, flinkRowType));
} else {
return input.keyBy(new PartitionKeySelector(partitionSpec, iSchema, flinkRowType));
}
}
} else {
if (partitionSpec.isUnpartitioned()) {
Expand Down
Loading