Elffuss BraidHub

kikocis/java-driver public

JAVA-1283: Don't cache failed query preparations in the mapper (#740)

Motivation:

The object mapper prepares its queries asynchronously and caches the
resulting ListenableFuture instances. If a PREPARE call fails because of
a transient issue (e.g. all nodes are down), a failed ListenableFuture
will be cached.  Even after the issue gets resolved, the mapper will
rely on the failed future and keep throwing the same exception.

Modifications:

If preparation fails, do not keep the future in the cache.

Result:

The mapper will retry preparing the next time it's called, giving it a
chance to succeed if the issue was resolved.
Changeset167c5c83cf3b9eba710cdad4c353a495e0baca9b2e382f3cc136c01e955e2fa7
AuthorAlexandre Dutra <adutra@users.noreply.github.com> · 2016-09-16 00:42 +02:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2016-09-15 15:42 -07:00
Parents9fa4ddba0db6

Browse the files at this changeset

3 files changed

modified changelog/README.md +1 −0
@@ -9,6 +9,7 @@
99 - [improvement] JAVA-1262: Use ParseUtils for quoting & unquoting.
1010 - [improvement] JAVA-1275: Use Netty's default thread factory
1111 - [bug] JAVA-1285: QueryBuilder routing key auto-discovery should handle case-sensitive column names.
12+- [bug] JAVA-1283: Don't cache failed query preparations in the mapper.
1213
1314
1415 ### 3.0.3
modified driver-mapping/src/main/java/com/datastax/driver/mapping/Mapper.java +4 −1
@@ -114,7 +114,7 @@
114114 if (old != null) {
115115 return old;
116116 } else {
117- String queryString = type.makePreparedQueryString(tableMetadata, mapper, manager, columns, options.values());
117+ final String queryString = type.makePreparedQueryString(tableMetadata, mapper, manager, columns, options.values());
118118 logger.debug("Preparing query {}", queryString);
119119 SimpleStatement s = new SimpleStatement(queryString);
120120 // all queries generated by the mapper are idempotent
@@ -128,6 +128,9 @@
128128 @Override
129129 public void onFailure(Throwable t) {
130130 future.setException(t);
131+ // do not keep a failed future in the query cache
132+ preparedQueries.remove(pqk, future);
133+ logger.error("Query preparation failed: " + queryString, t);
131134 }
132135 });
133136 return future;
added driver-mapping/src/test/java/com/datastax/driver/mapping/MapperReconnectionTest.java +80 −0
@@ -0,0 +1,80 @@
1+/*
2+ * Copyright (C) 2012-2015 DataStax Inc.
3+ *
4+ * Licensed under the Apache License, Version 2.0 (the "License");
5+ * you may not use this file except in compliance with the License.
6+ * You may obtain a copy of the License at
7+ *
8+ * http://www.apache.org/licenses/LICENSE-2.0
9+ *
10+ * Unless required by applicable law or agreed to in writing, software
11+ * distributed under the License is distributed on an "AS IS" BASIS,
12+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+ * See the License for the specific language governing permissions and
14+ * limitations under the License.
15+ */
16+package com.datastax.driver.mapping;
17+
18+import com.datastax.driver.core.CCMConfig;
19+import com.datastax.driver.core.CCMTestsSupport;
20+import com.datastax.driver.core.Cluster;
21+import com.datastax.driver.core.CreateCCM;
22+import com.datastax.driver.core.exceptions.NoHostAvailableException;
23+import com.datastax.driver.core.policies.ConstantReconnectionPolicy;
24+import org.testng.annotations.Test;
25+
26+import static com.datastax.driver.core.CreateCCM.TestMode.PER_METHOD;
27+import static com.datastax.driver.core.TestUtils.*;
28+import static com.datastax.driver.mapping.MapperTest.User;
29+import static org.testng.Assert.fail;
30+
31+@CreateCCM(PER_METHOD)
32+@CCMConfig(dirtiesContext = true)
33+public class MapperReconnectionTest extends CCMTestsSupport {
34+
35+ @Override
36+ public Cluster.Builder createClusterBuilder() {
37+ return super.createClusterBuilder().withReconnectionPolicy(new ConstantReconnectionPolicy(1000));
38+ }
39+
40+ @Override
41+ public void onTestContextInitialized() {
42+ execute("CREATE TABLE users (user_id uuid PRIMARY KEY, name text, email text, year int, gender text)");
43+ }
44+
45+ /**
46+ * Ensures that when the driver looses connectivity,
47+ * if a mapper query preparation is attempted in the meanwhile,
48+ * the failed future will not be kept in cache, so that when
49+ * connectivity comes back again,
50+ * the same query preparation can be reattempted.
51+ *
52+ * @jira_ticket JAVA-1283
53+ * @test_category object_mapper
54+ */
55+ @Test(groups = "long")
56+ public void should_not_keep_failed_future_in_query_cache() throws Exception {
57+ MappingManager manager = new MappingManager(session());
58+ Mapper<User> m = manager.mapper(User.class);
59+ User u1 = new User("Paul", "paul@gmail.com");
60+ m.save(u1);
61+ ccm().stop(1);
62+ ccm().waitForDown(1);
63+ waitForDown(ipOfNode(1), cluster());
64+ try {
65+ m.get(u1.getUserId());
66+ fail("Should have thrown NoHostAvailableException");
67+ } catch (NoHostAvailableException e) {
68+ // ok
69+ }
70+ ccm().start(1);
71+ ccm().waitForUp(1);
72+ waitForUp(ipOfNode(1), cluster());
73+ try {
74+ m.get(u1.getUserId());
75+ } catch (NoHostAvailableException e) {
76+ fail("Should not have thrown NoHostAvailableException");
77+ }
78+ }
79+
80+}