ranganathg commented on code in PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#discussion_r1338315584


##########
phoenix-core/src/main/java/org/apache/phoenix/monitoring/connqueryservice/ConnectionQueryServicesMetricsManager.java:
##########
@@ -0,0 +1,375 @@
+/*
+ * 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.monitoring.connqueryservice;
+
+import org.apache.hadoop.hbase.util.EnvironmentEdgeManager;
+import org.apache.phoenix.monitoring.HistogramDistribution;
+import org.apache.phoenix.monitoring.MetricPublisherSupplierFactory;
+import org.apache.phoenix.monitoring.MetricServiceResolver;
+import org.apache.phoenix.monitoring.MetricType;
+import org.apache.phoenix.monitoring.PhoenixConnectionProfileMetric;
+import org.apache.phoenix.query.QueryServicesOptions;
+import 
org.apache.phoenix.thirdparty.com.google.common.annotations.VisibleForTesting;
+import org.apache.phoenix.thirdparty.com.google.common.base.Strings;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.ConcurrentMap;
+
+/**
+ * Central place where we keep track of all the Connection Query Service 
metrics. Register each
+ * Connection Query Service and store the instance of it associated with 
ConnectionServiceName in a
+ * map This class exposes following functions as static methods to help catch 
all exception
+ * 1.clearConnectionQueryServiceMetrics
+ * 2.getTableMetricsMethod
+ * 3.pushMetricsFromConnInstanceMethod
+ * 4.updateMetricsMethod
+ */
+public class ConnectionQueryServicesMetricsManager {
+    private static final Logger LOGGER =
+            
LoggerFactory.getLogger(ConnectionQueryServicesMetricsManager.class);
+    private static boolean isConnectionQueryServiceMetricsEnabled;
+    private static boolean isConnectionQueryServiceMetricPublisherEnabled;
+    private static ConcurrentMap<String, PhoenixConnectionQueryServiceMetrics>
+            connectionQueryServiceMetricsMapping;
+    // Singleton object
+    private static volatile ConnectionQueryServicesMetricsManager
+            connectionQueryServicesMetricsManager = null;
+    private static volatile MetricPublisherSupplierFactory mPublisher = null;
+    private static volatile QueryServicesOptions options;
+
+    @VisibleForTesting
+    public ConnectionQueryServicesMetricsManager(QueryServicesOptions opts) {
+        options = opts;
+        isConnectionQueryServiceMetricsEnabled = 
options.isConnectionQueryServiceMetricsEnabled();
+        connectionQueryServiceMetricsMapping = new ConcurrentHashMap<>();
+        LOGGER.info("Phoenix connection query service metrics enabled status: "
+                + isConnectionQueryServiceMetricsEnabled);
+        // Return simply if connection query service metrics are not enabled
+        if (!isConnectionQueryServiceMetricsEnabled) {
+            return;
+        }
+        isConnectionQueryServiceMetricPublisherEnabled =
+                options.isConnectionQueryServiceMetricsPublisherEnabled();
+        LOGGER.info("Phoenix connection query service metrics publisher 
enabled status: "
+                + isConnectionQueryServiceMetricPublisherEnabled);
+    }
+
+    @VisibleForTesting
+    public static void setInstance(ConnectionQueryServicesMetricsManager 
metricsManager) {
+        connectionQueryServicesMetricsManager = metricsManager;
+    }
+
+    /**
+     * Method to provide instance of 
ConnectionQueryServiceMetricsManager(Create if needed in
+     * thread safe manner)
+     * @return returns instance of ConnectionQueryServicesMetricsManager for 
the said
+     *         ConnectionQueryService
+     */
+    public static ConnectionQueryServicesMetricsManager getInstance() {
+        ConnectionQueryServicesMetricsManager localRef = 
connectionQueryServicesMetricsManager;
+
+        if (localRef == null) {
+            synchronized (ConnectionQueryServicesMetricsManager.class) {
+                localRef = connectionQueryServicesMetricsManager;
+                if (localRef == null) {
+                    QueryServicesOptions options = 
QueryServicesOptions.withDefaults();
+                    localRef = connectionQueryServicesMetricsManager =
+                            new ConnectionQueryServicesMetricsManager(options);
+                    LOGGER.info("connection query service created object for 
metrics manager");
+                    if (isConnectionQueryServiceMetricsEnabled
+                            && isConnectionQueryServiceMetricPublisherEnabled) 
{
+                        String className = 
options.getConnectionProfileMetricsPublisherClass();
+                        if (className != null) {
+                            MetricServiceResolver mResolver = new 
MetricServiceResolver();
+                            LOGGER.info(String.format(
+                                    "connection query service metrics 
publisher className %s",
+                                    className));
+                            try {
+                                mPublisher = mResolver.instantiate(className);
+                                mPublisher.registerMetricProvider();
+                            } catch (Throwable e) {
+                                LOGGER.error("The exception from metric 
publish Function", e);
+                            }
+
+                        } else {
+                            LOGGER.warn("connection query service metrics 
publisher className"
+                                    + " can't be null");
+                        }
+                    }
+                }
+            }
+        }
+        return localRef;
+    }
+
+    private PhoenixConnectionQueryServiceMetrics 
getConnectionQueryServiceMetricsInstance(
+            String connectionQueryServiceName) {
+        if (!isConnectionQueryServiceMetricsEnabled) {
+            return null;
+        }
+        if (Strings.isNullOrEmpty(connectionQueryServiceName)) {
+            LOGGER.warn("connection query service Name can't be null or 
empty");
+            return null;
+        }
+
+        PhoenixConnectionQueryServiceMetrics tInstance;
+        tInstance = 
connectionQueryServiceMetricsMapping.get(connectionQueryServiceName);
+        if (tInstance == null) {
+            synchronized (ConnectionQueryServicesMetricsManager.class) {
+                tInstance = 
connectionQueryServiceMetricsMapping.get(connectionQueryServiceName);
+                if (tInstance == null) {
+
+                    LOGGER.info(String.format("connection query service 
metrics creating object"
+                            + " for connection query service: %s", 
connectionQueryServiceName));
+                    tInstance =
+                            new 
PhoenixConnectionQueryServiceMetrics(connectionQueryServiceName,
+                                    options.getConfiguration());
+                    
connectionQueryServiceMetricsMapping.put(connectionQueryServiceName, tInstance);
+                }
+            }
+        }
+        return tInstance;
+    }
+
+    /**
+     * This function will be used to add individual MetricType to LocalStore. 
Also this will serve
+     * as LocalStore to store connection query service metrics before their 
current value is added
+     * to histogram.
+     * This func is only used for metrics which are counter based, where 
values increases or
+     * decreases frequently. Like Open Conn Counter. This function will first 
retrieve it's current
+     * value and increment or decrement (by +/-1) it as required then update 
the new values.
+     * <br>
+     * Example :- OPEN_PHOENIX_CONNECTIONS_COUNTER, 
OPEN_INTERNAL_PHOENIX_CONNECTIONS_COUNTER
+     * <br>
+     * <br>
+     * histogram will update with each increment/decrement.
+     * @param connectionQueryServiceName
+     * @param type
+     * @param value
+     */
+    private void updateMetricsAsCounter(String connectionQueryServiceName, 
MetricType type,
+            long value) {
+
+        long startTime = EnvironmentEdgeManager.currentTime();
+
+        PhoenixConnectionQueryServiceMetrics
+                tInstance =
+                
getConnectionQueryServiceMetricsInstance(connectionQueryServiceName);
+        if (tInstance == null) {
+            LOGGER.trace("Phoenix connection query service metrics are 
disabled for connection"
+                    + " query service: " + connectionQueryServiceName);
+            return;
+        }
+        tInstance.changeMetricValue(type, value);
+
+        if (LOGGER.isDebugEnabled()) {
+            LOGGER.debug(String.format("Phoenix connection query service 
metrics completed"
+                            + " updating metric" + " %s to value %s, timetaken 
= %s",
+                            type, value, EnvironmentEdgeManager.currentTime() 
- startTime));
+        }
+    }
+
+    /**
+     * static methods to push, update or retrieve ConnectionQueryService 
Metrics.
+     * @param connectionQueryServiceName name of the connection query service
+     * @param type                       type of metric
+     * @param value                      metric value
+     */
+    public static void updateMetricsMethod(String connectionQueryServiceName, 
MetricType type,
+            long value) {
+        try {
+            ConnectionQueryServicesMetricsManager.getInstance()
+                    .updateMetricsAsCounter(connectionQueryServiceName, type, 
value);
+        } catch (Exception e) {
+            LOGGER.error("Failed updating Phoenix connection query service 
metrics", e);
+        }
+    }
+
+    /**
+     * Publish the metrics to wherever you want them published.
+     * @return map of conneciotn query service name -> 
ConnectionQueryServiceMetrics
+     */
+    private Map<String, List<PhoenixConnectionProfileMetric>> 
getConnectionQueryServiceMetrics() {
+
+        long startTime = EnvironmentEdgeManager.currentTime();
+        Map<String, List<PhoenixConnectionProfileMetric>> map = new 
HashMap<>();
+        for (Map.Entry<String, PhoenixConnectionQueryServiceMetrics> entry
+                : connectionQueryServiceMetricsMapping.entrySet()) {
+            map.put(entry.getKey(), entry.getValue().getMetricMap());
+        }
+        long timeTakenForMetricConversion = 
EnvironmentEdgeManager.currentTime() - startTime;
+        LOGGER.info(String.format(
+                "Phoenix connection query service metrics fetching complete, 
timeTaken: %d",
+                + timeTakenForMetricConversion));
+        return map;
+    }
+
+    /**
+     * This method will return all the counters for Phoenix connection query 
service.
+     * @return Map of all ConnectionQueryService Metrics.
+     */
+    public static Map<String, List<PhoenixConnectionProfileMetric>> 
getConnectionQueryServicesMetricsMethod() {

Review Comment:
   getConnectionQueryServicesMetrics?



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to