Search in sources :

Example 1 with CarrierBucketConfigRequest

use of com.couchbase.client.core.msg.kv.CarrierBucketConfigRequest in project couchbase-jvm-clients by couchbase.

the class KeyValueBucketLoaderTest method errorsIfNonSuccessful.

@Test
void errorsIfNonSuccessful() {
    CarrierBucketConfigResponse response = mock(CarrierBucketConfigResponse.class);
    when(response.status()).thenReturn(ResponseStatus.UNKNOWN);
    doAnswer(i -> {
        ((CarrierBucketConfigRequest) i.getArgument(0)).succeed(response);
        return null;
    }).when(core).send(any(CarrierBucketConfigRequest.class));
    assertThrows(ConfigException.class, () -> loader.discoverConfig(SEED, BUCKET).block());
}
Also used : CarrierBucketConfigResponse(com.couchbase.client.core.msg.kv.CarrierBucketConfigResponse) CarrierBucketConfigRequest(com.couchbase.client.core.msg.kv.CarrierBucketConfigRequest) Test(org.junit.jupiter.api.Test)

Example 2 with CarrierBucketConfigRequest

use of com.couchbase.client.core.msg.kv.CarrierBucketConfigRequest in project couchbase-jvm-clients by couchbase.

the class KeyValueBucketLoaderTest method cancelRequestOnceUnsubscribed.

/**
 * Since the client may run many loaders in parallel, once a good config is found the other
 * attempts will be stopped.
 *
 * <p>This test makes sure that if an operation is ongoing but the downstream listener
 * unsubscribes, it gets cancelled so we are not performing any loader ops that are not needed
 * anymore.</p>
 */
@Test
void cancelRequestOnceUnsubscribed() {
    final AtomicReference<CarrierBucketConfigRequest> request = new AtomicReference<>();
    doAnswer(i -> {
        request.set(i.getArgument(0));
        return null;
    }).when(core).send(any(CarrierBucketConfigRequest.class));
    Disposable disposable = loader.discoverConfig(SEED, BUCKET).subscribe();
    disposable.dispose();
    assertTrue(request.get().completed());
    assertTrue(request.get().cancelled());
    assertEquals(CancellationReason.STOPPED_LISTENING, request.get().cancellationReason());
}
Also used : Disposable(reactor.core.Disposable) AtomicReference(java.util.concurrent.atomic.AtomicReference) CarrierBucketConfigRequest(com.couchbase.client.core.msg.kv.CarrierBucketConfigRequest) Test(org.junit.jupiter.api.Test)

Example 3 with CarrierBucketConfigRequest

use of com.couchbase.client.core.msg.kv.CarrierBucketConfigRequest in project couchbase-jvm-clients by couchbase.

the class KeyValueBucketLoaderTest method loadsConfigSuccessfully.

@Test
void loadsConfigSuccessfully() {
    byte[] expectedConfig = "config".getBytes(UTF_8);
    CarrierBucketConfigResponse response = mock(CarrierBucketConfigResponse.class);
    when(response.status()).thenReturn(ResponseStatus.SUCCESS);
    when(response.content()).thenReturn(expectedConfig);
    doAnswer(i -> {
        ((CarrierBucketConfigRequest) i.getArgument(0)).succeed(response);
        return null;
    }).when(core).send(any(CarrierBucketConfigRequest.class));
    byte[] config = loader.discoverConfig(SEED, BUCKET).block();
    assertArrayEquals(expectedConfig, config);
}
Also used : CarrierBucketConfigResponse(com.couchbase.client.core.msg.kv.CarrierBucketConfigResponse) CarrierBucketConfigRequest(com.couchbase.client.core.msg.kv.CarrierBucketConfigRequest) Test(org.junit.jupiter.api.Test)

Example 4 with CarrierBucketConfigRequest

use of com.couchbase.client.core.msg.kv.CarrierBucketConfigRequest in project couchbase-jvm-clients by couchbase.

the class KeyValueBucketRefresher method fetchConfigPerNode.

/**
 * Helper method to fetch a config per node provided.
 *
 * <p>Note that the bucket config request sent here has a fail fast strategy, so that if nodes are offline they
 * do not circle the system forever (given they have a specific node target). Since the refresher polls every
 * fixed interval anyways, fresh requests will flood the system eventually and there is no point in keeping
 * the old ones around.</p>
 *
 * <p>Also, the timeout is set to the poll interval since it does not make sense to keep them around any
 * longer.</p>
 *
 * @param name the bucket name.
 * @param nodes the flux of nodes that can be used to fetch a config.
 * @return returns configs for each node if found.
 */
private Flux<ProposedBucketConfigContext> fetchConfigPerNode(final String name, final Flux<NodeInfo> nodes) {
    return nodes.flatMap(nodeInfo -> {
        CoreContext ctx = core.context();
        CarrierBucketConfigRequest request = new CarrierBucketConfigRequest(configRequestTimeout, ctx, new CollectionIdentifier(name, Optional.empty(), Optional.empty()), FailFastRetryStrategy.INSTANCE, nodeInfo.identifier());
        core.send(request);
        return Reactor.wrap(request, request.response(), true).filter(response -> {
            if (!response.status().success()) {
                eventBus.publish(new BucketConfigRefreshFailedEvent(core.context(), BucketConfigRefreshFailedEvent.RefresherType.KV, BucketConfigRefreshFailedEvent.Reason.INDIVIDUAL_REQUEST_FAILED, Optional.of(response)));
            }
            return response.status().success();
        }).map(response -> new ProposedBucketConfigContext(name, new String(response.content(), UTF_8), nodeInfo.hostname())).onErrorResume(t -> {
            eventBus.publish(new BucketConfigRefreshFailedEvent(core.context(), BucketConfigRefreshFailedEvent.RefresherType.KV, BucketConfigRefreshFailedEvent.Reason.INDIVIDUAL_REQUEST_FAILED, Optional.of(t)));
            return Mono.empty();
        });
    });
}
Also used : Disposable(reactor.core.Disposable) ArrayList(java.util.ArrayList) EventBus(com.couchbase.client.core.cnc.EventBus) ServiceType(com.couchbase.client.core.service.ServiceType) CoreContext(com.couchbase.client.core.CoreContext) Duration(java.time.Duration) Map(java.util.Map) Stability(com.couchbase.client.core.annotation.Stability) ProposedBucketConfigContext(com.couchbase.client.core.config.ProposedBucketConfigContext) BucketConfig(com.couchbase.client.core.config.BucketConfig) ConfigurationProvider(com.couchbase.client.core.config.ConfigurationProvider) Reactor(com.couchbase.client.core.Reactor) UTF_8(java.nio.charset.StandardCharsets.UTF_8) NodeInfo(com.couchbase.client.core.config.NodeInfo) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) Mono(reactor.core.publisher.Mono) FailFastRetryStrategy(com.couchbase.client.core.retry.FailFastRetryStrategy) TimeUnit(java.util.concurrent.TimeUnit) Flux(reactor.core.publisher.Flux) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Optional(java.util.Optional) CollectionIdentifier(com.couchbase.client.core.io.CollectionIdentifier) BucketConfigRefreshFailedEvent(com.couchbase.client.core.cnc.events.config.BucketConfigRefreshFailedEvent) CarrierBucketConfigRequest(com.couchbase.client.core.msg.kv.CarrierBucketConfigRequest) Core(com.couchbase.client.core.Core) CoreContext(com.couchbase.client.core.CoreContext) ProposedBucketConfigContext(com.couchbase.client.core.config.ProposedBucketConfigContext) BucketConfigRefreshFailedEvent(com.couchbase.client.core.cnc.events.config.BucketConfigRefreshFailedEvent) CarrierBucketConfigRequest(com.couchbase.client.core.msg.kv.CarrierBucketConfigRequest) CollectionIdentifier(com.couchbase.client.core.io.CollectionIdentifier)

Example 5 with CarrierBucketConfigRequest

use of com.couchbase.client.core.msg.kv.CarrierBucketConfigRequest in project couchbase-jvm-clients by couchbase.

the class KeyValueBucketLoaderTest method errorsIfUnsupported.

@Test
void errorsIfUnsupported() {
    CarrierBucketConfigResponse response = mock(CarrierBucketConfigResponse.class);
    when(response.status()).thenReturn(ResponseStatus.UNSUPPORTED);
    doAnswer(i -> {
        ((CarrierBucketConfigRequest) i.getArgument(0)).succeed(response);
        return null;
    }).when(core).send(any(CarrierBucketConfigRequest.class));
    assertThrows(UnsupportedConfigMechanismException.class, () -> loader.discoverConfig(SEED, BUCKET).block());
}
Also used : CarrierBucketConfigResponse(com.couchbase.client.core.msg.kv.CarrierBucketConfigResponse) CarrierBucketConfigRequest(com.couchbase.client.core.msg.kv.CarrierBucketConfigRequest) Test(org.junit.jupiter.api.Test)

Aggregations

CarrierBucketConfigRequest (com.couchbase.client.core.msg.kv.CarrierBucketConfigRequest)5 Test (org.junit.jupiter.api.Test)4 CarrierBucketConfigResponse (com.couchbase.client.core.msg.kv.CarrierBucketConfigResponse)3 Disposable (reactor.core.Disposable)2 Core (com.couchbase.client.core.Core)1 CoreContext (com.couchbase.client.core.CoreContext)1 Reactor (com.couchbase.client.core.Reactor)1 Stability (com.couchbase.client.core.annotation.Stability)1 EventBus (com.couchbase.client.core.cnc.EventBus)1 BucketConfigRefreshFailedEvent (com.couchbase.client.core.cnc.events.config.BucketConfigRefreshFailedEvent)1 BucketConfig (com.couchbase.client.core.config.BucketConfig)1 ConfigurationProvider (com.couchbase.client.core.config.ConfigurationProvider)1 NodeInfo (com.couchbase.client.core.config.NodeInfo)1 ProposedBucketConfigContext (com.couchbase.client.core.config.ProposedBucketConfigContext)1 CollectionIdentifier (com.couchbase.client.core.io.CollectionIdentifier)1 FailFastRetryStrategy (com.couchbase.client.core.retry.FailFastRetryStrategy)1 ServiceType (com.couchbase.client.core.service.ServiceType)1 UTF_8 (java.nio.charset.StandardCharsets.UTF_8)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1