/** * @param args Command line arguments */ public static void main(String[] args) throws Exception { try (final TxnLogToolkit lt = parseCommandLine(args)) { if (lt.isDumpMode()) { lt.dump(new Scanner(System.in)); lt.printStat(); } else { lt.chop(); } } catch (TxnLogToolkitParseException e) { System.err.println(e.getMessage() + "\n"); printHelpAndExit(e.getExitCode(), e.getOptions()); } catch (TxnLogToolkitException e) { System.err.println(e.getMessage()); System.exit(e.getExitCode()); } }
@Override public void close() throws IOException { if (recoveryMode) { closeRecoveryFile(); } closeTxnLogFile(); } }
public TxnLogToolkit(boolean recoveryMode, boolean verbose, String txnLogFileName, boolean force) throws FileNotFoundException, TxnLogToolkitException { this.recoveryMode = recoveryMode; this.verbose = verbose; this.force = force; txnLogFile = loadTxnFile(txnLogFileName); if (recoveryMode) { recoveryLogFile = new File(txnLogFile.toString() + ".fixed"); if (recoveryLogFile.exists()) { throw new TxnLogToolkitException(ExitCode.UNEXPECTED_ERROR.getValue(), "Recovery file %s already exists or not writable", recoveryLogFile); } } openTxnLogFile(); if (recoveryMode) { openRecoveryFile(); } }
@Test public void testDumpMode() throws Exception { // Arrange File logfile = new File(new File(mySnapDir, "version-2"), "log.274"); TxnLogToolkit lt = new TxnLogToolkit(false, false, logfile.toString(), true); // Act lt.dump(null); // Assert // no exception thrown }
/** * @param args Command line arguments */ public static void main(String[] args) throws Exception { final TxnLogToolkit lt = parseCommandLine(args); try { lt.dump(new Scanner(System.in)); lt.printStat(); } catch (TxnLogToolkitParseException e) { System.err.println(e.getMessage() + "\n"); printHelpAndExit(e.getExitCode()); } catch (TxnLogToolkitException e) { System.err.println(e.getMessage()); System.exit(e.getExitCode()); } finally { lt.close(); } }
CommandLine cli = parser.parse(options, args); if (cli.hasOption("help")) { printHelpAndExit(0, options); printHelpAndExit(1, options); return new TxnLogToolkit(cli.getArgs()[0], cli.getOptionValue("zxid")); return new TxnLogToolkit(cli.hasOption("recover"), cli.hasOption("verbose"), cli.getArgs()[0], cli.hasOption("yes")); } catch (ParseException e) { throw new TxnLogToolkitParseException(options, ExitCode.UNEXPECTED_ERROR.getValue(), e.getMessage());
@Test(expected = TxnLogToolkit.TxnLogToolkitException.class) public void testInitMissingFile() throws FileNotFoundException, TxnLogToolkit.TxnLogToolkitException { // Arrange & Act File logfile = new File("this_file_should_not_exists"); TxnLogToolkit lt = new TxnLogToolkit(false, false, logfile.toString(), true); }
public TxnLogToolkit(boolean recoveryMode, boolean verbose, String txnLogFileName, boolean force) throws FileNotFoundException, TxnLogToolkitException { this.recoveryMode = recoveryMode; this.verbose = verbose; this.force = force; txnLogFile = new File(txnLogFileName); if (!txnLogFile.exists() || !txnLogFile.canRead()) { throw new TxnLogToolkitException(1, "File doesn't exist or not readable: %s", txnLogFile); } if (recoveryMode) { recoveryLogFile = new File(txnLogFile.toString() + ".fixed"); if (recoveryLogFile.exists()) { throw new TxnLogToolkitException(1, "Recovery file %s already exists or not writable", recoveryLogFile); } } openTxnLogFile(); if (recoveryMode) { openRecoveryFile(); } }
private void printTxn(byte[] bytes) throws IOException { printTxn(bytes, ""); }
@Test public void testDumpWithCrcError() throws Exception { // Arrange File logfile = new File(new File(mySnapDir, "version-2"), "log.42"); TxnLogToolkit lt = new TxnLogToolkit(false, false, logfile.toString(), true); // Act lt.dump(null); // Assert String output = outContent.toString(); Pattern p = Pattern.compile("^CRC ERROR.*session 0x8061fac5ddeb0000 cxid 0x0 zxid 0x8800000002 createSession 30000$", Pattern.MULTILINE); Matcher m = p.matcher(output); assertTrue("Output doesn't indicate CRC error for the broken session id: " + output, m.find()); }
@Test(expected = TxnLogToolkit.TxnLogToolkitException.class) public void testInitWithRecoveryFileExists() throws IOException, TxnLogToolkit.TxnLogToolkitException { // Arrange & Act File logfile = new File(new File(mySnapDir, "version-2"), "log.274"); File recoveryFile = new File(new File(mySnapDir, "version-2"), "log.274.fixed"); recoveryFile.createNewFile(); TxnLogToolkit lt = new TxnLogToolkit(true, false, logfile.toString(), true); }
private void printTxn(byte[] bytes) throws IOException { printTxn(bytes, ""); }
@Test public void testRecoveryInteractiveMode() throws Exception { // Arrange File logfile = new File(new File(mySnapDir, "version-2"), "log.42"); TxnLogToolkit lt = new TxnLogToolkit(true, false, logfile.toString(), false); // Act lt.dump(new Scanner("y\n")); // Assert String output = outContent.toString(); assertThat(output, containsString("CRC ERROR")); // Should be able to dump the recovered logfile with no CRC error outContent.reset(); logfile = new File(new File(mySnapDir, "version-2"), "log.42.fixed"); lt = new TxnLogToolkit(false, false, logfile.toString(), true); lt.dump(null); output = outContent.toString(); assertThat(output, not(containsString("CRC ERROR"))); } }
@Override public void close() throws IOException { if (recoveryMode) { closeRecoveryFile(); } closeTxnLogFile(); } }
private static TxnLogToolkit parseCommandLine(String[] args) throws TxnLogToolkitException, FileNotFoundException { TxnLogToolkitCliParser parser = new TxnLogToolkitCliParser(); parser.parse(args); return new TxnLogToolkit(parser.isRecoveryMode(), parser.isVerbose(), parser.getTxnLogFileName(), parser.isForce()); }
@Test public void testRecoveryFixBrokenFile() throws Exception { // Arrange File logfile = new File(new File(mySnapDir, "version-2"), "log.42"); TxnLogToolkit lt = new TxnLogToolkit(true, false, logfile.toString(), true); // Act lt.dump(null); // Assert String output = outContent.toString(); assertThat(output, containsString("CRC FIXED")); // Should be able to dump the recovered logfile with no CRC error outContent.reset(); logfile = new File(new File(mySnapDir, "version-2"), "log.42.fixed"); lt = new TxnLogToolkit(false, false, logfile.toString(), true); lt.dump(null); output = outContent.toString(); assertThat(output, not(containsString("CRC ERROR"))); }