
org.apache.flink.runtime.state.ttl.TtlAggregateFunction Maven / Gradle / Ivy
The newest version!
/*
* 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.state.ttl;
import org.apache.flink.api.common.functions.AggregateFunction;
import org.apache.flink.api.common.state.StateTtlConfig;
import org.apache.flink.util.FlinkRuntimeException;
import org.apache.flink.util.Preconditions;
import org.apache.flink.util.function.ThrowingConsumer;
import org.apache.flink.util.function.ThrowingRunnable;
/**
* This class wraps aggregating function with TTL logic.
*
* @param The type of the values that are aggregated (input values)
* @param The type of the accumulator (intermediate aggregate state).
* @param The type of the aggregated result
*/
class TtlAggregateFunction
extends AbstractTtlDecorator>
implements AggregateFunction, OUT> {
ThrowingRunnable stateClear;
ThrowingConsumer, Exception> updater;
TtlAggregateFunction(
AggregateFunction aggFunction,
StateTtlConfig config,
TtlTimeProvider timeProvider) {
super(aggFunction, config, timeProvider);
}
@Override
public TtlValue createAccumulator() {
return wrapWithTs(original.createAccumulator());
}
@Override
public TtlValue add(IN value, TtlValue accumulator) {
ACC userAcc = getUnexpired(accumulator);
userAcc = userAcc == null ? original.createAccumulator() : userAcc;
return wrapWithTs(original.add(value, userAcc));
}
@Override
public OUT getResult(TtlValue accumulator) {
Preconditions.checkNotNull(updater, "State updater should be set in TtlAggregatingState");
Preconditions.checkNotNull(
stateClear, "State clearing should be set in TtlAggregatingState");
ACC userAcc;
try {
userAcc = getWithTtlCheckAndUpdate(() -> accumulator, updater, stateClear);
} catch (Exception e) {
throw new FlinkRuntimeException(
"Failed to retrieve original internal aggregating state", e);
}
return userAcc == null ? null : original.getResult(userAcc);
}
@Override
public TtlValue merge(TtlValue a, TtlValue b) {
ACC userA = getUnexpired(a);
ACC userB = getUnexpired(b);
if (userA != null && userB != null) {
return wrapWithTs(original.merge(userA, userB));
} else if (userA != null) {
return rewrapWithNewTs(a);
} else if (userB != null) {
return rewrapWithNewTs(b);
} else {
return null;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy