Skip to content

Commit

Permalink
[HUDI-5041] Fix lock metric register confict error (#6968)
Browse files Browse the repository at this point in the history
Co-authored-by: hbg <bingeng.huang@shopee.com>
  • Loading branch information
hbgstc123 and hbg authored Oct 19, 2022
1 parent 3c8988c commit e6eb4e6
Showing 1 changed file with 11 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,15 +18,15 @@

package org.apache.hudi.client.transaction.lock.metrics;

import org.apache.hudi.common.util.HoodieTimer;
import org.apache.hudi.config.HoodieWriteConfig;
import org.apache.hudi.metrics.Metrics;

import com.codahale.metrics.Counter;
import com.codahale.metrics.MetricRegistry;
import com.codahale.metrics.SlidingWindowReservoir;
import com.codahale.metrics.Timer;

import org.apache.hudi.common.util.HoodieTimer;
import org.apache.hudi.config.HoodieWriteConfig;
import org.apache.hudi.metrics.Metrics;

import java.util.concurrent.TimeUnit;

public class HoodieLockMetrics {
Expand All @@ -46,6 +46,7 @@ public class HoodieLockMetrics {
private transient Counter failedLockAttempts;
private transient Timer lockDuration;
private transient Timer lockApiRequestDuration;
private static final Object REGISTRY_LOCK = new Object();

public HoodieLockMetrics(HoodieWriteConfig writeConfig) {
this.isMetricsEnabled = writeConfig.isLockingMetricsEnabled();
Expand All @@ -69,10 +70,12 @@ private String getMetricsName(String metric) {

private Timer createTimerForMetrics(MetricRegistry registry, String metric) {
String metricName = getMetricsName(metric);
if (registry.getMetrics().get(metricName) == null) {
lockDuration = new Timer(new SlidingWindowReservoir(keepLastNtimes));
registry.register(metricName, lockDuration);
return lockDuration;
synchronized (REGISTRY_LOCK) {
if (registry.getMetrics().get(metricName) == null) {
lockDuration = new Timer(new SlidingWindowReservoir(keepLastNtimes));
registry.register(metricName, lockDuration);
return lockDuration;
}
}
return (Timer) registry.getMetrics().get(metricName);
}
Expand Down

0 comments on commit e6eb4e6

Please sign in to comment.