Search in sources :

Example 1 with DataInputViewStream

use of org.apache.flink.api.java.typeutils.runtime.DataInputViewStream in project flink by apache.

the class RocksDBKeyedStateBackend method restoreOldSavepointKeyedState.

/**
	 * For backwards compatibility, remove again later!
	 */
@Deprecated
private void restoreOldSavepointKeyedState(Collection<KeyGroupsStateHandle> restoreState) throws Exception {
    if (restoreState.isEmpty()) {
        return;
    }
    Preconditions.checkState(1 == restoreState.size(), "Only one element expected here.");
    HashMap<String, RocksDBStateBackend.FinalFullyAsyncSnapshot> namedStates;
    try (FSDataInputStream inputStream = restoreState.iterator().next().openInputStream()) {
        namedStates = InstantiationUtil.deserializeObject(inputStream, userCodeClassLoader);
    }
    Preconditions.checkState(1 == namedStates.size(), "Only one element expected here.");
    DataInputView inputView = namedStates.values().iterator().next().stateHandle.getState(userCodeClassLoader);
    // clear k/v state information before filling it
    kvStateInformation.clear();
    // first get the column family mapping
    int numColumns = inputView.readInt();
    Map<Byte, StateDescriptor<?, ?>> columnFamilyMapping = new HashMap<>(numColumns);
    for (int i = 0; i < numColumns; i++) {
        byte mappingByte = inputView.readByte();
        ObjectInputStream ooIn = new InstantiationUtil.ClassLoaderObjectInputStream(new DataInputViewStream(inputView), userCodeClassLoader);
        StateDescriptor stateDescriptor = (StateDescriptor) ooIn.readObject();
        columnFamilyMapping.put(mappingByte, stateDescriptor);
        // this will fill in the k/v state information
        getColumnFamily(stateDescriptor, MigrationNamespaceSerializerProxy.INSTANCE);
    }
    // try and read until EOF
    try {
        // the EOFException will get us out of this...
        while (true) {
            byte mappingByte = inputView.readByte();
            ColumnFamilyHandle handle = getColumnFamily(columnFamilyMapping.get(mappingByte), MigrationNamespaceSerializerProxy.INSTANCE);
            byte[] keyAndNamespace = BytePrimitiveArraySerializer.INSTANCE.deserialize(inputView);
            ByteArrayInputStreamWithPos bis = new ByteArrayInputStreamWithPos(keyAndNamespace);
            K reconstructedKey = keySerializer.deserialize(new DataInputViewStreamWrapper(bis));
            int len = bis.getPosition();
            int keyGroup = (byte) KeyGroupRangeAssignment.assignToKeyGroup(reconstructedKey, numberOfKeyGroups);
            if (keyGroupPrefixBytes == 1) {
                // copy and override one byte (42) between key and namespace
                System.arraycopy(keyAndNamespace, 0, keyAndNamespace, 1, len);
                keyAndNamespace[0] = (byte) keyGroup;
            } else {
                byte[] largerKey = new byte[1 + keyAndNamespace.length];
                // write key-group
                largerKey[0] = (byte) ((keyGroup >> 8) & 0xFF);
                largerKey[1] = (byte) (keyGroup & 0xFF);
                // write key
                System.arraycopy(keyAndNamespace, 0, largerKey, 2, len);
                //skip one byte (42), write namespace
                System.arraycopy(keyAndNamespace, 1 + len, largerKey, 2 + len, keyAndNamespace.length - len - 1);
                keyAndNamespace = largerKey;
            }
            byte[] value = BytePrimitiveArraySerializer.INSTANCE.deserialize(inputView);
            db.put(handle, keyAndNamespace, value);
        }
    } catch (EOFException e) {
    // expected
    }
}
Also used : HashMap(java.util.HashMap) DataInputView(org.apache.flink.core.memory.DataInputView) DataInputViewStreamWrapper(org.apache.flink.core.memory.DataInputViewStreamWrapper) ColumnFamilyHandle(org.rocksdb.ColumnFamilyHandle) ByteArrayInputStreamWithPos(org.apache.flink.core.memory.ByteArrayInputStreamWithPos) ReducingStateDescriptor(org.apache.flink.api.common.state.ReducingStateDescriptor) ListStateDescriptor(org.apache.flink.api.common.state.ListStateDescriptor) MapStateDescriptor(org.apache.flink.api.common.state.MapStateDescriptor) AggregatingStateDescriptor(org.apache.flink.api.common.state.AggregatingStateDescriptor) StateDescriptor(org.apache.flink.api.common.state.StateDescriptor) ValueStateDescriptor(org.apache.flink.api.common.state.ValueStateDescriptor) FoldingStateDescriptor(org.apache.flink.api.common.state.FoldingStateDescriptor) EOFException(java.io.EOFException) FSDataInputStream(org.apache.flink.core.fs.FSDataInputStream) DataInputViewStream(org.apache.flink.api.java.typeutils.runtime.DataInputViewStream) ObjectInputStream(java.io.ObjectInputStream)

Example 2 with DataInputViewStream

use of org.apache.flink.api.java.typeutils.runtime.DataInputViewStream in project flink by apache.

the class KryoSerializer method deserialize.

@SuppressWarnings("unchecked")
@Override
public T deserialize(DataInputView source) throws IOException {
    checkKryoInitialized();
    if (source != previousIn) {
        DataInputViewStream inputStream = new DataInputViewStream(source);
        input = new NoFetchingInput(inputStream);
        previousIn = source;
    }
    try {
        return (T) kryo.readClassAndObject(input);
    } catch (KryoException ke) {
        Throwable cause = ke.getCause();
        if (cause instanceof EOFException) {
            throw (EOFException) cause;
        } else {
            throw ke;
        }
    }
}
Also used : KryoException(com.esotericsoftware.kryo.KryoException) EOFException(java.io.EOFException) DataInputViewStream(org.apache.flink.api.java.typeutils.runtime.DataInputViewStream) NoFetchingInput(org.apache.flink.api.java.typeutils.runtime.NoFetchingInput)

Aggregations

EOFException (java.io.EOFException)2 DataInputViewStream (org.apache.flink.api.java.typeutils.runtime.DataInputViewStream)2 KryoException (com.esotericsoftware.kryo.KryoException)1 ObjectInputStream (java.io.ObjectInputStream)1 HashMap (java.util.HashMap)1 AggregatingStateDescriptor (org.apache.flink.api.common.state.AggregatingStateDescriptor)1 FoldingStateDescriptor (org.apache.flink.api.common.state.FoldingStateDescriptor)1 ListStateDescriptor (org.apache.flink.api.common.state.ListStateDescriptor)1 MapStateDescriptor (org.apache.flink.api.common.state.MapStateDescriptor)1 ReducingStateDescriptor (org.apache.flink.api.common.state.ReducingStateDescriptor)1 StateDescriptor (org.apache.flink.api.common.state.StateDescriptor)1 ValueStateDescriptor (org.apache.flink.api.common.state.ValueStateDescriptor)1 NoFetchingInput (org.apache.flink.api.java.typeutils.runtime.NoFetchingInput)1 FSDataInputStream (org.apache.flink.core.fs.FSDataInputStream)1 ByteArrayInputStreamWithPos (org.apache.flink.core.memory.ByteArrayInputStreamWithPos)1 DataInputView (org.apache.flink.core.memory.DataInputView)1 DataInputViewStreamWrapper (org.apache.flink.core.memory.DataInputViewStreamWrapper)1 ColumnFamilyHandle (org.rocksdb.ColumnFamilyHandle)1