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

org.apache.flink.runtime.state.KeyedStateBackend Maven / Gradle / Ivy

There is a newer version: 1.13.6
Show 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;

import org.apache.flink.api.common.state.State;
import org.apache.flink.api.common.state.StateDescriptor;
import org.apache.flink.api.common.typeutils.TypeSerializer;
import org.apache.flink.util.Disposable;

import java.util.stream.Stream;

/**
 * A keyed state backend provides methods for managing keyed state.
 *
 * @param  The key by which state is keyed.
 */
public interface KeyedStateBackend
	extends KeyedStateFactory, PriorityQueueSetFactory, Disposable {

	/**
	 * Sets the current key that is used for partitioned state.
	 * @param newKey The new current key.
	 */
	void setCurrentKey(K newKey);

	/**
	 * @return Current key.
	 */
	K getCurrentKey();

	/**
	 * @return Serializer of the key.
	 */
	TypeSerializer getKeySerializer();

	/**
	 * Applies the provided {@link KeyedStateFunction} to the state with the provided
	 * {@link StateDescriptor} of all the currently active keys.
	 *
	 * @param namespace the namespace of the state.
	 * @param namespaceSerializer the serializer for the namespace.
	 * @param stateDescriptor the descriptor of the state to which the function is going to be applied.
	 * @param function the function to be applied to the keyed state.
	 *
	 * @param  The type of the namespace.
	 * @param  The type of the state.
	 */
	 void applyToAllKeys(
			final N namespace,
			final TypeSerializer namespaceSerializer,
			final StateDescriptor stateDescriptor,
			final KeyedStateFunction function) throws Exception;

	/**
	 * @return A stream of all keys for the given state and namespace. Modifications to the state during iterating
	 * 		   over it keys are not supported.
	 * @param state State variable for which existing keys will be returned.
	 * @param namespace Namespace for which existing keys will be returned.
	 */
	 Stream getKeys(String state, N namespace);

	/**
	 * Creates or retrieves a keyed state backed by this state backend.
	 *
	 * @param namespaceSerializer The serializer used for the namespace type of the state
	 * @param stateDescriptor The identifier for the state. This contains name and can create a default state value.
	 *
	 * @param  The type of the namespace.
	 * @param  The type of the state.
	 *
	 * @return A new key/value state backed by this backend.
	 *
	 * @throws Exception Exceptions may occur during initialization of the state and should be forwarded.
	 */
	 S getOrCreateKeyedState(
			TypeSerializer namespaceSerializer,
			StateDescriptor stateDescriptor) throws Exception;

	/**
	 * Creates or retrieves a partitioned state backed by this state backend.
	 *
	 * TODO: NOTE: This method does a lot of work caching / retrieving states just to update the namespace.
	 *       This method should be removed for the sake of namespaces being lazily fetched from the keyed
	 *       state backend, or being set on the state directly.
	 *
	 * @param stateDescriptor The identifier for the state. This contains name and can create a default state value.
	 *
	 * @param  The type of the namespace.
	 * @param  The type of the state.
	 *
	 * @return A new key/value state backed by this backend.
	 *
	 * @throws Exception Exceptions may occur during initialization of the state and should be forwarded.
	 */
	 S getPartitionedState(
			N namespace,
			TypeSerializer namespaceSerializer,
			StateDescriptor stateDescriptor) throws Exception;

	@Override
	void dispose();

	/** State backend will call {@link KeySelectionListener#keySelected} when key context is switched if supported. */
	void registerKeySelectionListener(KeySelectionListener listener);

	/**
	 * Stop calling listener registered in {@link #registerKeySelectionListener}.
	 *
	 * @return returns true iff listener was registered before.
	 */
	boolean deregisterKeySelectionListener(KeySelectionListener listener);

	/** Listener is given a callback when {@link #setCurrentKey} is called (key context changes). */
	@FunctionalInterface
	interface KeySelectionListener {
		/** Callback when key context is switched. */
		void keySelected(K newKey);
	}
}