1
0

[HUDI-2213] Remove unnecessary parameter for HoodieMetrics constructor and fix NPE in UT (#3333)

This commit is contained in:
Xuedong Luan
2021-07-23 19:57:35 +08:00
committed by GitHub
parent 2c910ee3af
commit 71e14cf866
9 changed files with 20 additions and 19 deletions

View File

@@ -130,7 +130,7 @@ public abstract class AbstractHoodieWriteClient<T extends HoodieRecordPayload, I
public AbstractHoodieWriteClient(HoodieEngineContext context, HoodieWriteConfig writeConfig, public AbstractHoodieWriteClient(HoodieEngineContext context, HoodieWriteConfig writeConfig,
Option<EmbeddedTimelineService> timelineService) { Option<EmbeddedTimelineService> timelineService) {
super(context, writeConfig, timelineService); super(context, writeConfig, timelineService);
this.metrics = new HoodieMetrics(config, config.getTableName()); this.metrics = new HoodieMetrics(config);
this.index = createIndex(writeConfig); this.index = createIndex(writeConfig);
this.txnManager = new TransactionManager(config, fs); this.txnManager = new TransactionManager(config, fs);
} }

View File

@@ -54,9 +54,9 @@ public class HoodieMetrics {
private Timer clusteringTimer = null; private Timer clusteringTimer = null;
private Timer indexTimer = null; private Timer indexTimer = null;
public HoodieMetrics(HoodieWriteConfig config, String tableName) { public HoodieMetrics(HoodieWriteConfig config) {
this.config = config; this.config = config;
this.tableName = tableName; this.tableName = config.getTableName();
if (config.isMetricsOn()) { if (config.isMetricsOn()) {
Metrics.init(config); Metrics.init(config);
this.rollbackTimerName = getMetricsName("timer", HoodieTimeline.ROLLBACK_ACTION); this.rollbackTimerName = getMetricsName("timer", HoodieTimeline.ROLLBACK_ACTION);

View File

@@ -53,9 +53,7 @@ public class Metrics {
} }
reporter.start(); reporter.start();
Runtime.getRuntime().addShutdownHook(new Thread(() -> { Runtime.getRuntime().addShutdownHook(new Thread(this::reportAndCloseReporter));
reportAndCloseReporter();
}));
} }
private void reportAndCloseReporter() { private void reportAndCloseReporter() {

View File

@@ -41,20 +41,20 @@ public class MetricsReporterFactory {
private static final Logger LOG = LogManager.getLogger(MetricsReporterFactory.class); private static final Logger LOG = LogManager.getLogger(MetricsReporterFactory.class);
public static MetricsReporter createReporter(HoodieWriteConfig config, MetricRegistry registry) { public static MetricsReporter createReporter(HoodieWriteConfig config, MetricRegistry registry) {
MetricsReporterType type = config.getMetricsReporterType(); String reporterClassName = config.getMetricReporterClassName();
MetricsReporter reporter = null;
if (!StringUtils.isNullOrEmpty(config.getMetricReporterClassName())) { if (!StringUtils.isNullOrEmpty(reporterClassName)) {
Object instance = ReflectionUtils Object instance = ReflectionUtils.loadClass(
.loadClass(config.getMetricReporterClassName(), reporterClassName, new Class<?>[] {Properties.class, MetricRegistry.class}, config.getProps(), registry);
new Class<?>[] {Properties.class, MetricRegistry.class}, config.getProps(), registry);
if (!(instance instanceof AbstractUserDefinedMetricsReporter)) { if (!(instance instanceof AbstractUserDefinedMetricsReporter)) {
throw new HoodieException(config.getMetricReporterClassName() throw new HoodieException(config.getMetricReporterClassName()
+ " is not a subclass of AbstractUserDefinedMetricsReporter"); + " is not a subclass of AbstractUserDefinedMetricsReporter");
} }
return (MetricsReporter) instance; return (MetricsReporter) instance;
} }
MetricsReporterType type = config.getMetricsReporterType();
MetricsReporter reporter = null;
switch (type) { switch (type) {
case GRAPHITE: case GRAPHITE:
reporter = new MetricsGraphiteReporter(config, registry); reporter = new MetricsGraphiteReporter(config, registry);

View File

@@ -41,7 +41,7 @@ public class TestHoodieConsoleMetrics {
when(config.getTableName()).thenReturn("console_metrics_test"); when(config.getTableName()).thenReturn("console_metrics_test");
when(config.isMetricsOn()).thenReturn(true); when(config.isMetricsOn()).thenReturn(true);
when(config.getMetricsReporterType()).thenReturn(MetricsReporterType.CONSOLE); when(config.getMetricsReporterType()).thenReturn(MetricsReporterType.CONSOLE);
new HoodieMetrics(config, "raw_table"); new HoodieMetrics(config);
} }
@AfterEach @AfterEach

View File

@@ -52,7 +52,7 @@ public class TestHoodieJmxMetrics {
when(config.getMetricsReporterType()).thenReturn(MetricsReporterType.JMX); when(config.getMetricsReporterType()).thenReturn(MetricsReporterType.JMX);
when(config.getJmxHost()).thenReturn("localhost"); when(config.getJmxHost()).thenReturn("localhost");
when(config.getJmxPort()).thenReturn(String.valueOf(NetworkTestUtils.nextFreePort())); when(config.getJmxPort()).thenReturn(String.valueOf(NetworkTestUtils.nextFreePort()));
new HoodieMetrics(config, "raw_table"); new HoodieMetrics(config);
registerGauge("jmx_metric1", 123L); registerGauge("jmx_metric1", 123L);
assertEquals("123", Metrics.getInstance().getRegistry().getGauges() assertEquals("123", Metrics.getInstance().getRegistry().getGauges()
.get("jmx_metric1").getValue().toString()); .get("jmx_metric1").getValue().toString());
@@ -65,7 +65,7 @@ public class TestHoodieJmxMetrics {
when(config.getMetricsReporterType()).thenReturn(MetricsReporterType.JMX); when(config.getMetricsReporterType()).thenReturn(MetricsReporterType.JMX);
when(config.getJmxHost()).thenReturn("localhost"); when(config.getJmxHost()).thenReturn("localhost");
when(config.getJmxPort()).thenReturn(String.valueOf(NetworkTestUtils.nextFreePort())); when(config.getJmxPort()).thenReturn(String.valueOf(NetworkTestUtils.nextFreePort()));
new HoodieMetrics(config, "raw_table"); new HoodieMetrics(config);
registerGauge("jmx_metric2", 123L); registerGauge("jmx_metric2", 123L);
assertEquals("123", Metrics.getInstance().getRegistry().getGauges() assertEquals("123", Metrics.getInstance().getRegistry().getGauges()
.get("jmx_metric2").getValue().toString()); .get("jmx_metric2").getValue().toString());

View File

@@ -50,8 +50,9 @@ public class TestHoodieMetrics {
@BeforeEach @BeforeEach
void setUp() { void setUp() {
when(config.isMetricsOn()).thenReturn(true); when(config.isMetricsOn()).thenReturn(true);
when(config.getTableName()).thenReturn("raw_table");
when(config.getMetricsReporterType()).thenReturn(MetricsReporterType.INMEMORY); when(config.getMetricsReporterType()).thenReturn(MetricsReporterType.INMEMORY);
metrics = new HoodieMetrics(config, "raw_table"); metrics = new HoodieMetrics(config);
} }
@AfterEach @AfterEach

View File

@@ -46,10 +46,11 @@ public class TestPrometheusReporter {
@Test @Test
public void testRegisterGauge() { public void testRegisterGauge() {
when(config.isMetricsOn()).thenReturn(true); when(config.isMetricsOn()).thenReturn(true);
when(config.getTableName()).thenReturn("foo");
when(config.getMetricsReporterType()).thenReturn(MetricsReporterType.PROMETHEUS); when(config.getMetricsReporterType()).thenReturn(MetricsReporterType.PROMETHEUS);
when(config.getPrometheusPort()).thenReturn(9090); when(config.getPrometheusPort()).thenReturn(9090);
assertDoesNotThrow(() -> { assertDoesNotThrow(() -> {
new HoodieMetrics(config, "raw_table"); new HoodieMetrics(config);
}); });
} }
} }

View File

@@ -48,6 +48,7 @@ public class TestPushGateWayReporter {
@Test @Test
public void testRegisterGauge() { public void testRegisterGauge() {
when(config.isMetricsOn()).thenReturn(true); when(config.isMetricsOn()).thenReturn(true);
when(config.getTableName()).thenReturn("foo");
when(config.getMetricsReporterType()).thenReturn(MetricsReporterType.PROMETHEUS_PUSHGATEWAY); when(config.getMetricsReporterType()).thenReturn(MetricsReporterType.PROMETHEUS_PUSHGATEWAY);
when(config.getPushGatewayHost()).thenReturn("localhost"); when(config.getPushGatewayHost()).thenReturn("localhost");
when(config.getPushGatewayPort()).thenReturn(9091); when(config.getPushGatewayPort()).thenReturn(9091);
@@ -57,7 +58,7 @@ public class TestPushGateWayReporter {
when(config.getPushGatewayRandomJobNameSuffix()).thenReturn(false); when(config.getPushGatewayRandomJobNameSuffix()).thenReturn(false);
assertDoesNotThrow(() -> { assertDoesNotThrow(() -> {
new HoodieMetrics(config, "raw_table"); new HoodieMetrics(config);
}); });
registerGauge("pushGateWayReporter_metric", 123L); registerGauge("pushGateWayReporter_metric", 123L);