Code review comment: Why are some checks performed outside the `checkAndPrepareRestore ` method and some checks that immediately return are done inside?

Code before review:
public RocksDBKeyedStateBackend<K> build() throws IOException {
RocksDBKeyedStateBackend<K> backend = null;
RocksDBWriteBatchWrapper writeBatchWrapper = null;
ColumnFamilyHandle defaultColumnFamilyHandle = null;
RocksDBNativeMetricMonitor nativeMetricMonitor = null;
try {
prepareDirectories();
RocksDBIncrementalRestorePrepareResult restorePrepareResult = null;
if (restoreStateHandles != null && !restoreStateHandles.isEmpty()) {
restorePrepareResult = checkAndPrepareRestore();
}
if (db == null) {
db = RocksDBOperationUtils.openDB(dbPath, columnFamilyDescriptors,
columnFamilyHandles, columnFamilyOptions, dbOptions);
defaultColumnFamilyHandle = columnFamilyHandles.remove(0);
}
writeBatchWrapper = new RocksDBWriteBatchWrapper(db, writeOptions);
initializeSnapshotStrategy();
if (nativeMetricOptions.isEnabled()) {
nativeMetricMonitor = new RocksDBNativeMetricMonitor(db, nativeMetricOptions, metricGroup);
}
backend = new RocksDBKeyedStateBackend<>(
this.operatorIdentifier,
this.userCodeClassLoader,
this.instanceBasePath,
this.dbOptions,
this.columnFamilyOptions,
this.kvStateRegistry,
this.keySerializer,
this.numberOfKeyGroups,
this.keyGroupRange,
this.executionConfig,
this.numberOfTransferingThreads,
this.ttlTimeProvider,
this.db,
this.kvStateInformation,
this.keyGroupPrefixBytes,
this.cancelStreamRegistry,
this.keyGroupCompressionDecorator,
this.rocksDBResourceGuard,
this.checkpointSnapshotStrategy,
this.savepointSnapshotStrategy,
writeBatchWrapper,
defaultColumnFamilyHandle,
nativeMetricMonitor);
PriorityQueueSetFactory priorityQueueFactory = initPriorityQueueFactory(backend);
backend.setPriorityQueueFactory(priorityQueueFactory);
if (restorePrepareResult != null) {
postBackendInitialization(backend, restorePrepareResult);
}
return backend;
} catch (Throwable e) {
if (backend != null) {
backend.dispose();
} else {
IOUtils.closeQuietly(defaultColumnFamilyHandle);
IOUtils.closeQuietly(writeBatchWrapper);
IOUtils.closeQuietly(db);
IOUtils.closeQuietly(cancelStreamRegistry);
}
if (e instanceof IOException) {
throw e;
} else {
String errMsg = "Caught unexpected exception.";
LOG.error(errMsg, e);
throw new IOException(errMsg, e);
}
}
}

Line under review: if (restoreStateHandles != null && !restoreStateHandles.isEmpty()) {

Code after addressing the review:
public RocksDBKeyedStateBackend<K> build() throws BackendBuildingException {
RocksDBWriteBatchWrapper writeBatchWrapper = null;
ColumnFamilyHandle defaultColumnFamilyHandle = null;
RocksDBNativeMetricMonitor nativeMetricMonitor = null;
CloseableRegistry cancelStreamRegistry = new CloseableRegistry();
WriteOptions writeOptions = new WriteOptions().setDisableWAL(true);
LinkedHashMap<String, RocksDBKeyedStateBackend.RocksDbKvStateInfo> kvStateInformation = new LinkedHashMap<>();
RocksDB db = null;
AbstractRocksDBRestoreOperation restoreOperation = null;
RocksDbTtlCompactFiltersManager ttlCompactFiltersManager =
new RocksDbTtlCompactFiltersManager(enableTtlCompactionFilter);
ResourceGuard rocksDBResourceGuard = new ResourceGuard();
SnapshotStrategy<K> snapshotStrategy;
PriorityQueueSetFactory priorityQueueFactory;
RocksDBSerializedCompositeKeyBuilder<K> sharedRocksKeyBuilder;
int keyGroupPrefixBytes = RocksDBKeySerializationUtils.computeRequiredBytesInKeyGroupPrefix(numberOfKeyGroups);
try {
UUID backendUID = UUID.randomUUID();
SortedMap<Long, Set<StateHandleID>> materializedSstFiles = new TreeMap<>();
long lastCompletedCheckpointId = -1L;
if (injectedTestDB != null) {
db = injectedTestDB;
defaultColumnFamilyHandle = injectedDefaultColumnFamilyHandle;
nativeMetricMonitor = nativeMetricOptions.isEnabled() ?
new RocksDBNativeMetricMonitor(nativeMetricOptions, metricGroup, db) : null;
} else {
prepareDirectories();
restoreOperation = getRocksDBRestoreOperation(
keyGroupPrefixBytes, cancelStreamRegistry, kvStateInformation, ttlCompactFiltersManager);
RocksDBRestoreResult restoreResult = restoreOperation.restore();
db = restoreResult.getDb();
defaultColumnFamilyHandle = restoreResult.getDefaultColumnFamilyHandle();
nativeMetricMonitor = restoreResult.getNativeMetricMonitor();
if (restoreOperation instanceof RocksDBIncrementalRestoreOperation) {
backendUID = restoreResult.getBackendUID();
materializedSstFiles = restoreResult.getRestoredSstFiles();
lastCompletedCheckpointId = restoreResult.getLastCompletedCheckpointId();
}
}
writeBatchWrapper = new RocksDBWriteBatchWrapper(db, writeOptions);
sharedRocksKeyBuilder = new RocksDBSerializedCompositeKeyBuilder<>(
keySerializerProvider.currentSchemaSerializer(),
keyGroupPrefixBytes,
32);
snapshotStrategy = initializeSavepointAndCheckpointStrategies(cancelStreamRegistry, rocksDBResourceGuard,
kvStateInformation, keyGroupPrefixBytes, db, backendUID, materializedSstFiles, lastCompletedCheckpointId);
priorityQueueFactory = initPriorityQueueFactory(keyGroupPrefixBytes, kvStateInformation, db,
writeBatchWrapper, nativeMetricMonitor);
} catch (Throwable e) {
List<ColumnFamilyOptions> columnFamilyOptions = new ArrayList<>(kvStateInformation.values().size());
IOUtils.closeQuietly(cancelStreamRegistry);
IOUtils.closeQuietly(writeBatchWrapper);
RocksDBOperationUtils.addColumnFamilyOptionsToCloseLater(columnFamilyOptions, defaultColumnFamilyHandle);
IOUtils.closeQuietly(defaultColumnFamilyHandle);
IOUtils.closeQuietly(nativeMetricMonitor);
for (RocksDBKeyedStateBackend.RocksDbKvStateInfo kvStateInfo : kvStateInformation.values()) {
RocksDBOperationUtils.addColumnFamilyOptionsToCloseLater(columnFamilyOptions, kvStateInfo.columnFamilyHandle);
IOUtils.closeQuietly(kvStateInfo.columnFamilyHandle);
}
IOUtils.closeQuietly(db);
IOUtils.closeQuietly(restoreOperation);
IOUtils.closeAllQuietly(columnFamilyOptions);
IOUtils.closeQuietly(dbOptions);
IOUtils.closeQuietly(writeOptions);
ttlCompactFiltersManager.disposeAndClearRegisteredCompactionFactories();
kvStateInformation.clear();
try {
FileUtils.deleteDirectory(instanceBasePath);
} catch (Exception ex) {
LOG.warn("Failed to instance base path for RocksDB: " + instanceBasePath, ex);
}
if (e instanceof BackendBuildingException) {
throw (BackendBuildingException) e;
} else {
String errMsg = "Caught unexpected exception.";
LOG.error(errMsg, e);
throw new BackendBuildingException(errMsg, e);
}
}
return new RocksDBKeyedStateBackend<>(
this.operatorIdentifier,
this.userCodeClassLoader,
this.instanceBasePath,
this.dbOptions,
columnFamilyOptionsFactory,
this.kvStateRegistry,
this.keySerializerProvider,
this.numberOfKeyGroups,
this.keyGroupRange,
this.executionConfig,
this.numberOfTransferingThreads,
this.ttlTimeProvider,
db,
kvStateInformation,
keyGroupPrefixBytes,
cancelStreamRegistry,
this.keyGroupCompressionDecorator,
rocksDBResourceGuard,
snapshotStrategy.checkpointSnapshotStrategy,
snapshotStrategy.savepointSnapshotStrategy,
writeBatchWrapper,
defaultColumnFamilyHandle,
nativeMetricMonitor,
sharedRocksKeyBuilder,
priorityQueueFactory,
ttlCompactFiltersManager
);
}