[
https://issues.apache.org/jira/browse/FLINK-41808?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18154615#comment-18154615
]
Martijn Visser commented on FLINK-41808:
----------------------------------------
Reproducer:
{code:java}
import org.apache.flink.table.annotation.DataTypeHint;
import org.apache.flink.table.annotation.FunctionHint;
import org.apache.flink.table.api.EnvironmentSettings;
import org.apache.flink.table.api.TableEnvironment;
import org.apache.flink.table.functions.AsyncTableFunction;
import org.apache.flink.types.Row;
import org.apache.flink.util.CloseableIterator;
import org.junit.jupiter.api.Test;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import static org.assertj.core.api.Assertions.assertThat;
class AsyncTableFunctionConversionRaceTest {
/** Returns [x, x, ..., x]; completes in bursts of 64 so that conversions
run concurrently. */
@FunctionHint(output = @DataTypeHint("ROW<a ARRAY<STRING>>"))
public static class ArrayFunction extends AsyncTableFunction<Row> {
private static final ExecutorService POOL =
Executors.newFixedThreadPool(32);
private transient CountDownLatch gate;
private transient int inGate;
public void eval(CompletableFuture<Collection<Row>> future, Integer x) {
if (gate == null || inGate == 64) {
gate = new CountDownLatch(1);
inGate = 0;
}
final CountDownLatch g = gate;
final boolean release = ++inGate == 64;
POOL.submit(
() -> {
try {
g.await();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
}
String s = String.valueOf(x);
future.complete(
Collections.singletonList(
Row.of((Object) new String[] {s, s, s,
s, s, s, s, s})));
});
if (release) {
g.countDown();
}
}
}
@Test
void everyArrayBelongsToItsRow() throws Exception {
TableEnvironment env =
TableEnvironment.create(EnvironmentSettings.inStreamingMode());
env.getConfig().set("parallelism.default", "1");
env.getConfig().set("table.exec.async-table.max-concurrent-operations",
"64");
env.createTemporarySystemFunction("f", ArrayFunction.class);
// 19968 = 312 * 64, so every burst fills up and is released
env.executeSql(
"CREATE TABLE src (x INT) WITH ('connector' = 'datagen',"
+ " 'fields.x.kind' = 'sequence', 'fields.x.start' =
'1',"
+ " 'fields.x.end' = '19968')");
int wrong = 0;
try (CloseableIterator<Row> it =
env.executeSql("SELECT x, a FROM src, LATERAL TABLE(f(x)) AS
T(a)").collect()) {
while (it.hasNext()) {
Row r = it.next();
String x = String.valueOf(r.getField(0));
if (!Arrays.stream((String[])
r.getField(1)).allMatch(x::equals)) {
wrong++;
}
}
}
assertThat(wrong).isZero();
}
}
{code}
With the attached reproducer (an AsyncTableFunction returning ARRAY<STRING>, 64
concurrent completions, a plain LATERAL TABLE join), 3,664 to 5,538 of 19,968
rows had wrong array elements on master over 3 runs.
> AsyncTableFunction results are corrupted by concurrent conversion
> -----------------------------------------------------------------
>
> Key: FLINK-41808
> URL: https://issues.apache.org/jira/browse/FLINK-41808
> Project: Flink
> Issue Type: Bug
> Components: Table SQL / Runtime
> Affects Versions: 2.2.0, 2.4.0, 2.5.0, 2.3.2
> Reporter: Martijn Visser
> Priority: Major
>
> AsyncCorrelateRunner converts the function result to internal data on the
> thread that completes the future. The converter is shared by all in-flight
> calls and is not thread-safe (see FLINK-30612). With an AsyncTableFunction
> returning ARRAY<STRING> and 64 concurrent completions, 4,085 to 4,394 of
> 19,968 rows had wrong array elements on master over 3 runs, in a plain
> LATERAL TABLE join. Affects 2.2.0 and later. PR #26817 moves the conversion
> into the mailbox thread, which gives 0 wrong rows in the same runs.
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)