Skip to content

Commit dbdbc03

Browse files
authored
Fix Collection Support for Cache for non-Collection Server. (#1418)
Also fix test setup index flakiness on older servers. Closes #1417.
1 parent 313fa04 commit dbdbc03

File tree

2 files changed

+45
-10
lines changed

2 files changed

+45
-10
lines changed

src/main/java/org/springframework/data/couchbase/cache/DefaultCouchbaseCacheWriter.java

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.data.couchbase.cache;
1818

19+
import static com.couchbase.client.core.io.CollectionIdentifier.DEFAULT_COLLECTION;
20+
import static com.couchbase.client.core.io.CollectionIdentifier.DEFAULT_SCOPE;
1921
import static com.couchbase.client.java.kv.GetOptions.*;
2022
import static com.couchbase.client.java.kv.InsertOptions.*;
2123
import static com.couchbase.client.java.kv.UpsertOptions.*;
@@ -28,7 +30,6 @@
2830

2931
import com.couchbase.client.core.error.DocumentExistsException;
3032
import com.couchbase.client.core.error.DocumentNotFoundException;
31-
import com.couchbase.client.core.io.CollectionIdentifier;
3233
import com.couchbase.client.java.Collection;
3334
import com.couchbase.client.java.Scope;
3435
import com.couchbase.client.java.codec.Transcoder;
@@ -104,20 +105,35 @@ public boolean remove(final String collectionName, final String key) {
104105

105106
@Override
106107
public long clear(final String collectionName, final String pattern) {
107-
QueryResult result = clientFactory.getScope().query(
108-
"DELETE FROM `" + getCollection(collectionName).name() + "` where meta().id LIKE $pattern",
109-
queryOptions().scanConsistency(REQUEST_PLUS).metrics(true).parameters(JsonObject.create().put("pattern", pattern + "%")));
108+
QueryResult result;
109+
if (getScope() == null
110+
|| (DEFAULT_SCOPE.equals(getScope().name()) && DEFAULT_COLLECTION.equals(getCollection(collectionName).name()))) {
111+
result = clientFactory.getCluster().query(
112+
"DELETE FROM `" + clientFactory.getBucket().name() + "` where meta().id LIKE $pattern",
113+
queryOptions().scanConsistency(REQUEST_PLUS).metrics(true)
114+
.parameters(JsonObject.create().put("pattern", pattern + "%")));
115+
} else {
116+
result = clientFactory.getScope().query(
117+
"DELETE FROM `" + getCollection(collectionName).name() + "` where meta().id LIKE $pattern",
118+
queryOptions().scanConsistency(REQUEST_PLUS).metrics(true)
119+
.parameters(JsonObject.create().put("pattern", pattern + "%")));
120+
}
110121
return result.metaData().metrics().map(QueryMetrics::mutationCount).orElse(0L);
111122
}
112123

113124
private Collection getCollection(final String collectionName) {
114125
final Scope scope = clientFactory.getScope();
115126
if (collectionName == null) {
116-
if (!scope.name().equals(CollectionIdentifier.DEFAULT_SCOPE)) {
127+
if (!scope.name().equals(DEFAULT_SCOPE)) {
117128
throw new IllegalStateException("A collectionName must be provided if a non-default scope is used!");
118129
}
119130
return clientFactory.getBucket().defaultCollection();
120131
}
121132
return scope.collection(collectionName);
122133
}
134+
135+
private Scope getScope() {
136+
return clientFactory.getScope();
137+
}
138+
123139
}

src/test/java/org/springframework/data/couchbase/util/JavaIntegrationTests.java

Lines changed: 24 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import static org.springframework.data.couchbase.config.BeanNames.REACTIVE_COUCHBASE_TEMPLATE;
2828
import static org.springframework.data.couchbase.util.Util.waitUntilCondition;
2929

30+
import com.couchbase.client.core.retry.BestEffortRetryStrategy;
31+
import com.couchbase.client.core.retry.RetryStrategy;
3032
import okhttp3.Credentials;
3133
import okhttp3.FormBody;
3234
import okhttp3.OkHttpClient;
@@ -161,10 +163,27 @@ public static void setupScopeCollection(Cluster cluster, String scopeName, Strin
161163

162164
// the call to createPrimaryIndex takes about 60 seconds
163165

164-
try {
165-
block(createPrimaryIndex(cluster, config().bucketname(), scopeName, collectionName));
166-
} catch (Exception e) {
167-
e.printStackTrace();
166+
// sometimes fails with :
167+
// com.couchbase.client.core.error.IndexFailureException: The server reported an issue with the underlying index
168+
// {"completed":true,"coreId":"0xbbef22aa00000003","errors":[{"code":12003,"message":"Keyspace not found in CB
169+
// datastore: default:cfc84bb8-ab0e-433e-a1af-812d51fa8855.my_scope.my_collection2","retry":false}],
170+
// "httpStatus":500,"idempotent":false,"lastDispatchedFrom":"127.0.0.1:49908","lastDispatchedTo":"127.0.0.1:8093",
171+
// "requestId":58,"requestType":"QueryRequest","retried":0,"service":
172+
// {"operationId":"04b28225-2b0f-4d2c-943b-330ac637ecd8","statement":"CREATE PRIMARY INDEX ON
173+
// default:`cfc84bb8-ab0e-433e-a1af-812d51fa8855`.`my_scope`.`my_collection2`","type":"query"},
174+
// "timeoutMs":300000,"timings":{"dispatchMicros":746,"totalDispatchMicros":746,"totalMicros":1636}}
175+
176+
for (int i = 0; i < 10; i++) {
177+
try {
178+
sleepMs(100);
179+
block(createPrimaryIndex(cluster, config().bucketname(), scopeName, collectionName));
180+
break;
181+
} catch (Exception e) {
182+
System.err.println(e);
183+
if (i > 5) {
184+
e.printStackTrace();
185+
}
186+
}
168187
}
169188

170189
waitUntilCondition(
@@ -201,7 +220,7 @@ protected static void waitForQueryIndexerToHaveBucket(final Cluster cluster, fin
201220
private static void createAndDeleteBucket() {
202221
final OkHttpClient httpClient = new OkHttpClient.Builder().connectTimeout(30, TimeUnit.SECONDS)
203222
.readTimeout(30, TimeUnit.SECONDS).writeTimeout(30, TimeUnit.SECONDS).build();
204-
String hostPort = connectionString().replace("11210", "8091").replace("11207", "18091");
223+
String hostPort = connectionString().split("=")[0].replace("11210", "8091").replace("11207", "18091");
205224
String protocol = hostPort.equals("18091") ? "https" : "http";
206225
String bucketname = UUID.randomUUID().toString();
207226
try {

0 commit comments

Comments
 (0)