|
2 | 2 |
|
3 | 3 |
|
4 | 4 | import com.csforge.sstable.CassandraUtils;
|
5 |
| -import com.csforge.sstable.TableTransformer; |
6 | 5 | import com.google.common.base.Strings;
|
7 | 6 | import org.apache.cassandra.config.Config;
|
8 | 7 | import org.apache.cassandra.db.Mutation;
|
9 | 8 | import org.apache.cassandra.db.partitions.PartitionUpdate;
|
10 | 9 | import org.apache.cassandra.db.rows.Row;
|
11 | 10 | import org.apache.cassandra.io.util.DataInputPlus;
|
12 | 11 | import org.apache.cassandra.net.MessagingService;
|
| 12 | +import org.apache.commons.cli.*; |
13 | 13 |
|
14 |
| -import java.io.ByteArrayInputStream; |
15 |
| -import java.io.File; |
| 14 | +import java.io.*; |
16 | 15 | import java.nio.ByteBuffer;
|
17 | 16 | import java.util.Iterator;
|
18 | 17 |
|
| 18 | +import static com.csforge.sstable.TableTransformer.*; |
| 19 | + |
19 | 20 | public class HintsTool {
|
| 21 | + private static final String HOST_OPTION = "h"; |
| 22 | + private static final String PORT_OPTION = "p"; |
| 23 | + private static final String SILENT_OPTION = "s"; |
| 24 | + private static final Options options = new Options(); |
20 | 25 |
|
21 | 26 | static {
|
22 | 27 | Config.setClientMode(true);
|
| 28 | + Option hostOption = new Option(HOST_OPTION, true, "Host to extract schema frome."); |
| 29 | + hostOption.setRequired(false); |
| 30 | + options.addOption(hostOption); |
| 31 | + Option portOption = new Option(PORT_OPTION, true, "CQL native port."); |
| 32 | + portOption.setRequired(false); |
| 33 | + options.addOption(portOption); |
| 34 | + Option silentOption = new Option(SILENT_OPTION, false, "Only output mutations."); |
| 35 | + silentOption.setRequired(false); |
| 36 | + options.addOption(silentOption); |
23 | 37 | }
|
24 | 38 |
|
25 |
| - public static void main(String... args) { |
26 |
| - String host = "127.0.0.1"; |
27 |
| - int port = 9042; |
28 |
| - File hint_file = null; |
| 39 | + private static void dumpHints(File hint_file, PrintStream out, int version) throws IOException { |
| 40 | + try (HintsReader reader = HintsReader.open(hint_file)) { |
| 41 | + for (HintsReader.Page page : reader) { |
| 42 | + Iterator<ByteBuffer> hints = page.buffersIterator(); //hints iterator im pretty sure is busted |
| 43 | + while (hints.hasNext()) { |
| 44 | + ByteBuffer buf = hints.next(); |
| 45 | + DataInputPlus in = new DataInputPlus.DataInputStreamPlus(new ByteArrayInputStream(buf.array())); |
| 46 | + Hint hint = Hint.serializer.deserialize(in, version); |
| 47 | + Mutation mutation = hint.mutation; |
| 48 | + for (PartitionUpdate partition : mutation.getPartitionUpdates()) { |
| 49 | + out.println(partition); |
| 50 | + for (Row row : partition) { |
| 51 | + out.println(row); |
| 52 | + } |
| 53 | + } |
| 54 | + } |
| 55 | + } |
| 56 | + } |
| 57 | + } |
29 | 58 |
|
30 |
| - if (args.length == 1) { |
31 |
| - hint_file = new File(args[0]); |
32 |
| - } else if (args.length == 2) { |
33 |
| - host = args[0]; |
34 |
| - hint_file = new File(args[1]); |
35 |
| - } else if (args.length == 3) { |
36 |
| - host = args[0]; |
37 |
| - port = Integer.parseInt(args[1]); |
38 |
| - hint_file = new File(args[2]); |
39 |
| - } else { |
40 |
| - System.err.println("Usage: hints <host> <port> <hint_file>\n hints <host> <hint_file>\n" + |
41 |
| - " hints <hint_file>"); |
42 |
| - System.exit(-1); |
| 59 | + private static void printHelp() { |
| 60 | + try (PrintWriter errWriter = new PrintWriter(System.err, true)) { |
| 61 | + HelpFormatter formatter = new HelpFormatter(); |
| 62 | + formatter.printHelp(errWriter, 120, "hints hintfile [hintfile ...]", |
| 63 | + String.format("%nHint Dump for Apache Cassandra 3.x%nOptions:"), |
| 64 | + options, 2, 1, "", true); |
| 65 | + errWriter.println(" hintfile at least one file containing hints"); |
| 66 | + errWriter.println(); |
43 | 67 | }
|
| 68 | + } |
44 | 69 |
|
| 70 | + public static void main(String... args) { |
| 71 | + CommandLineParser parser = new PosixParser(); |
| 72 | + CommandLine cmd = null; |
45 | 73 | try {
|
46 |
| - System.out.printf("%sLoading schema from %s:%s", TableTransformer.ANSI_RED, host, port); |
47 |
| - System.out.flush(); |
48 |
| - CassandraUtils.loadTablesFromRemote(host, port); |
49 |
| - System.out.println("\r\u001B[1;34m" + hint_file.getAbsolutePath()); |
50 |
| - System.out.println(TableTransformer.ANSI_CYAN + Strings.repeat("=", hint_file.getAbsolutePath().length())); |
51 |
| - System.out.print(TableTransformer.ANSI_RESET); |
52 |
| - int version = Integer.parseInt(System.getProperty("hints.version", "" + MessagingService.current_version)); |
| 74 | + cmd = parser.parse(options, args); |
| 75 | + } catch (ParseException e) { |
| 76 | + System.err.format("%sFailure parsing arguments: %s%s%n%n", ANSI_RED, e.getMessage(), ANSI_RESET); |
| 77 | + printHelp(); |
| 78 | + System.exit(-1); |
53 | 79 |
|
54 |
| - try (HintsReader reader = HintsReader.open(hint_file)) { |
55 |
| - for (HintsReader.Page page : reader) { |
56 |
| - Iterator<ByteBuffer> hints = page.buffersIterator(); //hints iterator im pretty sure is busted |
57 |
| - while (hints.hasNext()) { |
58 |
| - ByteBuffer buf = hints.next(); |
59 |
| - DataInputPlus in = new DataInputPlus.DataInputStreamPlus(new ByteArrayInputStream(buf.array())); |
60 |
| - Hint hint = Hint.serializer.deserialize(in, version); |
61 |
| - Mutation mutation = hint.mutation; |
62 |
| - for (PartitionUpdate partition : mutation.getPartitionUpdates()) { |
63 |
| - System.out.println(partition); |
64 |
| - for (Row row : partition) { |
65 |
| - System.out.println(row); |
66 |
| - } |
67 |
| - } |
| 80 | + } |
| 81 | + if(cmd.getArgs().length == 0) { |
| 82 | + printHelp(); |
| 83 | + System.exit(-2); |
| 84 | + } |
| 85 | + String host = cmd.getOptionValue(HOST_OPTION, "127.0.0.1"); |
| 86 | + int port = Integer.parseInt(cmd.getOptionValue(PORT_OPTION, "9042")); |
| 87 | + try { |
| 88 | + int version = MessagingService.current_version; |
| 89 | + try { |
| 90 | + if (!cmd.hasOption(SILENT_OPTION)) { |
| 91 | + System.out.printf("%sLoading schema from %s:%s%s", ANSI_RED, host, port, ANSI_RESET); |
| 92 | + System.out.flush(); |
| 93 | + } |
| 94 | + CassandraUtils.loadTablesFromRemote(host, port); |
| 95 | + version = Integer.parseInt(System.getProperty("hints.version", "" + MessagingService.current_version)); |
| 96 | + } finally { |
| 97 | + System.out.print(ANSI_RESET); |
| 98 | + } |
| 99 | + for (String hint : cmd.getArgs()) { |
| 100 | + File hint_file = new File(hint); |
| 101 | + if (!hint_file.exists()) { |
| 102 | + if(!cmd.hasOption(SILENT_OPTION)) { |
| 103 | + System.out.println("\r" + ANSI_RESET); |
| 104 | + } |
| 105 | + System.err.println("Non-existant hint file provided: " + hint); |
| 106 | + } else { |
| 107 | + if(!cmd.hasOption(SILENT_OPTION)) { |
| 108 | + System.out.println("\r\u001B[1;34m" + hint_file.getAbsolutePath()); |
| 109 | + System.out.println(ANSI_CYAN + Strings.repeat("=", hint_file.getAbsolutePath().length())); |
| 110 | + System.out.print(ANSI_RESET); |
68 | 111 | }
|
| 112 | + dumpHints(hint_file, System.out, version); |
69 | 113 | }
|
70 | 114 | }
|
71 | 115 | } catch (Exception e) {
|
|
0 commit comments