Copilot commented on code in PR #18690: URL: https://github.com/apache/hudi/pull/18690#discussion_r3186171967
########## hudi-flink-datasource/hudi-flink/src/test/java/org/apache/hudi/sink/bootstrap/TestRLIBootstrapOperator.java: ########## @@ -0,0 +1,233 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.hudi.sink.bootstrap; + +import com.codahale.metrics.MetricRegistry; +import org.apache.hudi.common.config.HoodieMetadataConfig; +import org.apache.hudi.common.data.HoodieData; +import org.apache.hudi.common.model.HoodieRecordGlobalLocation; +import org.apache.hudi.common.table.HoodieTableMetaClient; +import org.apache.hudi.common.util.Option; +import org.apache.hudi.configuration.FlinkOptions; +import org.apache.hudi.metadata.HoodieBackedTableMetadata; +import org.apache.hudi.metadata.HoodieMetadataMetrics; +import org.apache.hudi.sink.utils.MockStreamingRuntimeContext; + +import org.apache.flink.configuration.Configuration; +import org.apache.flink.metrics.Gauge; +import org.apache.flink.metrics.groups.OperatorMetricGroup; +import org.apache.flink.runtime.state.StateInitializationContext; +import org.apache.flink.streaming.api.operators.StreamingRuntimeContext; +import org.junit.jupiter.api.Test; + +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.atLeastOnce; +import static org.mockito.Mockito.doAnswer; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * Tests for {@link RLIBootstrapOperator}, focusing on the Flink metric bridging added in initializeState. + */ +class TestRLIBootstrapOperator { + + @Test + void testSingleGaugeIsRegisteredWhenMetricsEnabled() throws Exception { + MetricRegistry dropwizard = new MetricRegistry(); + dropwizard.register("queryTime", (com.codahale.metrics.Gauge<Long>) () -> 42L); + + HoodieMetadataMetrics mockMetrics = mock(HoodieMetadataMetrics.class); + when(mockMetrics.registry()).thenReturn(dropwizard); + + HoodieBackedTableMetadata mockTable = mockMetadataTable( + HoodieMetadataConfig.newBuilder().enableMetrics(true).build(), + Option.of(mockMetrics)); + + Map<String, Gauge<?>> captured = new HashMap<>(); + OperatorMetricGroup metricGroup = trackingMetricGroup(captured); + + createOperator(mockTable, metricGroup).initializeState(mock(StateInitializationContext.class)); + + assertTrue(captured.containsKey("queryTime"), "Expected 'queryTime' gauge to be registered with Flink"); + assertEquals(42L, captured.get("queryTime").getValue()); + verify(metricGroup, atLeastOnce()).gauge(eq("queryTime"), any()); + } + + @Test + void testMultipleGaugesAreAllRegistered() throws Exception { + MetricRegistry dropwizard = new MetricRegistry(); + dropwizard.register("gauge1", (com.codahale.metrics.Gauge<Long>) () -> 1L); + dropwizard.register("gauge2", (com.codahale.metrics.Gauge<Long>) () -> 2L); + + HoodieMetadataMetrics mockMetrics = mock(HoodieMetadataMetrics.class); + when(mockMetrics.registry()).thenReturn(dropwizard); + + HoodieBackedTableMetadata mockTable = mockMetadataTable( + HoodieMetadataConfig.newBuilder().enableMetrics(true).build(), + Option.of(mockMetrics)); + + Map<String, Gauge<?>> captured = new HashMap<>(); + OperatorMetricGroup metricGroup = trackingMetricGroup(captured); + + createOperator(mockTable, metricGroup).initializeState(mock(StateInitializationContext.class)); + + assertEquals(2, captured.size(), "Both gauges should be bridged to Flink"); + assertEquals(1L, captured.get("gauge1").getValue()); + assertEquals(2L, captured.get("gauge2").getValue()); + } + + @Test + void testNoGaugesRegisteredWhenMetricsDisabled() throws Exception { + HoodieBackedTableMetadata mockTable = mockMetadataTable( + HoodieMetadataConfig.newBuilder().enableMetrics(false).build(), + Option.empty()); + + OperatorMetricGroup metricGroup = mock(OperatorMetricGroup.class); + + createOperator(mockTable, metricGroup).initializeState(mock(StateInitializationContext.class)); + + verify(metricGroup, never()).gauge(any(), any()); + } + + @Test + void testNoGaugesRegisteredWhenMetricsObjectAbsent() throws Exception { + // Metrics flag enabled but HoodieMetadataMetrics object is absent (e.g. metrics not initialised) + HoodieBackedTableMetadata mockTable = mockMetadataTable( + HoodieMetadataConfig.newBuilder().enableMetrics(true).build(), + Option.empty()); + + OperatorMetricGroup metricGroup = mock(OperatorMetricGroup.class); + + createOperator(mockTable, metricGroup).initializeState(mock(StateInitializationContext.class)); + + verify(metricGroup, never()).gauge(any(), any()); + } + + @Test + void testGaugeValueReflectsLiveDropwizardState() throws Exception { + long[] counter = {0L}; + MetricRegistry dropwizard = new MetricRegistry(); + dropwizard.register("liveCounter", (com.codahale.metrics.Gauge<Long>) () -> counter[0]); + + HoodieMetadataMetrics mockMetrics = mock(HoodieMetadataMetrics.class); + when(mockMetrics.registry()).thenReturn(dropwizard); + + HoodieBackedTableMetadata mockTable = mockMetadataTable( + HoodieMetadataConfig.newBuilder().enableMetrics(true).build(), + Option.of(mockMetrics)); + + Map<String, Gauge<?>> captured = new HashMap<>(); + createOperator(mockTable, trackingMetricGroup(captured)).initializeState(mock(StateInitializationContext.class)); + + counter[0] = 99L; + assertEquals(99L, captured.get("liveCounter").getValue(), + "Flink gauge should delegate to live Dropwizard gauge, not snapshot the value"); + } + + // ------------------------------------------------------------------------- + // Helpers + // ------------------------------------------------------------------------- + + private HoodieBackedTableMetadata mockMetadataTable( + HoodieMetadataConfig config, Option<HoodieMetadataMetrics> metrics) throws Exception { + HoodieBackedTableMetadata mockTable = mock(HoodieBackedTableMetadata.class); + when(mockTable.getMetadataConfig()).thenReturn(config); + when(mockTable.getMetrics()).thenReturn(metrics); + HoodieData<HoodieRecordGlobalLocation> emptyData = mock(HoodieData.class); + when(emptyData.collectAsList()).thenReturn(Collections.emptyList()); + when(mockTable.readRecordIndexLocations(any(HoodieData.class))).thenReturn(emptyData); + return mockTable; Review Comment: `mockMetadataTable` stubs `readRecordIndexLocations(HoodieData<String>)`, but `RLIBootstrapOperator#preLoadRLIRecords` calls the overload `readRecordIndexLocations(SerializableFunctionUnchecked<List<FileSlice>, List<FileSlice>>)` (returns `HoodiePairData`). With the current stubbing, the mock returns `null` for the overload used by the operator, causing an NPE when `rliData.forEach(...)` is invoked during `initializeState`. Update the stub to match the overload used by `RLIBootstrapOperator` and return an empty `HoodiePairData` (or a mock whose `forEach` is a no-op). -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
