Search in sources :

Example 6 with Controller

use of io.pravega.client.control.impl.Controller in project pravega by pravega.

the class JwtTokenProviderImplTest method testRetrievesNewTokenIfSignalledOfTokenExpiry.

@Test
public void testRetrievesNewTokenIfSignalledOfTokenExpiry() {
    final String token = String.format("newtokenheader.%s.signature", toCompact(JwtBody.builder().expirationTime(Instant.now().plusSeconds(100000).getEpochSecond()).build()));
    // Setup mock
    Controller mockController = mock(Controller.class);
    CompletableFuture<String> future = CompletableFuture.supplyAsync(new Supplier<String>() {

        @Override
        public String get() {
            return token + System.currentTimeMillis();
        }
    });
    when(mockController.getOrRefreshDelegationTokenFor("somescope", "somestream", AccessOperation.ANY)).thenReturn(future);
    // Setup the object under test
    DelegationTokenProvider objectUnderTest = new JwtTokenProviderImpl(mockController, "somescope", "somestream", AccessOperation.ANY);
    String firstToken = objectUnderTest.retrieveToken().join();
    String secondToken = objectUnderTest.retrieveToken().join();
    assertEquals(firstToken, secondToken);
    objectUnderTest.signalTokenExpired();
    assertEquals(firstToken, objectUnderTest.retrieveToken().join());
}
Also used : Controller(io.pravega.client.control.impl.Controller) Test(org.junit.Test)

Example 7 with Controller

use of io.pravega.client.control.impl.Controller in project pravega by pravega.

the class JwtTokenProviderImplTest method testRefreshTokenCompletesUponFailure.

@Test(expected = CompletionException.class)
public void testRefreshTokenCompletesUponFailure() {
    ClientConfig config = ClientConfig.builder().controllerURI(URI.create("tcp://non-existent-cluster:9090")).build();
    @Cleanup("shutdownNow") val executor = ExecutorServiceHelpers.newScheduledThreadPool(1, "test");
    @Cleanup Controller controllerClient = new ControllerImpl(ControllerImplConfig.builder().clientConfig(config).retryAttempts(1).build(), executor);
    DelegationTokenProvider tokenProvider = DelegationTokenProviderFactory.create(controllerClient, "bob-0", "bob-0", AccessOperation.ANY);
    try {
        tokenProvider.retrieveToken().join();
    } catch (CompletionException e) {
        assertEquals(RetriesExhaustedException.class.getName(), e.getCause().getClass().getName());
        throw e;
    }
}
Also used : lombok.val(lombok.val) CompletionException(java.util.concurrent.CompletionException) ControllerImpl(io.pravega.client.control.impl.ControllerImpl) ClientConfig(io.pravega.client.ClientConfig) Controller(io.pravega.client.control.impl.Controller) Cleanup(lombok.Cleanup) Test(org.junit.Test)

Example 8 with Controller

use of io.pravega.client.control.impl.Controller in project pravega by pravega.

the class JwtTokenProviderImplTest method testRetrievesSameTokenOutsideOfTokenRefreshThresholdWhenTokenIsNull.

@Test
public void testRetrievesSameTokenOutsideOfTokenRefreshThresholdWhenTokenIsNull() {
    final String token = String.format("newtokenheader.%s.signature", toCompact(JwtBody.builder().expirationTime(Instant.now().plusSeconds(10000).getEpochSecond()).build()));
    // Setup mock
    Controller mockController = mock(Controller.class);
    CompletableFuture<String> future = CompletableFuture.supplyAsync(new Supplier<String>() {

        @Override
        public String get() {
            return token;
        }
    });
    when(mockController.getOrRefreshDelegationTokenFor("somescope", "somestream", AccessOperation.ANY)).thenReturn(future);
    // Setup the object under test
    DelegationTokenProvider objectUnderTest = new JwtTokenProviderImpl(mockController, "somescope", "somestream", AccessOperation.ANY);
    assertEquals(token, objectUnderTest.retrieveToken().join());
}
Also used : Controller(io.pravega.client.control.impl.Controller) Test(org.junit.Test)

Example 9 with Controller

use of io.pravega.client.control.impl.Controller in project pravega by pravega.

the class KeyValueTableFactory method withScope.

/**
 * Creates a new instance of {@link KeyValueTableFactory}.
 *
 * @param scope  The Key-Value Table scope.
 * @param config Configuration for the client.
 * @return Instance of {@link KeyValueTableFactory} implementation.
 */
static KeyValueTableFactory withScope(String scope, ClientConfig config) {
    ConnectionFactory connectionFactory = new SocketConnectionFactoryImpl(config);
    ConnectionPool connectionPool = new ConnectionPoolImpl(config, connectionFactory);
    Controller controller = new ControllerImpl(ControllerImplConfig.builder().clientConfig(config).build(), connectionFactory.getInternalExecutor());
    return new KeyValueTableFactoryImpl(scope, controller, connectionPool);
}
Also used : ConnectionPool(io.pravega.client.connection.impl.ConnectionPool) ConnectionFactory(io.pravega.client.connection.impl.ConnectionFactory) KeyValueTableFactoryImpl(io.pravega.client.tables.impl.KeyValueTableFactoryImpl) ConnectionPoolImpl(io.pravega.client.connection.impl.ConnectionPoolImpl) ControllerImpl(io.pravega.client.control.impl.ControllerImpl) SocketConnectionFactoryImpl(io.pravega.client.connection.impl.SocketConnectionFactoryImpl) Controller(io.pravega.client.control.impl.Controller)

Example 10 with Controller

use of io.pravega.client.control.impl.Controller in project pravega by pravega.

the class ReaderGroupImpl method resetReaderGroup.

@Override
public void resetReaderGroup(ReaderGroupConfig config) {
    log.info("Reset ReaderGroup {} to {}", getGroupName(), config);
    synchronizer.fetchUpdates();
    while (true) {
        val currentConfig = synchronizer.getState().getConfig();
        // We only move into the block if the state transition has happened successfully.
        if (stateTransition(currentConfig, new UpdatingConfig(true))) {
            if (currentConfig.getReaderGroupId() == ReaderGroupConfig.DEFAULT_UUID && currentConfig.getGeneration() == ReaderGroupConfig.DEFAULT_GENERATION) {
                // Migration code path, for moving a ReaderGroup from version < 0.9 to 0.9+
                final ReaderGroupConfig updateConfig = ReaderGroupConfig.cloneConfig(config, UUID.randomUUID(), 0L);
                final long nextGen = Futures.getThrowingException(controller.createReaderGroup(scope, getGroupName(), updateConfig).thenCompose(conf -> {
                    if (!conf.getReaderGroupId().equals(updateConfig.getReaderGroupId())) {
                        return controller.updateReaderGroup(scope, groupName, ReaderGroupConfig.cloneConfig(updateConfig, conf.getReaderGroupId(), conf.getGeneration()));
                    } else {
                        // ReaderGroup IDs matched so our create was updated on Controller
                        return CompletableFuture.completedFuture(conf.getGeneration());
                    }
                }));
                updateConfigInStateSynchronizer(updateConfig, nextGen);
            } else {
                // normal code path
                // Use the latest generation and reader group Id.
                ReaderGroupConfig newConfig = ReaderGroupConfig.cloneConfig(config, currentConfig.getReaderGroupId(), currentConfig.getGeneration());
                long newGen = Futures.exceptionallyExpecting(controller.updateReaderGroup(scope, groupName, newConfig), e -> Exceptions.unwrap(e) instanceof ReaderGroupConfigRejectedException, -1L).join();
                if (newGen == -1) {
                    log.debug("Synchronize reader group with the one present on controller.");
                    synchronizeReaderGroupConfig();
                    continue;
                }
                updateConfigInStateSynchronizer(newConfig, newGen);
            }
            return;
        }
    }
}
Also used : lombok.val(lombok.val) ReaderGroupConfig(io.pravega.client.stream.ReaderGroupConfig) StreamCut(io.pravega.client.stream.StreamCut) ReaderGroupStateInit(io.pravega.client.stream.impl.ReaderGroupState.ReaderGroupStateInit) SneakyThrows(lombok.SneakyThrows) UpdatingConfig(io.pravega.client.stream.impl.ReaderGroupState.UpdatingConfig) ReaderGroup(io.pravega.client.stream.ReaderGroup) ReaderGroupMetrics(io.pravega.client.stream.ReaderGroupMetrics) EndOfDataNotification(io.pravega.client.stream.notifications.EndOfDataNotification) Position(io.pravega.client.stream.Position) Stream(io.pravega.client.stream.Stream) AccessOperation(io.pravega.shared.security.auth.AccessOperation) Duration(java.time.Duration) Map(java.util.Map) Checkpoint(io.pravega.client.stream.Checkpoint) Futures.getThrowingException(io.pravega.common.concurrent.Futures.getThrowingException) ReaderGroupConfig(io.pravega.client.stream.ReaderGroupConfig) ImmutableMap(com.google.common.collect.ImmutableMap) Set(java.util.Set) DelegationTokenProviderFactory(io.pravega.client.security.auth.DelegationTokenProviderFactory) UUID(java.util.UUID) InitialUpdate(io.pravega.client.state.InitialUpdate) Collectors(java.util.stream.Collectors) SegmentMetadataClient(io.pravega.client.segment.impl.SegmentMetadataClient) NotifierFactory(io.pravega.client.stream.notifications.NotifierFactory) Base64(java.util.Base64) List(java.util.List) Slf4j(lombok.extern.slf4j.Slf4j) Observable(io.pravega.client.stream.notifications.Observable) Entry(java.util.Map.Entry) Optional(java.util.Optional) Controller(io.pravega.client.control.impl.Controller) Futures(io.pravega.common.concurrent.Futures) ClearCheckpointsBefore(io.pravega.client.stream.impl.ReaderGroupState.ClearCheckpointsBefore) StateSynchronizer(io.pravega.client.state.StateSynchronizer) Segment(io.pravega.client.segment.impl.Segment) NotificationSystem(io.pravega.client.stream.notifications.NotificationSystem) Exceptions(io.pravega.common.Exceptions) SegmentMetadataClientFactoryImpl(io.pravega.client.segment.impl.SegmentMetadataClientFactoryImpl) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) ReaderGroupConfigRejectedException(io.pravega.client.control.impl.ReaderGroupConfigRejectedException) SegmentMetadataClientFactory(io.pravega.client.segment.impl.SegmentMetadataClientFactory) ArrayList(java.util.ArrayList) Update(io.pravega.client.state.Update) SegmentNotification(io.pravega.client.stream.notifications.SegmentNotification) CreateCheckpoint(io.pravega.client.stream.impl.ReaderGroupState.CreateCheckpoint) ThreadLocalRandom(java.util.concurrent.ThreadLocalRandom) ScheduledExecutorService(java.util.concurrent.ScheduledExecutorService) SynchronizerConfig(io.pravega.client.state.SynchronizerConfig) Serializer(io.pravega.client.stream.Serializer) NameUtils(io.pravega.shared.NameUtils) Futures.getAndHandleExceptions(io.pravega.common.concurrent.Futures.getAndHandleExceptions) ConnectionPool(io.pravega.client.connection.impl.ConnectionPool) lombok.val(lombok.val) Preconditions.checkNotNull(com.google.common.base.Preconditions.checkNotNull) Futures.allOfWithResults(io.pravega.common.concurrent.Futures.allOfWithResults) DelegationTokenProvider(io.pravega.client.security.auth.DelegationTokenProvider) SynchronizerClientFactory(io.pravega.client.SynchronizerClientFactory) ReaderSegmentDistribution(io.pravega.client.stream.ReaderSegmentDistribution) Data(lombok.Data) InvalidStreamException(io.pravega.client.stream.InvalidStreamException) Preconditions(com.google.common.base.Preconditions) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) ReaderGroupConfigRejectedException(io.pravega.client.control.impl.ReaderGroupConfigRejectedException) UpdatingConfig(io.pravega.client.stream.impl.ReaderGroupState.UpdatingConfig)

Aggregations

Controller (io.pravega.client.control.impl.Controller)120 Test (org.junit.Test)95 Cleanup (lombok.Cleanup)81 Segment (io.pravega.client.segment.impl.Segment)53 EventWriterConfig (io.pravega.client.stream.EventWriterConfig)50 StreamConfiguration (io.pravega.client.stream.StreamConfiguration)47 Stream (io.pravega.client.stream.Stream)37 CompletableFuture (java.util.concurrent.CompletableFuture)35 SegmentOutputStreamFactory (io.pravega.client.segment.impl.SegmentOutputStreamFactory)34 SocketConnectionFactoryImpl (io.pravega.client.connection.impl.SocketConnectionFactoryImpl)33 HashMap (java.util.HashMap)29 ClientConfig (io.pravega.client.ClientConfig)28 ClientFactoryImpl (io.pravega.client.stream.impl.ClientFactoryImpl)28 StreamImpl (io.pravega.client.stream.impl.StreamImpl)25 ReaderGroupManager (io.pravega.client.admin.ReaderGroupManager)24 Slf4j (lombok.extern.slf4j.Slf4j)24 Before (org.junit.Before)23 ConnectionFactory (io.pravega.client.connection.impl.ConnectionFactory)22 ConnectionPoolImpl (io.pravega.client.connection.impl.ConnectionPoolImpl)21 ScalingPolicy (io.pravega.client.stream.ScalingPolicy)21