public AbortTxnRequest deepCopy() { return new AbortTxnRequest(this); }
@Override public void rollbackTxn(long txnid) throws NoSuchTxnException, TException { client.abort_txn(new AbortTxnRequest(txnid)); }
@Override public void replRollbackTxn(long srcTxnId, String replPolicy) throws NoSuchTxnException, TException { AbortTxnRequest rqst = new AbortTxnRequest(srcTxnId); rqst.setReplPolicy(replPolicy); client.abort_txn(rqst); }
@Override public void rollbackTxn(long txnid) throws NoSuchTxnException, TException { client.abort_txn(new AbortTxnRequest(txnid)); }
@Test public void testAbortInvalidTxn() throws Exception { boolean caught = false; try { txnHandler.abortTxn(new AbortTxnRequest(195L)); } catch (NoSuchTxnException e) { caught = true; } assertTrue(caught); }
private void replAbortTxnForTest(List<Long> txnList, String replPolicy) throws Exception { for (Long txnId : txnList) { AbortTxnRequest rqst = new AbortTxnRequest(txnId); rqst.setReplPolicy(replPolicy); txnHandler.abortTxn(rqst); } checkReplTxnForTest(txnList.get(0), txnList.get(txnList.size() - 1), replPolicy, new ArrayList<>()); }
@Override public void replRollbackTxn(long srcTxnId, String replPolicy) throws NoSuchTxnException, TException { AbortTxnRequest rqst = new AbortTxnRequest(srcTxnId); rqst.setReplPolicy(replPolicy); client.abort_txn(rqst); }
@Test public void heartbeatTxnRangeOneAborted() throws Exception { long txnid = openTxn(); assertEquals(1, txnid); txnid = openTxn(); txnid = openTxn(); txnHandler.abortTxn(new AbortTxnRequest(3)); HeartbeatTxnRangeResponse rsp = txnHandler.heartbeatTxnRange(new HeartbeatTxnRangeRequest(1, 3)); assertEquals(1, rsp.getAbortedSize()); Long txn = rsp.getAborted().iterator().next(); assertEquals(3L, (long)txn); assertEquals(0, rsp.getNosuch().size()); }
@Test public void testValidTxnsSomeOpen() throws Exception { txnHandler.openTxns(new OpenTxnRequest(3, "me", "localhost")); txnHandler.abortTxn(new AbortTxnRequest(1)); txnHandler.commitTxn(new CommitTxnRequest(2)); GetOpenTxnsInfoResponse txnsInfo = txnHandler.getOpenTxnsInfo(); assertEquals(3L, txnsInfo.getTxn_high_water_mark()); assertEquals(2, txnsInfo.getOpen_txns().size()); assertEquals(1L, txnsInfo.getOpen_txns().get(0).getId()); assertEquals(TxnState.ABORTED, txnsInfo.getOpen_txns().get(0).getState()); assertEquals(3L, txnsInfo.getOpen_txns().get(1).getId()); assertEquals(TxnState.OPEN, txnsInfo.getOpen_txns().get(1).getState()); GetOpenTxnsResponse txns = txnHandler.getOpenTxns(); assertEquals(3L, txns.getTxn_high_water_mark()); assertEquals(2, txns.getOpen_txns().size()); boolean[] saw = new boolean[4]; for (int i = 0; i < saw.length; i++) saw[i] = false; for (Long tid : txns.getOpen_txns()) { saw[tid.intValue()] = true; } assertTrue(saw[1]); assertFalse(saw[2]); assertTrue(saw[3]); }
@Test public void testHeartbeatTxnAborted() throws Exception { // Test that when a transaction is aborted, the heartbeat fails openTxn(); txnHandler.abortTxn(new AbortTxnRequest(1)); HeartbeatRequest h = new HeartbeatRequest(); h.setTxnid(1); try { txnHandler.heartbeat(h); fail("Told there was a txn, when it should have been aborted."); } catch (TxnAbortedException e) { } }
@Test public void testUnlockOnAbort() throws Exception { // Test that committing unlocks long txnid = openTxn(); LockComponent comp = new LockComponent(LockType.SHARED_WRITE, LockLevel.DB, "mydb"); comp.setOperationType(DataOperationType.UPDATE); List<LockComponent> components = new ArrayList<LockComponent>(1); components.add(comp); LockRequest req = new LockRequest(components, "me", "localhost"); req.setTxnid(txnid); LockResponse res = txnHandler.lock(req); assertTrue(res.getState() == LockState.ACQUIRED); txnHandler.abortTxn(new AbortTxnRequest(txnid)); assertEquals(0, txnHandler.numLocksInLockTable()); }
protected void burnThroughTransactions(String dbName, String tblName, int num, Set<Long> open, Set<Long> aborted) throws MetaException, NoSuchTxnException, TxnAbortedException { OpenTxnsResponse rsp = txnHandler.openTxns(new OpenTxnRequest(num, "me", "localhost")); AllocateTableWriteIdsRequest awiRqst = new AllocateTableWriteIdsRequest(dbName, tblName); awiRqst.setTxnIds(rsp.getTxn_ids()); AllocateTableWriteIdsResponse awiResp = txnHandler.allocateTableWriteIds(awiRqst); int i = 0; for (long tid : rsp.getTxn_ids()) { assert(awiResp.getTxnToWriteIds().get(i++).getTxnId() == tid); if (aborted != null && aborted.contains(tid)) { txnHandler.abortTxn(new AbortTxnRequest(tid)); } else if (open == null || (open != null && !open.contains(tid))) { txnHandler.commitTxn(new CommitTxnRequest(tid)); } } }
@Test public void noCompactWhenNoCompactSetLowerCase() throws Exception { Map<String, String> parameters = new HashMap<String, String>(1); parameters.put("no_auto_compaction", "true"); Table t = newTable("default", "ncwncs", false, parameters); HiveConf.setIntVar(conf, HiveConf.ConfVars.HIVE_COMPACTOR_ABORTEDTXN_THRESHOLD, 10); for (int i = 0; i < 11; i++) { long txnid = openTxn(); LockComponent comp = new LockComponent(LockType.SHARED_WRITE, LockLevel.TABLE, "default"); comp.setOperationType(DataOperationType.DELETE); comp.setTablename("ncwncs"); List<LockComponent> components = new ArrayList<LockComponent>(1); components.add(comp); LockRequest req = new LockRequest(components, "me", "localhost"); req.setTxnid(txnid); LockResponse res = txnHandler.lock(req); txnHandler.abortTxn(new AbortTxnRequest(txnid)); } startInitiator(); ShowCompactResponse rsp = txnHandler.showCompact(new ShowCompactRequest()); Assert.assertEquals(0, rsp.getCompactsSize()); }
@Test public void noCompactWhenNoCompactSet() throws Exception { Map<String, String> parameters = new HashMap<String, String>(1); parameters.put("NO_AUTO_COMPACTION", "true"); Table t = newTable("default", "ncwncs", false, parameters); HiveConf.setIntVar(conf, HiveConf.ConfVars.HIVE_COMPACTOR_ABORTEDTXN_THRESHOLD, 10); for (int i = 0; i < 11; i++) { long txnid = openTxn(); LockComponent comp = new LockComponent(LockType.SHARED_WRITE, LockLevel.TABLE, "default"); comp.setTablename("ncwncs"); comp.setOperationType(DataOperationType.UPDATE); List<LockComponent> components = new ArrayList<LockComponent>(1); components.add(comp); LockRequest req = new LockRequest(components, "me", "localhost"); req.setTxnid(txnid); LockResponse res = txnHandler.lock(req); txnHandler.abortTxn(new AbortTxnRequest(txnid)); } startInitiator(); ShowCompactResponse rsp = txnHandler.showCompact(new ShowCompactRequest()); Assert.assertEquals(0, rsp.getCompactsSize()); }
@Test public void testCheckLockTxnAborted() throws Exception { // Test that when a transaction is aborted, the heartbeat fails long txnid = openTxn(); LockComponent comp = new LockComponent(LockType.SHARED_WRITE, LockLevel.DB, "mydb"); comp.setTablename("mytable"); comp.setPartitionname("mypartition"); comp.setOperationType(DataOperationType.DELETE); List<LockComponent> components = new ArrayList<LockComponent>(1); components.add(comp); LockRequest req = new LockRequest(components, "me", "localhost"); req.setTxnid(txnid); LockResponse res = txnHandler.lock(req); long lockid = res.getLockid(); txnHandler.abortTxn(new AbortTxnRequest(txnid)); try { // This will throw NoSuchLockException (even though it's the // transaction we've closed) because that will have deleted the lock. txnHandler.checkLock(new CheckLockRequest(lockid)); fail("Allowed to check lock on aborted transaction."); } catch (NoSuchLockException e) { } }
@Test public void majorCompactOnTableTooManyAborts() throws Exception { Table t = newTable("default", "mcottma", false); HiveConf.setIntVar(conf, HiveConf.ConfVars.HIVE_COMPACTOR_ABORTEDTXN_THRESHOLD, 10); for (int i = 0; i < 11; i++) { long txnid = openTxn(); LockComponent comp = new LockComponent(LockType.SHARED_WRITE, LockLevel.TABLE, "default"); comp.setTablename("mcottma"); comp.setOperationType(DataOperationType.UPDATE); List<LockComponent> components = new ArrayList<LockComponent>(1); components.add(comp); LockRequest req = new LockRequest(components, "me", "localhost"); req.setTxnid(txnid); LockResponse res = txnHandler.lock(req); txnHandler.abortTxn(new AbortTxnRequest(txnid)); } startInitiator(); ShowCompactResponse rsp = txnHandler.showCompact(new ShowCompactRequest()); List<ShowCompactResponseElement> compacts = rsp.getCompacts(); Assert.assertEquals(1, compacts.size()); Assert.assertEquals("initiated", compacts.get(0).getState()); Assert.assertEquals("mcottma", compacts.get(0).getTablename()); Assert.assertEquals(CompactionType.MAJOR, compacts.get(0).getType()); }
@Test public void testCheckLockAcquireAfterWaiting() throws Exception { LockComponent comp = new LockComponent(LockType.SHARED_WRITE, LockLevel.DB, "mydb"); comp.setTablename("mytable"); comp.setPartitionname("mypartition"); comp.setOperationType(DataOperationType.DELETE); List<LockComponent> components = new ArrayList<LockComponent>(1); components.add(comp); LockRequest req = new LockRequest(components, "me", "localhost"); long txnId = openTxn(); req.setTxnid(txnId); LockResponse res = txnHandler.lock(req); long lockid1 = res.getLockid(); assertTrue(res.getState() == LockState.ACQUIRED); comp = new LockComponent(LockType.SHARED_WRITE, LockLevel.DB, "mydb"); comp.setTablename("mytable"); comp.setPartitionname("mypartition"); comp.setOperationType(DataOperationType.UPDATE); components.clear(); components.add(comp); req = new LockRequest(components, "me", "localhost"); req.setTxnid(openTxn()); res = txnHandler.lock(req); long lockid2 = res.getLockid(); assertTrue(res.getState() == LockState.WAITING); txnHandler.abortTxn(new AbortTxnRequest(txnId)); res = txnHandler.checkLock(new CheckLockRequest(lockid2)); assertTrue(res.getState() == LockState.ACQUIRED); }
@Test public void majorCompactOnPartitionTooManyAborts() throws Exception { Table t = newTable("default", "mcoptma", true); Partition p = newPartition(t, "today"); HiveConf.setIntVar(conf, HiveConf.ConfVars.HIVE_COMPACTOR_ABORTEDTXN_THRESHOLD, 10); for (int i = 0; i < 11; i++) { long txnid = openTxn(); LockComponent comp = new LockComponent(LockType.SHARED_WRITE, LockLevel.TABLE, "default"); comp.setTablename("mcoptma"); comp.setPartitionname("ds=today"); comp.setOperationType(DataOperationType.DELETE); List<LockComponent> components = new ArrayList<LockComponent>(1); components.add(comp); LockRequest req = new LockRequest(components, "me", "localhost"); req.setTxnid(txnid); LockResponse res = txnHandler.lock(req); txnHandler.abortTxn(new AbortTxnRequest(txnid)); } startInitiator(); ShowCompactResponse rsp = txnHandler.showCompact(new ShowCompactRequest()); List<ShowCompactResponseElement> compacts = rsp.getCompacts(); Assert.assertEquals(1, compacts.size()); Assert.assertEquals("initiated", compacts.get(0).getState()); Assert.assertEquals("mcoptma", compacts.get(0).getTablename()); Assert.assertEquals("ds=today", compacts.get(0).getPartitionname()); Assert.assertEquals(CompactionType.MAJOR, compacts.get(0).getType()); }
@Test public void noCompactOnManyDifferentPartitionAborts() throws Exception { Table t = newTable("default", "ncomdpa", true); for (int i = 0; i < 11; i++) { Partition p = newPartition(t, "day-" + i); } HiveConf.setIntVar(conf, HiveConf.ConfVars.HIVE_COMPACTOR_ABORTEDTXN_THRESHOLD, 10); for (int i = 0; i < 11; i++) { long txnid = openTxn(); LockComponent comp = new LockComponent(LockType.SHARED_WRITE, LockLevel.TABLE, "default"); comp.setTablename("ncomdpa"); comp.setPartitionname("ds=day-" + i); comp.setOperationType(DataOperationType.UPDATE); List<LockComponent> components = new ArrayList<LockComponent>(1); components.add(comp); LockRequest req = new LockRequest(components, "me", "localhost"); req.setTxnid(txnid); LockResponse res = txnHandler.lock(req); txnHandler.abortTxn(new AbortTxnRequest(txnid)); } startInitiator(); ShowCompactResponse rsp = txnHandler.showCompact(new ShowCompactRequest()); Assert.assertEquals(0, rsp.getCompactsSize()); }
@Test public void cleanEmptyAbortedTxns() throws Exception { // Test that we are cleaning aborted transactions with no components left in txn_components. // Put one aborted transaction with an entry in txn_components to make sure we don't // accidently clean it too. Table t = newTable("default", "ceat", false); long txnid = openTxn(); LockComponent comp = new LockComponent(LockType.SHARED_WRITE, LockLevel.TABLE, "default"); comp.setTablename("ceat"); comp.setOperationType(DataOperationType.UPDATE); List<LockComponent> components = new ArrayList<LockComponent>(1); components.add(comp); LockRequest req = new LockRequest(components, "me", "localhost"); req.setTxnid(txnid); LockResponse res = txnHandler.lock(req); txnHandler.abortTxn(new AbortTxnRequest(txnid)); conf.setIntVar(HiveConf.ConfVars.HIVE_TXN_MAX_OPEN_BATCH, TxnStore.TIMED_OUT_TXN_ABORT_BATCH_SIZE + 50); OpenTxnsResponse resp = txnHandler.openTxns(new OpenTxnRequest( TxnStore.TIMED_OUT_TXN_ABORT_BATCH_SIZE + 50, "user", "hostname")); txnHandler.abortTxns(new AbortTxnsRequest(resp.getTxn_ids())); GetOpenTxnsResponse openTxns = txnHandler.getOpenTxns(); Assert.assertEquals(TxnStore.TIMED_OUT_TXN_ABORT_BATCH_SIZE + 50 + 1, openTxns.getOpen_txnsSize()); startInitiator(); openTxns = txnHandler.getOpenTxns(); Assert.assertEquals(1, openTxns.getOpen_txnsSize()); }