TransactionAdmin { @VisibleForTesting int doMain(String[] args, Configuration conf) { if (args.length < 1) { printUsage(); return 1; } Injector injector = Guice.createInjector( new ConfigModule(conf), new ZKModule(), new DiscoveryModules().getDistributedModules(), new TransactionModules().getDistributedModules(), new TransactionClientModule() ); ZKClientService zkClient = injector.getInstance(ZKClientService.class); zkClient.startAndWait(); try { TransactionSystemClient txClient = injector.getInstance(TransactionSystemClient.class); String option = args[0]; if (option.equals(OPT_TRUNCATE_INVALID_TX)) { if (args.length != 2) { printUsage(); return 1; } Set<Long> txIds; try { txIds = parseTxIds(args[1]); } catch (NumberFormatException e) { err.println("NumberFormatException: " + e.getMessage()); return 1; } if (!txIds.isEmpty()) { out.println("Invalid list size before truncation: " + txClient.getInvalidSize()); txClient.truncateInvalidTx(txIds); out.println("Invalid list size after truncation: " + txClient.getInvalidSize()); } } else if (option.equals(OPT_TRUNCATE_INVALID_TX_BEFORE)) { if (args.length != 2) { printUsage(); return 1; } try { long time = Long.parseLong(args[1]); out.println("Invalid list size before truncation: " + txClient.getInvalidSize()); txClient.truncateInvalidTxBefore(time); out.println("Invalid list size after truncation: " + txClient.getInvalidSize()); } catch (InvalidTruncateTimeException e) { err.println(e.getMessage()); return 1; } catch (NumberFormatException e) { err.println("NumberFormatException: " + e.getMessage()); return 1; } } else if (option.equals(OPT_GET_INVALID_TX_SIZE)) { if (args.length != 1) { printUsage(); return 1; } out.println("Invalid list size: " + txClient.getInvalidSize()); } else { printUsage(); return 1; } } finally { zkClient.stopAndWait(); } return 0; } TransactionAdmin(PrintStream out, PrintStream err); static void main(String[] args);  }
@Test public void testTruncateInvalidTx() throws Exception { Transaction tx1 = txClient.startLong(); Transaction tx2 = txClient.startShort(); txClient.invalidate(tx1.getTransactionId()); txClient.invalidate(tx2.getTransactionId()); Assert.assertEquals(2, txClient.getInvalidSize()); TransactionAdmin txAdmin = new TransactionAdmin(new PrintStream(System.out), new PrintStream(System.err)); int status = txAdmin.doMain(new String[]{"--truncate-invalid-tx", String.valueOf(tx2.getTransactionId())}, conf); Assert.assertEquals(0, status); Assert.assertEquals(1, txClient.getInvalidSize()); }
@Test public void testTruncateInvalidTxBefore() throws Exception { Transaction tx1 = txClient.startLong(); TimeUnit.MILLISECONDS.sleep(1); long beforeTx2 = System.currentTimeMillis(); Transaction tx2 = txClient.startLong(); Assert.assertEquals(0, txClient.getInvalidSize()); TransactionAdmin txAdmin = new TransactionAdmin(new PrintStream(System.out), new PrintStream(System.err)); int status = txAdmin.doMain(new String[]{"--truncate-invalid-tx-before", String.valueOf(beforeTx2)}, conf); Assert.assertEquals(1, status); Assert.assertEquals(0, txClient.getInvalidSize()); txClient.invalidate(tx1.getTransactionId()); txClient.invalidate(tx2.getTransactionId()); Assert.assertEquals(2, txClient.getInvalidSize()); status = txAdmin.doMain(new String[]{"--truncate-invalid-tx-before", String.valueOf(beforeTx2)}, conf); Assert.assertEquals(0, status); Assert.assertEquals(1, txClient.getInvalidSize()); }
@Test public void testGetInvalidTxSize() throws Exception { Transaction tx1 = txClient.startShort(); txClient.startLong(); txClient.invalidate(tx1.getTransactionId()); ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteArrayOutputStream err = new ByteArrayOutputStream(); TransactionAdmin txAdmin = new TransactionAdmin(new PrintStream(out), new PrintStream(err)); int status = txAdmin.doMain(new String[]{"--get-invalid-tx-size"}, conf); Assert.assertEquals(0, status); Assert.assertTrue(out.toString("UTF-8").contains("Invalid list size: 1\n")); }