[
https://issues.apache.org/jira/browse/FLINK-30612?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=18154695#comment-18154695
]
Martijn Visser commented on FLINK-30612:
----------------------------------------
Still reproduces on master, now in async lookup joins. AsyncLookupJoinRunner
gives every in-flight future the same converter and converts on the thread that
completes the user's CompletableFuture, so a lookup AsyncTableFunction<Row>
returning ARRAY, MAP, MULTISET, or a ROW or structured type containing them
corrupts rows. With 64 concurrent completions, 3,061 to 3,759 of 19,968 rows
had wrong array elements on master over 3 runs; without assertions some MAP
cases fail the job with IndexOutOfBoundsException instead. Lookup functions
returning RowData are not affected. The shared converter dates from FLINK-18890
(1.13). The DataFormatConverters classes in the description are no longer on
this path; the reusing class is now ArrayObjectArrayConverter. Async correlate
has the same problem in FLINK-41808; async scalar functions were fixed in
FLINK-37243. Giving each in-flight future its own converter fixes it at no
measurable cost. Reproducer below.
{code:java}
/*
* 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.flink.table.planner.runtime.stream.table;
import org.apache.flink.table.annotation.DataTypeHint;
import org.apache.flink.table.annotation.FunctionHint;
import org.apache.flink.table.api.DataTypes;
import org.apache.flink.table.api.EnvironmentSettings;
import org.apache.flink.table.api.Schema;
import org.apache.flink.table.api.TableEnvironment;
import org.apache.flink.table.functions.AsyncTableFunction;
import
org.apache.flink.table.legacy.connector.source.AsyncTableFunctionProvider;
import org.apache.flink.table.planner.factories.TableFactoryHarness;
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 AsyncLookupConversionRaceTest {
/** Returns (k, [k, ..., k]); completes in bursts of 64 so that conversions
run concurrently. */
@FunctionHint(output = @DataTypeHint("ROW<k INT, a ARRAY<STRING>>"))
public static class ArrayLookup 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 k) {
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(k);
future.complete(
Collections.singletonList(
Row.of(k, new String[] {s, s, s, s, s,
s, s, s})));
});
if (release) {
g.countDown();
}
}
}
/** Lookup source backed by the function above. */
public static class Dim extends TableFactoryHarness.LookupSourceBase {
@Override
public LookupRuntimeProvider getLookupRuntimeProvider(LookupContext
context) {
return AsyncTableFunctionProvider.of(new ArrayLookup());
}
}
@Test
void everyArrayBelongsToItsRow() throws Exception {
TableEnvironment env =
TableEnvironment.create(EnvironmentSettings.inStreamingMode());
env.getConfig().set("parallelism.default", "1");
env.getConfig().set("table.exec.async-lookup.buffer-capacity", "64");
// 19968 = 312 * 64, so every burst fills up and is released
env.executeSql(
"CREATE TABLE src (x INT, pt AS PROCTIME()) WITH ('connector' =
'datagen',"
+ " 'fields.x.kind' = 'sequence', 'fields.x.start' =
'1',"
+ " 'fields.x.end' = '19968')");
env.createTemporaryTable(
"dim",
TableFactoryHarness.newBuilder()
.schema(
Schema.newBuilder()
.column("k", DataTypes.INT())
.column("a",
DataTypes.ARRAY(DataTypes.STRING()))
.build())
.source(new Dim())
.build());
int wrong = 0;
try (CloseableIterator<Row> it =
env.executeSql(
"SELECT s.x, d.a FROM src AS s"
+ " JOIN dim FOR SYSTEM_TIME AS OF s.pt
AS d ON s.x = d.k")
.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}
> ObjectArrayConverter is not thread-safe,raised by reuseArray、reuseWriter
> ------------------------------------------------------------------------
>
> Key: FLINK-30612
> URL: https://issues.apache.org/jira/browse/FLINK-30612
> Project: Flink
> Issue Type: Bug
> Components: Table SQL / API, Table SQL / Runtime
> Affects Versions: 1.12.2
> Reporter: guoxin
> Priority: Major
>
> data clutter in multi-threaded environment.
> by design?
> or , how do i use it in multi-thread env?
> {code:java}
> package org.apache.flink.table.data.util;
> public class DataFormatConverters{
> ....
> public static final class ObjectArrayConverter<T> extends
> DataFormatConverter<ArrayData, T[]>{
> ....
> // it's not thread-safe
> private transient BinaryArrayData reuseArray;
> private transient BinaryArrayWriter reuseWriter;
> ....
> private ArrayData toBinaryArray(T[] value){
> // not thread-safe
> }
> ....
> }
> ....
> }
> {code}
--
This message was sent by Atlassian Jira
(v10.3.26#10030026)