World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.
Java ai-catalogdata-catalogdatalakefederated-querylakehousemetadatametalakemodel-catalogopendatacatalogskycomputingstratosphere
[#7572] improvement(gvfs): add fileset cache in GVFS (#8254)
mchades522748b436a5
diff --git a/clients/client-python/gravitino/filesystem/gvfs_base_operations.py b/clients/client-python/gravitino/filesystem/gvfs_base_operations.pyindex 30cd5dd048..85b74d6ddd 100644--- a/clients/client-python/gravitino/filesystem/gvfs_base_operations.py+++ b/clients/client-python/gravitino/filesystem/gvfs_base_operations.py@@ -28,6 +28,7 @@ from readerwriterlock import rwlockfrom gravitino.api.catalog import Catalogfrom gravitino.api.credential.credential import Credential+from gravitino.api.file.fileset import Filesetfrom gravitino.audit.caller_context import CallerContextHolder, CallerContextfrom gravitino.audit.fileset_audit_constants import FilesetAuditConstantsfrom gravitino.audit.fileset_data_operation import FilesetDataOperation@@ -78,6 +79,7 @@ class BaseGVFSOperations(ABC):ENV_CURRENT_LOCATION_NAME_ENV_VAR_DEFAULT = "CURRENT_LOCATION_NAME"ENABLE_CREDENTIAL_VENDING_DEFAULT = False+ ENABLE_FILESET_METADATA_CACHE_DEFAULT = Falsedef __init__(self,@@ -135,8 +137,20 @@ class BaseGVFSOperations(ABC):self._filesystem_cache = TTLCache(maxsize=cache_size, ttl=cache_expired_time)self._cache_lock = rwlock.RWLockFair()- self._catalog_cache = LRUCache(maxsize=100)- self._catalog_cache_lock = rwlock.RWLockFair()+ self._enable_fileset_metadata_cache = (+ self.ENABLE_FILESET_METADATA_CACHE_DEFAULT+ if options is None+ else options.get(+ GVFSConfig.GVFS_FILESYSTEM_ENABLE_FILESET_METADATA_CACHE,+ self.ENABLE_FILESET_METADATA_CACHE_DEFAULT+ )+ )+ if self._enable_fileset_metadata_cache:+ self._catalog_cache = LRUCache(maxsize=100)+ self._catalog_cache_lock = rwlock.RWLockFair()++ self._fileset_cache = LRUCache(maxsize=10000)+ self._fileset_cache_lock = rwlock.RWLockFair()self._enable_credential_vending = (False@@ -389,9 +403,7 @@ class BaseGVFSOperations(ABC):self._metalake, fileset_ident.namespace().level(1))catalog = self._get_fileset_catalog(catalog_ident)- fileset = catalog.as_fileset_catalog().load_fileset(- NameIdentifier.of(fileset_ident.namespace().level(2), fileset_ident.name())- )+ fileset = self._get_fileset(fileset_ident)target_location_name = (location_nameor fileset.properties().get(fileset.PROPERTY_DEFAULT_LOCATION_NAME)@@ -527,6 +539,9 @@ class BaseGVFSOperations(ABC):CallerContextHolder.remove()def _get_fileset_catalog(self, catalog_ident: NameIdentifier):+ if not self._enable_fileset_metadata_cache:+ return self._client.load_catalog(catalog_ident.name())+read_lock = self._catalog_cache_lock.gen_rlock()try:read_lock.acquire()@@ -547,3 +562,41 @@ class BaseGVFSOperations(ABC):return catalogfinally:write_lock.release()++ def _get_fileset(self, fileset_ident: NameIdentifier):+ if not self._enable_fileset_metadata_cache:+ catalog_ident: NameIdentifier = NameIdentifier.of(+ fileset_ident.namespace().level(0), fileset_ident.namespace().level(1)+ )+ catalog: FilesetCatalog = self._get_fileset_catalog(catalog_ident)+ return catalog.as_fileset_catalog().load_fileset(+ NameIdentifier.of(fileset_ident.namespace().level(2), fileset_ident.name())+ )++ read_lock = self._fileset_cache_lock.gen_rlock()+ try:+ read_lock.acquire()+ cache_value: Fileset = self._fileset_cache.get(fileset_ident)+ if cache_value is not None:+ return cache_value+ finally:+ read_lock.release()++ write_lock = self._fileset_cache_lock.gen_wlock()+ try:+ write_lock.acquire()+ cache_value: Fileset = self._fileset_cache.get(fileset_ident)+ if cache_value is not None:+ return cache_value++ catalog_ident: NameIdentifier = NameIdentifier.of(+ fileset_ident.namespace().level(0), fileset_ident.namespace().level(1)+ )+ catalog: FilesetCatalog = self._get_fileset_catalog(catalog_ident)+ fileset = catalog.as_fileset_catalog().load_fileset(+ NameIdentifier.of(fileset_ident.namespace().level(2), fileset_ident.name())+ )+ self._fileset_cache[fileset_ident] = fileset+ return fileset+ finally:+ write_lock.release()diff --git a/clients/client-python/gravitino/filesystem/gvfs_config.py b/clients/client-python/gravitino/filesystem/gvfs_config.pyindex 507528ecd0..6d03d41ebb 100644--- a/clients/client-python/gravitino/filesystem/gvfs_config.py+++ b/clients/client-python/gravitino/filesystem/gvfs_config.py@@ -76,3 +76,8 @@ class GVFSConfig:# The configuration key prefix for the client.GVFS_FILESYSTEM_CLIENT_CONFIG_PREFIX = "gvfs_gravitino_client_"++ # The configuration key for whether to enable fileset catalog cache. The default is false.+ # Note that this cache causes a side effect: if you modify the fileset or fileset catalog metadata,+ # the client can not see the latest changes.+ GVFS_FILESYSTEM_ENABLE_FILESET_METADATA_CACHE = "enable_fileset_metadata_cache"diff --git a/clients/filesystem-hadoop3/src/main/java/org/apache/gravitino/filesystem/hadoop/BaseGVFSOperations.java b/clients/filesystem-hadoop3/src/main/java/org/apache/gravitino/filesystem/hadoop/BaseGVFSOperations.javaindex 7dbbe6f09e..d76b8302c5 100644--- a/clients/filesystem-hadoop3/src/main/java/org/apache/gravitino/filesystem/hadoop/BaseGVFSOperations.java+++ b/clients/filesystem-hadoop3/src/main/java/org/apache/gravitino/filesystem/hadoop/BaseGVFSOperations.java@@ -20,6 +20,8 @@ package org.apache.gravitino.filesystem.hadoop;import static org.apache.gravitino.file.Fileset.PROPERTY_DEFAULT_LOCATION_NAME;import static org.apache.gravitino.filesystem.hadoop.GravitinoVirtualFileSystemConfiguration.FS_GRAVITINO_CURRENT_LOCATION_NAME;+import static org.apache.gravitino.filesystem.hadoop.GravitinoVirtualFileSystemConfiguration.FS_GRAVITINO_FILESET_CATALOG_CACHE_ENABLE;+import static org.apache.gravitino.filesystem.hadoop.GravitinoVirtualFileSystemConfiguration.FS_GRAVITINO_FILESET_CATALOG_CACHE_ENABLE_DEFAULT;import static org.apache.gravitino.filesystem.hadoop.GravitinoVirtualFileSystemUtils.extractIdentifier;import static org.apache.gravitino.filesystem.hadoop.GravitinoVirtualFileSystemUtils.getConfigMap;import static org.apache.gravitino.filesystem.hadoop.GravitinoVirtualFileSystemUtils.getSubPathFromGvfsPath;@@ -111,14 +113,11 @@ public abstract class BaseGVFSOperations implements Closeable {private final String metalakeName;- private final FilesetCatalogCache filesetCatalogCache;+ private final Optional<FilesetMetadataCache> filesetMetadataCache;+ private final GravitinoClient gravitinoClient;private final Configuration conf;- // Since Caffeine does not ensure that removalListener will be involved after expiration- // We use a scheduler with one thread to clean up expired clients.- private final ScheduledThreadPoolExecutor internalFileSystemCleanScheduler;-// Fileset nameIdentifier-locationName Pair and its corresponding FileSystem cache, the name// identifier has four levels, the first level is metalake name.private final Cache<Pair<NameIdentifier, String>, FileSystem> internalFileSystemCache;@@ -146,13 +145,17 @@ public abstract class BaseGVFSOperations implements Closeable {"'%s' is not set in the configuration",GravitinoVirtualFileSystemConfiguration.FS_GRAVITINO_CLIENT_METALAKE_KEY);- GravitinoClient client = GravitinoVirtualFileSystemUtils.createClient(configuration);- this.filesetCatalogCache = new FilesetCatalogCache(client);+ this.gravitinoClient = GravitinoVirtualFileSystemUtils.createClient(configuration);+ boolean enableFilesetCatalogCache =+ configuration.getBoolean(+ FS_GRAVITINO_FILESET_CATALOG_CACHE_ENABLE,+ FS_GRAVITINO_FILESET_CATALOG_CACHE_ENABLE_DEFAULT);+ this.filesetMetadataCache =+ enableFilesetCatalogCache+ ? Optional.of(new FilesetMetadataCache(gravitinoClient))+ : Optional.empty();- this.internalFileSystemCleanScheduler =- new ScheduledThreadPoolExecutor(1, newDaemonThreadFactory("gvfs-filesystem-cache-cleaner"));- this.internalFileSystemCache =- newFileSystemCache(configuration, internalFileSystemCleanScheduler);+ this.internalFileSystemCache = newFileSystemCache(configuration);this.fileSystemProvidersMap = ImmutableMap.copyOf(getFileSystemProviders());@@ -186,11 +189,10 @@ public abstract class BaseGVFSOperations implements Closeable {}}internalFileSystemCache.invalidateAll();- internalFileSystemCleanScheduler.shutdownNow();try {- if (filesetCatalogCache != null) {- filesetCatalogCache.close();+ if (filesetMetadataCache.isPresent()) {+ filesetMetadataCache.get().close();}} catch (IOException e) {// ignore@@ -527,14 +529,35 @@ public abstract class BaseGVFSOperations implements Closeable {}/**- * Get the fileset catalog by the catalog identifier from the cache. If the subclass does not want- * to use the cache, it can override this method.+ * Get the fileset catalog by the catalog identifier from the cache or load it from the server if+ * the cache is disabled.** @param catalogIdent the catalog identifier.* @return the fileset catalog.*/protected FilesetCatalog getFilesetCatalog(NameIdentifier catalogIdent) {- return filesetCatalogCache.getFilesetCatalog(catalogIdent);+ return filesetMetadataCache+ .map(cache -> cache.getFilesetCatalog(catalogIdent))+ .orElseGet(() -> gravitinoClient.loadCatalog(catalogIdent.name()).asFilesetCatalog());+ }++ /**+ * Get the fileset by the fileset identifier from the cache or load it from the server if the+ * cache is disabled.+ *+ * @param filesetIdent the fileset identifier.+ * @return the fileset.+ */+ protected Fileset getFileset(NameIdentifier filesetIdent) {+ return filesetMetadataCache+ .map(cache -> cache.getFileset(filesetIdent))+ .orElseGet(+ () ->+ getFilesetCatalog(+ NameIdentifier.of(+ filesetIdent.namespace().level(0), filesetIdent.namespace().level(1)))+ .loadFileset(+ NameIdentifier.of(filesetIdent.namespace().level(2), filesetIdent.name())));}@VisibleForTesting@@ -662,15 +685,8 @@ public abstract class BaseGVFSOperations implements Closeable {}}- private Fileset getFileset(NameIdentifier filesetIdent) {- NameIdentifier catalogIdent =- NameIdentifier.of(filesetIdent.namespace().level(0), filesetIdent.namespace().level(1));- return getFilesetCatalog(catalogIdent)- .loadFileset(NameIdentifier.of(filesetIdent.namespace().level(2), filesetIdent.name()));- }-private Cache<Pair<NameIdentifier, String>, FileSystem> newFileSystemCache(- Configuration configuration, ScheduledThreadPoolExecutor internalFileSystemCleanScheduler) {+ Configuration configuration) {int maxCapacity =configuration.getInt(GravitinoVirtualFileSystemConfiguration.FS_GRAVITINO_FILESET_CACHE_MAX_CAPACITY_KEY,@@ -696,7 +712,12 @@ public abstract class BaseGVFSOperations implements Closeable {Caffeine<Object, Object> cacheBuilder =Caffeine.newBuilder().maximumSize(maxCapacity)- .scheduler(Scheduler.forScheduledExecutorService(internalFileSystemCleanScheduler))+ // Since Caffeine does not ensure that removalListener will be involved after expiration+ // We use a scheduler with one thread to clean up expired fs.+ .scheduler(+ Scheduler.forScheduledExecutorService(+ new ScheduledThreadPoolExecutor(+ 1, newDaemonThreadFactory("gvfs-filesystem-cache-cleaner")))).removalListener((key, value, cause) -> {FileSystem fs = (FileSystem) value;diff --git a/clients/filesystem-hadoop3/src/main/java/org/apache/gravitino/filesystem/hadoop/FilesetCatalogCache.java b/clients/filesystem-hadoop3/src/main/java/org/apache/gravitino/filesystem/hadoop/FilesetMetadataCache.javasimilarity index 60%rename from clients/filesystem-hadoop3/src/main/java/org/apache/gravitino/filesystem/hadoop/FilesetCatalogCache.javarename to clients/filesystem-hadoop3/src/main/java/org/apache/gravitino/filesystem/hadoop/FilesetMetadataCache.javaindex 33e40ef20e..2972e31142 100644--- a/clients/filesystem-hadoop3/src/main/java/org/apache/gravitino/filesystem/hadoop/FilesetCatalogCache.java+++ b/clients/filesystem-hadoop3/src/main/java/org/apache/gravitino/filesystem/hadoop/FilesetMetadataCache.java@@ -30,28 +30,25 @@ import java.util.concurrent.ScheduledThreadPoolExecutor;import java.util.concurrent.ThreadFactory;import org.apache.gravitino.NameIdentifier;import org.apache.gravitino.client.GravitinoClient;+import org.apache.gravitino.file.Fileset;import org.apache.gravitino.file.FilesetCatalog;/** A cache for fileset catalogs. */-public class FilesetCatalogCache implements Closeable {+public class FilesetMetadataCache implements Closeable {private final GravitinoClient client;private final Cache<NameIdentifier, FilesetCatalog> catalogCache;-- // Since Caffeine does not ensure that removalListener will be involved after expiration- // We use a scheduler with one thread to clean up expired clients.- private final ScheduledThreadPoolExecutor catalogCleanScheduler;+ private final Cache<NameIdentifier, Fileset> filesetCache;/**- * Creates a new instance of {@link FilesetCatalogCache}.+ * Creates a new instance of {@link FilesetMetadataCache}.** @param client the Gravitino client.*/- public FilesetCatalogCache(GravitinoClient client) {+ public FilesetMetadataCache(GravitinoClient client) {this.client = client;- this.catalogCleanScheduler =- new ScheduledThreadPoolExecutor(1, newDaemonThreadFactory("gvfs-catalog-cache-cleaner"));- this.catalogCache = newCatalogCache(catalogCleanScheduler);+ this.catalogCache = newCatalogCache();+ this.filesetCache = newFilesetCache();}/**@@ -70,13 +67,46 @@ public class FilesetCatalogCache implements Closeable {return filesetCatalog;}- private Cache<NameIdentifier, FilesetCatalog> newCatalogCache(- ScheduledThreadPoolExecutor catalogCleanScheduler) {+ /**+ * Gets the fileset by the given fileset identifier.+ *+ * @param filesetIdent the fileset identifier.+ * @return the fileset.+ */+ public Fileset getFileset(NameIdentifier filesetIdent) {+ NameIdentifier catalogIdent =+ NameIdentifier.of(filesetIdent.namespace().level(0), filesetIdent.namespace().level(1));+ FilesetCatalog filesetCatalog = getFilesetCatalog(catalogIdent);+ return filesetCache.get(+ filesetIdent,+ ident ->+ filesetCatalog.loadFileset(+ NameIdentifier.of(filesetIdent.namespace().level(2), filesetIdent.name())));+ }++ private Cache<NameIdentifier, FilesetCatalog> newCatalogCache() {// In most scenarios, it will not read so many catalog filesets at the same time, so we can just// set a default value for this cache.return Caffeine.newBuilder().maximumSize(100)- .scheduler(Scheduler.forScheduledExecutorService(catalogCleanScheduler))+ // Since Caffeine does not ensure that removalListener will be involved after expiration+ // We use a scheduler with one thread to clean up expired catalogs.+ .scheduler(+ Scheduler.forScheduledExecutorService(+ new ScheduledThreadPoolExecutor(+ 1, newDaemonThreadFactory("gvfs-catalog-cache-cleaner"))))+ .build();+ }++ private Cache<NameIdentifier, Fileset> newFilesetCache() {+ return Caffeine.newBuilder()+ .maximumSize(10000)+ // Since Caffeine does not ensure that removalListener will be involved after expiration+ // We use a scheduler with one thread to clean up expired filesets.+ .scheduler(+ Scheduler.forScheduledExecutorService(+ new ScheduledThreadPoolExecutor(+ 1, newDaemonThreadFactory("gvfs-fileset-cache-cleaner")))).build();}@@ -87,6 +117,7 @@ public class FilesetCatalogCache implements Closeable {@Overridepublic void close() throws IOException {catalogCache.invalidateAll();+ filesetCache.invalidateAll();// close the clienttry {if (client != null) {@@ -95,6 +126,5 @@ public class FilesetCatalogCache implements Closeable {} catch (Exception e) {// ignore}- catalogCleanScheduler.shutdownNow();}}diff --git a/clients/filesystem-hadoop3/src/main/java/org/apache/gravitino/filesystem/hadoop/GravitinoVirtualFileSystemConfiguration.java b/clients/filesystem-hadoop3/src/main/java/org/apache/gravitino/filesystem/hadoop/GravitinoVirtualFileSystemConfiguration.javaindex 4ffb8d3731..80d8a90d35 100644--- a/clients/filesystem-hadoop3/src/main/java/org/apache/gravitino/filesystem/hadoop/GravitinoVirtualFileSystemConfiguration.java+++ b/clients/filesystem-hadoop3/src/main/java/org/apache/gravitino/filesystem/hadoop/GravitinoVirtualFileSystemConfiguration.java@@ -47,14 +47,17 @@ public class GravitinoVirtualFileSystemConfiguration {/** The authentication type for simple authentication. */public static final String SIMPLE_AUTH_TYPE = "simple";+/** The authentication type for oauth2 authentication. */public static final String OAUTH2_AUTH_TYPE = "oauth2";/** The authentication type for kerberos authentication. */public static final String KERBEROS_AUTH_TYPE = "kerberos";+// oauth2/** The configuration key prefix for oauth2 */public static final String FS_GRAVITINO_CLIENT_OAUTH2_PREFIX = "fs.gravitino.client.oauth2.";+/** The configuration key for the URI of the default OAuth server. */public static final String FS_GRAVITINO_CLIENT_OAUTH2_SERVER_URI_KEY ="fs.gravitino.client.oauth2.serverUri";@@ -73,6 +76,7 @@ public class GravitinoVirtualFileSystemConfiguration {/** The configuration key prefix for kerberos */public static final String FS_GRAVITINO_CLIENT_KERBEROS_PREFIX = "fs.gravitino.client.kerberos.";+/** The configuration key for the principal. */public static final String FS_GRAVITINO_CLIENT_KERBEROS_PRINCIPAL_KEY ="fs.gravitino.client.kerberos.principal";@@ -160,5 +164,16 @@ public class GravitinoVirtualFileSystemConfiguration {public static final List<String> NOT_GRAVITINO_CLIENT_CONFIG_LIST =ImmutableList.of(FS_GRAVITINO_CLIENT_METALAKE_KEY, FS_GRAVITINO_CLIENT_AUTH_TYPE_KEY);+ /**+ * The configuration key for whether to enable fileset catalog cache. The default is false. Note+ * that this cache causes a side effect: if you modify the fileset or fileset catalog metadata,+ * the client can not see the latest changes.+ */+ public static final String FS_GRAVITINO_FILESET_CATALOG_CACHE_ENABLE =+ "fs.gravitino.filesetCatalog.cache.enable";++ /** The default value for whether to enable fileset catalog cache. */+ public static final boolean FS_GRAVITINO_FILESET_CATALOG_CACHE_ENABLE_DEFAULT = false;+private GravitinoVirtualFileSystemConfiguration() {}}diff --git a/docs/how-to-use-gvfs.md b/docs/how-to-use-gvfs.mdindex 38415e64e7..75168c71fb 100644--- a/docs/how-to-use-gvfs.md+++ b/docs/how-to-use-gvfs.md@@ -64,7 +64,6 @@ the path mapping and convert automatically.| `fs.gravitino.client.kerberos.keytabFilePath` | The auth keytab file path for the Gravitino client when using `kerberos` auth type in the Gravitino Virtual File System. | (none) | No | 0.5.1 || `fs.gravitino.fileset.cache.maxCapacity` | The cache capacity of the Gravitino Virtual File System. | `20` | No | 0.5.0 || `fs.gravitino.fileset.cache.evictionMillsAfterAccess` | The value of time that the cache expires after accessing in the Gravitino Virtual File System. The value is in `milliseconds`. | `3600000` | No | 0.5.0 |-| `fs.gravitino.fileset.cache.evictionMillsAfterAccess` | The value of time that the cache expires after accessing in the Gravitino Virtual File System. The value is in `milliseconds`. | `3600000` | No | 0.5.0 || `fs.gravitino.current.location.name` | The configuration used to select the location of the fileset. If this configuration is not set, the value of environment variable configured by `fs.gravitino.current.location.env.var` will be checked. If neither is set, the value of fileset property `default-location-name` will be used as the location name. | the value of fileset property `default-location-name` | No | 0.9.0-incubating || `fs.gravitino.current.location.name.env.var` | The environment variable name to get the current location name. | `CURRENT_LOCATION_NAME` | No | 0.9.0-incubating || `fs.gravitino.operations.class` | The operations class to provide the FS operations for the Gravitino Virtual File System. Users can extends `BaseGVFSOperations` to implement their own operations and configure the class name in this conf to use custom FS operations. | `org.apache.gravitino.filesystem.hadoop.DefaultGVFSOperations` | No | 0.9.0-incubating |@@ -72,6 +71,7 @@ the path mapping and convert automatically.| `fs.gravitino.client.request.header.` | The configuration key prefix for the Gravitino client request header. You can set the request header for the Gravitino client. | (none) | No | 0.9.0-incubating || `fs.gravitino.enableCredentialVending` | Whether to enable credential vending for the Gravitino Virtual File System. | `false` | No | 0.9.0-incubating || `fs.gravitino.client.` | The configuration key prefix for the Gravitino client config. | (none) | No | 1.0.0 |+| `fs.gravitino.filesetMetadataCache.enable` | Whether to cache the fileset or fileset catalog metadata in the Gravitino Virtual File System. Note that this cache causes a side effect: if you modify the fileset or fileset catalog metadata, the client can not see the latest changes. | `false` | No | 1.0.0 |To configure the Gravitino client, use properties prefixed with `fs.gravitino.client.`. These properties will be passed to the Gravitino client after removing the `fs.` prefix.@@ -395,6 +395,7 @@ to recompile the native libraries like `libhdfs` and others, and completely repl| `client_request_header_` | The configuration key prefix for the Gravitino client request header. You can set the request header for the Gravitino client. | (none) | No | 0.9.0-incubating || `enable_credential_vending` | Whether to enable credential vending for the Gravitino Virtual File System. | `false` | No | 0.9.0-incubating || `gvfs_gravitino_client_` | The configuration key prefix for the Gravitino client. You can set the config for the Gravitino client. | (none) | No | 1.0.0 |+| `enable_fileset_metadata_cache` | Whether to cache the fileset or fileset catalog metadata in the Gravitino Virtual File System. Note that this cache causes a side effect: if you modify the fileset or fileset catalog metadata, the client can not see the latest changes. | `false` | No | 1.0.0 |To configure the Gravitino client, use properties prefixed with `gvfs_gravitino_client_`. These properties will be passed to the Gravitino client after removing the `gvfs_` prefix.