|
| 1 | +package kiwi.core.checksum; |
| 2 | + |
| 3 | +import kiwi.core.storage.bitcask.log.LogSegment; |
| 4 | +import kiwi.core.storage.bitcask.log.Record; |
| 5 | + |
| 6 | +import java.io.IOException; |
| 7 | +import java.nio.file.Files; |
| 8 | +import java.nio.file.InvalidPathException; |
| 9 | +import java.nio.file.Path; |
| 10 | +import java.nio.file.Paths; |
| 11 | +import java.util.ArrayList; |
| 12 | +import java.util.List; |
| 13 | +import java.util.concurrent.ExecutorService; |
| 14 | +import java.util.concurrent.Executors; |
| 15 | +import java.util.concurrent.Future; |
| 16 | +import java.util.stream.Stream; |
| 17 | + |
| 18 | +public class Run { |
| 19 | + |
| 20 | + public static void main(String[] args) throws IOException { |
| 21 | + Path logDir = null; |
| 22 | + int numThreads = Runtime.getRuntime().availableProcessors(); |
| 23 | + |
| 24 | + if (args.length == 0) { |
| 25 | + System.err.println("Usage: COMMAND --dir <log-dir> [--threads <num-threads>]"); |
| 26 | + System.exit(1); |
| 27 | + } |
| 28 | + |
| 29 | + for (int i = 0; i < args.length; i++) { |
| 30 | + switch (args[i]) { |
| 31 | + case "--dir": |
| 32 | + case "-d": |
| 33 | + if (i + 1 < args.length) { |
| 34 | + try { |
| 35 | + logDir = Paths.get(args[++i]); |
| 36 | + |
| 37 | + if (!logDir.toFile().isDirectory()) { |
| 38 | + System.err.println("Path for --dir or -d is not a directory"); |
| 39 | + System.exit(1); |
| 40 | + } |
| 41 | + } catch (InvalidPathException e) { |
| 42 | + System.err.println("Invalid path for --dir or -d"); |
| 43 | + System.exit(1); |
| 44 | + } |
| 45 | + } else { |
| 46 | + System.err.println("Missing value for --dir or -d"); |
| 47 | + System.exit(1); |
| 48 | + } |
| 49 | + break; |
| 50 | + case "--threads": |
| 51 | + case "-t": |
| 52 | + if (i + 1 < args.length) { |
| 53 | + try { |
| 54 | + numThreads = Integer.parseInt(args[++i]); |
| 55 | + } catch (NumberFormatException e) { |
| 56 | + System.err.println("Invalid number format for --threads or -t"); |
| 57 | + System.exit(1); |
| 58 | + } |
| 59 | + } else { |
| 60 | + System.err.println("Missing value for --threads or -t"); |
| 61 | + System.exit(1); |
| 62 | + } |
| 63 | + break; |
| 64 | + default: |
| 65 | + System.err.println("Unknown argument: " + args[i]); |
| 66 | + System.exit(1); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + if (logDir == null) { |
| 71 | + System.err.println("Argument '--dir' is required"); |
| 72 | + System.exit(1); |
| 73 | + } |
| 74 | + |
| 75 | + checksum(logDir, numThreads); |
| 76 | + |
| 77 | + System.exit(0); |
| 78 | + } |
| 79 | + |
| 80 | + static void checksum(Path logDir, int numThreads) throws IOException { |
| 81 | + ExecutorService executor = Executors.newFixedThreadPool(numThreads); |
| 82 | + List<Future<?>> futures = new ArrayList<>(); |
| 83 | + |
| 84 | + try (Stream<Path> paths = Files.walk(logDir)) { |
| 85 | + List<LogSegment> segments = paths.filter(Files::isRegularFile) |
| 86 | + .filter(path -> path.getFileName().toString().endsWith(".log")) |
| 87 | + .map((path) -> LogSegment.open(path, true)) |
| 88 | + .toList(); |
| 89 | + |
| 90 | + for (LogSegment segment : segments) { |
| 91 | + futures.add(executor.submit(() -> checkLogSegment(segment))); |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + futures.forEach(future -> { |
| 96 | + try { |
| 97 | + future.get(); |
| 98 | + } catch (Exception e) { |
| 99 | + System.err.println("Error processing log: " + e.getMessage()); |
| 100 | + } |
| 101 | + }); |
| 102 | + |
| 103 | + executor.shutdown(); |
| 104 | + } |
| 105 | + |
| 106 | + static void checkLogSegment(LogSegment segment) { |
| 107 | + long position = 0; |
| 108 | + for (Record record : segment.getRecords()) { |
| 109 | + if (!record.isValidChecksum()) { |
| 110 | + String message = String.format( |
| 111 | + "Checksum failed: segment=%s position=%s checksum=%d timestamp=%d ttl=%d keySize=%d valueSize=%d", |
| 112 | + segment.name(), |
| 113 | + position, |
| 114 | + record.header().checksum(), |
| 115 | + record.header().timestamp(), |
| 116 | + record.header().ttl(), |
| 117 | + record.header().keySize(), |
| 118 | + record.header().valueSize()); |
| 119 | + System.out.println(message); |
| 120 | + } |
| 121 | + position += record.size(); |
| 122 | + } |
| 123 | + } |
| 124 | +} |
0 commit comments