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 all 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
38 changes: 30 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,31 @@ public Builder suffix(String newSuffix) {
return this;
}

/**
* Configures a {@link FileIO} supplier, which can potentially be used to dynamically refresh
* the file IO instance when a table is refreshed.
*
* @param newIoSupplier The file IO supplier
* @return this builder instance
*/
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);
}
}
}
5 changes: 5 additions & 0 deletions flink/v1.17/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,11 @@ project(":iceberg-flink:iceberg-flink-${flinkMajorVersion}") {
}

testImplementation libs.awaitility
testImplementation libs.assertj.core
}

test {
stevenzwu marked this conversation as resolved.
Show resolved Hide resolved
useJUnitPlatform()
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,13 @@
*/
package org.apache.iceberg.flink;

import java.time.Duration;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import org.apache.flink.configuration.ConfigOption;
import org.apache.flink.configuration.ReadableConfig;
import org.apache.flink.util.TimeUtils;
import org.apache.iceberg.Table;
import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
import org.apache.iceberg.relocated.com.google.common.collect.Lists;
Expand Down Expand Up @@ -59,6 +61,10 @@ public StringConfParser stringConf() {
return new StringConfParser();
}

public DurationConfParser durationConf() {
return new DurationConfParser();
}

class BooleanConfParser extends ConfParser<BooleanConfParser, Boolean> {
private Boolean defaultValue;

Expand Down Expand Up @@ -180,6 +186,29 @@ public E parseOptional() {
}
}

class DurationConfParser extends ConfParser<DurationConfParser, Duration> {
private Duration defaultValue;

@Override
protected DurationConfParser self() {
return this;
}

public DurationConfParser defaultValue(Duration value) {
this.defaultValue = value;
return self();
}

public Duration parse() {
Preconditions.checkArgument(defaultValue != null, "Default value cannot be null");
return parse(TimeUtils::parseDuration, defaultValue);
}

public Duration parseOptional() {
return parse(TimeUtils::parseDuration, null);
}
}

abstract class ConfParser<ThisT, T> {
private final List<String> optionNames = Lists.newArrayList();
private String tablePropertyName;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@
*/
package org.apache.iceberg.flink;

import java.time.Duration;
import java.util.Map;
import org.apache.calcite.linq4j.function.Experimental;
import org.apache.flink.configuration.ReadableConfig;
import org.apache.iceberg.DistributionMode;
import org.apache.iceberg.FileFormat;
Expand Down Expand Up @@ -184,4 +186,20 @@ public String branch() {
public Integer writeParallelism() {
return confParser.intConf().option(FlinkWriteOptions.WRITE_PARALLELISM.key()).parseOptional();
}

/**
* NOTE: This may be removed or changed in a future release. This value specifies the interval for
* refreshing the table instances in sink writer subtasks. If not specified then the default
* behavior is to not refresh the table.
*
* @return the interval for refreshing the table in sink writer subtasks
*/
@Experimental
public Duration tableRefreshInterval() {
return confParser
.durationConf()
.option(FlinkWriteOptions.TABLE_REFRSH_INTERVAL.key())
.flinkConfig(FlinkWriteOptions.TABLE_REFRSH_INTERVAL)
.parseOptional();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
*/
package org.apache.iceberg.flink;

import java.time.Duration;
import org.apache.calcite.linq4j.function.Experimental;
import org.apache.flink.configuration.ConfigOption;
import org.apache.flink.configuration.ConfigOptions;
import org.apache.iceberg.SnapshotRef;
Expand Down Expand Up @@ -64,4 +66,8 @@ private FlinkWriteOptions() {}

public static final ConfigOption<Integer> WRITE_PARALLELISM =
ConfigOptions.key("write-parallelism").intType().noDefaultValue();

@Experimental
public static final ConfigOption<Duration> TABLE_REFRSH_INTERVAL =
ConfigOptions.key("table-refresh-interval").durationType().noDefaultValue();
}
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,8 @@ 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
@@ -0,0 +1,91 @@
/*
* 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.time.Duration;
import org.apache.flink.util.Preconditions;
import org.apache.iceberg.SerializableTable;
import org.apache.iceberg.Table;
import org.apache.iceberg.flink.TableLoader;
import org.apache.iceberg.util.DateTimeUtil;
import org.apache.iceberg.util.SerializableSupplier;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* A table loader that will only reload a table after a certain interval has passed. WARNING: This
* table loader should be used carefully when used with writer tasks. It could result in heavy load
* on a catalog for jobs with many writers.
*/
class CachingTableSupplier implements SerializableSupplier<Table> {

private static final Logger LOG = LoggerFactory.getLogger(CachingTableSupplier.class);

private final Table initialTable;
private final TableLoader tableLoader;
private final Duration tableRefreshInterval;
private long lastLoadTimeMillis;
private transient Table table;

CachingTableSupplier(
SerializableTable initialTable, TableLoader tableLoader, Duration tableRefreshInterval) {
Preconditions.checkArgument(initialTable != null, "initialTable cannot be null");
Preconditions.checkArgument(tableLoader != null, "tableLoader cannot be null");
Preconditions.checkArgument(
tableRefreshInterval != null, "tableRefreshInterval cannot be null");
this.initialTable = initialTable;
this.table = initialTable;
this.tableLoader = tableLoader;
this.tableRefreshInterval = tableRefreshInterval;
this.lastLoadTimeMillis = System.currentTimeMillis();
}

@Override
public Table get() {
if (table == null) {
this.table = initialTable;
stevenzwu marked this conversation as resolved.
Show resolved Hide resolved
}
return table;
}

Table initialTable() {
return initialTable;
}

void refreshTable() {
if (System.currentTimeMillis() > lastLoadTimeMillis + tableRefreshInterval.toMillis()) {
try {
if (!tableLoader.isOpen()) {
tableLoader.open();
}

this.table = tableLoader.loadTable();
this.lastLoadTimeMillis = System.currentTimeMillis();

LOG.info(
"Table {} reloaded, next min load time threshold is {}",
table.name(),
DateTimeUtil.formatTimestampMillis(
lastLoadTimeMillis + tableRefreshInterval.toMillis()));
} catch (Exception e) {
LOG.warn("An error occurred reloading table {}, table was not reloaded", table.name(), e);
}
}
}
}
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,14 @@ 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,
Map<String, String> tableProps,
String flinkJobId,
String operatorUniqueId,
int subTaskId,
long attemptNumber) {
return new ManifestOutputFileFactory(
ops,
table.io(),
table.properties(),
flinkJobId,
operatorUniqueId,
subTaskId,
attemptNumber);
tableSupplier, tableProps, flinkJobId, operatorUniqueId, subTaskId, attemptNumber);
}

/**
Expand Down
Loading