Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Flink: table supplier in writer operator for optional table refresh #8555

Merged
merged 45 commits into from
Sep 21, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
45 commits
Select commit Hold shift + click to select a range
c9bbee3
Flink: provide table supplier for optional table reload
bryanck Sep 12, 2023
d5f884d
add log for reload
bryanck Sep 13, 2023
41a27f4
open table loader if needed
bryanck Sep 13, 2023
de536d5
reload table at commit in committer
bryanck Sep 13, 2023
41dfe7e
store initial table props in manifest util
bryanck Sep 13, 2023
08825a5
PR feedback
bryanck Sep 13, 2023
90934eb
comment grammar
bryanck Sep 13, 2023
3b101ae
use junit5 for new test
bryanck Sep 13, 2023
dd6f799
spacing
bryanck Sep 13, 2023
77fd879
explicit refresh of table
bryanck Sep 14, 2023
389e1c3
reload test fix
bryanck Sep 14, 2023
b5dee48
PR cleanup feedback
bryanck Sep 14, 2023
83c6fb0
Merge branch 'master' into flink-table-supplier
bryanck Sep 14, 2023
b1e0ed7
remove jitter, rename vars
bryanck Sep 14, 2023
74754f5
update log statement
bryanck Sep 14, 2023
3b93157
javadoc fix
bryanck Sep 14, 2023
5d20cc6
test assertion fix
bryanck Sep 14, 2023
8af3916
log warning on reload failure
bryanck Sep 14, 2023
501ccce
refresh table in writer at startup also
bryanck Sep 15, 2023
86bbc69
specify table supplier in sink builder
bryanck Sep 15, 2023
f40330b
add comments
bryanck Sep 15, 2023
e0f12f1
comment typo
bryanck Sep 15, 2023
a50f545
update method name
bryanck Sep 15, 2023
1deaae5
use table loader interface instead of table supplier
bryanck Sep 17, 2023
d4a7116
check if table loader is already open
bryanck Sep 17, 2023
8190115
PR feedback
bryanck Sep 19, 2023
a1d5e60
checkstyle
bryanck Sep 19, 2023
48ef055
package private method
bryanck Sep 19, 2023
dcd6630
grammar/typo
bryanck Sep 19, 2023
f417bfd
revert back to table supplier
bryanck Sep 19, 2023
626312d
test fix
bryanck Sep 19, 2023
4e84f76
update test name
bryanck Sep 20, 2023
9b874c1
change param to SerializableTable
bryanck Sep 20, 2023
256859d
Merge branch 'master' into flink-table-supplier
bryanck Sep 20, 2023
bab1429
update test assertion
bryanck Sep 20, 2023
4a2370d
move refresh interval config to Flink config
bryanck Sep 20, 2023
ca740ca
javadoc
bryanck Sep 20, 2023
1290e20
duration config
bryanck Sep 20, 2023
d8c472e
test fix
bryanck Sep 20, 2023
e0346bc
store last load time instead of next
bryanck Sep 20, 2023
fe46157
add duration type support for config
bryanck Sep 20, 2023
967edcb
refresh table on get in supplier
bryanck Sep 21, 2023
77836e7
fix comment
bryanck Sep 21, 2023
d87a246
ensure initial table is used for schema for now
bryanck Sep 21, 2023
3a121bc
refresh before creating task writer
bryanck Sep 21, 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
31 changes: 23 additions & 8 deletions core/src/main/java/org/apache/iceberg/io/OutputFileFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.function.Supplier;
import org.apache.iceberg.FileFormat;
import org.apache.iceberg.PartitionSpec;
import org.apache.iceberg.StructLike;
Expand All @@ -35,7 +36,7 @@ public class OutputFileFactory {
private final PartitionSpec defaultSpec;
private final FileFormat format;
private final LocationProvider locations;
private final FileIO io;
private final Supplier<FileIO> ioSupplier;
private final EncryptionManager encryptionManager;
private final int partitionId;
private final long taskId;
Expand All @@ -56,7 +57,7 @@ public class OutputFileFactory {
* @param spec Partition specification used by the location provider
* @param format File format used for the extension
* @param locations Location provider used for generating locations
* @param io FileIO to store the files
* @param ioSupplier Supplier of FileIO to store the files
* @param encryptionManager Encryption manager used for encrypting the files
* @param partitionId First part of the file name
* @param taskId Second part of the file name
Expand All @@ -67,7 +68,7 @@ private OutputFileFactory(
PartitionSpec spec,
FileFormat format,
LocationProvider locations,
FileIO io,
Supplier<FileIO> ioSupplier,
EncryptionManager encryptionManager,
int partitionId,
long taskId,
Expand All @@ -76,7 +77,7 @@ private OutputFileFactory(
this.defaultSpec = spec;
this.format = format;
this.locations = locations;
this.io = io;
this.ioSupplier = ioSupplier;
this.encryptionManager = encryptionManager;
this.partitionId = partitionId;
this.taskId = taskId;
Expand All @@ -101,7 +102,7 @@ private String generateFilename() {

/** Generates an {@link EncryptedOutputFile} for unpartitioned writes. */
public EncryptedOutputFile newOutputFile() {
OutputFile file = io.newOutputFile(locations.newDataLocation(generateFilename()));
OutputFile file = ioSupplier.get().newOutputFile(locations.newDataLocation(generateFilename()));
return encryptionManager.encrypt(file);
}

Expand All @@ -113,7 +114,7 @@ public EncryptedOutputFile newOutputFile(StructLike partition) {
/** Generates an {@link EncryptedOutputFile} for partitioned writes in a given spec. */
public EncryptedOutputFile newOutputFile(PartitionSpec spec, StructLike partition) {
String newDataLocation = locations.newDataLocation(spec, partition, generateFilename());
OutputFile rawOutputFile = io.newOutputFile(newDataLocation);
OutputFile rawOutputFile = ioSupplier.get().newOutputFile(newDataLocation);
return encryptionManager.encrypt(rawOutputFile);
}

Expand All @@ -125,6 +126,7 @@ public static class Builder {
private String operationId;
private FileFormat format;
private String suffix;
private Supplier<FileIO> ioSupplier;

private Builder(Table table, int partitionId, long taskId) {
this.table = table;
Expand All @@ -136,6 +138,7 @@ private Builder(Table table, int partitionId, long taskId) {
String formatAsString =
table.properties().getOrDefault(DEFAULT_FILE_FORMAT, DEFAULT_FILE_FORMAT_DEFAULT);
this.format = FileFormat.fromString(formatAsString);
this.ioSupplier = table::io;
}

public Builder defaultSpec(PartitionSpec newDefaultSpec) {
Expand All @@ -158,12 +161,24 @@ public Builder suffix(String newSuffix) {
return this;
}

public Builder ioSupplier(Supplier<FileIO> newIoSupplier) {
stevenzwu marked this conversation as resolved.
Show resolved Hide resolved
this.ioSupplier = newIoSupplier;
return this;
}

public OutputFileFactory build() {
LocationProvider locations = table.locationProvider();
FileIO io = table.io();
EncryptionManager encryption = table.encryption();
return new OutputFileFactory(
defaultSpec, format, locations, io, encryption, partitionId, taskId, operationId, suffix);
defaultSpec,
format,
locations,
ioSupplier,
encryption,
partitionId,
taskId,
operationId,
suffix);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@ public interface TableLoader extends Closeable, Serializable, Cloneable {

void open();

boolean isOpen();
stevenzwu marked this conversation as resolved.
Show resolved Hide resolved

Table loadTable();

/** Clone a TableLoader */
Expand Down Expand Up @@ -75,6 +77,11 @@ public void open() {
tables = new HadoopTables(hadoopConf.get());
}

@Override
public boolean isOpen() {
return tables != null;
}

@Override
public Table loadTable() {
FlinkEnvironmentContext.init();
Expand Down Expand Up @@ -115,6 +122,11 @@ public void open() {
catalog = catalogLoader.loadCatalog();
}

@Override
public boolean isOpen() {
return catalog != null;
}

@Override
public Table loadTable() {
FlinkEnvironmentContext.init();
Expand All @@ -126,6 +138,7 @@ public void close() throws IOException {
if (catalog instanceof Closeable) {
((Closeable) catalog).close();
}
catalog = null;
nastra marked this conversation as resolved.
Show resolved Hide resolved
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,11 @@
import java.util.function.Supplier;
import org.apache.iceberg.DataFile;
import org.apache.iceberg.DeleteFile;
import org.apache.iceberg.HasTableOperations;
import org.apache.iceberg.ManifestFile;
import org.apache.iceberg.ManifestFiles;
import org.apache.iceberg.ManifestWriter;
import org.apache.iceberg.PartitionSpec;
import org.apache.iceberg.Table;
import org.apache.iceberg.TableOperations;
import org.apache.iceberg.io.CloseableIterable;
import org.apache.iceberg.io.FileIO;
import org.apache.iceberg.io.OutputFile;
Expand Down Expand Up @@ -64,16 +62,13 @@ static List<DataFile> readDataFiles(
}

static ManifestOutputFileFactory createOutputFileFactory(
Table table, String flinkJobId, String operatorUniqueId, int subTaskId, long attemptNumber) {
TableOperations ops = ((HasTableOperations) table).operations();
Supplier<Table> tableSupplier,
String flinkJobId,
String operatorUniqueId,
int subTaskId,
long attemptNumber) {
return new ManifestOutputFileFactory(
ops,
table.io(),
table.properties(),
flinkJobId,
operatorUniqueId,
subTaskId,
attemptNumber);
tableSupplier, flinkJobId, operatorUniqueId, subTaskId, attemptNumber);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import org.apache.calcite.linq4j.function.Experimental;
nastra marked this conversation as resolved.
Show resolved Hide resolved
import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.api.common.typeinfo.TypeInformation;
import org.apache.flink.api.common.typeinfo.Types;
Expand Down Expand Up @@ -67,6 +68,7 @@
import org.apache.iceberg.relocated.com.google.common.collect.Maps;
import org.apache.iceberg.relocated.com.google.common.collect.Sets;
import org.apache.iceberg.types.TypeUtil;
import org.apache.iceberg.util.SerializableSupplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

Expand Down Expand Up @@ -138,6 +140,7 @@ public static class Builder {
private ReadableConfig readableConfig = new Configuration();
private final Map<String, String> writeOptions = Maps.newHashMap();
private FlinkWriteConf flinkWriteConf = null;
private long reloadIntervalMs;

private Builder() {}

Expand Down Expand Up @@ -266,6 +269,20 @@ public Builder upsert(boolean enabled) {
return this;
}

/**
* Sets the interval for reloading of the Iceberg {@link Table} instance in the subtasks. When
* set, the table will be periodically reloaded using the {@link TableLoader} specified for the
* builder. Default is to not reload the table.
*
* @param intervalMs the interval for periodically reloading the table.
* @return {@link Builder} to connect the iceberg table.
*/
@Experimental
public Builder reloadIntervalMs(long intervalMs) {
this.reloadIntervalMs = intervalMs;
return this;
}

/**
* Configuring the equality field columns for iceberg table that accept CDC or UPSERT events.
*
Expand Down Expand Up @@ -429,7 +446,8 @@ private SingleOutputStreamOperator<Void> appendCommitter(
snapshotProperties,
flinkWriteConf.workerPoolSize(),
flinkWriteConf.branch(),
table.spec());
table.spec(),
reloadIntervalMs);
SingleOutputStreamOperator<Void> committerStream =
writerStream
.transform(operatorName(ICEBERG_FILES_COMMITTER_NAME), Types.VOID, filesCommitter)
Expand Down Expand Up @@ -463,7 +481,8 @@ private SingleOutputStreamOperator<WriteResult> appendWriter(
}

IcebergStreamWriter<RowData> streamWriter =
createStreamWriter(table, flinkWriteConf, flinkRowType, equalityFieldIds);
createStreamWriter(
table, tableLoader, flinkWriteConf, flinkRowType, equalityFieldIds, reloadIntervalMs);

int parallelism =
flinkWriteConf.writeParallelism() == null
Expand Down Expand Up @@ -581,16 +600,25 @@ static RowType toFlinkRowType(Schema schema, TableSchema requestedSchema) {

static IcebergStreamWriter<RowData> createStreamWriter(
Table table,
TableLoader tableLoader,
FlinkWriteConf flinkWriteConf,
RowType flinkRowType,
List<Integer> equalityFieldIds) {
List<Integer> equalityFieldIds,
long reloadIntervalMs) {
Preconditions.checkArgument(table != null, "Iceberg table shouldn't be null");

Table serializableTable = SerializableTable.copyOf(table);
SerializableSupplier<Table> tableSupplier;
if (reloadIntervalMs > 0) {
tableSupplier = new ReloadingTableSupplier(serializableTable, tableLoader, reloadIntervalMs);
} else {
tableSupplier = () -> serializableTable;
}

FileFormat format = flinkWriteConf.dataFileFormat();
TaskWriterFactory<RowData> taskWriterFactory =
new RowDataTaskWriterFactory(
serializableTable,
tableSupplier,
flinkRowType,
flinkWriteConf.targetDataFileSize(),
format,
Expand Down
Loading