Elffuss BraidHub

kikocis/java-driver public

Add tests for query debouncing (JAVA-657).
Changeset75b0d9b26d32059572914796a9fbf4c843a7143c7dff19a42019a1ad5b25f638
AuthorAndrew Tolbert <andrew.tolbert@datastax.com> · 2015-08-11 17:25 -05:00
Committerolim7t <omichallat+github@gmail.com> · 2015-08-14 18:57 +02:00
Parentse742462cc4ae

Browse the files at this changeset

4 files changed

added driver-core/src/test/java/com/datastax/driver/core/NodeListRefreshDebouncerTest.java +121 −0
@@ -0,0 +1,121 @@
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.core;
17+
18+import java.util.Collection;
19+
20+import org.testng.annotations.*;
21+
22+import static com.google.common.collect.Lists.newArrayList;
23+import static org.mockito.Mockito.reset;
24+import static org.mockito.Mockito.spy;
25+import static org.mockito.Mockito.timeout;
26+import static org.mockito.Mockito.times;
27+import static org.mockito.Mockito.verify;
28+
29+import static com.datastax.driver.core.TestUtils.CREATE_KEYSPACE_SIMPLE_FORMAT;
30+
31+public class NodeListRefreshDebouncerTest extends CCMBridge.PerClassSingleNodeCluster {
32+
33+ private static final int DEBOUNCE_TIME = 2000;
34+
35+ QueryOptions queryOptions;
36+
37+ Cluster cluster2;
38+
39+ Session session2;
40+
41+ // Control Connection to be spied.
42+ private ControlConnection controlConnection;
43+
44+ // Keyspaces to drop after test completes.
45+ private Collection<String> keyspaces = newArrayList();
46+
47+ @Override
48+ protected Collection<String> getTableDefinitions() {
49+ return newArrayList();
50+ }
51+
52+ @BeforeClass(groups = "short")
53+ public void setup() {
54+ queryOptions = new QueryOptions();
55+ queryOptions.setRefreshNodeListIntervalMillis(DEBOUNCE_TIME);
56+ queryOptions.setMaxPendingRefreshNodeListRequests(5);
57+ // Create a separate cluster that will receive the schema events on its control connection.
58+ cluster2 = this.configure(Cluster.builder())
59+ .addContactPointsWithPorts(newArrayList(hostAddress))
60+ .withQueryOptions(queryOptions)
61+ .build();
62+ session2 = cluster2.connect();
63+
64+ // Create a spy of the Cluster's control connection and replace it with the spy.
65+ controlConnection = spy(cluster2.manager.controlConnection);
66+ cluster2.manager.controlConnection = controlConnection;
67+ reset(controlConnection);
68+ }
69+
70+ @AfterMethod(groups = "short")
71+ public void beforeMethod() {
72+ reset(controlConnection);
73+ }
74+
75+ @AfterClass(groups = "short")
76+ public void teardown() {
77+ cluster2.close();
78+ // drop all keyspaces
79+ for(String keyspace : keyspaces) {
80+ session.execute("DROP KEYSPACE " + keyspace);
81+ }
82+ }
83+
84+ /**
85+ * Ensures that when a keyspace is created that refresh node list request
86+ * is debounced and processed within {@link QueryOptions#getRefreshSchemaIntervalMillis()}
87+ * + {@link QueryOptions#getRefreshNodeListIntervalMillis()}.
88+ *
89+ * @jira_ticket JAVA-657
90+ * @since 2.0.11
91+ */
92+ @Test(groups = "short")
93+ public void should_debounce_refresh_when_keyspace_created() {
94+ String keyspace = "sdrwkc";
95+ session.execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, keyspace, 1));
96+ keyspaces.add(keyspace);
97+
98+ verify(controlConnection, timeout(DEBOUNCE_TIME + queryOptions.getRefreshSchemaIntervalMillis())).refreshNodeListAndTokenMap();
99+ }
100+
101+ /**
102+ * Ensures that when enough refreshNodeList requests have been received
103+ * to reach {@link QueryOptions#getMaxPendingRefreshNodeListRequests()} that a
104+ * node list refresh is submitted right away.
105+ *
106+ * @jira_ticket JAVA-657
107+ * @since 2.0.11
108+ */
109+ @Test(groups = "short")
110+ public void should_refresh_when_max_pending_requests_reached() {
111+ // Enable and disable schema management 5 times to cause
112+ // refreshNodeListAndTokenMap to be called.
113+ for(int i = 0; i < 5; i++) {
114+ queryOptions.setMetadataEnabled(false);
115+ queryOptions.setMetadataEnabled(true);
116+ }
117+
118+ // add a 1 second delay to account for executor submit.
119+ verify(controlConnection, timeout(1000).times(1)).refreshNodeListAndTokenMap();
120+ }
121+}
added driver-core/src/test/java/com/datastax/driver/core/NodeRefreshDebouncerTest.java +81 −0
@@ -0,0 +1,81 @@
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.core;
17+
18+import java.util.Collection;
19+import java.util.concurrent.TimeUnit;
20+
21+import org.mockito.ArgumentCaptor;
22+import org.testng.annotations.Test;
23+
24+import static com.google.common.collect.Lists.newArrayList;
25+import static org.mockito.ArgumentCaptor.forClass;
26+import static org.mockito.Mockito.mock;
27+import static org.mockito.Mockito.timeout;
28+import static org.mockito.Mockito.verify;
29+
30+import static com.datastax.driver.core.Assertions.assertThat;
31+
32+public class NodeRefreshDebouncerTest extends CCMBridge.PerClassSingleNodeCluster {
33+
34+ @Override
35+ protected Collection<String> getTableDefinitions() {
36+ return newArrayList();
37+ }
38+
39+ /**
40+ * Ensures that when a new node is bootstrapped into the cluster, stopped, and then subsequently
41+ * started within {@link QueryOptions#setRefreshNodeIntervalMillis(int)} that an 'onAdd' event
42+ * event is the only one processed and that the {@link Host} is marked up.
43+ * <p>
44+ * Since NEW_NODE_DELAY_SECONDS is typically configured with a high value (60 seconds default
45+ * in the maven profile) this test can take a very long time.
46+ *
47+ * @jira_ticket JAVA-657
48+ * @since 2.0.11
49+ */
50+ @Test(groups="long")
51+ public void should_call_onAdd_with_bootstrap_stop_start() {
52+ int refreshNodeInterval = 10000;
53+ QueryOptions queryOptions = new QueryOptions().setRefreshNodeIntervalMillis(refreshNodeInterval);
54+ CCMBridge ccm = CCMBridge.builder("test").withNodes(1).build();
55+ Cluster cluster = Cluster.builder()
56+ .addContactPoint(CCMBridge.ipOfNode(1))
57+ .withQueryOptions(queryOptions)
58+ .build();
59+
60+ try {
61+ cluster.connect();
62+ Host.StateListener listener = mock(Host.StateListener.class);
63+ cluster.register(listener);
64+ ccm.bootstrapNode(2);
65+ ccm.stop(2);
66+ ccm.start(2);
67+
68+ ArgumentCaptor<Host> captor = forClass(Host.class);
69+
70+ // Only onAdd should be called, since stop and start should be discarded.
71+ verify(listener, timeout(refreshNodeInterval + TimeUnit.MILLISECONDS.convert(Cluster.NEW_NODE_DELAY_SECONDS, TimeUnit.SECONDS)).only()).onAdd(captor.capture());
72+
73+ // The hosts state should be UP.
74+ assertThat(captor.getValue().getState()).isEqualTo("UP");
75+ assertThat(cluster).host(2).hasState(Host.State.UP);
76+ } finally {
77+ cluster.close();
78+ ccm.remove();
79+ }
80+ }
81+}
added driver-core/src/test/java/com/datastax/driver/core/SchemaRefreshDebouncerTest.java +313 −0
@@ -0,0 +1,313 @@
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.core;
17+
18+import java.util.Collection;
19+
20+import org.mockito.ArgumentCaptor;
21+import org.testng.annotations.AfterClass;
22+import org.testng.annotations.AfterMethod;
23+import org.testng.annotations.BeforeClass;
24+import org.testng.annotations.Test;
25+
26+import static com.google.common.collect.Lists.newArrayList;
27+import static org.mockito.ArgumentCaptor.forClass;
28+import static org.mockito.Mockito.*;
29+
30+import static com.datastax.driver.core.Assertions.assertThat;
31+import static com.datastax.driver.core.TestUtils.CREATE_KEYSPACE_SIMPLE_FORMAT;
32+import static com.datastax.driver.core.TestUtils.CREATE_TABLE_SIMPLE_FORMAT;
33+
34+public class SchemaRefreshDebouncerTest extends CCMBridge.PerClassSingleNodeCluster {
35+
36+ // This may need to be tweaked depending on the reliability of the test environment.
37+ private static final int DEBOUNCE_TIME = 5000;
38+
39+ // Control Connection to be spied.
40+ private ControlConnection controlConnection;
41+
42+ // Schema Listener to be mocked.
43+ private SchemaChangeListener listener;
44+
45+ // Separate session/clusters to observe schema events on.
46+ private Session session2;
47+ private Cluster cluster2;
48+
49+ // Keyspaces to drop after test completes.
50+ private Collection<String> keyspaces = newArrayList();
51+
52+ @Override
53+ protected Collection<String> getTableDefinitions() {
54+ return newArrayList();
55+ }
56+
57+ @BeforeClass(groups = "short")
58+ public void setup() {
59+ QueryOptions queryOptions = new QueryOptions();
60+ queryOptions.setRefreshSchemaIntervalMillis(DEBOUNCE_TIME);
61+ queryOptions.setMaxPendingRefreshSchemaRequests(5);
62+ // Create a separate cluster that will receive the schema events on its control connection.
63+ cluster2 = this.configure(Cluster.builder())
64+ .addContactPointsWithPorts(newArrayList(hostAddress))
65+ .withQueryOptions(queryOptions)
66+ .build();
67+ session2 = cluster2.connect();
68+
69+ // Create a spy of the Cluster's control connection and replace it with the spy.
70+ controlConnection = spy(cluster2.manager.controlConnection);
71+ cluster2.manager.controlConnection = controlConnection;
72+
73+ // Create a mock of SchemaChangeListener to use for signalling events.
74+ listener = mock(SchemaChangeListener.class);
75+ cluster2.register(listener);
76+
77+ reset(listener);
78+ reset(controlConnection);
79+ }
80+
81+ @AfterMethod(groups = "short")
82+ public void beforeMethod() {
83+ reset(listener);
84+ reset(controlConnection);
85+ }
86+
87+ @AfterClass(groups = "short")
88+ public void teardown() {
89+ cluster2.close();
90+ // drop all keyspaces
91+ for(String keyspace : keyspaces) {
92+ session.execute("DROP KEYSPACE " + keyspace);
93+ }
94+ }
95+
96+ /**
97+ * Ensures that when a CREATED and UPDATED schema_change events are received on a control
98+ * connection for the same keyspace within {@link QueryOptions#getRefreshSchemaIntervalMillis()}
99+ * that the schema refresh is debounced and coalesced into a single schema refresh for that keyspace only.
100+ *
101+ * @throws Exception
102+ * @jira_ticket JAVA-657
103+ * @since 2.0.11
104+ */
105+ @Test(groups = "short")
106+ public void should_debounce_and_coalesce_create_and_alter_keyspace_into_refresh_keyspace() throws Exception {
107+ String keyspace = "sdaccaak";
108+ session.execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, keyspace, 1));
109+ keyspaces.add(keyspace);
110+ session.execute(String.format("ALTER KEYSPACE %s WITH DURABLE_WRITES=false", keyspace));
111+
112+ ArgumentCaptor<KeyspaceMetadata> captor = forClass(KeyspaceMetadata.class);
113+ verify(listener, timeout(DEBOUNCE_TIME * 2).only()).onKeyspaceAdded(captor.capture());
114+ assertThat(captor.getValue()).hasName(keyspace).isNotDurableWrites();
115+
116+ // Verify that the schema refresh was debounced and coalesced when a keyspace creation
117+ // and update event occur for the same keyspace.
118+ verify(controlConnection, times(1)).refreshSchema(keyspace, null);
119+
120+ KeyspaceMetadata ksm = cluster2.getMetadata().getKeyspace(keyspace);
121+ // By ensuring durable writes is false, we know that the single schema refresh occurred
122+ // after the alter event.
123+ assertThat(ksm).isNotNull().hasName(keyspace).isNotDurableWrites();
124+ }
125+
126+
127+ /**
128+ * Ensures that when a CREATED (keyspace) and CREATED (table) schema_change events are received
129+ * on a control connection with that table belonging to that keyspace within
130+ * {@link QueryOptions#getRefreshSchemaIntervalMillis()} that the schema refresh is debounced
131+ * and coalesced into a single schema refresh for that keyspace only.
132+ *
133+ * @throws Exception
134+ * @jira_ticket JAVA-657
135+ * @since 2.0.11
136+ */
137+ @Test(groups = "short")
138+ public void should_debounce_and_coalesce_create_keyspace_and_table_into_refresh_keyspace() throws Exception {
139+ String keyspace = "sdacckt";
140+ String table = "tbl1";
141+ session.execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, keyspace, 1));
142+ keyspaces.add(keyspace);
143+ session.execute(String.format(CREATE_TABLE_SIMPLE_FORMAT, keyspace + "." + table));
144+
145+ ArgumentCaptor<KeyspaceMetadata> keyspaceCaptor = forClass(KeyspaceMetadata.class);
146+ verify(listener, timeout(DEBOUNCE_TIME * 2).times(1)).onKeyspaceAdded(keyspaceCaptor.capture());
147+ assertThat(keyspaceCaptor.getValue()).hasName(keyspace);
148+
149+ ArgumentCaptor<TableMetadata> tableCaptor = forClass(TableMetadata.class);
150+ verify(listener, times(1)).onTableAdded(tableCaptor.capture());
151+ assertThat(tableCaptor.getValue()).hasName(table);
152+
153+ // Verify the schema refresh was debounced and coalesced when a keyspace event and table event
154+ // in that keyspace is detected.
155+ verify(controlConnection).refreshSchema(keyspace, null);
156+ verify(controlConnection, never()).refreshSchema(keyspace, table);
157+
158+ KeyspaceMetadata ksm = cluster2.getMetadata().getKeyspace(keyspace);
159+ assertThat(ksm).isNotNull();
160+ assertThat(ksm.getTable(table)).isNotNull();
161+ }
162+
163+ /**
164+ * Ensures that when multiple CREATED schema_change events are received
165+ * on a control connection for tables belonging to the same keyspace within
166+ * {@link QueryOptions#getRefreshSchemaIntervalMillis()} that the schema refresh is debounced
167+ * and coalesced into a single schema refresh for that keyspace only.
168+ *
169+ * @throws Exception
170+ * @jira_ticket JAVA-657
171+ * @since 2.0.11
172+ */
173+ @Test(groups = "short")
174+ public void should_debounce_and_coalesce_tables_in_same_keyspace_into_refresh_keyspace() throws Exception {
175+ String keyspace = "sdactisk";
176+ session2.execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, keyspace, 1));
177+ keyspaces.add(keyspace);
178+ // Reset invocations as creating keyspace causes a keyspace refresh.
179+ reset(controlConnection);
180+ reset(listener);
181+
182+ int tableCount = 3;
183+ for(int i =0 ; i < tableCount; i++) {
184+ session.execute(String.format(CREATE_TABLE_SIMPLE_FORMAT, keyspace + "." + "tbl" + i));
185+ }
186+
187+ verify(listener, timeout(DEBOUNCE_TIME * 3).times(3)).onTableAdded(any(TableMetadata.class));
188+
189+ // Verify a refresh of the keyspace was executed, but not individually on the
190+ // tables since those events were coalesced.
191+ verify(controlConnection).refreshSchema(keyspace, null);
192+
193+ KeyspaceMetadata ksm = cluster2.getMetadata().getKeyspace(keyspace);
194+ assertThat(ksm).isNotNull();
195+ // metadata is present for each table.
196+ for(int i = 0; i < tableCount; i++) {
197+ String table = "tbl" + i;
198+ // Should have never been a refreshSchema on the table.
199+ verify(controlConnection, never()).refreshSchema(keyspace, table);
200+ assertThat(ksm.getTable(table)).isNotNull();
201+ }
202+ }
203+
204+ /**
205+ * Ensures that when multiple UPDATED schema_change events are received
206+ * on a control connection for for the same table within
207+ * {@link QueryOptions#getRefreshSchemaIntervalMillis()} that the schema refresh is debounced
208+ * and coalesced into a single schema refresh for that table only.
209+ *
210+ * @throws Exception
211+ * @jira_ticket JAVA-657
212+ * @since 2.0.11
213+ */
214+ @Test(groups = "short")
215+ public void should_debounce_and_coalesce_multiple_alter_events_on_same_table_into_refresh_table() throws Exception {
216+ String keyspace = "sdacmaeost";
217+ String table = "tbl1";
218+ String comment = "I am changing this table.";
219+ String columnName = "added_column";
220+ // Execute on session 2 which refreshes schema as part of processing responses.
221+ session2.execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, keyspace, 1));
222+ keyspaces.add(keyspace);
223+ session2.execute(String.format(CREATE_TABLE_SIMPLE_FORMAT, keyspace + "." + table));
224+ reset(controlConnection);
225+ reset(listener);
226+
227+ session.execute(String.format("ALTER TABLE %s.%s WITH comment = '%s'", keyspace, table, comment));
228+ session.execute(String.format("ALTER TABLE %s.%s ADD %s int", keyspace, table, columnName));
229+
230+ ArgumentCaptor<TableMetadata> original = forClass(TableMetadata.class);
231+ ArgumentCaptor<TableMetadata> captor = forClass(TableMetadata.class);
232+ verify(listener, timeout(DEBOUNCE_TIME * 2).only()).onTableChanged(captor.capture(), original.capture());
233+ assertThat(captor.getValue())
234+ .hasName(table)
235+ .isInKeyspace(keyspace)
236+ .hasColumn(columnName)
237+ .hasComment(comment);
238+
239+ assertThat(original.getValue())
240+ .hasName(table)
241+ .isInKeyspace(keyspace)
242+ .hasNoColumn(columnName)
243+ .doesNotHaveComment(comment);
244+
245+ // Verify a refresh of the table was executed, but only once.
246+ verify(controlConnection, times(1)).refreshSchema(keyspace, table);
247+
248+ KeyspaceMetadata ksm = cluster2.getMetadata().getKeyspace(keyspace);
249+ assertThat(ksm).isNotNull();
250+ TableMetadata tm = ksm.getTable(table);
251+ assertThat(tm)
252+ .hasName(table)
253+ .isInKeyspace(keyspace)
254+ .hasColumn(columnName)
255+ .hasComment(comment);
256+ }
257+
258+ /**
259+ * Ensures that when a CREATED (keyspace) and CREATED (keyspace) schema_change events are received
260+ * on a control connection for different keyspaces within
261+ * {@link QueryOptions#getRefreshSchemaIntervalMillis()} that the schema refresh is debounced
262+ * and coalesced into a single full schema refresh.
263+ *
264+ * @throws Exception
265+ * @jira_ticket JAVA-657
266+ * @since 2.0.11
267+ */
268+ @Test(groups = "short")
269+ public void should_debounce_and_coalesce_multiple_keyspace_creates_into_refresh_entire_schema() throws Exception {
270+ String prefix = "sdacmkc";
271+ for(int i = 0; i < 3; i++) {
272+ session.execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, prefix + i, 1));
273+ keyspaces.add(prefix + i);
274+ }
275+
276+ verify(listener, timeout(DEBOUNCE_TIME * 3).times(3)).onKeyspaceAdded(any(KeyspaceMetadata.class));
277+ // Verify a complete schema refresh was executed, but only once.
278+ verify(controlConnection, times(1)).refreshSchema(null, null);
279+
280+ for(int i = 0; i < 3; i++) {
281+ KeyspaceMetadata ksm = cluster2.getMetadata().getKeyspace(prefix + i);
282+ assertThat(ksm).isNotNull().hasName(prefix + i);
283+ }
284+ }
285+
286+ /**
287+ * Ensures that when enough schema changes have been received on a control connection to
288+ * reach {@link QueryOptions#getMaxPendingRefreshSchemaRequests()} that a schema refresh
289+ * is submitted right away.
290+ *
291+ * @throws Exception
292+ * @jira_ticket JAVA-657
293+ * @since 2.0.11
294+ */
295+ @Test(groups = "short")
296+ public void should_refresh_when_max_pending_requests_reached() throws Exception {
297+ String prefix = "srwmprr";
298+ for(int i = 0; i < 5; i++) {
299+ session.execute(String.format(CREATE_KEYSPACE_SIMPLE_FORMAT, prefix + i, 1));
300+ keyspaces.add(prefix + i);
301+ }
302+
303+ // Event should be processed immediately as we hit our threshold.
304+ verify(listener, times(5)).onKeyspaceAdded(any(KeyspaceMetadata.class));
305+ // Verify a complete schema refresh was executed, but only once.
306+ verify(controlConnection, times(1)).refreshSchema(null, null);
307+
308+ for(int i = 0; i < 5; i++) {
309+ KeyspaceMetadata ksm = cluster2.getMetadata().getKeyspace(prefix + i);
310+ assertThat(ksm).isNotNull().hasName(prefix + i);
311+ }
312+ }
313+}
modified driver-core/src/test/java/com/datastax/driver/core/TableMetadataAssert.java +10 −0
@@ -50,4 +50,14 @@
5050 assertThat(actual.getColumn(columnName)).isNull();
5151 return this;
5252 }
53+
54+ public TableMetadataAssert hasComment(String comment) {
55+ assertThat(actual.getOptions().getComment()).isEqualTo(comment);
56+ return this;
57+ }
58+
59+ public TableMetadataAssert doesNotHaveComment(String comment) {
60+ assertThat(actual.getOptions().getComment()).isNotEqualTo(comment);
61+ return this;
62+ }
5363 }