Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#8314] feat(iceberg): Support Iceberg table metadata cache (#8696)

FANNGb38bdb5eea94
diff --git a/catalogs/catalog-common/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergConstants.java b/catalogs/catalog-common/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergConstants.java
index 8e1a520252..183853d005 100644
--- a/catalogs/catalog-common/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergConstants.java
+++ b/catalogs/catalog-common/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergConstants.java
@@ -94,4 +94,9 @@ public class IcebergConstants {
public static final String ICEBERG_REST_DEFAULT_METALAKE = "gravitino";
public static final String ICEBERG_REST_DEFAULT_CATALOG = "default_catalog";
public static final String ICEBERG_REST_DEFAULT_DYNAMIC_CATALOG_NAME = "default-catalog-name";
+
+ public static final String TABLE_METADATA_CACHE_IMPL = "table-metadata-cache-impl";
+ public static final String TABLE_METADATA_CACHE_CAPACITY = "table-metadata-cache-capacity";
+ public static final String TABLE_METADATA_CACHE_EXPIRE_MINUTES =
+ "table-metadata-cache-expire-minutes";
}
diff --git a/catalogs/catalog-common/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergPropertiesUtils.java b/catalogs/catalog-common/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergPropertiesUtils.java
index dc2ad6d1a0..48c24e7b8c 100644
--- a/catalogs/catalog-common/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergPropertiesUtils.java
+++ b/catalogs/catalog-common/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergPropertiesUtils.java
@@ -67,6 +67,15 @@ public class IcebergPropertiesUtils {
map.put(
AzureProperties.GRAVITINO_AZURE_STORAGE_ACCOUNT_KEY,
IcebergConstants.ICEBERG_ADLS_STORAGE_ACCOUNT_KEY);
+ // Table metadata cache
+ map.put(IcebergConstants.TABLE_METADATA_CACHE_IMPL, IcebergConstants.TABLE_METADATA_CACHE_IMPL);
+ map.put(
+ IcebergConstants.TABLE_METADATA_CACHE_CAPACITY,
+ IcebergConstants.TABLE_METADATA_CACHE_CAPACITY);
+ map.put(
+ IcebergConstants.TABLE_METADATA_CACHE_EXPIRE_MINUTES,
+ IcebergConstants.TABLE_METADATA_CACHE_EXPIRE_MINUTES);
+
GRAVITINO_CONFIG_TO_ICEBERG = Collections.unmodifiableMap(map);
Map<String, String> icebergCatalogConfigToGravitino = new HashMap<>();
diff --git a/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergCatalogPropertiesMetadata.java b/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergCatalogPropertiesMetadata.java
index 2a0510c86d..82b063f242 100644
--- a/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergCatalogPropertiesMetadata.java
+++ b/catalogs/catalog-lakehouse-iceberg/src/main/java/org/apache/gravitino/catalog/lakehouse/iceberg/IcebergCatalogPropertiesMetadata.java
@@ -19,6 +19,7 @@
package org.apache.gravitino.catalog.lakehouse.iceberg;
import static org.apache.gravitino.connector.PropertyEntry.enumImmutablePropertyEntry;
+import static org.apache.gravitino.connector.PropertyEntry.integerOptionalPropertyEntry;
import static org.apache.gravitino.connector.PropertyEntry.stringOptionalPropertyEntry;
import static org.apache.gravitino.connector.PropertyEntry.stringRequiredPropertyEntry;
@@ -121,6 +122,24 @@ public class IcebergCatalogPropertiesMetadata extends BaseCatalogPropertiesMetad
"Azure storage account key",
false /* immutable */,
null /* defaultValue */,
+ false /* hidden */),
+ stringOptionalPropertyEntry(
+ IcebergConstants.TABLE_METADATA_CACHE_IMPL,
+ "Table metadata cache implementation",
+ false /* immutable */,
+ null /* defaultValue */,
+ false /* hidden */),
+ integerOptionalPropertyEntry(
+ IcebergConstants.TABLE_METADATA_CACHE_CAPACITY,
+ "Table metadata cache capacity",
+ false /* immutable */,
+ 200 /* defaultValue */,
+ false /* hidden */),
+ integerOptionalPropertyEntry(
+ IcebergConstants.TABLE_METADATA_CACHE_EXPIRE_MINUTES,
+ "Table metadata cache TTL in minutes",
+ false /* immutable */,
+ 60 /* defaultValue */,
false /* hidden */));
HashMap<String, PropertyEntry<?>> result = Maps.newHashMap();
result.putAll(Maps.uniqueIndex(propertyEntries, PropertyEntry::getName));
diff --git a/common/src/main/java/org/apache/gravitino/config/ConfigConstants.java b/common/src/main/java/org/apache/gravitino/config/ConfigConstants.java
index 17c08ac0e9..0eb0faa08a 100644
--- a/common/src/main/java/org/apache/gravitino/config/ConfigConstants.java
+++ b/common/src/main/java/org/apache/gravitino/config/ConfigConstants.java
@@ -80,6 +80,9 @@ public final class ConfigConstants {
/** The version number for the 1.0.0 release. */
public static final String VERSION_1_0_0 = "1.0.0";
+ /** The version number for the 1.1.0 release. */
+ public static final String VERSION_1_1_0 = "1.1.0";
+
/** The current version of backend storage initialization script. */
public static final String CURRENT_SCRIPT_VERSION = VERSION_1_0_0;
}
diff --git a/core/src/main/java/org/apache/gravitino/utils/ClassUtils.java b/core/src/main/java/org/apache/gravitino/utils/ClassUtils.java
index 7307394eb8..a644462e3f 100644
--- a/core/src/main/java/org/apache/gravitino/utils/ClassUtils.java
+++ b/core/src/main/java/org/apache/gravitino/utils/ClassUtils.java
@@ -20,11 +20,19 @@
package org.apache.gravitino.utils;
public class ClassUtils {
- public static <T> T loadClass(String className) {
+ public static <T> T loadAndGetInstance(String className) {
try {
return (T) Class.forName(className).getDeclaredConstructor().newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
}
+
+ public static <T> T loadAndGetInstance(String className, ClassLoader classLoader) {
+ try {
+ return (T) Class.forName(className, true, classLoader).getDeclaredConstructor().newInstance();
+ } catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
}
diff --git a/docs/iceberg-rest-service.md b/docs/iceberg-rest-service.md
index a97618e5a6..5a06169b12 100644
--- a/docs/iceberg-rest-service.md
+++ b/docs/iceberg-rest-service.md
@@ -30,6 +30,7 @@ There are some key difference between Gravitino Iceberg REST server and Gravitin
- Supports Audit log.
- Supports OAuth2 and HTTPS.
- Provides a pluggable metrics store interface to store and delete Iceberg metrics.
+- Supports table metadata cache.
## Server management
@@ -424,6 +425,18 @@ Gravitino provides a pluggable metrics store interface to store and delete Icebe
| `gravitino.iceberg-rest.metricsStoreRetainDays` | The days to retain Iceberg metrics in store, the value not greater than 0 means retain forever. | -1 | No | 0.4.0 |
| `gravitino.iceberg-rest.metricsQueueCapacity` | The size of queue to store metrics temporally before storing to the persistent storage. Metrics will be dropped when queue is full. | 1000 | No | 0.4.0 |
+### Iceberg table metadata cache configuration
+
+Gravitino features a pluggable cache system for updating or retrieving table metadata in the cache. It validates the location of table metadata against the catalog backend to ensure the correctness of cached data.
+
+| Configuration item | Description | Default value | Required | Since Version |
+|--------------------------------------------------------------|---------------------------------------------|---------------|----------|---------------|
+| `gravitino.iceberg-rest.table-metadata-cache-impl` | The implement of the cache. | (none) | No | 1.1.0 |
+| `gravitino.iceberg-rest.table-metadata-cache-capacity` | The capacity of table metadata cache. | 200 | No | 1.1.0 |
+| `gravitino.iceberg-rest.table-metadata-cache-expire-minutes` | The expire minutes of table metadata cache. | 60 | No | 1.1.0 |
+
+Gravitino provides the build-in `org.apache.gravitino.iceberg.common.cache.LocalTableMetadataCache` to store the cached data in the memory. You could also implement your custom table metadata cache by implementing the `org.apache.gravitino.iceberg.common.cache.TableMetadataCache` interface.
+
### Misc configurations
| Configuration item | Description | Default value | Required | Since Version |
diff --git a/docs/lakehouse-iceberg-catalog.md b/docs/lakehouse-iceberg-catalog.md
index bb92e38c3f..8875ebf78e 100644
--- a/docs/lakehouse-iceberg-catalog.md
+++ b/docs/lakehouse-iceberg-catalog.md
@@ -30,6 +30,7 @@ Builds with Apache Iceberg `1.9.2`. The Apache Iceberg table format version is `
- Doesn't support snapshot or table management operations.
- Supports multi storage, including S3, GCS, ADLS, OSS and HDFS.
- Supports Kerberos or simple authentication for Iceberg catalog with Hive backend.
+- Supports table metadata cache.
### Catalog properties
@@ -167,6 +168,18 @@ Users can use the following properties to configure the security of the catalog
| `authentication.kerberos.check-interval-sec` | The check interval of Kerberos credential for Iceberg catalog. | 60 | No | 0.6.0-incubating |
| `authentication.kerberos.keytab-fetch-timeout-sec` | The fetch timeout of retrieving Kerberos keytab from `authentication.kerberos.keytab-uri`. | 60 | No | 0.6.0-incubating |
+#### Table metadata cache
+
+Gravitino features a pluggable cache system for updating or retrieving table metadata in the cache. It validates the location of table metadata against the catalog backend to ensure the correctness of cached data.
+
+| Configuration item | Description | Default value | Required | Since Version |
+|---------------------------------------|---------------------------------------------|---------------|----------|---------------|
+| `table-metadata-cache-impl` | The implement of the cache. | (none) | No | 1.1.0 |
+| `table-metadata-cache-capacity` | The capacity of table metadata cache. | 200 | No | 1.1.0 |
+| `table-metadata-cache-expire-minutes` | The expire minutes of table metadata cache. | 60 | No | 1.1.0 |
+
+Gravitino provides the build-in `org.apache.gravitino.iceberg.common.cache.LocalTableMetadataCache` to store the cached data in the memory. You could also implement your custom table metadata cache by implementing the `org.apache.gravitino.iceberg.common.cache.TableMetadataCache` interface.
+
### Catalog operations
Please refer to [Manage Relational Metadata Using Gravitino](./manage-relational-metadata-using-gravitino.md#catalog-operations) for more details.
diff --git a/iceberg/iceberg-common/build.gradle.kts b/iceberg/iceberg-common/build.gradle.kts
index b67e04238a..bf83f34945 100644
--- a/iceberg/iceberg-common/build.gradle.kts
+++ b/iceberg/iceberg-common/build.gradle.kts
@@ -110,6 +110,7 @@ dependencies {
testImplementation(project(":server-common"))
testImplementation(libs.junit.jupiter.api)
testImplementation(libs.junit.jupiter.params)
+ testImplementation(libs.mockito.core)
testImplementation(libs.sqlite.jdbc)
testRuntimeOnly(libs.junit.jupiter.engine)
diff --git a/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/IcebergConfig.java b/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/IcebergConfig.java
index cea8ec3d65..eb6e6b2a07 100644
--- a/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/IcebergConfig.java
+++ b/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/IcebergConfig.java
@@ -262,6 +262,27 @@ public class IcebergConfig extends Config implements OverwriteDefaultConfig {
.stringConf()
.create();
+ public static final ConfigEntry<String> TABLE_METADATA_CACHE_IMPL =
+ new ConfigBuilder(IcebergConstants.TABLE_METADATA_CACHE_IMPL)
+ .doc("Table metadata cache implementation")
+ .version(ConfigConstants.VERSION_1_1_0)
+ .stringConf()
+ .create();
+
+ public static final ConfigEntry<Integer> TABLE_METADATA_CACHE_CAPACITY =
+ new ConfigBuilder(IcebergConstants.TABLE_METADATA_CACHE_CAPACITY)
+ .doc("Table metadata cache capacity")
+ .version(ConfigConstants.VERSION_1_1_0)
+ .intConf()
+ .createWithDefault(200);
+
+ public static final ConfigEntry<Integer> TABLE_METADATA_CACHE_EXPIRE_MINUTES =
+ new ConfigBuilder(IcebergConstants.TABLE_METADATA_CACHE_EXPIRE_MINUTES)
+ .doc("Table metadata cache expire minutes")
+ .version(ConfigConstants.VERSION_1_1_0)
+ .intConf()
+ .createWithDefault(60);
+
public String getJdbcDriver() {
return get(JDBC_DRIVER);
}
diff --git a/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/cache/BaseTableMetadataCache.java b/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/cache/BaseTableMetadataCache.java
new file mode 100644
index 0000000000..897f25e618
--- /dev/null
+++ b/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/cache/BaseTableMetadataCache.java
@@ -0,0 +1,84 @@
+/*
+ * 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.gravitino.iceberg.common.cache;
+
+import java.util.Optional;
+import org.apache.iceberg.TableMetadata;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Abstract base class implementing {@link TableMetadataCache} that provides core metadata caching
+ * functionality with validation of metadata location against the latest version.
+ */
+public abstract class BaseTableMetadataCache implements TableMetadataCache {
+
+ public static final Logger LOG = LoggerFactory.getLogger(BaseTableMetadataCache.class);
+ /** Component to retrieve the latest metadata location for a table, used for validation. */
+ private SupportsMetadataLocation supportsMetadataLocation;
+
+ /**
+ * Abstract method to retrieve cached {@link TableMetadata} for a table. Subclasses must implement
+ * this to provide actual cache retrieval logic.
+ *
+ * @param tableIdentifier Identifier of the table to retrieve cached metadata for
+ * @return Cached {@link TableMetadata} wrapped in an {@link Optional}, or an empty {@link
+ * Optional} if not found in cache
+ */
+ protected abstract Optional<TableMetadata> doGetTableMetadata(TableIdentifier tableIdentifier);
+
+ protected void initialize(SupportsMetadataLocation supportsMetadataLocation) {
+ this.supportsMetadataLocation = supportsMetadataLocation;
+ }
+
+ /**
+ * Retrieves and validates cached table metadata by comparing the cached metadata's location with
+ * the latest known location. Invalidates the cache if locations mismatch.
+ *
+ * @param tableIdentifier Identifier of the table to retrieve metadata for
+ * @return Valid {@link TableMetadata} if cache is valid and locations match; {@code null} if
+ * cache miss, location is invalid, or locations mismatch (after invalidation)
+ */
+ @Override
+ public Optional<TableMetadata> getTableMetadata(TableIdentifier tableIdentifier) {
+ Optional<TableMetadata> tableMetadataOptional = doGetTableMetadata(tableIdentifier);
+ if (!tableMetadataOptional.isPresent()) {
+ return Optional.empty();
+ }
+ TableMetadata tableMetadata = tableMetadataOptional.get();
+ String latestLocation = supportsMetadataLocation.metadataLocation(tableIdentifier);
+ if (latestLocation == null) {
+ return Optional.empty();
+ }
+ if (latestLocation.equals(tableMetadata.metadataFileLocation())) {
+ return Optional.of(tableMetadata);
+ }
+
+ LOG.debug(
+ "The cached table metadata is not latest, table identifier: {}, "
+ + "table metadata location in cache: {}, latest metadata location: {}",
+ tableIdentifier,
+ tableMetadata.metadataFileLocation(),
+ latestLocation);
+ invalidate(tableIdentifier);
+ return Optional.empty();
+ }
+}
diff --git a/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/cache/LocalTableMetadataCache.java b/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/cache/LocalTableMetadataCache.java
new file mode 100644
index 0000000000..381e2605eb
--- /dev/null
+++ b/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/cache/LocalTableMetadataCache.java
@@ -0,0 +1,87 @@
+/*
+ * 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.gravitino.iceberg.common.cache;
+
+import com.github.benmanes.caffeine.cache.Cache;
+import com.github.benmanes.caffeine.cache.Caffeine;
+import com.google.common.annotations.VisibleForTesting;
+import java.io.IOException;
+import java.util.Map;
+import java.util.Optional;
+import java.util.concurrent.TimeUnit;
+import org.apache.iceberg.TableMetadata;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class LocalTableMetadataCache extends BaseTableMetadataCache {
+ public static final Logger LOG = LoggerFactory.getLogger(LocalTableMetadataCache.class);
+ private Cache<TableIdentifier, TableMetadata> tableMetadataCache;
+
+ @Override
+ public void initialize(
+ int capacity,
+ int expireMinutes,
+ Map<String, String> catalogProperties,
+ SupportsMetadataLocation supportsMetadataLocation) {
+ super.initialize(supportsMetadataLocation);
+ this.tableMetadataCache =
+ Caffeine.newBuilder()
+ .maximumSize(capacity)
+ .expireAfterAccess(expireMinutes, TimeUnit.MINUTES)
+ // control the cache size not exceed the cache capacity
+ .executor(Runnable::run)
+ .build();
+ }
+
+ @Override
+ public void invalidate(TableIdentifier tableIdentifier) {
+ LOG.debug("Invalidate table cache, table identifier: {}", tableIdentifier);
+ tableMetadataCache.invalidate(tableIdentifier);
+ }
+
+ @Override
+ public void updateTableMetadata(TableIdentifier tableIdentifier, TableMetadata tableMetadata) {
+ LOG.debug(
+ "Update table cache, table identifier: {}, table metadata location: {}",
+ tableIdentifier,
+ tableMetadata.metadataFileLocation());
+ tableMetadataCache.put(tableIdentifier, tableMetadata);
+ }
+
+ @Override
+ public void close() throws IOException {
+ LOG.info("Close Iceberg table metadata cache");
+ if (tableMetadataCache != null) {
+ tableMetadataCache.invalidateAll();
+ tableMetadataCache.cleanUp();
+ }
+ }
+
+ @Override
+ protected Optional<TableMetadata> doGetTableMetadata(TableIdentifier tableIdentifier) {
+ return Optional.ofNullable(tableMetadataCache.getIfPresent(tableIdentifier));
+ }
+
+ @VisibleForTesting
+ int size() {
+ return tableMetadataCache.asMap().size();
+ }
+}
diff --git a/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/cache/SupportsMetadataLocation.java b/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/cache/SupportsMetadataLocation.java
new file mode 100644
index 0000000000..ea52ae3888
--- /dev/null
+++ b/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/cache/SupportsMetadataLocation.java
@@ -0,0 +1,36 @@
+/*
+ * 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.gravitino.iceberg.common.cache;
+
+import org.apache.iceberg.catalog.TableIdentifier;
+
+/** An interface that supports retrieving the metadata location for a table. */
+public interface SupportsMetadataLocation {
+ /** The property key used to store or reference the metadata location of a table. */
+ String METADATA_LOCATION_PROP = "metadata_location";
+
+ /**
+ * Retrieves the metadata location for the specified table.
+ *
+ * @param tableIdentifier the identifier of the table to retrieve metadata location for
+ * @return the metadata location of the table as a String
+ */
+ String metadataLocation(TableIdentifier tableIdentifier);
+}
diff --git a/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/cache/TableMetadataCache.java b/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/cache/TableMetadataCache.java
new file mode 100644
index 0000000000..c4804a1927
--- /dev/null
+++ b/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/cache/TableMetadataCache.java
@@ -0,0 +1,99 @@
+/*
+ * 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.gravitino.iceberg.common.cache;
+
+import java.io.Closeable;
+import java.util.Map;
+import java.util.Optional;
+import org.apache.iceberg.TableMetadata;
+import org.apache.iceberg.catalog.TableIdentifier;
+
+/**
+ * A cache interface for managing {@link TableMetadata} objects, providing methods for
+ * initialization, retrieval, update, invalidation, and resource cleanup.
+ */
+public interface TableMetadataCache extends Closeable {
+ /**
+ * A no-op (dummy) implementation of {@link TableMetadataCache} that performs no actual caching.
+ * Useful for scenarios where caching functionality is not required.
+ */
+ TableMetadataCache DUMMY =
+ new TableMetadataCache() {
+ @Override
+ public void initialize(
+ int capacity,
+ int expireMinutes,
+ Map<String, String> catalogProperties,
+ SupportsMetadataLocation supportsMetadataLocation) {}
+
+ @Override
+ public void invalidate(TableIdentifier tableIdentifier) {}
+
+ @Override
+ public Optional<TableMetadata> getTableMetadata(TableIdentifier tableIdentifier) {
+ return Optional.empty();
+ }
+
+ @Override
+ public void updateTableMetadata(
+ TableIdentifier tableIdentifier, TableMetadata tableMetadata) {}
+
+ @Override
+ public void close() {}
+ };
+
+ /**
+ * Initializes the metadata cache with specified configuration.
+ *
+ * @param capacity Maximum number of entries the cache can hold
+ * @param catalogProperties Catalog-specific properties that may influence cache behavior
+ * @param supportsMetadataLocation Component to resolve the latest metadata location for
+ * validation
+ */
+ void initialize(
+ int capacity,
+ int expireMinutes,
+ Map<String, String> catalogProperties,
+ SupportsMetadataLocation supportsMetadataLocation);
+
+ /**
+ * Removes the cached metadata entry for the specified table from the cache.
+ *
+ * @param tableIdentifier Identifier of the table whose cached metadata should be invalidated
+ */
+ void invalidate(TableIdentifier tableIdentifier);
+
+ /**
+ * Retrieves the latest {@link TableMetadata} for the specified table.
+ *
+ * @param tableIdentifier Identifier of the table to retrieve metadata for
+ * @return Cached {@link TableMetadata} wrapped in an {@link Optional}, or an empty {@link
+ * Optional} if no valid entry exists in the cache or the cache is not latest.
+ */
+ Optional<TableMetadata> getTableMetadata(TableIdentifier tableIdentifier);
+
+ /**
+ * Updates the cache with new {@link TableMetadata} for the specified table.
+ *
+ * @param tableIdentifier Identifier of the table to update metadata for
+ * @param tableMetadata New {@link TableMetadata} to store in the cache
+ */
+ void updateTableMetadata(TableIdentifier tableIdentifier, TableMetadata tableMetadata);
+}
diff --git a/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/IcebergCatalogWrapper.java b/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/IcebergCatalogWrapper.java
index 9f96e36b08..3db4b29dc7 100644
--- a/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/IcebergCatalogWrapper.java
+++ b/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/ops/IcebergCatalogWrapper.java
@@ -29,10 +29,14 @@ import lombok.Setter;
import org.apache.commons.lang3.StringUtils;
import org.apache.gravitino.catalog.lakehouse.iceberg.IcebergCatalogBackend;
import org.apache.gravitino.iceberg.common.IcebergConfig;
+import org.apache.gravitino.iceberg.common.cache.SupportsMetadataLocation;
+import org.apache.gravitino.iceberg.common.cache.TableMetadataCache;
import org.apache.gravitino.iceberg.common.utils.IcebergCatalogUtil;
+import org.apache.gravitino.utils.ClassUtils;
import org.apache.gravitino.utils.IsolatedClassLoader;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.security.UserGroupInformation;
+import org.apache.iceberg.TableMetadata;
import org.apache.iceberg.Transaction;
import org.apache.iceberg.catalog.Catalog;
import org.apache.iceberg.catalog.Namespace;
@@ -70,6 +74,7 @@ public class IcebergCatalogWrapper implements AutoCloseable {
private final IcebergCatalogBackend catalogBackend;
private String catalogUri = null;
private Map<String, String> catalogPropertiesMap;
+ private TableMetadataCache metadataCache;
public IcebergCatalogWrapper(IcebergConfig icebergConfig) {
this.catalogBackend =
@@ -90,6 +95,7 @@ public class IcebergCatalogWrapper implements AutoCloseable {
this.asNamespaceCatalog = (SupportsNamespaces) catalog;
}
+ this.metadataCache = loadTableMetadataCache(icebergConfig, catalog);
this.catalogPropertiesMap = icebergConfig.getIcebergCatalogProperties();
}
@@ -163,19 +169,35 @@ public class IcebergCatalogWrapper implements AutoCloseable {
if (request.stageCreate()) {
return CatalogHandlers.stageTableCreate(catalog, namespace, request);
}
- return CatalogHandlers.createTable(catalog, namespace, request);
+ LoadTableResponse loadTableResponse = CatalogHandlers.createTable(catalog, namespace, request);
+ if (loadTableResponse != null) {
+ TableIdentifier tableIdentifier = TableIdentifier.of(namespace, request.name());
+ metadataCache.updateTableMetadata(tableIdentifier, loadTableResponse.tableMetadata());
+ }
+ return loadTableResponse;
}
public void dropTable(TableIdentifier tableIdentifier) {
+ metadataCache.invalidate(tableIdentifier);
CatalogHandlers.dropTable(catalog, tableIdentifier);
}
public void purgeTable(TableIdentifier tableIdentifier) {
+ metadataCache.invalidate(tableIdentifier);
CatalogHandlers.purgeTable(catalog, tableIdentifier);
}
public LoadTableResponse loadTable(TableIdentifier tableIdentifier) {
- return CatalogHandlers.loadTable(catalog, tableIdentifier);
+ Optional<TableMetadata> tableMetadataOptional = metadataCache.getTableMetadata(tableIdentifier);
+ if (tableMetadataOptional.isPresent()) {
+ return LoadTableResponse.builder().withTableMetadata(tableMetadataOptional.get()).build();
+ }
+
+ LoadTableResponse loadTableResponse = CatalogHandlers.loadTable(catalog, tableIdentifier);
+ if (loadTableResponse != null) {
+ metadataCache.updateTableMetadata(tableIdentifier, loadTableResponse.tableMetadata());
+ }
+ return loadTableResponse;
}
public boolean tableExists(TableIdentifier tableIdentifier) {
@@ -187,12 +209,19 @@ public class IcebergCatalogWrapper implements AutoCloseable {
}
public void renameTable(RenameTableRequest renameTableRequest) {
+ metadataCache.invalidate(renameTableRequest.source());
CatalogHandlers.renameTable(catalog, renameTableRequest);
}
public LoadTableResponse updateTable(
TableIdentifier tableIdentifier, UpdateTableRequest updateTableRequest) {
- return CatalogHandlers.updateTable(catalog, tableIdentifier, updateTableRequest);
+ metadataCache.invalidate(tableIdentifier);
+ LoadTableResponse loadTableResponse =
+ CatalogHandlers.updateTable(catalog, tableIdentifier, updateTableRequest);
+ if (loadTableResponse != null) {
+ metadataCache.updateTableMetadata(tableIdentifier, loadTableResponse.tableMetadata());
+ }
+ return loadTableResponse;
}
public LoadTableResponse updateTable(IcebergTableChange icebergTableChange) {
@@ -242,6 +271,7 @@ public class IcebergCatalogWrapper implements AutoCloseable {
// JdbcCatalog and WrappedHiveCatalog need close.
((AutoCloseable) catalog).close();
}
+ metadataCache.close();
// Because each catalog in Gravitino has its own classloader, after a catalog is no longer used
// for a long time or dropped, the instance of classloader needs to be released. In order to
@@ -306,4 +336,31 @@ public class IcebergCatalogWrapper implements AutoCloseable {
this.transaction = transaction;
}
}
+
+ private TableMetadataCache loadTableMetadataCache(IcebergConfig config, Catalog catalog) {
+ String impl = config.get(IcebergConfig.TABLE_METADATA_CACHE_IMPL);
+ if (StringUtils.isBlank(impl)) {
+ return TableMetadataCache.DUMMY;
+ }
+
+ Preconditions.checkArgument(
+ catalog instanceof SupportsMetadataLocation,
+ "You shouldn't enable Iceberg metadata cache for the catalog %s,"
+ + " because the catalog impl does not support get metadata location.",
+ catalog.name());
+
+ TableMetadataCache cache =
+ ClassUtils.loadAndGetInstance(impl, Thread.currentThread().getContextClassLoader());
+ int capacity = config.get(IcebergConfig.TABLE_METADATA_CACHE_CAPACITY);
+ int expireMinutes = config.get(IcebergConfig.TABLE_METADATA_CACHE_EXPIRE_MINUTES);
+ cache.initialize(
+ capacity, expireMinutes, config.getAllConfig(), (SupportsMetadataLocation) catalog);
+ LOG.info(
+ "Load Iceberg table metadata cache for catalog: {}, impl:{}, capacity: {}, expire minutes: {}",
+ catalog.name(),
+ impl,
+ capacity,
+ expireMinutes);
+ return cache;
+ }
}
diff --git a/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/utils/IcebergCatalogUtil.java b/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/utils/IcebergCatalogUtil.java
index fe33948606..3ed1ad3ec0 100644
--- a/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/utils/IcebergCatalogUtil.java
+++ b/iceberg/iceberg-common/src/main/java/org/apache/gravitino/iceberg/common/utils/IcebergCatalogUtil.java
@@ -43,9 +43,12 @@ import org.apache.iceberg.CatalogProperties;
import org.apache.iceberg.CatalogUtil;
import org.apache.iceberg.catalog.Catalog;
import org.apache.iceberg.hive.HiveCatalog;
+import org.apache.iceberg.hive.HiveCatalogWithMetadataLocationSupport;
import org.apache.iceberg.inmemory.InMemoryCatalog;
import org.apache.iceberg.jdbc.JdbcCatalog;
+import org.apache.iceberg.jdbc.JdbcCatalogWithMetadataLocationSupport;
import org.apache.iceberg.jdbc.UncheckedSQLException;
+import org.apache.iceberg.memory.MemoryCatalogWithMetadataLocationSupport;
import org.apache.iceberg.rest.RESTCatalog;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@@ -56,7 +59,7 @@ public class IcebergCatalogUtil {
private static InMemoryCatalog loadMemoryCatalog(IcebergConfig icebergConfig) {
String icebergCatalogName = icebergConfig.getCatalogBackendName();
- InMemoryCatalog memoryCatalog = new InMemoryCatalog();
+ InMemoryCatalog memoryCatalog = new MemoryCatalogWithMetadataLocationSupport();
Map<String, String> resultProperties = icebergConfig.getIcebergCatalogProperties();
if (!resultProperties.containsKey(IcebergConstants.WAREHOUSE)) {
resultProperties.put(IcebergConstants.WAREHOUSE, "/tmp");
@@ -66,7 +69,7 @@ public class IcebergCatalogUtil {
}
private static HiveCatalog loadHiveCatalog(IcebergConfig icebergConfig) {
- ClosableHiveCatalog hiveCatalog = new ClosableHiveCatalog();
+ ClosableHiveCatalog hiveCatalog = new HiveCatalogWithMetadataLocationSupport();
HdfsConfiguration hdfsConfiguration = new HdfsConfiguration();
String icebergCatalogName = icebergConfig.getCatalogBackendName();
@@ -129,7 +132,8 @@ public class IcebergCatalogUtil {
throw new IllegalArgumentException("Couldn't load jdbc driver " + driverClassName);
}
JdbcCatalog jdbcCatalog =
- new JdbcCatalog(null, null, icebergConfig.get(IcebergConfig.JDBC_INIT_TABLES));
+ new JdbcCatalogWithMetadataLocationSupport(
+ icebergConfig.get(IcebergConfig.JDBC_INIT_TABLES));
HdfsConfiguration hdfsConfiguration = new HdfsConfiguration();
properties.forEach(hdfsConfiguration::set);
diff --git a/iceberg/iceberg-common/src/main/java/org/apache/iceberg/hive/HiveCatalogWithMetadataLocationSupport.java b/iceberg/iceberg-common/src/main/java/org/apache/iceberg/hive/HiveCatalogWithMetadataLocationSupport.java
new file mode 100644
index 0000000000..8a9414c4d3
--- /dev/null
+++ b/iceberg/iceberg-common/src/main/java/org/apache/iceberg/hive/HiveCatalogWithMetadataLocationSupport.java
@@ -0,0 +1,72 @@
+/*
+ * 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.iceberg.hive;
+
+import com.google.common.base.Preconditions;
+import java.util.Map;
+import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.gravitino.iceberg.common.ClosableHiveCatalog;
+import org.apache.gravitino.iceberg.common.cache.SupportsMetadataLocation;
+import org.apache.hadoop.hive.metastore.IMetaStoreClient;
+import org.apache.hadoop.hive.metastore.api.Table;
+import org.apache.iceberg.BaseMetastoreTableOperations;
+import org.apache.iceberg.ClientPool;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.thrift.TException;
+
+public class HiveCatalogWithMetadataLocationSupport extends ClosableHiveCatalog
+ implements SupportsMetadataLocation {
+ private ClientPool<IMetaStoreClient, TException> metaClients;
+
+ @Override
+ public void initialize(String name, Map<String, String> properties) {
+ super.initialize(name, properties);
+ loadFields();
+ }
+
+ @Override
+ public String metadataLocation(TableIdentifier tableIdentifier) {
+ String dbName = tableIdentifier.namespace().level(0);
+ String tableName = tableIdentifier.name();
+
+ try {
+ Table table = metaClients.run(client -> client.getTable(dbName, tableName));
+ String tableType = table.getParameters().get(BaseMetastoreTableOperations.TABLE_TYPE_PROP);
+ if (tableType == null
+ || !tableType.equalsIgnoreCase(BaseMetastoreTableOperations.ICEBERG_TABLE_TYPE_VALUE)) {
+ return null;
+ }
+ return table.getParameters().get(METADATA_LOCATION_PROP);
+ } catch (Exception e) {
+ return null;
+ }
+ }
+
+ private void loadFields() {
+ try {
+ this.metaClients =
+ (ClientPool<IMetaStoreClient, TException>) FieldUtils.readField(this, "clients", true);
+ Preconditions.checkState(
+ metaClients != null, "Failed to get clients field from hive catalog");
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException(e);
+ }
+ }
+}
diff --git a/iceberg/iceberg-common/src/main/java/org/apache/iceberg/jdbc/JdbcCatalogWithMetadataLocationSupport.java b/iceberg/iceberg-common/src/main/java/org/apache/iceberg/jdbc/JdbcCatalogWithMetadataLocationSupport.java
new file mode 100644
index 0000000000..70efa855ea
--- /dev/null
+++ b/iceberg/iceberg-common/src/main/java/org/apache/iceberg/jdbc/JdbcCatalogWithMetadataLocationSupport.java
@@ -0,0 +1,76 @@
+/*
+ * 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.iceberg.jdbc;
+
+import com.google.common.base.Preconditions;
+import java.util.Map;
+import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.gravitino.iceberg.common.cache.SupportsMetadataLocation;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.jdbc.JdbcUtil.SchemaVersion;
+
+// Use Iceberg package to reuse JdbcUtil related classes.
+public class JdbcCatalogWithMetadataLocationSupport extends JdbcCatalog
+ implements SupportsMetadataLocation {
+ private String jdbcCatalogName;
+ private JdbcClientPool jdbcConnections;
+ private SchemaVersion jdbcSchemaVersion;
+
+ public JdbcCatalogWithMetadataLocationSupport(boolean initializeCatalogTables) {
+ super(null, null, initializeCatalogTables);
+ }
+
+ @Override
+ public void initialize(String name, Map<String, String> properties) {
+ super.initialize(name, properties);
+ loadFields();
+ }
+
+ @Override
+ public String metadataLocation(TableIdentifier tableIdentifier) {
+ Map<String, String> table;
+
+ try {
+ table =
+ JdbcUtil.loadTable(jdbcSchemaVersion, jdbcConnections, jdbcCatalogName, tableIdentifier);
+ } catch (Exception e) {
+ return null;
+ }
+
+ return table.get(METADATA_LOCATION_PROP);
+ }
+
+ private void loadFields() {
+ try {
+ this.jdbcCatalogName = (String) FieldUtils.readField(this, "catalogName", true);
+ Preconditions.checkState(
+ jdbcCatalogName != null, "Failed to get catalogName field from JDBC catalog");
+ this.jdbcConnections = (JdbcClientPool) FieldUtils.readField(this, "connections", true);
+ Preconditions.checkState(
+ jdbcConnections != null, "Failed to get connections field from JDBC catalog");
+ this.jdbcSchemaVersion =
+ (JdbcUtil.SchemaVersion) FieldUtils.readField(this, "schemaVersion", true);
+ Preconditions.checkState(
+ jdbcSchemaVersion != null, "Failed to get schemaVersion field from JDBC catalog");
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException(e);
+ }
+ }
+}
diff --git a/iceberg/iceberg-common/src/main/java/org/apache/iceberg/memory/MemoryCatalogWithMetadataLocationSupport.java b/iceberg/iceberg-common/src/main/java/org/apache/iceberg/memory/MemoryCatalogWithMetadataLocationSupport.java
new file mode 100644
index 0000000000..e209d91b3e
--- /dev/null
+++ b/iceberg/iceberg-common/src/main/java/org/apache/iceberg/memory/MemoryCatalogWithMetadataLocationSupport.java
@@ -0,0 +1,56 @@
+/*
+ * 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.iceberg.memory;
+
+import com.google.common.base.Preconditions;
+import java.util.Map;
+import java.util.concurrent.ConcurrentMap;
+import org.apache.commons.lang3.reflect.FieldUtils;
+import org.apache.gravitino.iceberg.common.cache.SupportsMetadataLocation;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.inmemory.InMemoryCatalog;
+
+public class MemoryCatalogWithMetadataLocationSupport extends InMemoryCatalog
+ implements SupportsMetadataLocation {
+
+ private ConcurrentMap<TableIdentifier, String> tableStore;
+
+ @Override
+ public void initialize(String name, Map<String, String> properties) {
+ super.initialize(name, properties);
+ loadFields();
+ }
+
+ @Override
+ public String metadataLocation(TableIdentifier tableIdentifier) {
+ return tableStore.get(tableIdentifier);
+ }
+
+ private void loadFields() {
+ try {
+ this.tableStore =
+ (ConcurrentMap<TableIdentifier, String>) FieldUtils.readField(this, "tables", true);
+ Preconditions.checkArgument(
+ tableStore != null, "Failed to get tables field from memory catalog");
+ } catch (IllegalAccessException e) {
+ throw new RuntimeException(e);
+ }
+ }
+}
diff --git a/iceberg/iceberg-common/src/test/java/org/apache/gravitino/iceberg/common/cache/TestLocalMetadataCache.java b/iceberg/iceberg-common/src/test/java/org/apache/gravitino/iceberg/common/cache/TestLocalMetadataCache.java
new file mode 100644
index 0000000000..9b21877b04
--- /dev/null
+++ b/iceberg/iceberg-common/src/test/java/org/apache/gravitino/iceberg/common/cache/TestLocalMetadataCache.java
@@ -0,0 +1,102 @@
+/*
+ * 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.gravitino.iceberg.common.cache;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+
+import java.io.IOException;
+import java.util.Collections;
+import org.apache.iceberg.TableMetadata;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+class TestLocalMetadataCache {
+ private LocalTableMetadataCache cache;
+ private TableIdentifier testIdentifier;
+ private TableMetadata testMetadata;
+ private SupportsMetadataLocation supportsMetadataLocation;
+
+ @BeforeEach
+ void setUp() {
+ cache = new LocalTableMetadataCache();
+ testIdentifier = TableIdentifier.of("test", "table");
+ testMetadata = mock(TableMetadata.class);
+ when(testMetadata.metadataFileLocation()).thenReturn("test_location_" + testIdentifier.name());
+ supportsMetadataLocation = mock(SupportsMetadataLocation.class);
+ when(supportsMetadataLocation.metadataLocation(testIdentifier))
+ .thenReturn("test_location_" + testIdentifier.name());
+
+ cache.initialize(10, 60, Collections.emptyMap(), supportsMetadataLocation);
+ }
+
+ @AfterEach
+ void tearDown() throws IOException {
+ cache.close();
+ }
+
+ @Test
+ void testUpdateAndGetTableMetadata() {
+ Assertions.assertNull(cache.getTableMetadata(testIdentifier).orElse(null));
+ cache.updateTableMetadata(testIdentifier, testMetadata);
+ Assertions.assertEquals(testMetadata, cache.getTableMetadata(testIdentifier).orElse(null));
+
+ TableMetadata oldMetadata = mock(TableMetadata.class);
+ cache.updateTableMetadata(testIdentifier, oldMetadata);
+ Assertions.assertEquals(oldMetadata, cache.doGetTableMetadata(testIdentifier).orElse(null));
+ Assertions.assertNull(cache.getTableMetadata(testIdentifier).orElse(null));
+ }
+
+ @Test
+ void testInvalidate() {
+ cache.updateTableMetadata(testIdentifier, testMetadata);
+ Assertions.assertEquals(testMetadata, cache.getTableMetadata(testIdentifier).orElse(null));
+
+ cache.invalidate(testIdentifier);
+ Assertions.assertNull(cache.doGetTableMetadata(testIdentifier).orElse(null));
+ Assertions.assertNull(cache.getTableMetadata(testIdentifier).orElse(null));
+ }
+
+ @Test
+ void testClose() throws IOException {
+ cache.updateTableMetadata(testIdentifier, testMetadata);
+ cache.close();
+ Assertions.assertEquals(0, cache.size());
+ }
+
+ @Test
+ void testCacheCapacity() {
+ for (int i = 0; i < 15; i++) {
+ TableIdentifier id = TableIdentifier.of("test", "table" + i);
+ TableMetadata metadata = mock(TableMetadata.class);
+ cache.updateTableMetadata(id, metadata);
+ }
+ Assertions.assertEquals(10, cache.size());
+ }
+
+ @Test
+ void testCloseWithNullCache() throws IOException {
+ LocalTableMetadataCache emptyCache = new LocalTableMetadataCache();
+ emptyCache.close(); // Should not throw exception
+ }
+}
diff --git a/iceberg/iceberg-common/src/test/java/org/apache/iceberg/hive/TestHiveCatalogWithMetadataLocationSupport.java b/iceberg/iceberg-common/src/test/java/org/apache/iceberg/hive/TestHiveCatalogWithMetadataLocationSupport.java
new file mode 100644
index 0000000000..43e84d4b42
--- /dev/null
+++ b/iceberg/iceberg-common/src/test/java/org/apache/iceberg/hive/TestHiveCatalogWithMetadataLocationSupport.java
@@ -0,0 +1,33 @@
+/*
+ * 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.iceberg.hive;
+
+import java.util.Collections;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class TestHiveCatalogWithMetadataLocationSupport {
+
+ @Test
+ void testLoadFields() {
+ HiveCatalogWithMetadataLocationSupport catalog = new HiveCatalogWithMetadataLocationSupport();
+ Assertions.assertDoesNotThrow(() -> catalog.initialize("hive", Collections.emptyMap()));
+ }
+}
diff --git a/iceberg/iceberg-common/src/test/java/org/apache/iceberg/jdbc/TestJdbcCatalogWithMetadataLocationSupport.java b/iceberg/iceberg-common/src/test/java/org/apache/iceberg/jdbc/TestJdbcCatalogWithMetadataLocationSupport.java
new file mode 100644
index 0000000000..b029be1726
--- /dev/null
+++ b/iceberg/iceberg-common/src/test/java/org/apache/iceberg/jdbc/TestJdbcCatalogWithMetadataLocationSupport.java
@@ -0,0 +1,43 @@
+/*
+ * 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.iceberg.jdbc;
+
+import com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.CatalogProperties;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class TestJdbcCatalogWithMetadataLocationSupport {
+
+ @Test
+ void testLoadFields() {
+ JdbcCatalogWithMetadataLocationSupport catalog =
+ new JdbcCatalogWithMetadataLocationSupport(false);
+ Assertions.assertDoesNotThrow(
+ () ->
+ catalog.initialize(
+ "jdbc",
+ ImmutableMap.of(
+ CatalogProperties.URI,
+ "jdbc:h2:mem:test",
+ CatalogProperties.WAREHOUSE_LOCATION,
+ "warehouse")));
+ }
+}
diff --git a/iceberg/iceberg-common/src/test/java/org/apache/iceberg/memory/TestMemoryCatalogWithMetadataLocationSupport.java b/iceberg/iceberg-common/src/test/java/org/apache/iceberg/memory/TestMemoryCatalogWithMetadataLocationSupport.java
new file mode 100644
index 0000000000..cbfbd7b22f
--- /dev/null
+++ b/iceberg/iceberg-common/src/test/java/org/apache/iceberg/memory/TestMemoryCatalogWithMetadataLocationSupport.java
@@ -0,0 +1,56 @@
+/*
+ * 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.iceberg.memory;
+
+import java.util.Collections;
+import org.apache.iceberg.BaseTable;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.catalog.Namespace;
+import org.apache.iceberg.catalog.TableIdentifier;
+import org.apache.iceberg.types.Types;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+public class TestMemoryCatalogWithMetadataLocationSupport {
+ private static final Schema SCHEMA =
+ new Schema(Types.NestedField.required(1, "id", Types.IntegerType.get()));
+
+ @Test
+ void testMetadataLocation() {
+ MemoryCatalogWithMetadataLocationSupport catalog =
+ new MemoryCatalogWithMetadataLocationSupport();
+ catalog.initialize("memory", Collections.emptyMap());
+ catalog.createNamespace(Namespace.of("ns"));
+ TableIdentifier tableIdentifier = TableIdentifier.of("ns", "t");
+ BaseTable table = (BaseTable) catalog.createTable(tableIdentifier, SCHEMA);
+ String tableLocation = table.operations().current().metadataFileLocation();
+ Assertions.assertEquals(tableLocation, catalog.metadataLocation(tableIdentifier));
+
+ // update table
+ table.updateSchema().renameColumn("id", "new_id").commit();
+ Assertions.assertNotEquals(tableLocation, catalog.metadataLocation(tableIdentifier));
+ tableLocation = table.operations().current().metadataFileLocation();
+ Assertions.assertEquals(tableLocation, catalog.metadataLocation(tableIdentifier));
+
+ // delete table
+ catalog.dropTable(tableIdentifier);
+ Assertions.assertNull(catalog.metadataLocation(tableIdentifier));
+ }
+}
diff --git a/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/integration/test/IcebergRESTServiceBaseIT.java b/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/integration/test/IcebergRESTServiceBaseIT.java
index 0800f891e8..1cd1d0f63c 100644
--- a/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/integration/test/IcebergRESTServiceBaseIT.java
+++ b/iceberg/iceberg-rest-server/src/test/java/org/apache/gravitino/iceberg/integration/test/IcebergRESTServiceBaseIT.java
@@ -33,6 +33,7 @@ import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.StringUtils;
import org.apache.gravitino.catalog.lakehouse.iceberg.IcebergCatalogBackend;
import org.apache.gravitino.iceberg.common.IcebergConfig;
+import org.apache.gravitino.iceberg.common.cache.LocalTableMetadataCache;
import org.apache.gravitino.iceberg.integration.test.util.IcebergRESTServerManager;
import org.apache.gravitino.integration.test.util.ITUtils;
import org.apache.gravitino.server.web.JettyServerConfig;
@@ -117,6 +118,9 @@ public abstract class IcebergRESTServiceBaseIT {
private void registerIcebergCatalogConfig() {
Map<String, String> icebergConfigs = getCatalogConfig();
+ icebergConfigs.put(
+ IcebergConfig.ICEBERG_CONFIG_PREFIX + IcebergConfig.TABLE_METADATA_CACHE_IMPL.getKey(),
+ LocalTableMetadataCache.class.getName());
icebergRESTServerManager.registerCustomConfigs(icebergConfigs);
LOG.info("Iceberg REST service config registered, {}", StringUtils.join(icebergConfigs));
}
diff --git a/lineage/src/main/java/org/apache/gravitino/lineage/LineageService.java b/lineage/src/main/java/org/apache/gravitino/lineage/LineageService.java
index 2c74456d06..0bd4f1c67c 100644
--- a/lineage/src/main/java/org/apache/gravitino/lineage/LineageService.java
+++ b/lineage/src/main/java/org/apache/gravitino/lineage/LineageService.java
@@ -41,11 +41,11 @@ public class LineageService implements LineageDispatcher, SupportsRESTPackages {
public void initialize(LineageConfig lineageConfig) {
String sourceName = lineageConfig.source();
String sourceClass = lineageConfig.sourceClass();
- this.source = ClassUtils.loadClass(sourceClass);
+ this.source = ClassUtils.loadAndGetInstance(sourceClass);
this.sinkManager = new LineageSinkManager();
String processorClassName = lineageConfig.processorClass();
- this.processor = ClassUtils.loadClass(processorClassName);
+ this.processor = ClassUtils.loadAndGetInstance(processorClassName);
sinkManager.initialize(lineageConfig.sinks(), lineageConfig.getSinkConfigs());
source.initialize(lineageConfig.getConfigsWithPrefix(sourceName), this);
diff --git a/lineage/src/main/java/org/apache/gravitino/lineage/sink/LineageSinkEventListener.java b/lineage/src/main/java/org/apache/gravitino/lineage/sink/LineageSinkEventListener.java
index 2ed4625ecc..91ba6c3159 100644
--- a/lineage/src/main/java/org/apache/gravitino/lineage/sink/LineageSinkEventListener.java
+++ b/lineage/src/main/java/org/apache/gravitino/lineage/sink/LineageSinkEventListener.java
@@ -40,7 +40,7 @@ public class LineageSinkEventListener implements EventListenerPlugin {
Preconditions.checkArgument(
StringUtils.isNotBlank(sinkClassName),
LineageConfig.LINEAGE_SINK_CLASS_NAME + " is not set.");
- this.lineageSink = ClassUtils.loadClass(sinkClassName);
+ this.lineageSink = ClassUtils.loadAndGetInstance(sinkClassName);
lineageSink.initialize(properties);
}
diff --git a/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/iceberg/SparkIcebergCatalogHiveBackendIT.java b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/iceberg/SparkIcebergCatalogHiveBackendIT.java
index 344b538cc5..df4a1d7b90 100644
--- a/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/iceberg/SparkIcebergCatalogHiveBackendIT.java
+++ b/spark-connector/spark-common/src/test/java/org/apache/gravitino/spark/connector/integration/test/iceberg/SparkIcebergCatalogHiveBackendIT.java
@@ -20,6 +20,7 @@ package org.apache.gravitino.spark.connector.integration.test.iceberg;
import com.google.common.collect.Maps;
import java.util.Map;
+import org.apache.gravitino.catalog.lakehouse.iceberg.IcebergConstants;
import org.apache.gravitino.spark.connector.iceberg.IcebergPropertiesConstants;
import org.junit.jupiter.api.Tag;
@@ -37,6 +38,10 @@ public abstract class SparkIcebergCatalogHiveBackendIT extends SparkIcebergCatal
IcebergPropertiesConstants.GRAVITINO_ICEBERG_CATALOG_WAREHOUSE, warehouse);
catalogProperties.put(
IcebergPropertiesConstants.GRAVITINO_ICEBERG_CATALOG_URI, hiveMetastoreUri);
+ catalogProperties.put(
+ IcebergConstants.TABLE_METADATA_CACHE_IMPL,
+ "org.apache.gravitino.iceberg.common.cache.LocalTableMetadataCache");
+
return catalogProperties;
}
}