nsivabalan commented on code in PR #13660: URL: https://github.com/apache/hudi/pull/13660#discussion_r2249062634
########## hudi-client/hudi-client-common/src/test/java/org/apache/hudi/table/action/savepoint/TestSavepointActionExecutor.java: ########## @@ -0,0 +1,120 @@ +/* + * 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.table.action.savepoint; + +import org.apache.hudi.avro.model.HoodieActionInstant; +import org.apache.hudi.avro.model.HoodieCleanMetadata; +import org.apache.hudi.avro.model.HoodieCleanerPlan; +import org.apache.hudi.common.engine.HoodieLocalEngineContext; +import org.apache.hudi.common.table.timeline.HoodieInstant; +import org.apache.hudi.common.table.timeline.HoodieTimeline; +import org.apache.hudi.common.table.timeline.InstantGenerator; +import org.apache.hudi.common.table.timeline.versioning.v2.InstantComparatorV2; +import org.apache.hudi.common.testutils.MockHoodieTimeline; +import org.apache.hudi.common.util.Option; +import org.apache.hudi.storage.hadoop.HadoopStorageConfiguration; +import org.apache.hudi.table.HoodieTable; + +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.util.stream.Stream; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.Mockito.RETURNS_DEEP_STUBS; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class TestSavepointActionExecutor { + private final HoodieTable mockTable = mock(HoodieTable.class, RETURNS_DEEP_STUBS); + private final HoodieLocalEngineContext engineContext = new HoodieLocalEngineContext(new HadoopStorageConfiguration(false)); + private final String instantTime = "20240121101012345"; + + @Test + void testLastCommitRetained_noCleanCommits() { + String expectedInstant = "20240101101012345"; + when(mockTable.getCompletedCommitsTimeline().firstInstant()).thenReturn(Option.of(new HoodieInstant(HoodieInstant.State.COMPLETED, HoodieTimeline.COMMIT_ACTION, expectedInstant, + InstantComparatorV2.REQUESTED_TIME_BASED_COMPARATOR))); + when(mockTable.getCleanTimeline().lastInstant()).thenReturn(Option.empty()); + + SavepointActionExecutor executor = new SavepointActionExecutor(engineContext, null, mockTable, instantTime, "user", "comment"); + assertEquals(expectedInstant, executor.getLastCommitRetained()); + } + + @Test + void testLastCommitRetained_completedClean() throws IOException { + String expectedInstant = "20240101101012345"; + HoodieInstant cleanInstant = new HoodieInstant(HoodieInstant.State.COMPLETED, HoodieTimeline.CLEAN_ACTION, "20240111101012345", + InstantComparatorV2.REQUESTED_TIME_BASED_COMPARATOR); + when(mockTable.getCompletedCommitsTimeline().firstInstant()).thenReturn(Option.of(new HoodieInstant(HoodieInstant.State.COMPLETED, HoodieTimeline.COMMIT_ACTION, expectedInstant, + InstantComparatorV2.REQUESTED_TIME_BASED_COMPARATOR))); + when(mockTable.getCleanTimeline().lastInstant()).thenReturn(Option.of(cleanInstant)); + + HoodieCleanMetadata cleanMetadata = new HoodieCleanMetadata(); + cleanMetadata.setEarliestCommitToRetain(expectedInstant); + when(mockTable.getActiveTimeline().readCleanMetadata(cleanInstant)).thenReturn(cleanMetadata); + + SavepointActionExecutor executor = new SavepointActionExecutor(engineContext, null, mockTable, instantTime, "user", "comment"); + assertEquals(expectedInstant, executor.getLastCommitRetained()); + } + + @Test + void testLastCommitRetained_requestedClean() throws IOException { + String expectedInstant = "20240101101012345"; + String cleanRequested = "20240111101012345"; + HoodieInstant cleanInstant = new HoodieInstant(HoodieInstant.State.REQUESTED, HoodieTimeline.CLEAN_ACTION, cleanRequested, + InstantComparatorV2.REQUESTED_TIME_BASED_COMPARATOR); + InstantGenerator instantGenerator = mock(InstantGenerator.class); + when(mockTable.getInstantGenerator()).thenReturn(instantGenerator); + when(instantGenerator.createNewInstant(HoodieInstant.State.REQUESTED, HoodieTimeline.CLEAN_ACTION, cleanRequested)) + .thenReturn(cleanInstant); + + when(mockTable.getCompletedCommitsTimeline().firstInstant()).thenReturn(Option.of(new HoodieInstant(HoodieInstant.State.COMPLETED, HoodieTimeline.COMMIT_ACTION, expectedInstant, + InstantComparatorV2.REQUESTED_TIME_BASED_COMPARATOR))); + when(mockTable.getCleanTimeline().lastInstant()).thenReturn(Option.of(cleanInstant)); + + HoodieCleanerPlan cleanerPlan = new HoodieCleanerPlan(); + HoodieActionInstant lastInstantToRetain = HoodieActionInstant.newBuilder().setAction(HoodieTimeline.COMMIT_ACTION) + .setState(HoodieInstant.State.COMPLETED.name()).setTimestamp(expectedInstant).build(); + cleanerPlan.setEarliestInstantToRetain(lastInstantToRetain); + when(mockTable.getActiveTimeline().readCleanerPlan(cleanInstant)).thenReturn(cleanerPlan); + + SavepointActionExecutor executor = new SavepointActionExecutor(engineContext, null, mockTable, instantTime, "user", "comment"); + assertEquals(expectedInstant, executor.getLastCommitRetained()); + } + + @Test + void testLastCommitRetained_completedCleanWithoutEarliestInstantRetained() throws IOException { + String expectedInstant = "20240110101012345"; + HoodieInstant cleanInstant = new HoodieInstant(HoodieInstant.State.COMPLETED, HoodieTimeline.CLEAN_ACTION, "20240111101012345", + InstantComparatorV2.REQUESTED_TIME_BASED_COMPARATOR); + when(mockTable.getCompletedCommitsTimeline().firstInstant()).thenReturn(Option.of(new HoodieInstant(HoodieInstant.State.COMPLETED, HoodieTimeline.COMMIT_ACTION, expectedInstant, + InstantComparatorV2.REQUESTED_TIME_BASED_COMPARATOR))); + when(mockTable.getCleanTimeline().lastInstant()).thenReturn(Option.of(cleanInstant)); + + HoodieCleanMetadata cleanMetadata = new HoodieCleanMetadata(); + when(mockTable.getActiveTimeline().readCleanMetadata(cleanInstant)).thenReturn(cleanMetadata); + + MockHoodieTimeline mockTimeline = new MockHoodieTimeline(Stream.of("20240108101012345", "20240109101012345", expectedInstant), Stream.empty()); Review Comment: can we add more completed instants greater than latest completed clean in this test as well. ########## hudi-client/hudi-client-common/src/main/java/org/apache/hudi/table/action/savepoint/SavepointActionExecutor.java: ########## @@ -145,10 +132,41 @@ public HoodieSavepointMetadata execute() { .saveAsComplete( true, instantGenerator.createNewInstant(HoodieInstant.State.INFLIGHT, HoodieTimeline.SAVEPOINT_ACTION, instantTime), Option.of(metadata), savepointCompletedInstant -> table.getMetaClient().getTableFormat().savepoint(savepointCompletedInstant, table.getContext(), table.getMetaClient(), table.getViewManager())); - LOG.info("Savepoint " + instantTime + " created"); + LOG.info("Savepoint {} created", instantTime); return metadata; } catch (HoodieIOException e) { throw new HoodieSavepointException("Failed to savepoint " + instantTime, e); } } + + @VisibleForTesting + String getLastCommitRetained() { + Option<HoodieInstant> cleanInstant = table.getCleanTimeline().lastInstant(); + // if there are no clean instants, we can use the first completed commit + if (cleanInstant.isEmpty()) { + return table.getCompletedCommitsTimeline().firstInstant().get().requestedTime(); + } + return cleanInstant.map(instant -> { + try { + if (instant.isCompleted()) { + return table.getActiveTimeline().readCleanMetadata(instant) + .getEarliestCommitToRetain(); + } else { + // clean is pending or inflight + return table.getActiveTimeline().readCleanerPlan( + instantGenerator.createNewInstant(REQUESTED, instant.getAction(), instant.requestedTime())) + .getEarliestInstantToRetain().getTimestamp(); + } + } catch (IOException e) { + throw new HoodieSavepointException("Failed to savepoint " + instantTime, e); + } + }).orElseGet(() -> + // If there is no earliest commit to retain, but there are clean instants on the timeline, we must assume KEEP_LATEST_FILE_VERSIONS policy is used. + // In that case, the last commit before the clean instant is the last commit guaranteed to be retained. + table.getActiveTimeline().getWriteTimeline().filterCompletedInstants() Review Comment: if earliest commit to retain is not found, can we add a warn log to recommend users to switch to KEEP_LATEST_COMMITS or KEEP_LATEST_BY_HOURS if they are looking to savepoint earlier commits. we can still proceed w/ whats best we could do for current savepoint trigger. but user can switch the clean policy from next time. -- 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]
