Skip to content

HDDS-12715. add integration tests for debug-replicas-verify-checksums tool #8209

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
HDDS-12715. add integration tests for checksums command
  • Loading branch information
ptlrs committed May 19, 2025
commit 0674e6c5f715ad12377cb769946ae4dff3f7a6ae
Original file line number Diff line number Diff line change
Expand Up @@ -18,18 +18,26 @@
package org.apache.hadoop.ozone.container;

import static org.apache.hadoop.ozone.OzoneConsts.INCREMENTAL_CHUNK_LIST;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.fail;

import com.google.common.base.Preconditions;
import jakarta.annotation.Nonnull;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.UncheckedIOException;
import java.nio.ByteBuffer;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import org.apache.commons.io.IOUtils;
import org.apache.hadoop.conf.StorageUnit;
import org.apache.hadoop.hdds.client.BlockID;
import org.apache.hadoop.hdds.protocol.datanode.proto.ContainerProtos;
Expand Down Expand Up @@ -674,4 +682,41 @@ public static ContainerCommandRequestProto getDummyCommandRequestProto(

return builder.build();
}

/**
* Overwrite the file with random bytes.
*/
public static void corruptFile(File file) {
try {
final int length = (int) file.length();
Path path = file.toPath();
try (InputStream originalInputStream = Files.newInputStream(path)) {
final byte[] original = IOUtils.readFully(originalInputStream, length);
final byte[] corruptedBytes = new byte[length];
ThreadLocalRandom.current().nextBytes(corruptedBytes);
Files.write(path, corruptedBytes, StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.SYNC);
try (InputStream corruptedInputStream = Files.newInputStream(path)) {
assertThat(IOUtils.readFully(corruptedInputStream, length))
.isEqualTo(corruptedBytes)
.isNotEqualTo(original);
}
}
} catch (IOException ex) {
// Fail the test.
throw new UncheckedIOException(ex);
}
}

/**
* Truncate the file to 0 bytes in length.
*/
public static void truncateFile(File file) {
try {
Files.write(file.toPath(), new byte[0], StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.SYNC);
assertEquals(0, file.length());
} catch (IOException ex) {
// Fail the test.
throw new UncheckedIOException(ex);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
import static org.apache.hadoop.hdds.client.ReplicationFactor.ONE;
import static org.apache.hadoop.hdds.client.ReplicationType.RATIS;
import static org.apache.hadoop.hdds.protocol.proto.StorageContainerDatanodeProtocolProtos.ContainerReplicaProto.State;
import static org.apache.hadoop.ozone.container.ContainerTestHelper.corruptFile;
import static org.apache.hadoop.ozone.container.ContainerTestHelper.truncateFile;
import static org.apache.hadoop.ozone.container.common.interfaces.Container.ScanResult;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertEquals;
Expand All @@ -32,20 +34,16 @@
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
import java.time.Duration;
import java.util.Arrays;
import java.util.EnumSet;
import java.util.Objects;
import java.util.Optional;
import java.util.Set;
import java.util.UUID;
import java.util.concurrent.ThreadLocalRandom;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.hadoop.hdds.HddsConfigKeys;
import org.apache.hadoop.hdds.conf.OzoneConfiguration;
import org.apache.hadoop.hdds.protocol.proto.HddsProtos;
Expand Down Expand Up @@ -347,45 +345,5 @@ public static Set<ContainerCorruptions> getAllParamsExcept(
Arrays.asList(exclude).forEach(includeSet::remove);
return includeSet;
}

/**
* Overwrite the file with random bytes.
*/
private static void corruptFile(File file) {
try {
final int length = (int) file.length();

Path path = file.toPath();
final byte[] original = IOUtils.readFully(Files.newInputStream(path), length);

final byte[] corruptedBytes = new byte[length];
ThreadLocalRandom.current().nextBytes(corruptedBytes);

Files.write(path, corruptedBytes,
StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.SYNC);

assertThat(IOUtils.readFully(Files.newInputStream(path), length))
.isEqualTo(corruptedBytes)
.isNotEqualTo(original);
} catch (IOException ex) {
// Fail the test.
throw new UncheckedIOException(ex);
}
}

/**
* Truncate the file to 0 bytes in length.
*/
private static void truncateFile(File file) {
try {
Files.write(file.toPath(), new byte[0],
StandardOpenOption.TRUNCATE_EXISTING, StandardOpenOption.SYNC);

assertEquals(0, file.length());
} catch (IOException ex) {
// Fail the test.
throw new UncheckedIOException(ex);
}
}
}
}
Loading