forked from apache/flink
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[FLINK-10543][table] Leverage efficient timer deletion in relational …
…operators This closes apache#6918
- Loading branch information
1 parent
eaead20
commit 5716e4d
Showing
24 changed files
with
423 additions
and
430 deletions.
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
...es/flink-table/src/main/scala/org/apache/flink/table/runtime/aggregate/CleanupState.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
/* | ||
* 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:https://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.aggregate | ||
|
||
import org.apache.flink.api.common.state.ValueState | ||
import org.apache.flink.streaming.api.functions.ProcessFunction | ||
import org.apache.flink.streaming.api.functions.co.CoProcessFunction | ||
import java.lang.{Long => JLong} | ||
|
||
import org.apache.flink.streaming.api.TimerService | ||
|
||
/** | ||
* Base class for clean up state, both for [[ProcessFunction]] and [[CoProcessFunction]]. | ||
*/ | ||
trait CleanupState { | ||
|
||
def registerProcessingCleanupTimer( | ||
cleanupTimeState: ValueState[JLong], | ||
currentTime: Long, | ||
minRetentionTime: Long, | ||
maxRetentionTime: Long, | ||
timerService: TimerService): Unit = { | ||
|
||
// last registered timer | ||
val curCleanupTime = cleanupTimeState.value() | ||
|
||
// check if a cleanup timer is registered and | ||
// that the current cleanup timer won't delete state we need to keep | ||
if (curCleanupTime == null || (currentTime + minRetentionTime) > curCleanupTime) { | ||
// we need to register a new (later) timer | ||
val cleanupTime = currentTime + maxRetentionTime | ||
// register timer and remember clean-up time | ||
timerService.registerProcessingTimeTimer(cleanupTime) | ||
// delete expired timer | ||
if (curCleanupTime != null) { | ||
timerService.deleteProcessingTimeTimer(curCleanupTime) | ||
} | ||
cleanupTimeState.update(cleanupTime) | ||
} | ||
} | ||
} |
69 changes: 69 additions & 0 deletions
69
...in/scala/org/apache/flink/table/runtime/aggregate/CoProcessFunctionWithCleanupState.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/* | ||
* 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:https://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.aggregate | ||
|
||
import java.lang.{Long => JLong} | ||
|
||
import org.apache.flink.api.common.state.{State, ValueState, ValueStateDescriptor} | ||
import org.apache.flink.streaming.api.TimeDomain | ||
import org.apache.flink.streaming.api.functions.co.CoProcessFunction | ||
import org.apache.flink.table.api.{StreamQueryConfig, Types} | ||
|
||
abstract class CoProcessFunctionWithCleanupState[IN1, IN2, OUT](queryConfig: StreamQueryConfig) | ||
extends CoProcessFunction[IN1, IN2, OUT] | ||
with CleanupState { | ||
|
||
protected val minRetentionTime: Long = queryConfig.getMinIdleStateRetentionTime | ||
protected val maxRetentionTime: Long = queryConfig.getMaxIdleStateRetentionTime | ||
protected val stateCleaningEnabled: Boolean = minRetentionTime > 1 | ||
|
||
// holds the latest registered cleanup timer | ||
private var cleanupTimeState: ValueState[JLong] = _ | ||
|
||
protected def initCleanupTimeState(stateName: String) { | ||
if (stateCleaningEnabled) { | ||
val cleanupTimeDescriptor: ValueStateDescriptor[JLong] = | ||
new ValueStateDescriptor[JLong](stateName, Types.LONG) | ||
cleanupTimeState = getRuntimeContext.getState(cleanupTimeDescriptor) | ||
} | ||
} | ||
|
||
protected def processCleanupTimer( | ||
ctx: CoProcessFunction[IN1, IN2, OUT]#Context, | ||
currentTime: Long): Unit = { | ||
if (stateCleaningEnabled) { | ||
registerProcessingCleanupTimer( | ||
cleanupTimeState, | ||
currentTime, | ||
minRetentionTime, | ||
maxRetentionTime, | ||
ctx.timerService() | ||
) | ||
} | ||
} | ||
|
||
protected def isProcessingTimeTimer(ctx: OnTimerContext): Boolean = { | ||
ctx.timeDomain() == TimeDomain.PROCESSING_TIME | ||
} | ||
|
||
protected def cleanupState(states: State*): Unit = { | ||
// clear all state | ||
states.foreach(_.clear()) | ||
this.cleanupTimeState.clear() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.