carp84 commented on a change in pull request #7674: [FLINK-10043] [State Backends] Refactor RocksDBKeyedStateBackend object construction/initialization/restore code URL: https://github.com/apache/flink/pull/7674#discussion_r257909986
########## File path: flink-state-backends/flink-statebackend-rocksdb/src/main/java/org/apache/flink/contrib/streaming/state/restore/RocksDBFullRestoreOperation.java ########## @@ -0,0 +1,276 @@ +/* + * 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.contrib.streaming.state.restore; + +import org.apache.flink.api.common.typeutils.TypeSerializerSchemaCompatibility; +import org.apache.flink.api.common.typeutils.base.array.BytePrimitiveArraySerializer; +import org.apache.flink.api.java.tuple.Tuple2; +import org.apache.flink.configuration.ConfigConstants; +import org.apache.flink.contrib.streaming.state.RocksDBKeyedStateBackend; +import org.apache.flink.contrib.streaming.state.RocksDBNativeMetricMonitor; +import org.apache.flink.contrib.streaming.state.RocksDBWriteBatchWrapper; +import org.apache.flink.contrib.streaming.state.StateColumnFamilyHandle; +import org.apache.flink.core.fs.CloseableRegistry; +import org.apache.flink.core.fs.FSDataInputStream; +import org.apache.flink.core.memory.DataInputView; +import org.apache.flink.core.memory.DataInputViewStreamWrapper; +import org.apache.flink.runtime.state.KeyGroupRange; +import org.apache.flink.runtime.state.KeyGroupsStateHandle; +import org.apache.flink.runtime.state.KeyedBackendSerializationProxy; +import org.apache.flink.runtime.state.KeyedStateHandle; +import org.apache.flink.runtime.state.RegisteredStateMetaInfoBase; +import org.apache.flink.runtime.state.SnappyStreamCompressionDecorator; +import org.apache.flink.runtime.state.StateSerializerProvider; +import org.apache.flink.runtime.state.StreamCompressionDecorator; +import org.apache.flink.runtime.state.UncompressedStreamCompressionDecorator; +import org.apache.flink.runtime.state.metainfo.StateMetaInfoSnapshot; +import org.apache.flink.util.IOUtils; +import org.apache.flink.util.Preconditions; +import org.apache.flink.util.StateMigrationException; + +import org.rocksdb.ColumnFamilyDescriptor; +import org.rocksdb.ColumnFamilyHandle; +import org.rocksdb.ColumnFamilyOptions; +import org.rocksdb.DBOptions; +import org.rocksdb.RocksDB; +import org.rocksdb.RocksDBException; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; +import java.util.Map; + +import static org.apache.flink.contrib.streaming.state.snapshot.RocksSnapshotUtil.END_OF_KEY_GROUP_MARK; +import static org.apache.flink.contrib.streaming.state.snapshot.RocksSnapshotUtil.clearMetaDataFollowsFlag; +import static org.apache.flink.contrib.streaming.state.snapshot.RocksSnapshotUtil.hasMetaDataFollowsFlag; + +/** + * Encapsulates the process of restoring a RocksDB instance from a full snapshot. + */ +public class RocksDBFullRestoreOperation<K> extends AbstractRocksDBRestoreOperation<K> { + /** + * Current key-groups state handle from which we restore key-groups. + */ + private KeyGroupsStateHandle currentKeyGroupsStateHandle; + /** + * Current input stream we obtained from currentKeyGroupsStateHandle. + */ + private FSDataInputStream currentStateHandleInStream; + /** + * Current data input view that wraps currentStateHandleInStream. + */ + private DataInputView currentStateHandleInView; + /** + * Current list of ColumnFamilyHandles for all column families we restore from currentKeyGroupsStateHandle. + */ + private List<ColumnFamilyHandle> currentStateHandleKVStateColumnFamilies; + /** + * The compression decorator that was used for writing the state, as determined by the meta data. + */ + private StreamCompressionDecorator keygroupStreamCompressionDecorator; + + private boolean isKeySerializerCompatibilityChecked; + + public RocksDBFullRestoreOperation(KeyGroupRange keyGroupRange, + int keyGroupPrefixBytes, + int numberOfTransferringThreads, + CloseableRegistry cancelStreamRegistry, + ClassLoader userCodeClassLoader, + Map<String, StateColumnFamilyHandle> kvStateInformation, + StateSerializerProvider<K> keySerializerProvider, + File instanceBasePath, + File instanceRocksDBPath, + DBOptions dbOptions, + ColumnFamilyOptions columnOptions, + RocksDBNativeMetricMonitor nativeMetricMonitor) { + super(keyGroupRange, + keyGroupPrefixBytes, + numberOfTransferringThreads, + cancelStreamRegistry, + userCodeClassLoader, + kvStateInformation, + keySerializerProvider, + instanceBasePath, + instanceRocksDBPath, + dbOptions, + columnOptions, + nativeMetricMonitor); + } + + /** + * Restores all key-groups data that is referenced by the passed state handles. + * @param keyedStateHandles List of all key groups state handles that shall be restored. + * @param db + */ + @Override + public RocksDBRestoreResult restore(Collection<KeyedStateHandle> keyedStateHandles, RocksDB db) + throws IOException, StateMigrationException, RocksDBException { + if (db == null) { + openDB(); + } else { + this.db = db; + } + for (KeyedStateHandle keyedStateHandle : keyedStateHandles) { + if (keyedStateHandle != null) { + + if (!(keyedStateHandle instanceof KeyGroupsStateHandle)) { + throw new IllegalStateException("Unexpected state handle type, " + + "expected: " + KeyGroupsStateHandle.class + + ", but found: " + keyedStateHandle.getClass()); + } + this.currentKeyGroupsStateHandle = (KeyGroupsStateHandle) keyedStateHandle; + restoreKeyGroupsInStateHandle(); + } + } + return new RocksDBRestoreResult(this.db, lastCompletedCheckpointId, backendUID, + restoredSstFiles, defaultColumnFamilyHandle); + } + + /** + * Restore one key groups state handle. + */ + private void restoreKeyGroupsInStateHandle() + throws IOException, StateMigrationException, RocksDBException { + try { + currentStateHandleInStream = currentKeyGroupsStateHandle.openInputStream(); + cancelStreamRegistry.registerCloseable(currentStateHandleInStream); + currentStateHandleInView = new DataInputViewStreamWrapper(currentStateHandleInStream); + restoreKVStateMetaData(); + restoreKVStateData(); + } finally { + if (cancelStreamRegistry.unregisterCloseable(currentStateHandleInStream)) { + IOUtils.closeQuietly(currentStateHandleInStream); + } + } + } + + /** + * Restore the KV-state / ColumnFamily meta data for all key-groups referenced by the current state handle. + */ + private void restoreKVStateMetaData() throws IOException, StateMigrationException, RocksDBException { + + // isSerializerPresenceRequired flag is set to false, since for the RocksDB state backend, + // deserialization of state happens lazily during runtime; we depend on the fact + // that the new serializer for states could be compatible, and therefore the restore can continue + // without old serializers required to be present. + KeyedBackendSerializationProxy<K> serializationProxy = + new KeyedBackendSerializationProxy<>(userCodeClassLoader); + + serializationProxy.read(currentStateHandleInView); + + if (!isKeySerializerCompatibilityChecked) { + // 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()); Review comment: Agreed but please allow me to leave this as the last task of this review round, after emptying `restore` of rocksdb backend and remove those many `if` to prevent double-restore. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org With regards, Apache Git Services