ajantha-bhat commented on a change in pull request #3672: [CRBONDATA-3746] 
Support column chunk cache in reader
URL: https://github.com/apache/carbondata/pull/3672#discussion_r394872157
 
 

 ##########
 File path: 
core/src/main/java/org/apache/carbondata/core/indexstore/columncache/ColumnChunkCache.java
 ##########
 @@ -0,0 +1,168 @@
+/*
+ * 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.carbondata.core.indexstore.columncache;
+
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.carbondata.common.logging.LogServiceFactory;
+import org.apache.carbondata.core.datastore.chunk.AbstractRawColumnChunk;
+import org.apache.carbondata.core.metadata.schema.table.CarbonTable;
+
+import com.google.common.cache.Cache;
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.RemovalListener;
+import com.google.common.cache.RemovalNotification;
+import org.apache.log4j.Logger;
+
+/**
+ * Column Chunk level data cache for table which is enabled for this feature.
+ */
+public class ColumnChunkCache {
+
+  private static Logger LOG =
+      
LogServiceFactory.getLogService(ColumnChunkCache.class.getCanonicalName());
+
+  // A global cache for all tables which is enabled for this feature.
+  // The cached object is raw column chunks including raw binary data, so we 
can
+  // leverage this cache to avoid IO read from underlying storage.
+  private static Map<String, Cache<CacheKey, AbstractRawColumnChunk>> 
cacheForEachTable =
+      new ConcurrentHashMap<>();
+
+  /**
+   * Enable or disable the column chunk cache for specified table, based on 
the whether the
+   * corresponding table property is set
+   */
+  public static void setCacheForTable(CarbonTable carbonTable) {
+    String tableId = carbonTable.getTableId();
+    boolean shouldEnable = carbonTable.isColumnCacheEnabled();
+    boolean alreadyEnabled = cacheForEachTable.containsKey(tableId);
+    if (alreadyEnabled && !shouldEnable) {
+      cacheForEachTable.get(tableId).invalidateAll();
+      cacheForEachTable.remove(tableId);
+    } else if (!alreadyEnabled && shouldEnable) {
+      Cache<CacheKey, AbstractRawColumnChunk> cache = 
createCacheForTable(tableId);
+      cacheForEachTable.put(tableId, cache);
+    }
+  }
+
+  private static Cache<CacheKey, AbstractRawColumnChunk> 
createCacheForTable(String tableId) {
+    return CacheBuilder.newBuilder()
+        // expire after 10 minutes after last accessed
+        .expireAfterAccess(10, TimeUnit.MINUTES)
+        // max number of entries
+        .maximumSize(1000)
+        .removalListener(new RemovalListener<CacheKey, 
AbstractRawColumnChunk>() {
+          @Override
+          public void onRemoval(RemovalNotification<CacheKey, 
AbstractRawColumnChunk> entry) {
+            AbstractRawColumnChunk chunk = entry.getValue();
+            if (chunk != null) {
+              chunk.setCached(false);
+              chunk.freeMemory();
+              if (LOG.isInfoEnabled()) {
+                CacheKey key = entry.getKey();
+                LOG.info(String.format("column cache removed: key %s", 
key.toString()));
+              }
+            }
+          }
+        }).build();
+  }
+
+  /**
+   * Return true if column chunk cache is enabled for specified table
+   */
+  public static boolean isEnabledForTable(String tableId) {
+    return tableId != null && cacheForEachTable.containsKey(tableId);
+  }
+
+  /**
+   * Put column chunk into the cache
+   */
+  public static void put(String tableId, CacheKey key, AbstractRawColumnChunk 
chunk) {
+    if (isEnabledForTable(tableId)) {
+      cacheForEachTable.get(tableId).put(key, chunk);
 
 Review comment:
   Concurrent query performance will be impacted with this as this is a 
system-level cache. concurrent hash map will have a lock.

----------------------------------------------------------------
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:
[email protected]


With regards,
Apache Git Services

Reply via email to