Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
Expand Up @@ -17,12 +17,14 @@

package org.apache.fluss.flink.procedure;

import org.apache.fluss.annotation.VisibleForTesting;
import org.apache.fluss.client.tiering.FlussTableLakeSnapshotCommitter;
import org.apache.fluss.config.ConfigOptions;
import org.apache.fluss.config.Configuration;
import org.apache.fluss.exception.FlussRuntimeException;
import org.apache.fluss.exception.LakeTableSnapshotNotExistException;
import org.apache.fluss.exception.TableAlreadyExistException;
import org.apache.fluss.exception.TableNotExistException;
import org.apache.fluss.flink.utils.DataLakeUtils;
import org.apache.fluss.lake.lakestorage.LakeCatalog;
import org.apache.fluss.lake.lakestorage.LakeStorage;
Expand All @@ -33,13 +35,16 @@
import org.apache.fluss.metadata.TableInfo;
import org.apache.fluss.metadata.TablePath;
import org.apache.fluss.utils.ExceptionUtils;
import org.apache.fluss.utils.RetryUtils;
import org.apache.fluss.utils.TemporaryClassLoaderContext;
import org.apache.fluss.utils.function.ThrowingRunnable;

import org.apache.flink.table.annotation.ArgumentHint;
import org.apache.flink.table.annotation.DataTypeHint;
import org.apache.flink.table.annotation.ProcedureHint;
import org.apache.flink.table.procedure.ProcedureContext;

import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashMap;
Expand All @@ -54,6 +59,10 @@ public class EnableFlussOnLakeTableProcedure extends ProcedureBase {

private static final String SUCCESS_MESSAGE = "Successfully enabled Fluss on %s table '%s'.";

private static final int COMMIT_MAX_ATTEMPTS = 10;
private static final long COMMIT_INITIAL_BACKOFF_MS = 100L;
private static final long COMMIT_MAX_BACKOFF_MS = 2000L;

@ProcedureHint(argument = {@ArgumentHint(name = "table", type = @DataTypeHint("STRING"))})
public String[] call(ProcedureContext context, String table) throws Exception {
return enable(table, Collections.<String, String>emptyMap());
Expand Down Expand Up @@ -134,8 +143,13 @@ private void registerLakeSnapshot(TableInfo tableInfo) {
new FlussTableLakeSnapshotCommitter(
flussProcedureContext.getFlussConfiguration())) {
committer.open();
committer.commitInitialSnapshot(
tableInfo.getTableId(), tableInfo.getTablePath(), snapshotId.get());
commitInitialSnapshotWithRetry(
() ->
committer.commitInitialSnapshot(
tableInfo.getTableId(),
tableInfo.getTablePath(),
snapshotId.get()),
tableInfo.getTablePath());
}
} catch (Exception e) {
throw new FlussRuntimeException(
Expand All @@ -146,6 +160,27 @@ private void registerLakeSnapshot(TableInfo tableInfo) {
}
}

/**
* Runs the commit of the initial lake snapshot, retrying while the coordinator reports the
* table as missing. The coordinator learns about a newly created table asynchronously via
* ZooKeeper, so an immediate commit can be rejected with {@link TableNotExistException} until
* then.
*/
@VisibleForTesting
static void commitInitialSnapshotWithRetry(
ThrowingRunnable<IOException> commit, TablePath tablePath) throws IOException {
RetryUtils.executeWithRetry(
() -> {
commit.run();
return null;
},
String.format("Committing the initial lake snapshot of table %s", tablePath),
COMMIT_MAX_ATTEMPTS,
COMMIT_INITIAL_BACKOFF_MS,
COMMIT_MAX_BACKOFF_MS,
e -> ExceptionUtils.findThrowable(e, TableNotExistException.class).isPresent());
}

private LakeCatalog createLakeCatalog(TableInfo tableInfo) {
DataLakeFormat dataLakeFormat = tableInfo.getTableConfig().getDataLakeFormat().get();
LakeStoragePlugin lakeStoragePlugin =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,17 @@

package org.apache.fluss.flink.procedure;

import org.apache.fluss.exception.TableNotExistException;
import org.apache.fluss.metadata.TablePath;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.NullAndEmptySource;
import org.junit.jupiter.params.provider.ValueSource;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
Expand Down Expand Up @@ -63,4 +66,35 @@ void testRejectInvalidOptions(String options) {
assertThatThrownBy(() -> EnableFlussOnLakeTableProcedure.parseOptions(options))
.isInstanceOf(IllegalArgumentException.class);
}

@Test
void testRetryCommitWhileCoordinatorDoesNotKnowTable() throws Exception {
AtomicInteger attempts = new AtomicInteger();
EnableFlussOnLakeTableProcedure.commitInitialSnapshotWithRetry(
() -> {
if (attempts.incrementAndGet() < 3) {
throw new IOException(
"Fail to commit table lake snapshot",
new TableNotExistException("Table 9 not found."));
}
},
TablePath.of("db", "table"));
assertThat(attempts).hasValue(3);
}

@Test
void testDoNotRetryCommitOnOtherFailures() {
AtomicInteger attempts = new AtomicInteger();
assertThatThrownBy(
() ->
EnableFlussOnLakeTableProcedure.commitInitialSnapshotWithRetry(
() -> {
attempts.incrementAndGet();
throw new IOException("unexpected failure");
},
TablePath.of("db", "table")))
.isInstanceOf(IOException.class)
.hasMessage("unexpected failure");
assertThat(attempts).hasValue(1);
}
}
Loading