From b78068d7de91ff93a9312960d78efee8d498ac21 Mon Sep 17 00:00:00 2001 From: Michael Nitschinger Date: Tue, 5 May 2020 15:07:04 +0200 Subject: [PATCH] couchbase: wait until query engine knows about bucket before creating primary index The motivation for this change is that in slow environments, it could be that the query engine is not aware of the just created bucket yet and so the create primary index will take longer than the 10s read timeout for the subsequent http request. As a result, this is just another precaution to minimize the chances that in slow environments the create primary index call times out with a read timeout. --- .../couchbase/CouchbaseContainer.java | 20 +++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/modules/couchbase/src/main/java/org/testcontainers/couchbase/CouchbaseContainer.java b/modules/couchbase/src/main/java/org/testcontainers/couchbase/CouchbaseContainer.java index 14e5744bb6d..1f9cc592812 100644 --- a/modules/couchbase/src/main/java/org/testcontainers/couchbase/CouchbaseContainer.java +++ b/modules/couchbase/src/main/java/org/testcontainers/couchbase/CouchbaseContainer.java @@ -25,6 +25,7 @@ import okhttp3.Request; import okhttp3.RequestBody; import okhttp3.Response; +import org.rnorth.ducttape.unreliables.Unreliables; import org.testcontainers.containers.GenericContainer; import org.testcontainers.containers.wait.strategy.HttpWaitStrategy; import org.testcontainers.containers.wait.strategy.WaitAllStrategy; @@ -36,6 +37,7 @@ import java.util.List; import java.util.Optional; import java.util.Set; +import java.util.concurrent.TimeUnit; import java.util.stream.Collectors; /** @@ -353,6 +355,24 @@ private void createBuckets() { .forStatusCode(200) .waitUntilReady(this); + if (enabledServices.contains(CouchbaseService.QUERY)) { + // If the query service is enabled, make sure that we only proceed if the query engine also + // knows about the bucket in its metadata configuration. + Unreliables.retryUntilTrue(1, TimeUnit.MINUTES, () -> { + Response queryResponse = doHttpRequest(QUERY_PORT, "/query/service", "POST", new FormBody.Builder() + .add("statement", "SELECT COUNT(*) > 0 as present FROM system:keyspaces WHERE name = \"" + bucket.getName() + "\"") + .build(), true); + + String body = queryResponse.body() != null ? queryResponse.body().string() : null; + checkSuccessfulResponse(queryResponse, "Could not poll query service state for bucket: " + bucket.getName()); + + return Optional.of(MAPPER.readTree(body)) + .map(n -> n.at("/results/0/present")) + .map(JsonNode::asBoolean) + .orElse(false); + }); + } + if (bucket.hasPrimaryIndex()) { if (enabledServices.contains(CouchbaseService.QUERY)) { Response queryResponse = doHttpRequest(QUERY_PORT, "/query/service", "POST", new FormBody.Builder()