use of io.pravega.controller.store.TestOperationContext in project pravega by pravega.
the class ZkStreamTest method testStreamRecreation.
@Test(timeout = 30000)
public void testStreamRecreation() {
// We will first create stream. Verify that its metadata is present in the cache.
ZKStoreHelper storeHelper = new ZKStoreHelper(cli, executor);
ZkOrderedStore orderer = new ZkOrderedStore("txn", storeHelper, executor);
String scope = "scope";
String stream1 = "streamToDelete";
ZKStream stream = new ZKStream(scope, stream1, storeHelper, executor, orderer);
final int startingSegmentNumber = 0;
storeHelper.createZNodeIfNotExist("/store/scope").join();
final ScalingPolicy policy1 = ScalingPolicy.fixed(2);
final StreamConfiguration configuration1 = StreamConfiguration.builder().scalingPolicy(policy1).build();
OperationContext context = new TestOperationContext();
stream.create(configuration1, System.currentTimeMillis(), startingSegmentNumber, context).join();
stream.createStreamPositionNodeIfAbsent(0).join();
stream.updateState(State.ACTIVE, context).join();
Long creationTime = stream.getCreationTime(context).join();
Integer position = stream.getStreamPosition().join();
assertEquals(0, position.intValue());
ZKStoreHelper.ZkCacheKey<Integer> key = new ZKStoreHelper.ZkCacheKey<>(stream.getCreationPath(), position.toString(), x -> BitConverter.readInt(x, 0));
VersionedMetadata<?> cachedCreationTime = storeHelper.getCache().getCachedData(key);
// verify that both timestamps are same
assertEquals(creationTime, cachedCreationTime.getObject());
// delete stream.
stream.updateState(State.SEALING, context).join();
stream.updateState(State.SEALED, context).join();
stream.deleteStream(context).join();
// refresh the stream object to indicate new request context
stream.refresh();
AssertExtensions.assertFutureThrows("should throw data not found for stream", stream.getEpochRecord(0, context), e -> Exceptions.unwrap(e) instanceof StoreException.DataNotFoundException);
// refresh the stream object to indicate new request context
stream.refresh();
// verify that metadata doesn't exist in the store.
AssertExtensions.assertFutureThrows("Stream deleted", stream.getCreationTime(context), e -> Exceptions.unwrap(e) instanceof StoreException.DataNotFoundException);
// verify that cached entries still exist.
VersionedMetadata<?> cachedCreationTimeExists = storeHelper.getCache().getCachedData(key);
assertEquals(cachedCreationTime.getObject(), cachedCreationTimeExists.getObject());
// create stream again.
stream.create(configuration1, System.currentTimeMillis(), startingSegmentNumber, context).join();
stream.createStreamPositionNodeIfAbsent(1).join();
stream.updateState(State.ACTIVE, context).join();
Long creationTimeNew = stream.getCreationTime(context).join();
Integer positionNew = stream.getStreamPosition().join();
assertEquals(1, positionNew.intValue());
ZKStoreHelper.ZkCacheKey<Integer> keyNew = new ZKStoreHelper.ZkCacheKey<>(stream.getCreationPath(), positionNew.toString(), x -> BitConverter.readInt(x, 0));
VersionedMetadata<?> cachedCreationTimeNew = storeHelper.getCache().getCachedData(keyNew);
// verify that both times are different
assertNotEquals(creationTime, creationTimeNew);
assertNotEquals(cachedCreationTime.getObject(), cachedCreationTimeNew.getObject());
}
use of io.pravega.controller.store.TestOperationContext in project pravega by pravega.
the class ZkStreamTest method testGetActiveTxn.
@Test(timeout = 30000)
public void testGetActiveTxn() throws Exception {
ZKStoreHelper storeHelper = spy(new ZKStoreHelper(cli, executor));
ZkOrderedStore orderer = new ZkOrderedStore("txn", storeHelper, executor);
ZKStream stream = new ZKStream("scope", "stream", storeHelper, executor, orderer);
final int startingSegmentNumber = 0;
storeHelper.createZNodeIfNotExist("/store/scope").join();
final ScalingPolicy policy1 = ScalingPolicy.fixed(2);
final StreamConfiguration configuration1 = StreamConfiguration.builder().scalingPolicy(policy1).build();
OperationContext context = new TestOperationContext();
stream.create(configuration1, System.currentTimeMillis(), startingSegmentNumber, context).join();
stream.updateState(State.ACTIVE, context).join();
UUID txId = stream.generateNewTxnId(0, 0L, context).join();
stream.createTransaction(txId, 1000L, 1000L, context).join();
String activeTxPath = stream.getActiveTxPath(0, txId.toString());
// throw DataNotFoundException for txn path
doReturn(Futures.failedFuture(StoreException.create(StoreException.Type.DATA_NOT_FOUND, "txn data not found"))).when(storeHelper).getData(eq(activeTxPath), any());
Map<UUID, ActiveTxnRecord> result = stream.getActiveTxns(context).join();
// verify that call succeeds and no active txns were found
assertTrue(result.isEmpty());
// throw generic exception for txn path
doReturn(Futures.failedFuture(new RuntimeException())).when(storeHelper).getData(eq(activeTxPath), any());
ZKStream stream2 = new ZKStream("scope", "stream", storeHelper, executor, orderer);
// verify that the call fails
AssertExtensions.assertFutureThrows("", stream2.getActiveTxns(context), e -> Exceptions.unwrap(e) instanceof RuntimeException);
reset(storeHelper);
ZKStream stream3 = new ZKStream("scope", "stream", storeHelper, executor, orderer);
result = stream3.getActiveTxns(context).join();
assertEquals(1, result.size());
}
Aggregations