ajantha-bhat commented on a change in pull request #3908: URL: https://github.com/apache/carbondata/pull/3908#discussion_r486965218
########## File path: core/src/main/java/org/apache/carbondata/core/constants/CarbonCommonConstants.java ########## @@ -1273,6 +1273,13 @@ private CarbonCommonConstants() { @CarbonProperty public static final String CARBON_MAX_DRIVER_LRU_CACHE_SIZE = "carbon.max.driver.lru.cache.size"; + /** + * max driver lru cache size upto which partition lru cache will be loaded in memory + */ + @CarbonProperty Review comment: please also need to update the document ########## File path: core/src/main/java/org/apache/carbondata/core/constants/CarbonCommonConstants.java ########## @@ -1273,6 +1273,13 @@ private CarbonCommonConstants() { @CarbonProperty public static final String CARBON_MAX_DRIVER_LRU_CACHE_SIZE = "carbon.max.driver.lru.cache.size"; + /** + * max driver lru cache size upto which partition lru cache will be loaded in memory + */ + @CarbonProperty Review comment: please also update the document ########## File path: integration/spark/src/main/scala/org/apache/spark/util/PartitionCacheManager.scala ########## @@ -0,0 +1,181 @@ +/* + * 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.spark.util + +import java.net.URI +import java.util + +import scala.collection.JavaConverters._ +import scala.collection.mutable + +import org.apache.log4j.Logger +import org.apache.spark.sql.catalyst.catalog.{CatalogStorageFormat, CatalogTablePartition} + +import org.apache.carbondata.common.logging.LogServiceFactory +import org.apache.carbondata.core.cache.{Cache, Cacheable, CarbonLRUCache} +import org.apache.carbondata.core.constants.CarbonCommonConstants +import org.apache.carbondata.core.datastore.impl.FileFactory +import org.apache.carbondata.core.index.Segment +import org.apache.carbondata.core.metadata.{AbsoluteTableIdentifier, SegmentFileStore} +import org.apache.carbondata.core.statusmanager.{LoadMetadataDetails, SegmentStatusManager} +import org.apache.carbondata.core.util.path.CarbonTablePath + +object PartitionCacheManager extends Cache[PartitionCacheKey, + java.util.List[CatalogTablePartition]] { + + private val CACHE = new CarbonLRUCache( + CarbonCommonConstants.CARBON_PARTITION_MAX_DRIVER_LRU_CACHE_SIZE, + CarbonCommonConstants.CARBON_MAX_LRU_CACHE_SIZE_DEFAULT) + + val LOGGER: Logger = LogServiceFactory.getLogService(this.getClass.getName) + + def get(identifier: PartitionCacheKey): java.util.List[CatalogTablePartition] = { + LOGGER.info("Reading partition values from store") + // read the tableStatus file to get valid and invalid segments + val validInvalidSegments = new SegmentStatusManager(AbsoluteTableIdentifier.from( + identifier.tablePath, null, null, identifier.tableId)) + .getValidAndInvalidSegments + val cacheablePartitionSpecs = validInvalidSegments.getValidSegments.asScala.map { segment => + val segmentFileName = segment.getSegmentFileName + val segmentFilePath = FileFactory.getCarbonFile( + CarbonTablePath.getSegmentFilePath(identifier.tablePath, segmentFileName)) + // read the last modified time + val segmentFileModifiedTime = segmentFilePath.getLastModifiedTime + val existingCache = CACHE.get(identifier.tableId) + if (existingCache != null) { + val segmentCache = CACHE.get(identifier.tableId).asInstanceOf[CacheablePartitionSpec] + .partitionSpecs.get(segment.getSegmentNo) + segmentCache match { + case Some(c) => + // check if cache needs to be updated + if (segmentFileModifiedTime > c._2) { + (segment.getSegmentNo, (readPartition(identifier, + segmentFilePath.getAbsolutePath), segmentFileModifiedTime)) + } else { + (segment.getSegmentNo, c) + } + case None => + (segment.getSegmentNo, (readPartition(identifier, + segmentFilePath.getAbsolutePath), segmentFileModifiedTime)) + } + } else { + // read the partitions if not available in cache. + (segment.getSegmentNo, (readPartition(identifier, Review comment: readPartition reads the segment file. so basically first time we read all the segment files of the table. I have seen the users with 200K segments in one table stored in cloud. query takes more than 5 hours for them if we do this. So, need to change the key as partition spec and cache only matched partitions @kumarvishal09 , @QiangCai @marchpure : what is your opinion on this ? ########## File path: integration/spark/src/main/scala/org/apache/spark/util/PartitionCacheManager.scala ########## @@ -0,0 +1,181 @@ +/* + * 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.spark.util + +import java.net.URI +import java.util + +import scala.collection.JavaConverters._ +import scala.collection.mutable + +import org.apache.log4j.Logger +import org.apache.spark.sql.catalyst.catalog.{CatalogStorageFormat, CatalogTablePartition} + +import org.apache.carbondata.common.logging.LogServiceFactory +import org.apache.carbondata.core.cache.{Cache, Cacheable, CarbonLRUCache} +import org.apache.carbondata.core.constants.CarbonCommonConstants +import org.apache.carbondata.core.datastore.impl.FileFactory +import org.apache.carbondata.core.index.Segment +import org.apache.carbondata.core.metadata.{AbsoluteTableIdentifier, SegmentFileStore} +import org.apache.carbondata.core.statusmanager.{LoadMetadataDetails, SegmentStatusManager} +import org.apache.carbondata.core.util.path.CarbonTablePath + +object PartitionCacheManager extends Cache[PartitionCacheKey, + java.util.List[CatalogTablePartition]] { + + private val CACHE = new CarbonLRUCache( + CarbonCommonConstants.CARBON_PARTITION_MAX_DRIVER_LRU_CACHE_SIZE, + CarbonCommonConstants.CARBON_MAX_LRU_CACHE_SIZE_DEFAULT) + + val LOGGER: Logger = LogServiceFactory.getLogService(this.getClass.getName) + + def get(identifier: PartitionCacheKey): java.util.List[CatalogTablePartition] = { + LOGGER.info("Reading partition values from store") + // read the tableStatus file to get valid and invalid segments + val validInvalidSegments = new SegmentStatusManager(AbsoluteTableIdentifier.from( + identifier.tablePath, null, null, identifier.tableId)) + .getValidAndInvalidSegments + val cacheablePartitionSpecs = validInvalidSegments.getValidSegments.asScala.map { segment => + val segmentFileName = segment.getSegmentFileName + val segmentFilePath = FileFactory.getCarbonFile( + CarbonTablePath.getSegmentFilePath(identifier.tablePath, segmentFileName)) + // read the last modified time + val segmentFileModifiedTime = segmentFilePath.getLastModifiedTime + val existingCache = CACHE.get(identifier.tableId) + if (existingCache != null) { + val segmentCache = CACHE.get(identifier.tableId).asInstanceOf[CacheablePartitionSpec] + .partitionSpecs.get(segment.getSegmentNo) + segmentCache match { + case Some(c) => + // check if cache needs to be updated + if (segmentFileModifiedTime > c._2) { + (segment.getSegmentNo, (readPartition(identifier, + segmentFilePath.getAbsolutePath), segmentFileModifiedTime)) + } else { + (segment.getSegmentNo, c) + } + case None => + (segment.getSegmentNo, (readPartition(identifier, + segmentFilePath.getAbsolutePath), segmentFileModifiedTime)) + } + } else { + // read the partitions if not available in cache. + (segment.getSegmentNo, (readPartition(identifier, Review comment: readPartition reads the segment file. so basically first time we read all the segment files of the table. I have seen the users with 200K segments in one table stored in cloud. query takes more than 5 hours for them if we do this. **So, need to change the key as partition spec and cache only matched partitions** @kumarvishal09 , @QiangCai @marchpure : what is your opinion on this ? ########## File path: integration/spark/src/main/scala/org/apache/spark/util/PartitionCacheManager.scala ########## @@ -0,0 +1,181 @@ +/* + * 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.spark.util + +import java.net.URI +import java.util + +import scala.collection.JavaConverters._ +import scala.collection.mutable + +import org.apache.log4j.Logger +import org.apache.spark.sql.catalyst.catalog.{CatalogStorageFormat, CatalogTablePartition} + +import org.apache.carbondata.common.logging.LogServiceFactory +import org.apache.carbondata.core.cache.{Cache, Cacheable, CarbonLRUCache} +import org.apache.carbondata.core.constants.CarbonCommonConstants +import org.apache.carbondata.core.datastore.impl.FileFactory +import org.apache.carbondata.core.index.Segment +import org.apache.carbondata.core.metadata.{AbsoluteTableIdentifier, SegmentFileStore} +import org.apache.carbondata.core.statusmanager.{LoadMetadataDetails, SegmentStatusManager} +import org.apache.carbondata.core.util.path.CarbonTablePath + +object PartitionCacheManager extends Cache[PartitionCacheKey, + java.util.List[CatalogTablePartition]] { + + private val CACHE = new CarbonLRUCache( + CarbonCommonConstants.CARBON_PARTITION_MAX_DRIVER_LRU_CACHE_SIZE, + CarbonCommonConstants.CARBON_MAX_LRU_CACHE_SIZE_DEFAULT) + + val LOGGER: Logger = LogServiceFactory.getLogService(this.getClass.getName) + + def get(identifier: PartitionCacheKey): java.util.List[CatalogTablePartition] = { + LOGGER.info("Reading partition values from store") + // read the tableStatus file to get valid and invalid segments + val validInvalidSegments = new SegmentStatusManager(AbsoluteTableIdentifier.from( + identifier.tablePath, null, null, identifier.tableId)) + .getValidAndInvalidSegments + val cacheablePartitionSpecs = validInvalidSegments.getValidSegments.asScala.map { segment => + val segmentFileName = segment.getSegmentFileName + val segmentFilePath = FileFactory.getCarbonFile( + CarbonTablePath.getSegmentFilePath(identifier.tablePath, segmentFileName)) + // read the last modified time + val segmentFileModifiedTime = segmentFilePath.getLastModifiedTime + val existingCache = CACHE.get(identifier.tableId) + if (existingCache != null) { + val segmentCache = CACHE.get(identifier.tableId).asInstanceOf[CacheablePartitionSpec] + .partitionSpecs.get(segment.getSegmentNo) + segmentCache match { + case Some(c) => + // check if cache needs to be updated + if (segmentFileModifiedTime > c._2) { + (segment.getSegmentNo, (readPartition(identifier, + segmentFilePath.getAbsolutePath), segmentFileModifiedTime)) + } else { + (segment.getSegmentNo, c) + } + case None => + (segment.getSegmentNo, (readPartition(identifier, + segmentFilePath.getAbsolutePath), segmentFileModifiedTime)) + } + } else { + // read the partitions if not available in cache. + (segment.getSegmentNo, (readPartition(identifier, Review comment: readPartition reads the segment file. so basically first time we read all the segment files of the table. I have seen the users with 200K segments in one table stored in cloud. query takes more than 5 hours for them if we do this. **So, the current design fails. need to change the key as partition spec and cache only matched partitions** @kumarvishal09 , @QiangCai @marchpure : what is your opinion on this ? ########## File path: integration/spark/src/main/scala/org/apache/spark/util/PartitionCacheManager.scala ########## @@ -0,0 +1,181 @@ +/* + * 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.spark.util + +import java.net.URI +import java.util + +import scala.collection.JavaConverters._ +import scala.collection.mutable + +import org.apache.log4j.Logger +import org.apache.spark.sql.catalyst.catalog.{CatalogStorageFormat, CatalogTablePartition} + +import org.apache.carbondata.common.logging.LogServiceFactory +import org.apache.carbondata.core.cache.{Cache, Cacheable, CarbonLRUCache} +import org.apache.carbondata.core.constants.CarbonCommonConstants +import org.apache.carbondata.core.datastore.impl.FileFactory +import org.apache.carbondata.core.index.Segment +import org.apache.carbondata.core.metadata.{AbsoluteTableIdentifier, SegmentFileStore} +import org.apache.carbondata.core.statusmanager.{LoadMetadataDetails, SegmentStatusManager} +import org.apache.carbondata.core.util.path.CarbonTablePath + +object PartitionCacheManager extends Cache[PartitionCacheKey, + java.util.List[CatalogTablePartition]] { + + private val CACHE = new CarbonLRUCache( + CarbonCommonConstants.CARBON_PARTITION_MAX_DRIVER_LRU_CACHE_SIZE, + CarbonCommonConstants.CARBON_MAX_LRU_CACHE_SIZE_DEFAULT) + + val LOGGER: Logger = LogServiceFactory.getLogService(this.getClass.getName) + + def get(identifier: PartitionCacheKey): java.util.List[CatalogTablePartition] = { + LOGGER.info("Reading partition values from store") + // read the tableStatus file to get valid and invalid segments + val validInvalidSegments = new SegmentStatusManager(AbsoluteTableIdentifier.from( + identifier.tablePath, null, null, identifier.tableId)) + .getValidAndInvalidSegments + val cacheablePartitionSpecs = validInvalidSegments.getValidSegments.asScala.map { segment => + val segmentFileName = segment.getSegmentFileName + val segmentFilePath = FileFactory.getCarbonFile( + CarbonTablePath.getSegmentFilePath(identifier.tablePath, segmentFileName)) + // read the last modified time + val segmentFileModifiedTime = segmentFilePath.getLastModifiedTime + val existingCache = CACHE.get(identifier.tableId) + if (existingCache != null) { + val segmentCache = CACHE.get(identifier.tableId).asInstanceOf[CacheablePartitionSpec] + .partitionSpecs.get(segment.getSegmentNo) + segmentCache match { + case Some(c) => + // check if cache needs to be updated + if (segmentFileModifiedTime > c._2) { + (segment.getSegmentNo, (readPartition(identifier, + segmentFilePath.getAbsolutePath), segmentFileModifiedTime)) + } else { + (segment.getSegmentNo, c) + } + case None => + (segment.getSegmentNo, (readPartition(identifier, + segmentFilePath.getAbsolutePath), segmentFileModifiedTime)) + } + } else { + // read the partitions if not available in cache. + (segment.getSegmentNo, (readPartition(identifier, Review comment: @akashrn5 : yeah, my problem scenario is first time query or upgrade, taking huge time (near to 5 hours for first time query) is still not ok. In this scenario, I feel reading from hive metastore itself is very fast compared to number of IO from the cloud storage ########## File path: integration/spark/src/main/scala/org/apache/spark/util/PartitionCacheManager.scala ########## @@ -0,0 +1,181 @@ +/* + * 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.spark.util + +import java.net.URI +import java.util + +import scala.collection.JavaConverters._ +import scala.collection.mutable + +import org.apache.log4j.Logger +import org.apache.spark.sql.catalyst.catalog.{CatalogStorageFormat, CatalogTablePartition} + +import org.apache.carbondata.common.logging.LogServiceFactory +import org.apache.carbondata.core.cache.{Cache, Cacheable, CarbonLRUCache} +import org.apache.carbondata.core.constants.CarbonCommonConstants +import org.apache.carbondata.core.datastore.impl.FileFactory +import org.apache.carbondata.core.index.Segment +import org.apache.carbondata.core.metadata.{AbsoluteTableIdentifier, SegmentFileStore} +import org.apache.carbondata.core.statusmanager.{LoadMetadataDetails, SegmentStatusManager} +import org.apache.carbondata.core.util.path.CarbonTablePath + +object PartitionCacheManager extends Cache[PartitionCacheKey, + java.util.List[CatalogTablePartition]] { + + private val CACHE = new CarbonLRUCache( + CarbonCommonConstants.CARBON_PARTITION_MAX_DRIVER_LRU_CACHE_SIZE, + CarbonCommonConstants.CARBON_MAX_LRU_CACHE_SIZE_DEFAULT) + + val LOGGER: Logger = LogServiceFactory.getLogService(this.getClass.getName) + + def get(identifier: PartitionCacheKey): java.util.List[CatalogTablePartition] = { + LOGGER.info("Reading partition values from store") + // read the tableStatus file to get valid and invalid segments + val validInvalidSegments = new SegmentStatusManager(AbsoluteTableIdentifier.from( + identifier.tablePath, null, null, identifier.tableId)) + .getValidAndInvalidSegments + val cacheablePartitionSpecs = validInvalidSegments.getValidSegments.asScala.map { segment => + val segmentFileName = segment.getSegmentFileName + val segmentFilePath = FileFactory.getCarbonFile( + CarbonTablePath.getSegmentFilePath(identifier.tablePath, segmentFileName)) + // read the last modified time + val segmentFileModifiedTime = segmentFilePath.getLastModifiedTime + val existingCache = CACHE.get(identifier.tableId) + if (existingCache != null) { + val segmentCache = CACHE.get(identifier.tableId).asInstanceOf[CacheablePartitionSpec] + .partitionSpecs.get(segment.getSegmentNo) + segmentCache match { + case Some(c) => + // check if cache needs to be updated + if (segmentFileModifiedTime > c._2) { + (segment.getSegmentNo, (readPartition(identifier, + segmentFilePath.getAbsolutePath), segmentFileModifiedTime)) + } else { + (segment.getSegmentNo, c) + } + case None => + (segment.getSegmentNo, (readPartition(identifier, + segmentFilePath.getAbsolutePath), segmentFileModifiedTime)) + } + } else { + // read the partitions if not available in cache. + (segment.getSegmentNo, (readPartition(identifier, Review comment: ok. If the user feels hive metastore getPartitions is slow, still he cannot enable this in cloud scenario if he had the huge segments as it will be slower than hive metastore. As the feature is disabled by default. Ok for me. ########## File path: integration/spark/src/main/scala/org/apache/spark/util/PartitionCacheManager.scala ########## @@ -0,0 +1,181 @@ +/* + * 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.spark.util + +import java.net.URI +import java.util + +import scala.collection.JavaConverters._ +import scala.collection.mutable + +import org.apache.log4j.Logger +import org.apache.spark.sql.catalyst.catalog.{CatalogStorageFormat, CatalogTablePartition} + +import org.apache.carbondata.common.logging.LogServiceFactory +import org.apache.carbondata.core.cache.{Cache, Cacheable, CarbonLRUCache} +import org.apache.carbondata.core.constants.CarbonCommonConstants +import org.apache.carbondata.core.datastore.impl.FileFactory +import org.apache.carbondata.core.index.Segment +import org.apache.carbondata.core.metadata.{AbsoluteTableIdentifier, SegmentFileStore} +import org.apache.carbondata.core.statusmanager.{LoadMetadataDetails, SegmentStatusManager} +import org.apache.carbondata.core.util.path.CarbonTablePath + +object PartitionCacheManager extends Cache[PartitionCacheKey, + java.util.List[CatalogTablePartition]] { + + private val CACHE = new CarbonLRUCache( + CarbonCommonConstants.CARBON_PARTITION_MAX_DRIVER_LRU_CACHE_SIZE, + CarbonCommonConstants.CARBON_MAX_LRU_CACHE_SIZE_DEFAULT) + + val LOGGER: Logger = LogServiceFactory.getLogService(this.getClass.getName) + + def get(identifier: PartitionCacheKey): java.util.List[CatalogTablePartition] = { + LOGGER.info("Reading partition values from store") + // read the tableStatus file to get valid and invalid segments + val validInvalidSegments = new SegmentStatusManager(AbsoluteTableIdentifier.from( + identifier.tablePath, null, null, identifier.tableId)) + .getValidAndInvalidSegments + val cacheablePartitionSpecs = validInvalidSegments.getValidSegments.asScala.map { segment => + val segmentFileName = segment.getSegmentFileName + val segmentFilePath = FileFactory.getCarbonFile( + CarbonTablePath.getSegmentFilePath(identifier.tablePath, segmentFileName)) + // read the last modified time + val segmentFileModifiedTime = segmentFilePath.getLastModifiedTime + val existingCache = CACHE.get(identifier.tableId) + if (existingCache != null) { + val segmentCache = CACHE.get(identifier.tableId).asInstanceOf[CacheablePartitionSpec] + .partitionSpecs.get(segment.getSegmentNo) + segmentCache match { + case Some(c) => + // check if cache needs to be updated + if (segmentFileModifiedTime > c._2) { + (segment.getSegmentNo, (readPartition(identifier, + segmentFilePath.getAbsolutePath), segmentFileModifiedTime)) + } else { + (segment.getSegmentNo, c) + } + case None => + (segment.getSegmentNo, (readPartition(identifier, + segmentFilePath.getAbsolutePath), segmentFileModifiedTime)) + } + } else { + // read the partitions if not available in cache. + (segment.getSegmentNo, (readPartition(identifier, Review comment: ok. If the user feels hive metastore getPartitions is slow, still he cannot enable this in cloud scenario if he had the huge number of segments (as IO is very slow) as it will be slower than hive metastore. As the feature is disabled by default. Ok for me. ########## File path: integration/spark/src/main/scala/org/apache/spark/util/PartitionCacheManager.scala ########## @@ -0,0 +1,181 @@ +/* + * 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.spark.util + +import java.net.URI +import java.util + +import scala.collection.JavaConverters._ +import scala.collection.mutable + +import org.apache.log4j.Logger +import org.apache.spark.sql.catalyst.catalog.{CatalogStorageFormat, CatalogTablePartition} + +import org.apache.carbondata.common.logging.LogServiceFactory +import org.apache.carbondata.core.cache.{Cache, Cacheable, CarbonLRUCache} +import org.apache.carbondata.core.constants.CarbonCommonConstants +import org.apache.carbondata.core.datastore.impl.FileFactory +import org.apache.carbondata.core.index.Segment +import org.apache.carbondata.core.metadata.{AbsoluteTableIdentifier, SegmentFileStore} +import org.apache.carbondata.core.statusmanager.{LoadMetadataDetails, SegmentStatusManager} +import org.apache.carbondata.core.util.path.CarbonTablePath + +object PartitionCacheManager extends Cache[PartitionCacheKey, + java.util.List[CatalogTablePartition]] { + + private val CACHE = new CarbonLRUCache( + CarbonCommonConstants.CARBON_PARTITION_MAX_DRIVER_LRU_CACHE_SIZE, + CarbonCommonConstants.CARBON_MAX_LRU_CACHE_SIZE_DEFAULT) + + val LOGGER: Logger = LogServiceFactory.getLogService(this.getClass.getName) + + def get(identifier: PartitionCacheKey): java.util.List[CatalogTablePartition] = { + LOGGER.info("Reading partition values from store") + // read the tableStatus file to get valid and invalid segments + val validInvalidSegments = new SegmentStatusManager(AbsoluteTableIdentifier.from( + identifier.tablePath, null, null, identifier.tableId)) + .getValidAndInvalidSegments + val cacheablePartitionSpecs = validInvalidSegments.getValidSegments.asScala.map { segment => + val segmentFileName = segment.getSegmentFileName + val segmentFilePath = FileFactory.getCarbonFile( + CarbonTablePath.getSegmentFilePath(identifier.tablePath, segmentFileName)) + // read the last modified time + val segmentFileModifiedTime = segmentFilePath.getLastModifiedTime + val existingCache = CACHE.get(identifier.tableId) + if (existingCache != null) { + val segmentCache = CACHE.get(identifier.tableId).asInstanceOf[CacheablePartitionSpec] + .partitionSpecs.get(segment.getSegmentNo) + segmentCache match { + case Some(c) => + // check if cache needs to be updated + if (segmentFileModifiedTime > c._2) { + (segment.getSegmentNo, (readPartition(identifier, + segmentFilePath.getAbsolutePath), segmentFileModifiedTime)) + } else { + (segment.getSegmentNo, c) + } + case None => + (segment.getSegmentNo, (readPartition(identifier, + segmentFilePath.getAbsolutePath), segmentFileModifiedTime)) + } + } else { + // read the partitions if not available in cache. + (segment.getSegmentNo, (readPartition(identifier, Review comment: readPartition reads the segment file. so basically first time we read all the segment files of the table. I have seen the users with 200K segments in one table stored in cloud. query takes more than 5 hours for them if we do this. So, the current design fails for huge segments. need to change the key as partition spec or cache only matched partitions from hive metastore read @kumarvishal09 , @QiangCai @marchpure : what is your opinion on this ? ########## File path: integration/spark/src/main/scala/org/apache/spark/util/PartitionCacheManager.scala ########## @@ -0,0 +1,181 @@ +/* + * 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.spark.util + +import java.net.URI +import java.util + +import scala.collection.JavaConverters._ +import scala.collection.mutable + +import org.apache.log4j.Logger +import org.apache.spark.sql.catalyst.catalog.{CatalogStorageFormat, CatalogTablePartition} + +import org.apache.carbondata.common.logging.LogServiceFactory +import org.apache.carbondata.core.cache.{Cache, Cacheable, CarbonLRUCache} +import org.apache.carbondata.core.constants.CarbonCommonConstants +import org.apache.carbondata.core.datastore.impl.FileFactory +import org.apache.carbondata.core.index.Segment +import org.apache.carbondata.core.metadata.{AbsoluteTableIdentifier, SegmentFileStore} +import org.apache.carbondata.core.statusmanager.{LoadMetadataDetails, SegmentStatusManager} +import org.apache.carbondata.core.util.path.CarbonTablePath + +object PartitionCacheManager extends Cache[PartitionCacheKey, + java.util.List[CatalogTablePartition]] { + + private val CACHE = new CarbonLRUCache( + CarbonCommonConstants.CARBON_PARTITION_MAX_DRIVER_LRU_CACHE_SIZE, + CarbonCommonConstants.CARBON_MAX_LRU_CACHE_SIZE_DEFAULT) + + val LOGGER: Logger = LogServiceFactory.getLogService(this.getClass.getName) + + def get(identifier: PartitionCacheKey): java.util.List[CatalogTablePartition] = { + LOGGER.info("Reading partition values from store") + // read the tableStatus file to get valid and invalid segments + val validInvalidSegments = new SegmentStatusManager(AbsoluteTableIdentifier.from( + identifier.tablePath, null, null, identifier.tableId)) + .getValidAndInvalidSegments + val cacheablePartitionSpecs = validInvalidSegments.getValidSegments.asScala.map { segment => + val segmentFileName = segment.getSegmentFileName + val segmentFilePath = FileFactory.getCarbonFile( + CarbonTablePath.getSegmentFilePath(identifier.tablePath, segmentFileName)) + // read the last modified time + val segmentFileModifiedTime = segmentFilePath.getLastModifiedTime + val existingCache = CACHE.get(identifier.tableId) + if (existingCache != null) { + val segmentCache = CACHE.get(identifier.tableId).asInstanceOf[CacheablePartitionSpec] + .partitionSpecs.get(segment.getSegmentNo) + segmentCache match { + case Some(c) => + // check if cache needs to be updated + if (segmentFileModifiedTime > c._2) { + (segment.getSegmentNo, (readPartition(identifier, + segmentFilePath.getAbsolutePath), segmentFileModifiedTime)) + } else { + (segment.getSegmentNo, c) + } + case None => + (segment.getSegmentNo, (readPartition(identifier, + segmentFilePath.getAbsolutePath), segmentFileModifiedTime)) + } + } else { + // read the partitions if not available in cache. + (segment.getSegmentNo, (readPartition(identifier, Review comment: ok. If the user feels hive metastore getPartitions is slow, still he cannot enable this in cloud scenario if he had the huge number of segments (as IO is very slow) as it will be slower than hive metastore. Also if the hive metastore supports filter pushdown, they can prune the partitions faster and give us the results using some metadata. (this might already be there) instead of we pruning manually by doing IO of all segment file first. **As the feature is disabled by default. Ok for me.** ########## File path: integration/spark/src/main/scala/org/apache/spark/util/PartitionCacheManager.scala ########## @@ -0,0 +1,181 @@ +/* + * 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.spark.util + +import java.net.URI +import java.util + +import scala.collection.JavaConverters._ +import scala.collection.mutable + +import org.apache.log4j.Logger +import org.apache.spark.sql.catalyst.catalog.{CatalogStorageFormat, CatalogTablePartition} + +import org.apache.carbondata.common.logging.LogServiceFactory +import org.apache.carbondata.core.cache.{Cache, Cacheable, CarbonLRUCache} +import org.apache.carbondata.core.constants.CarbonCommonConstants +import org.apache.carbondata.core.datastore.impl.FileFactory +import org.apache.carbondata.core.index.Segment +import org.apache.carbondata.core.metadata.{AbsoluteTableIdentifier, SegmentFileStore} +import org.apache.carbondata.core.statusmanager.{LoadMetadataDetails, SegmentStatusManager} +import org.apache.carbondata.core.util.path.CarbonTablePath + +object PartitionCacheManager extends Cache[PartitionCacheKey, + java.util.List[CatalogTablePartition]] { + + private val CACHE = new CarbonLRUCache( + CarbonCommonConstants.CARBON_PARTITION_MAX_DRIVER_LRU_CACHE_SIZE, + CarbonCommonConstants.CARBON_MAX_LRU_CACHE_SIZE_DEFAULT) + + val LOGGER: Logger = LogServiceFactory.getLogService(this.getClass.getName) + + def get(identifier: PartitionCacheKey): java.util.List[CatalogTablePartition] = { + LOGGER.info("Reading partition values from store") + // read the tableStatus file to get valid and invalid segments + val validInvalidSegments = new SegmentStatusManager(AbsoluteTableIdentifier.from( + identifier.tablePath, null, null, identifier.tableId)) + .getValidAndInvalidSegments + val cacheablePartitionSpecs = validInvalidSegments.getValidSegments.asScala.map { segment => + val segmentFileName = segment.getSegmentFileName + val segmentFilePath = FileFactory.getCarbonFile( + CarbonTablePath.getSegmentFilePath(identifier.tablePath, segmentFileName)) + // read the last modified time + val segmentFileModifiedTime = segmentFilePath.getLastModifiedTime + val existingCache = CACHE.get(identifier.tableId) + if (existingCache != null) { + val segmentCache = CACHE.get(identifier.tableId).asInstanceOf[CacheablePartitionSpec] + .partitionSpecs.get(segment.getSegmentNo) + segmentCache match { + case Some(c) => + // check if cache needs to be updated + if (segmentFileModifiedTime > c._2) { + (segment.getSegmentNo, (readPartition(identifier, + segmentFilePath.getAbsolutePath), segmentFileModifiedTime)) + } else { + (segment.getSegmentNo, c) + } + case None => + (segment.getSegmentNo, (readPartition(identifier, + segmentFilePath.getAbsolutePath), segmentFileModifiedTime)) + } + } else { + // read the partitions if not available in cache. + (segment.getSegmentNo, (readPartition(identifier, Review comment: we have users with 200k segments. But the data is partitioned by date and time. so all queries will have partition columns filter. so they query-specific segments every time (without any degrade in query time) ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: us...@infra.apache.org