wombatu-kun commented on code in PR #19968: URL: https://github.com/apache/hudi/pull/19968#discussion_r4022629142
########## hudi-flink-datasource/hudi-flink/src/test/java/org/apache/hudi/sink/compact/handler/TestDataTableCompactHandler.java: ########## @@ -0,0 +1,121 @@ +/* + * 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.compact.handler; + +import org.apache.hudi.client.HoodieFlinkWriteClient; +import org.apache.hudi.common.model.CompactionOperation; +import org.apache.hudi.exception.HoodieException; +import org.apache.hudi.sink.compact.CompactionCommitEvent; +import org.apache.hudi.sink.compact.CompactionPlanEvent; +import org.apache.hudi.sink.utils.NonThrownExecutor; +import org.apache.hudi.table.HoodieFlinkTable; + +import org.apache.flink.metrics.Counter; +import org.apache.flink.metrics.MetricGroup; +import org.apache.flink.util.Collector; +import org.apache.flink.util.ExceptionUtils; +import org.apache.flink.util.function.ThrowingRunnable; +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; + +import java.util.concurrent.atomic.AtomicReference; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +/** + * Tests that a compaction failure occurring on the async {@link NonThrownExecutor} path is + * reflected in the {@code compactionErrorCount} metric instead of being swallowed silently. + */ +class TestDataTableCompactHandler { + + @Test + @SuppressWarnings("unchecked") + void testAsyncCompactionFailureIncrementsErrorMetric() throws Exception { + HoodieFlinkWriteClient<?> writeClient = mock(HoodieFlinkWriteClient.class); + HoodieFlinkTable<?> table = mock(HoodieFlinkTable.class); + when(writeClient.getHoodieTable()).thenReturn((HoodieFlinkTable) table); + + RuntimeException compactionFailure = new RuntimeException("compaction boom"); + DataTableCompactHandler handler = new DataTableCompactHandler(writeClient, 0) { + @Override + protected void doCompaction(CompactionPlanEvent event, org.apache.flink.util.Collector<CompactionCommitEvent> collector, boolean needReloadMetaClient) throws Exception { + throw compactionFailure; + } + }; + + MetricGroup metricGroup = mock(MetricGroup.class); + ArgumentCaptor<Counter> counterCaptor = ArgumentCaptor.forClass(Counter.class); + handler.registerMetrics(metricGroup); + verify(metricGroup).counter(anyString(), counterCaptor.capture()); + Counter errorCounter = counterCaptor.getValue(); + assertEquals(0, errorCounter.getCount()); + + CompactionOperation operation = mock(CompactionOperation.class); + when(operation.getFileId()).thenReturn("file-1"); + CompactionPlanEvent event = new CompactionPlanEvent("001", operation, 0, false, false); + + AtomicReference<CompactionCommitEvent> collected = new AtomicReference<>(); + Collector<CompactionCommitEvent> collector = new Collector<CompactionCommitEvent>() { + @Override + public void collect(CompactionCommitEvent record) { + collected.set(record); + } + + @Override + public void close() { + // no-op + } + }; + + NonThrownExecutor executor = new SyncFailFastExecutor(); + handler.compact(executor, event, collector, false); + + assertEquals(1, errorCounter.getCount(), "compaction failure on the async path must be reflected in the error metric"); + assertTrue(collected.get().isFailed(), "a failed commit event must still be emitted downstream"); + } + + /** + * Executes actions synchronously so the async failure hook runs on the calling test thread. + */ + private static class SyncFailFastExecutor extends NonThrownExecutor { Review Comment: `SyncFailFastExecutor` re-implements `MockCoordinatorExecutor` from `org.apache.hudi.sink.utils`, down to the unused exception hook it passes to super. `CompactFunctionWrapper` already builds that executor for `CompactOperator` as `new MockCoordinatorExecutor(new MockOperatorCoordinatorContext(new OperatorID(), 1))` - reusing it here would drop the inner class. ########## hudi-flink-datasource/hudi-flink/src/main/java/org/apache/hudi/metrics/FlinkCompactionMetrics.java: ########## @@ -72,6 +74,14 @@ public class FlinkCompactionMetrics extends FlinkWriteMetrics { */ private long compactionStateSignal; + /** + * Counter for the number of compaction operations that failed, including the ones swallowed Review Comment: The offline compactor forces `compaction.operation.execute.async.enabled` to false, and the sync branch of `DataTableCompactHandler.compact` never calls `markCompactionFailed`, so its failures are not counted. Is async-only intentional - if so, could the javadoc say so instead of reading as a count of every failed compaction operation? -- 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]
