Skip to content

Commit

Permalink
🐛 ignore "Build Already In Progress" N1QL error
Browse files Browse the repository at this point in the history
  • Loading branch information
tchlyah committed Sep 18, 2020
1 parent 9081f8f commit 89014d9
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,8 @@
import com.couchbase.client.core.deps.com.fasterxml.jackson.databind.JsonNode;
import com.couchbase.client.core.deps.com.fasterxml.jackson.databind.ObjectMapper;
import com.couchbase.client.core.deps.com.fasterxml.jackson.databind.node.ObjectNode;
import com.couchbase.client.core.error.CouchbaseException;
import com.couchbase.client.core.error.DocumentNotFoundException;
import com.couchbase.client.core.error.IndexNotFoundException;
import com.couchbase.client.core.error.*;
import com.couchbase.client.core.error.context.QueryErrorContext;
import com.couchbase.client.core.json.Mapper;
import com.couchbase.client.core.retry.BestEffortRetryStrategy;
import com.couchbase.client.java.Bucket;
Expand Down Expand Up @@ -35,6 +34,7 @@
import org.slf4j.Logger;

import java.util.Map;
import java.util.stream.Stream;

import static com.couchbase.client.java.kv.InsertOptions.insertOptions;
import static com.couchbase.client.java.kv.ReplaceOptions.replaceOptions;
Expand Down Expand Up @@ -147,7 +147,24 @@ public void query(String n1qlStatement) {
try {
cluster.query(parametrizedStatement, withRetry(QueryOptions.queryOptions().scanConsistency(QueryScanConsistency.REQUEST_PLUS)));
} catch (Exception e) {
throw new CouchmoveException("Unable to execute n1ql request", e);
//noinspection SimplifyStreamApiCallChains
boolean ignore = Stream.of(e)
.filter(InternalServerFailureException.class::isInstance)
.map(InternalServerFailureException.class::cast)
.map(CouchbaseException::context)
.filter(QueryErrorContext.class::isInstance)
.map(QueryErrorContext.class::cast)
.map(QueryErrorContext::errors)
.flatMap(java.util.Collection::stream)
.map(ErrorCodeAndMessage::message)
.filter(m -> m.contains("Build Already In Progress"))
.findAny()
.isPresent();
if (ignore) {
logger.warn("Ignoring error while executing N1QL request", e);
} else {
throw new CouchmoveException("Unable to execute n1ql request", e);
}
}
}

Expand Down
40 changes: 40 additions & 0 deletions src/test/java/com/github/couchmove/CouchmoveIT.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,46 @@ public void should_update_changeLog() {
assertLike(changeLogRepository.findOne(PREFIX_ID + "1"), "1", 1, "create index", N1QL, "V1__create_index.n1ql", "69eb9007c910c2b9cac46044a54de5e933b768ae874c6408356372576ab88dbd", EXECUTED);
}

@Test
public void should_build_multiple_index_not_fail() {
// Given a Couchmove instance configured for success migration folder
Couchmove couchmove = getCouchmove("multiple-deferred-indexes");

// When we launch migration
couchmove.migrate();

// Then all changeLogs should be inserted in DB
List<ChangeLog> changeLogs = Stream.of("0", "1")
.map(version -> PREFIX_ID + version)
.map(changeLogRepository::findOne)
.collect(Collectors.toList());

assertEquals(2, changeLogs.size());
assertLike(changeLogs.get(0),
"0", 1, "create deferred index", N1QL, "V0__create_deferred_index.n1ql",
"060f486279932b3838a90f23032a135ad20f8a364fbbda9305f6e20a5b065085",
EXECUTED);
assertLike(changeLogs.get(1),
"1", 2, "create second deferred index", N1QL, "V1__create_second_deferred_index.n1ql",
"49fed597ee5f7012b6ab7eb66825e20de4906ecfb10ee9b5ae8f74dfe242b74a",
EXECUTED);

// And successfully executed

// Index inserted
Optional<QueryIndex> userIndexInfo = getCluster().queryIndexes().getAllIndexes(getBucket().name()).stream()
.filter(i -> i.name().equals("buyer_index"))
.findFirst();
assertTrue(userIndexInfo.isPresent());
assertEquals("`username`", userIndexInfo.get().indexKey().get(0));

userIndexInfo = getCluster().queryIndexes().getAllIndexes(getBucket().name()).stream()
.filter(i -> i.name().equals("merchant_index"))
.findFirst();
assertTrue(userIndexInfo.isPresent());
assertEquals("`username`", userIndexInfo.get().indexKey().get(0));
}

private static void assertLike(ChangeLog changeLog, String version, Integer order, String description, Type type, String script, String checksum, Status status) {
assertNotNull("ChangeLog", changeLog);
assertEquals("version", version, changeLog.getVersion());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- create Index
CREATE INDEX buyer_index ON `${bucket}`(username)
WHERE type = 'buyer'
WITH { "defer_build" : true };

BUILD INDEX ON `${bucket}`(buyer_index);
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
-- create Index
CREATE INDEX merchant_index ON `${bucket}`(username)
WHERE type = 'merchant'
WITH { "defer_build" : true };

BUILD INDEX ON `${bucket}`(merchant_index);

0 comments on commit 89014d9

Please sign in to comment.