yihua commented on code in PR #18477: URL: https://github.com/apache/hudi/pull/18477#discussion_r3045565009
########## hudi-flink-datasource/hudi-flink/src/main/java/org/apache/hudi/sink/compact/handler/CompositeTableServiceHandler.java: ########## @@ -0,0 +1,51 @@ +/* + * 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.common.util.Option; + +import java.io.Closeable; +import java.util.function.Consumer; + +/** + * Base class for composite handlers that encapsulate data table and metadata table service handlers. + * + * <p>The composite subclasses expose a single entry point to Flink operators while keeping the + * specific handler references, and routing inside the handler layer. + * + * @param <H> table service handler type + */ +public abstract class CompositeTableServiceHandler<H> implements Closeable { + protected final Option<H> dataTableHandler; + protected final Option<H> metadataTableHandler; + + protected CompositeTableServiceHandler(Option<H> dataTableHandler, Option<H> metadataTableHandler) { + this.dataTableHandler = dataTableHandler; + this.metadataTableHandler = metadataTableHandler; + } + + protected H getHandler(boolean isMetadataTable) { + return isMetadataTable ? metadataTableHandler.get() : dataTableHandler.get(); + } + + protected void forEachHandler(Consumer<H> consumer) { Review Comment: 🤖 Could this throw `NoSuchElementException` at runtime? If a subclass calls `getHandler(true)` but `metadataTableHandler` is `Option.empty()` (e.g. metadata table is disabled), the unconditional `.get()` will blow up with a confusing exception. Would it be safer to guard this with a presence check and throw a descriptive `IllegalStateException`, or have each subclass validate before delegating? ########## hudi-flink-datasource/hudi-flink/src/test/java/org/apache/hudi/sink/compact/handler/TestCompositeHandlers.java: ########## @@ -0,0 +1,116 @@ +/* + * 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.WriteStatus; +import org.apache.hudi.common.util.Option; +import org.apache.hudi.metrics.FlinkCompactionMetrics; +import org.apache.hudi.sink.compact.CompactionCommitEvent; +import org.apache.hudi.sink.compact.CompactionPlanEvent; +import org.apache.hudi.util.Lazy; + +import org.apache.flink.streaming.api.operators.Output; +import org.apache.flink.streaming.runtime.streamrecord.StreamRecord; +import org.junit.jupiter.api.Test; +import org.mockito.InOrder; + +import java.util.Collections; + +import static org.mockito.Mockito.inOrder; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.verifyNoMoreInteractions; +import static org.mockito.Mockito.same; + +class TestCompositeHandlers { + + @Test + void testCommitHandlerRoutesEventsToMatchingTableServiceHandler() { + CompactCommitHandler dataHandler = mock(CompactCommitHandler.class); + CompactCommitHandler metadataHandler = mock(CompactCommitHandler.class); + CompositeCompactCommitHandler handler = new CompositeCompactCommitHandler( + Option.of(Lazy.eagerly(dataHandler)), + Option.of(Lazy.eagerly(metadataHandler))); + FlinkCompactionMetrics metrics = mock(FlinkCompactionMetrics.class); + CompactionCommitEvent dataEvent = + new CompactionCommitEvent("001", "file-1", Collections.<WriteStatus>emptyList(), 0, false, false); + CompactionCommitEvent metadataEvent = + new CompactionCommitEvent("002", "file-2", Collections.<WriteStatus>emptyList(), 0, true, false); + + handler.commitIfNecessary(dataEvent, metrics); + handler.commitIfNecessary(metadataEvent, metrics); + + verify(dataHandler).commitIfNecessary(same(dataEvent), same(metrics)); + verify(metadataHandler).commitIfNecessary(same(metadataEvent), same(metrics)); + verifyNoMoreInteractions(dataHandler, metadataHandler); + } + + @Test + void testCleanHandlerBroadcastsLifecycleCallsToAvailableHandlersOnly() { + CleanHandler dataHandler = mock(CleanHandler.class); + CompositeCleanHandler handler = new CompositeCleanHandler(Option.of(dataHandler), Option.empty()); + + handler.clean(); + handler.startAsyncCleaning(); + handler.waitForCleaningFinish(); + handler.close(); + + verify(dataHandler).clean(); + verify(dataHandler).startAsyncCleaning(); + verify(dataHandler).waitForCleaningFinish(); + verify(dataHandler).close(); + verifyNoMoreInteractions(dataHandler); + } + + @Test + @SuppressWarnings("unchecked") + void testCompactionPlanHandlerPreservesDataThenMetadataBroadcastOrder() { + CompactionPlanHandler dataHandler = mock(CompactionPlanHandler.class); + CompactionPlanHandler metadataHandler = mock(MetadataCompactionPlanHandler.class); + CompositeCompactionPlanHandler handler = + new CompositeCompactionPlanHandler(Option.of(dataHandler), Option.of(metadataHandler)); + FlinkCompactionMetrics metrics = mock(FlinkCompactionMetrics.class); + Output<StreamRecord<CompactionPlanEvent>> output = mock(Output.class); + + handler.collectCompactionOperations(100L, metrics, output); + + InOrder inOrder = inOrder(dataHandler, metadataHandler); + inOrder.verify(dataHandler).collectCompactionOperations(100L, metrics, output); + inOrder.verify(metadataHandler).collectCompactionOperations(100L, metrics, output); + verifyNoMoreInteractions(dataHandler, metadataHandler); + } + + @Test + @SuppressWarnings("unchecked") + void testCompactionPlanHandlerSkipsMissingMetadataServiceHandler() { + CompactionPlanHandler dataHandler = mock(CompactionPlanHandler.class); + CompactionPlanHandler metadataHandler = mock(MetadataCompactionPlanHandler.class); + CompositeCompactionPlanHandler handler = + new CompositeCompactionPlanHandler(Option.of(dataHandler), Option.empty()); + FlinkCompactionMetrics metrics = mock(FlinkCompactionMetrics.class); + Output<StreamRecord<CompactionPlanEvent>> output = mock(Output.class); + + handler.collectCompactionOperations(101L, metrics, output); + + verify(dataHandler).collectCompactionOperations(101L, metrics, output); + verify(metadataHandler, never()).collectCompactionOperations(101L, metrics, output); + verifyNoMoreInteractions(dataHandler, metadataHandler); + } Review Comment: 🤖 This `verify(metadataHandler, never())` assertion is vacuously true — `metadataHandler` was never passed to the composite handler (it's `Option.empty()` on line 108), so Mockito hasn't recorded any interactions on it regardless of the implementation. To actually test the skip behavior you'd want to drop this `verify` and just assert that `dataHandler` was called exactly once (which the line above already does), making the intent clear. ########## hudi-flink-datasource/hudi-flink/src/main/java/org/apache/hudi/sink/compact/handler/CompositeCompactHandler.java: ########## @@ -0,0 +1,70 @@ +/* + * 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.util.Option; +import org.apache.hudi.metrics.FlinkCompactionMetrics; +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.util.FlinkWriteClients; +import org.apache.hudi.util.Lazy; +import org.apache.hudi.util.StreamerUtil; + +import org.apache.flink.api.common.functions.RuntimeContext; +import org.apache.flink.configuration.Configuration; +import org.apache.flink.util.Collector; + +import javax.annotation.Nullable; + +/** + * Composite handler for compaction execution services of the data table and metadata table. + */ +public class CompositeCompactHandler extends CompositeTableServiceHandler<Lazy<CompactHandler>> { + + CompositeCompactHandler(Option<Lazy<CompactHandler>> dataTableHandler, Option<Lazy<CompactHandler>> metadataTableHandler) { + super(dataTableHandler, metadataTableHandler); + } + + public static CompositeCompactHandler create(Configuration conf, RuntimeContext runtimeContext, int taskId) { + HoodieFlinkWriteClient writeClient = FlinkWriteClients.createWriteClient(conf, runtimeContext); + return new CompositeCompactHandler( + Option.of(Lazy.lazily(() -> new CompactHandler(writeClient, taskId))), + Option.of(Lazy.lazily(() -> new MetadataCompactHandler(StreamerUtil.createMetadataWriteClient(writeClient), taskId)))); + } + + public void compact(@Nullable NonThrownExecutor executor, + CompactionPlanEvent event, + Collector<CompactionCommitEvent> collector, + boolean needReloadMetaClient, + FlinkCompactionMetrics compactionMetrics) throws Exception { Review Comment: 🤖 Could you double-check the invariant being relied on here? `getHandler(event.isMetadataTable())` ultimately calls `metadataTableHandler.get()` (from the base class), which throws `NoSuchElementException` if the option is empty. Since `create()` always populates both options, this is safe today — but it's a subtle coupling: the correctness of this call depends on the upstream plan operator never emitting a metadata-table event when metadata compaction is disabled. Would it be worth either asserting that invariant, or having `getHandler` defensively check `isPresent()` in the base class? -- 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]
