Search in sources :

Example 1 with TransactionSnapshot

use of org.apache.tephra.persist.TransactionSnapshot in project cdap by caskdata.

the class AbstractSnapshotCodec method decode.

@Override
public TransactionSnapshot decode(InputStream in) {
    BinaryDecoder decoder = new BinaryDecoder(in);
    try {
        long timestamp = decoder.readLong();
        long readPointer = decoder.readLong();
        long writePointer = decoder.readLong();
        // some attributes where removed during format change, luckily those stored at the end, so we just give a chance
        // to skip them
        decodeObsoleteAttributes(decoder);
        Collection<Long> invalid = decodeInvalid(decoder);
        NavigableMap<Long, TransactionManager.InProgressTx> inProgress = decodeInProgress(decoder);
        NavigableMap<Long, Set<ChangeId>> committing = decodeChangeSets(decoder);
        NavigableMap<Long, Set<ChangeId>> committed = decodeChangeSets(decoder);
        return new TransactionSnapshot(timestamp, readPointer, writePointer, invalid, inProgress, committing, committed);
    } catch (IOException e) {
        LOG.error("Unable to deserialize transaction state: ", e);
        throw Throwables.propagate(e);
    }
}
Also used : TransactionSnapshot(org.apache.tephra.persist.TransactionSnapshot) Set(java.util.Set) HashSet(java.util.HashSet) IOException(java.io.IOException) BinaryDecoder(org.apache.tephra.snapshot.BinaryDecoder)

Example 2 with TransactionSnapshot

use of org.apache.tephra.persist.TransactionSnapshot in project cdap by caskdata.

the class AbstractSnapshotCodec method decodeTransactionVisibilityState.

@Override
public TransactionVisibilityState decodeTransactionVisibilityState(InputStream in) {
    BinaryDecoder decoder = new BinaryDecoder(in);
    try {
        long timestamp = decoder.readLong();
        long readPointer = decoder.readLong();
        long writePointer = decoder.readLong();
        // some attributes where removed during format change, luckily those stored at the end, so we just give a chance
        // to skip them
        decodeObsoleteAttributes(decoder);
        Collection<Long> invalid = decodeInvalid(decoder);
        NavigableMap<Long, TransactionManager.InProgressTx> inProgress = decodeInProgress(decoder);
        return new TransactionSnapshot(timestamp, readPointer, writePointer, invalid, inProgress);
    } catch (IOException e) {
        LOG.error("Unable to deserialize transaction state: ", e);
        throw Throwables.propagate(e);
    }
}
Also used : TransactionSnapshot(org.apache.tephra.persist.TransactionSnapshot) IOException(java.io.IOException) BinaryDecoder(org.apache.tephra.snapshot.BinaryDecoder)

Example 3 with TransactionSnapshot

use of org.apache.tephra.persist.TransactionSnapshot in project cdap by caskdata.

the class SparkTransactionHandlerTest method testRunJob.

/**
 * Simulates a single job run which contains multiple stages with an optional explicit {@link Transaction} to use.
 *
 * @param jobId the job id
 * @param stages stages of the job
 * @param jobSucceeded end result of the job
 * @param explicitTransaction the job transaction to use if not {@code null}
 */
private void testRunJob(int jobId, Set<Integer> stages, boolean jobSucceeded, @Nullable final Transaction explicitTransaction) throws Exception {
    // Before job start, no transaction will be associated with the stages
    verifyStagesTransactions(stages, new ClientTransactionVerifier() {

        @Override
        public boolean verify(@Nullable Transaction transaction, @Nullable Throwable failureCause) throws Exception {
            return transaction == null && failureCause instanceof TimeoutException;
        }
    });
    // Now start the job
    if (explicitTransaction == null) {
        sparkTxHandler.jobStarted(jobId, stages);
    } else {
        sparkTxHandler.jobStarted(jobId, stages, new TransactionInfo() {

            @Override
            public Transaction getTransaction() {
                return explicitTransaction;
            }

            @Override
            public boolean commitOnJobEnded() {
                return false;
            }

            @Override
            public void onJobStarted() {
            // no-op
            }

            @Override
            public void onTransactionCompleted(boolean jobSucceeded, @Nullable TransactionFailureException failureCause) {
            // no-op
            }
        });
    }
    // For all stages, it should get the same transaction
    final Set<Transaction> transactions = Collections.newSetFromMap(new ConcurrentHashMap<Transaction, Boolean>());
    verifyStagesTransactions(stages, new ClientTransactionVerifier() {

        @Override
        public boolean verify(@Nullable Transaction transaction, @Nullable Throwable failureCause) throws Exception {
            transactions.add(new TransactionWrapper(transaction));
            return transaction != null;
        }
    });
    // Transactions returned for all stages belonging to the same job must return the same transaction
    Assert.assertEquals(1, transactions.size());
    // The transaction must be in progress
    Transaction transaction = transactions.iterator().next();
    Assert.assertTrue(txManager.getCurrentState().getInProgress().containsKey(transaction.getWritePointer()));
    // If run with an explicit transaction, then all stages' transactions must be the same as the explicit transaction
    if (explicitTransaction != null) {
        Assert.assertEquals(new TransactionWrapper(explicitTransaction), transaction);
    }
    // Now finish the job
    sparkTxHandler.jobEnded(jobId, jobSucceeded);
    // After job finished, no transaction will be associated with the stages
    verifyStagesTransactions(stages, new ClientTransactionVerifier() {

        @Override
        public boolean verify(@Nullable Transaction transaction, @Nullable Throwable failureCause) throws Exception {
            return transaction == null && failureCause instanceof TimeoutException;
        }
    });
    // Check the transaction state based on the job result
    TransactionSnapshot txState = txManager.getCurrentState();
    // If explicit transaction is used, the transaction should still be in-progress
    if (explicitTransaction != null) {
        Assert.assertTrue(txState.getInProgress().containsKey(transaction.getWritePointer()));
    } else {
        // With implicit transaction, after job completed, the tx shouldn't be in-progress
        Assert.assertFalse(txState.getInProgress().containsKey(transaction.getWritePointer()));
        if (jobSucceeded) {
            // Transaction must not be in the invalid list
            Assert.assertFalse(txState.getInvalid().contains(transaction.getWritePointer()));
        } else {
            // Transaction must be in the invalid list
            Assert.assertTrue(txState.getInvalid().contains(transaction.getWritePointer()));
        }
    }
}
Also used : TransactionFailureException(org.apache.tephra.TransactionFailureException) TimeoutException(java.util.concurrent.TimeoutException) UnknownHostException(java.net.UnknownHostException) TransactionFailureException(org.apache.tephra.TransactionFailureException) TransactionSnapshot(org.apache.tephra.persist.TransactionSnapshot) Transaction(org.apache.tephra.Transaction) TimeoutException(java.util.concurrent.TimeoutException)

Example 4 with TransactionSnapshot

use of org.apache.tephra.persist.TransactionSnapshot in project cdap by caskdata.

the class TransactionServiceClientTest method testGetSnapshot.

@Test
public void testGetSnapshot() throws Exception {
    TransactionSystemClient client = getClient();
    SnapshotCodecProvider codecProvider = new SnapshotCodecProvider(injector.getInstance(Configuration.class));
    Transaction tx1 = client.startShort();
    long currentTime = System.currentTimeMillis();
    TransactionSnapshot snapshot;
    try (InputStream in = client.getSnapshotInputStream()) {
        snapshot = codecProvider.decode(in);
    }
    Assert.assertTrue(snapshot.getTimestamp() >= currentTime);
    Assert.assertTrue(snapshot.getInProgress().containsKey(tx1.getWritePointer()));
    // Ensures that getSnapshot didn't persist a snapshot
    TransactionSnapshot snapshotAfter = getStateStorage().getLatestSnapshot();
    if (snapshotAfter != null) {
        Assert.assertTrue(snapshot.getTimestamp() > snapshotAfter.getTimestamp());
    }
}
Also used : TransactionSystemClient(org.apache.tephra.TransactionSystemClient) TransactionSnapshot(org.apache.tephra.persist.TransactionSnapshot) CConfiguration(co.cask.cdap.common.conf.CConfiguration) Configuration(org.apache.hadoop.conf.Configuration) Transaction(org.apache.tephra.Transaction) InputStream(java.io.InputStream) SnapshotCodecProvider(org.apache.tephra.snapshot.SnapshotCodecProvider) TransactionSystemTest(org.apache.tephra.TransactionSystemTest) Test(org.junit.Test)

Example 5 with TransactionSnapshot

use of org.apache.tephra.persist.TransactionSnapshot in project cdap by caskdata.

the class HiveExploreServiceStopTest method testServiceStop.

@Test
public void testServiceStop() throws Exception {
    ExploreService exploreService = injector.getInstance(ExploreService.class);
    Set<Long> beforeTxns = transactionManager.getCurrentState().getInProgress().keySet();
    exploreService.execute(NAMESPACE_ID, "show tables");
    Set<Long> queryTxns = Sets.difference(transactionManager.getCurrentState().getInProgress().keySet(), beforeTxns);
    Assert.assertFalse(queryTxns.isEmpty());
    // Stop all services so that explore service gets stopped.
    stopServices();
    // Make sure that the transaction got closed
    TransactionStateStorage transactionStateStorage = injector.getInstance(TransactionStateStorage.class);
    TransactionSnapshot latestSnapshot = transactionStateStorage.getLatestSnapshot();
    Assert.assertEquals(ImmutableSet.<Long>of(), Sets.intersection(queryTxns, latestSnapshot.getInProgress().keySet()).immutableCopy());
}
Also used : TransactionSnapshot(org.apache.tephra.persist.TransactionSnapshot) TransactionStateStorage(org.apache.tephra.persist.TransactionStateStorage) Test(org.junit.Test)

Aggregations

TransactionSnapshot (org.apache.tephra.persist.TransactionSnapshot)8 IOException (java.io.IOException)4 File (java.io.File)2 FileInputStream (java.io.FileInputStream)2 InputStream (java.io.InputStream)2 Transaction (org.apache.tephra.Transaction)2 BinaryDecoder (org.apache.tephra.snapshot.BinaryDecoder)2 SnapshotCodecProvider (org.apache.tephra.snapshot.SnapshotCodecProvider)2 Test (org.junit.Test)2 CConfiguration (co.cask.cdap.common.conf.CConfiguration)1 ConfigurationWriter (co.cask.cdap.data2.util.hbase.ConfigurationWriter)1 HBaseDDLExecutorFactory (co.cask.cdap.data2.util.hbase.HBaseDDLExecutorFactory)1 HBaseTableUtilFactory (co.cask.cdap.data2.util.hbase.HBaseTableUtilFactory)1 FileNotFoundException (java.io.FileNotFoundException)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 UnknownHostException (java.net.UnknownHostException)1 HashSet (java.util.HashSet)1