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);
}
}
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);
}
}
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()));
}
}
}
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());
}
}
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());
}
Aggregations