yunfengzhou-hub commented on code in PR #24657: URL: https://github.com/apache/flink/pull/24657#discussion_r1563570370
########## flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/AsyncExecutionController.java: ########## @@ -51,26 +51,43 @@ public class AsyncExecutionController<R, K> { private final int maxInFlightRecordNum; /** The key accounting unit which is used to detect the key conflict. */ - final KeyAccountingUnit<R, K> keyAccountingUnit; + final KeyAccountingUnit<K> keyAccountingUnit; /** * A factory to build {@link org.apache.flink.core.state.InternalStateFuture}, this will auto * wire the created future with mailbox executor. Also conducting the context switch. */ - private final StateFutureFactory<R, K> stateFutureFactory; + private final StateFutureFactory<K> stateFutureFactory; /** The state executor where the {@link StateRequest} is actually executed. */ final StateExecutor stateExecutor; /** The corresponding context that currently runs in task thread. */ - RecordContext<R, K> currentContext; + RecordContext<K> currentContext; + @VisibleForTesting public AsyncExecutionController(MailboxExecutor mailboxExecutor, StateExecutor stateExecutor) { - this(mailboxExecutor, stateExecutor, DEFAULT_MAX_IN_FLIGHT_RECORD_NUM); + this(null, mailboxExecutor, stateExecutor); } public AsyncExecutionController( - MailboxExecutor mailboxExecutor, StateExecutor stateExecutor, int maxInFlightRecords) { + Class<K> type, MailboxExecutor mailboxExecutor, StateExecutor stateExecutor) { + this(type, mailboxExecutor, stateExecutor, DEFAULT_MAX_IN_FLIGHT_RECORD_NUM); + } + + /** + * Create an async execution controller. + * + * @param type the type class for key. + * @param mailboxExecutor the mailbox executor that will run the callback. + * @param stateExecutor the state executor that executing a batch of state requests. + * @param maxInFlightRecords the max allowed number of in-flight records. + */ + public AsyncExecutionController( + Class<K> type, Review Comment: The type parameter seems not used in this constructor? ########## flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/operators/asyncprocessing/AbstractAsyncStateStreamOperator.java: ########## @@ -0,0 +1,127 @@ +/* + * 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.streaming.runtime.operators.asyncprocessing; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.annotation.VisibleForTesting; +import org.apache.flink.api.common.operators.MailboxExecutor; +import org.apache.flink.api.common.typeutils.TypeSerializer; +import org.apache.flink.api.java.functions.KeySelector; +import org.apache.flink.runtime.asyncprocessing.AsyncExecutionController; +import org.apache.flink.runtime.asyncprocessing.RecordContext; +import org.apache.flink.streaming.api.graph.StreamConfig; +import org.apache.flink.streaming.api.operators.AbstractStreamOperator; +import org.apache.flink.streaming.api.operators.Output; +import org.apache.flink.streaming.api.operators.TwoInputStreamOperator; +import org.apache.flink.streaming.runtime.streamrecord.StreamRecord; +import org.apache.flink.streaming.runtime.tasks.StreamTask; +import org.apache.flink.util.function.ThrowingConsumer; + +import java.lang.reflect.ParameterizedType; + +/** + * This operator is an abstract class that give the {@link AbstractStreamOperator} the ability to + * perform {@link AsyncStateProcessing}. The aim is to make any subclass of {@link + * AbstractStreamOperator} could manipulate async state with only a change of base class. + */ +@Internal +@SuppressWarnings("rawtypes") +public abstract class AbstractAsyncStateStreamOperator<OUT> extends AbstractStreamOperator<OUT> + implements AsyncStateProcessing { + + private AsyncExecutionController asyncExecutionController; + + private RecordContext lastProcessContext; + + /** Initialize necessary state components for {@link AbstractStreamOperator}. */ + @Override + public void setup( + StreamTask<?, ?> containingTask, + StreamConfig config, + Output<StreamRecord<OUT>> output) { + super.setup(containingTask, config, output); + // TODO: properly read config and setup + final MailboxExecutor mailboxExecutor = + containingTask.getEnvironment().getMainMailboxExecutor(); + this.asyncExecutionController = + new AsyncExecutionController(getTypeClassOfKey(), mailboxExecutor, null); + } + + private Class<?> getTypeClassOfKey() { + final TypeSerializer<?> keySerializer = + config.getStateKeySerializer(getUserCodeClassloader()); + return (Class) + ((ParameterizedType) keySerializer.getClass().getGenericSuperclass()) + .getActualTypeArguments()[0]; + } + + @Override + public final boolean isAsyncStateProcessingEnabled() { + // TODO: Read from config + return true; + } + + @Override + @SuppressWarnings("unchecked") + public final <T> void setAsyncKeyedContextElement( + StreamRecord<T> record, KeySelector<T, ?> keySelector) throws Exception { + lastProcessContext = + asyncExecutionController.buildContext( + record.getValue(), keySelector.getKey(record.getValue())); + lastProcessContext.retain(); + asyncExecutionController.setCurrentContext(lastProcessContext); + } + + @Override + public final void postProcessElement() { + lastProcessContext.release(); + } + + @Override + @SuppressWarnings("unchecked") + public final <T> ThrowingConsumer<StreamRecord<T>, Exception> getRecordProcessor( Review Comment: Instead of introducing `setAsyncKeyedContextElement`, `postProcessElement` and `getRecordProcessor`, how about the following implementation? ```java @Override public void processElement(StreamRecord<T> record){ lastProcessContext = asyncExecutionController.buildContext( record.getValue(), keySelector.getKey(record.getValue())); lastProcessContext.retain(); asyncExecutionController.setCurrentContext(lastProcessContext); super.processElement(record); lastProcessContext.release(); // getRecordProcessor is removed, since the implementation in RecordProcessUtils should be enough } ``` ########## flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/operators/asyncprocessing/AbstractAsyncStateStreamOperator.java: ########## @@ -0,0 +1,127 @@ +/* + * 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.streaming.runtime.operators.asyncprocessing; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.annotation.VisibleForTesting; +import org.apache.flink.api.common.operators.MailboxExecutor; +import org.apache.flink.api.common.typeutils.TypeSerializer; +import org.apache.flink.api.java.functions.KeySelector; +import org.apache.flink.runtime.asyncprocessing.AsyncExecutionController; +import org.apache.flink.runtime.asyncprocessing.RecordContext; +import org.apache.flink.streaming.api.graph.StreamConfig; +import org.apache.flink.streaming.api.operators.AbstractStreamOperator; +import org.apache.flink.streaming.api.operators.Output; +import org.apache.flink.streaming.api.operators.TwoInputStreamOperator; +import org.apache.flink.streaming.runtime.streamrecord.StreamRecord; +import org.apache.flink.streaming.runtime.tasks.StreamTask; +import org.apache.flink.util.function.ThrowingConsumer; + +import java.lang.reflect.ParameterizedType; + +/** + * This operator is an abstract class that give the {@link AbstractStreamOperator} the ability to + * perform {@link AsyncStateProcessing}. The aim is to make any subclass of {@link + * AbstractStreamOperator} could manipulate async state with only a change of base class. + */ +@Internal +@SuppressWarnings("rawtypes") +public abstract class AbstractAsyncStateStreamOperator<OUT> extends AbstractStreamOperator<OUT> Review Comment: Making the async functionalities an abstract class would prevent the operator from extending from other abstract classes. For example, a RichMapFunction should be a subclass of `AbstractUdfStreamOperator`, and since it has extended from this abstract class, it cannot extend from `AbstractAsyncStateStreamOperator` at the same time. ########## flink-streaming-java/src/main/java/org/apache/flink/streaming/runtime/operators/asyncprocessing/AbstractAsyncStateStreamOperator.java: ########## @@ -0,0 +1,127 @@ +/* + * 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.streaming.runtime.operators.asyncprocessing; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.annotation.VisibleForTesting; +import org.apache.flink.api.common.operators.MailboxExecutor; +import org.apache.flink.api.common.typeutils.TypeSerializer; +import org.apache.flink.api.java.functions.KeySelector; +import org.apache.flink.runtime.asyncprocessing.AsyncExecutionController; +import org.apache.flink.runtime.asyncprocessing.RecordContext; +import org.apache.flink.streaming.api.graph.StreamConfig; +import org.apache.flink.streaming.api.operators.AbstractStreamOperator; +import org.apache.flink.streaming.api.operators.Output; +import org.apache.flink.streaming.api.operators.TwoInputStreamOperator; +import org.apache.flink.streaming.runtime.streamrecord.StreamRecord; +import org.apache.flink.streaming.runtime.tasks.StreamTask; +import org.apache.flink.util.function.ThrowingConsumer; + +import java.lang.reflect.ParameterizedType; + +/** + * This operator is an abstract class that give the {@link AbstractStreamOperator} the ability to + * perform {@link AsyncStateProcessing}. The aim is to make any subclass of {@link + * AbstractStreamOperator} could manipulate async state with only a change of base class. + */ +@Internal +@SuppressWarnings("rawtypes") +public abstract class AbstractAsyncStateStreamOperator<OUT> extends AbstractStreamOperator<OUT> + implements AsyncStateProcessing { + + private AsyncExecutionController asyncExecutionController; + + private RecordContext lastProcessContext; + + /** Initialize necessary state components for {@link AbstractStreamOperator}. */ + @Override + public void setup( + StreamTask<?, ?> containingTask, + StreamConfig config, + Output<StreamRecord<OUT>> output) { + super.setup(containingTask, config, output); + // TODO: properly read config and setup + final MailboxExecutor mailboxExecutor = + containingTask.getEnvironment().getMainMailboxExecutor(); + this.asyncExecutionController = + new AsyncExecutionController(getTypeClassOfKey(), mailboxExecutor, null); + } + + private Class<?> getTypeClassOfKey() { + final TypeSerializer<?> keySerializer = + config.getStateKeySerializer(getUserCodeClassloader()); + return (Class) + ((ParameterizedType) keySerializer.getClass().getGenericSuperclass()) + .getActualTypeArguments()[0]; + } + + @Override + public final boolean isAsyncStateProcessingEnabled() { + // TODO: Read from config Review Comment: It might be better to configure `isAsyncStateProcessingEnabled` globally on the whole Flink job instead of at operators' granularity, and in that case this method would be unnecessary. -- 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. To unsubscribe, e-mail: issues-unsubscr...@flink.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org