n3nash commented on a change in pull request #1964: URL: https://github.com/apache/hudi/pull/1964#discussion_r475925115
########## File path: hudi-common/src/test/java/org/apache/hudi/common/table/TestTimelineUtils.java ########## @@ -0,0 +1,160 @@ +/* + * 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.common.table; + +import org.apache.hudi.avro.model.HoodieCleanMetadata; +import org.apache.hudi.avro.model.HoodieCleanPartitionMetadata; +import org.apache.hudi.common.model.HoodieCleaningPolicy; +import org.apache.hudi.common.model.HoodieCommitMetadata; +import org.apache.hudi.common.model.HoodieWriteStat; +import org.apache.hudi.common.table.timeline.HoodieActiveTimeline; +import org.apache.hudi.common.table.timeline.HoodieInstant; +import org.apache.hudi.common.table.timeline.HoodieTimeline; +import org.apache.hudi.common.table.timeline.TimelineMetadataUtils; +import org.apache.hudi.common.table.timeline.TimelineUtils; +import org.apache.hudi.common.testutils.HoodieCommonTestHarness; +import org.apache.hudi.common.util.Option; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.nio.file.Paths; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.HashMap; +import java.util.List; +import java.util.Map; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; + +public class TestTimelineUtils extends HoodieCommonTestHarness { + + @BeforeEach + public void setUp() throws Exception { + initMetaClient(); + } + + @Test + public void testGetPartitions() throws IOException { + HoodieActiveTimeline activeTimeline = metaClient.getActiveTimeline(); + HoodieTimeline activeCommitTimeline = activeTimeline.getCommitTimeline(); + assertTrue(activeCommitTimeline.empty()); + + String olderPartition = "0"; // older partitions that is modified by all cleans + for (int i = 1; i <= 5; i++) { + String ts = i + ""; + HoodieInstant instant = new HoodieInstant(true, HoodieTimeline.COMMIT_ACTION, ts); + activeTimeline.createNewInstant(instant); + activeTimeline.saveAsComplete(instant, Option.of(getCommitMeta(basePath, ts, ts, 2))); + + HoodieInstant cleanInstant = new HoodieInstant(true, HoodieTimeline.CLEAN_ACTION, ts); + activeTimeline.createNewInstant(cleanInstant); + activeTimeline.saveAsComplete(cleanInstant, getCleanMeta(olderPartition, ts)); + } + + metaClient.reloadActiveTimeline(); + + // verify modified partitions included cleaned data + List<String> partitions = TimelineUtils.getPartitionsMutated(metaClient.getActiveTimeline().findInstantsAfter("1", 10)); + assertEquals(5, partitions.size()); + assertEquals(partitions, Arrays.asList(new String[]{"0", "2", "3", "4", "5"})); + + partitions = TimelineUtils.getPartitionsMutated(metaClient.getActiveTimeline().findInstantsInRange("1", "4")); + assertEquals(4, partitions.size()); + assertEquals(partitions, Arrays.asList(new String[]{"0", "2", "3", "4"})); + + // verify only commit actions + partitions = TimelineUtils.getPartitionsWritten(metaClient.getActiveTimeline().findInstantsAfter("1", 10)); + assertEquals(4, partitions.size()); + assertEquals(partitions, Arrays.asList(new String[]{"2", "3", "4", "5"})); + + partitions = TimelineUtils.getPartitionsWritten(metaClient.getActiveTimeline().findInstantsInRange("1", "4")); + assertEquals(3, partitions.size()); + assertEquals(partitions, Arrays.asList(new String[]{"2", "3", "4"})); + } + + @Test + public void testGetPartitionsUnpartitioned() throws IOException { + HoodieActiveTimeline activeTimeline = metaClient.getActiveTimeline(); + HoodieTimeline activeCommitTimeline = activeTimeline.getCommitTimeline(); + assertTrue(activeCommitTimeline.empty()); + + String partitionPath = ""; + for (int i = 1; i <= 5; i++) { + String ts = i + ""; + HoodieInstant instant = new HoodieInstant(true, HoodieTimeline.COMMIT_ACTION, ts); + activeTimeline.createNewInstant(instant); + activeTimeline.saveAsComplete(instant, Option.of(getCommitMeta(basePath, partitionPath, ts, 2))); + + HoodieInstant cleanInstant = new HoodieInstant(true, HoodieTimeline.CLEAN_ACTION, ts); + activeTimeline.createNewInstant(cleanInstant); + activeTimeline.saveAsComplete(cleanInstant, getCleanMeta(partitionPath, ts)); + } + + metaClient.reloadActiveTimeline(); + + // verify modified partitions included cleaned data + List<String> partitions = TimelineUtils.getPartitionsMutated(metaClient.getActiveTimeline().findInstantsAfter("1", 10)); + assertTrue(partitions.isEmpty()); + + partitions = TimelineUtils.getPartitionsMutated(metaClient.getActiveTimeline().findInstantsInRange("1", "4")); + assertTrue(partitions.isEmpty()); + } + + private byte[] getCommitMeta(String basePath, String partition, String commitTs, int count) Review comment: Let's keep the same name as before `getCommitMetadata` & `getCleanMetadata` unless there is a specific need for keeping the names short.. ---------------------------------------------------------------- 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. For queries about this service, please contact Infrastructure at: [email protected]
