Search in sources :

Example 26 with GrpcAuthHelper

use of io.pravega.controller.server.security.auth.GrpcAuthHelper in project pravega by pravega.

the class StreamTransactionMetadataTasksTest method partialTxnCreationTest.

@Test(timeout = 10000)
public void partialTxnCreationTest() {
    // Create mock writer objects.
    EventStreamWriterMock<CommitEvent> commitWriter = new EventStreamWriterMock<>();
    EventStreamWriterMock<AbortEvent> abortWriter = new EventStreamWriterMock<>();
    // Create transaction tasks.
    txnTasks = new StreamTransactionMetadataTasks(streamStore, SegmentHelperMock.getFailingSegmentHelperMock(), executor, "host", new GrpcAuthHelper(this.authEnabled, "secret", 600));
    txnTasks.initializeStreamWriters(commitWriter, abortWriter);
    // Create ControllerService.
    consumer = new ControllerService(kvtStore, kvtMetadataTasks, streamStore, bucketStore, streamMetadataTasks, txnTasks, segmentHelperMock, executor, null, requestTracker);
    final ScalingPolicy policy1 = ScalingPolicy.fixed(2);
    final StreamConfiguration configuration1 = StreamConfiguration.builder().scalingPolicy(policy1).build();
    // Create stream and scope
    Assert.assertEquals(Controller.CreateScopeStatus.Status.SUCCESS, consumer.createScope(SCOPE, 0L).join().getStatus());
    Assert.assertEquals(Controller.CreateStreamStatus.Status.SUCCESS, streamMetadataTasks.createStream(SCOPE, STREAM, configuration1, 0, 0L).join());
    // Create partial transaction
    final long lease = 10000;
    AssertExtensions.assertFutureThrows("Transaction creation fails, although a new txn id gets added to the store", txnTasks.createTxn(SCOPE, STREAM, lease, 0L, 1024 * 1024L), e -> e instanceof RuntimeException);
    // Ensure that exactly one transaction is active on the stream.
    Set<UUID> txns = streamStore.getActiveTxns(SCOPE, STREAM, null, executor).join().keySet();
    assertEquals(1, txns.size());
    // Ensure that transaction state is OPEN.
    UUID txn1 = txns.stream().findFirst().get();
    assertEquals(TxnStatus.OPEN, streamStore.transactionStatus(SCOPE, STREAM, txn1, null, executor).join());
    // Ensure that timeout service knows about the transaction.
    assertTrue(txnTasks.getTimeoutService().containsTxn(SCOPE, STREAM, txn1));
}
Also used : ScalingPolicy(io.pravega.client.stream.ScalingPolicy) EventStreamWriterMock(io.pravega.controller.mocks.EventStreamWriterMock) ControllerService(io.pravega.controller.server.ControllerService) GrpcAuthHelper(io.pravega.controller.server.security.auth.GrpcAuthHelper) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) CommitEvent(io.pravega.shared.controller.event.CommitEvent) AbortEvent(io.pravega.shared.controller.event.AbortEvent) UUID(java.util.UUID) Test(org.junit.Test)

Example 27 with GrpcAuthHelper

use of io.pravega.controller.server.security.auth.GrpcAuthHelper in project pravega by pravega.

the class StreamTransactionMetadataTasksTest method txnCreationTest.

@Test(timeout = 10000)
public void txnCreationTest() {
    // Create mock writer objects.
    EventStreamWriterMock<CommitEvent> commitWriter = new EventStreamWriterMock<>();
    EventStreamWriterMock<AbortEvent> abortWriter = new EventStreamWriterMock<>();
    StreamMetadataStore streamStoreMock = spy(StreamStoreFactory.createZKStore(zkClient, executor));
    // Create transaction tasks.
    txnTasks = new StreamTransactionMetadataTasks(streamStoreMock, SegmentHelperMock.getSegmentHelperMock(), executor, "host", new GrpcAuthHelper(this.authEnabled, "secret", 600));
    txnTasks.initializeStreamWriters(commitWriter, abortWriter);
    final ScalingPolicy policy1 = ScalingPolicy.fixed(2);
    final StreamConfiguration configuration1 = StreamConfiguration.builder().scalingPolicy(policy1).build();
    // Create stream and scope
    streamStoreMock.createScope(SCOPE, null, executor).join();
    streamStoreMock.createStream(SCOPE, STREAM, configuration1, System.currentTimeMillis(), null, executor).join();
    streamStoreMock.setState(SCOPE, STREAM, State.ACTIVE, null, executor).join();
    // mock streamMetadataStore.generateTxnId should throw excecption first time.
    // Note: it should be retried.
    // now the id should have been generated
    doAnswer(new Answer<CompletableFuture<UUID>>() {

        AtomicInteger count = new AtomicInteger(0);

        @Override
        public CompletableFuture<UUID> answer(InvocationOnMock invocation) throws Throwable {
            // first time throw exception.
            if (count.getAndIncrement() == 0) {
                return Futures.failedFuture(StoreException.create(StoreException.Type.WRITE_CONFLICT, "write conflict on counter update"));
            }
            // subsequent times call origin method
            @SuppressWarnings("unchecked") CompletableFuture<UUID> future = (CompletableFuture<UUID>) invocation.callRealMethod();
            return future;
        }
    }).when(streamStoreMock).generateTransactionId(eq(SCOPE), eq(STREAM), any(), any());
    doAnswer(new Answer<CompletableFuture<VersionedTransactionData>>() {

        AtomicInteger count = new AtomicInteger(0);

        @Override
        public CompletableFuture<VersionedTransactionData> answer(InvocationOnMock invocation) throws Throwable {
            // first time throw exception.
            if (count.getAndIncrement() == 0) {
                return Futures.failedFuture(StoreException.create(StoreException.Type.DATA_NOT_FOUND, "Epoch not found"));
            }
            // subsequent times call origin method
            @SuppressWarnings("unchecked") CompletableFuture<VersionedTransactionData> future = (CompletableFuture<VersionedTransactionData>) invocation.callRealMethod();
            return future;
        }
    }).when(streamStoreMock).createTransaction(any(), any(), any(), anyLong(), anyLong(), any(), any());
    Pair<VersionedTransactionData, List<StreamSegmentRecord>> txn = txnTasks.createTxn(SCOPE, STREAM, 10000L, 0L, 1024 * 1024L).join();
    // verify that generate transaction id is called 3 times
    verify(streamStoreMock, times(3)).generateTransactionId(any(), any(), any(), any());
    // verify that create transaction is called 2 times
    verify(streamStoreMock, times(2)).createTransaction(any(), any(), any(), anyLong(), anyLong(), any(), any());
    // verify that the txn id that is generated is of type ""
    UUID txnId = txn.getKey().getId();
    assertEquals(0, (int) (txnId.getMostSignificantBits() >> 32));
    assertEquals(2, txnId.getLeastSignificantBits());
}
Also used : ScalingPolicy(io.pravega.client.stream.ScalingPolicy) EventStreamWriterMock(io.pravega.controller.mocks.EventStreamWriterMock) StreamMetadataStore(io.pravega.controller.store.stream.StreamMetadataStore) VersionedTransactionData(io.pravega.controller.store.stream.VersionedTransactionData) CompletableFuture(java.util.concurrent.CompletableFuture) GrpcAuthHelper(io.pravega.controller.server.security.auth.GrpcAuthHelper) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InvocationOnMock(org.mockito.invocation.InvocationOnMock) StreamConfiguration(io.pravega.client.stream.StreamConfiguration) CommitEvent(io.pravega.shared.controller.event.CommitEvent) ArrayList(java.util.ArrayList) List(java.util.List) AbortEvent(io.pravega.shared.controller.event.AbortEvent) UUID(java.util.UUID) Test(org.junit.Test)

Example 28 with GrpcAuthHelper

use of io.pravega.controller.server.security.auth.GrpcAuthHelper in project pravega by pravega.

the class PravegaTablesStoreHelperTest method testNoRetriesOnUpdate.

@Test
public void testNoRetriesOnUpdate() {
    SegmentHelper segmentHelper = SegmentHelperMock.getSegmentHelperMockForTables(executor);
    GrpcAuthHelper authHelper = GrpcAuthHelper.getDisabledAuthHelper();
    PravegaTablesStoreHelper storeHelper = spy(new PravegaTablesStoreHelper(segmentHelper, authHelper, executor, 2));
    // region connection dropped
    CompletableFuture<Void> connectionDropped = Futures.failedFuture(new WireCommandFailedException(WireCommandType.UPDATE_TABLE_ENTRIES, WireCommandFailedException.Reason.ConnectionDropped));
    doAnswer(x -> connectionDropped).when(segmentHelper).updateTableEntries(anyString(), any(), anyString(), anyLong());
    AssertExtensions.assertFutureThrows("ConnectionDropped", storeHelper.addNewEntry("table", "key", new byte[0], x -> x, 0L), e -> Exceptions.unwrap(e) instanceof StoreException.StoreConnectionException);
    verify(segmentHelper, times(1)).updateTableEntries(anyString(), any(), anyString(), anyLong());
    AssertExtensions.assertFutureThrows("ConnectionDropped", storeHelper.updateEntry("table", "key", new byte[0], x -> x, new Version.LongVersion(0L), 0L), e -> Exceptions.unwrap(e) instanceof StoreException.StoreConnectionException);
    verify(segmentHelper, times(2)).updateTableEntries(anyString(), any(), anyString(), anyLong());
    // endregion
    // region connectionfailed
    CompletableFuture<Void> connectionFailed = Futures.failedFuture(new WireCommandFailedException(WireCommandType.UPDATE_TABLE_ENTRIES, WireCommandFailedException.Reason.ConnectionFailed));
    doAnswer(x -> connectionFailed).when(segmentHelper).updateTableEntries(anyString(), any(), anyString(), anyLong());
    AssertExtensions.assertFutureThrows("ConnectionDropped", storeHelper.addNewEntry("table", "key", new byte[0], x -> x, 0L), e -> Exceptions.unwrap(e) instanceof StoreException.StoreConnectionException);
    verify(segmentHelper, times(3)).updateTableEntries(anyString(), any(), anyString(), anyLong());
    AssertExtensions.assertFutureThrows("ConnectionDropped", storeHelper.updateEntry("table", "key", new byte[0], x -> x, new Version.LongVersion(0L), 0L), e -> Exceptions.unwrap(e) instanceof StoreException.StoreConnectionException);
    verify(segmentHelper, times(4)).updateTableEntries(anyString(), any(), anyString(), anyLong());
    // endregion
    CompletableFuture<Void> unknownHost = Futures.failedFuture(new WireCommandFailedException(WireCommandType.UPDATE_TABLE_ENTRIES, WireCommandFailedException.Reason.UnknownHost));
    doAnswer(x -> unknownHost).when(segmentHelper).updateTableEntries(anyString(), any(), anyString(), anyLong());
    AssertExtensions.assertFutureThrows("ConnectionDropped", storeHelper.addNewEntry("table", "key", new byte[0], x -> x, 0L), e -> Exceptions.unwrap(e) instanceof StoreException.StoreConnectionException);
    // this should be retried. we have configured 2 retries, so 2 retries should happen hence jump from 4 to 6.
    verify(segmentHelper, times(6)).updateTableEntries(anyString(), any(), anyString(), anyLong());
    AssertExtensions.assertFutureThrows("ConnectionDropped", storeHelper.updateEntry("table", "key", new byte[0], x -> x, new Version.LongVersion(0L), 0L), e -> Exceptions.unwrap(e) instanceof StoreException.StoreConnectionException);
    verify(segmentHelper, times(8)).updateTableEntries(anyString(), any(), anyString(), anyLong());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) SegmentHelper(io.pravega.controller.server.SegmentHelper) AssertExtensions(io.pravega.test.common.AssertExtensions) Exceptions(io.pravega.common.Exceptions) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Mockito.spy(org.mockito.Mockito.spy) Unpooled(io.netty.buffer.Unpooled) ArrayList(java.util.ArrayList) Assert.assertSame(org.junit.Assert.assertSame) Lists(com.google.common.collect.Lists) ByteBuf(io.netty.buffer.ByteBuf) StoreException(io.pravega.controller.store.stream.StoreException) Mockito.anyBoolean(org.mockito.Mockito.anyBoolean) Map(java.util.Map) After(org.junit.After) Mockito.doAnswer(org.mockito.Mockito.doAnswer) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) WireCommandType(io.pravega.shared.protocol.netty.WireCommandType) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) Before(org.junit.Before) SegmentHelperMock(io.pravega.controller.mocks.SegmentHelperMock) Assert.assertTrue(org.junit.Assert.assertTrue) Test(org.junit.Test) Mockito.times(org.mockito.Mockito.times) Mockito.verify(org.mockito.Mockito.verify) AbstractMap(java.util.AbstractMap) Base64(java.util.Base64) List(java.util.List) WireCommandFailedException(io.pravega.controller.server.WireCommandFailedException) ExecutorServiceHelpers(io.pravega.common.concurrent.ExecutorServiceHelpers) Futures(io.pravega.common.concurrent.Futures) Assert.assertEquals(org.junit.Assert.assertEquals) GrpcAuthHelper(io.pravega.controller.server.security.auth.GrpcAuthHelper) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) GrpcAuthHelper(io.pravega.controller.server.security.auth.GrpcAuthHelper) WireCommandFailedException(io.pravega.controller.server.WireCommandFailedException) SegmentHelper(io.pravega.controller.server.SegmentHelper) StoreException(io.pravega.controller.store.stream.StoreException) Test(org.junit.Test)

Aggregations

GrpcAuthHelper (io.pravega.controller.server.security.auth.GrpcAuthHelper)28 Test (org.junit.Test)22 SegmentHelper (io.pravega.controller.server.SegmentHelper)10 Controller (io.pravega.controller.stream.api.grpc.v1.Controller)10 StreamMetadataStore (io.pravega.controller.store.stream.StreamMetadataStore)9 StreamConfiguration (io.pravega.client.stream.StreamConfiguration)8 AuthorizationResourceImpl (io.pravega.shared.security.auth.AuthorizationResourceImpl)8 UUID (java.util.UUID)8 TaskMetadataStore (io.pravega.controller.store.task.TaskMetadataStore)7 ArrayList (java.util.ArrayList)7 EventStreamWriterMock (io.pravega.controller.mocks.EventStreamWriterMock)6 EventHelper (io.pravega.controller.task.EventHelper)6 List (java.util.List)6 ScalingPolicy (io.pravega.client.stream.ScalingPolicy)5 AbortEvent (io.pravega.shared.controller.event.AbortEvent)5 CommitEvent (io.pravega.shared.controller.event.CommitEvent)5 ControllerEventStreamWriterMock (io.pravega.controller.mocks.ControllerEventStreamWriterMock)4 BucketStore (io.pravega.controller.store.stream.BucketStore)4 TableMetadataTasks (io.pravega.controller.task.KeyValueTable.TableMetadataTasks)4 StreamMetadataTasks (io.pravega.controller.task.Stream.StreamMetadataTasks)4