This is an automated email from the ASF dual-hosted git repository.

vjasani pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/phoenix.git


The following commit(s) were added to refs/heads/master by this push:
     new dd533d8c83 PHOENIX-7395 Metadata Cache metrics at server and client 
side (#1976)
dd533d8c83 is described below

commit dd533d8c83cb1508ca601c7faf3b30f53b84ba82
Author: Jing Yu <[email protected]>
AuthorDate: Mon Sep 16 21:52:29 2024 -0700

    PHOENIX-7395 Metadata Cache metrics at server and client side (#1976)
---
 .../phoenix/monitoring/GlobalClientMetrics.java    |   8 ++
 .../org/apache/phoenix/monitoring/MetricType.java  |  11 ++-
 .../org/apache/phoenix/schema/PMetaDataCache.java  |  12 +++
 .../org/apache/phoenix/schema/PMetaDataImpl.java   |  29 +++++-
 .../schema/metrics/MetricsMetadataSource.java      |  57 ++++++++++-
 .../schema/metrics/MetricsMetadataSourceImpl.java  |  55 +++++++++++
 .../java/org/apache/phoenix/cache/GlobalCache.java |  21 ++++-
 .../phoenix/coprocessor/MetaDataEndpointImpl.java  |  48 +++++++++-
 .../apache/phoenix/end2end/MetaDataEndPointIT.java | 105 +++++++++++++++++++++
 .../apache/phoenix/monitoring/IndexMetricsIT.java  |  11 +++
 .../phoenix/monitoring/MetadataMetricsIT.java      |  45 +++++++++
 .../phoenix/query/MetaDataCacheMetricsIT.java      | 103 ++++++++++++++++++++
 .../apache/phoenix/query/MetaDataCachingIT.java    |   2 +-
 13 files changed, 496 insertions(+), 11 deletions(-)

diff --git 
a/phoenix-core-client/src/main/java/org/apache/phoenix/monitoring/GlobalClientMetrics.java
 
b/phoenix-core-client/src/main/java/org/apache/phoenix/monitoring/GlobalClientMetrics.java
index 8763a7c5ec..f0f071cbcf 100644
--- 
a/phoenix-core-client/src/main/java/org/apache/phoenix/monitoring/GlobalClientMetrics.java
+++ 
b/phoenix-core-client/src/main/java/org/apache/phoenix/monitoring/GlobalClientMetrics.java
@@ -17,6 +17,10 @@
  */
 package org.apache.phoenix.monitoring;
 
+import static 
org.apache.phoenix.monitoring.MetricType.CLIENT_METADATA_CACHE_ADD_COUNTER;
+import static 
org.apache.phoenix.monitoring.MetricType.CLIENT_METADATA_CACHE_ESTIMATED_USED_SIZE;
+import static 
org.apache.phoenix.monitoring.MetricType.CLIENT_METADATA_CACHE_EVICTION_COUNTER;
+import static 
org.apache.phoenix.monitoring.MetricType.CLIENT_METADATA_CACHE_REMOVAL_COUNTER;
 import static org.apache.phoenix.monitoring.MetricType.HCONNECTIONS_COUNTER;
 import static org.apache.phoenix.monitoring.MetricType.MEMORY_CHUNK_BYTES;
 import static org.apache.phoenix.monitoring.MetricType.MEMORY_WAIT_TIME;
@@ -161,6 +165,10 @@ public enum GlobalClientMetrics {
 
     
GLOBAL_CLIENT_METADATA_CACHE_MISS_COUNTER(CLIENT_METADATA_CACHE_MISS_COUNTER),
     
GLOBAL_CLIENT_METADATA_CACHE_HIT_COUNTER(CLIENT_METADATA_CACHE_HIT_COUNTER),
+    
GLOBAL_CLIENT_METADATA_CACHE_EVICTION_COUNTER(CLIENT_METADATA_CACHE_EVICTION_COUNTER),
+    
GLOBAL_CLIENT_METADATA_CACHE_REMOVAL_COUNTER(CLIENT_METADATA_CACHE_REMOVAL_COUNTER),
+    
GLOBAL_CLIENT_METADATA_CACHE_ADD_COUNTER(CLIENT_METADATA_CACHE_ADD_COUNTER),
+    
GLOBAL_CLIENT_METADATA_CACHE_ESTIMATED_USED_SIZE(CLIENT_METADATA_CACHE_ESTIMATED_USED_SIZE),
     
GLOBAL_CLIENT_STALE_METADATA_CACHE_EXCEPTION_COUNTER(STALE_METADATA_CACHE_EXCEPTION_COUNTER);
 
     private static final Logger LOGGER = 
LoggerFactory.getLogger(GlobalClientMetrics.class);
diff --git 
a/phoenix-core-client/src/main/java/org/apache/phoenix/monitoring/MetricType.java
 
b/phoenix-core-client/src/main/java/org/apache/phoenix/monitoring/MetricType.java
index b7b0c4b562..d66fb0e19d 100644
--- 
a/phoenix-core-client/src/main/java/org/apache/phoenix/monitoring/MetricType.java
+++ 
b/phoenix-core-client/src/main/java/org/apache/phoenix/monitoring/MetricType.java
@@ -149,7 +149,16 @@ public enum MetricType {
                                                 ", not including throttled 
connections", LogLevel.OFF, PLong.INSTANCE),
     CLIENT_METADATA_CACHE_MISS_COUNTER("cmcm", "Number of cache misses for the 
CQSI cache.", LogLevel.DEBUG, PLong.INSTANCE),
     CLIENT_METADATA_CACHE_HIT_COUNTER("cmch", "Number of cache hits for the 
CQSI cache.", LogLevel.DEBUG, PLong.INSTANCE),
-    PAGED_ROWS_COUNTER("prc", "Number of dummy rows returned to client due to 
paging.", LogLevel.DEBUG, PLong.INSTANCE),
+    CLIENT_METADATA_CACHE_EVICTION_COUNTER("cmce", "Number of cache evictions 
for the CQSI cache" +
+            ".", LogLevel.DEBUG,  PLong.INSTANCE),
+    CLIENT_METADATA_CACHE_REMOVAL_COUNTER("cmcr", "Number of cache removals 
for the CQSI cache.",
+            LogLevel.DEBUG, PLong.INSTANCE),
+    CLIENT_METADATA_CACHE_ADD_COUNTER("cmca", "Number of cache adds for the 
CQSI cache.",
+            LogLevel.DEBUG, PLong.INSTANCE),
+    CLIENT_METADATA_CACHE_ESTIMATED_USED_SIZE("cmcu", "Estimated used size of 
the CQSI cache.",
+            LogLevel.DEBUG, PLong.INSTANCE),
+    PAGED_ROWS_COUNTER("prc", "Number of dummy rows returned to client due to 
paging.",
+            LogLevel.DEBUG, PLong.INSTANCE),
     STALE_METADATA_CACHE_EXCEPTION_COUNTER("smce",
             "Number of StaleMetadataCacheException encountered.",
             LogLevel.DEBUG, PLong.INSTANCE),
diff --git 
a/phoenix-core-client/src/main/java/org/apache/phoenix/schema/PMetaDataCache.java
 
b/phoenix-core-client/src/main/java/org/apache/phoenix/schema/PMetaDataCache.java
index 89d3cbcf3c..767969785a 100644
--- 
a/phoenix-core-client/src/main/java/org/apache/phoenix/schema/PMetaDataCache.java
+++ 
b/phoenix-core-client/src/main/java/org/apache/phoenix/schema/PMetaDataCache.java
@@ -22,6 +22,7 @@ import java.util.Map;
 import java.util.concurrent.ConcurrentHashMap;
 
 import org.apache.phoenix.jdbc.PhoenixDatabaseMetaData;
+import org.apache.phoenix.monitoring.GlobalClientMetrics;
 import org.apache.phoenix.parse.PFunction;
 import org.apache.phoenix.parse.PSchema;
 import org.apache.phoenix.thirdparty.com.google.common.cache.CacheBuilder;
@@ -52,6 +53,17 @@ class PMetaDataCache {
                         String key = notification.getKey().toString();
                         LOGGER.debug("Expiring " + key + " because of "
                                 + notification.getCause().name());
+                        if (notification.wasEvicted()) {
+                            
GlobalClientMetrics.GLOBAL_CLIENT_METADATA_CACHE_EVICTION_COUNTER
+                                    .increment();
+                        } else {
+                            
GlobalClientMetrics.GLOBAL_CLIENT_METADATA_CACHE_REMOVAL_COUNTER
+                                    .increment();
+                        }
+                        if (notification.getValue() != null) {
+                            
GlobalClientMetrics.GLOBAL_CLIENT_METADATA_CACHE_ESTIMATED_USED_SIZE
+                                    
.update(-notification.getValue().getEstimatedSize());
+                        }
                     }
                 })
                 .maximumWeight(maxByteSize)
diff --git 
a/phoenix-core-client/src/main/java/org/apache/phoenix/schema/PMetaDataImpl.java
 
b/phoenix-core-client/src/main/java/org/apache/phoenix/schema/PMetaDataImpl.java
index e61f93c8be..2029868ded 100644
--- 
a/phoenix-core-client/src/main/java/org/apache/phoenix/schema/PMetaDataImpl.java
+++ 
b/phoenix-core-client/src/main/java/org/apache/phoenix/schema/PMetaDataImpl.java
@@ -121,6 +121,9 @@ public class PMetaDataImpl implements PMetaData {
     public void updateResolvedTimestamp(PTable table, long resolvedTimestamp) 
throws SQLException {
         metaData.put(table.getKey(), tableRefFactory.makePTableRef(table,
                 this.timeKeeper.getCurrentTime(), resolvedTimestamp));
+        
GlobalClientMetrics.GLOBAL_CLIENT_METADATA_CACHE_ADD_COUNTER.increment();
+        
GlobalClientMetrics.GLOBAL_CLIENT_METADATA_CACHE_ESTIMATED_USED_SIZE.update(
+                table.getEstimatedSize());
     }
 
     @Override
@@ -157,12 +160,20 @@ public class PMetaDataImpl implements PMetaData {
         
         if (newParentTable != null) { // Upsert new index table into parent 
data table list
             metaData.put(newParentTable.getKey(), newParentTableRef);
-            metaData.put(table.getKey(), tableRef);
-        } else {
-            metaData.put(table.getKey(), tableRef);
+            
GlobalClientMetrics.GLOBAL_CLIENT_METADATA_CACHE_ADD_COUNTER.increment();
+            
GlobalClientMetrics.GLOBAL_CLIENT_METADATA_CACHE_ESTIMATED_USED_SIZE.update(
+                    newParentTable.getEstimatedSize());
         }
+        metaData.put(table.getKey(), tableRef);
+        
GlobalClientMetrics.GLOBAL_CLIENT_METADATA_CACHE_ADD_COUNTER.increment();
+        
GlobalClientMetrics.GLOBAL_CLIENT_METADATA_CACHE_ESTIMATED_USED_SIZE.update(
+                table.getEstimatedSize());
+
         for (PTable index : table.getIndexes()) {
             metaData.put(index.getKey(), tableRefFactory.makePTableRef(index, 
this.timeKeeper.getCurrentTime(), resolvedTime));
+            
GlobalClientMetrics.GLOBAL_CLIENT_METADATA_CACHE_ADD_COUNTER.increment();
+            
GlobalClientMetrics.GLOBAL_CLIENT_METADATA_CACHE_ESTIMATED_USED_SIZE.update(
+                    index.getEstimatedSize());
         }
         if (table.getPhysicalName(true) != null &&
                 
!Strings.isNullOrEmpty(table.getPhysicalName(true).getString()) && 
!table.getPhysicalName(true).getString().equals(table.getTableName().getString()))
 {
@@ -212,6 +223,9 @@ public class PMetaDataImpl implements PMetaData {
                         }
                         PTable parentTable = parentTableBuilder.build();
                         metaData.put(parentTable.getKey(), 
tableRefFactory.makePTableRef(parentTable, this.timeKeeper.getCurrentTime(), 
parentTableRef.getResolvedTimeStamp()));
+                        
GlobalClientMetrics.GLOBAL_CLIENT_METADATA_CACHE_ADD_COUNTER.increment();
+                        
GlobalClientMetrics.GLOBAL_CLIENT_METADATA_CACHE_ESTIMATED_USED_SIZE.update(
+                                parentTable.getEstimatedSize());
                         break;
                     }
                 }
@@ -258,6 +272,9 @@ public class PMetaDataImpl implements PMetaData {
                     .build();
         }
         tables.put(table.getKey(), tableRefFactory.makePTableRef(table, 
this.timeKeeper.getCurrentTime(), resolvedTime));
+        
GlobalClientMetrics.GLOBAL_CLIENT_METADATA_CACHE_ADD_COUNTER.increment();
+        
GlobalClientMetrics.GLOBAL_CLIENT_METADATA_CACHE_ESTIMATED_USED_SIZE.update(
+                table.getEstimatedSize());
     }
 
     @Override
@@ -283,6 +300,9 @@ public class PMetaDataImpl implements PMetaData {
     @Override
     public void addFunction(PFunction function) throws SQLException {
         this.metaData.functions.put(function.getKey(), function);
+        
GlobalClientMetrics.GLOBAL_CLIENT_METADATA_CACHE_ADD_COUNTER.increment();
+        
GlobalClientMetrics.GLOBAL_CLIENT_METADATA_CACHE_ESTIMATED_USED_SIZE.update(
+                function.getEstimatedSize());
     }
 
     @Override
@@ -314,6 +334,9 @@ public class PMetaDataImpl implements PMetaData {
     @Override
     public void addSchema(PSchema schema) throws SQLException {
         this.metaData.schemas.put(schema.getSchemaKey(), schema);
+        
GlobalClientMetrics.GLOBAL_CLIENT_METADATA_CACHE_ADD_COUNTER.increment();
+        
GlobalClientMetrics.GLOBAL_CLIENT_METADATA_CACHE_ESTIMATED_USED_SIZE.update(
+                schema.getEstimatedSize());
     }
 
     @Override
diff --git 
a/phoenix-core-client/src/main/java/org/apache/phoenix/schema/metrics/MetricsMetadataSource.java
 
b/phoenix-core-client/src/main/java/org/apache/phoenix/schema/metrics/MetricsMetadataSource.java
index 8ed65a93cf..873a9f2d30 100644
--- 
a/phoenix-core-client/src/main/java/org/apache/phoenix/schema/metrics/MetricsMetadataSource.java
+++ 
b/phoenix-core-client/src/main/java/org/apache/phoenix/schema/metrics/MetricsMetadataSource.java
@@ -88,6 +88,24 @@ public interface MetricsMetadataSource {
     String DROP_FUNCTION_COUNT = "dropFunctionCount";
     String DROP_FUNCTION_COUNT_DESC = "Count of DROP FUNCTION DDL statements";
 
+    String METADATA_CACHE_ESTIMATED_USED_SIZE = 
"metadataCacheEstimatedUsedSize";
+    String METADATA_CACHE_ESTIMATED_USED_SIZE_DESC = "Estimated used size of 
the metadata cache";
+
+    String METADATA_CACHE_HIT_COUNT = "metadataCacheHitCount";
+    String METADATA_CACHE_HIT_COUNT_DESC = "Hit count of the metadata cache";
+
+    String METADATA_CACHE_MISS_COUNT = "metadataCacheMissCount";
+    String METADATA_CACHE_MISS_COUNT_DESC = "Miss count of the metadata cache";
+
+    String METADATA_CACHE_EVICTION_COUNT = "metadataCacheEvictionCount";
+    String METADATA_CACHE_EVICTION_COUNT_DESC = "Eviction count of the 
metadata cache";
+
+    String METADATA_CACHE_REMOVAL_COUNT = "metadataCacheRemovalCount";
+    String METADATA_CACHE_REMOVAL_COUNT_DESC = "Removal count of the metadata 
cache";
+
+    String METADATA_CACHE_ADD_COUNT = "metadataCacheAddCount";
+    String METADATA_CACHE_ADD_COUNT_DESC = "Add count of the metadata cache";
+
     // TODO: metrics for addIndexToTable and dropIndexes
 
     /**
@@ -120,7 +138,7 @@ public interface MetricsMetadataSource {
 
     /**
      * Updates the histogram of time taken updating the schema registry for 
ALTER statements
-     * @param t time takne
+     * @param t time taken
      */
     void updateAlterExportTime(long t);
 
@@ -195,4 +213,41 @@ public interface MetricsMetadataSource {
      * Updates the count of successful DROP FUNCTION DDL operations
      */
     void incrementDropFunctionCount();
+
+    /**
+     * Increases the estimated used size of metadata cache
+     * @param estimatedSize the estimated size to be increased
+     */
+    void incrementMetadataCacheUsedSize(long estimatedSize);
+
+    /**
+     * Decreases the estimated used size of metadata cache
+     * @param estimatedSize the estimated size to be decreased
+     */
+    void decrementMetadataCacheUsedSize(long estimatedSize);
+
+    /**
+     * Updates the count of metadata cache hit
+     */
+    void incrementMetadataCacheHitCount();
+
+    /**
+     * Updates the count of metadata cache miss
+     */
+    void incrementMetadataCacheMissCount();
+
+    /**
+     * Updates the count of metadata cache eviction
+     */
+    void incrementMetadataCacheEvictionCount();
+
+    /**
+     * Updates the count of metadata cache removal
+     */
+    void incrementMetadataCacheRemovalCount();
+
+    /**
+     * Updates the count of metadata cache add
+     */
+    void incrementMetadataCacheAddCount();
 }
diff --git 
a/phoenix-core-client/src/main/java/org/apache/phoenix/schema/metrics/MetricsMetadataSourceImpl.java
 
b/phoenix-core-client/src/main/java/org/apache/phoenix/schema/metrics/MetricsMetadataSourceImpl.java
index d887ad539c..4af91ff1cc 100644
--- 
a/phoenix-core-client/src/main/java/org/apache/phoenix/schema/metrics/MetricsMetadataSourceImpl.java
+++ 
b/phoenix-core-client/src/main/java/org/apache/phoenix/schema/metrics/MetricsMetadataSourceImpl.java
@@ -50,6 +50,13 @@ public class MetricsMetadataSourceImpl extends 
BaseSourceImpl implements Metrics
     private final MutableFastCounter dropSchemaCount;
     private final MutableFastCounter dropFunctionCount;
 
+    private final MutableFastCounter metadataCacheUsedSize;
+    private final MutableFastCounter metadataCacheHitCount;
+    private final MutableFastCounter metadataCacheMissCount;
+    private final MutableFastCounter metadataCacheEvictionCount;
+    private final MutableFastCounter metadataCacheRemovalCount;
+    private final MutableFastCounter metadataCacheAddCount;
+
     public MetricsMetadataSourceImpl() {
         this(METRICS_NAME, METRICS_DESCRIPTION, METRICS_CONTEXT, 
METRICS_JMX_CONTEXT);
     }
@@ -102,6 +109,19 @@ public class MetricsMetadataSourceImpl extends 
BaseSourceImpl implements Metrics
                 DROP_SCHEMA_COUNT_DESC, 0L);
         dropFunctionCount = 
getMetricsRegistry().newCounter(DROP_FUNCTION_COUNT,
                 DROP_FUNCTION_COUNT_DESC, 0L);
+
+        metadataCacheUsedSize = 
getMetricsRegistry().newCounter(METADATA_CACHE_ESTIMATED_USED_SIZE,
+                METADATA_CACHE_ESTIMATED_USED_SIZE_DESC, 0L);
+        metadataCacheHitCount = 
getMetricsRegistry().newCounter(METADATA_CACHE_HIT_COUNT,
+                METADATA_CACHE_HIT_COUNT_DESC, 0L);
+        metadataCacheMissCount = 
getMetricsRegistry().newCounter(METADATA_CACHE_MISS_COUNT,
+                METADATA_CACHE_MISS_COUNT_DESC, 0L);
+        metadataCacheEvictionCount = 
getMetricsRegistry().newCounter(METADATA_CACHE_EVICTION_COUNT,
+                METADATA_CACHE_EVICTION_COUNT_DESC, 0L);
+        metadataCacheRemovalCount = 
getMetricsRegistry().newCounter(METADATA_CACHE_REMOVAL_COUNT,
+                METADATA_CACHE_REMOVAL_COUNT_DESC, 0L);
+        metadataCacheAddCount = 
getMetricsRegistry().newCounter(METADATA_CACHE_ADD_COUNT,
+                METADATA_CACHE_ADD_COUNT_DESC, 0L);
     }
 
     @Override public void incrementCreateExportCount() {
@@ -183,4 +203,39 @@ public class MetricsMetadataSourceImpl extends 
BaseSourceImpl implements Metrics
     @Override public void incrementDropFunctionCount() {
         dropFunctionCount.incr();
     }
+
+    @Override
+    public void incrementMetadataCacheUsedSize(long estimatedSize) {
+        metadataCacheUsedSize.incr(estimatedSize);
+    }
+
+    @Override
+    public void decrementMetadataCacheUsedSize(long estimatedSize) {
+        metadataCacheUsedSize.incr(-estimatedSize);
+    }
+
+    @Override
+    public void incrementMetadataCacheHitCount() {
+        metadataCacheHitCount.incr();
+    }
+
+    @Override
+    public void incrementMetadataCacheMissCount() {
+        metadataCacheMissCount.incr();
+    }
+
+    @Override
+    public void incrementMetadataCacheEvictionCount() {
+        metadataCacheEvictionCount.incr();
+    }
+
+    @Override
+    public void incrementMetadataCacheRemovalCount() {
+        metadataCacheRemovalCount.incr();
+    }
+
+    @Override
+    public void incrementMetadataCacheAddCount() {
+        metadataCacheAddCount.incr();
+    }
 }
diff --git 
a/phoenix-core-server/src/main/java/org/apache/phoenix/cache/GlobalCache.java 
b/phoenix-core-server/src/main/java/org/apache/phoenix/cache/GlobalCache.java
index ba5bb4b1fd..fb54e8dc25 100644
--- 
a/phoenix-core-server/src/main/java/org/apache/phoenix/cache/GlobalCache.java
+++ 
b/phoenix-core-server/src/main/java/org/apache/phoenix/cache/GlobalCache.java
@@ -36,6 +36,7 @@ import org.apache.phoenix.memory.GlobalMemoryManager;
 import org.apache.phoenix.query.QueryServices;
 import org.apache.phoenix.query.QueryServicesOptions;
 import org.apache.phoenix.schema.PMetaDataEntity;
+import org.apache.phoenix.schema.metrics.MetricsMetadataSource;
 import org.apache.phoenix.util.SizedUtil;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -62,7 +63,8 @@ public class GlobalCache extends TenantCacheImpl {
     private final ConcurrentMap<ImmutableBytesWritable,TenantCache> 
perTenantCacheMap = new ConcurrentHashMap<ImmutableBytesWritable,TenantCache>();
     // Cache for lastest PTable for a given Phoenix table
     private volatile Cache<ImmutableBytesPtr,PMetaDataEntity> metaDataCache;
-    
+    private MetricsMetadataSource metricsSource;
+
     public long clearTenantCache() {
         long unfreedBytes = getMemoryManager().getMaxMemory() - 
getMemoryManager().getAvailableMemory();
         if (unfreedBytes != 0 && LOGGER.isDebugEnabled()) {
@@ -111,6 +113,19 @@ public class GlobalCache extends TenantCacheImpl {
                                     return SizedUtil.IMMUTABLE_BYTES_PTR_SIZE 
+ key.getLength() + table.getEstimatedSize();
                                 }
                             })
+                            .removalListener(notification -> {
+                                if (this.metricsSource != null) {
+                                    if (notification.wasEvicted()) {
+                                        
metricsSource.incrementMetadataCacheEvictionCount();
+                                    } else {
+                                        
metricsSource.incrementMetadataCacheRemovalCount();
+                                    }
+                                    if (notification.getValue() != null) {
+                                        
metricsSource.decrementMetadataCacheUsedSize(
+                                                
notification.getValue().getEstimatedSize());
+                                    }
+                                }
+                            })
                             .build();
                 }
             }
@@ -194,6 +209,10 @@ public class GlobalCache extends TenantCacheImpl {
         return tenantCache;
     }
 
+    public void setMetricsSource(MetricsMetadataSource metricsSource) {
+        this.metricsSource = metricsSource;
+    }
+
     public static class FunctionBytesPtr extends ImmutableBytesPtr {
 
         public FunctionBytesPtr(byte[] key) {
diff --git 
a/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java
 
b/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java
index 9497ce01af..c15feff75b 100644
--- 
a/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java
+++ 
b/phoenix-core-server/src/main/java/org/apache/phoenix/coprocessor/MetaDataEndpointImpl.java
@@ -677,6 +677,7 @@ TABLE_FAMILY_BYTES, TABLE_SEQ_NUM_BYTES);
         Tracing.addTraceMetricsSource();
         Metrics.ensureConfigured();
         metricsSource = 
MetricsMetadataSourceFactory.getMetadataMetricsSource();
+        GlobalCache.getInstance(this.env).setMetricsSource(metricsSource);
     }
 
     @Override
@@ -779,6 +780,11 @@ TABLE_FAMILY_BYTES, TABLE_SEQ_NUM_BYTES);
         region.startRegionOperation();
         try (RegionScanner scanner = region.getScanner(scan)) {
             PTable oldTable = (PTable) metaDataCache.getIfPresent(cacheKey);
+            if (oldTable == null) {
+                metricsSource.incrementMetadataCacheMissCount();
+            } else {
+                metricsSource.incrementMetadataCacheHitCount();
+            }
             long tableTimeStamp = oldTable == null ? MIN_TABLE_TIMESTAMP - 1 : 
oldTable.getTimeStamp();
             newTable = getTable(scanner, clientTimeStamp, tableTimeStamp, 
clientVersion);
             if (newTable != null
@@ -793,6 +799,8 @@ TABLE_FAMILY_BYTES, TABLE_SEQ_NUM_BYTES);
                             + tableTimeStamp);
                 }
                 metaDataCache.put(cacheKey, newTable);
+                metricsSource.incrementMetadataCacheAddCount();
+                
metricsSource.incrementMetadataCacheUsedSize(newTable.getEstimatedSize());
             }
         } finally {
             region.closeRegionOperation();
@@ -831,6 +839,8 @@ TABLE_FAMILY_BYTES, TABLE_SEQ_NUM_BYTES);
                                         .getTenantId().getBytes(), 
Bytes.toBytes(function
                                         .getFunctionName()));
                 metaDataCache.put(new FunctionBytesPtr(functionKey), function);
+                metricsSource.incrementMetadataCacheAddCount();
+                
metricsSource.incrementMetadataCacheUsedSize(function.getEstimatedSize());
                 functions.add(function);
             }
             return functions;
@@ -867,6 +877,8 @@ TABLE_FAMILY_BYTES, TABLE_SEQ_NUM_BYTES);
                     return null;
                 }
                 metaDataCache.put(cacheKey, schema);
+                metricsSource.incrementMetadataCacheAddCount();
+                
metricsSource.incrementMetadataCacheUsedSize(schema.getEstimatedSize());
                 schemas.add(schema);
             }
             return schemas;
@@ -2083,6 +2095,8 @@ TABLE_FAMILY_BYTES, TABLE_SEQ_NUM_BYTES);
                         GlobalCache.getInstance(this.env).getMetaDataCache();
                 PTable table = newDeletedTableMarker(kv.getTimestamp());
                 metaDataCache.put(cacheKey, table);
+                metricsSource.incrementMetadataCacheAddCount();
+                
metricsSource.incrementMetadataCacheUsedSize(table.getEstimatedSize());
                 return table;
             }
         }
@@ -2111,6 +2125,8 @@ TABLE_FAMILY_BYTES, TABLE_SEQ_NUM_BYTES);
                         GlobalCache.getInstance(this.env).getMetaDataCache();
                 PFunction function = 
newDeletedFunctionMarker(kv.getTimestamp());
                 metaDataCache.put(cacheKey, function);
+                metricsSource.incrementMetadataCacheAddCount();
+                
metricsSource.incrementMetadataCacheUsedSize(function.getEstimatedSize());
                 return function;
             }
         }
@@ -2138,6 +2154,8 @@ TABLE_FAMILY_BYTES, TABLE_SEQ_NUM_BYTES);
                         .getMetaDataCache();
                 PSchema schema = newDeletedSchemaMarker(kv.getTimestamp());
                 metaDataCache.put(cacheKey, schema);
+                metricsSource.incrementMetadataCacheAddCount();
+                
metricsSource.incrementMetadataCacheUsedSize(schema.getEstimatedSize());
                 return schema;
             }
         }
@@ -2206,6 +2224,11 @@ TABLE_FAMILY_BYTES, TABLE_SEQ_NUM_BYTES);
     private PTable getTableFromCache(ImmutableBytesPtr cacheKey, long 
clientTimeStamp, int clientVersion) {
         Cache<ImmutableBytesPtr, PMetaDataEntity> metaDataCache = 
GlobalCache.getInstance(this.env).getMetaDataCache();
         PTable table = (PTable) metaDataCache.getIfPresent(cacheKey);
+        if (table == null) {
+            metricsSource.incrementMetadataCacheMissCount();
+        } else {
+            metricsSource.incrementMetadataCacheHitCount();
+        }
         return table;
     }
 
@@ -2217,8 +2240,10 @@ TABLE_FAMILY_BYTES, TABLE_SEQ_NUM_BYTES);
         PFunction function = (PFunction) metaDataCache.getIfPresent(cacheKey);
         // We always cache the latest version - fault in if not in cache
         if (function != null && !isReplace) {
+            metricsSource.incrementMetadataCacheHitCount();
             return function;
         }
+        metricsSource.incrementMetadataCacheMissCount();
         ArrayList<byte[]> arrayList = new ArrayList<byte[]>(1);
         arrayList.add(key);
         List<PFunction> functions = buildFunctions(arrayList, region, 
asOfTimeStamp, isReplace, deleteMutationsForReplace);
@@ -2239,8 +2264,10 @@ TABLE_FAMILY_BYTES, TABLE_SEQ_NUM_BYTES);
         PSchema schema = (PSchema) metaDataCache.getIfPresent(cacheKey);
         // We always cache the latest version - fault in if not in cache
         if (schema != null) {
+            metricsSource.incrementMetadataCacheHitCount();
             return schema;
         }
+        metricsSource.incrementMetadataCacheMissCount();
         ArrayList<byte[]> arrayList = new ArrayList<byte[]>(1);
         arrayList.add(key);
         List<PSchema> schemas = buildSchemas(arrayList, region, asOfTimeStamp, 
cacheKey);
@@ -3105,7 +3132,10 @@ TABLE_FAMILY_BYTES, TABLE_SEQ_NUM_BYTES);
 
                 long currentTime = 
MetaDataUtil.getClientTimeStamp(tableMetadata);
                 for (ImmutableBytesPtr ckey : invalidateList) {
-                    metaDataCache.put(ckey, 
newDeletedTableMarker(currentTime));
+                    PTable table = newDeletedTableMarker(currentTime);
+                    metaDataCache.put(ckey, table);
+                    metricsSource.incrementMetadataCacheAddCount();
+                    
metricsSource.incrementMetadataCacheUsedSize(table.getEstimatedSize());
                 }
                 if (parentLockKey != null) {
                     ImmutableBytesPtr parentCacheKey = new 
ImmutableBytesPtr(parentLockKey);
@@ -4009,6 +4039,11 @@ TABLE_FAMILY_BYTES, TABLE_SEQ_NUM_BYTES);
             while (iterator.hasNext()) {
                 byte[] key = iterator.next();
                 PFunction function = (PFunction) 
metaDataCache.getIfPresent(new FunctionBytesPtr(key));
+                if (function == null) {
+                    metricsSource.incrementMetadataCacheMissCount();
+                } else {
+                    metricsSource.incrementMetadataCacheHitCount();
+                }
                 if (function != null && function.getTimeStamp() < 
clientTimeStamp) {
                     if (isFunctionDeleted(function)) {
                         return null;
@@ -4850,8 +4885,10 @@ TABLE_FAMILY_BYTES, TABLE_SEQ_NUM_BYTES);
                 long currentTime = 
MetaDataUtil.getClientTimeStamp(functionMetaData);
                 for (ImmutableBytesPtr ptr : invalidateList) {
                     metaDataCache.invalidate(ptr);
-                    metaDataCache.put(ptr, 
newDeletedFunctionMarker(currentTime));
-
+                    PFunction function = newDeletedFunctionMarker(currentTime);
+                    metaDataCache.put(ptr, function);
+                    metricsSource.incrementMetadataCacheAddCount();
+                    
metricsSource.incrementMetadataCacheUsedSize(function.getEstimatedSize());
                 }
 
                 done.run(MetaDataMutationResult.toProto(result));
@@ -5022,7 +5059,10 @@ TABLE_FAMILY_BYTES, TABLE_SEQ_NUM_BYTES);
                 long currentTime = 
MetaDataUtil.getClientTimeStamp(schemaMetaData);
                 for (ImmutableBytesPtr ptr : invalidateList) {
                     metaDataCache.invalidate(ptr);
-                    metaDataCache.put(ptr, 
newDeletedSchemaMarker(currentTime));
+                    PSchema schema = newDeletedSchemaMarker(currentTime);
+                    metaDataCache.put(ptr, schema);
+                    metricsSource.incrementMetadataCacheAddCount();
+                    
metricsSource.incrementMetadataCacheUsedSize(schema.getEstimatedSize());
                 }
                 done.run(MetaDataMutationResult.toProto(result));
 
diff --git 
a/phoenix-core/src/it/java/org/apache/phoenix/end2end/MetaDataEndPointIT.java 
b/phoenix-core/src/it/java/org/apache/phoenix/end2end/MetaDataEndPointIT.java
index 7445a42082..3c1446e467 100644
--- 
a/phoenix-core/src/it/java/org/apache/phoenix/end2end/MetaDataEndPointIT.java
+++ 
b/phoenix-core/src/it/java/org/apache/phoenix/end2end/MetaDataEndPointIT.java
@@ -18,15 +18,22 @@
 package org.apache.phoenix.end2end;
 
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
 
 import java.sql.Connection;
 import java.sql.DriverManager;
 
 import org.apache.hadoop.hbase.client.Table;
+import org.apache.hadoop.metrics2.lib.DynamicMetricsRegistry;
 import org.apache.phoenix.coprocessorclient.MetaDataProtocol.MutationCode;
 import org.apache.phoenix.jdbc.PhoenixConnection;
 import org.apache.phoenix.jdbc.PhoenixDatabaseMetaData;
+import org.apache.phoenix.monitoring.IndexMetricsIT;
 import org.apache.phoenix.schema.PIndexState;
+import org.apache.phoenix.schema.PTable;
+import org.apache.phoenix.schema.metrics.MetricsMetadataSource;
+import org.apache.phoenix.schema.metrics.MetricsMetadataSourceFactory;
+import org.apache.phoenix.schema.metrics.MetricsMetadataSourceImpl;
 import org.apache.phoenix.util.EnvironmentEdgeManager;
 import org.apache.phoenix.util.IndexUtil;
 import org.apache.phoenix.util.SchemaUtil;
@@ -54,4 +61,102 @@ public class MetaDataEndPointIT extends 
ParallelStatsDisabledIT {
             assertEquals(MutationCode.UNALLOWED_TABLE_MUTATION, code);
         }
        }
+
+    @Test
+    public void testMetadataMetricsOfCreateTable() throws Throwable {
+        String schemaName = generateUniqueName();
+        String tableName = generateUniqueName();
+        final String fullTableName = SchemaUtil.getTableName(schemaName, 
tableName);
+        try (PhoenixConnection conn = (PhoenixConnection) 
DriverManager.getConnection(getUrl())) {
+            MetricsMetadataSourceImpl metricsSource = 
(MetricsMetadataSourceImpl) 
MetricsMetadataSourceFactory.getMetadataMetricsSource();
+            DynamicMetricsRegistry registry = 
metricsSource.getMetricsRegistry();
+            long expectedCreateTableCount =
+                    
IndexMetricsIT.getCounterValueByName(MetricsMetadataSource.CREATE_TABLE_COUNT, 
registry);
+            long expectedCacheUsedSize =
+                    
IndexMetricsIT.getCounterValueByName(MetricsMetadataSource.METADATA_CACHE_ESTIMATED_USED_SIZE,
 registry);
+            long expectedCacheAddCount =
+                    
IndexMetricsIT.getCounterValueByName(MetricsMetadataSource.METADATA_CACHE_ADD_COUNT,
 registry);
+
+            String ddl = "CREATE TABLE " + fullTableName + "(k INTEGER PRIMARY 
KEY, v1 INTEGER, " +
+                    "v2 INTEGER) COLUMN_ENCODED_BYTES = 0, STORE_NULLS=true, 
GUIDE_POSTS_WIDTH=1000";
+            conn.createStatement().execute(ddl);
+
+            expectedCreateTableCount += 1;
+            
IndexMetricsIT.verifyCounterWithValue(MetricsMetadataSource.CREATE_TABLE_COUNT,
+                    registry, expectedCreateTableCount);
+
+            PTable table = conn.getTable(fullTableName);
+            expectedCacheUsedSize += table.getEstimatedSize();
+            
IndexMetricsIT.verifyCounterWithValue(MetricsMetadataSource.METADATA_CACHE_ESTIMATED_USED_SIZE,
+                    registry, expectedCacheUsedSize);
+
+            expectedCacheAddCount += 1;
+            
IndexMetricsIT.verifyCounterWithValue(MetricsMetadataSource.METADATA_CACHE_ADD_COUNT,
+                    registry, expectedCacheAddCount);
+        }
+    }
+
+    @Test
+    public void testMetadataMetricsOfDropTable() throws Throwable {
+        String schemaName = generateUniqueName();
+        String tableName = generateUniqueName();
+        final String fullTableName = SchemaUtil.getTableName(schemaName, 
tableName);
+        try (PhoenixConnection conn = (PhoenixConnection) 
DriverManager.getConnection(getUrl())) {
+            MetricsMetadataSourceImpl metricsSource = 
(MetricsMetadataSourceImpl) 
MetricsMetadataSourceFactory.getMetadataMetricsSource();
+            DynamicMetricsRegistry registry = 
metricsSource.getMetricsRegistry();
+
+            String ddl = "CREATE TABLE " + fullTableName + "(k INTEGER PRIMARY 
KEY, v1 INTEGER, " +
+                    "v2 INTEGER) COLUMN_ENCODED_BYTES = 0, STORE_NULLS=true, 
GUIDE_POSTS_WIDTH=1000";
+            conn.createStatement().execute(ddl);
+
+            long expectedDropTableCount =
+                    
IndexMetricsIT.getCounterValueByName(MetricsMetadataSource.DROP_TABLE_COUNT, 
registry);
+            long expectedCacheUsedSize =
+                    
IndexMetricsIT.getCounterValueByName(MetricsMetadataSource.METADATA_CACHE_ESTIMATED_USED_SIZE,
 registry);
+            PTable table = conn.getTable(fullTableName);
+
+            ddl = "DROP TABLE " + fullTableName;
+            conn.createStatement().execute(ddl);
+
+            expectedDropTableCount += 1;
+            
IndexMetricsIT.verifyCounterWithValue(MetricsMetadataSource.DROP_TABLE_COUNT,
+                    registry, expectedDropTableCount);
+
+            expectedCacheUsedSize -= table.getEstimatedSize();
+            
IndexMetricsIT.verifyCounterWithValue(MetricsMetadataSource.METADATA_CACHE_ESTIMATED_USED_SIZE,
+                    registry, expectedCacheUsedSize);
+        }
+    }
+
+    @Test
+    public void testMetadataMetricsOfAlterTableAddCol() throws Throwable {
+        String schemaName = generateUniqueName();
+        String tableName = generateUniqueName();
+        final String fullTableName = SchemaUtil.getTableName(schemaName, 
tableName);
+        try (PhoenixConnection conn = (PhoenixConnection) 
DriverManager.getConnection(getUrl())) {
+            MetricsMetadataSourceImpl metricsSource = 
(MetricsMetadataSourceImpl) 
MetricsMetadataSourceFactory.getMetadataMetricsSource();
+            DynamicMetricsRegistry registry = 
metricsSource.getMetricsRegistry();
+
+            String ddl = "CREATE TABLE " + fullTableName +
+                    "  (a_string varchar not null, col1 integer" +
+                    "  CONSTRAINT pk PRIMARY KEY (a_string)) ";
+            conn.createStatement().execute(ddl);
+
+            long expectedDropTableCount =
+                    
IndexMetricsIT.getCounterValueByName(MetricsMetadataSource.ALTER_ADD_COLUMN_COUNT,
 registry);
+            long prevCacheUsedSize =
+                    
IndexMetricsIT.getCounterValueByName(MetricsMetadataSource.METADATA_CACHE_ESTIMATED_USED_SIZE,
 registry);
+
+            ddl = "ALTER TABLE " + fullTableName + " ADD b_string VARCHAR  
NULL PRIMARY KEY  ";
+            conn.createStatement().execute(ddl);
+
+            expectedDropTableCount += 1;
+            
IndexMetricsIT.verifyCounterWithValue(MetricsMetadataSource.ALTER_ADD_COLUMN_COUNT,
+                    registry, expectedDropTableCount);
+
+            long currCacheUsedSize =
+                    
IndexMetricsIT.getCounterValueByName(MetricsMetadataSource.METADATA_CACHE_ESTIMATED_USED_SIZE,
 registry);
+            assertTrue(currCacheUsedSize >= prevCacheUsedSize);
+        }
+    }
 }
diff --git 
a/phoenix-core/src/it/java/org/apache/phoenix/monitoring/IndexMetricsIT.java 
b/phoenix-core/src/it/java/org/apache/phoenix/monitoring/IndexMetricsIT.java
index d6ea8145f6..f34f865cf9 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/monitoring/IndexMetricsIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/monitoring/IndexMetricsIT.java
@@ -206,6 +206,17 @@ public class IndexMetricsIT extends 
ParallelStatsDisabledIT {
         assertEquals(1, counter.value());
     }
 
+    public static void verifyCounterWithValue(String counterName, 
DynamicMetricsRegistry registry
+            , long expectedValue) {
+        MutableFastCounter counter = registry.getCounter(counterName, 0);
+        assertEquals(expectedValue, counter.value());
+    }
+
+    public static long getCounterValueByName(String counterName, 
DynamicMetricsRegistry registry) {
+        MutableFastCounter counter = registry.getCounter(counterName, 0);
+        return counter.value();
+    }
+
     private String getTableCounterName(String baseCounterName) {
         return baseCounterName + "." + TABLE_NAME;
     }
diff --git 
a/phoenix-core/src/it/java/org/apache/phoenix/monitoring/MetadataMetricsIT.java 
b/phoenix-core/src/it/java/org/apache/phoenix/monitoring/MetadataMetricsIT.java
index ad0cc917bb..0b8fbf2abb 100644
--- 
a/phoenix-core/src/it/java/org/apache/phoenix/monitoring/MetadataMetricsIT.java
+++ 
b/phoenix-core/src/it/java/org/apache/phoenix/monitoring/MetadataMetricsIT.java
@@ -189,4 +189,49 @@ public class MetadataMetricsIT extends 
ParallelStatsDisabledIT {
         metadataSource.incrementDropFunctionCount();
         
IndexMetricsIT.verifyCounter(MetricsMetadataSource.DROP_FUNCTION_COUNT, 
registry);
     }
+
+    @Test
+    public void testMetadataCacheUsedSizeMetrics() {
+        MetricsMetadataSourceImpl metadataSource = new 
MetricsMetadataSourceImpl();
+        DynamicMetricsRegistry registry = metadataSource.getMetricsRegistry();
+
+        int estimatedSizeOfTable1 = 1000;
+        int estimatedSizeOfTable2 = 500;
+        metadataSource.incrementMetadataCacheUsedSize(estimatedSizeOfTable1);
+        
IndexMetricsIT.verifyCounterWithValue(MetricsMetadataSource.METADATA_CACHE_ESTIMATED_USED_SIZE,
+                registry, estimatedSizeOfTable1);
+
+        metadataSource.incrementMetadataCacheUsedSize(estimatedSizeOfTable2);
+        
IndexMetricsIT.verifyCounterWithValue(MetricsMetadataSource.METADATA_CACHE_ESTIMATED_USED_SIZE,
+                registry, estimatedSizeOfTable1 + estimatedSizeOfTable2);
+
+        metadataSource.decrementMetadataCacheUsedSize(estimatedSizeOfTable1);
+        
IndexMetricsIT.verifyCounterWithValue(MetricsMetadataSource.METADATA_CACHE_ESTIMATED_USED_SIZE,
+                registry, estimatedSizeOfTable2);
+
+        metadataSource.decrementMetadataCacheUsedSize(estimatedSizeOfTable2);
+        
IndexMetricsIT.verifyCounterWithValue(MetricsMetadataSource.METADATA_CACHE_ESTIMATED_USED_SIZE,
+                registry, 0);
+    }
+
+    @Test
+    public void testMetadataCacheCountMetrics() {
+        MetricsMetadataSourceImpl metadataSource = new 
MetricsMetadataSourceImpl();
+        DynamicMetricsRegistry registry = metadataSource.getMetricsRegistry();
+
+        metadataSource.incrementMetadataCacheHitCount();
+        
IndexMetricsIT.verifyCounter(MetricsMetadataSource.METADATA_CACHE_HIT_COUNT, 
registry);
+
+        metadataSource.incrementMetadataCacheMissCount();
+        
IndexMetricsIT.verifyCounter(MetricsMetadataSource.METADATA_CACHE_MISS_COUNT, 
registry);
+
+        metadataSource.incrementMetadataCacheEvictionCount();
+        
IndexMetricsIT.verifyCounter(MetricsMetadataSource.METADATA_CACHE_EVICTION_COUNT,
 registry);
+
+        metadataSource.incrementMetadataCacheRemovalCount();
+        
IndexMetricsIT.verifyCounter(MetricsMetadataSource.METADATA_CACHE_REMOVAL_COUNT,
 registry);
+
+        metadataSource.incrementMetadataCacheAddCount();
+        
IndexMetricsIT.verifyCounter(MetricsMetadataSource.METADATA_CACHE_ADD_COUNT, 
registry);
+    }
 }
diff --git 
a/phoenix-core/src/it/java/org/apache/phoenix/query/MetaDataCacheMetricsIT.java 
b/phoenix-core/src/it/java/org/apache/phoenix/query/MetaDataCacheMetricsIT.java
new file mode 100644
index 0000000000..7ceaac8413
--- /dev/null
+++ 
b/phoenix-core/src/it/java/org/apache/phoenix/query/MetaDataCacheMetricsIT.java
@@ -0,0 +1,103 @@
+/*
+ * 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.phoenix.query;
+
+import org.apache.phoenix.end2end.NeedsOwnMiniClusterTest;
+import org.apache.phoenix.jdbc.PhoenixConnection;
+import org.apache.phoenix.monitoring.GlobalClientMetrics;
+import org.apache.phoenix.util.RunUntilFailure;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.junit.runner.RunWith;
+
+import java.sql.DriverManager;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+@RunWith(RunUntilFailure.class)
+@Category(NeedsOwnMiniClusterTest.class)
+public class MetaDataCacheMetricsIT extends MetaDataCachingIT {
+
+    @Test
+    public void testGlobalClientCacheMetricsOfCreateAndDropTable() throws 
Exception {
+        
GlobalClientMetrics.GLOBAL_CLIENT_METADATA_CACHE_ADD_COUNTER.getMetric().reset();
+        
GlobalClientMetrics.GLOBAL_CLIENT_METADATA_CACHE_REMOVAL_COUNTER.getMetric().reset();
+        
GlobalClientMetrics.GLOBAL_CLIENT_METADATA_CACHE_ESTIMATED_USED_SIZE.getMetric().reset();
+
+        String tableName = generateUniqueName();
+        try (PhoenixConnection conn = (PhoenixConnection) 
DriverManager.getConnection(getUrl())) {
+            long prevCacheAddCount =
+                    
GlobalClientMetrics.GLOBAL_CLIENT_METADATA_CACHE_ADD_COUNTER.getMetric()
+                            .getValue();
+            long prevEstimatedUsedCacheSize =
+                    
GlobalClientMetrics.GLOBAL_CLIENT_METADATA_CACHE_ESTIMATED_USED_SIZE.getMetric()
+                            .getValue();
+            createTable(conn, tableName, 0);
+
+            assertEquals("Incorrect number of client metadata cache adds",
+                    prevCacheAddCount + 1,
+                    
GlobalClientMetrics.GLOBAL_CLIENT_METADATA_CACHE_ADD_COUNTER.getMetric()
+                            .getValue());
+
+            long currEstimatedUsedCacheSize =
+                    
GlobalClientMetrics.GLOBAL_CLIENT_METADATA_CACHE_ESTIMATED_USED_SIZE.getMetric()
+                            .getValue();
+            int tableEstimatedSize = 
conn.getTable(tableName).getEstimatedSize();
+            assertTrue(String.format("Incorrect estimated used size of client 
metadata cache " +
+                                    "after creating table %s: 
tableEstimatedSize=%d, " +
+                                    "prevEstimatedUsedCacheSize=%d,  " +
+                                    "currEstimatedUsedCacheSize=%d", tableName,
+                            tableEstimatedSize, prevEstimatedUsedCacheSize, 
currEstimatedUsedCacheSize),
+                    currEstimatedUsedCacheSize >= prevEstimatedUsedCacheSize + 
tableEstimatedSize);
+
+            long prevCacheRemovalCount =
+                    
GlobalClientMetrics.GLOBAL_CLIENT_METADATA_CACHE_REMOVAL_COUNTER.getMetric()
+                            .getValue();
+            prevEstimatedUsedCacheSize = currEstimatedUsedCacheSize;
+
+            conn.createStatement().execute("DROP TABLE " + tableName);
+
+            currEstimatedUsedCacheSize =
+                    
GlobalClientMetrics.GLOBAL_CLIENT_METADATA_CACHE_ESTIMATED_USED_SIZE.getMetric()
+                            .getValue();
+            assertEquals("Incorrect number of client metadata cache removals",
+                    prevCacheRemovalCount + 1,
+                    
GlobalClientMetrics.GLOBAL_CLIENT_METADATA_CACHE_REMOVAL_COUNTER.getMetric()
+                            .getValue());
+            assertTrue(String.format("Incorrect estimated used size of client 
metadata cache " +
+                                    "after dropping table %s: 
tableEstimatedSize=%d, " +
+                                    "prevEstimatedUsedCacheSize=%d, " +
+                                    "currEstimatedUsedCacheSize=%d", tableName,
+                            tableEstimatedSize, prevEstimatedUsedCacheSize, 
currEstimatedUsedCacheSize),
+                    currEstimatedUsedCacheSize < prevEstimatedUsedCacheSize
+                            && currEstimatedUsedCacheSize >=
+                            prevEstimatedUsedCacheSize - tableEstimatedSize);
+        }
+    }
+
+    @Test
+    public void testSystemTablesAreInCache() throws Exception {
+        // no-op
+    }
+
+    @Test
+    public void testGlobalClientCacheMetrics() throws Exception {
+        // no-op
+    }
+}
\ No newline at end of file
diff --git 
a/phoenix-core/src/it/java/org/apache/phoenix/query/MetaDataCachingIT.java 
b/phoenix-core/src/it/java/org/apache/phoenix/query/MetaDataCachingIT.java
index 06cc176fa0..f091488ea1 100644
--- a/phoenix-core/src/it/java/org/apache/phoenix/query/MetaDataCachingIT.java
+++ b/phoenix-core/src/it/java/org/apache/phoenix/query/MetaDataCachingIT.java
@@ -66,7 +66,7 @@ public class MetaDataCachingIT extends BaseTest {
         setUpTestDriver(new ReadOnlyProps(props.entrySet().iterator()));
     }
 
-    private void createTable(Connection conn, String tableName, long 
updateCacheFrequency) throws SQLException {
+    protected void createTable(Connection conn, String tableName, long 
updateCacheFrequency) throws SQLException {
         conn.createStatement().execute("CREATE TABLE " + tableName
                 + "(k INTEGER NOT NULL PRIMARY KEY, v1 INTEGER, v2 INTEGER, v3 
VARCHAR, v4 Date, "
                 +" v5 BIGINT, v6 SMALLINT)" + (updateCacheFrequency == 0 ? "" 
: "UPDATE_CACHE_FREQUENCY="+updateCacheFrequency));


Reply via email to