Search in sources :

Example 11 with AuthenticationException

use of io.pravega.auth.AuthenticationException in project pravega by pravega.

the class RawClientTest method testExceptionHandling.

@Test
public void testExceptionHandling() throws ConnectionFailedException {
    PravegaNodeUri endpoint = new PravegaNodeUri("localhost", -1);
    @Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    @Cleanup MockController controller = new MockController(endpoint.getEndpoint(), endpoint.getPort(), connectionFactory, true);
    ClientConnection connection = Mockito.mock(ClientConnection.class);
    connectionFactory.provideConnection(endpoint, connection);
    Segment segment = new Segment("scope", "test", 0);
    RawClient rawClient = new RawClient(controller, connectionFactory, segment);
    WireCommands.ReadSegment request1 = new WireCommands.ReadSegment(segment.getScopedName(), 0, 10, "", requestId);
    CompletableFuture<Reply> future = rawClient.sendRequest(requestId, request1);
    // Verify if the request was sent over the connection.
    Mockito.verify(connection).send(Mockito.eq(request1));
    assertFalse("Since there is no response the future should not be completed", future.isDone());
    ReplyProcessor processor = connectionFactory.getProcessor(endpoint);
    processor.processingFailure(new ConnectionFailedException("Custom error"));
    assertTrue(future.isCompletedExceptionally());
    assertFutureThrows("The future should be completed exceptionally", future, t -> t instanceof ConnectionFailedException);
    rawClient.close();
    rawClient = new RawClient(controller, connectionFactory, segment);
    WireCommands.ReadSegment request2 = new WireCommands.ReadSegment(segment.getScopedName(), 0, 10, "", 2L);
    future = rawClient.sendRequest(2L, request2);
    // Verify if the request was sent over the connection.
    Mockito.verify(connection).send(Mockito.eq(request2));
    assertFalse("Since there is no response the future should not be completed", future.isDone());
    processor = connectionFactory.getProcessor(endpoint);
    processor.authTokenCheckFailed(new WireCommands.AuthTokenCheckFailed(2L, "", WireCommands.AuthTokenCheckFailed.ErrorCode.TOKEN_CHECK_FAILED));
    assertTrue(future.isCompletedExceptionally());
    assertFutureThrows("The future should be completed exceptionally", future, t -> t instanceof AuthenticationException);
    rawClient.close();
}
Also used : AuthenticationException(io.pravega.auth.AuthenticationException) Cleanup(lombok.Cleanup) Segment(io.pravega.client.segment.impl.Segment) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) Reply(io.pravega.shared.protocol.netty.Reply) WireCommands(io.pravega.shared.protocol.netty.WireCommands) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) ReplyProcessor(io.pravega.shared.protocol.netty.ReplyProcessor) Test(org.junit.Test)

Example 12 with AuthenticationException

use of io.pravega.auth.AuthenticationException in project pravega by pravega.

the class ConditionalOutputStreamTest method handleUnexpectedReplythrowsAppropriateTokenExceptions.

@Test
public void handleUnexpectedReplythrowsAppropriateTokenExceptions() {
    @Cleanup MockConnectionFactoryImpl connectionFactory = new MockConnectionFactoryImpl();
    @Cleanup MockController controller = new MockController("localhost", 0, connectionFactory, true);
    ConditionalOutputStreamFactory factory = new ConditionalOutputStreamFactoryImpl(controller, connectionFactory);
    Segment segment = new Segment("scope", "testWrite", 1);
    @Cleanup ConditionalOutputStreamImpl objectUnderTest = (ConditionalOutputStreamImpl) factory.createConditionalOutputStream(segment, DelegationTokenProviderFactory.create("token", controller, segment, AccessOperation.ANY), EventWriterConfig.builder().build());
    AssertExtensions.assertThrows("AuthenticationException wasn't thrown", () -> objectUnderTest.handleUnexpectedReply(new WireCommands.AuthTokenCheckFailed(1L, "SomeException", WireCommands.AuthTokenCheckFailed.ErrorCode.TOKEN_CHECK_FAILED), "test"), e -> e instanceof AuthenticationException);
    AssertExtensions.assertThrows("AuthenticationException wasn't thrown", () -> objectUnderTest.handleUnexpectedReply(new WireCommands.AuthTokenCheckFailed(1L, "SomeException", WireCommands.AuthTokenCheckFailed.ErrorCode.UNSPECIFIED), "test"), e -> e instanceof AuthenticationException);
    AssertExtensions.assertThrows("TokenExpiredException wasn't thrown", () -> objectUnderTest.handleUnexpectedReply(new WireCommands.AuthTokenCheckFailed(1L, "SomeException", WireCommands.AuthTokenCheckFailed.ErrorCode.TOKEN_EXPIRED), "test"), e -> e instanceof TokenExpiredException);
    AssertExtensions.assertThrows("InvalidEventNumber wasn't treated as a connection failure", () -> objectUnderTest.handleUnexpectedReply(new WireCommands.InvalidEventNumber(UUID.randomUUID(), 1, "SomeException"), "test"), e -> e instanceof ConnectionFailedException);
    AssertExtensions.assertThrows("Hello wasn't treated as a connection failure", () -> objectUnderTest.handleUnexpectedReply(new WireCommands.Hello(1, 1), "test"), e -> e instanceof ConnectionFailedException);
}
Also used : AuthenticationException(io.pravega.auth.AuthenticationException) Cleanup(lombok.Cleanup) TokenExpiredException(io.pravega.auth.TokenExpiredException) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) MockController(io.pravega.client.stream.mock.MockController) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) Test(org.junit.Test)

Example 13 with AuthenticationException

use of io.pravega.auth.AuthenticationException in project pravega by pravega.

the class SegmentHelperTest method createTransaction.

@Test
public void createTransaction() {
    MockConnectionFactory factory = new MockConnectionFactory();
    @Cleanup SegmentHelper helper = new SegmentHelper(factory, new MockHostControllerStore(), executorService());
    UUID txId = new UUID(0, 0L);
    CompletableFuture<Void> retVal = helper.createTransaction("", "", 0L, txId, "", System.nanoTime(), 1024 * 1024L);
    long requestId = ((MockConnection) (factory.connection)).getRequestId();
    factory.rp.process(new WireCommands.AuthTokenCheckFailed(requestId, "SomeException"));
    AssertExtensions.assertThrows("", () -> retVal.join(), ex -> ex instanceof WireCommandFailedException && ex.getCause() instanceof AuthenticationException);
    CompletableFuture<Void> result = helper.createTransaction("", "", 0L, new UUID(0L, 0L), "", System.nanoTime(), 1024 * 1024L);
    requestId = ((MockConnection) (factory.connection)).getRequestId();
    factory.rp.process(new WireCommands.SegmentCreated(requestId, getQualifiedStreamSegmentName("", "", 0L)));
    result.join();
    result = helper.createTransaction("", "", 0L, new UUID(0L, 0L), "", System.nanoTime(), 1024 * 1024L);
    requestId = ((MockConnection) (factory.connection)).getRequestId();
    factory.rp.process(new WireCommands.SegmentAlreadyExists(requestId, getQualifiedStreamSegmentName("", "", 0L), ""));
    result.join();
    Supplier<CompletableFuture<?>> futureSupplier = () -> helper.createTransaction("", "", 0L, txId, "", System.nanoTime(), 1024 * 1024L);
    validateProcessingFailureCFE(factory, futureSupplier);
    testConnectionFailure(factory, futureSupplier);
}
Also used : AuthenticationException(io.pravega.auth.AuthenticationException) Cleanup(lombok.Cleanup) CompletableFuture(java.util.concurrent.CompletableFuture) UUID(java.util.UUID) WireCommands(io.pravega.shared.protocol.netty.WireCommands) Test(org.junit.Test)

Example 14 with AuthenticationException

use of io.pravega.auth.AuthenticationException in project pravega by pravega.

the class SegmentHelperTest method commitTransaction.

@Test
public void commitTransaction() {
    MockConnectionFactory factory = new MockConnectionFactory();
    String scope = "testScope";
    String stream = "testStream";
    String delegationToken = "";
    long sourceSegmentId = 1L;
    long targetSegmentId = 1L;
    UUID txnId = new UUID(0, 0L);
    List<UUID> txnIdList = List.of(txnId);
    @Cleanup SegmentHelper helper = new SegmentHelper(factory, new MockHostControllerStore(), executorService());
    CompletableFuture<List<Long>> retVal = helper.mergeTxnSegments(scope, stream, targetSegmentId, sourceSegmentId, txnIdList, delegationToken, System.nanoTime());
    long requestId = ((MockConnection) (factory.connection)).getRequestId();
    factory.rp.process(new WireCommands.AuthTokenCheckFailed(requestId, "SomeException"));
    AssertExtensions.assertThrows("", () -> retVal.join(), ex -> ex instanceof WireCommandFailedException && ex.getCause() instanceof AuthenticationException);
    CompletableFuture<List<Long>> result = helper.mergeTxnSegments(scope, stream, targetSegmentId, sourceSegmentId, txnIdList, delegationToken, System.nanoTime());
    requestId = ((MockConnection) (factory.connection)).getRequestId();
    factory.rp.process(new WireCommands.SegmentsBatchMerged(requestId, getQualifiedStreamSegmentName(scope, stream, targetSegmentId), List.of(getQualifiedStreamSegmentName(scope, stream, sourceSegmentId)), List.of(10L)));
    result.join();
    CompletableFuture<List<Long>> resultException = helper.mergeTxnSegments(scope, stream, targetSegmentId, sourceSegmentId, txnIdList, delegationToken, System.nanoTime());
    requestId = ((MockConnection) (factory.connection)).getRequestId();
    factory.rp.process(new WireCommands.NoSuchSegment(requestId, getQualifiedStreamSegmentName(scope, stream, targetSegmentId), "", 0L));
    AssertExtensions.assertThrows("", () -> resultException.join(), ex -> ex instanceof WireCommandFailedException && ((WireCommandFailedException) ex).getReason().equals(WireCommandFailedException.Reason.SegmentDoesNotExist));
    Supplier<CompletableFuture<?>> futureSupplier = () -> helper.mergeTxnSegments(scope, stream, targetSegmentId, sourceSegmentId, txnIdList, delegationToken, System.nanoTime());
    validateProcessingFailureCFE(factory, futureSupplier);
    testConnectionFailure(factory, futureSupplier);
}
Also used : AuthenticationException(io.pravega.auth.AuthenticationException) Cleanup(lombok.Cleanup) CompletableFuture(java.util.concurrent.CompletableFuture) List(java.util.List) UUID(java.util.UUID) WireCommands(io.pravega.shared.protocol.netty.WireCommands) Test(org.junit.Test)

Example 15 with AuthenticationException

use of io.pravega.auth.AuthenticationException in project pravega by pravega.

the class TableSegmentImplTest method testReconnect.

/**
 * Tests the ability to reconnect in the following situations:
 * - {@link ConnectionFailedException}
 * - {@link AuthenticationException})
 * - Unexpected replies.
 * <p>
 * These should result in automatic retries and reconnects up to a certain point.
 */
@Test
public void testReconnect() throws Exception {
    val keys = Arrays.asList(100L, 200L, 300L);
    val entries = Arrays.asList(versionedEntry(keys.get(0), "one hundred", 1L), versionedEntry(keys.get(1), "two hundred", 2L), versionedEntry(keys.get(2), "three hundred", 3L));
    val versions = entries.stream().map(e -> e.getKey().getVersion().getSegmentVersion()).collect(Collectors.toList());
    // All retryable replies. WrongHost and AuthTokenCheckFailed are converted into exceptions by RawClient, while all
    // others need to be handled by TableSegmentImpl.handleReply.
    val failureReplies = Arrays.<Function<Long, Reply>>asList(requestId -> new WireCommands.WrongHost(requestId, SEGMENT.getScopedName(), "NewHost", ""), requestId -> new WireCommands.AuthTokenCheckFailed(requestId, "", WireCommands.AuthTokenCheckFailed.ErrorCode.TOKEN_EXPIRED), requestId -> new WireCommands.OperationUnsupported(requestId, "Intentional", ""));
    for (val fr : failureReplies) {
        // TableSegment.put()
        testConnectionFailure(ts -> ts.put(entries.get(0)), fr, requestId -> new WireCommands.TableEntriesUpdated(requestId, versions), result -> Assert.assertEquals("", (long) versions.get(0), result.getSegmentVersion()));
        // TableSegment.get()
        val wireEntries = entries.subList(0, 1);
        testConnectionFailure(ts -> ts.get(buf(keys.get(0))), fr, requestId -> new WireCommands.TableRead(requestId, SEGMENT.getScopedName(), toWireEntries(wireEntries, null)), result -> Assert.assertTrue("", entryEquals(wireEntries.get(0), result)));
        // TableSegment.remove()
        testConnectionFailure(ts -> ts.remove(unversionedKey(keys.get(0))), fr, requestId -> new WireCommands.TableKeysRemoved(requestId, SEGMENT.getScopedName()), result -> {
        });
        // Iterators. It is sufficient to test one of them.
        val args = SegmentIteratorArgs.builder().maxItemsAtOnce(1).fromKey(Unpooled.wrappedBuffer(new byte[] { 1 })).toKey(Unpooled.wrappedBuffer(new byte[] { 1 })).build();
        testConnectionFailure(ts -> ts.entryIterator(args).getNext(), fr, requestId -> new WireCommands.TableEntriesRead(requestId, SEGMENT.getScopedName(), toWireEntries(entries, null), Unpooled.wrappedBuffer(new byte[1])), result -> AssertExtensions.assertListEquals("", entries, result.getItems(), this::entryEquals));
    }
}
Also used : lombok.val(lombok.val) Arrays(java.util.Arrays) NoSuchKeyException(io.pravega.client.tables.NoSuchKeyException) AssertExtensions(io.pravega.test.common.AssertExtensions) RequiredArgsConstructor(lombok.RequiredArgsConstructor) Cleanup(lombok.Cleanup) Unpooled(io.netty.buffer.Unpooled) MockController(io.pravega.client.stream.mock.MockController) ClientConnection(io.pravega.client.connection.impl.ClientConnection) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) PravegaNodeUri(io.pravega.shared.protocol.netty.PravegaNodeUri) IteratorItem(io.pravega.client.tables.IteratorItem) Request(io.pravega.shared.protocol.netty.Request) DelegationTokenProviderFactory(io.pravega.client.security.auth.DelegationTokenProviderFactory) Collectors(java.util.stream.Collectors) RetriesExhaustedException(io.pravega.common.util.RetriesExhaustedException) List(java.util.List) ThreadPooledTestSuite(io.pravega.test.common.ThreadPooledTestSuite) TestUtils(io.pravega.test.common.TestUtils) BadKeyVersionException(io.pravega.client.tables.BadKeyVersionException) IntStream(java.util.stream.IntStream) Segment(io.pravega.client.segment.impl.Segment) Setter(lombok.Setter) Getter(lombok.Getter) ConnectionFailedException(io.pravega.shared.protocol.netty.ConnectionFailedException) Reply(io.pravega.shared.protocol.netty.Reply) Exceptions(io.pravega.common.Exceptions) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) CompletableFuture(java.util.concurrent.CompletableFuture) Deque(java.util.Deque) Append(io.pravega.shared.protocol.netty.Append) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Iterators(com.google.common.collect.Iterators) ArrayList(java.util.ArrayList) BiPredicate(java.util.function.BiPredicate) Lists(com.google.common.collect.Lists) ByteBuf(io.netty.buffer.ByteBuf) Charsets(com.google.common.base.Charsets) LongStream(java.util.stream.LongStream) MockConnectionFactoryImpl(io.pravega.client.stream.mock.MockConnectionFactoryImpl) lombok.val(lombok.val) AsyncIterator(io.pravega.common.util.AsyncIterator) Test(org.junit.Test) AuthenticationException(io.pravega.auth.AuthenticationException) WireCommands(io.pravega.shared.protocol.netty.WireCommands) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) WireCommand(io.pravega.shared.protocol.netty.WireCommand) AbstractMap(java.util.AbstractMap) KeyValueTableClientConfiguration(io.pravega.client.tables.KeyValueTableClientConfiguration) ArrayDeque(java.util.ArrayDeque) Assert(org.junit.Assert) Collections(java.util.Collections) Function(java.util.function.Function) WireCommands(io.pravega.shared.protocol.netty.WireCommands) Test(org.junit.Test)

Aggregations

AuthenticationException (io.pravega.auth.AuthenticationException)19 ConnectionFailedException (io.pravega.shared.protocol.netty.ConnectionFailedException)12 WireCommands (io.pravega.shared.protocol.netty.WireCommands)10 CompletableFuture (java.util.concurrent.CompletableFuture)9 Test (org.junit.Test)7 TokenExpiredException (io.pravega.auth.TokenExpiredException)6 Cleanup (lombok.Cleanup)6 ConnectionClosedException (io.pravega.client.stream.impl.ConnectionClosedException)5 Reply (io.pravega.shared.protocol.netty.Reply)5 WrongHost (io.pravega.shared.protocol.netty.WireCommands.WrongHost)5 VisibleForTesting (com.google.common.annotations.VisibleForTesting)4 Segment (io.pravega.client.segment.impl.Segment)4 List (java.util.List)4 UUID (java.util.UUID)4 Unpooled (io.netty.buffer.Unpooled)3 TxnFailedException (io.pravega.client.stream.TxnFailedException)3 MockConnectionFactoryImpl (io.pravega.client.stream.mock.MockConnectionFactoryImpl)3 MockController (io.pravega.client.stream.mock.MockController)3 Exceptions (io.pravega.common.Exceptions)3 FailingReplyProcessor (io.pravega.shared.protocol.netty.FailingReplyProcessor)3