Skip to content

Commit

Permalink
Implement contains for SelectedSelectionKeySet (#13452)
Browse files Browse the repository at this point in the history
Motivation:
The `contains` method may be called in the
`sub.nio.ch.SelectorImpl.processReadyEvents` method on macOS and
Windows, so we need to implement it correctly. Otherwise, we can lose
read-poll events.
See the investigation on the Hazelcast repo:
hazelcast/hazelcast#24267

Modification:
Add a contains method that just does a linear scan for the selection
key. This preserves the performance characteristics of all the other
methods.

Result:
Fixes #13328
  • Loading branch information
chrisvest committed Jun 16, 2023
1 parent b878314 commit 26a7df3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import java.util.Arrays;
import java.util.Iterator;
import java.util.NoSuchElementException;
import java.util.Objects;

final class SelectedSelectionKeySet extends AbstractSet<SelectionKey> {

Expand Down Expand Up @@ -51,6 +52,13 @@ public boolean remove(Object o) {

@Override
public boolean contains(Object o) {
SelectionKey[] array = keys;
for (int i = 0, s = size; i < s; i++) {
SelectionKey k = array[i];
if (k.equals(o)) {
return true;
}
}
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ public void contains() {
SelectedSelectionKeySet set = new SelectedSelectionKeySet();
assertTrue(set.add(mockKey));
assertTrue(set.add(mockKey2));
assertFalse(set.contains(mockKey));
assertFalse(set.contains(mockKey2));
assertTrue(set.contains(mockKey));
assertTrue(set.contains(mockKey2));
assertFalse(set.contains(mockKey3));
}

Expand Down

0 comments on commit 26a7df3

Please sign in to comment.