[GitHub] qiuchenjian commented on a change in pull request #3078: [WIP] Support show lru command to list the cache sizes for all tables

classic Classic list List threaded Threaded
1 message Options
Reply | Threaded
Open this post in threaded view
|

[GitHub] qiuchenjian commented on a change in pull request #3078: [WIP] Support show lru command to list the cache sizes for all tables

GitBox
qiuchenjian commented on a change in pull request #3078: [WIP] Support show lru command to list the cache sizes for all tables
URL: https://github.com/apache/carbondata/pull/3078#discussion_r248964249
 
 

 ##########
 File path: integration/spark2/src/main/scala/org/apache/spark/sql/execution/command/lru/CarbonDataShowLRUCommand.scala
 ##########
 @@ -0,0 +1,197 @@
+/*
+ * 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.sql.execution.command.lru
+
+import scala.collection.mutable
+import scala.collection.JavaConverters._
+
+import org.apache.spark.sql.{CarbonEnv, Row, SparkSession}
+import org.apache.spark.sql.catalyst.TableIdentifier
+import org.apache.spark.sql.catalyst.analysis.NoSuchTableException
+import org.apache.spark.sql.catalyst.expressions.{Attribute, AttributeReference}
+import org.apache.spark.sql.execution.command.DataCommand
+import org.apache.spark.sql.types.{LongType, StringType}
+
+import org.apache.carbondata.core.cache.CacheProvider
+import org.apache.carbondata.core.cache.dictionary.AbstractColumnDictionaryInfo
+import org.apache.carbondata.core.constants.CarbonCommonConstants
+import org.apache.carbondata.core.indexstore.BlockletDataMapIndexWrapper
+
+/**
+ * SHOW LRU
+ */
+case class CarbonDataShowLRUCommand(tableIdentifier: Option[TableIdentifier])
+  extends DataCommand {
+
+  override def output: Seq[Attribute] = {
+    Seq(AttributeReference("database", StringType, nullable = false)(),
+      AttributeReference("table", StringType, nullable = false)(),
+      AttributeReference("index size", LongType, nullable = false)(),
+      AttributeReference("dictionary size", LongType, nullable = false)())
+  }
+
+  override protected def opName: String = "SHOW LRU"
+
+  def showLRUOfAllTables(sparkSession: SparkSession): Seq[Row] = {
+    val currentDatabase = sparkSession.sessionState.catalog.getCurrentDatabase
+    val lruCache = CacheProvider.getInstance().getCarbonLRUCache()
+    if (lruCache == null) {
+      Seq(Row("ALL", "ALL", 0L, 0L),
+        Row(currentDatabase, "ALL", 0L, 0L))
+    } else {
+      val tableIdents = sparkSession.sessionState.catalog.listTables(currentDatabase).toArray
+      val dbLocation = CarbonEnv.getDatabaseLocation(currentDatabase, sparkSession)
+      val tempLocation = dbLocation.replace(
+        CarbonCommonConstants.WINDOWS_FILE_SEPARATOR, CarbonCommonConstants.FILE_SEPARATOR)
+      val tablePaths = tableIdents.map { tableIdent =>
+        (tempLocation + CarbonCommonConstants.FILE_SEPARATOR +
+         tableIdent.table + CarbonCommonConstants.FILE_SEPARATOR,
+          tableIdent.database.get + "." + tableIdent.table)
+      }
+
+      val dictIds = tableIdents.flatMap { tableIdent =>
+        CarbonEnv
+          .getCarbonTable(tableIdent)(sparkSession)
+          .getAllDimensions
+          .asScala
+          .filter(_.isGlobalDictionaryEncoding)
+          .toArray
+          .map(dim => (dim.getColumnId, tableIdent.database.get + "." + tableIdent.table))
+      }
+
+      // all databases
+      var (allIndexSize, allDictSize) = (0L, 0L)
+      // current database
+      var (dbIndexSize, dbDictSize) = (0L, 0L)
+      val tableMapIndexSize = mutable.HashMap[String, Long]()
+      val tableMapDictSize = mutable.HashMap[String, Long]()
+      val cacheIterator = lruCache.getLruCacheMap.entrySet().iterator()
+      while (cacheIterator.hasNext) {
+        val entry = cacheIterator.next()
+        val cache = entry.getValue
+        if (cache.isInstanceOf[BlockletDataMapIndexWrapper]) {
+          allIndexSize = allIndexSize + cache.getMemorySize
+          val indexPath = entry.getKey.replace(
+            CarbonCommonConstants.WINDOWS_FILE_SEPARATOR, CarbonCommonConstants.FILE_SEPARATOR)
+          val tablePath = tablePaths.find(path => indexPath.startsWith(path._1))
+          if (tablePath.isDefined) {
+            dbIndexSize = dbIndexSize + cache.getMemorySize
+            val memorySize = tableMapIndexSize.get(tablePath.get._2)
+            if (memorySize.isEmpty) {
+              tableMapIndexSize.put(tablePath.get._2, cache.getMemorySize)
+            } else {
+              tableMapIndexSize.put(tablePath.get._2, memorySize.get + cache.getMemorySize)
+            }
+          }
+        } else if (cache.isInstanceOf[AbstractColumnDictionaryInfo]) {
 
 Review comment:
   Will this pr will support the BloomDataMapCache?

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[hidden email]


With regards,
Apache Git Services