Skip to content

Commit

Permalink
SPY-165: Do not cache the selectedKeys.
Browse files Browse the repository at this point in the history
Motivation
----------
Caching the selected keys may cause issues if the selector is accessed by different
threads, also the loop for the keys is not using the iterator correct.

Modifications
-------------
Make it not cache the keys and also correctly make use of an iterator to loop through
the keys.

Result
------
No stale/cached selected keys and correctly using the iterator should lead to better
stability during failure cases.

Thanks to Brad Svee (@sveesible) for suggesting the fix

Change-Id: If82cdfc810c758a4196415cb709a03ebd72c3d15
Reviewed-on: http://review.couchbase.org/36222
Reviewed-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
Tested-by: Michael Nitschinger <michael.nitschinger@couchbase.com>
  • Loading branch information
daschl authored and Michael Nitschinger committed May 5, 2014
1 parent 206b198 commit fde7a94
Showing 1 changed file with 7 additions and 6 deletions.
13 changes: 7 additions & 6 deletions src/main/java/net/spy/memcached/MemcachedConnection.java
Expand Up @@ -66,7 +66,6 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.concurrent.ConcurrentLinkedQueue;
Expand Down Expand Up @@ -405,19 +404,21 @@ public void handleIO() throws IOException {
getLogger().debug("Selecting with delay of %sms", delay);
assert selectorsMakeSense() : "Selectors don't make sense.";
int selected = selector.select(delay);
Set<SelectionKey> selectedKeys = selector.selectedKeys();
//Set<SelectionKey> selectedKeys = selector.selectedKeys();

if (selectedKeys.isEmpty() && !shutDown) {
if (selector.selectedKeys().isEmpty() && !shutDown) {
handleEmptySelects();
} else {
getLogger().debug("Selected %d, selected %d keys", selected,
selectedKeys.size());
selector.selectedKeys().size());
emptySelects = 0;

for (SelectionKey sk : selectedKeys) {
Iterator<SelectionKey> iterator = selector.selectedKeys().iterator();
while(iterator.hasNext()) {
SelectionKey sk = iterator.next();
handleIO(sk);
iterator.remove();
}
selectedKeys.clear();
}

handleOperationalTasks();
Expand Down

0 comments on commit fde7a94

Please sign in to comment.