You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@kylin.apache.org by li...@apache.org on 2017/06/11 09:42:32 UTC

kylin git commit: KYLIN-2667 ignore whitespace when caching query

Repository: kylin
Updated Branches:
  refs/heads/2.0.x a9a1c2f53 -> 848df9cad


KYLIN-2667 ignore whitespace when caching query


Project: http://git-wip-us.apache.org/repos/asf/kylin/repo
Commit: http://git-wip-us.apache.org/repos/asf/kylin/commit/848df9ca
Tree: http://git-wip-us.apache.org/repos/asf/kylin/tree/848df9ca
Diff: http://git-wip-us.apache.org/repos/asf/kylin/diff/848df9ca

Branch: refs/heads/2.0.x
Commit: 848df9cad5406e27f6631d92c70cd6464b1250fe
Parents: a9a1c2f
Author: Hongbin Ma <ma...@apache.org>
Authored: Sun Jun 11 17:42:22 2017 +0800
Committer: Hongbin Ma <ma...@apache.org>
Committed: Sun Jun 11 17:42:22 2017 +0800

----------------------------------------------------------------------
 .../org/apache/kylin/rest/request/SQLRequest.java    | 15 +++++++++++++++
 .../org/apache/kylin/rest/service/QueryService.java  | 11 +++++------
 2 files changed, 20 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/kylin/blob/848df9ca/server-base/src/main/java/org/apache/kylin/rest/request/SQLRequest.java
----------------------------------------------------------------------
diff --git a/server-base/src/main/java/org/apache/kylin/rest/request/SQLRequest.java b/server-base/src/main/java/org/apache/kylin/rest/request/SQLRequest.java
index 1896f4f..17ca8e1 100644
--- a/server-base/src/main/java/org/apache/kylin/rest/request/SQLRequest.java
+++ b/server-base/src/main/java/org/apache/kylin/rest/request/SQLRequest.java
@@ -21,6 +21,11 @@ package org.apache.kylin.rest.request;
 import java.io.Serializable;
 import java.util.Map;
 
+import com.google.common.collect.Lists;
+
+/**
+ * if you're adding/removing fields from SQLRequest, take a look at getCacheKey
+ */
 public class SQLRequest implements Serializable {
     protected static final long serialVersionUID = 1L;
 
@@ -33,6 +38,8 @@ public class SQLRequest implements Serializable {
 
     private Map<String, String> backdoorToggles;
 
+    private volatile Object cacheKey = null;
+
     public SQLRequest() {
     }
 
@@ -84,6 +91,14 @@ public class SQLRequest implements Serializable {
         this.acceptPartial = acceptPartial;
     }
 
+    public Object getCacheKey() {
+        if (cacheKey != null)
+            return cacheKey;
+
+        cacheKey = Lists.newArrayList(sql.replaceAll("\\s+", ""), project, offset, limit, acceptPartial, backdoorToggles);
+        return cacheKey;
+    }
+
     @Override
     public boolean equals(Object o) {
         if (this == o)

http://git-wip-us.apache.org/repos/asf/kylin/blob/848df9ca/server-base/src/main/java/org/apache/kylin/rest/service/QueryService.java
----------------------------------------------------------------------
diff --git a/server-base/src/main/java/org/apache/kylin/rest/service/QueryService.java b/server-base/src/main/java/org/apache/kylin/rest/service/QueryService.java
index 6a38638..0070aaf 100644
--- a/server-base/src/main/java/org/apache/kylin/rest/service/QueryService.java
+++ b/server-base/src/main/java/org/apache/kylin/rest/service/QueryService.java
@@ -367,7 +367,7 @@ public class QueryService extends BasicService {
                             && checkCondition(sqlResponse.getDuration() > durationThreshold || sqlResponse.getTotalScanCount() > scanCountThreshold || sqlResponse.getTotalScanBytes() > scanBytesThreshold, //
                                     "query is too lightweight with duration: {} (threshold {}), scan count: {} (threshold {}), scan bytes: {} (threshold {})", sqlResponse.getDuration(), durationThreshold, sqlResponse.getTotalScanCount(), scanCountThreshold, sqlResponse.getTotalScanBytes(), scanBytesThreshold)
                             && checkCondition(sqlResponse.getResults().size() < kylinConfig.getLargeQueryThreshold(), "query response is too large: {} ({})", sqlResponse.getResults().size(), kylinConfig.getLargeQueryThreshold())) {
-                        cacheManager.getCache(SUCCESS_QUERY_CACHE).put(new Element(sqlRequest, sqlResponse));
+                        cacheManager.getCache(SUCCESS_QUERY_CACHE).put(new Element(sqlRequest.getCacheKey(), sqlResponse));
                     }
 
                 } else {
@@ -388,7 +388,7 @@ public class QueryService extends BasicService {
 
                 if (queryCacheEnabled && e.getCause() != null && e.getCause() instanceof ResourceLimitExceededException) {
                     Cache exceptionCache = cacheManager.getCache(EXCEPTION_QUERY_CACHE);
-                    exceptionCache.put(new Element(sqlRequest, sqlResponse));
+                    exceptionCache.put(new Element(sqlRequest.getCacheKey(), sqlResponse));
                 }
             }
 
@@ -412,14 +412,13 @@ public class QueryService extends BasicService {
         Cache exceptionCache = cacheManager.getCache(EXCEPTION_QUERY_CACHE);
         Cache successCache = cacheManager.getCache(SUCCESS_QUERY_CACHE);
 
-        if (exceptionCache.get(sqlRequest) != null) {
+        Element element = null;
+        if ((element = exceptionCache.get(sqlRequest.getCacheKey())) != null) {
             logger.info("The sqlResponse is found in EXCEPTION_QUERY_CACHE");
-            Element element = exceptionCache.get(sqlRequest);
             response = (SQLResponse) element.getObjectValue();
             response.setHitExceptionCache(true);
-        } else if (successCache.get(sqlRequest) != null) {
+        } else if ((element = successCache.get(sqlRequest.getCacheKey())) != null) {
             logger.info("The sqlResponse is found in SUCCESS_QUERY_CACHE");
-            Element element = successCache.get(sqlRequest);
             response = (SQLResponse) element.getObjectValue();
             response.setStorageCacheUsed(true);
         }