Skip to content
Open
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 @@ -115,6 +115,9 @@ private enum PoolState {
@GuardedBy("this")
private int consecutiveFailures = 0;

@GuardedBy("this")
private boolean isDraining = false;

/**
* When the client fallback to a non-session AFE session creation will return unimplemented
* errors. In which case the requests should fallback to classic client instead of waiting for an
Expand Down Expand Up @@ -525,17 +528,25 @@ private void onSessionClose(

@GuardedBy("this")
private void tryDrainPendingRpcs() {
while (!pendingRpcs.isEmpty()) {
if (pendingRpcs.peek().isCancelled) {
pendingRpcs.pop();
continue;
}
Optional<SessionHandle> handle = picker.pickSession();
if (!handle.isPresent()) {
break;
if (isDraining) {
return;
}
isDraining = true;
try {
while (!pendingRpcs.isEmpty()) {
if (pendingRpcs.peek().isCancelled) {
pendingRpcs.pop();
continue;
}
Optional<SessionHandle> handle = picker.pickSession();
if (!handle.isPresent()) {
break;
}
PendingVRpc<?, ?> rpc = pendingRpcs.removeFirst();
rpc.drainTo(handle.get());
}
PendingVRpc<?, ?> rpc = pendingRpcs.removeFirst();
rpc.drainTo(handle.get());
} finally {
isDraining = false;
}
}
Comment on lines +531 to 551
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation of tryDrainPendingRpcs performs synchronous calls to rpc.drainTo(handle.get()) while holding the lock on this. Calling external code while holding a monitor lock is discouraged as it can lead to deadlocks or extended lock contention. Furthermore, in performance-sensitive code like a session pool, prefer using explicit locks over the 'synchronized' keyword to protect shared state. Consider refactoring this to collect the PendingVRpc and SessionHandle pairs under an explicit lock, and then execute the drainTo calls outside of the locked section.

References
  1. In performance-sensitive code, prefer using explicit locks over the 'synchronized' keyword to protect shared state while ensuring thread safety and visibility.


Expand Down
Loading