fredia commented on code in PR #25853: URL: https://github.com/apache/flink/pull/25853#discussion_r1900535515
########## flink-runtime/src/main/java/org/apache/flink/runtime/asyncprocessing/operators/AsyncKeyedProcessOperator.java: ########## @@ -0,0 +1,220 @@ +/* + * 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.runtime.asyncprocessing.operators; + +import org.apache.flink.annotation.Internal; +import org.apache.flink.api.common.typeutils.base.LongSerializer; +import org.apache.flink.api.common.typeutils.base.StringSerializer; +import org.apache.flink.runtime.asyncprocessing.declare.DeclarationContext; +import org.apache.flink.runtime.asyncprocessing.declare.DeclaredVariable; +import org.apache.flink.runtime.asyncprocessing.streaming.api.AsyncKeyedProcessFunction; +import org.apache.flink.runtime.state.VoidNamespace; +import org.apache.flink.runtime.state.VoidNamespaceSerializer; +import org.apache.flink.streaming.api.SimpleTimerService; +import org.apache.flink.streaming.api.TimeDomain; +import org.apache.flink.streaming.api.TimerService; +import org.apache.flink.streaming.api.operators.InternalTimer; +import org.apache.flink.streaming.api.operators.InternalTimerService; +import org.apache.flink.streaming.api.operators.OneInputStreamOperator; +import org.apache.flink.streaming.api.operators.StreamOperator; +import org.apache.flink.streaming.api.operators.Triggerable; +import org.apache.flink.streaming.runtime.streamrecord.StreamRecord; +import org.apache.flink.util.OutputTag; +import org.apache.flink.util.function.ThrowingConsumer; + +import static org.apache.flink.util.Preconditions.checkNotNull; +import static org.apache.flink.util.Preconditions.checkState; + +/** A {@link StreamOperator} for executing {@link AsyncKeyedProcessFunction}. */ +@Internal +public class AsyncKeyedProcessOperator<K, IN, OUT> + extends AbstractAsyncStateUdfStreamOperator<OUT, AsyncKeyedProcessFunction<K, IN, OUT>> + implements OneInputStreamOperator<IN, OUT>, Triggerable<K, VoidNamespace> { + + private static final long serialVersionUID = 1L; + + // Shared timestamp variable for collector, context and onTimerContext. + private transient DeclaredVariable<Long> sharedTimestamp; + + private transient TimestampedCollectorWithDeclaredVariable<OUT> collector; + + private transient ContextImpl context; + + private transient OnTimerContextImpl onTimerContext; + + private transient DeclarationContext declarationContext; + + private transient ThrowingConsumer<IN, Exception> processor; + private transient ThrowingConsumer<Long, Exception> timerProcessor; + + public AsyncKeyedProcessOperator(AsyncKeyedProcessFunction<K, IN, OUT> function) { + super(function); + } + + @Override + @SuppressWarnings({"unchecked", "rawtypes"}) + public void open() throws Exception { + super.open(); + declarationContext = new DeclarationContext(getDeclarationManager()); + userFunction.open(declarationContext); + sharedTimestamp = + declarationContext.declareVariable( + LongSerializer.INSTANCE, + "_AsyncKeyedProcessOperator$sharedTimestamp", + null); + + collector = new TimestampedCollectorWithDeclaredVariable<>(output, sharedTimestamp); + + InternalTimerService<VoidNamespace> internalTimerService = + getInternalTimerService("user-timers", VoidNamespaceSerializer.INSTANCE, this); + + TimerService timerService = new SimpleTimerService(internalTimerService); + + context = new ContextImpl(userFunction, timerService, sharedTimestamp); + onTimerContext = + new OnTimerContextImpl( + userFunction, timerService, declarationContext, sharedTimestamp); + + processor = userFunction.declareProcess(declarationContext, context, collector); + timerProcessor = userFunction.declareOnTimer(declarationContext, onTimerContext, collector); + } + + @Override + public void onEventTime(InternalTimer<K, VoidNamespace> timer) throws Exception { + collector.setAbsoluteTimestamp(timer.getTimestamp()); + invokeUserFunction(TimeDomain.EVENT_TIME, timer); + } + + @Override + public void onProcessingTime(InternalTimer<K, VoidNamespace> timer) throws Exception { + collector.eraseTimestamp(); + invokeUserFunction(TimeDomain.PROCESSING_TIME, timer); + } + + @Override + public void processElement(StreamRecord<IN> element) throws Exception { + collector.setTimestamp(element); + processor.accept(element.getValue()); Review Comment: 👍Thanks for the clarification. -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
