commit b8cff2d221c2c5e6f59a9452e2a5fbac1ea2a6ba Author: Mithun RK Date: Tue Oct 3 13:33:48 2017 -0700 HIVE-17669: Cache to optimize SearchArgument deserialization (Mithun Radhakrishnan, reviewed by Prasanth Jayachandran) diff --git a/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java b/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java index 06efb88b20..9ee4ca0058 100644 --- a/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java +++ b/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java @@ -1371,6 +1371,9 @@ private static void populateLlapDaemonVarsSet(Set llapDaemonVarsSetLocal "references for the cached object. Setting this to true can help avoid out of memory\n" + "issues under memory pressure (in some cases) at the cost of slight unpredictability in\n" + "overall query performance."), + HIVE_IO_SARG_CACHE_MAX_WEIGHT_MB("hive.io.sarg.cache.max.weight.mb", 10, + "The max weight allowed for the SearchArgument Cache. By default, the cache allows a max-weight of 10MB, " + + "after which entries will be evicted."), HIVE_ORC_SKIP_CORRUPT_DATA("hive.exec.orc.skip.corrupt.data", false, "If ORC reader encounters corrupt data, this value will be used to determine\n" + "whether to skip the corrupt data or throw exception. The default behavior is to throw exception."), diff --git a/ql/src/java/org/apache/hadoop/hive/ql/io/sarg/ConvertAstToSearchArg.java b/ql/src/java/org/apache/hadoop/hive/ql/io/sarg/ConvertAstToSearchArg.java index a3d327869a..f9e4c85eff 100644 --- a/ql/src/java/org/apache/hadoop/hive/ql/io/sarg/ConvertAstToSearchArg.java +++ b/ql/src/java/org/apache/hadoop/hive/ql/io/sarg/ConvertAstToSearchArg.java @@ -21,13 +21,18 @@ import java.sql.Date; import java.sql.Timestamp; import java.util.List; +import java.util.concurrent.Callable; +import java.util.concurrent.ExecutionException; +import com.google.common.cache.Cache; +import com.google.common.cache.CacheBuilder; +import com.google.common.cache.Weigher; import org.apache.commons.codec.binary.Base64; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hive.common.type.HiveChar; import org.apache.hadoop.hive.common.type.HiveDecimal; +import org.apache.hadoop.hive.conf.HiveConf; import org.apache.hadoop.hive.ql.exec.SerializationUtilities; -import org.apache.hadoop.hive.ql.io.sarg.LiteralDelegate; import org.apache.hadoop.hive.ql.plan.ExprNodeColumnDesc; import org.apache.hadoop.hive.ql.plan.ExprNodeConstantDesc; import org.apache.hadoop.hive.ql.plan.ExprNodeDesc; @@ -437,14 +442,75 @@ private void parse(ExprNodeDesc expression) { } } - public static final String SARG_PUSHDOWN = "sarg.pushdown"; + private static volatile Cache sargsCache = null; + + private static synchronized Cache initializeAndGetSargsCache(Configuration conf) { + if (sargsCache == null) { + sargsCache = CacheBuilder.newBuilder() + .weigher(new Weigher() { + @Override + public int weigh(String key, SearchArgument value) { + return key.length(); + } + }) + .maximumWeight( + HiveConf.getIntVar(conf, + HiveConf.ConfVars.HIVE_IO_SARG_CACHE_MAX_WEIGHT_MB) * 1024 *1024 + ) + .build(); // Can't use CacheLoader because SearchArguments may be built either from Kryo strings, + // or from expressions. + } + return sargsCache; + } + + private static Cache getSargsCache(Configuration conf) { + return sargsCache == null? initializeAndGetSargsCache(conf) : sargsCache; + } + + private static boolean isSargsCacheEnabled(Configuration conf) { + return HiveConf.getIntVar(conf, HiveConf.ConfVars.HIVE_IO_SARG_CACHE_MAX_WEIGHT_MB) > 0; + } + + private static SearchArgument getSearchArgumentFromString(Configuration conf, final String sargString) { + + try { + return isSargsCacheEnabled(conf)? + getSargsCache(conf).get(sargString, new Callable() { + @Override + public SearchArgument call() { + return create(sargString); + } + }) + : create(sargString); + } + catch (ExecutionException exception) { + throw new RuntimeException(exception); + } + } + + private static SearchArgument getSearchArgumentFromExpression(final Configuration conf, final String sargString) { + + try { + return isSargsCacheEnabled(conf)? + getSargsCache(conf).get(sargString, new Callable() { + @Override + public SearchArgument call() { + return create(conf, SerializationUtilities.deserializeExpression(sargString)); + } + }) + : create(conf, SerializationUtilities.deserializeExpression(sargString)); + } + catch (ExecutionException exception) { + throw new RuntimeException(exception); + } + } + public static SearchArgument create(Configuration conf, ExprNodeGenericFuncDesc expression) { return new ConvertAstToSearchArg(conf, expression).buildSearchArgument(); } - private final static ThreadLocal kryo = new ThreadLocal() { protected Kryo initialValue() { return new Kryo(); } }; @@ -460,9 +526,9 @@ public static SearchArgument create(byte[] kryoBytes) { public static SearchArgument createFromConf(Configuration conf) { String sargString; if ((sargString = conf.get(TableScanDesc.FILTER_EXPR_CONF_STR)) != null) { - return create(conf, SerializationUtilities.deserializeExpression(sargString)); + return getSearchArgumentFromExpression(conf, sargString); } else if ((sargString = conf.get(SARG_PUSHDOWN)) != null) { - return create(sargString); + return getSearchArgumentFromString(conf, sargString); } return null; }