Skip to content

Commit

Permalink
Revert "couchbase: wait until query engine knows about bucket before …
Browse files Browse the repository at this point in the history
…creating… (testcontainers#2662)"

This reverts commit 835ac71.
  • Loading branch information
sd-yip committed May 11, 2021
1 parent 4b34db5 commit da63eac
Showing 1 changed file with 15 additions and 30 deletions.
Expand Up @@ -19,14 +19,12 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.dockerjava.api.command.InspectContainerResponse;
import com.github.dockerjava.api.model.ContainerNetwork;
import lombok.Cleanup;
import okhttp3.Credentials;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
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 @@ -39,7 +37,6 @@
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 @@ -240,7 +237,7 @@ private void waitUntilNodeIsOnline() {
private void renameNode() {
logger().debug("Renaming Couchbase Node from localhost to {}", getHost());

@Cleanup Response response = doHttpRequest(MGMT_PORT, "/node/controller/rename", "POST", new FormBody.Builder()
Response response = doHttpRequest(MGMT_PORT, "/node/controller/rename", "POST", new FormBody.Builder()
.add("hostname", getInternalIpAddress())
.build(), false
);
Expand All @@ -264,7 +261,7 @@ private void initializeServices() {
}
}).collect(Collectors.joining(","));

@Cleanup Response response = doHttpRequest(MGMT_PORT, "/node/controller/setupServices", "POST", new FormBody.Builder()
Response response = doHttpRequest(MGMT_PORT, "/node/controller/setupServices", "POST", new FormBody.Builder()
.add("services", services)
.build(), false
);
Expand All @@ -280,7 +277,7 @@ private void initializeServices() {
private void configureAdminUser() {
logger().debug("Configuring couchbase admin user with username: \"{}\"", username);

@Cleanup Response response = doHttpRequest(MGMT_PORT, "/settings/web", "POST", new FormBody.Builder()
Response response = doHttpRequest(MGMT_PORT, "/settings/web", "POST", new FormBody.Builder()
.add("username", username)
.add("password", password)
.add("port", Integer.toString(MGMT_PORT))
Expand Down Expand Up @@ -321,7 +318,7 @@ private void configureExternalPorts() {
builder.add("ftsSSL", Integer.toString(getMappedPort(SEARCH_SSL_PORT)));
}

@Cleanup Response response = doHttpRequest(
final Response response = doHttpRequest(
MGMT_PORT,
"/node/controller/setupAlternateAddresses/external",
"PUT",
Expand All @@ -338,7 +335,7 @@ private void configureExternalPorts() {
private void configureIndexer() {
logger().debug("Configuring the indexer service");

@Cleanup Response response = doHttpRequest(MGMT_PORT, "/settings/indexes", "POST", new FormBody.Builder()
Response response = doHttpRequest(MGMT_PORT, "/settings/indexes", "POST", new FormBody.Builder()
.add("storageMode", "memory_optimized")
.build(), true
);
Expand All @@ -355,7 +352,7 @@ private void createBuckets() {
for (BucketDefinition bucket : buckets) {
logger().debug("Creating bucket \"" + bucket.getName() + "\"");

@Cleanup Response response = doHttpRequest(MGMT_PORT, "/pools/default/buckets", "POST", new FormBody.Builder()
Response response = doHttpRequest(MGMT_PORT, "/pools/default/buckets", "POST", new FormBody.Builder()
.add("name", bucket.getName())
.add("ramQuotaMB", Integer.toString(bucket.getQuota()))
.build(), true);
Expand All @@ -369,27 +366,9 @@ 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, () -> {
@Cleanup 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)) {
@Cleanup Response queryResponse = doHttpRequest(QUERY_PORT, "/query/service", "POST", new FormBody.Builder()
Response queryResponse = doHttpRequest(QUERY_PORT, "/query/service", "POST", new FormBody.Builder()
.add("statement", "CREATE PRIMARY INDEX on `" + bucket.getName() + "`")
.build(), true);

Expand Down Expand Up @@ -418,8 +397,14 @@ private String getInternalIpAddress() {
* @param message the message that should be part of the exception of not successful.
*/
private void checkSuccessfulResponse(final Response response, final String message) {
if (!response.isSuccessful()) {
throw new IllegalStateException(message + ": " + response.toString());
try {
if (!response.isSuccessful()) {
throw new IllegalStateException(message + ": " + response.toString());
}
} finally {
if (response.body() != null) {
response.body().close();
}
}
}

Expand Down

0 comments on commit da63eac

Please sign in to comment.