diff --git a/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/AggregateStatsCache.java b/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/AggregateStatsCache.java index 125e5b99f1..2878c7d158 100644 --- a/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/AggregateStatsCache.java +++ b/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/AggregateStatsCache.java @@ -23,6 +23,7 @@ import org.apache.hadoop.hive.metastore.conf.MetastoreConf.ConfVars; import org.slf4j.Logger; import org.slf4j.LoggerFactory; +import org.apache.curator.shaded.com.google.common.base.Preconditions; import org.apache.hadoop.conf.Configuration; import org.apache.hadoop.hive.metastore.api.ColumnStatisticsObj; import org.apache.hive.common.util.BloomFilter; @@ -165,7 +166,7 @@ public AggrColStats get(String catName, String dbName, String tblName, String co AggrColStatsList candidateList = cacheStore.get(key); // No key, or no nodes in candidate list if ((candidateList == null) || (candidateList.nodes.size() == 0)) { - LOG.debug("No aggregate stats cached for " + key.toString()); + LOG.debug("No aggregate stats cached for {}", key); return null; } // Find the value object @@ -190,7 +191,7 @@ public AggrColStats get(String catName, String dbName, String tblName, String co cacheMisses.incrementAndGet(); } } catch (InterruptedException e) { - LOG.debug("Interrupted Exception ignored ",e); + LOG.debug("Interrupted Exception ignored", e); } finally { if (isLocked) { candidateList.readLock.unlock(); @@ -287,13 +288,11 @@ public void add(String catName, String dbName, String tblName, String colName, l Key key = new Key(catName, dbName, tblName, colName); // Add new node to the cache AggrColStats node = new AggrColStats(numPartsCached, bloomFilter, colStats); - AggrColStatsList nodeList; - AggrColStatsList newNodeList = new AggrColStatsList(); - newNodeList.nodes = new ArrayList<>(); - nodeList = cacheStore.putIfAbsent(key, newNodeList); - if (nodeList == null) { - nodeList = newNodeList; - } + AggrColStatsList nodeList = cacheStore.computeIfAbsent(key, (k) -> { + AggrColStatsList newNodeList = new AggrColStatsList(); + newNodeList.nodes = new ArrayList<>(); + return newNodeList; + }); boolean isLocked = false; try { isLocked = nodeList.writeLock.tryLock(maxWriterWaitTime, TimeUnit.MILLISECONDS); @@ -304,7 +303,7 @@ public void add(String catName, String dbName, String tblName, String colName, l currentNodes.getAndIncrement(); } } catch (InterruptedException e) { - LOG.debug("Interrupted Exception ignored ", e); + LOG.debug("Interrupted Exception ignored", e); } finally { if (isLocked) { nodeList.writeLock.unlock(); @@ -360,7 +359,7 @@ public void run() { } } } catch (InterruptedException e) { - LOG.debug("Interrupted Exception ignored ",e); + LOG.debug("Interrupted Exception ignored", e); } finally { if (isLocked) { candidateList.writeLock.unlock(); @@ -448,7 +447,7 @@ private void evictOneNode() { numRemovedLRU++; } } catch (InterruptedException e) { - LOG.debug("Interrupted Exception ignored ",e); + LOG.debug("Interrupted Exception ignored", e); } finally { if (isLocked) { candidateList.writeLock.unlock(); @@ -470,10 +469,8 @@ private boolean isExpired(AggrColStats aggrColStats) { private final String colName; Key(String cat, String db, String table, String col) { - // Don't construct an illegal cache key - if (cat == null || (db == null) || (table == null) || (col == null)) { - throw new IllegalArgumentException("catName, dbName, tblName, colName can't be null"); - } + Preconditions.checkArgument((cat != null) && (db != null) && (table != null) && (col != null), + "catName, dbName, tblName, colName cannot be null"); catName = cat; dbName = db; tblName = table; @@ -481,27 +478,36 @@ private boolean isExpired(AggrColStats aggrColStats) { } @Override - public boolean equals(Object other) { - if ((other == null) || !(other instanceof Key)) { - return false; - } - Key that = (Key) other; - return catName.equals(that.catName) && dbName.equals(that.dbName) && - tblName.equals(that.tblName) && colName.equals(that.colName); + public int hashCode() { + final int prime = 31; + int result = 1; + result = prime * result + catName.hashCode(); + result = prime * result + colName.hashCode(); + result = prime * result + dbName.hashCode(); + result = prime * result + tblName.hashCode(); + return result; } @Override - public int hashCode() { - return catName.hashCode() * 31 + dbName.hashCode() * 31 + tblName.hashCode() * 31 + - colName.hashCode(); + public boolean equals(Object obj) { + if (this == obj) { + return true; + } + if (obj == null) { + return false; + } + if (getClass() != obj.getClass()) { + return false; + } + Key other = (Key) obj; + return (other.colName.equals(colName) && other.tblName.equals(tblName) && other.dbName.equals(dbName) + && other.catName.equals(catName)); } @Override public String toString() { - return "catalog: " + catName + ", database:" + dbName + ", table:" + tblName + ", column:" + - colName; + return "Key [catName=" + catName + ", dbName=" + dbName + ", tblName=" + tblName + ", colName=" + colName + "]"; } - } static class AggrColStatsList {