org.apache.flink.runtime.state.OperatorStreamStateHandle 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;
import org.apache.flink.core.fs.FSDataInputStream;
import org.apache.flink.runtime.state.filesystem.FileSegmentStateHandle;
import org.apache.flink.util.Preconditions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.IOException;
import java.util.Map;
/**
* State handle for partitionable operator state. Besides being a {@link StreamStateHandle}, this also provides a
* map that contains the offsets to the partitions of named states in the stream.
*/
public class OperatorStreamStateHandle implements OperatorStateHandle {
private static final Logger LOG = LoggerFactory.getLogger(OperatorStateHandle.class);
private static final long serialVersionUID = 35876522969227335L;
/**
* unique state name -> offsets for available partitions in the handle stream
*/
private final Map stateNameToPartitionOffsets;
private final StreamStateHandle delegateStateHandle;
private transient SharedStateRegistry operatorStateRegistry;
public OperatorStreamStateHandle(
Map stateNameToPartitionOffsets,
StreamStateHandle delegateStateHandle) {
this.delegateStateHandle = Preconditions.checkNotNull(delegateStateHandle);
this.stateNameToPartitionOffsets = Preconditions.checkNotNull(stateNameToPartitionOffsets);
operatorStateRegistry = null;
}
@Override
public Map getStateNameToPartitionOffsets() {
return stateNameToPartitionOffsets;
}
@Override
public void discardState() throws Exception {
SharedStateRegistry registry = this.operatorStateRegistry;
final boolean isRegistered = (registry != null);
if (delegateStateHandle instanceof FileSegmentStateHandle && isRegistered) {
SharedStateRegistryKey registryKey = ((FileSegmentStateHandle) delegateStateHandle).getRegistryKey();
registry.unregisterReference(registryKey);
LOG.debug("UnregisterReference for operator segment state handle key {}, handle {}.", registryKey, delegateStateHandle);
} else {
delegateStateHandle.discardState();
LOG.debug("Discard operator state directly, handle {}.", delegateStateHandle);
}
}
@Override
public void registerOperatorStatesIfNeeded(SharedStateRegistry stateRegistry) {
// This is a quick check to avoid that we register twice with the same registry. However, the code allows to
// register again with a different registry. The implication is that ownership is transferred to this new
// registry. This should only happen in case of a restart, when the CheckpointCoordinator creates a new
// SharedStateRegistry for the current attempt and the old registry becomes meaningless. We also assume that
// an old registry object from a previous run is due to be GCed and will never be used for registration again.
if (delegateStateHandle instanceof FileSegmentStateHandle) {
Preconditions.checkState(operatorStateRegistry != stateRegistry,
"The state handle has already registered its shared states to the given registry.");
operatorStateRegistry = Preconditions.checkNotNull(stateRegistry);
SharedStateRegistryKey registryKey = ((FileSegmentStateHandle) delegateStateHandle).getRegistryKey();
stateRegistry.registerReference(registryKey, delegateStateHandle);
LOG.debug("Register segment state handle for operator state key {}, state {}.", registryKey, delegateStateHandle);
}
}
@Override
public long getStateSize() {
return delegateStateHandle.getStateSize();
}
@Override
public FSDataInputStream openInputStream() throws IOException {
return delegateStateHandle.openInputStream();
}
@Override
public StreamStateHandle getDelegateStateHandle() {
return delegateStateHandle;
}
@Override
public boolean equals(Object o) {
if (this == o) {
return true;
}
if (!(o instanceof OperatorStreamStateHandle)) {
return false;
}
OperatorStreamStateHandle that = (OperatorStreamStateHandle) o;
if (stateNameToPartitionOffsets.size() != that.stateNameToPartitionOffsets.size()) {
return false;
}
for (Map.Entry entry : stateNameToPartitionOffsets.entrySet()) {
if (!entry.getValue().equals(that.stateNameToPartitionOffsets.get(entry.getKey()))) {
return false;
}
}
return delegateStateHandle.equals(that.delegateStateHandle);
}
@Override
public int hashCode() {
int result = delegateStateHandle.hashCode();
for (Map.Entry entry : stateNameToPartitionOffsets.entrySet()) {
int entryHash = entry.getKey().hashCode();
if (entry.getValue() != null) {
entryHash += entry.getValue().hashCode();
}
result = 31 * result + entryHash;
}
return result;
}
@Override
public String toString() {
return "OperatorStateHandle{" +
"stateNameToPartitionOffsets=" + stateNameToPartitionOffsets +
", delegateStateHandle=" + delegateStateHandle +
'}';
}
}