[ https://issues.apache.org/jira/browse/FLINK-10583?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16653691#comment-16653691 ]
ASF GitHub Bot commented on FLINK-10583: ---------------------------------------- pnowojski commented on a change in pull request #6871: [FLINK-10583][table] Add base TwoInputStreamOperator with TTL operator. URL: https://github.com/apache/flink/pull/6871#discussion_r225956138 ########## File path: flink-libraries/flink-table/src/main/scala/org/apache/flink/table/runtime/join/AbstractTwoInputStreamOperatorWithTTL.scala ########## @@ -0,0 +1,178 @@ +/* + * 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 + +import java.lang.{Long => JLong} + +import org.apache.flink.annotation.PublicEvolving +import org.apache.flink.api.common.state.{ValueState, ValueStateDescriptor} +import org.apache.flink.runtime.state.{VoidNamespace, VoidNamespaceSerializer} +import org.apache.flink.streaming.api.SimpleTimerService +import org.apache.flink.streaming.api.operators.{AbstractStreamOperator, InternalTimer, Triggerable, TwoInputStreamOperator} +import org.apache.flink.streaming.runtime.streamrecord.StreamRecord +import org.apache.flink.table.api.{StreamQueryConfig, Types} +import org.apache.flink.table.runtime.types.CRow + +/** + * An abstract [[TwoInputStreamOperator]] that allows its subclasses to clean + * up their state based on a TTL. This TTL should be specified in the provided + * [[StreamQueryConfig]]. + * + * For each known key, this operator registers a timer (in processing time) to + * fire after the TTL expires. When the timer fires, the subclass can decide which + * state to cleanup and what further action to take. + * + * This class takes care of maintaining at most one timer per key. + */ +@PublicEvolving +abstract class AbstractTwoInputStreamOperatorWithTTL( + queryConfig: StreamQueryConfig) + extends AbstractStreamOperator[CRow] + with TwoInputStreamOperator[CRow, CRow, CRow] + with Triggerable[Any, VoidNamespace] { + + private val minRetentionTime: Long = queryConfig.getMinIdleStateRetentionTime + private val maxRetentionTime: Long = queryConfig.getMaxIdleStateRetentionTime + private val stateCleaningEnabled: Boolean = minRetentionTime > 1 + + private val ACTIVE_CLEANUP_TIMESTAMP = "cleanup-timestamp" + private val TIMERS_STATE_NAME = "timers" + + // the latest registered cleanup timer + private var cleanupTimeState: ValueState[JLong] = _ + + protected var timerService: SimpleTimerService = _ + + override final def open(): Unit = { + + initializeTimerService() + + if (stateCleaningEnabled) { + val inputCntDescriptor: ValueStateDescriptor[JLong] = + new ValueStateDescriptor[JLong](ACTIVE_CLEANUP_TIMESTAMP, Types.LONG) + cleanupTimeState = getRuntimeContext.getState(inputCntDescriptor) + } + + onOpen() + } + + private def initializeTimerService(): Unit = { + + val internalTimerService = getInternalTimerService( + TIMERS_STATE_NAME, + VoidNamespaceSerializer.INSTANCE, + this) + + timerService = new SimpleTimerService(internalTimerService) + } + + override final def processElement1(element: StreamRecord[CRow]): Unit = { + onProcessElement1(element) + registerProcessingCleanupTimer() + } + + override final def processElement2(element: StreamRecord[CRow]): Unit = { + onProcessElement2(element) + registerProcessingCleanupTimer() + } + + private def registerProcessingCleanupTimer(): Unit = { + if (stateCleaningEnabled) { + val currentProcessingTime = timerService.currentProcessingTime() + val currentCleanupTime = cleanupTimeState.value() Review comment: I would avoid nulls and wrap this variable with `Option` and pass `Option` to the `updateCleanupTimer` method. Handling nullable variables without compilation errors enables is very dangerous and error prone. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org > Add support for state retention to the Processing Time versioned joins. > ----------------------------------------------------------------------- > > Key: FLINK-10583 > URL: https://issues.apache.org/jira/browse/FLINK-10583 > Project: Flink > Issue Type: Sub-task > Components: Table API & SQL > Affects Versions: 1.7.0 > Reporter: Kostas Kloudas > Assignee: Kostas Kloudas > Priority: Major > Labels: pull-request-available > Fix For: 1.7.0 > > -- This message was sent by Atlassian JIRA (v7.6.3#76005)