Skip to content

Commit

Permalink
couchbase: wait until query engine knows about bucket before creating…
Browse files Browse the repository at this point in the history
… 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.
  • Loading branch information
daschl committed May 5, 2020
1 parent 0050e70 commit b78068d
Showing 1 changed file with 20 additions and 0 deletions.
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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()
Expand Down

0 comments on commit b78068d

Please sign in to comment.