use of io.pravega.client.stream.ScalingPolicy in project pravega by pravega.
the class ZkStreamTest method testTransaction.
@Test(timeout = 10000)
public void testTransaction() throws Exception {
final ScalingPolicy policy = ScalingPolicy.fixed(5);
final StreamMetadataStore store = new ZKStreamMetadataStore(cli, executor);
final String streamName = "testTx";
store.createScope(SCOPE).get();
final Predicate<Throwable> operationNotAllowedPredicate = ex -> Exceptions.unwrap(ex) instanceof StoreException.IllegalStateException;
StreamConfiguration streamConfig = StreamConfiguration.builder().scope(SCOPE).streamName(streamName).scalingPolicy(policy).build();
store.createStream(SCOPE, streamName, streamConfig, System.currentTimeMillis(), null, executor).get();
store.setState(SCOPE, streamName, State.ACTIVE, null, executor).get();
OperationContext context = store.createContext(ZkStreamTest.SCOPE, streamName);
UUID txnId1 = UUID.randomUUID();
VersionedTransactionData tx = store.createTransaction(SCOPE, streamName, txnId1, 10000, 600000, 30000, context, executor).get();
Assert.assertEquals(txnId1, tx.getId());
UUID txnId2 = UUID.randomUUID();
VersionedTransactionData tx2 = store.createTransaction(SCOPE, streamName, txnId2, 10000, 600000, 30000, context, executor).get();
Assert.assertEquals(txnId2, tx2.getId());
store.sealTransaction(SCOPE, streamName, tx.getId(), true, Optional.<Integer>empty(), context, executor).get();
assert store.transactionStatus(SCOPE, streamName, tx.getId(), context, executor).get().equals(TxnStatus.COMMITTING);
// Test to ensure that sealTransaction is idempotent.
Assert.assertEquals(TxnStatus.COMMITTING, store.sealTransaction(SCOPE, streamName, tx.getId(), true, Optional.empty(), context, executor).join().getKey());
// Test to ensure that COMMITTING transaction cannot be aborted.
testAbortFailure(store, SCOPE, streamName, tx.getEpoch(), tx.getId(), context, operationNotAllowedPredicate);
CompletableFuture<TxnStatus> f1 = store.commitTransaction(SCOPE, streamName, tx.getEpoch(), tx.getId(), context, executor);
store.sealTransaction(SCOPE, streamName, tx2.getId(), false, Optional.<Integer>empty(), context, executor).get();
assert store.transactionStatus(SCOPE, streamName, tx2.getId(), context, executor).get().equals(TxnStatus.ABORTING);
// Test to ensure that sealTransaction is idempotent.
Assert.assertEquals(TxnStatus.ABORTING, store.sealTransaction(SCOPE, streamName, tx2.getId(), false, Optional.empty(), context, executor).join().getKey());
// Test to ensure that ABORTING transaction cannot be committed.
testCommitFailure(store, SCOPE, streamName, tx2.getEpoch(), tx2.getId(), context, operationNotAllowedPredicate);
CompletableFuture<TxnStatus> f2 = store.abortTransaction(SCOPE, streamName, tx2.getEpoch(), tx2.getId(), context, executor);
CompletableFuture.allOf(f1, f2).get();
assert store.transactionStatus(SCOPE, streamName, tx.getId(), context, executor).get().equals(TxnStatus.COMMITTED);
assert store.transactionStatus(SCOPE, streamName, tx2.getId(), context, executor).get().equals(TxnStatus.ABORTED);
// Test to ensure that sealTransaction, to commit it, on committed transaction does not throw an error.
Assert.assertEquals(TxnStatus.COMMITTED, store.sealTransaction(SCOPE, streamName, tx.getId(), true, Optional.empty(), context, executor).join().getKey());
// Test to ensure that commitTransaction is idempotent.
Assert.assertEquals(TxnStatus.COMMITTED, store.commitTransaction(SCOPE, streamName, tx.getEpoch(), tx.getId(), context, executor).join());
// Test to ensure that sealTransaction, to abort it, and abortTransaction on committed transaction throws error.
testAbortFailure(store, SCOPE, streamName, tx.getEpoch(), tx.getId(), context, operationNotAllowedPredicate);
// Test to ensure that sealTransaction, to abort it, on aborted transaction does not throw an error.
Assert.assertEquals(TxnStatus.ABORTED, store.sealTransaction(SCOPE, streamName, tx2.getId(), false, Optional.empty(), context, executor).join().getKey());
// Test to ensure that abortTransaction is idempotent.
Assert.assertEquals(TxnStatus.ABORTED, store.abortTransaction(SCOPE, streamName, tx2.getEpoch(), tx2.getId(), context, executor).join());
// Test to ensure that sealTransaction, to abort it, and abortTransaction on committed transaction throws error.
testCommitFailure(store, SCOPE, streamName, tx2.getEpoch(), tx2.getId(), context, operationNotAllowedPredicate);
assert store.commitTransaction(ZkStreamTest.SCOPE, streamName, 0, UUID.randomUUID(), null, executor).handle((ok, ex) -> {
if (ex.getCause() instanceof StoreException.DataNotFoundException) {
return true;
} else {
throw new RuntimeException("assert failed");
}
}).get();
assert store.abortTransaction(ZkStreamTest.SCOPE, streamName, 0, UUID.randomUUID(), null, executor).handle((ok, ex) -> {
if (ex.getCause() instanceof StoreException.DataNotFoundException) {
return true;
} else {
throw new RuntimeException("assert failed");
}
}).get();
assert store.transactionStatus(ZkStreamTest.SCOPE, streamName, UUID.randomUUID(), context, executor).get().equals(TxnStatus.UNKNOWN);
}
use of io.pravega.client.stream.ScalingPolicy in project pravega by pravega.
the class ModelHelperTest method encodeScalingPolicy.
@Test
public void encodeScalingPolicy() {
ScalingPolicy policy = ModelHelper.encode(ModelHelper.decode(ScalingPolicy.byEventRate(100, 2, 3)));
assertEquals(ScalingPolicy.ScaleType.BY_RATE_IN_EVENTS_PER_SEC, policy.getScaleType());
assertEquals(100L, policy.getTargetRate());
assertEquals(2, policy.getScaleFactor());
assertEquals(3, policy.getMinNumSegments());
}
Aggregations