Search in sources :

Example 11 with StateMetaInfoSnapshot

use of org.apache.flink.runtime.state.metainfo.StateMetaInfoSnapshot in project flink by apache.

the class HeapMetaInfoRestoreOperation method createOrCheckStateForMetaInfo.

Map<Integer, StateMetaInfoSnapshot> createOrCheckStateForMetaInfo(List<StateMetaInfoSnapshot> restoredMetaInfo, Map<String, StateTable<K, ?, ?>> registeredKVStates, Map<String, HeapPriorityQueueSnapshotRestoreWrapper<?>> registeredPQStates) {
    final Map<Integer, StateMetaInfoSnapshot> kvStatesById = new HashMap<>();
    for (StateMetaInfoSnapshot metaInfoSnapshot : restoredMetaInfo) {
        final StateSnapshotRestore registeredState;
        switch(metaInfoSnapshot.getBackendStateType()) {
            case KEY_VALUE:
                registeredState = registeredKVStates.get(metaInfoSnapshot.getName());
                if (registeredState == null) {
                    RegisteredKeyValueStateBackendMetaInfo<?, ?> registeredKeyedBackendStateMetaInfo = new RegisteredKeyValueStateBackendMetaInfo<>(metaInfoSnapshot);
                    registeredKVStates.put(metaInfoSnapshot.getName(), stateTableFactory.newStateTable(keyContext, registeredKeyedBackendStateMetaInfo, keySerializerProvider.currentSchemaSerializer()));
                }
                break;
            case PRIORITY_QUEUE:
                registeredState = registeredPQStates.get(metaInfoSnapshot.getName());
                if (registeredState == null) {
                    registeredPQStates.put(metaInfoSnapshot.getName(), createInternal(new RegisteredPriorityQueueStateBackendMetaInfo<>(metaInfoSnapshot)));
                }
                break;
            default:
                throw new IllegalStateException("Unexpected state type: " + metaInfoSnapshot.getBackendStateType() + ".");
        }
        // always put metaInfo into kvStatesById, because kvStatesById is KeyGroupsStateHandle
        // related
        kvStatesById.put(kvStatesById.size(), metaInfoSnapshot);
    }
    return kvStatesById;
}
Also used : HashMap(java.util.HashMap) StateMetaInfoSnapshot(org.apache.flink.runtime.state.metainfo.StateMetaInfoSnapshot) StateSnapshotRestore(org.apache.flink.runtime.state.StateSnapshotRestore) RegisteredKeyValueStateBackendMetaInfo(org.apache.flink.runtime.state.RegisteredKeyValueStateBackendMetaInfo) RegisteredPriorityQueueStateBackendMetaInfo(org.apache.flink.runtime.state.RegisteredPriorityQueueStateBackendMetaInfo)

Example 12 with StateMetaInfoSnapshot

use of org.apache.flink.runtime.state.metainfo.StateMetaInfoSnapshot in project flink by apache.

the class HeapRestoreOperation method restore.

@Override
public Void restore() throws Exception {
    registeredKVStates.clear();
    registeredPQStates.clear();
    boolean keySerializerRestored = false;
    for (KeyedStateHandle keyedStateHandle : restoreStateHandles) {
        if (keyedStateHandle == null) {
            continue;
        }
        if (!(keyedStateHandle instanceof KeyGroupsStateHandle)) {
            throw unexpectedStateHandleException(KeyGroupsStateHandle.class, keyedStateHandle.getClass());
        }
        LOG.info("Starting to restore from state handle: {}.", keyedStateHandle);
        KeyGroupsStateHandle keyGroupsStateHandle = (KeyGroupsStateHandle) keyedStateHandle;
        FSDataInputStream fsDataInputStream = keyGroupsStateHandle.openInputStream();
        cancelStreamRegistry.registerCloseable(fsDataInputStream);
        try {
            DataInputViewStreamWrapper inView = new DataInputViewStreamWrapper(fsDataInputStream);
            KeyedBackendSerializationProxy<K> serializationProxy = new KeyedBackendSerializationProxy<>(userCodeClassLoader);
            serializationProxy.read(inView);
            if (!keySerializerRestored) {
                // fetch current serializer now because if it is incompatible, we can't access
                // it anymore to improve the error message
                TypeSerializer<K> currentSerializer = keySerializerProvider.currentSchemaSerializer();
                // check for key serializer compatibility; this also reconfigures the
                // key serializer to be compatible, if it is required and is possible
                TypeSerializerSchemaCompatibility<K> keySerializerSchemaCompat = keySerializerProvider.setPreviousSerializerSnapshotForRestoredState(serializationProxy.getKeySerializerSnapshot());
                if (keySerializerSchemaCompat.isCompatibleAfterMigration() || keySerializerSchemaCompat.isIncompatible()) {
                    throw new StateMigrationException("The new key serializer (" + currentSerializer + ") must be compatible with the previous key serializer (" + keySerializerProvider.previousSchemaSerializer() + ").");
                }
                keySerializerRestored = true;
            }
            List<StateMetaInfoSnapshot> restoredMetaInfos = serializationProxy.getStateMetaInfoSnapshots();
            final Map<Integer, StateMetaInfoSnapshot> kvStatesById = this.heapMetaInfoRestoreOperation.createOrCheckStateForMetaInfo(restoredMetaInfos, registeredKVStates, registeredPQStates);
            readStateHandleStateData(fsDataInputStream, inView, keyGroupsStateHandle.getGroupRangeOffsets(), kvStatesById, restoredMetaInfos.size(), serializationProxy.getReadVersion(), serializationProxy.isUsingKeyGroupCompression());
            LOG.info("Finished restoring from state handle: {}.", keyedStateHandle);
        } finally {
            if (cancelStreamRegistry.unregisterCloseable(fsDataInputStream)) {
                IOUtils.closeQuietly(fsDataInputStream);
            }
        }
    }
    return null;
}
Also used : KeyedBackendSerializationProxy(org.apache.flink.runtime.state.KeyedBackendSerializationProxy) StateMetaInfoSnapshot(org.apache.flink.runtime.state.metainfo.StateMetaInfoSnapshot) KeyedStateHandle(org.apache.flink.runtime.state.KeyedStateHandle) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) KeyGroupsStateHandle(org.apache.flink.runtime.state.KeyGroupsStateHandle) StateMigrationException(org.apache.flink.util.StateMigrationException) FSDataInputStream(org.apache.flink.core.fs.FSDataInputStream)

Example 13 with StateMetaInfoSnapshot

use of org.apache.flink.runtime.state.metainfo.StateMetaInfoSnapshot in project flink by apache.

the class HeapRestoreOperation method readKeyGroupStateData.

private void readKeyGroupStateData(InputStream inputStream, Map<Integer, StateMetaInfoSnapshot> kvStatesById, int keyGroupIndex, int numStates, int readVersion) throws IOException {
    DataInputViewStreamWrapper inView = new DataInputViewStreamWrapper(inputStream);
    for (int i = 0; i < numStates; i++) {
        final int kvStateId = inView.readShort();
        final StateMetaInfoSnapshot stateMetaInfoSnapshot = kvStatesById.get(kvStateId);
        final StateSnapshotRestore registeredState;
        switch(stateMetaInfoSnapshot.getBackendStateType()) {
            case KEY_VALUE:
                registeredState = registeredKVStates.get(stateMetaInfoSnapshot.getName());
                break;
            case PRIORITY_QUEUE:
                registeredState = registeredPQStates.get(stateMetaInfoSnapshot.getName());
                break;
            default:
                throw new IllegalStateException("Unexpected state type: " + stateMetaInfoSnapshot.getBackendStateType() + ".");
        }
        StateSnapshotKeyGroupReader keyGroupReader = registeredState.keyGroupReader(readVersion);
        keyGroupReader.readMappingsInKeyGroup(inView, keyGroupIndex);
    }
}
Also used : StateSnapshotKeyGroupReader(org.apache.flink.runtime.state.StateSnapshotKeyGroupReader) StateMetaInfoSnapshot(org.apache.flink.runtime.state.metainfo.StateMetaInfoSnapshot) StateSnapshotRestore(org.apache.flink.runtime.state.StateSnapshotRestore) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper)

Example 14 with StateMetaInfoSnapshot

use of org.apache.flink.runtime.state.metainfo.StateMetaInfoSnapshot in project flink by apache.

the class HeapSavepointRestoreOperation method restore.

@Override
public Void restore() throws Exception {
    registeredKVStates.clear();
    registeredPQStates.clear();
    try (ThrowingIterator<SavepointRestoreResult> restore = this.savepointRestoreOperation.restore()) {
        while (restore.hasNext()) {
            SavepointRestoreResult restoreResult = restore.next();
            List<StateMetaInfoSnapshot> restoredMetaInfos = restoreResult.getStateMetaInfoSnapshots();
            final Map<Integer, StateMetaInfoSnapshot> kvStatesById = this.heapMetaInfoRestoreOperation.createOrCheckStateForMetaInfo(restoredMetaInfos, registeredKVStates, registeredPQStates);
            try (ThrowingIterator<KeyGroup> keyGroups = restoreResult.getRestoredKeyGroups()) {
                while (keyGroups.hasNext()) {
                    readKeyGroupStateData(keyGroups.next(), keySerializerProvider.previousSchemaSerializer(), kvStatesById);
                }
            }
        }
    }
    return null;
}
Also used : CompositeKeySerializationUtils.readKeyGroup(org.apache.flink.runtime.state.CompositeKeySerializationUtils.readKeyGroup) KeyGroup(org.apache.flink.runtime.state.restore.KeyGroup) StateMetaInfoSnapshot(org.apache.flink.runtime.state.metainfo.StateMetaInfoSnapshot) SavepointRestoreResult(org.apache.flink.runtime.state.restore.SavepointRestoreResult)

Example 15 with StateMetaInfoSnapshot

use of org.apache.flink.runtime.state.metainfo.StateMetaInfoSnapshot in project flink by apache.

the class RocksIncrementalSnapshotStrategy method syncPrepareResources.

@Override
public IncrementalRocksDBSnapshotResources syncPrepareResources(long checkpointId) throws Exception {
    final SnapshotDirectory snapshotDirectory = prepareLocalSnapshotDirectory(checkpointId);
    LOG.trace("Local RocksDB checkpoint goes to backup path {}.", snapshotDirectory);
    final List<StateMetaInfoSnapshot> stateMetaInfoSnapshots = new ArrayList<>(kvStateInformation.size());
    final PreviousSnapshot previousSnapshot = snapshotMetaData(checkpointId, stateMetaInfoSnapshots);
    takeDBNativeCheckpoint(snapshotDirectory);
    return new IncrementalRocksDBSnapshotResources(snapshotDirectory, previousSnapshot, stateMetaInfoSnapshots);
}
Also used : ArrayList(java.util.ArrayList) StateMetaInfoSnapshot(org.apache.flink.runtime.state.metainfo.StateMetaInfoSnapshot) SnapshotDirectory(org.apache.flink.runtime.state.SnapshotDirectory)

Aggregations

StateMetaInfoSnapshot (org.apache.flink.runtime.state.metainfo.StateMetaInfoSnapshot)23 DataInputViewStreamWrapper (org.apache.flink.core.memory.DataInputViewStreamWrapper)8 ArrayList (java.util.ArrayList)7 DataOutputViewStreamWrapper (org.apache.flink.core.memory.DataOutputViewStreamWrapper)7 ByteArrayInputStreamWithPos (org.apache.flink.core.memory.ByteArrayInputStreamWithPos)5 ByteArrayOutputStreamWithPos (org.apache.flink.core.memory.ByteArrayOutputStreamWithPos)5 IOException (java.io.IOException)4 HashMap (java.util.HashMap)4 Test (org.junit.Test)4 Map (java.util.Map)3 StateMetaInfoReader (org.apache.flink.runtime.state.metainfo.StateMetaInfoReader)3 KeyGroup (org.apache.flink.runtime.state.restore.KeyGroup)3 ColumnFamilyHandle (org.rocksdb.ColumnFamilyHandle)3 List (java.util.List)2 Nonnull (javax.annotation.Nonnull)2 RocksDbKvStateInfo (org.apache.flink.contrib.streaming.state.RocksDBKeyedStateBackend.RocksDbKvStateInfo)2 FSDataInputStream (org.apache.flink.core.fs.FSDataInputStream)2 CheckpointOptions (org.apache.flink.runtime.checkpoint.CheckpointOptions)2 KeyGroupsStateHandle (org.apache.flink.runtime.state.KeyGroupsStateHandle)2 KeyedBackendSerializationProxy (org.apache.flink.runtime.state.KeyedBackendSerializationProxy)2