Elffuss BraidHub

kikocis/java-driver public

JAVA-1192: Make EventDebouncer settings updatable at runtime (#668)

Changeset465ac6b8b47f072376ec6c945ce4f3a7f8eb6579e56f193804c5b2edd0dc659d
AuthorAlexandre Dutra <adutra@users.noreply.github.com> · 2016-07-18 23:13 +02:00
CommitterOlivier Michallat <olim7t@users.noreply.github.com> · 2016-07-18 14:13 -07:00
Parents75ab152b44cc

Browse the files at this changeset

5 files changed

modified changelog/README.md +1 −0
@@ -9,6 +9,7 @@
99 - [bug] JAVA-1161: Preserve full time zone info in ZonedDateTimeCodec and DateTimeCodec.
1010 - [new feature] JAVA-1157: Allow asynchronous paging of Mapper Result.
1111 - [improvement] JAVA-1212: Don't retry non-idempotent statements by default.
12+- [improvement] JAVA-1192: Make EventDebouncer settings updatable at runtime.
1213
1314 Merged from 3.0.x branch:
1415
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +40 −16
@@ -1384,31 +1384,55 @@
13841384 this.preparedQueries = new MapMaker().weakValues().makeMap();
13851385
13861386 // create debouncers - at this stage, they are not running yet
1387- QueryOptions queryOptions = configuration.getQueryOptions();
1387+ final QueryOptions queryOptions = configuration.getQueryOptions();
13881388 this.nodeListRefreshRequestDebouncer = new EventDebouncer<NodeListRefreshRequest>(
13891389 "Node list refresh",
13901390 scheduledTasksExecutor,
1391- new NodeListRefreshRequestDeliveryCallback(),
1392- queryOptions.getRefreshNodeListIntervalMillis(),
1393- queryOptions.getMaxPendingRefreshNodeListRequests(),
1394- EventDebouncer.DEFAULT_MAX_QUEUED_EVENTS
1395- );
1391+ new NodeListRefreshRequestDeliveryCallback()
1392+ ) {
1393+
1394+ @Override
1395+ int maxPendingEvents() {
1396+ return configuration.getQueryOptions().getMaxPendingRefreshNodeListRequests();
1397+ }
1398+
1399+ @Override
1400+ long delayMs() {
1401+ return configuration.getQueryOptions().getRefreshNodeListIntervalMillis();
1402+ }
1403+ };
13961404 this.nodeRefreshRequestDebouncer = new EventDebouncer<NodeRefreshRequest>(
13971405 "Node refresh",
13981406 scheduledTasksExecutor,
1399- new NodeRefreshRequestDeliveryCallback(),
1400- queryOptions.getRefreshNodeIntervalMillis(),
1401- queryOptions.getMaxPendingRefreshNodeRequests(),
1402- EventDebouncer.DEFAULT_MAX_QUEUED_EVENTS
1403- );
1407+ new NodeRefreshRequestDeliveryCallback()
1408+ ) {
1409+
1410+ @Override
1411+ int maxPendingEvents() {
1412+ return configuration.getQueryOptions().getMaxPendingRefreshNodeRequests();
1413+ }
1414+
1415+ @Override
1416+ long delayMs() {
1417+ return configuration.getQueryOptions().getRefreshNodeIntervalMillis();
1418+ }
1419+ };
14041420 this.schemaRefreshRequestDebouncer = new EventDebouncer<SchemaRefreshRequest>(
14051421 "Schema refresh",
14061422 scheduledTasksExecutor,
1407- new SchemaRefreshRequestDeliveryCallback(),
1408- queryOptions.getRefreshSchemaIntervalMillis(),
1409- queryOptions.getMaxPendingRefreshSchemaRequests(),
1410- EventDebouncer.DEFAULT_MAX_QUEUED_EVENTS
1411- );
1423+ new SchemaRefreshRequestDeliveryCallback()
1424+ ) {
1425+
1426+ @Override
1427+ int maxPendingEvents() {
1428+ return configuration.getQueryOptions().getMaxPendingRefreshSchemaRequests();
1429+ }
1430+
1431+ @Override
1432+ long delayMs() {
1433+ return configuration.getQueryOptions().getRefreshSchemaIntervalMillis();
1434+ }
1435+ };
14121436
14131437 this.scheduledTasksExecutor.scheduleWithFixedDelay(new CleanupIdleConnectionsTask(), 10, 10, TimeUnit.SECONDS);
14141438
modified driver-core/src/main/java/com/datastax/driver/core/EventDebouncer.java +13 −8
@@ -44,11 +44,11 @@
4444 * - no events have been received for delayMs
4545 * - maxPendingEvents have been received
4646 */
47-class EventDebouncer<T> {
47+abstract class EventDebouncer<T> {
4848
4949 private static final Logger logger = LoggerFactory.getLogger(EventDebouncer.class);
5050
51- static final int DEFAULT_MAX_QUEUED_EVENTS = 10000;
51+ private static final int DEFAULT_MAX_QUEUED_EVENTS = 10000;
5252
5353 private final String name;
5454
@@ -59,8 +59,6 @@
5959
6060 private final DeliveryCallback<T> callback;
6161
62- private final long delayMs;
63- private final int maxPendingEvents;
6462 private final int maxQueuedEvents;
6563
6664 private final Queue<Entry<T>> events;
@@ -73,18 +71,24 @@
7371 private static final long OVERFLOW_WARNING_INTERVAL = NANOSECONDS.convert(5, SECONDS);
7472 private volatile long lastOverflowWarning = Long.MIN_VALUE;
7573
76- EventDebouncer(String name, ScheduledExecutorService executor, DeliveryCallback<T> callback, long delayMs, int maxPendingEvents, int maxQueuedEvents) {
74+ EventDebouncer(String name, ScheduledExecutorService executor, DeliveryCallback<T> callback) {
75+ this(name, executor, callback, DEFAULT_MAX_QUEUED_EVENTS);
76+ }
77+
78+ EventDebouncer(String name, ScheduledExecutorService executor, DeliveryCallback<T> callback, int maxQueuedEvents) {
7779 this.name = name;
7880 this.executor = executor;
7981 this.callback = callback;
80- this.delayMs = delayMs;
81- this.maxPendingEvents = maxPendingEvents;
8282 this.maxQueuedEvents = maxQueuedEvents;
8383 this.events = new ConcurrentLinkedQueue<Entry<T>>();
8484 this.eventCount = new AtomicInteger();
8585 this.state = State.NEW;
8686 }
8787
88+ abstract int maxPendingEvents();
89+
90+ abstract long delayMs();
91+
8892 void start() {
8993 logger.trace("Starting {} debouncer...", name);
9094 state = State.RUNNING;
@@ -151,6 +155,7 @@
151155
152156 if (state == State.RUNNING) {
153157 int count = eventCount.get();
158+ int maxPendingEvents = maxPendingEvents();
154159 if (count < maxPendingEvents) {
155160 scheduleDelayedDelivery();
156161 } else if (count == maxPendingEvents) {
@@ -262,7 +267,7 @@
262267
263268 void scheduleAfterDelay() {
264269 if (state != State.STOPPED)
265- deliveryFuture = executor.schedule(this, delayMs, TimeUnit.MILLISECONDS);
270+ deliveryFuture = executor.schedule(this, delayMs(), TimeUnit.MILLISECONDS);
266271 }
267272
268273 @Override
modified driver-core/src/test/java/com/datastax/driver/core/EventDebouncerIntegrationTest.java +32 −0
@@ -26,6 +26,7 @@
2626 import static com.datastax.driver.core.TestUtils.ipOfNode;
2727 import static org.assertj.core.api.Assertions.assertThat;
2828 import static org.assertj.core.api.Fail.fail;
29+import static org.mockito.Mockito.*;
2930
3031 @CreateCCM(PER_METHOD)
3132 public class EventDebouncerIntegrationTest extends CCMTestsSupport {
@@ -68,6 +69,37 @@
6869 assertThat(policy.hosts).doesNotContain(TestUtils.findHost(cluster, 3));
6970 }
7071
72+ /**
73+ * Tests that settings for a debouncer can be modified dynamically
74+ * without requiring the cluster to be restarted.
75+ *
76+ * @throws InterruptedException
77+ * @jira_ticket JAVA-1192
78+ */
79+ @CCMConfig(numberOfNodes = 1)
80+ @Test(groups = "short")
81+ public void should_change_debouncer_settings_dynamically() throws InterruptedException {
82+ // Create a spy of the Cluster's control connection and replace it with the spy.
83+ ControlConnection controlConnection = spy(cluster().manager.controlConnection);
84+ cluster().manager.controlConnection = controlConnection;
85+ for (int i = 0; i < 10; i++) {
86+ cluster().manager.submitNodeListRefresh();
87+ Thread.sleep(100);
88+ }
89+ // all requests should be coalesced into a single one
90+ verify(controlConnection, timeout(10000)).refreshNodeListAndTokenMap();
91+ reset(controlConnection);
92+ // disable debouncing
93+ cluster().getConfiguration().getQueryOptions()
94+ .setRefreshNodeListIntervalMillis(0);
95+ for (int i = 0; i < 10; i++) {
96+ cluster().manager.submitNodeListRefresh();
97+ Thread.sleep(100);
98+ }
99+ // each request should have been handled separately
100+ verify(controlConnection, timeout(10000).times(10)).refreshNodeListAndTokenMap();
101+ }
102+
71103 private class TestLoadBalancingPolicy extends SortingLoadBalancingPolicy {
72104
73105 CyclicBarrier stop = new CyclicBarrier(2);
modified driver-core/src/test/java/com/datastax/driver/core/EventDebouncerTest.java +88 −9
@@ -34,7 +34,6 @@
3434 import java.util.concurrent.locks.Lock;
3535 import java.util.concurrent.locks.ReentrantLock;
3636
37-import static com.datastax.driver.core.EventDebouncer.DEFAULT_MAX_QUEUED_EVENTS;
3837 import static java.util.concurrent.TimeUnit.MILLISECONDS;
3938 import static java.util.concurrent.TimeUnit.MINUTES;
4039 import static org.assertj.core.api.Assertions.assertThat;
@@ -59,7 +58,17 @@
5958
6059 @Test(groups = "unit")
6160 public void should_deliver_single_event() throws InterruptedException {
62- EventDebouncer<MockEvent> debouncer = new EventDebouncer<MockEvent>("test", executor, callback, 10, 50, DEFAULT_MAX_QUEUED_EVENTS);
61+ EventDebouncer<MockEvent> debouncer = new EventDebouncer<MockEvent>("test", executor, callback) {
62+ @Override
63+ int maxPendingEvents() {
64+ return 10;
65+ }
66+
67+ @Override
68+ long delayMs() {
69+ return 50;
70+ }
71+ };
6372 debouncer.start();
6473 MockEvent event = new MockEvent(0);
6574 debouncer.eventReceived(event);
@@ -75,7 +84,17 @@
7584 logger.setLevel(Level.WARN);
7685 logger.addAppender(logs);
7786 try {
78- EventDebouncer<MockEvent> debouncer = new EventDebouncer<MockEvent>("test", executor, callback, 100, 15, 10);
87+ EventDebouncer<MockEvent> debouncer = new EventDebouncer<MockEvent>("test", executor, callback, 10) {
88+ @Override
89+ int maxPendingEvents() {
90+ return 100;
91+ }
92+
93+ @Override
94+ long delayMs() {
95+ return 15;
96+ }
97+ };
7998 debouncer.start();
8099 List<MockEvent> events = new ArrayList<MockEvent>();
81100 for (int i = 0; i < 14; i++) {
@@ -96,7 +115,17 @@
96115
97116 @Test(groups = "unit")
98117 public void should_deliver_n_events_in_order() throws InterruptedException {
99- EventDebouncer<MockEvent> debouncer = new EventDebouncer<MockEvent>("test", executor, callback, 10, 50, DEFAULT_MAX_QUEUED_EVENTS);
118+ EventDebouncer<MockEvent> debouncer = new EventDebouncer<MockEvent>("test", executor, callback) {
119+ @Override
120+ int maxPendingEvents() {
121+ return 10;
122+ }
123+
124+ @Override
125+ long delayMs() {
126+ return 50;
127+ }
128+ };
100129 debouncer.start();
101130 List<MockEvent> events = new ArrayList<MockEvent>();
102131 for (int i = 0; i < 50; i++) {
@@ -110,7 +139,17 @@
110139
111140 @Test(groups = "unit")
112141 public void should_deliver_n_events_in_order_even_if_queue_full() throws InterruptedException {
113- EventDebouncer<MockEvent> debouncer = new EventDebouncer<MockEvent>("test", executor, callback, 10, 1, DEFAULT_MAX_QUEUED_EVENTS);
142+ EventDebouncer<MockEvent> debouncer = new EventDebouncer<MockEvent>("test", executor, callback) {
143+ @Override
144+ int maxPendingEvents() {
145+ return 10;
146+ }
147+
148+ @Override
149+ long delayMs() {
150+ return 1;
151+ }
152+ };
114153 debouncer.start();
115154 List<MockEvent> events = new ArrayList<MockEvent>();
116155 for (int i = 0; i < 50; i++) {
@@ -124,7 +163,17 @@
124163
125164 @Test(groups = "unit")
126165 public void should_accumulate_events_if_not_ready() throws InterruptedException {
127- EventDebouncer<MockEvent> debouncer = new EventDebouncer<MockEvent>("test", executor, callback, 10, 50, DEFAULT_MAX_QUEUED_EVENTS);
166+ EventDebouncer<MockEvent> debouncer = new EventDebouncer<MockEvent>("test", executor, callback) {
167+ @Override
168+ int maxPendingEvents() {
169+ return 10;
170+ }
171+
172+ @Override
173+ long delayMs() {
174+ return 50;
175+ }
176+ };
128177 List<MockEvent> events = new ArrayList<MockEvent>();
129178 for (int i = 0; i < 50; i++) {
130179 MockEvent event = new MockEvent(i);
@@ -140,7 +189,17 @@
140189
141190 @Test(groups = "unit")
142191 public void should_accumulate_all_events_until_start() throws InterruptedException {
143- final EventDebouncer<MockEvent> debouncer = new EventDebouncer<MockEvent>("test", executor, callback, 10, 25, DEFAULT_MAX_QUEUED_EVENTS);
192+ final EventDebouncer<MockEvent> debouncer = new EventDebouncer<MockEvent>("test", executor, callback) {
193+ @Override
194+ int maxPendingEvents() {
195+ return 10;
196+ }
197+
198+ @Override
199+ long delayMs() {
200+ return 25;
201+ }
202+ };
144203 final List<MockEvent> events = new ArrayList<MockEvent>();
145204
146205 for (int i = 0; i < 50; i++) {
@@ -157,7 +216,17 @@
157216
158217 @Test(groups = "unit")
159218 public void should_reset_timer_if_n_events_received_within_same_window() throws InterruptedException {
160- final EventDebouncer<MockEvent> debouncer = new EventDebouncer<MockEvent>("test", executor, callback, 50, 50, DEFAULT_MAX_QUEUED_EVENTS);
219+ final EventDebouncer<MockEvent> debouncer = new EventDebouncer<MockEvent>("test", executor, callback) {
220+ @Override
221+ int maxPendingEvents() {
222+ return 50;
223+ }
224+
225+ @Override
226+ long delayMs() {
227+ return 50;
228+ }
229+ };
161230 debouncer.start();
162231 final CountDownLatch latch = new CountDownLatch(50);
163232 ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
@@ -179,7 +248,17 @@
179248
180249 @Test(groups = "unit")
181250 public void should_stop_receiving_events() throws InterruptedException {
182- final EventDebouncer<MockEvent> debouncer = new EventDebouncer<MockEvent>("test", executor, callback, 10, 50, DEFAULT_MAX_QUEUED_EVENTS);
251+ final EventDebouncer<MockEvent> debouncer = new EventDebouncer<MockEvent>("test", executor, callback) {
252+ @Override
253+ int maxPendingEvents() {
254+ return 10;
255+ }
256+
257+ @Override
258+ long delayMs() {
259+ return 50;
260+ }
261+ };
183262 debouncer.start();
184263 for (int i = 0; i < 50; i++) {
185264 MockEvent event = new MockEvent(i);