Elffuss BraidHub

kikocis/java-driver public

Merge branch '2.1' into 3.0
Changesetddcb7a9a91615af71b79dc3d7639a3a245670411fad77ee3531104999fcd15ce
Authorolim7t <omichallat+github@gmail.com> · 2016-01-26 15:23 +01:00
Committerolim7t <omichallat+github@gmail.com> · 2016-01-26 15:24 +01:00
Parentsd6b0dc0c73f3, 35167456ffff

Browse the files at this changeset

4 files changed

modified changelog/README.md +2 −0
@@ -10,6 +10,7 @@
1010 Merged from 2.1 branch:
1111
1212 - [improvement] JAVA-1030: Log token to replica map computation times.
13+- [bug] JAVA-1039: Minor bugs in Event Debouncer.
1314
1415
1516 ### 3.0.0-rc1
@@ -265,6 +266,7 @@
265266
266267 - [bug] JAVA-988: Metadata.handleId should handle escaped double quotes.
267268 - [improvement] JAVA-1030: Log token to replica map computation times.
269+- [bug] JAVA-1039: Minor bugs in Event Debouncer.
268270
269271 Merged from 2.0 branch:
270272
modified driver-core/src/main/java/com/datastax/driver/core/Cluster.java +9 −3
@@ -1374,19 +1374,25 @@
13741374 scheduledTasksExecutor,
13751375 new NodeListRefreshRequestDeliveryCallback(),
13761376 queryOptions.getRefreshNodeListIntervalMillis(),
1377- queryOptions.getMaxPendingRefreshNodeListRequests());
1377+ queryOptions.getMaxPendingRefreshNodeListRequests(),
1378+ EventDebouncer.DEFAULT_MAX_QUEUED_EVENTS
1379+ );
13781380 this.nodeRefreshRequestDebouncer = new EventDebouncer<NodeRefreshRequest>(
13791381 "Node refresh",
13801382 scheduledTasksExecutor,
13811383 new NodeRefreshRequestDeliveryCallback(),
13821384 queryOptions.getRefreshNodeIntervalMillis(),
1383- queryOptions.getMaxPendingRefreshNodeRequests());
1385+ queryOptions.getMaxPendingRefreshNodeRequests(),
1386+ EventDebouncer.DEFAULT_MAX_QUEUED_EVENTS
1387+ );
13841388 this.schemaRefreshRequestDebouncer = new EventDebouncer<SchemaRefreshRequest>(
13851389 "Schema refresh",
13861390 scheduledTasksExecutor,
13871391 new SchemaRefreshRequestDeliveryCallback(),
13881392 queryOptions.getRefreshSchemaIntervalMillis(),
1389- queryOptions.getMaxPendingRefreshSchemaRequests());
1393+ queryOptions.getMaxPendingRefreshSchemaRequests(),
1394+ EventDebouncer.DEFAULT_MAX_QUEUED_EVENTS
1395+ );
13901396
13911397 this.scheduledTasksExecutor.scheduleWithFixedDelay(new CleanupIdleConnectionsTask(), 10, 10, TimeUnit.SECONDS);
13921398
modified driver-core/src/main/java/com/datastax/driver/core/EventDebouncer.java +17 −13
@@ -48,7 +48,7 @@
4848
4949 private static final Logger logger = LoggerFactory.getLogger(EventDebouncer.class);
5050
51- private static final int MAX_QUEUED_EVENTS = 10000;
51+ static final int DEFAULT_MAX_QUEUED_EVENTS = 10000;
5252
5353 private final String name;
5454
@@ -61,6 +61,7 @@
6161
6262 private final long delayMs;
6363 private final int maxPendingEvents;
64+ private final int maxQueuedEvents;
6465
6566 private final Queue<Entry<T>> events;
6667 private final AtomicInteger eventCount;
@@ -72,12 +73,13 @@
7273 private static final long OVERFLOW_WARNING_INTERVAL = NANOSECONDS.convert(5, SECONDS);
7374 private volatile long lastOverflowWarning = Long.MIN_VALUE;
7475
75- EventDebouncer(String name, ScheduledExecutorService executor, DeliveryCallback<T> callback, long delayMs, int maxPendingEvents) {
76+ EventDebouncer(String name, ScheduledExecutorService executor, DeliveryCallback<T> callback, long delayMs, int maxPendingEvents, int maxQueuedEvents) {
7677 this.name = name;
7778 this.executor = executor;
7879 this.callback = callback;
7980 this.delayMs = delayMs;
8081 this.maxPendingEvents = maxPendingEvents;
82+ this.maxQueuedEvents = maxQueuedEvents;
8183 this.events = new ConcurrentLinkedQueue<Entry<T>>();
8284 this.eventCount = new AtomicInteger();
8385 this.state = State.NEW;
@@ -126,15 +128,16 @@
126128 checkNotNull(event);
127129 logger.trace("{} debouncer: event received {}", name, event);
128130
129- int count = eventCount.incrementAndGet();
130-
131131 // Safeguard against the queue filling up faster than we can process it
132- long now;
133- if (count > MAX_QUEUED_EVENTS && (now = System.nanoTime()) > lastOverflowWarning + OVERFLOW_WARNING_INTERVAL) {
134- lastOverflowWarning = now;
135- logger.warn("{} debouncer enqueued more than {} events, rejecting new events. "
132+ if (eventCount.incrementAndGet() > maxQueuedEvents) {
133+ long now = System.nanoTime();
134+ if (now > lastOverflowWarning + OVERFLOW_WARNING_INTERVAL) {
135+ lastOverflowWarning = now;
136+ logger.warn("{} debouncer enqueued more than {} events, rejecting new events. "
136137 + "This should not happen and is likely a sign that something is wrong.",
137- name, MAX_QUEUED_EVENTS);
138+ name, maxQueuedEvents);
139+ }
140+ eventCount.decrementAndGet();
138141 return MoreFutures.VOID_SUCCESS;
139142 }
140143
@@ -147,6 +150,7 @@
147150 }
148151
149152 if (state == State.RUNNING) {
153+ int count = eventCount.get();
150154 if (count < maxPendingEvents) {
151155 scheduleDelayedDelivery();
152156 } else if (count == maxPendingEvents) {
@@ -165,8 +169,8 @@
165169
166170 while (state == State.RUNNING) {
167171 DeliveryAttempt previous = immediateDelivery.get();
168- if (previous != null && !previous.isDone())
169- return;
172+ if (previous != null)
173+ previous.cancel();
170174
171175 DeliveryAttempt current = new DeliveryAttempt();
172176 if (immediateDelivery.compareAndSet(previous, current)) {
@@ -206,7 +210,7 @@
206210 Entry<T> entry;
207211 // Limit the number of events we dequeue, to avoid an infinite loop if the queue starts filling faster than we can consume it.
208212 int count = 0;
209- while (++count <= MAX_QUEUED_EVENTS && (entry = this.events.poll()) != null) {
213+ while (++count <= maxQueuedEvents && (entry = this.events.poll()) != null) {
210214 toDeliver.add(entry.event);
211215 futures.add(entry.future);
212216 }
@@ -234,7 +238,7 @@
234238
235239 // If we didn't dequeue all events (or new ones arrived since we did), make sure we eventually
236240 // process the remaining events, because eventReceived might have skipped the delivery
237- if (!events.isEmpty())
241+ if (eventCount.get() > 0)
238242 scheduleDelayedDelivery();
239243 }
240244
modified driver-core/src/test/java/com/datastax/driver/core/EventDebouncerTest.java +39 −19
@@ -18,6 +18,8 @@
1818 import com.datastax.driver.core.EventDebouncer.DeliveryCallback;
1919 import com.google.common.util.concurrent.Futures;
2020 import com.google.common.util.concurrent.ListenableFuture;
21+import org.apache.log4j.Level;
22+import org.apache.log4j.Logger;
2123 import org.testng.annotations.AfterMethod;
2224 import org.testng.annotations.BeforeMethod;
2325 import org.testng.annotations.Test;
@@ -28,14 +30,15 @@
2830 import java.util.concurrent.CountDownLatch;
2931 import java.util.concurrent.Executors;
3032 import java.util.concurrent.ScheduledExecutorService;
31-import java.util.concurrent.atomic.AtomicInteger;
3233 import java.util.concurrent.locks.Condition;
3334 import java.util.concurrent.locks.Lock;
3435 import java.util.concurrent.locks.ReentrantLock;
3536
37+import static com.datastax.driver.core.EventDebouncer.DEFAULT_MAX_QUEUED_EVENTS;
3638 import static java.util.concurrent.TimeUnit.MILLISECONDS;
3739 import static java.util.concurrent.TimeUnit.MINUTES;
3840 import static org.assertj.core.api.Assertions.assertThat;
41+import static org.assertj.core.api.Assertions.fail;
3942
4043 public class EventDebouncerTest {
4144
@@ -56,7 +59,7 @@
5659
5760 @Test(groups = "unit")
5861 public void should_deliver_single_event() throws InterruptedException {
59- EventDebouncer<MockEvent> debouncer = new EventDebouncer<MockEvent>("test", executor, callback, 10, 50);
62+ EventDebouncer<MockEvent> debouncer = new EventDebouncer<MockEvent>("test", executor, callback, 10, 50, DEFAULT_MAX_QUEUED_EVENTS);
6063 debouncer.start();
6164 MockEvent event = new MockEvent(0);
6265 debouncer.eventReceived(event);
@@ -64,9 +67,36 @@
6467 assertThat(callback.getEvents()).containsOnly(event);
6568 }
6669
70+ @Test(groups = "unit")
71+ public void should_log_and_drop_events_on_overflow() throws InterruptedException {
72+ MemoryAppender logs = new MemoryAppender();
73+ Logger logger = Logger.getLogger(EventDebouncer.class);
74+ Level originalLoggerLevel = logger.getLevel();
75+ logger.setLevel(Level.WARN);
76+ logger.addAppender(logs);
77+ try {
78+ EventDebouncer<MockEvent> debouncer = new EventDebouncer<MockEvent>("test", executor, callback, 100, 15, 10);
79+ debouncer.start();
80+ List<MockEvent> events = new ArrayList<MockEvent>();
81+ for (int i = 0; i < 14; i++) {
82+ MockEvent event = new MockEvent(i);
83+ events.add(event);
84+ debouncer.eventReceived(event);
85+ }
86+ // Only 10 events should have been handled.
87+ callback.awaitEvents(10);
88+ assertThat(callback.getEvents()).isEqualTo(events.subList(0, 10));
89+ // Debouncer warning should have been logged, but only once.
90+ assertThat(logs.get()).containsOnlyOnce("test debouncer enqueued more than 10 events, rejecting new events.");
91+ } finally {
92+ logger.removeAppender(logs);
93+ logger.setLevel(originalLoggerLevel);
94+ }
95+ }
96+
6797 @Test(groups = "unit")
6898 public void should_deliver_n_events_in_order() throws InterruptedException {
69- EventDebouncer<MockEvent> debouncer = new EventDebouncer<MockEvent>("test", executor, callback, 10, 50);
99+ EventDebouncer<MockEvent> debouncer = new EventDebouncer<MockEvent>("test", executor, callback, 10, 50, DEFAULT_MAX_QUEUED_EVENTS);
70100 debouncer.start();
71101 List<MockEvent> events = new ArrayList<MockEvent>();
72102 for (int i = 0; i < 50; i++) {
@@ -76,12 +106,11 @@
76106 }
77107 callback.awaitEvents(50);
78108 assertThat(callback.getEvents()).isEqualTo(events);
79- assertThat(callback.getInvocations()).isEqualTo(1);
80109 }
81110
82111 @Test(groups = "unit")
83112 public void should_deliver_n_events_in_order_even_if_queue_full() throws InterruptedException {
84- EventDebouncer<MockEvent> debouncer = new EventDebouncer<MockEvent>("test", executor, callback, 10, 1);
113+ EventDebouncer<MockEvent> debouncer = new EventDebouncer<MockEvent>("test", executor, callback, 10, 1, DEFAULT_MAX_QUEUED_EVENTS);
85114 debouncer.start();
86115 List<MockEvent> events = new ArrayList<MockEvent>();
87116 for (int i = 0; i < 50; i++) {
@@ -95,7 +124,7 @@
95124
96125 @Test(groups = "unit")
97126 public void should_accumulate_events_if_not_ready() throws InterruptedException {
98- EventDebouncer<MockEvent> debouncer = new EventDebouncer<MockEvent>("test", executor, callback, 10, 50);
127+ EventDebouncer<MockEvent> debouncer = new EventDebouncer<MockEvent>("test", executor, callback, 10, 50, DEFAULT_MAX_QUEUED_EVENTS);
99128 List<MockEvent> events = new ArrayList<MockEvent>();
100129 for (int i = 0; i < 50; i++) {
101130 MockEvent event = new MockEvent(i);
@@ -111,7 +140,7 @@
111140
112141 @Test(groups = "unit")
113142 public void should_accumulate_all_events_until_start() throws InterruptedException {
114- final EventDebouncer<MockEvent> debouncer = new EventDebouncer<MockEvent>("test", executor, callback, 10, 25);
143+ final EventDebouncer<MockEvent> debouncer = new EventDebouncer<MockEvent>("test", executor, callback, 10, 25, DEFAULT_MAX_QUEUED_EVENTS);
115144 final List<MockEvent> events = new ArrayList<MockEvent>();
116145
117146 for (int i = 0; i < 50; i++) {
@@ -124,12 +153,11 @@
124153
125154 callback.awaitEvents(50);
126155 assertThat(callback.getEvents()).isEqualTo(events);
127- assertThat(callback.getInvocations()).isEqualTo(1);
128156 }
129157
130158 @Test(groups = "unit")
131159 public void should_reset_timer_if_n_events_received_within_same_window() throws InterruptedException {
132- final EventDebouncer<MockEvent> debouncer = new EventDebouncer<MockEvent>("test", executor, callback, 50, 50);
160+ final EventDebouncer<MockEvent> debouncer = new EventDebouncer<MockEvent>("test", executor, callback, 50, 50, DEFAULT_MAX_QUEUED_EVENTS);
133161 debouncer.start();
134162 final CountDownLatch latch = new CountDownLatch(50);
135163 ScheduledExecutorService pool = Executors.newScheduledThreadPool(1);
@@ -147,12 +175,11 @@
147175 pool.shutdownNow();
148176 callback.awaitEvents(50);
149177 assertThat(callback.getEvents()).hasSize(50);
150- assertThat(callback.getInvocations()).isEqualTo(1);
151178 }
152179
153180 @Test(groups = "unit")
154181 public void should_stop_receiving_events() throws InterruptedException {
155- final EventDebouncer<MockEvent> debouncer = new EventDebouncer<MockEvent>("test", executor, callback, 10, 50);
182+ final EventDebouncer<MockEvent> debouncer = new EventDebouncer<MockEvent>("test", executor, callback, 10, 50, DEFAULT_MAX_QUEUED_EVENTS);
156183 debouncer.start();
157184 for (int i = 0; i < 50; i++) {
158185 MockEvent event = new MockEvent(i);
@@ -163,7 +190,6 @@
163190 MockEvent event = new MockEvent(0);
164191 debouncer.eventReceived(event);
165192 assertThat(callback.getEvents()).hasSize(50);
166- assertThat(callback.getInvocations()).isEqualTo(1);
167193 }
168194
169195 private static class MockDeliveryCallback implements DeliveryCallback<MockEvent> {
@@ -174,14 +200,11 @@
174200
175201 final Condition cond = lock.newCondition();
176202
177- final AtomicInteger invocations = new AtomicInteger(0);
178-
179203 @Override
180204 public ListenableFuture<?> deliver(List<MockEvent> events) {
181205 lock.lock();
182206 try {
183207 this.events.addAll(events);
184- invocations.incrementAndGet();
185208 cond.signal();
186209 } finally {
187210 lock.unlock();
@@ -195,7 +218,7 @@
195218 try {
196219 while (events.size() < expected) {
197220 if (nanos <= 0L)
198- break; // timeout
221+ fail("Timed out waiting for events");
199222 nanos = cond.awaitNanos(nanos);
200223 }
201224 } finally {
@@ -207,9 +230,6 @@
207230 return events;
208231 }
209232
210- public int getInvocations() {
211- return invocations.get();
212- }
213233 }
214234
215235 private class MockEvent {