snazy commented on code in PR #433: URL: https://github.com/apache/polaris/pull/433#discussion_r1852323946
########## polaris-service/src/main/java/org/apache/polaris/service/persistence/MetadataCacheManager.java: ########## @@ -0,0 +1,143 @@ +/* + * 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.polaris.service.persistence; + +import java.nio.charset.StandardCharsets; +import java.util.function.Supplier; +import org.apache.iceberg.Table; +import org.apache.iceberg.TableMetadata; +import org.apache.iceberg.TableMetadataParser; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.polaris.core.PolarisCallContext; +import org.apache.polaris.core.PolarisConfiguration; +import org.apache.polaris.core.entity.PolarisEntity; +import org.apache.polaris.core.entity.PolarisEntitySubType; +import org.apache.polaris.core.entity.TableLikeEntity; +import org.apache.polaris.core.persistence.PolarisMetaStoreManager; +import org.apache.polaris.core.persistence.PolarisResolvedPathWrapper; +import org.apache.polaris.core.persistence.resolver.PolarisResolutionManifestCatalogView; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class MetadataCacheManager { Review Comment: This is rather a collection of static utility methods than a "cache manager"? Maybe give it a better name. BTW: If it only has utility methods, add a private no-arg constructor. ########## polaris-service/src/main/java/org/apache/polaris/service/persistence/MetadataCacheManager.java: ########## @@ -0,0 +1,143 @@ +/* + * 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.polaris.service.persistence; + +import java.nio.charset.StandardCharsets; +import java.util.function.Supplier; +import org.apache.iceberg.Table; +import org.apache.iceberg.TableMetadata; +import org.apache.iceberg.TableMetadataParser; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.polaris.core.PolarisCallContext; +import org.apache.polaris.core.PolarisConfiguration; +import org.apache.polaris.core.entity.PolarisEntity; +import org.apache.polaris.core.entity.PolarisEntitySubType; +import org.apache.polaris.core.entity.TableLikeEntity; +import org.apache.polaris.core.persistence.PolarisMetaStoreManager; +import org.apache.polaris.core.persistence.PolarisResolvedPathWrapper; +import org.apache.polaris.core.persistence.resolver.PolarisResolutionManifestCatalogView; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class MetadataCacheManager { + private static final Logger LOGGER = LoggerFactory.getLogger(MetadataCacheManager.class); + + /** + * Load the cached {@link Table} or fall back to `fallback` if one doesn't exist. If the metadata + * is not currently cached, it may be added to the cache. + */ + public static TableMetadata loadTableMetadata( + TableIdentifier tableIdentifier, + long maxBytesToCache, + PolarisCallContext callContext, + PolarisMetaStoreManager metastoreManager, + PolarisResolutionManifestCatalogView resolvedEntityView, + Supplier<TableMetadata> fallback) { + LOGGER.debug(String.format("Loading cached metadata for %s", tableIdentifier)); + PolarisResolvedPathWrapper resolvedEntities = + resolvedEntityView.getResolvedPath(tableIdentifier, PolarisEntitySubType.TABLE); + TableLikeEntity tableLikeEntity = TableLikeEntity.of(resolvedEntities.getRawLeafEntity()); + boolean isCacheValid = + tableLikeEntity.getMetadataCacheLocation() != null + && tableLikeEntity + .getMetadataLocation() + .equals(tableLikeEntity.getMetadataCacheLocation()); Review Comment: When can this inconsistency be triggered? Shouldn't updates to that entity be consistent and not leave dirty state behind? ########## polaris-service/src/main/java/org/apache/polaris/service/persistence/MetadataCacheManager.java: ########## @@ -0,0 +1,143 @@ +/* + * 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.polaris.service.persistence; + +import java.nio.charset.StandardCharsets; +import java.util.function.Supplier; +import org.apache.iceberg.Table; +import org.apache.iceberg.TableMetadata; +import org.apache.iceberg.TableMetadataParser; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.polaris.core.PolarisCallContext; +import org.apache.polaris.core.PolarisConfiguration; +import org.apache.polaris.core.entity.PolarisEntity; +import org.apache.polaris.core.entity.PolarisEntitySubType; +import org.apache.polaris.core.entity.TableLikeEntity; +import org.apache.polaris.core.persistence.PolarisMetaStoreManager; +import org.apache.polaris.core.persistence.PolarisResolvedPathWrapper; +import org.apache.polaris.core.persistence.resolver.PolarisResolutionManifestCatalogView; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class MetadataCacheManager { + private static final Logger LOGGER = LoggerFactory.getLogger(MetadataCacheManager.class); + + /** + * Load the cached {@link Table} or fall back to `fallback` if one doesn't exist. If the metadata + * is not currently cached, it may be added to the cache. + */ + public static TableMetadata loadTableMetadata( + TableIdentifier tableIdentifier, + long maxBytesToCache, + PolarisCallContext callContext, + PolarisMetaStoreManager metastoreManager, + PolarisResolutionManifestCatalogView resolvedEntityView, + Supplier<TableMetadata> fallback) { + LOGGER.debug(String.format("Loading cached metadata for %s", tableIdentifier)); + PolarisResolvedPathWrapper resolvedEntities = + resolvedEntityView.getResolvedPath(tableIdentifier, PolarisEntitySubType.TABLE); + TableLikeEntity tableLikeEntity = TableLikeEntity.of(resolvedEntities.getRawLeafEntity()); + boolean isCacheValid = + tableLikeEntity.getMetadataCacheLocation() != null + && tableLikeEntity + .getMetadataLocation() + .equals(tableLikeEntity.getMetadataCacheLocation()); + if (isCacheValid) { + LOGGER.debug(String.format("Using cached metadata for %s", tableIdentifier)); + return TableMetadataParser.fromJson(tableLikeEntity.getMetadataCacheContent()); + } else { + TableMetadata metadata = fallback.get(); + PolarisMetaStoreManager.EntityResult cacheResult = + cacheTableMetadata( + tableLikeEntity, + metadata, + maxBytesToCache, + callContext, + metastoreManager, + resolvedEntityView); + if (!cacheResult.isSuccess()) { + LOGGER.debug(String.format("Failed to cache metadata for %s", tableIdentifier)); + } + return metadata; + } + } + + /** + * Attempt to add table metadata to the cache + * + * @return The result of trying to cache the metadata + */ + private static PolarisMetaStoreManager.EntityResult cacheTableMetadata( + TableLikeEntity tableLikeEntity, + TableMetadata metadata, + long maxBytesToCache, + PolarisCallContext callContext, + PolarisMetaStoreManager metaStoreManager, + PolarisResolutionManifestCatalogView resolvedEntityView) { + String json = TableMetadataParser.toJson(metadata); Review Comment: TBH, this requires a lot of duplicate expensive operations: 1. it loads the metadata JSON from the object store 2. parses JSON into a TableMetadata 3. serializes the TableMetadata to the same string representation as in step 1 4. converts the `String` into a `byte[]` representation - just to check the length The latter steps are likely performed for every access to a table-metadata that exceeds the METADATA_CACHE_MAX_BYTES_NO_CACHING setting, adding a _lot_ of new heap pressure. ########## polaris-service/src/main/java/org/apache/polaris/service/persistence/MetadataCacheManager.java: ########## @@ -0,0 +1,143 @@ +/* + * 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.polaris.service.persistence; + +import java.nio.charset.StandardCharsets; +import java.util.function.Supplier; +import org.apache.iceberg.Table; +import org.apache.iceberg.TableMetadata; +import org.apache.iceberg.TableMetadataParser; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.polaris.core.PolarisCallContext; +import org.apache.polaris.core.PolarisConfiguration; +import org.apache.polaris.core.entity.PolarisEntity; +import org.apache.polaris.core.entity.PolarisEntitySubType; +import org.apache.polaris.core.entity.TableLikeEntity; +import org.apache.polaris.core.persistence.PolarisMetaStoreManager; +import org.apache.polaris.core.persistence.PolarisResolvedPathWrapper; +import org.apache.polaris.core.persistence.resolver.PolarisResolutionManifestCatalogView; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class MetadataCacheManager { + private static final Logger LOGGER = LoggerFactory.getLogger(MetadataCacheManager.class); + + /** + * Load the cached {@link Table} or fall back to `fallback` if one doesn't exist. If the metadata + * is not currently cached, it may be added to the cache. + */ + public static TableMetadata loadTableMetadata( Review Comment: What's the reason why view metadata isn't handled as well? ########## polaris-service/src/main/java/org/apache/polaris/service/persistence/MetadataCacheManager.java: ########## @@ -0,0 +1,143 @@ +/* + * 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.polaris.service.persistence; + +import java.nio.charset.StandardCharsets; +import java.util.function.Supplier; +import org.apache.iceberg.Table; +import org.apache.iceberg.TableMetadata; +import org.apache.iceberg.TableMetadataParser; +import org.apache.iceberg.catalog.TableIdentifier; +import org.apache.polaris.core.PolarisCallContext; +import org.apache.polaris.core.PolarisConfiguration; +import org.apache.polaris.core.entity.PolarisEntity; +import org.apache.polaris.core.entity.PolarisEntitySubType; +import org.apache.polaris.core.entity.TableLikeEntity; +import org.apache.polaris.core.persistence.PolarisMetaStoreManager; +import org.apache.polaris.core.persistence.PolarisResolvedPathWrapper; +import org.apache.polaris.core.persistence.resolver.PolarisResolutionManifestCatalogView; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +public class MetadataCacheManager { + private static final Logger LOGGER = LoggerFactory.getLogger(MetadataCacheManager.class); + + /** + * Load the cached {@link Table} or fall back to `fallback` if one doesn't exist. If the metadata + * is not currently cached, it may be added to the cache. + */ + public static TableMetadata loadTableMetadata( + TableIdentifier tableIdentifier, + long maxBytesToCache, + PolarisCallContext callContext, + PolarisMetaStoreManager metastoreManager, + PolarisResolutionManifestCatalogView resolvedEntityView, + Supplier<TableMetadata> fallback) { + LOGGER.debug(String.format("Loading cached metadata for %s", tableIdentifier)); + PolarisResolvedPathWrapper resolvedEntities = + resolvedEntityView.getResolvedPath(tableIdentifier, PolarisEntitySubType.TABLE); + TableLikeEntity tableLikeEntity = TableLikeEntity.of(resolvedEntities.getRawLeafEntity()); + boolean isCacheValid = + tableLikeEntity.getMetadataCacheLocation() != null + && tableLikeEntity + .getMetadataLocation() + .equals(tableLikeEntity.getMetadataCacheLocation()); + if (isCacheValid) { + LOGGER.debug(String.format("Using cached metadata for %s", tableIdentifier)); + return TableMetadataParser.fromJson(tableLikeEntity.getMetadataCacheContent()); + } else { + TableMetadata metadata = fallback.get(); + PolarisMetaStoreManager.EntityResult cacheResult = + cacheTableMetadata( + tableLikeEntity, + metadata, + maxBytesToCache, + callContext, + metastoreManager, + resolvedEntityView); + if (!cacheResult.isSuccess()) { + LOGGER.debug(String.format("Failed to cache metadata for %s", tableIdentifier)); + } + return metadata; + } + } + + /** + * Attempt to add table metadata to the cache + * + * @return The result of trying to cache the metadata + */ + private static PolarisMetaStoreManager.EntityResult cacheTableMetadata( + TableLikeEntity tableLikeEntity, + TableMetadata metadata, + long maxBytesToCache, + PolarisCallContext callContext, + PolarisMetaStoreManager metaStoreManager, + PolarisResolutionManifestCatalogView resolvedEntityView) { + String json = TableMetadataParser.toJson(metadata); + // We should not reach this method in this case, but check just in case... + if (maxBytesToCache != PolarisConfiguration.METADATA_CACHE_MAX_BYTES_NO_CACHING) { + long sizeInBytes = json.getBytes(StandardCharsets.UTF_8).length; + if (sizeInBytes > maxBytesToCache) { + LOGGER.debug( + String.format( + "Will not cache metadata for %s; metadata is %d bytes and the limit is %d", + tableLikeEntity.getTableIdentifier(), sizeInBytes, maxBytesToCache)); + return new PolarisMetaStoreManager.EntityResult( + PolarisMetaStoreManager.ReturnStatus.SUCCESS, null); + } else { + LOGGER.debug( + String.format("Caching metadata for %s", tableLikeEntity.getTableIdentifier())); + TableLikeEntity newTableLikeEntity = + new TableLikeEntity.Builder(tableLikeEntity) + .setMetadataContent(tableLikeEntity.getMetadataLocation(), json) Review Comment: Storing JSON uncompressed and as a string is a waste of resources on the network, on heap and in the database. I'd suggest to use Smile serialization format and likely compress that as well. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
