You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by ha...@apache.org on 2024/04/26 01:48:57 UTC
(flink) 07/07: [FLINK-35047][state] Support FlinkEnv to init ForSt
This is an automated email from the ASF dual-hosted git repository.
hangxiang pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git
commit 19577b0da7f42fe569084f0207bce93a9be08f57
Author: Hangxiang Yu <ma...@gmail.com>
AuthorDate: Fri Apr 19 01:33:21 2024 +0800
[FLINK-35047][state] Support FlinkEnv to init ForSt
---
.../flink/state/forst/ForStKeyedStateBackend.java | 47 +++++------
.../state/forst/ForStKeyedStateBackendBuilder.java | 51 +++---------
.../org/apache/flink/state/forst/ForStOptions.java | 14 +++-
.../flink/state/forst/ForStResourceContainer.java | 91 ++++++++++++++++++++--
.../flink/state/forst/ForStStateBackend.java | 53 +++++++++----
.../state/forst/ForStResourceContainerTest.java | 23 ++++++
.../state/forst/ForStStateBackendConfigTest.java | 68 +++++++++++-----
7 files changed, 242 insertions(+), 105 deletions(-)
diff --git a/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStKeyedStateBackend.java b/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStKeyedStateBackend.java
index 8c7dfd0ba0f..5737ba7ceac 100644
--- a/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStKeyedStateBackend.java
+++ b/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStKeyedStateBackend.java
@@ -19,7 +19,7 @@ package org.apache.flink.state.forst;
import org.apache.flink.annotation.VisibleForTesting;
import org.apache.flink.api.common.typeutils.TypeSerializer;
-import org.apache.flink.util.FileUtils;
+import org.apache.flink.util.Disposable;
import org.apache.flink.util.IOUtils;
import org.apache.flink.util.Preconditions;
@@ -29,14 +29,14 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.File;
-import java.io.IOException;
+import java.net.URI;
/**
* A KeyedStateBackend that stores its state in {@code ForSt}. This state backend can store very
* large state that exceeds memory even disk to remote storage. TODO: Support to implement the new
* interface of KeyedStateBackend
*/
-public class ForStKeyedStateBackend<K> {
+public class ForStKeyedStateBackend<K> implements Disposable {
private static final Logger LOG = LoggerFactory.getLogger(ForStKeyedStateBackend.class);
@@ -46,9 +46,6 @@ public class ForStKeyedStateBackend<K> {
/** The container of ForSt options. */
private final ForStResourceContainer optionsContainer;
- /** Path where this configured instance stores its local data directory. */
- private final File localBasePath;
-
/**
* We are not using the default column family for Flink state ops, but we still need to remember
* this handle so that we can close it properly when the backend is closed. Note that the one
@@ -70,14 +67,11 @@ public class ForStKeyedStateBackend<K> {
private boolean disposed = false;
public ForStKeyedStateBackend(
- File localBasePath,
ForStResourceContainer optionsContainer,
TypeSerializer<K> keySerializer,
RocksDB db,
ColumnFamilyHandle defaultColumnFamilyHandle,
ForStNativeMetricMonitor nativeMetricMonitor) {
-
- this.localBasePath = Preconditions.checkNotNull(localBasePath);
this.optionsContainer = Preconditions.checkNotNull(optionsContainer);
this.keySerializer = keySerializer;
this.db = db;
@@ -86,6 +80,7 @@ public class ForStKeyedStateBackend<K> {
}
/** Should only be called by one thread, and only after all accesses to the DB happened. */
+ @Override
public void dispose() {
if (this.disposed) {
return;
@@ -108,27 +103,33 @@ public class ForStKeyedStateBackend<K> {
// ... and finally close the DB instance ...
IOUtils.closeQuietly(db);
- IOUtils.closeQuietly(optionsContainer);
+ LOG.info(
+ "Closed ForSt State Backend. Cleaning up ForSt local working directory {}, remote working directory {}.",
+ optionsContainer.getLocalBasePath(),
+ optionsContainer.getRemoteBasePath());
+
+ try {
+ optionsContainer.clearDirectories();
+ } catch (Exception ex) {
+ LOG.warn(
+ "Could not delete ForSt local working directory {}, remote working directory {}.",
+ optionsContainer.getLocalBasePath(),
+ optionsContainer.getRemoteBasePath(),
+ ex);
+ }
- cleanLocalBasePath();
+ IOUtils.closeQuietly(optionsContainer);
}
this.disposed = true;
}
- private void cleanLocalBasePath() {
- LOG.info(
- "Closed ForSt State Backend. Cleaning up ForSt local working directory {}.",
- localBasePath);
-
- try {
- FileUtils.deleteDirectory(localBasePath);
- } catch (IOException ex) {
- LOG.warn("Could not delete ForSt local working directory: {}", localBasePath, ex);
- }
+ @VisibleForTesting
+ File getLocalBasePath() {
+ return optionsContainer.getLocalBasePath();
}
@VisibleForTesting
- File getInstanceBasePath() {
- return localBasePath;
+ URI getRemoteBasePath() {
+ return optionsContainer.getRemoteBasePath();
}
}
diff --git a/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStKeyedStateBackendBuilder.java b/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStKeyedStateBackendBuilder.java
index 1ff68f413a4..8cf45ff7c6a 100644
--- a/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStKeyedStateBackendBuilder.java
+++ b/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStKeyedStateBackendBuilder.java
@@ -28,7 +28,6 @@ import org.apache.flink.state.forst.restore.ForStNoneRestoreOperation;
import org.apache.flink.state.forst.restore.ForStRestoreOperation;
import org.apache.flink.state.forst.restore.ForStRestoreResult;
import org.apache.flink.util.CollectionUtil;
-import org.apache.flink.util.FileUtils;
import org.apache.flink.util.IOUtils;
import org.apache.flink.util.Preconditions;
@@ -41,8 +40,6 @@ import org.slf4j.LoggerFactory;
import javax.annotation.Nonnull;
-import java.io.File;
-import java.io.IOException;
import java.util.Collection;
import java.util.function.Function;
@@ -66,12 +63,6 @@ public class ForStKeyedStateBackendBuilder<K>
/** The container of ForSt option factory and predefined options. */
private final ForStResourceContainer optionsContainer;
- /** Path where this configured instance stores its data directory. */
- private final File localBasePath;
-
- /** Path where this configured instance stores its ForSt database. */
- private final File localForStPath;
-
private final MetricGroup metricGroup;
/** ForSt property-based and statistics-based native metrics options. */
@@ -84,8 +75,6 @@ public class ForStKeyedStateBackendBuilder<K>
MetricGroup metricGroup,
@Nonnull Collection<KeyedStateHandle> stateHandles) {
this.optionsContainer = optionsContainer;
- this.localBasePath = optionsContainer.getLocalBasePath();
- this.localForStPath = optionsContainer.getLocalForStPath();
this.columnFamilyOptionsFactory = Preconditions.checkNotNull(columnFamilyOptionsFactory);
this.keySerializerProvider =
StateSerializerProvider.fromNewRegisteredSerializer(keySerializer);
@@ -108,7 +97,7 @@ public class ForStKeyedStateBackendBuilder<K>
ForStRestoreOperation restoreOperation = null;
try {
- prepareLocalDirectories();
+ optionsContainer.prepareDirectories();
restoreOperation = getForStRestoreOperation();
ForStRestoreResult restoreResult = restoreOperation.restore();
db = restoreResult.getDb();
@@ -124,12 +113,16 @@ public class ForStKeyedStateBackendBuilder<K>
IOUtils.closeQuietly(db);
// it's possible that db has been initialized but later restore steps failed
IOUtils.closeQuietly(restoreOperation);
- IOUtils.closeQuietly(optionsContainer);
try {
- FileUtils.deleteDirectory(localBasePath);
+ optionsContainer.clearDirectories();
} catch (Exception ex) {
- logger.warn("Failed to delete local base path for ForSt: " + localBasePath, ex);
+ logger.warn(
+ "Failed to delete ForSt local base path {}, remote base path {}.",
+ optionsContainer.getLocalBasePath(),
+ optionsContainer.getRemoteBasePath(),
+ ex);
}
+ IOUtils.closeQuietly(optionsContainer);
// Log and rethrow
if (e instanceof BackendBuildingException) {
throw (BackendBuildingException) e;
@@ -140,10 +133,10 @@ public class ForStKeyedStateBackendBuilder<K>
}
}
logger.info(
- "Finished building ForSt keyed state-backend at local base path: {}.",
- localBasePath);
+ "Finished building ForSt keyed state-backend at local base path: {}, remote base path: {}.",
+ optionsContainer.getLocalBasePath(),
+ optionsContainer.getRemoteBasePath());
return new ForStKeyedStateBackend<>(
- this.localBasePath,
this.optionsContainer,
this.keySerializerProvider.currentSchemaSerializer(),
db,
@@ -155,7 +148,7 @@ public class ForStKeyedStateBackendBuilder<K>
DBOptions dbOptions = optionsContainer.getDbOptions();
if (CollectionUtil.isEmptyOrAllElementsNull(restoreStateHandles)) {
return new ForStNoneRestoreOperation(
- localForStPath,
+ optionsContainer.getLocalForStPath(),
dbOptions,
columnFamilyOptionsFactory,
nativeMetricOptions,
@@ -164,24 +157,4 @@ public class ForStKeyedStateBackendBuilder<K>
// TODO: Support Restoring
throw new UnsupportedOperationException("Not support restoring yet for ForStStateBackend");
}
-
- private void prepareLocalDirectories() throws IOException {
- checkAndCreateDirectory(localBasePath);
- if (localForStPath.exists()) {
- // Clear the base directory when the backend is created
- // in case something crashed and the backend never reached dispose()
- FileUtils.deleteDirectory(localBasePath);
- }
- }
-
- private static void checkAndCreateDirectory(File directory) throws IOException {
- if (directory.exists()) {
- if (!directory.isDirectory()) {
- throw new IOException("Not a directory: " + directory);
- }
- } else if (!directory.mkdirs()) {
- throw new IOException(
- String.format("Could not create ForSt data directory at %s.", directory));
- }
- }
}
diff --git a/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStOptions.java b/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStOptions.java
index ba8ce6b2c62..c0bd1e123a6 100644
--- a/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStOptions.java
+++ b/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStOptions.java
@@ -30,7 +30,7 @@ import org.apache.flink.configuration.description.TextElement;
@Experimental
public class ForStOptions {
- /** The local directory (on the TaskManager) where ForSt puts its some files. */
+ /** The local directory (on the TaskManager) where ForSt puts some meta files. */
public static final ConfigOption<String> LOCAL_DIRECTORIES =
ConfigOptions.key("state.backend.forst.localdir")
.stringType()
@@ -38,13 +38,23 @@ public class ForStOptions {
.withDescription(
Description.builder()
.text(
- "The local directory (on the TaskManager) where ForSt puts its some files. Per default, it will be <WORKING_DIR>/tmp. See %s for more details.",
+ "The local directory (on the TaskManager) where ForSt puts some meta files. Per default, it will be <WORKING_DIR>/tmp. See %s for more details.",
TextElement.code(
ClusterOptions
.TASK_MANAGER_PROCESS_WORKING_DIR_BASE
.key()))
.build());
+ /** The remote directory where ForSt puts its SST files. */
+ public static final ConfigOption<String> REMOTE_DIRECTORY =
+ ConfigOptions.key("state.backend.forst.remotedir")
+ .stringType()
+ .noDefaultValue()
+ .withDescription(
+ String.format(
+ "The remote directory where ForSt puts its SST files, fallback to %s if not configured.",
+ LOCAL_DIRECTORIES.key()));
+
/** The options factory class for ForSt to create DBOptions and ColumnFamilyOptions. */
public static final ConfigOption<String> OPTIONS_FACTORY =
ConfigOptions.key("state.backend.forst.options-factory")
diff --git a/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStResourceContainer.java b/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStResourceContainer.java
index 2f54522d1c7..2b912ca9732 100644
--- a/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStResourceContainer.java
+++ b/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStResourceContainer.java
@@ -22,6 +22,7 @@ import org.apache.flink.annotation.VisibleForTesting;
import org.apache.flink.configuration.ConfigOption;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.configuration.ReadableConfig;
+import org.apache.flink.core.fs.FileSystem;
import org.apache.flink.runtime.memory.OpaqueMemoryResource;
import org.apache.flink.util.FileUtils;
import org.apache.flink.util.IOUtils;
@@ -33,6 +34,7 @@ import org.rocksdb.Cache;
import org.rocksdb.ColumnFamilyOptions;
import org.rocksdb.DBOptions;
import org.rocksdb.Filter;
+import org.rocksdb.FlinkEnv;
import org.rocksdb.IndexType;
import org.rocksdb.PlainTableConfig;
import org.rocksdb.ReadOptions;
@@ -46,6 +48,7 @@ import javax.annotation.Nullable;
import java.io.File;
import java.io.IOException;
+import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
@@ -66,7 +69,11 @@ public final class ForStResourceContainer implements AutoCloseable {
// the filename length limit is 255 on most operating systems
private static final int INSTANCE_PATH_LENGTH_LIMIT = 255 - FORST_RELOCATE_LOG_SUFFIX.length();
- private static final String LOCAL_DB_DIR_STRING = "db";
+ private static final String DB_DIR_STRING = "db";
+
+ @Nullable private final URI remoteBasePath;
+
+ @Nullable private final URI remoteForStPath;
@Nullable private final File localBasePath;
@@ -94,19 +101,19 @@ public final class ForStResourceContainer implements AutoCloseable {
@VisibleForTesting
public ForStResourceContainer() {
- this(new Configuration(), null, null, null, false);
+ this(new Configuration(), null, null, null, null, false);
}
@VisibleForTesting
public ForStResourceContainer(@Nullable ForStOptionsFactory optionsFactory) {
- this(new Configuration(), optionsFactory, null, null, false);
+ this(new Configuration(), optionsFactory, null, null, null, false);
}
@VisibleForTesting
public ForStResourceContainer(
@Nullable ForStOptionsFactory optionsFactory,
@Nullable OpaqueMemoryResource<ForStSharedResources> sharedResources) {
- this(new Configuration(), optionsFactory, sharedResources, null, false);
+ this(new Configuration(), optionsFactory, sharedResources, null, null, false);
}
public ForStResourceContainer(
@@ -114,6 +121,7 @@ public final class ForStResourceContainer implements AutoCloseable {
@Nullable ForStOptionsFactory optionsFactory,
@Nullable OpaqueMemoryResource<ForStSharedResources> sharedResources,
@Nullable File localBasePath,
+ @Nullable URI remoteBasePath,
boolean enableStatistics) {
this.configuration = configuration;
@@ -121,8 +129,10 @@ public final class ForStResourceContainer implements AutoCloseable {
this.sharedResources = sharedResources;
this.localBasePath = localBasePath;
- this.localForStPath =
- localBasePath != null ? new File(localBasePath, LOCAL_DB_DIR_STRING) : null;
+ this.localForStPath = localBasePath != null ? new File(localBasePath, DB_DIR_STRING) : null;
+ this.remoteBasePath = remoteBasePath;
+ this.remoteForStPath =
+ remoteBasePath != null ? remoteBasePath.resolve(DB_DIR_STRING) : null;
this.enableStatistics = enableStatistics;
this.handlesToClose = new ArrayList<>();
@@ -156,6 +166,13 @@ public final class ForStResourceContainer implements AutoCloseable {
handlesToClose.add(statistics);
}
+ // TODO: Fallback to checkpoint directory when checkpoint feature is ready if not
+ // configured,
+ // fallback to local directory currently temporarily.
+ if (remoteForStPath != null) {
+ opt.setEnv(new FlinkEnv(remoteForStPath.toString()));
+ }
+
return opt;
}
@@ -241,6 +258,68 @@ public final class ForStResourceContainer implements AutoCloseable {
return localForStPath;
}
+ @Nullable
+ public URI getRemoteBasePath() {
+ return remoteBasePath;
+ }
+
+ /**
+ * Prepare local and remote directories.
+ *
+ * @throws Exception if any unexpected behaviors.
+ */
+ public void prepareDirectories() throws Exception {
+ if (remoteBasePath != null && remoteForStPath != null) {
+ prepareDirectories(remoteBasePath, remoteForStPath);
+ }
+ if (localBasePath != null && localForStPath != null) {
+ prepareDirectories(new URI(localBasePath.getPath()), new URI(localForStPath.getPath()));
+ }
+ }
+
+ private static void prepareDirectories(URI basePath, URI dbPath) throws IOException {
+ FileSystem fileSystem = FileSystem.get(basePath);
+ org.apache.flink.core.fs.Path tempBasePath = new org.apache.flink.core.fs.Path(basePath),
+ tempDBPath = new org.apache.flink.core.fs.Path(dbPath);
+ if (fileSystem.exists(tempBasePath)) {
+ if (!fileSystem.getFileStatus(tempBasePath).isDir()) {
+ throw new IOException("Not a directory: " + tempBasePath);
+ }
+ } else if (!fileSystem.mkdirs(tempBasePath)) {
+ throw new IOException(
+ String.format("Could not create ForSt directory at %s.", tempBasePath));
+ }
+ if (fileSystem.exists(tempDBPath)) {
+ fileSystem.delete(tempDBPath, true);
+ }
+ if (!fileSystem.mkdirs(tempDBPath)) {
+ throw new IOException(
+ String.format("Could not create ForSt db directory at %s.", tempDBPath));
+ }
+ }
+
+ /**
+ * Clear local and remote directories.
+ *
+ * @throws Exception if any unexpected behaviors.
+ */
+ public void clearDirectories() throws Exception {
+ if (remoteBasePath != null) {
+ clearDirectories(remoteBasePath);
+ }
+ if (localBasePath != null) {
+ clearDirectories(new URI(localBasePath.getPath()));
+ }
+ }
+
+ private static void clearDirectories(URI basePath) throws IOException {
+ FileSystem fileSystem = FileSystem.get(basePath);
+ org.apache.flink.core.fs.Path tempBasePath = new org.apache.flink.core.fs.Path(basePath);
+ if (fileSystem.exists(tempBasePath)) {
+ fileSystem.delete(tempBasePath, true);
+ }
+ }
+
ForStNativeMetricOptions getMemoryWatcherOptions(
ForStNativeMetricOptions defaultMetricOptions) {
return optionsFactory == null
diff --git a/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStStateBackend.java b/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStStateBackend.java
index be21b54db18..1ab6961350b 100644
--- a/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStStateBackend.java
+++ b/flink-state-backends/flink-statebackend-forst/src/main/java/org/apache/flink/state/forst/ForStStateBackend.java
@@ -50,6 +50,7 @@ import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
import java.net.URI;
+import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
@@ -84,6 +85,13 @@ public class ForStStateBackend extends AbstractManagedMemoryStateBackend
// -- configuration values, set in the application / configuration
+ /**
+ * Base paths for ForSt remote directory, as configured. Null if not yet set, in which case the
+ * configuration values will be used. The configuration will fallback to local directory by
+ * default. TODO: fallback to checkpoint directory if not configured.
+ */
+ @Nullable private URI remoteForStDirectory;
+
/**
* Base paths for ForSt directory, as configured. Null if not yet set, in which case the
* configuration values will be used. The configuration defaults to the TaskManager's temp
@@ -142,6 +150,21 @@ public class ForStStateBackend extends AbstractManagedMemoryStateBackend
original.memoryConfiguration, config);
this.memoryConfiguration.validate();
+ if (original.remoteForStDirectory != null) {
+ this.remoteForStDirectory = original.remoteForStDirectory;
+ } else {
+ String remoteDirStr = config.get(ForStOptions.REMOTE_DIRECTORY);
+ try {
+ this.remoteForStDirectory = remoteDirStr == null ? null : new URI(remoteDirStr);
+ } catch (URISyntaxException e) {
+ throw new RuntimeException(
+ String.format(
+ "Exception when transform %s to URI, the value is: %s",
+ ForStOptions.REMOTE_DIRECTORY.key(), remoteDirStr),
+ e);
+ }
+ }
+
// configure local directories
if (original.localForStDirectories != null) {
this.localForStDirectories = original.localForStDirectories;
@@ -279,15 +302,12 @@ public class ForStStateBackend extends AbstractManagedMemoryStateBackend
lazyInitializeForJob(env, fileCompatibleIdentifier);
- File instanceBasePath =
- new File(
- getNextStoragePath(),
- "job_"
- + jobId
- + "_op_"
- + fileCompatibleIdentifier
- + "_uuid_"
- + UUID.randomUUID());
+ String childPath =
+ "job_" + jobId + "_op_" + fileCompatibleIdentifier + "_uuid_" + UUID.randomUUID();
+
+ File localBasePath = new File(getNextStoragePath(), childPath);
+ URI remoteBasePath =
+ remoteForStDirectory != null ? remoteForStDirectory.resolve(childPath) : null;
final OpaqueMemoryResource<ForStSharedResources> sharedResources =
ForStOperationUtils.allocateSharedCachesIfConfigured(
@@ -302,7 +322,8 @@ public class ForStStateBackend extends AbstractManagedMemoryStateBackend
final ForStResourceContainer resourceContainer =
createOptionsAndResourceContainer(
sharedResources,
- instanceBasePath,
+ localBasePath,
+ remoteBasePath,
nativeMetricOptions.isStatisticsEnabled());
ForStKeyedStateBackendBuilder<K> builder =
@@ -534,21 +555,23 @@ public class ForStStateBackend extends AbstractManagedMemoryStateBackend
}
@VisibleForTesting
- ForStResourceContainer createOptionsAndResourceContainer(@Nullable File instanceBasePath) {
- return createOptionsAndResourceContainer(null, instanceBasePath, false);
+ ForStResourceContainer createOptionsAndResourceContainer(@Nullable File localBasePath) {
+ return createOptionsAndResourceContainer(null, localBasePath, null, false);
}
@VisibleForTesting
private ForStResourceContainer createOptionsAndResourceContainer(
@Nullable OpaqueMemoryResource<ForStSharedResources> sharedResources,
- @Nullable File instanceBasePath,
+ @Nullable File localBasePath,
+ @Nullable URI remoteBasePath,
boolean enableStatistics) {
return new ForStResourceContainer(
configurableOptions != null ? configurableOptions : new Configuration(),
forStOptionsFactory,
sharedResources,
- instanceBasePath,
+ localBasePath,
+ remoteBasePath,
enableStatistics);
}
@@ -557,6 +580,8 @@ public class ForStStateBackend extends AbstractManagedMemoryStateBackend
return "ForStStateBackend{"
+ ", localForStDirectories="
+ Arrays.toString(localForStDirectories)
+ + ", remoteForStDirectory="
+ + remoteForStDirectory
+ '}';
}
diff --git a/flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStResourceContainerTest.java b/flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStResourceContainerTest.java
index 53c0524308b..bec699931c6 100644
--- a/flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStResourceContainerTest.java
+++ b/flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStResourceContainerTest.java
@@ -18,6 +18,7 @@
package org.apache.flink.state.forst;
+import org.apache.flink.configuration.Configuration;
import org.apache.flink.runtime.memory.OpaqueMemoryResource;
import org.apache.flink.util.function.ThrowingRunnable;
@@ -30,6 +31,7 @@ import org.rocksdb.BloomFilter;
import org.rocksdb.Cache;
import org.rocksdb.ColumnFamilyOptions;
import org.rocksdb.DBOptions;
+import org.rocksdb.FlinkEnv;
import org.rocksdb.IndexType;
import org.rocksdb.LRUCache;
import org.rocksdb.NativeLibraryLoader;
@@ -38,8 +40,10 @@ import org.rocksdb.TableFormatConfig;
import org.rocksdb.WriteBufferManager;
import org.rocksdb.WriteOptions;
+import java.io.File;
import java.io.IOException;
import java.lang.reflect.Field;
+import java.net.URI;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
@@ -48,6 +52,7 @@ import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;
/** Tests to guard {@link ForStResourceContainer}. */
@@ -294,4 +299,22 @@ public class ForStResourceContainerTest {
}
assertFalse("Block based filter is left unclosed.", blockBasedFilter.isOwningHandle());
}
+
+ @Test
+ public void testDirectoryResources() throws Exception {
+ File localBasePath = TMP_FOLDER.newFolder();
+ URI remoteBasePath = TMP_FOLDER.newFolder().toURI();
+ try (final ForStResourceContainer optionsContainer =
+ new ForStResourceContainer(
+ new Configuration(), null, null, localBasePath, remoteBasePath, false)) {
+ optionsContainer.prepareDirectories();
+ assertTrue(localBasePath.exists());
+ assertTrue(new File(remoteBasePath).exists());
+ assertTrue(optionsContainer.getDbOptions().getEnv() instanceof FlinkEnv);
+
+ optionsContainer.clearDirectories();
+ assertFalse(localBasePath.exists());
+ assertFalse(new File(remoteBasePath).exists());
+ }
+ }
}
diff --git a/flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStStateBackendConfigTest.java b/flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStStateBackendConfigTest.java
index 11733f0ceca..89bd9a527cd 100644
--- a/flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStStateBackendConfigTest.java
+++ b/flink-state-backends/flink-statebackend-forst/src/test/java/org/apache/flink/state/forst/ForStStateBackendConfigTest.java
@@ -144,7 +144,7 @@ public class ForStStateBackendConfigTest {
createKeyedStateBackend(forStStateBackend, env, IntSerializer.INSTANCE);
try {
- File instanceBasePath = keyedBackend.getInstanceBasePath();
+ File instanceBasePath = keyedBackend.getLocalBasePath();
assertThat(
instanceBasePath.getAbsolutePath(),
anyOf(startsWith(testDir1), startsWith(testDir2)));
@@ -207,7 +207,7 @@ public class ForStStateBackendConfigTest {
createKeyedStateBackend(forStBackend, env, IntSerializer.INSTANCE);
try {
- File instanceBasePath = keyedBackend.getInstanceBasePath();
+ File instanceBasePath = keyedBackend.getLocalBasePath();
assertThat(
instanceBasePath.getAbsolutePath(), startsWith(expectedPath.getAbsolutePath()));
@@ -253,26 +253,26 @@ public class ForStStateBackendConfigTest {
conf.set(ForStConfigurableOptions.LOG_LEVEL, InfoLogLevel.DEBUG_LEVEL);
conf.set(ForStConfigurableOptions.LOG_FILE_NUM, 4);
conf.set(ForStConfigurableOptions.LOG_MAX_FILE_SIZE, MemorySize.parse("1kb"));
- final ForStStateBackend rocksDbBackend =
+ final ForStStateBackend forStBackend =
new ForStStateBackend().configure(conf, getClass().getClassLoader());
final String dbStoragePath = new Path(folder.toURI().toString()).toString();
- rocksDbBackend.setLocalDbStoragePath(dbStoragePath);
+ forStBackend.setLocalDbStoragePath(dbStoragePath);
final MockEnvironment env = getMockEnvironment(tempFolder.newFolder());
ForStKeyedStateBackend<Integer> keyedBackend =
- createKeyedStateBackend(rocksDbBackend, env, IntSerializer.INSTANCE);
+ createKeyedStateBackend(forStBackend, env, IntSerializer.INSTANCE);
- File instanceBasePath = keyedBackend.getInstanceBasePath();
- File instanceRocksDBPath = new File(instanceBasePath, "db");
+ File localBasePath = keyedBackend.getLocalBasePath();
+ File localForStPath = new File(localBasePath, "db");
// avoid tests without relocate.
- Assume.assumeTrue(instanceRocksDBPath.getAbsolutePath().length() <= 255 - "_LOG".length());
+ Assume.assumeTrue(localForStPath.getAbsolutePath().length() <= 255 - "_LOG".length());
java.nio.file.Path[] relocatedDbLogs;
try {
relocatedDbLogs = FileUtils.listDirectory(relocatedDBLogDir.toPath());
while (relocatedDbLogs.length <= 2) {
- // If the default number of log files in rocksdb is not enough, add more logs.
+ // If the default number of log files in ForSt is not enough, add more logs.
try (FlushOptions flushOptions = new FlushOptions()) {
keyedBackend.db.put(RandomUtils.nextBytes(32), RandomUtils.nextBytes(512));
keyedBackend.db.flush(flushOptions);
@@ -290,11 +290,11 @@ public class ForStStateBackendConfigTest {
}
// ------------------------------------------------------------------------
- // RocksDB local file automatic from temp directories
+ // ForSt local file automatic from temp directories
// ------------------------------------------------------------------------
/**
- * This tests whether the RocksDB backends uses the temp directories that are provided from the
+ * This tests whether the ForSt backends uses the temp directories that are provided from the
* {@link Environment} when no db storage path is set.
*/
@Test
@@ -326,7 +326,7 @@ public class ForStStateBackendConfigTest {
cancelStreamRegistry));
try {
- File instanceBasePath = keyedBackend.getInstanceBasePath();
+ File instanceBasePath = keyedBackend.getLocalBasePath();
assertThat(instanceBasePath.getAbsolutePath(), startsWith(dir1.getAbsolutePath()));
} finally {
keyedBackend.dispose();
@@ -335,7 +335,7 @@ public class ForStStateBackendConfigTest {
}
// ------------------------------------------------------------------------
- // RocksDB local file directory initialization
+ // ForSt local file directory initialization
// ------------------------------------------------------------------------
@Test
@@ -429,7 +429,7 @@ public class ForStStateBackendConfigTest {
}
// ------------------------------------------------------------------------
- // RocksDB Options
+ // ForSt Options
// ------------------------------------------------------------------------
@Test
public void testConfigurableOptionsFromConfig() throws Exception {
@@ -465,7 +465,7 @@ public class ForStStateBackendConfigTest {
// verify legal configuration
{
configuration.setString(ForStConfigurableOptions.LOG_LEVEL.key(), "DEBUG_LEVEL");
- configuration.setString(ForStConfigurableOptions.LOG_DIR.key(), "/tmp/rocksdb-logs/");
+ configuration.setString(ForStConfigurableOptions.LOG_DIR.key(), "/tmp/ForSt-logs/");
configuration.setString(ForStConfigurableOptions.LOG_FILE_NUM.key(), "10");
configuration.setString(ForStConfigurableOptions.LOG_MAX_FILE_SIZE.key(), "2MB");
configuration.setString(ForStConfigurableOptions.COMPACTION_STYLE.key(), "level");
@@ -486,12 +486,12 @@ public class ForStStateBackendConfigTest {
configuration.setString(ForStConfigurableOptions.USE_BLOOM_FILTER.key(), "TRUE");
try (ForStResourceContainer optionsContainer =
- new ForStResourceContainer(configuration, null, null, null, false)) {
+ new ForStResourceContainer(configuration, null, null, null, null, false)) {
DBOptions dbOptions = optionsContainer.getDbOptions();
assertEquals(-1, dbOptions.maxOpenFiles());
assertEquals(InfoLogLevel.DEBUG_LEVEL, dbOptions.infoLogLevel());
- assertEquals("/tmp/rocksdb-logs/", dbOptions.dbLogDir());
+ assertEquals("/tmp/ForSt-logs/", dbOptions.dbLogDir());
assertEquals(10, dbOptions.keepLogFileNum());
assertEquals(2 * SizeUnit.MB, dbOptions.maxLogFileSize());
@@ -569,7 +569,7 @@ public class ForStStateBackendConfigTest {
Configuration configuration = new Configuration();
configuration.set(ForStConfigurableOptions.COMPACTION_STYLE, CompactionStyle.UNIVERSAL);
try (final ForStResourceContainer optionsContainer =
- new ForStResourceContainer(configuration, null, null, null, false)) {
+ new ForStResourceContainer(configuration, null, null, null, null, false)) {
final ColumnFamilyOptions columnFamilyOptions = optionsContainer.getColumnOptions();
assertNotNull(columnFamilyOptions);
@@ -577,7 +577,7 @@ public class ForStStateBackendConfigTest {
}
try (final ForStResourceContainer optionsContainer =
- new ForStResourceContainer(new Configuration(), null, null, null, false)) {
+ new ForStResourceContainer(new Configuration(), null, null, null, null, false)) {
final ColumnFamilyOptions columnFamilyOptions = optionsContainer.getColumnOptions();
assertNotNull(columnFamilyOptions);
@@ -638,7 +638,7 @@ public class ForStStateBackendConfigTest {
}
// ------------------------------------------------------------------------
- // RocksDB Memory Control
+ // ForSt Memory Control
// ------------------------------------------------------------------------
@Test
@@ -705,6 +705,32 @@ public class ForStStateBackendConfigTest {
}
}
+ @Test
+ public void testRemoteDirectory() throws Exception {
+ FileSystem.initialize(new Configuration(), null);
+ Configuration configuration = new Configuration();
+ configuration.set(ForStOptions.REMOTE_DIRECTORY, tempFolder.newFolder().toURI().toString());
+ ForStStateBackend forStStateBackend =
+ new ForStStateBackend().configure(configuration, null);
+ ForStKeyedStateBackend<Integer> keyedBackend = null;
+ try {
+ keyedBackend =
+ createKeyedStateBackend(
+ forStStateBackend,
+ getMockEnvironment(tempFolder.newFolder()),
+ IntSerializer.INSTANCE);
+ assertTrue(
+ keyedBackend
+ .getRemoteBasePath()
+ .toString()
+ .startsWith(configuration.get(ForStOptions.REMOTE_DIRECTORY)));
+ } finally {
+ if (keyedBackend != null) {
+ keyedBackend.dispose();
+ }
+ }
+ }
+
private void verifySetParameter(Runnable setter) {
try {
setter.run();
@@ -742,7 +768,7 @@ public class ForStStateBackendConfigTest {
/** An implementation of options factory for testing. */
public static class TestOptionsFactory implements ConfigurableForStOptionsFactory {
public static final ConfigOption<Integer> BACKGROUND_JOBS_OPTION =
- ConfigOptions.key("my.custom.rocksdb.backgroundJobs").intType().defaultValue(2);
+ ConfigOptions.key("my.custom.forst.backgroundJobs").intType().defaultValue(2);
private int backgroundJobs = BACKGROUND_JOBS_OPTION.defaultValue();