Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix a hot loop in DirectExchangeClient #22039

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@

import java.io.Closeable;
import java.net.URI;
import java.util.Deque;
import java.util.LinkedList;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
Expand Down Expand Up @@ -73,7 +73,7 @@ public class DirectExchangeClient
private final Map<URI, HttpPageBufferClient> allClients = new ConcurrentHashMap<>();

@GuardedBy("this")
private final Deque<HttpPageBufferClient> queuedClients = new LinkedList<>();
private final Set<HttpPageBufferClient> queuedClients = new LinkedHashSet<>();

private final Set<HttpPageBufferClient> completedClients = newConcurrentHashSet();
private final DirectExchangeBuffer buffer;
Expand Down Expand Up @@ -284,17 +284,22 @@ synchronized int scheduleRequestIfNecessary()
long projectedBytesToBeRequested = 0;
int clientCount = 0;

for (HttpPageBufferClient client : queuedClients) {
Iterator<HttpPageBufferClient> clientIterator = queuedClients.iterator();
while (clientIterator.hasNext()) {
HttpPageBufferClient client = clientIterator.next();
if (projectedBytesToBeRequested >= neededBytes * concurrentRequestMultiplier - reservedBytesForScheduledClients) {
break;
}
projectedBytesToBeRequested += client.getAverageRequestSizeInBytes();
clientCount++;
}
for (int i = 0; i < clientCount; i++) {
HttpPageBufferClient client = queuedClients.poll();

client.scheduleRequest();

// Remove the client from the queuedClient's set.
clientIterator.remove();

clientCount++;
}

return clientCount;
}

Expand All @@ -304,7 +309,7 @@ public ListenableFuture<Void> isBlocked()
}

@VisibleForTesting
Deque<HttpPageBufferClient> getQueuedClients()
Set<HttpPageBufferClient> getQueuedClients()
{
return queuedClients;
}
Expand Down