Copilot commented on code in PR #7855:
URL: https://github.com/apache/hbase/pull/7855#discussion_r3155318745


##########
hbase-server/src/main/java/org/apache/hadoop/hbase/regionserver/metrics/MetricsTableRequests.java:
##########
@@ -327,41 +328,30 @@ public void updateCheckAndMutate(long time, long 
blockBytesScanned) {
    * @param count Number of occurrences to record
    */
   public void updateTableReadQueryMeter(long count) {
-    if (isEnabTableQueryMeterMetrics()) {
+    if (isEnableTableQueryMeterMetrics()) {
       readMeter.mark(count);
     }
   }
 
-  /**
-   * Update table read QPS
-   */
-  public void updateTableReadQueryMeter() {
-    if (isEnabTableQueryMeterMetrics()) {
-      readMeter.mark();
-    }
-  }
-
   /**
    * Update table write QPS
    * @param count Number of occurrences to record
    */
   public void updateTableWriteQueryMeter(long count) {
-    if (isEnabTableQueryMeterMetrics()) {
+    if (isEnableTableQueryMeterMetrics()) {
       writeMeter.mark(count);
     }
   }
 
-  /**
-   * Update table write QPS
-   */
-  public void updateTableWriteQueryMeter() {
-    if (isEnabTableQueryMeterMetrics()) {
-      writeMeter.mark();
-    }
-  }
-
-  // Visible for testing
+  @RestrictedApi(explanation = "Should only be called in 
TestMetricsTableRequests", link = "",
+      allowedOnPath = ".*/TestMetricsTableRequests.java")
   public MetricRegistryInfo getMetricRegistryInfo() {
     return registryInfo;
   }
+
+  @RestrictedApi(explanation = "Should only be called in 
TestMetricsTableRequests", link = "",
+      allowedOnPath = ".*/TestMetricsTableRequests.java")
+  public MetricRegistry getMetricRegistry() {

Review Comment:
   getMetricRegistry() is newly public but appears intended only for tests. 
Since TestMetricsTableRequests is now in the same package, consider making 
getMetricRegistry()/getMetricRegistryInfo() package-private (and dropping the 
public test-only accessor) to avoid expanding the production API surface.
   ```suggestion
     MetricRegistryInfo getMetricRegistryInfo() {
       return registryInfo;
     }
   
     @RestrictedApi(explanation = "Should only be called in 
TestMetricsTableRequests", link = "",
         allowedOnPath = ".*/TestMetricsTableRequests.java")
     MetricRegistry getMetricRegistry() {
   ```



##########
hbase-hadoop-compat/src/test/java/org/apache/hadoop/hbase/metrics/impl/TestGlobalMetricRegistriesAdapter.java:
##########
@@ -0,0 +1,126 @@
+/*
+ * 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.hadoop.hbase.metrics.impl;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotSame;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+import java.lang.reflect.Constructor;
+import java.lang.reflect.Method;
+import java.util.Collection;
+import org.apache.hadoop.hbase.metrics.MetricRegistries;
+import org.apache.hadoop.hbase.metrics.MetricRegistry;
+import org.apache.hadoop.hbase.metrics.MetricRegistryInfo;
+import org.apache.hadoop.hbase.testclassification.MetricsTests;
+import org.apache.hadoop.hbase.testclassification.SmallTests;
+import org.apache.hadoop.metrics2.AbstractMetric;
+import org.apache.hadoop.metrics2.MetricsRecord;
+import org.apache.hadoop.metrics2.impl.MetricsExportHelper;
+import org.apache.hadoop.metrics2.lib.DefaultMetricsSystem;
+import org.junit.jupiter.api.AfterEach;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Tag;
+import org.junit.jupiter.api.Test;
+
+@Tag(MetricsTests.TAG)
+@Tag(SmallTests.TAG)
+public class TestGlobalMetricRegistriesAdapter {
+
+  private static final String METRICS_NAME = 
"TestGlobalMetricRegistriesAdapter";
+  private static final String COUNTER_NAME = "requests";
+  private static final MetricRegistryInfo INFO =
+    new MetricRegistryInfo(METRICS_NAME, "Metrics for 
GlobalMetricRegistriesAdapter tests",
+      "RegionServer,sub=" + METRICS_NAME, "regionserver", false);
+
+  private GlobalMetricRegistriesAdapter adapter;
+
+  @BeforeEach
+  public void setUp() {
+    MetricRegistries.global().clear();
+    DefaultMetricsSystem.shutdown();
+    DefaultMetricsSystem.initialize("globalMetricRegistriesAdapterTest");
+    DefaultMetricsSystem.instance().start();
+    adapter = createAdapter();
+  }
+
+  @AfterEach
+  public void tearDown() {
+    MetricRegistries.global().clear();
+    if (adapter != null) {
+      // First unregister sources for the cleared registries, then stop the 
adapter's executor.
+      runAdapter();
+      adapter.stop();
+      runAdapter();
+    }
+    DefaultMetricsSystem.shutdown();
+  }
+
+  @Test
+  public void testExportUsesRecreatedMetricRegistry() {
+    MetricRegistry registryBefore = MetricRegistries.global().create(INFO);
+    registryBefore.counter(COUNTER_NAME).increment(1);
+    runAdapter();
+
+    assertEquals(1, getExportedCounterValue());
+
+    assertTrue(MetricRegistries.global().remove(INFO));
+    MetricRegistry registryAfter = MetricRegistries.global().create(INFO);
+    assertNotSame(registryBefore, registryAfter);
+    registryAfter.counter(COUNTER_NAME).increment(2);
+    runAdapter();
+
+    assertEquals(2, getExportedCounterValue());
+  }
+
+  private GlobalMetricRegistriesAdapter createAdapter() {
+    try {
+      Constructor<GlobalMetricRegistriesAdapter> constructor =
+        GlobalMetricRegistriesAdapter.class.getDeclaredConstructor();
+      constructor.setAccessible(true);
+      return constructor.newInstance();
+    } catch (ReflectiveOperationException e) {
+      throw new AssertionError("Failed to create 
GlobalMetricRegistriesAdapter", e);
+    }

Review Comment:
   createAdapter() reflectively accesses the private constructor, but 
GlobalMetricRegistriesAdapter exposes a public static init() factory. Prefer 
calling GlobalMetricRegistriesAdapter.init() directly to avoid brittle 
reflective access in the test.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

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

Reply via email to