tibrewalpratik17 commented on code in PR #14064: URL: https://github.com/apache/pinot/pull/14064#discussion_r1775942903
########## pinot-common/src/test/java/org/apache/pinot/common/metrics/AbstractMetricsTest.java: ########## @@ -268,4 +274,157 @@ public void testMeteredMetrics() { testMetrics.getMetricsRegistry().allMetrics().keySet().forEach(testMetrics.getMetricsRegistry()::removeMetric); Assert.assertTrue(testMetrics.getMetricsRegistry().allMetrics().isEmpty()); } + + @Test + public void testGlobalGaugeMetricsAsyncAddRemove() throws ExecutionException, InterruptedException { + ControllerMetrics controllerMetrics = buildTestMetrics(); + int gaugeOperationsRuntimeMs = 100; + + ExecutorService executorService = Executors.newFixedThreadPool(2); + long endTime = System.currentTimeMillis() + gaugeOperationsRuntimeMs; + + Future<?> addFuture = executorService.submit(() -> { + while (System.currentTimeMillis() < endTime + gaugeOperationsRuntimeMs) { + controllerMetrics.addValueToGlobalGauge(ControllerGauge.VERSION, 1L); + } + }); + Future<?> removeFuture = executorService.submit(() -> { + while (System.currentTimeMillis() < endTime) { + controllerMetrics.removeGauge(ControllerGauge.VERSION.getGaugeName()); + } + }); + + addFuture.get(); + removeFuture.get(); + + executorService.shutdown(); + boolean terminated = executorService.awaitTermination(1, TimeUnit.SECONDS); + Assert.assertTrue(terminated, "Tasks should complete and executor should shut down after 1 seconds."); + Assert.assertFalse(controllerMetrics.getMetricsRegistry().allMetrics().isEmpty()); + + Long gaugeValue = controllerMetrics.getGaugeValue(ControllerGauge.VERSION.getGaugeName()); + Assert.assertNotNull(gaugeValue); + Assert.assertTrue(gaugeValue > 0); + } + + @Test + public void testTableGaugeMetricsAsyncAddRemove() throws ExecutionException, InterruptedException { + ControllerMetrics controllerMetrics = buildTestMetrics(); + int gaugeOperationsRuntimeMs = 100; + + ExecutorService executorService = Executors.newFixedThreadPool(2); + long endTime = System.currentTimeMillis() + gaugeOperationsRuntimeMs; + + Future<?> addFuture = executorService.submit(() -> { + while (System.currentTimeMillis() < endTime + gaugeOperationsRuntimeMs) { + controllerMetrics.addValueToTableGauge("test_table", ControllerGauge.VERSION, 1L); + } + }); + Future<?> removeFuture = executorService.submit(() -> { + while (System.currentTimeMillis() < endTime) { + controllerMetrics.removeTableGauge("test_table", ControllerGauge.VERSION); + } + }); + + addFuture.get(); + removeFuture.get(); + + executorService.shutdown(); + boolean terminated = executorService.awaitTermination(1, TimeUnit.SECONDS); + Assert.assertTrue(terminated, "Tasks should complete and executor should shut down after 1 seconds."); + Assert.assertFalse(controllerMetrics.getMetricsRegistry().allMetrics().isEmpty()); + + Long gaugeValue = controllerMetrics.getGaugeValue(ControllerGauge.VERSION.getGaugeName() + ".test_table"); + Assert.assertNotNull(gaugeValue); + Assert.assertTrue(gaugeValue > 0); + } + + @Test + public void testInitializeGlobalMeters() { + ControllerMetrics controllerMetrics = buildTestMetrics(); + + controllerMetrics.initializeGlobalMeters(); + Assert.assertFalse(controllerMetrics.getMetricsRegistry().allMetrics().isEmpty()); + Assert.assertEquals(0, controllerMetrics.getGaugeValue(ControllerGauge.VERSION.getGaugeName())); + } + + @Test + public void testSetOrUpdateGlobalGauges() { + ControllerMetrics controllerMetrics = buildTestMetrics(); + MetricsInspector inspector = new MetricsInspector(controllerMetrics.getMetricsRegistry()); + + controllerMetrics.setOrUpdateGlobalGauge(ControllerGauge.VERSION, () -> 1L); + Assert.assertEquals(MetricValueUtils.getGaugeValue(controllerMetrics, ControllerGauge.VERSION.getGaugeName()), 1); + + controllerMetrics.setOrUpdateGlobalGauge(ControllerGauge.VERSION, (Supplier<Long>) () -> 2L); + Assert.assertEquals(MetricValueUtils.getGaugeValue(controllerMetrics, ControllerGauge.VERSION.getGaugeName()), 2); + + controllerMetrics.setValueOfGlobalGauge(ControllerGauge.OFFLINE_TABLE_COUNT, "suffix", 3L); + Assert.assertEquals(MetricValueUtils.getGaugeValue(controllerMetrics, + ControllerGauge.OFFLINE_TABLE_COUNT.getGaugeName() + ".suffix"), 3); Review Comment: can we also add one UT for `setValueOfGlobalGauge(final G gauge, final long value)` without suffix? -- 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