-
Notifications
You must be signed in to change notification settings - Fork 997
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: prevent long stall during reconnection/disconnection by providin…
…g LinkedHashSetQueue instead of ArrayDeque for CommandHandler#stack
- Loading branch information
Showing
4 changed files
with
479 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
254 changes: 254 additions & 0 deletions
254
src/main/java/io/lettuce/core/datastructure/queue/HashIndexedQueue.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,254 @@ | ||
package io.lettuce.core.datastructure.queue; | ||
|
||
import java.util.AbstractQueue; | ||
import java.util.ArrayList; | ||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.NoSuchElementException; | ||
import java.util.Queue; | ||
|
||
import io.lettuce.core.internal.LettuceAssert; | ||
|
||
/** | ||
* @author chenxiaofan | ||
*/ | ||
@SuppressWarnings("unchecked") | ||
public class HashIndexedQueue<E> extends AbstractQueue<E> { | ||
|
||
private final Map<E, Object> map; // Object can be Node<E> or List<Node<E>> | ||
|
||
private Node<E> head; | ||
|
||
private Node<E> tail; | ||
|
||
private int size; | ||
|
||
private static class Node<E> { | ||
|
||
E value; | ||
|
||
Node<E> next; | ||
|
||
Node<E> prev; | ||
|
||
Node(E value) { | ||
this.value = value; | ||
} | ||
|
||
} | ||
|
||
public HashIndexedQueue() { | ||
map = new HashMap<>(); | ||
size = 0; | ||
} | ||
|
||
@Override | ||
public boolean add(E e) { | ||
return offer(e); | ||
} | ||
|
||
@Override | ||
public boolean offer(E e) { | ||
final Node<E> newNode = new Node<>(e); | ||
if (tail == null) { | ||
head = tail = newNode; | ||
} else { | ||
tail.next = newNode; | ||
newNode.prev = tail; | ||
tail = newNode; | ||
} | ||
|
||
if (!map.containsKey(e)) { | ||
map.put(e, newNode); | ||
} else { | ||
Object current = map.get(e); | ||
if (current instanceof Node) { | ||
List<Node<E>> nodes = new ArrayList<>(); | ||
nodes.add((Node<E>) current); | ||
nodes.add(newNode); | ||
map.put(e, nodes); | ||
} else { | ||
((List<Node<E>>) current).add(newNode); | ||
} | ||
} | ||
size++; | ||
return true; | ||
} | ||
|
||
@Override | ||
public E poll() { | ||
if (head == null) { | ||
return null; | ||
} | ||
E value = head.value; | ||
removeNodeFromMap(head); | ||
head = head.next; | ||
if (head == null) { | ||
tail = null; | ||
} else { | ||
head.prev = null; | ||
} | ||
size--; | ||
return value; | ||
} | ||
|
||
@Override | ||
public E peek() { | ||
if (head == null) { | ||
return null; | ||
} | ||
return head.value; | ||
} | ||
|
||
@Override | ||
public boolean remove(Object o) { | ||
return removeFirstOccurrence(o); | ||
} | ||
|
||
@Override | ||
public int size() { | ||
return size; | ||
} | ||
|
||
@Override | ||
public boolean contains(Object o) { | ||
return map.containsKey(o); | ||
} | ||
|
||
public class Iterator implements java.util.Iterator<E> { | ||
|
||
private Node<E> current; | ||
|
||
private Node<E> prev; | ||
|
||
private Iterator() { | ||
current = HashIndexedQueue.this.head; | ||
prev = null; | ||
} | ||
|
||
@Override | ||
public boolean hasNext() { | ||
return current != null; | ||
} | ||
|
||
@Override | ||
public E next() { | ||
if (!hasNext()) { | ||
throw new NoSuchElementException(); | ||
} | ||
E value = current.value; | ||
prev = current; | ||
current = current.next; | ||
return value; | ||
} | ||
|
||
@Override | ||
public void remove() { | ||
if (prev != null) { | ||
removeNodeFromMap(prev); | ||
removeNode(prev); | ||
size--; | ||
// remove once | ||
prev = null; | ||
} | ||
} | ||
|
||
} | ||
|
||
@Override | ||
public Iterator iterator() { | ||
return new Iterator(); | ||
} | ||
|
||
@Override | ||
public boolean removeAll(Collection<?> c) { | ||
boolean modified = false; | ||
for (Object e : c) { | ||
if (removeAllOccurrences(e)) { | ||
modified = true; | ||
} | ||
} | ||
return modified; | ||
} | ||
|
||
@Override | ||
public void clear() { | ||
head = null; | ||
tail = null; | ||
map.clear(); | ||
size = 0; | ||
} | ||
|
||
private boolean removeFirstOccurrence(Object o) { | ||
Object current = map.get(o); | ||
if (current == null) { | ||
return false; | ||
} | ||
if (current instanceof Node) { | ||
Node<E> node = (Node<E>) current; | ||
removeNode(node); | ||
map.remove(o); | ||
} else { | ||
List<Node<E>> nodes = (List<Node<E>>) current; | ||
Node<E> node = nodes.remove(0); | ||
if (nodes.isEmpty()) { | ||
map.remove(o); | ||
} | ||
removeNode(node); | ||
} | ||
size--; | ||
return true; | ||
} | ||
|
||
private boolean removeAllOccurrences(Object o) { | ||
Object current = map.get(o); | ||
if (current == null) { | ||
return false; | ||
} | ||
if (current instanceof Node) { | ||
final Node<E> node = (Node<E>) current; | ||
removeNode(node); | ||
size--; | ||
} else { | ||
final List<Node<E>> nodes = (List<Node<E>>) current; | ||
for (Node<E> node : nodes) { | ||
removeNode(node); | ||
size--; | ||
} | ||
} | ||
map.remove(o); | ||
return true; | ||
} | ||
|
||
private void removeNode(Node<E> node) { | ||
if (node.prev != null) { | ||
node.prev.next = node.next; | ||
} else { | ||
head = node.next; | ||
} | ||
if (node.next != null) { | ||
node.next.prev = node.prev; | ||
} else { | ||
tail = node.prev; | ||
} | ||
} | ||
|
||
private void removeNodeFromMap(Node<E> node) { | ||
E value = node.value; | ||
Object current = map.get(value); | ||
if (current instanceof Node) { | ||
LettuceAssert.assertState(current == node, "current != node"); | ||
map.remove(value); | ||
} else { | ||
List<Node<E>> nodes = (List<Node<E>>) current; | ||
final boolean removed = nodes.remove(node); | ||
LettuceAssert.assertState(removed, "!nodes.remove(node)"); | ||
if (nodes.isEmpty()) { | ||
map.remove(value); | ||
} | ||
} | ||
} | ||
|
||
} |
Oops, something went wrong.