Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix flaky gauge value issue #13679

Merged
merged 2 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -451,7 +451,7 @@ public void setValueOfGlobalGauge(final G gauge, final long value) {
setValueOfGauge(value, gaugeName);
}

private void setValueOfGauge(long value, String gaugeName) {
protected void setValueOfGauge(long value, String gaugeName) {
AtomicLong gaugeValue = _gaugeValues.get(gaugeName);
if (gaugeValue == null) {
synchronized (_gaugeValues) {
Expand Down Expand Up @@ -787,8 +787,10 @@ public String composePluginGaugeName(String pluginName, Gauge gauge) {
* @param gaugeName gauge name
*/
public void removeGauge(final String gaugeName) {
_gaugeValues.remove(gaugeName);
removeGaugeFromMetricRegistry(gaugeName);
synchronized (_gaugeValues) {
_gaugeValues.remove(gaugeName);
removeGaugeFromMetricRegistry(gaugeName);
}
}

public void removeTableMeter(final String tableName, final M meter) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
*/
package org.apache.pinot.common.metrics;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.TimeUnit;
import java.util.stream.IntStream;
import org.apache.pinot.plugin.metrics.yammer.YammerMetricsRegistry;
import org.apache.pinot.spi.env.PinotConfiguration;
import org.apache.pinot.spi.metrics.PinotMetricUtils;
Expand Down Expand Up @@ -48,4 +52,33 @@ public void testAddOrUpdateGauge() {
controllerMetrics.removeGauge(metricName);
Assert.assertTrue(controllerMetrics.getMetricsRegistry().allMetrics().isEmpty());
}

@Test
public void testUpdateAndRemoveGauge()
jackjlli marked this conversation as resolved.
Show resolved Hide resolved
throws InterruptedException {
PinotConfiguration pinotConfiguration = new PinotConfiguration();
pinotConfiguration.setProperty(CONFIG_OF_METRICS_FACTORY_CLASS_NAME,
"org.apache.pinot.plugin.metrics.yammer.YammerMetricsFactory");
PinotMetricUtils.init(pinotConfiguration);
ControllerMetrics controllerMetrics = new ControllerMetrics(new YammerMetricsRegistry());
String metricName = "test";

// update and remove gauge simultaneously
ExecutorService service = Executors.newFixedThreadPool(3);
IntStream.range(0, 100).forEach(i -> {
if (i % 5 == 0) {
service.submit(() -> controllerMetrics.removeGauge(metricName));
}
service.submit(() -> controllerMetrics.setValueOfGauge(i, metricName));
});
service.shutdown();
service.awaitTermination(1, TimeUnit.MINUTES);

// The gauge should be present in both map and metrics-registry
Assert.assertNotNull(controllerMetrics.getGaugeValue(metricName));
Assert.assertFalse(controllerMetrics.getMetricsRegistry().allMetrics().isEmpty());

controllerMetrics.removeGauge(metricName);
Assert.assertTrue(controllerMetrics.getMetricsRegistry().allMetrics().isEmpty());
}
}
Loading