All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.flink.runtime.state.metrics.AbstractLatencyTrackState Maven / Gradle / Ivy

/*
 * 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.metrics;

import org.apache.flink.annotation.VisibleForTesting;
import org.apache.flink.api.common.typeutils.TypeSerializer;
import org.apache.flink.runtime.state.internal.InternalKvState;
import org.apache.flink.util.function.SupplierWithException;
import org.apache.flink.util.function.ThrowingRunnable;

import java.io.IOException;
import java.util.function.Supplier;

/**
 * Abstract implementation of latency tracking state.
 *
 * @param  The type of key the state is associated to
 * @param  The type of the namespace
 * @param  Type of the user entry value of state
 * @param  Type of the internal kv state
 * @param  Type of the latency tracking state metrics
 */
class AbstractLatencyTrackState<
                K, N, V, S extends InternalKvState, LSM extends StateLatencyMetricBase>
        implements InternalKvState {

    protected S original;
    protected LSM latencyTrackingStateMetric;

    AbstractLatencyTrackState(S original, LSM latencyTrackingStateMetric) {
        this.original = original;
        this.latencyTrackingStateMetric = latencyTrackingStateMetric;
    }

    @Override
    public TypeSerializer getKeySerializer() {
        return original.getKeySerializer();
    }

    @Override
    public TypeSerializer getNamespaceSerializer() {
        return original.getNamespaceSerializer();
    }

    @Override
    public TypeSerializer getValueSerializer() {
        return original.getValueSerializer();
    }

    @Override
    public void setCurrentNamespace(N namespace) {
        original.setCurrentNamespace(namespace);
    }

    @Override
    public byte[] getSerializedValue(
            byte[] serializedKeyAndNamespace,
            TypeSerializer safeKeySerializer,
            TypeSerializer safeNamespaceSerializer,
            TypeSerializer safeValueSerializer)
            throws Exception {
        return original.getSerializedValue(
                serializedKeyAndNamespace,
                safeKeySerializer,
                safeNamespaceSerializer,
                safeValueSerializer);
    }

    @Override
    public StateIncrementalVisitor getStateIncrementalVisitor(
            int recommendedMaxNumberOfReturnedRecords) {
        return original.getStateIncrementalVisitor(recommendedMaxNumberOfReturnedRecords);
    }

    @Override
    public void clear() {
        if (latencyTrackingStateMetric.trackLatencyOnClear()) {
            trackLatency(original::clear, StateLatencyMetricBase.STATE_CLEAR_LATENCY);
        } else {
            original.clear();
        }
    }

    protected  T trackLatency(Supplier supplier, String latencyLabel) {
        long startTime = System.nanoTime();
        T result = supplier.get();
        long latency = System.nanoTime() - startTime;
        latencyTrackingStateMetric.updateLatency(latencyLabel, latency);
        return result;
    }

    protected  T trackLatencyWithIOException(
            SupplierWithException supplier, String latencyLabel)
            throws IOException {
        long startTime = System.nanoTime();
        T result = supplier.get();
        long latency = System.nanoTime() - startTime;
        latencyTrackingStateMetric.updateLatency(latencyLabel, latency);
        return result;
    }

    protected void trackLatencyWithIOException(
            ThrowingRunnable runnable, String latencyLabel) throws IOException {
        long startTime = System.nanoTime();
        runnable.run();
        long latency = System.nanoTime() - startTime;
        latencyTrackingStateMetric.updateLatency(latencyLabel, latency);
    }

    protected  T trackLatencyWithException(
            SupplierWithException supplier, String latencyLabel) throws Exception {
        long startTime = System.nanoTime();
        T result = supplier.get();
        long latency = System.nanoTime() - startTime;
        latencyTrackingStateMetric.updateLatency(latencyLabel, latency);
        return result;
    }

    protected void trackLatencyWithException(
            ThrowingRunnable runnable, String latencyLabel) throws Exception {
        long startTime = System.nanoTime();
        runnable.run();
        long latency = System.nanoTime() - startTime;
        latencyTrackingStateMetric.updateLatency(latencyLabel, latency);
    }

    protected void trackLatency(Runnable runnable, String latencyLabel) {
        long startTime = System.nanoTime();
        runnable.run();
        long latency = System.nanoTime() - startTime;
        latencyTrackingStateMetric.updateLatency(latencyLabel, latency);
    }

    @VisibleForTesting
    LSM getLatencyTrackingStateMetric() {
        return latencyTrackingStateMetric;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy