errose28 commented on code in PR #6875: URL: https://github.com/apache/ozone/pull/6875#discussion_r1697501785
########## hadoop-hdds/container-service/src/test/java/org/apache/hadoop/ozone/container/checksum/TestContainerChecksumTreeManager.java: ########## @@ -0,0 +1,169 @@ +/* + * 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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.hadoop.ozone.container.checksum; + +import org.apache.hadoop.hdds.conf.OzoneConfiguration; +import org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos; +import org.apache.hadoop.ozone.container.common.helpers.ChunkInfo; +import org.apache.hadoop.ozone.container.keyvalue.KeyValueContainerData; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.io.TempDir; + +import java.io.File; +import java.io.FileInputStream; +import java.io.IOException; +import java.nio.ByteBuffer; +import java.util.Arrays; +import java.util.List; +import java.util.TreeSet; + +import static org.apache.hadoop.ozone.container.checksum.TestContainerMerkleTree.assertTreesSortedAndMatch; +import static org.apache.hadoop.ozone.container.checksum.TestContainerMerkleTree.buildChunk; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class TestContainerChecksumTreeManager { + + private static final long CONTAINER_ID = 1L; + @TempDir + private File testDir; + private KeyValueContainerData container; + private File checksumFile; + private ContainerChecksumTreeManager checksumManager; + + @BeforeEach + public void init() { + container = mock(KeyValueContainerData.class); + when(container.getContainerID()).thenReturn(CONTAINER_ID); + when(container.getMetadataPath()).thenReturn(testDir.getAbsolutePath()); + checksumFile = new File(testDir, CONTAINER_ID + ".tree"); + checksumManager = new ContainerChecksumTreeManager(new OzoneConfiguration()); + } + + @Test + public void testWriteEmptyTreeToFile() throws Exception { + checksumManager.writeContainerDataTree(container, new ContainerMerkleTree()); + ContainerProtos.ContainerChecksumInfo checksumInfo = readFile(); + + assertEquals(CONTAINER_ID, checksumInfo.getContainerID()); + assertTrue(checksumInfo.getDeletedBlocksList().isEmpty()); + ContainerProtos.ContainerMerkleTree treeProto = checksumInfo.getContainerMerkleTree(); + assertEquals(0, treeProto.getDataChecksum()); + assertTrue(treeProto.getBlockMerkleTreeList().isEmpty()); + } + + @Test + public void testWriteEmptyBlockListToFile() throws Exception { + checksumManager.markBlocksAsDeleted(container, new TreeSet<>()); + ContainerProtos.ContainerChecksumInfo checksumInfo = readFile(); + + assertEquals(CONTAINER_ID, checksumInfo.getContainerID()); + assertTrue(checksumInfo.getDeletedBlocksList().isEmpty()); + ContainerProtos.ContainerMerkleTree treeProto = checksumInfo.getContainerMerkleTree(); + assertEquals(0, treeProto.getDataChecksum()); + assertTrue(treeProto.getBlockMerkleTreeList().isEmpty()); + } + + @Test + public void testWriteOnlyTreeToFile() throws Exception { + ContainerMerkleTree tree = buildTestTree(); + checksumManager.writeContainerDataTree(container, tree); + + ContainerProtos.ContainerChecksumInfo checksumInfo = readFile(); + + assertEquals(CONTAINER_ID, checksumInfo.getContainerID()); + assertTrue(checksumInfo.getDeletedBlocksList().isEmpty()); + // TestContainerMerkleTree verifies that going from ContainerMerkleTree to its proto is consistent. + // Therefore, we can use the proto version of our expected tree to check what was written to the file. + assertTreesSortedAndMatch(tree.toProto(), checksumInfo.getContainerMerkleTree()); + } + + @Test + public void testWriteOnlyDeletedBlocksToFile() throws Exception { + List<Long> expectedBlocksToDelete = Arrays.asList(1L, 2L, 3L); + checksumManager.markBlocksAsDeleted(container, new TreeSet<>(expectedBlocksToDelete)); + + ContainerProtos.ContainerChecksumInfo checksumInfo = readFile(); + + assertEquals(CONTAINER_ID, checksumInfo.getContainerID()); + assertEquals(expectedBlocksToDelete, checksumInfo.getDeletedBlocksList()); + ContainerProtos.ContainerMerkleTree treeProto = checksumInfo.getContainerMerkleTree(); + assertEquals(0, treeProto.getDataChecksum()); + assertTrue(treeProto.getBlockMerkleTreeList().isEmpty()); + } + + @Test + public void testDeletedBlocksPreservedOnTreeWrite() throws Exception { + List<Long> expectedBlocksToDelete = Arrays.asList(1L, 2L, 3L); + checksumManager.markBlocksAsDeleted(container, new TreeSet<>(expectedBlocksToDelete)); Review Comment: I added more tests to `TestContainerChecksumTreeManager` for this. What happened is that originally the method required `SortedSet` as input so the extra tests weren't necessary. I made it `Collection` so that the block deleting service could pass anything in and not worry about conversion. The manager would do that in this method. This loosened the contract but I didn't add tests that the internals were still handling it. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
