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

[Feature] ThreadlessExecutor waitAndDrain method seems to not match its annotation. #14119

Open
4 tasks done
CycleMaker opened this issue Apr 22, 2024 · 1 comment
Open
4 tasks done
Labels
component/need-triage Need maintainers to triage type/need-triage Need maintainers to triage

Comments

@CycleMaker
Copy link
Contributor

Pre-check

  • I am sure that all the content I provide is in English.

Search before asking

  • I had searched in the issues and found no similar feature requirement.

Apache Dubbo Component

Java SDK (apache/dubbo)

Descriptions

This is the source code of the ThreadlessExecutor waitAndDrain method:

/**
* Waits until there is a task, executes the task and all queued tasks (if there're any). The task is either a normal
* response or a timeout response.
*/
public void waitAndDrain(long deadline) throws InterruptedException {
throwIfInterrupted();
Runnable runnable = queue.poll();
if (runnable == null) {
if (waiter.compareAndSet(null, Thread.currentThread())) {
try {
while ((runnable = queue.poll()) == null && waiter.get() == Thread.currentThread()) {
long restTime = deadline - System.nanoTime();
if (restTime <= 0) {
return;
}
LockSupport.parkNanos(this, restTime);
throwIfInterrupted();
}
} finally {
waiter.compareAndSet(Thread.currentThread(), null);
}
}
}
do {
if (runnable != null) {
runnable.run();
}
} while ((runnable = queue.poll()) != null);
}

It seems that if waitAndDrain is called twice in a multi-threaded scenario, there will be no wait on the second execution because if the cas fails, it will return. Here is my test code:

@test
void testV2() throws InterruptedException {
new Thread(()->{
try {
executor.waitAndDrain(Long.MAX_VALUE);
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
}).start();
executor.waitAndDrain(Long.MAX_VALUE);
System.out.println("wait not worked");
}

There is no example in Dubbo where waitAndDrain is executed through the same threadlessExecutor across multiple threads, so there shouldn't be any issues. I don't know if this is a bug.

Related issues

No response

Are you willing to submit a pull request to fix on your own?

  • Yes I am willing to submit a pull request on my own!

Code of Conduct

@CycleMaker CycleMaker added component/need-triage Need maintainers to triage type/need-triage Need maintainers to triage labels Apr 22, 2024
@AlbumenJ
Copy link
Member

AlbumenJ commented May 8, 2024

Yes, it has been updated since Triple protocol introduced. You're welcome to update the comment here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
component/need-triage Need maintainers to triage type/need-triage Need maintainers to triage
Projects
Status: Todo
Development

No branches or pull requests

2 participants