tibrewalpratik17 commented on code in PR #13991:
URL: https://github.com/apache/pinot/pull/13991#discussion_r1760951898


##########
pinot-common/src/test/java/org/apache/pinot/common/metrics/AbstractMetricsTest.java:
##########
@@ -108,4 +112,154 @@ public void testMultipleGauges() {
     controllerMetrics.removeGauge(metricName2);
     
Assert.assertTrue(controllerMetrics.getMetricsRegistry().allMetrics().isEmpty());
   }
+
+  /**
+   * Creates and initializes a concrete instance of {@link AbstractMetrics} 
(in this case, a {@code ControllerMetrics}).
+   * @return a {@code ControllerMetrics} suitable for testing {@code 
AbstractMetrics} APIs
+   */
+  private static ControllerMetrics buildTestMetrics() {
+    PinotConfiguration pinotConfiguration = new PinotConfiguration();
+    pinotConfiguration.setProperty(CONFIG_OF_METRICS_FACTORY_CLASS_NAME,
+        "org.apache.pinot.plugin.metrics.yammer.YammerMetricsFactory");
+    PinotMetricUtils.init(pinotConfiguration);
+    return new ControllerMetrics(new YammerMetricsRegistry());
+  }
+
+  /**
+   * Tests the {@link AbstractMetrics} APIs relating to query phases
+   */
+  @Test
+  public void testQueryPhases() {
+    final ControllerMetrics testMetrics = buildTestMetrics();
+    final MetricsInspector inspector = new 
MetricsInspector(testMetrics.getMetricsRegistry());
+
+    // Establish dummy values to be used in the test
+    final AbstractMetrics.QueryPhase testPhase = () -> "testPhase";
+    Assert.assertEquals(testPhase.getDescription(), "");
+    final String testTableName = "tbl_testQueryPhases";
+    final String testTableName2 = "tbl2_testQueryPhases";
+
+    // Add a phase timing, check for correctness
+    testMetrics.addPhaseTiming(testTableName, testPhase, 1, TimeUnit.SECONDS);
+    final MetricName tbl1Metric = inspector.lastMetric();
+    Assert.assertEquals(inspector.getTimer(tbl1Metric).sum(), 1000);
+
+    // Add to the existing timer, using different API
+    testMetrics.addPhaseTiming(testTableName, testPhase, 444000000);
+    Assert.assertEquals(inspector.getTimer(tbl1Metric).sum(), 1444);
+
+    // Add phase timing to a different table. Verify new timer is set up 
correctly, old timer is not affected
+    testMetrics.addPhaseTiming(testTableName2, testPhase, 22, 
TimeUnit.MILLISECONDS);
+    final MetricName tbl2Metric = inspector.lastMetric();
+    Assert.assertEquals(inspector.getTimer(tbl2Metric).sum(), 22);
+    Assert.assertEquals(inspector.getTimer(tbl1Metric).sum(), 1444);
+
+    // Remove both timers. Verify the metrics registry is now empty
+    testMetrics.removePhaseTiming(testTableName, testPhase);
+    testMetrics.removePhaseTiming(testTableName2, testPhase);
+    Assert.assertTrue(testMetrics.getMetricsRegistry().allMetrics().isEmpty());
+  }
+
+  /**
+   * Tests the {@link AbstractMetrics} APIs relating to timer metrics
+   */
+  @Test
+  public void testTimerMetrics() {
+    ControllerMetrics testMetrics = buildTestMetrics();
+    MetricsInspector inspector = new 
MetricsInspector(testMetrics.getMetricsRegistry());
+    String tableName = "tbl_testTimerMetrics";
+    String keyName = "keyName";
+    ControllerTimer timer = ControllerTimer.IDEAL_STATE_UPDATE_TIME_MS;
+
+    // Test timed table APIs
+    testMetrics.addTimedTableValue(tableName, timer, 6, TimeUnit.SECONDS);
+    final MetricName t1Metric = inspector.lastMetric();
+    Assert.assertEquals(inspector.getTimer(t1Metric).sum(), 6000);
+    testMetrics.addTimedTableValue(tableName, keyName, timer, 500, 
TimeUnit.MILLISECONDS);
+    final MetricName t2Metric = inspector.lastMetric();
+    Assert.assertEquals(inspector.getTimer(t2Metric).sum(), 500);
+
+    // Test timed value APIs
+    testMetrics.addTimedValue(timer, 40, TimeUnit.MILLISECONDS);
+    final MetricName t3Metric = inspector.lastMetric();
+    Assert.assertEquals(inspector.getTimer(t3Metric).sum(), 40);
+    testMetrics.addTimedValue(keyName, timer, 3, TimeUnit.MILLISECONDS);
+    final MetricName t4Metric = inspector.lastMetric();
+    Assert.assertEquals(inspector.getTimer(t4Metric).sum(), 3);
+
+    // Remove added timers and verify the metrics registry is now empty
+    Assert.assertEquals(testMetrics.getMetricsRegistry().allMetrics().size(), 
4);
+    testMetrics.removeTableTimer(tableName, timer);
+    testMetrics.removeTimer(t2Metric.getName());
+    testMetrics.removeTimer(t3Metric.getName());
+    testMetrics.removeTimer(t4Metric.getName());
+    Assert.assertTrue(testMetrics.getMetricsRegistry().allMetrics().isEmpty());
+  }
+
+  /**
+   * Tests the {@link AbstractMetrics} APIs relating to metered metrics
+   */
+  @Test
+  public void testMeteredMetrics() {
+    final ControllerMetrics testMetrics = buildTestMetrics();
+    final MetricsInspector inspector = new 
MetricsInspector(testMetrics.getMetricsRegistry());
+    final String tableName = "tbl_testMeteredMetrics";
+    final String keyName = "keyName";
+    final ControllerMeter meter = 
ControllerMeter.CONTROLLER_INSTANCE_POST_ERROR;
+    final ControllerMeter meter2 = 
ControllerMeter.CONTROLLER_PERIODIC_TASK_ERROR;
+
+    // Holder for the most recently seen Metric
+    final MetricName[] currentMetric = new MetricName[1];
+
+    // When a new metric is expected we'll call this lambda to assert the 
metric was created, and update currentMetric
+    final Runnable expectNewMetric = () -> {
+      Assert.assertNotEquals(inspector.lastMetric(), currentMetric[0]);
+      currentMetric[0] = inspector.lastMetric();
+    };
+
+    // Lambda to verify that the latest metric has the expected value, and its 
creation was expected
+    final IntConsumer expectMeteredCount = expected -> {
+      Assert.assertEquals(inspector.getMetered(currentMetric[0]).count(), 
expected);
+      Assert.assertEquals(currentMetric[0], inspector.lastMetric());
+    };
+
+    // Test global meter APIs
+    testMetrics.addMeteredGlobalValue(meter, 5);
+    expectNewMetric.run();
+    expectMeteredCount.accept(5);
+    testMetrics.addMeteredGlobalValue(meter, 4, 
testMetrics.getMeteredValue(meter));
+    expectMeteredCount.accept(9);
+
+    // Test meter with key APIs
+    testMetrics.addMeteredValue(keyName, meter, 9);
+    expectNewMetric.run();

Review Comment:
   let's run `expectMeteredCount` for `addMeteredValue(keyName, meter, 9)` as 
well



-- 
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: commits-unsubscr...@pinot.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org
For additional commands, e-mail: commits-h...@pinot.apache.org

Reply via email to