You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@phoenix.apache.org by "mihir6692 (via GitHub)" <gi...@apache.org> on 2023/09/20 22:41:00 UTC

[GitHub] [phoenix] mihir6692 opened a new pull request, #1682: PHOENIX-7038 : Implement Phoenix Connection Profile Metrics

mihir6692 opened a new pull request, #1682:
URL: https://github.com/apache/phoenix/pull/1682

   (no comment)


-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [phoenix] ranganathg commented on a diff in pull request #1682: PHOENIX-7038 : Implement Phoenix Connection Profile Metrics

Posted by "ranganathg (via GitHub)" <gi...@apache.org>.
ranganathg commented on code in PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#discussion_r1338306192


##########
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);

Review Comment:
   can this happen?



-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [phoenix] ranganathg commented on a diff in pull request #1682: PHOENIX-7038 : Implement Phoenix Connection Profile Metrics

Posted by "ranganathg (via GitHub)" <gi...@apache.org>.
ranganathg commented on code in PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#discussion_r1338328908


##########
phoenix-core/src/test/java/org/apache/phoenix/monitoring/connqueryservice/ConnectionQueryServiceNameMetricsGenerateAndVerifyTestData.java:
##########
@@ -0,0 +1,82 @@
+/**
+ * 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.phoenix.monitoring.MetricType;
+import org.apache.phoenix.monitoring.PhoenixConnectionProfileMetric;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.apache.phoenix.monitoring.MetricType.OPEN_INTERNAL_PHOENIX_CONNECTIONS_COUNTER;
+import static org.apache.phoenix.monitoring.MetricType.OPEN_PHOENIX_CONNECTIONS_COUNTER;
+import static org.apache.phoenix.monitoring.MetricType.PHOENIX_CONNECTIONS_THROTTLED_COUNTER;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * This class is used primarily to populate data and
+ * verification methods
+ */
+
+public class ConnectionQueryServiceNameMetricsGenerateAndVerifyTestData {
+
+    public static final String[] connectionQueryServiceNames = { "USE_CASE_1", "USE_CASE_2", "USE_CASE_3" };
+    public static Map<String, Map<MetricType, Long>>[] connectionQueryServiceNameMetricMap = new Map[connectionQueryServiceNames.length];
+    public static final long[] openPhoenixConnCounter = { 1, 1, 1 };
+    public static final long[] openInternalPhoenixConnCounter = { 1, 1, 1 };
+    public static final long[] phoenixConnThrottledCounter = { 1, 2, 3 };
+
+
+    public void populateMetrics() {
+        for (int i = 0; i < connectionQueryServiceNameMetricMap.length; i++) {
+            connectionQueryServiceNameMetricMap[i] = new HashMap<>();
+        }
+        for (int i = 0; i < connectionQueryServiceNames.length; i++) {
+            Map<MetricType, Long> metrics = new HashMap<>();
+            metrics.put(OPEN_PHOENIX_CONNECTIONS_COUNTER, openPhoenixConnCounter[i]);
+            metrics.put(OPEN_INTERNAL_PHOENIX_CONNECTIONS_COUNTER, openInternalPhoenixConnCounter[i]);
+            metrics.put(PHOENIX_CONNECTIONS_THROTTLED_COUNTER, phoenixConnThrottledCounter[i]);
+
+            connectionQueryServiceNameMetricMap[i].put(connectionQueryServiceNames[i], metrics);
+        }
+    }
+
+    public void verfyCountOfConnecitonQueryServices(int noOfConnectionQueryServiceName) {

Review Comment:
   typo verifyCountOfConnectionQueryServices



-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [phoenix] ranganathg commented on a diff in pull request #1682: PHOENIX-7038 : Implement Phoenix Connection Profile Metrics

Posted by "ranganathg (via GitHub)" <gi...@apache.org>.
ranganathg commented on code in PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#discussion_r1338335686


##########
phoenix-core/src/test/java/org/apache/phoenix/monitoring/connqueryservice/PhoenixConnectionQueryServiceMetricsTest.java:
##########
@@ -0,0 +1,99 @@
+/**
+ * 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.conf.Configuration;
+import org.apache.phoenix.monitoring.MetricType;
+import org.apache.phoenix.monitoring.PhoenixConnectionProfileMetric;
+import org.junit.Test;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+
+import static org.apache.phoenix.monitoring.connqueryservice.ConnectionQueryServiceNameMetricsGenerateAndVerifyTestData.openInternalPhoenixConnCounter;
+import static org.apache.phoenix.monitoring.connqueryservice.ConnectionQueryServiceNameMetricsGenerateAndVerifyTestData.openPhoenixConnCounter;
+import static org.apache.phoenix.monitoring.connqueryservice.ConnectionQueryServiceNameMetricsGenerateAndVerifyTestData.connectionQueryServiceNames;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class PhoenixConnectionQueryServiceMetricsTest {
+    static Map<String, PhoenixConnectionQueryServiceMetrics> phoxnixConnectionQueryServiceSet = new HashMap<>();

Review Comment:
   nit:phoenixConnectionQueryServiceSet ?



-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] PHOENIX-7038 : Implement Connection Query Service Metrics [phoenix]

Posted by "mihir6692 (via GitHub)" <gi...@apache.org>.
mihir6692 commented on code in PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#discussion_r1374843852


##########
phoenix-core/src/test/java/org/apache/phoenix/monitoring/connqueryservice/ConnectionQueryServiceNameMetricsGenerateAndVerifyTestData.java:
##########
@@ -0,0 +1,82 @@
+/**
+ * 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.phoenix.monitoring.MetricType;
+import org.apache.phoenix.monitoring.PhoenixConnectionProfileMetric;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.apache.phoenix.monitoring.MetricType.OPEN_INTERNAL_PHOENIX_CONNECTIONS_COUNTER;
+import static org.apache.phoenix.monitoring.MetricType.OPEN_PHOENIX_CONNECTIONS_COUNTER;
+import static org.apache.phoenix.monitoring.MetricType.PHOENIX_CONNECTIONS_THROTTLED_COUNTER;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * This class is used primarily to populate data and
+ * verification methods
+ */
+
+public class ConnectionQueryServiceNameMetricsGenerateAndVerifyTestData {
+
+    public static final String[] connectionQueryServiceNames = { "USE_CASE_1", "USE_CASE_2", "USE_CASE_3" };
+    public static Map<String, Map<MetricType, Long>>[] connectionQueryServiceNameMetricMap = new Map[connectionQueryServiceNames.length];
+    public static final long[] openPhoenixConnCounter = { 1, 1, 1 };
+    public static final long[] openInternalPhoenixConnCounter = { 1, 1, 1 };
+    public static final long[] phoenixConnThrottledCounter = { 1, 2, 3 };
+
+
+    public void populateMetrics() {
+        for (int i = 0; i < connectionQueryServiceNameMetricMap.length; i++) {
+            connectionQueryServiceNameMetricMap[i] = new HashMap<>();
+        }
+        for (int i = 0; i < connectionQueryServiceNames.length; i++) {
+            Map<MetricType, Long> metrics = new HashMap<>();
+            metrics.put(OPEN_PHOENIX_CONNECTIONS_COUNTER, openPhoenixConnCounter[i]);
+            metrics.put(OPEN_INTERNAL_PHOENIX_CONNECTIONS_COUNTER, openInternalPhoenixConnCounter[i]);
+            metrics.put(PHOENIX_CONNECTIONS_THROTTLED_COUNTER, phoenixConnThrottledCounter[i]);
+
+            connectionQueryServiceNameMetricMap[i].put(connectionQueryServiceNames[i], metrics);
+        }
+    }
+
+    public void verfyCountOfConnecitonQueryServices(int noOfConnectionQueryServiceName) {

Review Comment:
   Done



##########
phoenix-core/src/test/java/org/apache/phoenix/monitoring/connqueryservice/PhoenixConnectionQueryServiceMetricsTest.java:
##########
@@ -0,0 +1,99 @@
+/**
+ * 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.conf.Configuration;
+import org.apache.phoenix.monitoring.MetricType;
+import org.apache.phoenix.monitoring.PhoenixConnectionProfileMetric;
+import org.junit.Test;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+
+import static org.apache.phoenix.monitoring.connqueryservice.ConnectionQueryServiceNameMetricsGenerateAndVerifyTestData.openInternalPhoenixConnCounter;
+import static org.apache.phoenix.monitoring.connqueryservice.ConnectionQueryServiceNameMetricsGenerateAndVerifyTestData.openPhoenixConnCounter;
+import static org.apache.phoenix.monitoring.connqueryservice.ConnectionQueryServiceNameMetricsGenerateAndVerifyTestData.connectionQueryServiceNames;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class PhoenixConnectionQueryServiceMetricsTest {
+    static Map<String, PhoenixConnectionQueryServiceMetrics> phoxnixConnectionQueryServiceSet = new HashMap<>();
+
+    public boolean verifyConnectionQueryServiceName() {
+
+        if (phoxnixConnectionQueryServiceSet.isEmpty()) {
+            return false;
+        }
+        for (String connectionQueryServiceName : connectionQueryServiceNames) {
+            PhoenixConnectionQueryServiceMetrics instance = phoxnixConnectionQueryServiceSet.get(connectionQueryServiceName);
+            if (!instance.getConnectionQueryServiceName().equals(connectionQueryServiceName)) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    public void verifyMetricsFromPhoenixConnectionQueryServiceMetrics() {
+        assertFalse(phoxnixConnectionQueryServiceSet.isEmpty());
+        for (int i = 0; i < connectionQueryServiceNames.length; i++) {
+            PhoenixConnectionQueryServiceMetrics instance = phoxnixConnectionQueryServiceSet.get(connectionQueryServiceNames[i]);
+            assertEquals(instance.getConnectionQueryServiceName(), connectionQueryServiceNames[i]);
+            List<PhoenixConnectionProfileMetric> metricList = instance.getMetricMap();
+            for (PhoenixConnectionProfileMetric metric : metricList) {
+
+                if (metric.getMetricType().equals(MetricType.OPEN_INTERNAL_PHOENIX_CONNECTIONS_COUNTER)) {
+                    assertEquals(openInternalPhoenixConnCounter[i], metric.getValue());
+                }
+                if (metric.getMetricType().equals(MetricType.OPEN_PHOENIX_CONNECTIONS_COUNTER)) {
+                    assertEquals(openPhoenixConnCounter[i], metric.getValue());
+                }
+            }
+        }
+    }
+
+    @Test
+    public void testPhoenixConnectionQueryServiceMetricsforPhoenixConnectionQueryServiceName() {
+        Configuration conf = new Configuration();
+        for (int i = 0; i < connectionQueryServiceNames.length; i++) {
+            PhoenixConnectionQueryServiceMetrics instance = new PhoenixConnectionQueryServiceMetrics(connectionQueryServiceNames[i], conf);
+            phoxnixConnectionQueryServiceSet.put(connectionQueryServiceNames[i], instance);
+        }
+        assertTrue(verifyConnectionQueryServiceName());
+    }
+
+    /**
+     * This test is for changeMetricValue() Method and getMetricMap()
+     */
+    @Test
+    public void testPhoenixconnectionQueryServiceMetrics() {

Review Comment:
   done



##########
phoenix-core/src/test/java/org/apache/phoenix/monitoring/connqueryservice/PhoenixConnectionQueryServiceMetricsTest.java:
##########
@@ -0,0 +1,99 @@
+/**
+ * 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.conf.Configuration;
+import org.apache.phoenix.monitoring.MetricType;
+import org.apache.phoenix.monitoring.PhoenixConnectionProfileMetric;
+import org.junit.Test;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+
+import static org.apache.phoenix.monitoring.connqueryservice.ConnectionQueryServiceNameMetricsGenerateAndVerifyTestData.openInternalPhoenixConnCounter;
+import static org.apache.phoenix.monitoring.connqueryservice.ConnectionQueryServiceNameMetricsGenerateAndVerifyTestData.openPhoenixConnCounter;
+import static org.apache.phoenix.monitoring.connqueryservice.ConnectionQueryServiceNameMetricsGenerateAndVerifyTestData.connectionQueryServiceNames;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class PhoenixConnectionQueryServiceMetricsTest {
+    static Map<String, PhoenixConnectionQueryServiceMetrics> phoxnixConnectionQueryServiceSet = new HashMap<>();

Review Comment:
   done



-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] PHOENIX-7038 : Implement Phoenix Connection Profile Metrics [phoenix]

Posted by "stoty (via GitHub)" <gi...@apache.org>.
stoty commented on PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#issuecomment-1758877367

   I am in general very wary of overriding HBase properties from a Phoenix application, as this can very easily lead to failures for properties that must be in sync across the cluster for HBase to work.
   
   In my ideal world we'd have some kind of whitelist for properties that are known to be safe to override (or an inverse blacklist)
   (Of course users could still wreak havoc by modifying the hbase-site.xml used by the client)
   
   I would tie the id propagation part to https://issues.apache.org/jira/browse/PHOENIX-5974.
   We could either correlate that single ID with the ConnectionInfo/profile (though it requires extra bookeepeing), or just add it alongside the query id.
   


-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] PHOENIX-7038 : Implement Connection Query Service Metrics [phoenix]

Posted by "mihir6692 (via GitHub)" <gi...@apache.org>.
mihir6692 commented on code in PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#discussion_r1374844085


##########
phoenix-core/src/main/java/org/apache/phoenix/query/QueryServicesOptions.java:
##########
@@ -28,6 +28,10 @@
 import static org.apache.phoenix.query.QueryServices.CLIENT_SPOOL_THRESHOLD_BYTES_ATTRIB;
 import static org.apache.phoenix.query.QueryServices.COLLECT_REQUEST_LEVEL_METRICS;
 import static org.apache.phoenix.query.QueryServices.COMMIT_STATS_ASYNC;
+import static org.apache.phoenix.query.QueryServices.CONNECTION_QUERY_SERVICE_HISTOGRAM_SIZE_RANGES;
+import static org.apache.phoenix.query.QueryServices.CONNECTION_QUERY_SERVICE_METRICS_ENABLED;
+import static org.apache.phoenix.query.QueryServices.CONNECTION_QUERY_SERVICE_METRICS_PUBLISHER_ENABLED;
+import static org.apache.phoenix.query.QueryServices.CONNECTION_QUERY_SERVICE_METRICS_PUBLISH_CLASS_NAME;

Review Comment:
   Updated.



-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] PHOENIX-7038 : Implement Connection Query Service Metrics [phoenix]

Posted by "mihir6692 (via GitHub)" <gi...@apache.org>.
mihir6692 commented on PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#issuecomment-1783400093

   > I am in general very wary of overriding HBase properties from a Phoenix application, as this can very easily lead to failures for properties that must be in sync across the cluster for HBase to work.
   > 
   > In my ideal world we'd have some kind of whitelist for properties that are known to be safe to override (or an inverse blacklist)
   > (Of course users could still wreak havoc by modifying the hbase-site.xml used by the client)
   > 
   > I would tie the id propagation part to https://issues.apache.org/jira/browse/PHOENIX-5974.
   > We could either correlate that single ID with the ConnectionInfo/profile (though it requires extra bookeepeing), or just add it alongside the query id.
   
   QUERY_SERVICE_NAME config property is only for Phoenix Connection and for metrics related to CSQI. It won't be used anywhere in HBase layer. As far as I understand, [PHOENIX-5974](https://issues.apache.org/jira/browse/PHOENIX-5974) is different from this JIRA and having a default name for CQSI Object.
   
   @stoty 


-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] PHOENIX-7038 : Implement Phoenix Connection Profile Metrics [phoenix]

Posted by "jpisaac (via GitHub)" <gi...@apache.org>.
jpisaac commented on code in PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#discussion_r1355852348


##########
phoenix-core/src/it/java/org/apache/phoenix/monitoring/connqueryservice/PhoenixConnectionQueryServiceMetrcisIT.java:
##########
@@ -0,0 +1,357 @@
+/*
+ * 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.conf.Configuration;
+import org.apache.hadoop.hbase.HBaseConfiguration;
+import org.apache.hadoop.hbase.HBaseTestingUtility;
+import org.apache.phoenix.end2end.NeedsOwnMiniClusterTest;
+import org.apache.phoenix.jdbc.PhoenixConnection;
+import org.apache.phoenix.jdbc.PhoenixDriver;
+import org.apache.phoenix.monitoring.HistogramDistribution;
+import org.apache.phoenix.monitoring.Metric;
+import org.apache.phoenix.monitoring.MetricType;
+import org.apache.phoenix.monitoring.PhoenixConnectionProfileMetric;
+import org.apache.phoenix.query.BaseTest;
+import org.apache.phoenix.query.ConfigurationFactory;
+import org.apache.phoenix.query.HBaseFactoryProvider;
+import org.apache.phoenix.query.QueryServices;
+import org.apache.phoenix.query.QueryServicesOptions;
+import org.apache.phoenix.util.InstanceResolver;
+import org.apache.phoenix.util.PhoenixRuntime;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.Statement;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static org.apache.phoenix.monitoring.MetricType.OPEN_INTERNAL_PHOENIX_CONNECTIONS_COUNTER;
+import static org.apache.phoenix.monitoring.MetricType.OPEN_PHOENIX_CONNECTIONS_COUNTER;
+import static org.apache.phoenix.monitoring.MetricType.PHOENIX_CONNECTIONS_THROTTLED_COUNTER;
+import static org.apache.phoenix.query.QueryServices.CLIENT_CONNECTION_MAX_ALLOWED_CONNECTIONS;
+import static org.apache.phoenix.query.QueryServices.CONNECTION_QUERY_SERVICE_METRICS_ENABLED;
+import static org.apache.phoenix.query.QueryServices.INTERNAL_CONNECTION_MAX_ALLOWED_CONNECTIONS;
+import static org.apache.phoenix.query.QueryServices.QUERY_SERVICES_NAME;
+import static org.apache.phoenix.util.PhoenixRuntime.clearAllConnectionQueryServiceMetrics;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+@Category(NeedsOwnMiniClusterTest.class)
+public class PhoenixConnectionQueryServiceMetrcisIT extends BaseTest {

Review Comment:
   nit: Typo in classname



##########
phoenix-core/src/main/java/org/apache/phoenix/monitoring/PhoenixConnectionProfileMetric.java:
##########
@@ -0,0 +1,38 @@
+/*
+ * 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;
+
+/**
+ * Class that exposes the various phoenix metrics collected
+ * at the Phoenix Connection Profile level. Because metrics are dynamic in nature,
+ * it is not guaranteed that the state exposed will always be in sync with each other.
+ * One should use these metrics primarily for monitoring and debugging purposes.
+ */
+public interface PhoenixConnectionProfileMetric extends Metric {

Review Comment:
   We do not have a concept of PhoenixConnectionProfile in the open source right?
   ConnectionProfile is a SFDC concept. 
   ConnectionProfile => QueryServiceName



##########
phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java:
##########
@@ -443,6 +448,11 @@ public ConnectionQueryServicesImpl(QueryServices services, ConnectionInfo connec
         for (Entry<String,String> entry : connectionInfo.asProps()) {
             config.set(entry.getKey(), entry.getValue());
         }
+        if (connectionInfo.getPrincipal() != null) {
+            config.set(QUERY_SERVICES_NAME, connectionInfo.getPrincipal());
+        }
+        LOGGER.info(String.format("CQS initialized with connection profile : %s",

Review Comment:
   Maybe the log should read "CQS initialized with query service name"



##########
phoenix-core/src/main/java/org/apache/phoenix/monitoring/connqueryservice/ConnectionQueryServiceMetricsHistograms.java:
##########
@@ -0,0 +1,60 @@
+/*
+ * 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;

Review Comment:
   How are the metric classes in this package different from the ones in the monitoring package?



-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] PHOENIX-7038 : Implement Phoenix Connection Profile Metrics [phoenix]

Posted by "stoty (via GitHub)" <gi...@apache.org>.
stoty commented on PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#issuecomment-1758919352

   Just opened a related [DISCUSS] thread on dev, @dbwong .


-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] PHOENIX-7038 : Implement Connection Query Service Metrics [phoenix]

Posted by "jpisaac (via GitHub)" <gi...@apache.org>.
jpisaac commented on PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#issuecomment-1793114010

   > In my ideal world we'd have some kind of whitelist for properties that are known to be safe to override (or an inverse blacklist)
   > (Of course users could still wreak havoc by modifying the hbase-site.xml used by the client)
   
   @stoty You are right, in the SFDC world, we do have such a whitelist in the app. Something we can think of bringing down into the Phoenix layer.
   
   


-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] PHOENIX-7038 : Implement Connection Query Service Metrics [phoenix]

Posted by "mihir6692 (via GitHub)" <gi...@apache.org>.
mihir6692 commented on code in PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#discussion_r1374843109


##########
phoenix-core/src/main/java/org/apache/phoenix/monitoring/connqueryservice/PhoenixConnectionQueryServiceMetrics.java:
##########
@@ -0,0 +1,116 @@
+/*
+ * 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.conf.Configuration;
+import org.apache.phoenix.monitoring.MetricType;
+import org.apache.phoenix.monitoring.PhoenixConnectionProfileMetric;
+import org.apache.phoenix.monitoring.PhoenixConnectionProfileMetricImpl;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.apache.phoenix.monitoring.MetricType.OPEN_INTERNAL_PHOENIX_CONNECTIONS_COUNTER;
+import static org.apache.phoenix.monitoring.MetricType.OPEN_PHOENIX_CONNECTIONS_COUNTER;
+import static org.apache.phoenix.monitoring.MetricType.PHOENIX_CONNECTIONS_THROTTLED_COUNTER;
+
+public class PhoenixConnectionQueryServiceMetrics {
+    public enum ConnectionQueryServiceMetrics {
+        CONNECTION_QUERY_SERVICE_OPEN_PHOENIX_CONNECTIONS_COUNTER(OPEN_PHOENIX_CONNECTIONS_COUNTER),
+        CONNECTION_QUERY_SERVICE_OPEN_INTERNAL_PHOENIX_CONNECTIONS_COUNTER(OPEN_INTERNAL_PHOENIX_CONNECTIONS_COUNTER),
+        CONNECTION_QUERY_SERVICE_PHOENIX_CONNECTIONS_THROTTLED_COUNTER(PHOENIX_CONNECTIONS_THROTTLED_COUNTER);
+
+        private MetricType metricType;
+        private PhoenixConnectionProfileMetric metric;
+
+        ConnectionQueryServiceMetrics(MetricType metricType) {
+            this.metricType = metricType;
+        }
+    }
+
+    private final String connectionQueryServiceName;
+    private Map<MetricType, PhoenixConnectionProfileMetric> metricRegister;
+    private ConnectionQueryServiceMetricsHistograms connectionQueryServiceMetricsHistograms;
+
+    public PhoenixConnectionQueryServiceMetrics(final String connectionQueryServiceName,
+            Configuration conf) {
+        this.connectionQueryServiceName = connectionQueryServiceName;
+        metricRegister = new HashMap<>();
+        for (ConnectionQueryServiceMetrics connectionQueryServiceMetric
+                : ConnectionQueryServiceMetrics.values()) {
+            connectionQueryServiceMetric.metric =
+                    new PhoenixConnectionProfileMetricImpl(connectionQueryServiceMetric.metricType);
+            metricRegister.put(connectionQueryServiceMetric.metricType,
+                    connectionQueryServiceMetric.metric);
+        }
+        connectionQueryServiceMetricsHistograms =
+                new ConnectionQueryServiceMetricsHistograms(connectionQueryServiceName, conf);
+    }
+
+    /**
+     * This function is used to update the value of Metric
+     * In case of counter val will be passed as 1.
+     *
+     * @param type metric type
+     * @param val update value. In case of counters, this will be 1
+     */
+    public void changeMetricValue(MetricType type, long val) {

Review Comment:
   done



-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [phoenix] ranganathg commented on a diff in pull request #1682: PHOENIX-7038 : Implement Phoenix Connection Profile Metrics

Posted by "ranganathg (via GitHub)" <gi...@apache.org>.
ranganathg commented on code in PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#discussion_r1338302775


##########
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;

Review Comment:
   localRef doesn't seem to be required right? this code can work on connectionQueryServicesMetricsManager ?



-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [phoenix] ranganathg commented on a diff in pull request #1682: PHOENIX-7038 : Implement Phoenix Connection Profile Metrics

Posted by "ranganathg (via GitHub)" <gi...@apache.org>.
ranganathg commented on code in PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#discussion_r1338297694


##########
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;

Review Comment:
   is it a good practise to return from a constructor? 
   Also can this code restructure to something like this in a semantic sense?
   `options =opts
   iscqsimetricsenabled =;
   iscqsimetricpublisherenabled=;
   initializemap
   Logger // this can log both the flags in one log line`
   



-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] PHOENIX-7038 : Implement Connection Query Service Metrics [phoenix]

Posted by "mihir6692 (via GitHub)" <gi...@apache.org>.
mihir6692 commented on code in PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#discussion_r1374840685


##########
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);

Review Comment:
   Yes, We only create Query Service mapping once for JVM so it could be null first time.



-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] PHOENIX-7038 : Implement Connection Query Service Metrics [phoenix]

Posted by "mihir6692 (via GitHub)" <gi...@apache.org>.
mihir6692 commented on code in PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#discussion_r1374838430


##########
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;

Review Comment:
   Done



-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] PHOENIX-7038 : Implement Connection Query Service Metrics [phoenix]

Posted by "mihir6692 (via GitHub)" <gi...@apache.org>.
mihir6692 commented on code in PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#discussion_r1374843652


##########
phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServices.java:
##########
@@ -221,4 +221,8 @@ default void closeAllConnections(SQLExceptionInfo.Builder reasonBuilder) {
     }
 
     PMetaData getMetaDataCache();
+
+    int getConnectionCount();

Review Comment:
   Thanks for suggestion, I have updated it. 



##########
phoenix-core/src/test/java/org/apache/phoenix/monitoring/connqueryservice/ConnectionQueryServiceNameMetricsGenerateAndVerifyTestData.java:
##########
@@ -0,0 +1,82 @@
+/**
+ * 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.phoenix.monitoring.MetricType;
+import org.apache.phoenix.monitoring.PhoenixConnectionProfileMetric;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.apache.phoenix.monitoring.MetricType.OPEN_INTERNAL_PHOENIX_CONNECTIONS_COUNTER;
+import static org.apache.phoenix.monitoring.MetricType.OPEN_PHOENIX_CONNECTIONS_COUNTER;
+import static org.apache.phoenix.monitoring.MetricType.PHOENIX_CONNECTIONS_THROTTLED_COUNTER;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * This class is used primarily to populate data and
+ * verification methods
+ */
+
+public class ConnectionQueryServiceNameMetricsGenerateAndVerifyTestData {

Review Comment:
   Done



-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] PHOENIX-7038 : Implement Connection Query Service Metrics [phoenix]

Posted by "mihir6692 (via GitHub)" <gi...@apache.org>.
mihir6692 commented on code in PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#discussion_r1374847551


##########
phoenix-core/src/main/java/org/apache/phoenix/monitoring/connqueryservice/ConnectionQueryServiceMetricsHistograms.java:
##########
@@ -0,0 +1,60 @@
+/*
+ * 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;

Review Comment:
   This metric class is for CQS Metrics. We have similar metrics class for Global, Table etc.
   
   Some example : 
   
   `TableClientMetrics.java`
   `GlobalClientMetrics.java`
   
   This could be combined, I guess. I will add note in [PHOENIX-6174](https://issues.apache.org/jira/browse/PHOENIX-6174) to simplify in future in monitoring.



##########
phoenix-core/src/it/java/org/apache/phoenix/monitoring/connqueryservice/PhoenixConnectionQueryServiceMetrcisIT.java:
##########
@@ -0,0 +1,357 @@
+/*
+ * 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.conf.Configuration;
+import org.apache.hadoop.hbase.HBaseConfiguration;
+import org.apache.hadoop.hbase.HBaseTestingUtility;
+import org.apache.phoenix.end2end.NeedsOwnMiniClusterTest;
+import org.apache.phoenix.jdbc.PhoenixConnection;
+import org.apache.phoenix.jdbc.PhoenixDriver;
+import org.apache.phoenix.monitoring.HistogramDistribution;
+import org.apache.phoenix.monitoring.Metric;
+import org.apache.phoenix.monitoring.MetricType;
+import org.apache.phoenix.monitoring.PhoenixConnectionProfileMetric;
+import org.apache.phoenix.query.BaseTest;
+import org.apache.phoenix.query.ConfigurationFactory;
+import org.apache.phoenix.query.HBaseFactoryProvider;
+import org.apache.phoenix.query.QueryServices;
+import org.apache.phoenix.query.QueryServicesOptions;
+import org.apache.phoenix.util.InstanceResolver;
+import org.apache.phoenix.util.PhoenixRuntime;
+import org.junit.After;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.Statement;
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import static org.apache.phoenix.monitoring.MetricType.OPEN_INTERNAL_PHOENIX_CONNECTIONS_COUNTER;
+import static org.apache.phoenix.monitoring.MetricType.OPEN_PHOENIX_CONNECTIONS_COUNTER;
+import static org.apache.phoenix.monitoring.MetricType.PHOENIX_CONNECTIONS_THROTTLED_COUNTER;
+import static org.apache.phoenix.query.QueryServices.CLIENT_CONNECTION_MAX_ALLOWED_CONNECTIONS;
+import static org.apache.phoenix.query.QueryServices.CONNECTION_QUERY_SERVICE_METRICS_ENABLED;
+import static org.apache.phoenix.query.QueryServices.INTERNAL_CONNECTION_MAX_ALLOWED_CONNECTIONS;
+import static org.apache.phoenix.query.QueryServices.QUERY_SERVICES_NAME;
+import static org.apache.phoenix.util.PhoenixRuntime.clearAllConnectionQueryServiceMetrics;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+@Category(NeedsOwnMiniClusterTest.class)
+public class PhoenixConnectionQueryServiceMetrcisIT extends BaseTest {

Review Comment:
   done



-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] PHOENIX-7038 : Implement Connection Query Service Metrics [phoenix]

Posted by "mihir6692 (via GitHub)" <gi...@apache.org>.
mihir6692 commented on PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#issuecomment-1803053694

   Test failure is not related.


-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [phoenix] ranganathg commented on a diff in pull request #1682: PHOENIX-7038 : Implement Phoenix Connection Profile Metrics

Posted by "ranganathg (via GitHub)" <gi...@apache.org>.
ranganathg commented on code in PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#discussion_r1338324240


##########
phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServices.java:
##########
@@ -221,4 +221,8 @@ default void closeAllConnections(SQLExceptionInfo.Builder reasonBuilder) {
     }
 
     PMetaData getMetaDataCache();
+
+    int getConnectionCount();

Review Comment:
   just thinking can it be like "int getConnection(bool isInternal);"?



-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [phoenix] ranganathg commented on a diff in pull request #1682: PHOENIX-7038 : Implement Phoenix Connection Profile Metrics

Posted by "ranganathg (via GitHub)" <gi...@apache.org>.
ranganathg commented on code in PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#discussion_r1338300180


##########
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"

Review Comment:
   is it better to throw an exception if this is not acceptable condition?



-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] PHOENIX-7038 : Implement Connection Query Service Metrics [phoenix]

Posted by "mihir6692 (via GitHub)" <gi...@apache.org>.
mihir6692 merged PR #1682:
URL: https://github.com/apache/phoenix/pull/1682


-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] PHOENIX-7038 : Implement Phoenix Connection Profile Metrics [phoenix]

Posted by "mihir6692 (via GitHub)" <gi...@apache.org>.
mihir6692 commented on PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#issuecomment-1771650283

   Thanks for the reviews and comments @stoty @ranganathg @dbwong @jpisaac 
   
   I will go through comments and DISCUSS Thread today. 


-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] PHOENIX-7038 : Implement Connection Query Service Metrics [phoenix]

Posted by "mihir6692 (via GitHub)" <gi...@apache.org>.
mihir6692 commented on code in PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#discussion_r1374843398


##########
phoenix-core/src/main/java/org/apache/phoenix/monitoring/connqueryservice/PhoenixConnectionQueryServiceMetrics.java:
##########
@@ -0,0 +1,116 @@
+/*
+ * 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.conf.Configuration;
+import org.apache.phoenix.monitoring.MetricType;
+import org.apache.phoenix.monitoring.PhoenixConnectionProfileMetric;
+import org.apache.phoenix.monitoring.PhoenixConnectionProfileMetricImpl;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.apache.phoenix.monitoring.MetricType.OPEN_INTERNAL_PHOENIX_CONNECTIONS_COUNTER;
+import static org.apache.phoenix.monitoring.MetricType.OPEN_PHOENIX_CONNECTIONS_COUNTER;
+import static org.apache.phoenix.monitoring.MetricType.PHOENIX_CONNECTIONS_THROTTLED_COUNTER;
+
+public class PhoenixConnectionQueryServiceMetrics {
+    public enum ConnectionQueryServiceMetrics {
+        CONNECTION_QUERY_SERVICE_OPEN_PHOENIX_CONNECTIONS_COUNTER(OPEN_PHOENIX_CONNECTIONS_COUNTER),
+        CONNECTION_QUERY_SERVICE_OPEN_INTERNAL_PHOENIX_CONNECTIONS_COUNTER(OPEN_INTERNAL_PHOENIX_CONNECTIONS_COUNTER),
+        CONNECTION_QUERY_SERVICE_PHOENIX_CONNECTIONS_THROTTLED_COUNTER(PHOENIX_CONNECTIONS_THROTTLED_COUNTER);
+
+        private MetricType metricType;
+        private PhoenixConnectionProfileMetric metric;
+
+        ConnectionQueryServiceMetrics(MetricType metricType) {
+            this.metricType = metricType;
+        }
+    }
+
+    private final String connectionQueryServiceName;
+    private Map<MetricType, PhoenixConnectionProfileMetric> metricRegister;
+    private ConnectionQueryServiceMetricsHistograms connectionQueryServiceMetricsHistograms;
+
+    public PhoenixConnectionQueryServiceMetrics(final String connectionQueryServiceName,
+            Configuration conf) {
+        this.connectionQueryServiceName = connectionQueryServiceName;
+        metricRegister = new HashMap<>();
+        for (ConnectionQueryServiceMetrics connectionQueryServiceMetric
+                : ConnectionQueryServiceMetrics.values()) {
+            connectionQueryServiceMetric.metric =
+                    new PhoenixConnectionProfileMetricImpl(connectionQueryServiceMetric.metricType);
+            metricRegister.put(connectionQueryServiceMetric.metricType,
+                    connectionQueryServiceMetric.metric);
+        }
+        connectionQueryServiceMetricsHistograms =
+                new ConnectionQueryServiceMetricsHistograms(connectionQueryServiceName, conf);
+    }
+
+    /**
+     * This function is used to update the value of Metric
+     * In case of counter val will be passed as 1.
+     *
+     * @param type metric type
+     * @param val update value. In case of counters, this will be 1
+     */
+    public void changeMetricValue(MetricType type, long val) {
+        if (!metricRegister.containsKey(type)) {
+            return;
+        }
+        PhoenixConnectionProfileMetric metric = metricRegister.get(type);
+        metric.set(val);
+    }
+
+    /**
+     * This function is used to get the value of Metric.
+     *
+     * @param type metric type
+     * @return val current value of metric.
+     */
+    public long getMetricValue(MetricType type) {
+        if (!metricRegister.containsKey(type)) {
+            return 0;
+        }
+        PhoenixConnectionProfileMetric metric = metricRegister.get(type);
+        return metric.getValue();
+    }
+
+    public String getConnectionQueryServiceName() {
+        return connectionQueryServiceName;
+    }
+
+    /**
+     * This method is called to aggregate all the Metrics across all Connection Query Service.
+     *
+     * @return map of Connection Query Service name -> list of ConnectionQueryServiceMetric.
+     */
+    public List<PhoenixConnectionProfileMetric> getMetricMap() {

Review Comment:
   Changed name to `getAllMetrics()`



-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] PHOENIX-7038 : Implement Connection Query Service Metrics [phoenix]

Posted by "mihir6692 (via GitHub)" <gi...@apache.org>.
mihir6692 commented on PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#issuecomment-1789464919

   Test failures are not related. 


-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [phoenix] ranganathg commented on a diff in pull request #1682: PHOENIX-7038 : Implement Phoenix Connection Profile Metrics

Posted by "ranganathg (via GitHub)" <gi...@apache.org>.
ranganathg commented on code in PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#discussion_r1338341913


##########
phoenix-core/src/main/java/org/apache/phoenix/monitoring/connqueryservice/ConnectionQueryServiceMetricsHistograms.java:
##########
@@ -0,0 +1,60 @@
+/*
+ * 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;

Review Comment:
   just thinking loud - can this package be org.apache.phoenix.monitoring.queryservice; or org.apache.phoenix.monitoring.connectionqueryservice (full name in package)?



-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [phoenix] ranganathg commented on a diff in pull request #1682: PHOENIX-7038 : Implement Phoenix Connection Profile Metrics

Posted by "ranganathg (via GitHub)" <gi...@apache.org>.
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [phoenix] ranganathg commented on a diff in pull request #1682: PHOENIX-7038 : Implement Phoenix Connection Profile Metrics

Posted by "ranganathg (via GitHub)" <gi...@apache.org>.
ranganathg commented on code in PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#discussion_r1338311821


##########
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(

Review Comment:
   should this be the responsibility of PhoenixConnectionQueryServiceMetrics itself?



-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [phoenix] ranganathg commented on a diff in pull request #1682: PHOENIX-7038 : Implement Phoenix Connection Profile Metrics

Posted by "ranganathg (via GitHub)" <gi...@apache.org>.
ranganathg commented on code in PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#discussion_r1338323357


##########
phoenix-core/src/main/java/org/apache/phoenix/monitoring/connqueryservice/PhoenixConnectionQueryServiceMetrics.java:
##########
@@ -0,0 +1,116 @@
+/*
+ * 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.conf.Configuration;
+import org.apache.phoenix.monitoring.MetricType;
+import org.apache.phoenix.monitoring.PhoenixConnectionProfileMetric;
+import org.apache.phoenix.monitoring.PhoenixConnectionProfileMetricImpl;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.apache.phoenix.monitoring.MetricType.OPEN_INTERNAL_PHOENIX_CONNECTIONS_COUNTER;
+import static org.apache.phoenix.monitoring.MetricType.OPEN_PHOENIX_CONNECTIONS_COUNTER;
+import static org.apache.phoenix.monitoring.MetricType.PHOENIX_CONNECTIONS_THROTTLED_COUNTER;
+
+public class PhoenixConnectionQueryServiceMetrics {
+    public enum ConnectionQueryServiceMetrics {
+        CONNECTION_QUERY_SERVICE_OPEN_PHOENIX_CONNECTIONS_COUNTER(OPEN_PHOENIX_CONNECTIONS_COUNTER),
+        CONNECTION_QUERY_SERVICE_OPEN_INTERNAL_PHOENIX_CONNECTIONS_COUNTER(OPEN_INTERNAL_PHOENIX_CONNECTIONS_COUNTER),
+        CONNECTION_QUERY_SERVICE_PHOENIX_CONNECTIONS_THROTTLED_COUNTER(PHOENIX_CONNECTIONS_THROTTLED_COUNTER);
+
+        private MetricType metricType;
+        private PhoenixConnectionProfileMetric metric;
+
+        ConnectionQueryServiceMetrics(MetricType metricType) {
+            this.metricType = metricType;
+        }
+    }
+
+    private final String connectionQueryServiceName;
+    private Map<MetricType, PhoenixConnectionProfileMetric> metricRegister;
+    private ConnectionQueryServiceMetricsHistograms connectionQueryServiceMetricsHistograms;
+
+    public PhoenixConnectionQueryServiceMetrics(final String connectionQueryServiceName,
+            Configuration conf) {
+        this.connectionQueryServiceName = connectionQueryServiceName;
+        metricRegister = new HashMap<>();
+        for (ConnectionQueryServiceMetrics connectionQueryServiceMetric
+                : ConnectionQueryServiceMetrics.values()) {
+            connectionQueryServiceMetric.metric =
+                    new PhoenixConnectionProfileMetricImpl(connectionQueryServiceMetric.metricType);
+            metricRegister.put(connectionQueryServiceMetric.metricType,
+                    connectionQueryServiceMetric.metric);
+        }
+        connectionQueryServiceMetricsHistograms =
+                new ConnectionQueryServiceMetricsHistograms(connectionQueryServiceName, conf);
+    }
+
+    /**
+     * This function is used to update the value of Metric
+     * In case of counter val will be passed as 1.
+     *
+     * @param type metric type
+     * @param val update value. In case of counters, this will be 1
+     */
+    public void changeMetricValue(MetricType type, long val) {
+        if (!metricRegister.containsKey(type)) {
+            return;
+        }
+        PhoenixConnectionProfileMetric metric = metricRegister.get(type);
+        metric.set(val);
+    }
+
+    /**
+     * This function is used to get the value of Metric.
+     *
+     * @param type metric type
+     * @return val current value of metric.
+     */
+    public long getMetricValue(MetricType type) {
+        if (!metricRegister.containsKey(type)) {
+            return 0;
+        }
+        PhoenixConnectionProfileMetric metric = metricRegister.get(type);
+        return metric.getValue();
+    }
+
+    public String getConnectionQueryServiceName() {
+        return connectionQueryServiceName;
+    }
+
+    /**
+     * This method is called to aggregate all the Metrics across all Connection Query Service.
+     *
+     * @return map of Connection Query Service name -> list of ConnectionQueryServiceMetric.
+     */
+    public List<PhoenixConnectionProfileMetric> getMetricMap() {

Review Comment:
   getMetrics? this method is not really returning a map right?



-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [phoenix] ranganathg commented on a diff in pull request #1682: PHOENIX-7038 : Implement Phoenix Connection Profile Metrics

Posted by "ranganathg (via GitHub)" <gi...@apache.org>.
ranganathg commented on code in PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#discussion_r1338327909


##########
phoenix-core/src/test/java/org/apache/phoenix/monitoring/connqueryservice/ConnectionQueryServiceNameMetricsGenerateAndVerifyTestData.java:
##########
@@ -0,0 +1,82 @@
+/**
+ * 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.phoenix.monitoring.MetricType;
+import org.apache.phoenix.monitoring.PhoenixConnectionProfileMetric;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.apache.phoenix.monitoring.MetricType.OPEN_INTERNAL_PHOENIX_CONNECTIONS_COUNTER;
+import static org.apache.phoenix.monitoring.MetricType.OPEN_PHOENIX_CONNECTIONS_COUNTER;
+import static org.apache.phoenix.monitoring.MetricType.PHOENIX_CONNECTIONS_THROTTLED_COUNTER;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+/**
+ * This class is used primarily to populate data and
+ * verification methods
+ */
+
+public class ConnectionQueryServiceNameMetricsGenerateAndVerifyTestData {

Review Comment:
   can be ConnectionQueryServiceNameMetricsTest?



-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] PHOENIX-7038 : Implement Connection Query Service Metrics [phoenix]

Posted by "mihir6692 (via GitHub)" <gi...@apache.org>.
mihir6692 commented on code in PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#discussion_r1374839769


##########
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;

Review Comment:
   Done. Removed use of `localRef`



-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] PHOENIX-7038 : Implement Connection Query Service Metrics [phoenix]

Posted by "mihir6692 (via GitHub)" <gi...@apache.org>.
mihir6692 commented on PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#issuecomment-1783251091

   > My main concern is this is too sfdc focused. A conection profile doesn't exist within the concept space of phoenix. I'd rebrand this as ConnectionInfo metrics. 
   
   That's right. I have changed it to ConnecitonQueryService Metrics since this metrics are calculated at CQS Level. 
   
   > My second concern is there apears to be a bunch of copy pasted or similar code... I opened a Jira for a better overall metrics infrastrucutre and while that is out of scope is it possiblegenericfy some of this.
   
   That's right there are multiple class/code blocks which are copy pasted. I will add notes of these [PHOENIX-6174](https://issues.apache.org/jira/browse/PHOENIX-6174) to simplify in future.
   
   @dbwong 


-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [phoenix] ranganathg commented on a diff in pull request #1682: PHOENIX-7038 : Implement Phoenix Connection Profile Metrics

Posted by "ranganathg (via GitHub)" <gi...@apache.org>.
ranganathg commented on code in PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#discussion_r1338321739


##########
phoenix-core/src/main/java/org/apache/phoenix/monitoring/connqueryservice/PhoenixConnectionQueryServiceMetrics.java:
##########
@@ -0,0 +1,116 @@
+/*
+ * 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.conf.Configuration;
+import org.apache.phoenix.monitoring.MetricType;
+import org.apache.phoenix.monitoring.PhoenixConnectionProfileMetric;
+import org.apache.phoenix.monitoring.PhoenixConnectionProfileMetricImpl;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import static org.apache.phoenix.monitoring.MetricType.OPEN_INTERNAL_PHOENIX_CONNECTIONS_COUNTER;
+import static org.apache.phoenix.monitoring.MetricType.OPEN_PHOENIX_CONNECTIONS_COUNTER;
+import static org.apache.phoenix.monitoring.MetricType.PHOENIX_CONNECTIONS_THROTTLED_COUNTER;
+
+public class PhoenixConnectionQueryServiceMetrics {
+    public enum ConnectionQueryServiceMetrics {
+        CONNECTION_QUERY_SERVICE_OPEN_PHOENIX_CONNECTIONS_COUNTER(OPEN_PHOENIX_CONNECTIONS_COUNTER),
+        CONNECTION_QUERY_SERVICE_OPEN_INTERNAL_PHOENIX_CONNECTIONS_COUNTER(OPEN_INTERNAL_PHOENIX_CONNECTIONS_COUNTER),
+        CONNECTION_QUERY_SERVICE_PHOENIX_CONNECTIONS_THROTTLED_COUNTER(PHOENIX_CONNECTIONS_THROTTLED_COUNTER);
+
+        private MetricType metricType;
+        private PhoenixConnectionProfileMetric metric;
+
+        ConnectionQueryServiceMetrics(MetricType metricType) {
+            this.metricType = metricType;
+        }
+    }
+
+    private final String connectionQueryServiceName;
+    private Map<MetricType, PhoenixConnectionProfileMetric> metricRegister;
+    private ConnectionQueryServiceMetricsHistograms connectionQueryServiceMetricsHistograms;
+
+    public PhoenixConnectionQueryServiceMetrics(final String connectionQueryServiceName,
+            Configuration conf) {
+        this.connectionQueryServiceName = connectionQueryServiceName;
+        metricRegister = new HashMap<>();
+        for (ConnectionQueryServiceMetrics connectionQueryServiceMetric
+                : ConnectionQueryServiceMetrics.values()) {
+            connectionQueryServiceMetric.metric =
+                    new PhoenixConnectionProfileMetricImpl(connectionQueryServiceMetric.metricType);
+            metricRegister.put(connectionQueryServiceMetric.metricType,
+                    connectionQueryServiceMetric.metric);
+        }
+        connectionQueryServiceMetricsHistograms =
+                new ConnectionQueryServiceMetricsHistograms(connectionQueryServiceName, conf);
+    }
+
+    /**
+     * This function is used to update the value of Metric
+     * In case of counter val will be passed as 1.
+     *
+     * @param type metric type
+     * @param val update value. In case of counters, this will be 1
+     */
+    public void changeMetricValue(MetricType type, long val) {

Review Comment:
   can this be setMetricValue or updateMetricValue?



-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [phoenix] ranganathg commented on a diff in pull request #1682: PHOENIX-7038 : Implement Phoenix Connection Profile Metrics

Posted by "ranganathg (via GitHub)" <gi...@apache.org>.
ranganathg commented on code in PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#discussion_r1338334701


##########
phoenix-core/src/test/java/org/apache/phoenix/monitoring/connqueryservice/PhoenixConnectionQueryServiceMetricsTest.java:
##########
@@ -0,0 +1,99 @@
+/**
+ * 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.conf.Configuration;
+import org.apache.phoenix.monitoring.MetricType;
+import org.apache.phoenix.monitoring.PhoenixConnectionProfileMetric;
+import org.junit.Test;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+
+import static org.apache.phoenix.monitoring.connqueryservice.ConnectionQueryServiceNameMetricsGenerateAndVerifyTestData.openInternalPhoenixConnCounter;
+import static org.apache.phoenix.monitoring.connqueryservice.ConnectionQueryServiceNameMetricsGenerateAndVerifyTestData.openPhoenixConnCounter;
+import static org.apache.phoenix.monitoring.connqueryservice.ConnectionQueryServiceNameMetricsGenerateAndVerifyTestData.connectionQueryServiceNames;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertTrue;
+
+public class PhoenixConnectionQueryServiceMetricsTest {
+    static Map<String, PhoenixConnectionQueryServiceMetrics> phoxnixConnectionQueryServiceSet = new HashMap<>();
+
+    public boolean verifyConnectionQueryServiceName() {
+
+        if (phoxnixConnectionQueryServiceSet.isEmpty()) {
+            return false;
+        }
+        for (String connectionQueryServiceName : connectionQueryServiceNames) {
+            PhoenixConnectionQueryServiceMetrics instance = phoxnixConnectionQueryServiceSet.get(connectionQueryServiceName);
+            if (!instance.getConnectionQueryServiceName().equals(connectionQueryServiceName)) {
+                return false;
+            }
+        }
+        return true;
+    }
+
+    public void verifyMetricsFromPhoenixConnectionQueryServiceMetrics() {
+        assertFalse(phoxnixConnectionQueryServiceSet.isEmpty());
+        for (int i = 0; i < connectionQueryServiceNames.length; i++) {
+            PhoenixConnectionQueryServiceMetrics instance = phoxnixConnectionQueryServiceSet.get(connectionQueryServiceNames[i]);
+            assertEquals(instance.getConnectionQueryServiceName(), connectionQueryServiceNames[i]);
+            List<PhoenixConnectionProfileMetric> metricList = instance.getMetricMap();
+            for (PhoenixConnectionProfileMetric metric : metricList) {
+
+                if (metric.getMetricType().equals(MetricType.OPEN_INTERNAL_PHOENIX_CONNECTIONS_COUNTER)) {
+                    assertEquals(openInternalPhoenixConnCounter[i], metric.getValue());
+                }
+                if (metric.getMetricType().equals(MetricType.OPEN_PHOENIX_CONNECTIONS_COUNTER)) {
+                    assertEquals(openPhoenixConnCounter[i], metric.getValue());
+                }
+            }
+        }
+    }
+
+    @Test
+    public void testPhoenixConnectionQueryServiceMetricsforPhoenixConnectionQueryServiceName() {
+        Configuration conf = new Configuration();
+        for (int i = 0; i < connectionQueryServiceNames.length; i++) {
+            PhoenixConnectionQueryServiceMetrics instance = new PhoenixConnectionQueryServiceMetrics(connectionQueryServiceNames[i], conf);
+            phoxnixConnectionQueryServiceSet.put(connectionQueryServiceNames[i], instance);
+        }
+        assertTrue(verifyConnectionQueryServiceName());
+    }
+
+    /**
+     * This test is for changeMetricValue() Method and getMetricMap()
+     */
+    @Test
+    public void testPhoenixconnectionQueryServiceMetrics() {

Review Comment:
   nit: testPhoenixConnectionQueryServiceMetrics - Capital C



-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [phoenix] ranganathg commented on a diff in pull request #1682: PHOENIX-7038 : Implement Phoenix Connection Profile Metrics

Posted by "ranganathg (via GitHub)" <gi...@apache.org>.
ranganathg commented on code in PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#discussion_r1338338845


##########
phoenix-core/src/main/java/org/apache/phoenix/query/QueryServicesOptions.java:
##########
@@ -28,6 +28,10 @@
 import static org.apache.phoenix.query.QueryServices.CLIENT_SPOOL_THRESHOLD_BYTES_ATTRIB;
 import static org.apache.phoenix.query.QueryServices.COLLECT_REQUEST_LEVEL_METRICS;
 import static org.apache.phoenix.query.QueryServices.COMMIT_STATS_ASYNC;
+import static org.apache.phoenix.query.QueryServices.CONNECTION_QUERY_SERVICE_HISTOGRAM_SIZE_RANGES;
+import static org.apache.phoenix.query.QueryServices.CONNECTION_QUERY_SERVICE_METRICS_ENABLED;
+import static org.apache.phoenix.query.QueryServices.CONNECTION_QUERY_SERVICE_METRICS_PUBLISHER_ENABLED;
+import static org.apache.phoenix.query.QueryServices.CONNECTION_QUERY_SERVICE_METRICS_PUBLISH_CLASS_NAME;

Review Comment:
   PUBLISHER_CLASSNAME?



-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [phoenix] ranganathg commented on a diff in pull request #1682: PHOENIX-7038 : Implement Phoenix Connection Profile Metrics

Posted by "ranganathg (via GitHub)" <gi...@apache.org>.
ranganathg commented on code in PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#discussion_r1338315012


##########
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,

Review Comment:
   updateMetrics?



-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [phoenix] ranganathg commented on a diff in pull request #1682: PHOENIX-7038 : Implement Phoenix Connection Profile Metrics

Posted by "ranganathg (via GitHub)" <gi...@apache.org>.
ranganathg commented on code in PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#discussion_r1338314093


##########
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()) {

Review Comment:
   is isDebugEnabled check required for debug logging? what happens if its not enabled and still you have debug Log?



-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] PHOENIX-7038 : Implement Connection Query Service Metrics [phoenix]

Posted by "mihir6692 (via GitHub)" <gi...@apache.org>.
mihir6692 commented on code in PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#discussion_r1374839499


##########
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"

Review Comment:
   It's better to not kill a service/JVM start for Metrics classname issue. 



-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] PHOENIX-7038 : Implement Connection Query Service Metrics [phoenix]

Posted by "mihir6692 (via GitHub)" <gi...@apache.org>.
mihir6692 commented on code in PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#discussion_r1374842870


##########
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()) {

Review Comment:
   It's better to check for if logging level is enabled or not. It would avoid processing of log message, trace/debug level logging could be happening too frequently. I have also changed the logging level to TRACE since we may not need it outside of dev enviornment.



##########
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,

Review Comment:
   Done



##########
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:
   done



-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] PHOENIX-7038 : Implement Connection Query Service Metrics [phoenix]

Posted by "mihir6692 (via GitHub)" <gi...@apache.org>.
mihir6692 commented on code in PR #1682:
URL: https://github.com/apache/phoenix/pull/1682#discussion_r1374848007


##########
phoenix-core/src/main/java/org/apache/phoenix/monitoring/PhoenixConnectionProfileMetric.java:
##########
@@ -0,0 +1,38 @@
+/*
+ * 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;
+
+/**
+ * Class that exposes the various phoenix metrics collected
+ * at the Phoenix Connection Profile level. Because metrics are dynamic in nature,
+ * it is not guaranteed that the state exposed will always be in sync with each other.
+ * One should use these metrics primarily for monitoring and debugging purposes.
+ */
+public interface PhoenixConnectionProfileMetric extends Metric {

Review Comment:
   Done. Changed everywhere.



##########
phoenix-core/src/main/java/org/apache/phoenix/query/ConnectionQueryServicesImpl.java:
##########
@@ -443,6 +448,11 @@ public ConnectionQueryServicesImpl(QueryServices services, ConnectionInfo connec
         for (Entry<String,String> entry : connectionInfo.asProps()) {
             config.set(entry.getKey(), entry.getValue());
         }
+        if (connectionInfo.getPrincipal() != null) {
+            config.set(QUERY_SERVICES_NAME, connectionInfo.getPrincipal());
+        }
+        LOGGER.info(String.format("CQS initialized with connection profile : %s",

Review Comment:
   done



-- 
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: issues-unsubscribe@phoenix.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org