Search in sources :

Example 1 with RateLimitedException

use of com.couchbase.client.core.error.RateLimitedException in project couchbase-jvm-clients by couchbase.

the class QueryChunkResponseParser method errorsToThrowable.

static CouchbaseException errorsToThrowable(final byte[] bytes, HttpResponse header, RequestContext ctx) {
    int httpStatus = header != null ? header.status().code() : 0;
    final List<ErrorCodeAndMessage> errors = bytes.length == 0 ? Collections.emptyList() : ErrorCodeAndMessage.fromJsonArray(bytes);
    QueryErrorContext errorContext = new QueryErrorContext(ctx, errors, httpStatus);
    if (errors.size() >= 1) {
        ErrorCodeAndMessage codeAndMessage = errors.get(0);
        int code = codeAndMessage.code();
        String message = codeAndMessage.message();
        int reasonCode = codeAndMessage.reason() != null ? (int) codeAndMessage.reason().getOrDefault("code", 0) : 0;
        if (code == 3000) {
            return new ParsingFailureException(errorContext);
        } else if (PREPARED_ERROR_CODES.contains(code)) {
            return new PreparedStatementFailureException(errorContext, RETRYABLE_PREPARED_ERROR_CODES.contains(code));
        } else if (code == 4300 && message.matches("^.*index .*already exist.*")) {
            return new IndexExistsException(errorContext);
        } else if (code >= 4000 && code < 5000) {
            return new PlanningFailureException(errorContext);
        } else if (code == 12004 || code == 12016 || (code == 5000 && message.matches("^.*index .+ not found.*"))) {
            return new IndexNotFoundException(errorContext);
        } else if (code == 5000 && message.matches("^.*Index .*already exist.*")) {
            return new IndexExistsException(errorContext);
        } else if (code == 5000 && message.contains("limit for number of indexes that can be created per scope has been reached")) {
            return new QuotaLimitedException(errorContext);
        } else if (code >= 5000 && code < 6000) {
            return new InternalServerFailureException(errorContext);
        } else if (code == 12009) {
            if (message.contains("CAS mismatch") || reasonCode == 12033) {
                return new CasMismatchException(errorContext);
            } else if (reasonCode == 17014) {
                return new DocumentNotFoundException(errorContext);
            } else if (reasonCode == 17012) {
                return new DocumentExistsException(errorContext);
            } else {
                return new DmlFailureException(errorContext);
            }
        } else if ((code >= 10000 && code < 11000) || code == 13014) {
            return new AuthenticationFailureException("Could not authenticate query", errorContext, null);
        } else if ((code >= 12000 && code < 13000) || (code >= 14000 && code < 15000)) {
            return new IndexFailureException(errorContext);
        } else if (code == 1065) {
            if (message.contains("query_context")) {
                return FeatureNotAvailableException.scopeLevelQuery(ServiceType.QUERY);
            }
            if (message.contains("preserve_expiry")) {
                return FeatureNotAvailableException.queryPreserveExpiry();
            }
        } else if (code == 1080) {
            // engine will proactively send us a timeout and we need to convert it.
            return new UnambiguousTimeoutException("Query timed out while streaming/receiving rows", new CancellationErrorContext(errorContext));
        } else if (code == 1191 || code == 1192 || code == 1193 || code == 1194) {
            return new RateLimitedException(errorContext);
        } else if (code == 3230) {
            String feature = null;
            if (message.contains("Advisor") || message.contains("Advise")) {
                feature = "Query Index Advisor";
            } else if (message.contains("Window")) {
                feature = "Query Window Functions";
            }
            return FeatureNotAvailableException.communityEdition(feature);
        }
    }
    return new CouchbaseException("Unknown query error", errorContext);
}
Also used : IndexExistsException(com.couchbase.client.core.error.IndexExistsException) DocumentNotFoundException(com.couchbase.client.core.error.DocumentNotFoundException) DocumentExistsException(com.couchbase.client.core.error.DocumentExistsException) CasMismatchException(com.couchbase.client.core.error.CasMismatchException) PlanningFailureException(com.couchbase.client.core.error.PlanningFailureException) UnambiguousTimeoutException(com.couchbase.client.core.error.UnambiguousTimeoutException) AuthenticationFailureException(com.couchbase.client.core.error.AuthenticationFailureException) CouchbaseException(com.couchbase.client.core.error.CouchbaseException) QueryErrorContext(com.couchbase.client.core.error.context.QueryErrorContext) QuotaLimitedException(com.couchbase.client.core.error.QuotaLimitedException) ErrorCodeAndMessage(com.couchbase.client.core.error.ErrorCodeAndMessage) ParsingFailureException(com.couchbase.client.core.error.ParsingFailureException) PreparedStatementFailureException(com.couchbase.client.core.error.PreparedStatementFailureException) IndexNotFoundException(com.couchbase.client.core.error.IndexNotFoundException) DmlFailureException(com.couchbase.client.core.error.DmlFailureException) IndexFailureException(com.couchbase.client.core.error.IndexFailureException) CancellationErrorContext(com.couchbase.client.core.error.context.CancellationErrorContext) RateLimitedException(com.couchbase.client.core.error.RateLimitedException) InternalServerFailureException(com.couchbase.client.core.error.InternalServerFailureException)

Example 2 with RateLimitedException

use of com.couchbase.client.core.error.RateLimitedException in project couchbase-jvm-clients by couchbase.

the class SearchChunkResponseParser method errorsToThrowable.

private CouchbaseException errorsToThrowable(final byte[] bytes) {
    int statusCode = responseHeader().status().code();
    String errorDecoded = bytes == null || bytes.length == 0 ? "" : new String(bytes, CharsetUtil.UTF_8);
    SearchErrorContext errorContext = new SearchErrorContext(HttpProtocol.decodeStatus(responseHeader().status()), requestContext(), statusCode, errorDecoded);
    if (statusCode == 400 && errorDecoded.contains("index not found")) {
        return new IndexNotFoundException(errorContext);
    } else if (statusCode == 500) {
        return new InternalServerFailureException(errorContext);
    } else if (statusCode == 401 || statusCode == 403) {
        return new AuthenticationFailureException("Could not authenticate search query", errorContext, null);
    } else if (statusCode == 400 && errorDecoded.contains("num_fts_indexes")) {
        return new QuotaLimitedException(errorContext);
    } else if (statusCode == 429) {
        if (errorDecoded.contains("num_concurrent_requests") || errorDecoded.contains("num_queries_per_min") || errorDecoded.contains("ingress_mib_per_min") || errorDecoded.contains("egress_mib_per_min")) {
            return new RateLimitedException(errorContext);
        }
    }
    return new CouchbaseException("Unknown search error: " + errorDecoded, errorContext);
}
Also used : CouchbaseException(com.couchbase.client.core.error.CouchbaseException) QuotaLimitedException(com.couchbase.client.core.error.QuotaLimitedException) SearchErrorContext(com.couchbase.client.core.error.context.SearchErrorContext) IndexNotFoundException(com.couchbase.client.core.error.IndexNotFoundException) AuthenticationFailureException(com.couchbase.client.core.error.AuthenticationFailureException) RateLimitedException(com.couchbase.client.core.error.RateLimitedException) InternalServerFailureException(com.couchbase.client.core.error.InternalServerFailureException)

Example 3 with RateLimitedException

use of com.couchbase.client.core.error.RateLimitedException in project couchbase-jvm-clients by couchbase.

the class RateLimitingIntegrationTest method kvRateLimitIngress.

@Test
void kvRateLimitIngress() throws Exception {
    String username = "kvRateLimitIngress";
    Limits limits = new Limits();
    limits.keyValueLimits = new KeyValueLimits(10, 100, 1, 10);
    createRateLimitedUser(username, limits);
    Cluster cluster = createTestCluster(username);
    try {
        Bucket bucket = cluster.bucket(config().bucketname());
        Collection collection = bucket.defaultCollection();
        bucket.waitUntilReady(Duration.ofSeconds(10));
        collection.upsert("ratelimitingress", randomString(1024 * 512), UpsertOptions.upsertOptions().timeout(Duration.ofSeconds(5)));
        RateLimitedException ex = assertThrows(RateLimitedException.class, () -> collection.upsert("ratelimitingress", randomString(1024 * 512), UpsertOptions.upsertOptions().timeout(Duration.ofSeconds(5))));
        assertTrue(ex.getMessage().contains("RATE_LIMITED_NETWORK_INGRESS"));
    } finally {
        cluster.disconnect();
        adminCluster.users().dropUser(username);
    }
}
Also used : RateLimitedException(com.couchbase.client.core.error.RateLimitedException) JavaIntegrationTest(com.couchbase.client.java.util.JavaIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 4 with RateLimitedException

use of com.couchbase.client.core.error.RateLimitedException in project couchbase-jvm-clients by couchbase.

the class RateLimitingIntegrationTest method queryRateLimitIngress.

@Test
@IgnoreWhen(missesCapabilities = Capabilities.QUERY)
void queryRateLimitIngress() throws Exception {
    String username = "queryRateLimitIngress";
    Limits limits = new Limits();
    limits.queryLimits = new QueryLimits(10000, 10000, 1, 10);
    createRateLimitedUser(username, limits);
    Cluster cluster = createTestCluster(username);
    try {
        cluster.waitUntilReady(Duration.ofSeconds(10));
        RateLimitedException ex = assertThrows(RateLimitedException.class, () -> {
            for (int i = 0; i < 5; i++) {
                String content = repeatString(1024 * 1024 * 5, "a");
                cluster.query("UPSERT INTO `" + config().bucketname() + "` (KEY,VALUE) VALUES (\"key1\", \"" + content + "\")", // Can take a while
                QueryOptions.queryOptions().timeout(Duration.ofSeconds(30)));
            }
        });
        assertTrue(ex.getMessage().contains("User has exceeded input network traffic limit"));
    } finally {
        cluster.disconnect();
        adminCluster.users().dropUser(username);
    }
}
Also used : RateLimitedException(com.couchbase.client.core.error.RateLimitedException) IgnoreWhen(com.couchbase.client.test.IgnoreWhen) JavaIntegrationTest(com.couchbase.client.java.util.JavaIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 5 with RateLimitedException

use of com.couchbase.client.core.error.RateLimitedException in project couchbase-jvm-clients by couchbase.

the class RateLimitingIntegrationTest method kvRateLimitMaxCommands.

@Test
void kvRateLimitMaxCommands() throws Exception {
    String username = "kvRateLimit";
    Limits limits = new Limits();
    limits.keyValueLimits = new KeyValueLimits(10, 10, 10, 10);
    createRateLimitedUser(username, limits);
    Cluster cluster = createTestCluster(username);
    try {
        Bucket bucket = cluster.bucket(config().bucketname());
        Collection collection = bucket.defaultCollection();
        bucket.waitUntilReady(Duration.ofSeconds(10));
        RateLimitedException ex = assertThrows(RateLimitedException.class, () -> {
            for (int i = 0; i < 30; i++) {
                collection.upsert("ratelimit", "test");
            }
        });
        assertTrue(ex.getMessage().contains("RATE_LIMITED_MAX_COMMANDS"));
    } finally {
        cluster.disconnect();
        adminCluster.users().dropUser(username);
    }
}
Also used : RateLimitedException(com.couchbase.client.core.error.RateLimitedException) JavaIntegrationTest(com.couchbase.client.java.util.JavaIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

RateLimitedException (com.couchbase.client.core.error.RateLimitedException)14 JavaIntegrationTest (com.couchbase.client.java.util.JavaIntegrationTest)12 Test (org.junit.jupiter.api.Test)12 IgnoreWhen (com.couchbase.client.test.IgnoreWhen)8 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)5 UnambiguousTimeoutException (com.couchbase.client.core.error.UnambiguousTimeoutException)4 SearchIndex (com.couchbase.client.java.manager.search.SearchIndex)4 TimeoutException (com.couchbase.client.core.error.TimeoutException)3 AuthenticationFailureException (com.couchbase.client.core.error.AuthenticationFailureException)2 IndexNotFoundException (com.couchbase.client.core.error.IndexNotFoundException)2 InternalServerFailureException (com.couchbase.client.core.error.InternalServerFailureException)2 QuotaLimitedException (com.couchbase.client.core.error.QuotaLimitedException)2 CasMismatchException (com.couchbase.client.core.error.CasMismatchException)1 DmlFailureException (com.couchbase.client.core.error.DmlFailureException)1 DocumentExistsException (com.couchbase.client.core.error.DocumentExistsException)1 DocumentNotFoundException (com.couchbase.client.core.error.DocumentNotFoundException)1 ErrorCodeAndMessage (com.couchbase.client.core.error.ErrorCodeAndMessage)1 IndexExistsException (com.couchbase.client.core.error.IndexExistsException)1 IndexFailureException (com.couchbase.client.core.error.IndexFailureException)1 ParsingFailureException (com.couchbase.client.core.error.ParsingFailureException)1