You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@flink.apache.org by GitBox <gi...@apache.org> on 2019/05/08 03:41:34 UTC

[GitHub] [flink] JingsongLi commented on a change in pull request #8302: [FLINK-12269][table-blink] Support Temporal Table Join in blink planner and runtime

JingsongLi commented on a change in pull request #8302: [FLINK-12269][table-blink] Support Temporal Table Join in blink planner and runtime
URL: https://github.com/apache/flink/pull/8302#discussion_r281903520
 
 

 ##########
 File path: flink-table/flink-table-runtime-blink/src/main/java/org/apache/flink/table/runtime/join/lookup/AsyncLookupJoinRunner.java
 ##########
 @@ -0,0 +1,274 @@
+/*
+ * 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.runtime.join.lookup;
+
+import org.apache.flink.api.common.functions.util.FunctionUtils;
+import org.apache.flink.api.common.typeinfo.TypeInformation;
+import org.apache.flink.api.java.typeutils.RowTypeInfo;
+import org.apache.flink.configuration.Configuration;
+import org.apache.flink.streaming.api.functions.async.AsyncFunction;
+import org.apache.flink.streaming.api.functions.async.ResultFuture;
+import org.apache.flink.streaming.api.functions.async.RichAsyncFunction;
+import org.apache.flink.table.dataformat.BaseRow;
+import org.apache.flink.table.dataformat.DataFormatConverters;
+import org.apache.flink.table.dataformat.DataFormatConverters.RowConverter;
+import org.apache.flink.table.dataformat.GenericRow;
+import org.apache.flink.table.dataformat.JoinedRow;
+import org.apache.flink.table.generated.GeneratedFunction;
+import org.apache.flink.table.generated.GeneratedResultFuture;
+import org.apache.flink.table.runtime.collector.TableFunctionResultFuture;
+import org.apache.flink.table.typeutils.BaseRowTypeInfo;
+import org.apache.flink.types.Row;
+
+import javax.annotation.Nullable;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.ArrayBlockingQueue;
+import java.util.concurrent.BlockingQueue;
+
+/**
+ * The async join runner to lookup the dimension table.
+ */
+public class AsyncLookupJoinRunner extends RichAsyncFunction<BaseRow, BaseRow> {
+	private static final long serialVersionUID = -6664660022391632480L;
+
+	private final GeneratedFunction<AsyncFunction<BaseRow, Object>> generatedFetcher;
+	private final GeneratedResultFuture<TableFunctionResultFuture<BaseRow>> generatedResultFuture;
+	private final boolean isLeftOuterJoin;
+	private final int asyncBufferCapacity;
+	private final TypeInformation<?> fetcherReturnType;
+	private final BaseRowTypeInfo rightRowTypeInfo;
+
+	private transient AsyncFunction<BaseRow, Object> fetcher;
+
+	/**
+	 * Buffers {@link ResultFuture} to avoid newInstance cost when processing elements every time.
+	 * We use {@link BlockingQueue} to make sure the head {@link ResultFuture}s are available.
+	 */
+	private transient BlockingQueue<JoinedRowResultFuture> resultFutureBuffer;
+
+	public AsyncLookupJoinRunner(
+			GeneratedFunction<AsyncFunction<BaseRow, Object>> generatedFetcher,
+			GeneratedResultFuture<TableFunctionResultFuture<BaseRow>> generatedResultFuture,
+			TypeInformation<?> fetcherReturnType,
+			BaseRowTypeInfo rightRowTypeInfo,
+			boolean isLeftOuterJoin,
+			int asyncBufferCapacity) {
+		this.generatedFetcher = generatedFetcher;
+		this.generatedResultFuture = generatedResultFuture;
+		this.isLeftOuterJoin = isLeftOuterJoin;
+		this.asyncBufferCapacity = asyncBufferCapacity;
+		this.fetcherReturnType = fetcherReturnType;
+		this.rightRowTypeInfo = rightRowTypeInfo;
+	}
+
+	@Override
+	public void open(Configuration parameters) throws Exception {
+		super.open(parameters);
+		this.fetcher = generatedFetcher.newInstance(getRuntimeContext().getUserCodeClassLoader());
+		FunctionUtils.setFunctionRuntimeContext(fetcher, getRuntimeContext());
+		FunctionUtils.openFunction(fetcher, parameters);
+
+		// try to compile the generated ResultFuture, fail fast if the code is corrupt.
+		generatedResultFuture.compile(getRuntimeContext().getUserCodeClassLoader());
+
+		// row converter is stateless which is thread-safe
+		RowConverter rowConverter;
+		if (fetcherReturnType instanceof RowTypeInfo) {
+			rowConverter = (RowConverter) DataFormatConverters.getConverterForTypeInfo(fetcherReturnType);
+		} else if (fetcherReturnType instanceof BaseRowTypeInfo) {
+			rowConverter = null;
+		} else {
+			throw new IllegalStateException("This should never happen, " +
+				"currently fetcherReturnType can only be BaseRowTypeInfo or RowTypeInfo");
+		}
+
+		// asyncBufferCapacity + 1 as the queue size in order to avoid
+		// blocking on the queue when taking a collector.
+		this.resultFutureBuffer = new ArrayBlockingQueue<>(asyncBufferCapacity + 1);
+		for (int i = 0; i < asyncBufferCapacity + 1; i++) {
+			JoinedRowResultFuture rf = new JoinedRowResultFuture(
+				resultFutureBuffer,
+				createFetcherResultFuture(),
+				rowConverter,
+				isLeftOuterJoin,
+				rightRowTypeInfo.getArity());
+			// add will throw exception immediately if the queue is full which should never happen
+			resultFutureBuffer.add(rf);
+		}
+	}
+
+	@Override
+	public void asyncInvoke(BaseRow input, ResultFuture<BaseRow> resultFuture) throws Exception {
+		JoinedRowResultFuture outResultFuture = resultFutureBuffer.take();
+		// the input row is copied when object reuse in AsyncWaitOperator
+		outResultFuture.reset(input, resultFuture);
+
+		// fetcher has copied the input field when object reuse is enabled
+		fetcher.asyncInvoke(input, outResultFuture);
+	}
+
+	public TableFunctionResultFuture<BaseRow> createFetcherResultFuture() throws Exception {
+		TableFunctionResultFuture<BaseRow> resultFuture = generatedResultFuture.newInstance(
+			getRuntimeContext().getUserCodeClassLoader());
+		FunctionUtils.setFunctionRuntimeContext(resultFuture, getRuntimeContext());
+		FunctionUtils.openFunction(resultFuture, new Configuration());
 
 Review comment:
   Consider close `resultFuture`?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
users@infra.apache.org


With regards,
Apache Git Services