Java Code Examples for com.couchbase.client.core.message.ResponseStatus#RETRY

The following examples show how to use com.couchbase.client.core.message.ResponseStatus#RETRY . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractGenericHandler.java    From couchbase-jvm-core with Apache License 2.0 6 votes vote down vote up
/**
 * Publishes a response with the attached observable.
 *
 * @param response the response to publish.
 * @param observable pushing into the event sink.
 */
protected void publishResponse(final CouchbaseResponse response,
    final Subject<CouchbaseResponse, CouchbaseResponse> observable) {
    if (response.status() != ResponseStatus.RETRY && observable != null) {
        if (moveResponseOut) {
            Scheduler scheduler = env().scheduler();
            if (scheduler instanceof CoreScheduler) {
                scheduleDirect((CoreScheduler) scheduler, response, observable);
            } else {
                scheduleWorker(scheduler, response, observable);
            }
        } else {
            completeResponse(response, observable);
        }
    } else {
        responseBuffer.publishEvent(ResponseHandler.RESPONSE_TRANSLATOR, response, observable);
    }
}
 
Example 2
Source File: ResponseHandler.java    From couchbase-jvm-core with Apache License 2.0 5 votes vote down vote up
/**
 * Handles {@link ResponseEvent}s that come into the response RingBuffer.
 *
 * Hey I just mapped you,
 * And this is crazy,
 * But here's my data
 * so subscribe me maybe.
 *
 * It's hard to block right,
 * at you baby,
 * But here's my data ,
 * so subscribe me maybe.
 */
@Override
public void onEvent(final ResponseEvent event, long sequence, boolean endOfBatch) throws Exception {
    try {
        CouchbaseMessage message = event.getMessage();
        if (message instanceof SignalConfigReload) {
            configurationProvider.signalOutdated();
        } else if (message instanceof CouchbaseResponse) {
            final CouchbaseResponse response = (CouchbaseResponse) message;
            ResponseStatus status = response.status();
            if (status == ResponseStatus.RETRY) {
                retry(event, true);
            } else {
                final Scheduler.Worker worker = environment.scheduler().createWorker();
                final Subject<CouchbaseResponse, CouchbaseResponse> obs = event.getObservable();
                worker.schedule(new Action0() {
                    @Override
                    public void call() {
                        try {
                            obs.onNext(response);
                            obs.onCompleted();
                        } catch(Exception ex) {
                            obs.onError(ex);
                        } finally {
                            worker.unsubscribe();
                        }
                    }
                });
            }
        } else if (message instanceof CouchbaseRequest) {
            retry(event, false);
        } else {
            throw new IllegalStateException("Got message type I do not understand: " + message);
        }
    } finally {
       event.setMessage(null);
       event.setObservable(null);
    }
}
 
Example 3
Source File: ResponseHandlerTest.java    From couchbase-jvm-core with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDispatchFirstNMVImmediately() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    ClusterFacade clusterMock = mock(ClusterFacade.class);
    when(clusterMock.send(any(CouchbaseRequest.class))).then(new Answer<Object>() {
        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            latch.countDown();
            return null;
        }
    });

    ConfigurationProvider providerMock = mock(ConfigurationProvider.class);
    ClusterConfig clusterConfig = mock(ClusterConfig.class);
    BucketConfig bucketConfig = mock(BucketConfig.class);
    when(providerMock.config()).thenReturn(clusterConfig);
    when(clusterConfig.bucketConfig("bucket")).thenReturn(bucketConfig);
    when(bucketConfig.hasFastForwardMap()).thenReturn(true);

    ResponseHandler handler = new ResponseHandler(ENVIRONMENT, clusterMock, providerMock);

    GetRequest request = new GetRequest("key", "bucket");
    GetResponse response = new GetResponse(ResponseStatus.RETRY, (short) 0, 0L ,0, "bucket", Unpooled.EMPTY_BUFFER,
            request);

    ResponseEvent retryEvent = new ResponseEvent();
    retryEvent.setMessage(response);
    retryEvent.setObservable(request.observable());

    handler.onEvent(retryEvent, 1, true);

    long start = System.nanoTime();
    latch.await(5, TimeUnit.SECONDS);
    long end = System.nanoTime();

    // assert immediate dispatch
    assertTrue(TimeUnit.NANOSECONDS.toMillis(end - start) < 10);
}
 
Example 4
Source File: ResponseHandlerTest.java    From couchbase-jvm-core with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDispatchFirstNMVBWithDelayIfNoFFMap() throws Exception {

    final CountDownLatch latch = new CountDownLatch(1);
    ClusterFacade clusterMock = mock(ClusterFacade.class);
    when(clusterMock.send(any(CouchbaseRequest.class))).then(new Answer<Object>() {
        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            latch.countDown();
            return null;
        }
    });

    ConfigurationProvider providerMock = mock(ConfigurationProvider.class);
    ClusterConfig clusterConfig = mock(ClusterConfig.class);
    BucketConfig bucketConfig = mock(BucketConfig.class);
    when(providerMock.config()).thenReturn(clusterConfig);
    when(clusterConfig.bucketConfig("bucket")).thenReturn(bucketConfig);
    when(bucketConfig.hasFastForwardMap()).thenReturn(false);

    ResponseHandler handler = new ResponseHandler(ENVIRONMENT, clusterMock, providerMock);

    GetRequest request = new GetRequest("key", "bucket");
    GetResponse response = new GetResponse(ResponseStatus.RETRY, (short) 0, 0L ,0, "bucket", Unpooled.EMPTY_BUFFER,
            request);

    ResponseEvent retryEvent = new ResponseEvent();
    retryEvent.setMessage(response);
    retryEvent.setObservable(request.observable());

    handler.onEvent(retryEvent, 1, true);

    long start = System.nanoTime();
    latch.await(5, TimeUnit.SECONDS);
    long end = System.nanoTime();

    // assert delayed dispatch
    assertTrue(TimeUnit.NANOSECONDS.toMillis(end - start) >= 100);
}
 
Example 5
Source File: ResponseHandlerTest.java    From couchbase-jvm-core with Apache License 2.0 5 votes vote down vote up
@Test
public void shouldDispatchSecondNMVWithDelay() throws Exception {
    final CountDownLatch latch = new CountDownLatch(1);
    ClusterFacade clusterMock = mock(ClusterFacade.class);
    when(clusterMock.send(any(CouchbaseRequest.class))).then(new Answer<Object>() {
        @Override
        public Object answer(InvocationOnMock invocationOnMock) throws Throwable {
            latch.countDown();
            return null;
        }
    });

    ConfigurationProvider providerMock = mock(ConfigurationProvider.class);
    ClusterConfig clusterConfig = mock(ClusterConfig.class);
    BucketConfig bucketConfig = mock(BucketConfig.class);
    when(providerMock.config()).thenReturn(clusterConfig);
    when(clusterConfig.bucketConfig("bucket")).thenReturn(bucketConfig);
    when(bucketConfig.hasFastForwardMap()).thenReturn(true);

    ResponseHandler handler = new ResponseHandler(ENVIRONMENT, clusterMock, providerMock);

    GetRequest request = new GetRequest("key", "bucket");
    request.incrementRetryCount(); // pretend its at least once retried!
    GetResponse response = new GetResponse(ResponseStatus.RETRY, (short) 0, 0L ,0, "bucket", Unpooled.EMPTY_BUFFER,
        request);

    ResponseEvent retryEvent = new ResponseEvent();
    retryEvent.setMessage(response);
    retryEvent.setObservable(request.observable());

    handler.onEvent(retryEvent, 1, true);

    long start = System.nanoTime();
    latch.await(5, TimeUnit.SECONDS);
    long end = System.nanoTime();

    // assert delayed dispatch
    assertTrue(TimeUnit.NANOSECONDS.toMillis(end - start) >= 100);
}
 
Example 6
Source File: ResponseStatusConverter.java    From couchbase-jvm-core with Apache License 2.0 4 votes vote down vote up
/**
 * Convert the binary protocol status in a typesafe enum that can be acted upon later.
 *
 * @param code the status to convert.
 * @return the converted response status.
 */
public static ResponseStatus fromBinary(final short code) {
    KeyValueStatus status = KeyValueStatus.valueOf(code);
    switch (status) {
        case SUCCESS:
            return ResponseStatus.SUCCESS;
        case ERR_EXISTS:
            return ResponseStatus.EXISTS;
        case ERR_NOT_FOUND:
            return ResponseStatus.NOT_EXISTS;
        case ERR_NOT_MY_VBUCKET:
            return ResponseStatus.RETRY;
        case ERR_NOT_STORED:
            return ResponseStatus.NOT_STORED;
        case ERR_TOO_BIG:
            return ResponseStatus.TOO_BIG;
        case ERR_TEMP_FAIL:
            return ResponseStatus.TEMPORARY_FAILURE;
        case ERR_BUSY:
            return ResponseStatus.SERVER_BUSY;
        case ERR_NO_MEM:
            return ResponseStatus.OUT_OF_MEMORY;
        case ERR_UNKNOWN_COMMAND:
            return ResponseStatus.COMMAND_UNAVAILABLE;
        case ERR_NOT_SUPPORTED:
            return ResponseStatus.COMMAND_UNAVAILABLE;
        case ERR_ACCESS:
            return ResponseStatus.ACCESS_ERROR;
        case ERR_INTERNAL:
            return ResponseStatus.INTERNAL_ERROR;
        case ERR_INVALID:
            return ResponseStatus.INVALID_ARGUMENTS;
        case ERR_DELTA_BADVAL:
            return ResponseStatus.INVALID_ARGUMENTS;
        case ERR_RANGE:
            return ResponseStatus.RANGE_ERROR;
        case ERR_ROLLBACK:
            return ResponseStatus.ROLLBACK;
        //== the following codes are for subdocument API ==
        case ERR_SUBDOC_PATH_NOT_FOUND:
            return ResponseStatus.SUBDOC_PATH_NOT_FOUND;
        case ERR_SUBDOC_PATH_MISMATCH:
            return ResponseStatus.SUBDOC_PATH_MISMATCH;
        case ERR_SUBDOC_PATH_INVALID:
            return ResponseStatus.SUBDOC_PATH_INVALID;
        case ERR_SUBDOC_PATH_TOO_BIG:
            return ResponseStatus.SUBDOC_PATH_TOO_BIG;
        case ERR_SUBDOC_DOC_TOO_DEEP:
            return ResponseStatus.SUBDOC_DOC_TOO_DEEP;
        case ERR_SUBDOC_VALUE_CANTINSERT:
            return ResponseStatus.SUBDOC_VALUE_CANTINSERT;
        case ERR_SUBDOC_DOC_NOT_JSON:
            return ResponseStatus.SUBDOC_DOC_NOT_JSON;
        case ERR_SUBDOC_NUM_RANGE:
            return ResponseStatus.SUBDOC_NUM_RANGE;
        case ERR_SUBDOC_DELTA_RANGE:
            return ResponseStatus.SUBDOC_DELTA_RANGE;
        case ERR_SUBDOC_PATH_EXISTS:
            return ResponseStatus.SUBDOC_PATH_EXISTS;
        case ERR_SUBDOC_VALUE_TOO_DEEP:
            return ResponseStatus.SUBDOC_VALUE_TOO_DEEP;
        case ERR_SUBDOC_INVALID_COMBO:
            return ResponseStatus.SUBDOC_INVALID_COMBO;
        case ERR_SUBDOC_MULTI_PATH_FAILURE:
            return ResponseStatus.SUBDOC_MULTI_PATH_FAILURE;
        case ERR_SUBDOC_XATTR_INVALID_FLAG_COMBO:
            return ResponseStatus.INTERNAL_ERROR;
        case ERR_SUBDOC_XATTR_UNKNOWN_MACRO:
            return ResponseStatus.SUBDOC_XATTR_UNKNOWN_MACRO;
        case ERR_SUBDOC_XATTR_INVALID_KEY_COMBO:
            return ResponseStatus.SUBDOC_XATTR_INVALID_KEY_COMBO;
        //== end of subdocument API codes ==
    }
    return ResponseStatus.FAILURE;
}