Search in sources :

Example 1 with GetCollectionIdRequest

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

the class DefaultConfigurationProvider method refreshCollectionId.

@Override
public synchronized void refreshCollectionId(final CollectionIdentifier identifier) {
    if (collectionRefreshInProgress(identifier)) {
        eventBus.publish(new CollectionMapRefreshIgnoredEvent(core.context(), identifier));
        return;
    }
    collectionMapRefreshInProgress.add(identifier);
    long start = System.nanoTime();
    GetCollectionIdRequest request = new GetCollectionIdRequest(core.context().environment().timeoutConfig().kvTimeout(), core.context(), BestEffortRetryStrategy.INSTANCE, identifier);
    core.send(request);
    request.response().whenComplete((response, throwable) -> {
        try {
            final Duration duration = Duration.ofNanos(System.nanoTime() - start);
            if (throwable != null) {
                eventBus.publish(new CollectionMapRefreshFailedEvent(duration, core.context(), identifier, throwable, CollectionMapRefreshFailedEvent.Reason.FAILED));
                return;
            }
            if (response.status().success()) {
                if (response.collectionId().isPresent()) {
                    long cid = response.collectionId().get();
                    collectionMap.put(identifier, UnsignedLEB128.encode(cid));
                    eventBus.publish(new CollectionMapRefreshSucceededEvent(duration, core.context(), identifier, cid));
                } else {
                    eventBus.publish(new CollectionMapRefreshFailedEvent(duration, core.context(), identifier, null, CollectionMapRefreshFailedEvent.Reason.COLLECTION_ID_NOT_PRESENT));
                }
            } else {
                Throwable cause = null;
                CollectionMapRefreshFailedEvent.Reason reason;
                if (response.status() == ResponseStatus.UNKNOWN || response.status() == ResponseStatus.NO_COLLECTIONS_MANIFEST) {
                    reason = CollectionMapRefreshFailedEvent.Reason.NOT_SUPPORTED;
                } else if (response.status() == ResponseStatus.UNKNOWN_COLLECTION) {
                    reason = CollectionMapRefreshFailedEvent.Reason.UNKNOWN_COLLECTION;
                } else if (response.status() == ResponseStatus.UNKNOWN_SCOPE) {
                    reason = CollectionMapRefreshFailedEvent.Reason.UNKNOWN_SCOPE;
                } else if (response.status() == ResponseStatus.INVALID_REQUEST) {
                    reason = CollectionMapRefreshFailedEvent.Reason.INVALID_REQUEST;
                } else {
                    cause = new CouchbaseException(response.toString());
                    reason = CollectionMapRefreshFailedEvent.Reason.UNKNOWN;
                }
                eventBus.publish(new CollectionMapRefreshFailedEvent(duration, core.context(), identifier, cause, reason));
            }
        } finally {
            collectionMapRefreshInProgress.remove(identifier);
        }
    });
}
Also used : CouchbaseException(com.couchbase.client.core.error.CouchbaseException) CollectionMapRefreshSucceededEvent(com.couchbase.client.core.cnc.events.config.CollectionMapRefreshSucceededEvent) CollectionMapRefreshFailedEvent(com.couchbase.client.core.cnc.events.config.CollectionMapRefreshFailedEvent) Duration(java.time.Duration) GetCollectionIdRequest(com.couchbase.client.core.msg.kv.GetCollectionIdRequest) CollectionMapRefreshIgnoredEvent(com.couchbase.client.core.cnc.events.config.CollectionMapRefreshIgnoredEvent)

Example 2 with GetCollectionIdRequest

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

the class DefaultConfigurationProviderTest method ignoresMultipleCollectionIdRefreshAttempts.

/**
 * It is allowed to have multiple attempts in-flight at the same time, but not for the same collection identifier
 * (since this would just spam the cluster unnecessarily).
 */
@Test
void ignoresMultipleCollectionIdRefreshAttempts() {
    Core core = mock(Core.class);
    CoreContext ctx = new CoreContext(core, 1, ENVIRONMENT, mock(Authenticator.class));
    when(core.context()).thenReturn(ctx);
    Set<SeedNode> seedNodes = new HashSet<>(Collections.singletonList(SeedNode.create("127.0.0.1")));
    List<GetCollectionIdRequest> capturedRequests = new ArrayList<>();
    doAnswer(invocation -> {
        capturedRequests.add(invocation.getArgument(0));
        return null;
    }).when(core).send(any(GetCollectionIdRequest.class));
    DefaultConfigurationProvider provider = new DefaultConfigurationProvider(core, seedNodes);
    assertFalse(provider.collectionRefreshInProgress());
    CollectionIdentifier identifier1 = new CollectionIdentifier("bucket", Optional.of("scope"), Optional.of("collection"));
    CollectionIdentifier identifier2 = new CollectionIdentifier("bucket", Optional.of("_default"), Optional.of("_default"));
    provider.refreshCollectionId(identifier1);
    assertEquals(1, provider.collectionMapRefreshInProgress.size());
    assertTrue(provider.collectionMapRefreshInProgress.contains(identifier1));
    provider.refreshCollectionId(identifier2);
    assertEquals(2, provider.collectionMapRefreshInProgress.size());
    assertTrue(provider.collectionMapRefreshInProgress.contains(identifier2));
    provider.refreshCollectionId(identifier2);
    assertEquals(2, provider.collectionMapRefreshInProgress.size());
    assertTrue(provider.collectionMapRefreshInProgress.contains(identifier2));
    boolean found = false;
    for (Event event : EVENT_BUS.publishedEvents()) {
        if (event instanceof CollectionMapRefreshIgnoredEvent) {
            assertEquals(((CollectionMapRefreshIgnoredEvent) event).collectionIdentifier(), identifier2);
            found = true;
        }
    }
    assertTrue(found);
    capturedRequests.get(0).succeed(new GetCollectionIdResponse(ResponseStatus.SUCCESS, Optional.of(1234L)));
    assertTrue(provider.collectionRefreshInProgress());
    capturedRequests.get(1).cancel(CancellationReason.TIMEOUT);
    waitUntilCondition(() -> !provider.collectionRefreshInProgress());
    found = false;
    for (Event event : EVENT_BUS.publishedEvents()) {
        if (event instanceof CollectionMapRefreshFailedEvent) {
            assertEquals(((CollectionMapRefreshFailedEvent) event).collectionIdentifier(), identifier2);
            found = true;
        }
    }
    assertTrue(found);
}
Also used : GetCollectionIdResponse(com.couchbase.client.core.msg.kv.GetCollectionIdResponse) CoreContext(com.couchbase.client.core.CoreContext) SeedNode(com.couchbase.client.core.env.SeedNode) ArrayList(java.util.ArrayList) CollectionMapRefreshIgnoredEvent(com.couchbase.client.core.cnc.events.config.CollectionMapRefreshIgnoredEvent) CollectionMapRefreshFailedEvent(com.couchbase.client.core.cnc.events.config.CollectionMapRefreshFailedEvent) CollectionMapRefreshIgnoredEvent(com.couchbase.client.core.cnc.events.config.CollectionMapRefreshIgnoredEvent) CollectionMapRefreshFailedEvent(com.couchbase.client.core.cnc.events.config.CollectionMapRefreshFailedEvent) Event(com.couchbase.client.core.cnc.Event) GetCollectionIdRequest(com.couchbase.client.core.msg.kv.GetCollectionIdRequest) CollectionIdentifier(com.couchbase.client.core.io.CollectionIdentifier) PasswordAuthenticator(com.couchbase.client.core.env.PasswordAuthenticator) Authenticator(com.couchbase.client.core.env.Authenticator) Core(com.couchbase.client.core.Core) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

CollectionMapRefreshFailedEvent (com.couchbase.client.core.cnc.events.config.CollectionMapRefreshFailedEvent)2 CollectionMapRefreshIgnoredEvent (com.couchbase.client.core.cnc.events.config.CollectionMapRefreshIgnoredEvent)2 GetCollectionIdRequest (com.couchbase.client.core.msg.kv.GetCollectionIdRequest)2 Core (com.couchbase.client.core.Core)1 CoreContext (com.couchbase.client.core.CoreContext)1 Event (com.couchbase.client.core.cnc.Event)1 CollectionMapRefreshSucceededEvent (com.couchbase.client.core.cnc.events.config.CollectionMapRefreshSucceededEvent)1 Authenticator (com.couchbase.client.core.env.Authenticator)1 PasswordAuthenticator (com.couchbase.client.core.env.PasswordAuthenticator)1 SeedNode (com.couchbase.client.core.env.SeedNode)1 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)1 CollectionIdentifier (com.couchbase.client.core.io.CollectionIdentifier)1 GetCollectionIdResponse (com.couchbase.client.core.msg.kv.GetCollectionIdResponse)1 Duration (java.time.Duration)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 Test (org.junit.jupiter.api.Test)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1