Search in sources :

Example 6 with TransactionContext

use of org.apache.tephra.TransactionContext in project cdap by caskdata.

the class TransactionContextTest method testCanCommitFalse.

@Test
public void testCanCommitFalse() throws TransactionFailureException, InterruptedException {
    txClient.failCanCommitOnce = true;
    TransactionContext context = newTransactionContext(ds1, ds2);
    // start transaction
    context.start();
    // add a change to ds1 and ds2
    ds1.addChange(A);
    ds2.addChange(B);
    // commit transaction should fail and cause rollback
    try {
        context.finish();
        Assert.fail("commit failed - exception should be thrown");
    } catch (TransactionConflictException e) {
        Assert.assertNull(e.getCause());
    }
    // verify both are rolled back and tx is aborted
    Assert.assertTrue(ds1.started);
    Assert.assertTrue(ds2.started);
    Assert.assertTrue(ds1.checked);
    Assert.assertTrue(ds2.checked);
    Assert.assertFalse(ds1.committed);
    Assert.assertFalse(ds2.committed);
    Assert.assertFalse(ds1.postCommitted);
    Assert.assertFalse(ds2.postCommitted);
    Assert.assertTrue(ds1.rolledBack);
    Assert.assertTrue(ds2.rolledBack);
    Assert.assertEquals(txClient.state, DummyTxClient.CommitState.Aborted);
}
Also used : TransactionContext(org.apache.tephra.TransactionContext) TransactionConflictException(org.apache.tephra.TransactionConflictException) Test(org.junit.Test)

Example 7 with TransactionContext

use of org.apache.tephra.TransactionContext in project cdap by caskdata.

the class TransactionContextTest method testAddThenRemoveSuccess.

@Test
public void testAddThenRemoveSuccess() throws TransactionFailureException {
    TransactionContext context = newTransactionContext();
    context.start();
    Assert.assertTrue(context.addTransactionAware(ds1));
    ds1.addChange(A);
    try {
        context.removeTransactionAware(ds1);
        Assert.fail("Removal of TransactionAware should fails when there is active transaction.");
    } catch (IllegalStateException e) {
    // Expected
    }
    context.finish();
    Assert.assertTrue(context.removeTransactionAware(ds1));
    // Removing a TransactionAware not added before should returns false
    Assert.assertFalse(context.removeTransactionAware(ds2));
    // Verify ds1 is committed and post-committed
    Assert.assertTrue(ds1.started);
    Assert.assertTrue(ds1.checked);
    Assert.assertTrue(ds1.committed);
    Assert.assertTrue(ds1.postCommitted);
    Assert.assertFalse(ds1.rolledBack);
    // Verify nothing happen to ds2
    Assert.assertFalse(ds2.started);
    Assert.assertFalse(ds2.checked);
    Assert.assertFalse(ds2.committed);
    Assert.assertFalse(ds2.postCommitted);
    Assert.assertFalse(ds2.rolledBack);
    Assert.assertEquals(txClient.state, DummyTxClient.CommitState.Committed);
}
Also used : TransactionContext(org.apache.tephra.TransactionContext) Test(org.junit.Test)

Example 8 with TransactionContext

use of org.apache.tephra.TransactionContext in project cdap by caskdata.

the class TransactionContextTest method testPersistAndRollbackFalse.

@Test
public void testPersistAndRollbackFalse() throws TransactionFailureException, InterruptedException {
    ds1.failCommitTxOnce = InduceFailure.ReturnFalse;
    ds1.failRollbackTxOnce = InduceFailure.ReturnFalse;
    TransactionContext context = newTransactionContext(ds1, ds2);
    // start transaction
    context.start();
    // add a change to ds1 and ds2
    ds1.addChange(A);
    ds2.addChange(B);
    // commit transaction should fail and cause rollback
    try {
        context.finish();
        Assert.fail("Persist should have failed - exception should be thrown");
    } catch (TransactionFailureException e) {
        // in this case, the ds simply returned false
        Assert.assertNull(e.getCause());
    }
    // verify both are rolled back and tx is invalidated
    Assert.assertTrue(ds1.started);
    Assert.assertTrue(ds2.started);
    Assert.assertTrue(ds1.checked);
    Assert.assertTrue(ds2.checked);
    Assert.assertTrue(ds1.committed);
    Assert.assertFalse(ds2.committed);
    Assert.assertFalse(ds1.postCommitted);
    Assert.assertFalse(ds2.postCommitted);
    Assert.assertTrue(ds1.rolledBack);
    Assert.assertTrue(ds2.rolledBack);
    Assert.assertEquals(txClient.state, DummyTxClient.CommitState.Invalidated);
}
Also used : TransactionFailureException(org.apache.tephra.TransactionFailureException) TransactionContext(org.apache.tephra.TransactionContext) Test(org.junit.Test)

Example 9 with TransactionContext

use of org.apache.tephra.TransactionContext in project cdap by caskdata.

the class TransactionContextTest method testPersistFalse.

@Test
public void testPersistFalse() throws TransactionFailureException, InterruptedException {
    ds1.failCommitTxOnce = InduceFailure.ReturnFalse;
    TransactionContext context = newTransactionContext(ds1, ds2);
    // start transaction
    context.start();
    // add a change to ds1 and ds2
    ds1.addChange(A);
    ds2.addChange(B);
    // commit transaction should fail and cause rollback
    try {
        context.finish();
        Assert.fail("Persist should have failed - exception should be thrown");
    } catch (TransactionFailureException e) {
        // in this case, the ds simply returned false
        Assert.assertNull(e.getCause());
    }
    // verify both are rolled back and tx is aborted
    Assert.assertTrue(ds1.started);
    Assert.assertTrue(ds2.started);
    Assert.assertTrue(ds1.checked);
    Assert.assertTrue(ds2.checked);
    Assert.assertTrue(ds1.committed);
    Assert.assertFalse(ds2.committed);
    Assert.assertFalse(ds1.postCommitted);
    Assert.assertFalse(ds2.postCommitted);
    Assert.assertTrue(ds1.rolledBack);
    Assert.assertTrue(ds2.rolledBack);
    Assert.assertEquals(txClient.state, DummyTxClient.CommitState.Aborted);
}
Also used : TransactionFailureException(org.apache.tephra.TransactionFailureException) TransactionContext(org.apache.tephra.TransactionContext) Test(org.junit.Test)

Example 10 with TransactionContext

use of org.apache.tephra.TransactionContext in project cdap by caskdata.

the class TransactionContextTest method testStartAndRollbackFailure.

@Test
public void testStartAndRollbackFailure() throws TransactionFailureException, InterruptedException {
    ds1.failStartTxOnce = InduceFailure.ThrowException;
    TransactionContext context = newTransactionContext(ds1, ds2);
    // start transaction
    try {
        context.start();
        Assert.fail("start failed - exception should be thrown");
    } catch (TransactionFailureException e) {
        Assert.assertEquals("start failure", e.getCause().getMessage());
    }
    // verify both are not rolled back and tx is aborted
    Assert.assertTrue(ds1.started);
    Assert.assertFalse(ds2.started);
    Assert.assertFalse(ds1.checked);
    Assert.assertFalse(ds2.checked);
    Assert.assertFalse(ds1.committed);
    Assert.assertFalse(ds2.committed);
    Assert.assertFalse(ds1.postCommitted);
    Assert.assertFalse(ds2.postCommitted);
    Assert.assertFalse(ds1.rolledBack);
    Assert.assertFalse(ds2.rolledBack);
    Assert.assertEquals(txClient.state, DummyTxClient.CommitState.Aborted);
}
Also used : TransactionFailureException(org.apache.tephra.TransactionFailureException) TransactionContext(org.apache.tephra.TransactionContext) Test(org.junit.Test)

Aggregations

TransactionContext (org.apache.tephra.TransactionContext)47 Test (org.junit.Test)34 TransactionFailureException (org.apache.tephra.TransactionFailureException)18 ConsumerConfig (co.cask.cdap.data2.queue.ConsumerConfig)14 QueueConsumer (co.cask.cdap.data2.queue.QueueConsumer)9 ConsumerGroupConfig (co.cask.cdap.data2.queue.ConsumerGroupConfig)8 PartitionedFileSet (co.cask.cdap.api.dataset.lib.PartitionedFileSet)7 QueueName (co.cask.cdap.common.queue.QueueName)7 QueueEntry (co.cask.cdap.data2.queue.QueueEntry)7 QueueProducer (co.cask.cdap.data2.queue.QueueProducer)7 StreamEvent (co.cask.cdap.api.flow.flowlet.StreamEvent)6 StreamId (co.cask.cdap.proto.id.StreamId)5 Location (org.apache.twill.filesystem.Location)4 PartitionKey (co.cask.cdap.api.dataset.lib.PartitionKey)3 PartitionOutput (co.cask.cdap.api.dataset.lib.PartitionOutput)3 Properties (java.util.Properties)3 Stopwatch (com.google.common.base.Stopwatch)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 CountDownLatch (java.util.concurrent.CountDownLatch)2