-
Notifications
You must be signed in to change notification settings - Fork 930
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
Replace synchonized blocks in retrofit2 with ReentrantLock #4610
Replace synchonized blocks in retrofit2 with ReentrantLock #4610
Conversation
executionStateUpdater.compareAndSet(this, ExecutionState.IDLE, ExecutionState.RUNNING); | ||
httpResponse = doCall(callFactory, request); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is likely that httpResponse
can be atomically updated without a lock or synchronized
.
if (executionStateUpdater.compareAndSet(this, ExecutionState.IDLE, ExecutionState.RUNNING)) {
httpResponse = doCall(callFactory, request);
} else {
throw new IllegalStateException("executed already");
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so this method should be reverted to previous state then? just realised the volatile on httpResponse @ikhoon
It is likely that
httpResponse
can be atomically updated without a lock orsynchronized
.if (executionStateUpdater.compareAndSet(this, ExecutionState.IDLE, ExecutionState.RUNNING)) { httpResponse = doCall(callFactory, request); } else { throw new IllegalStateException("executed already"); }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
changes reverted.
It is likely that
httpResponse
can be atomically updated without a lock orsynchronized
.if (executionStateUpdater.compareAndSet(this, ExecutionState.IDLE, ExecutionState.RUNNING)) { httpResponse = doCall(callFactory, request); } else { throw new IllegalStateException("executed already"); }
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
so this method should be reverted to previous state then?
I meant to fix the code to use if
with compareAndSet
without ReentrantLock
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, no point calling lock/synchronized on a CAS operation. However this change
if (executionStateUpdater.compareAndSet(this, ExecutionState.IDLE, ExecutionState.RUNNING)) { httpResponse = doCall(callFactory, request); } else { throw new IllegalStateException("executed already"); }
caused a test failure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, it seems like we need to define a separate boolean variable to check if the response has been updated or not
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good to me once @ikhoon 's comment is addressed 👍
Can you take a look at CI failures?
@Nullable | ||
private Throwable sinkClosedException; | ||
|
||
void write(byte[] source, int offset, int byteCount) { | ||
if (byteCount == 0) { | ||
return; | ||
} | ||
synchronized (buffer) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems like okio
enforces users to use wait
/notifyAll
so we can't simply replace synchronized
. (causing test failure)
What do you think of reverting changes in this file?
square/okio#1169
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
got it. reverted changes in this file @jrhee17
It seems like
okio
enforces users to usewait
/notifyAll
so we can't simply replacesynchronized
. (causing test failure)What do you think of reverting changes in this file? square/okio#1169
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ikhoon @jrhee17 I am facing the issue highlighted here Task :examples:static-files:test FAILED locally. any hint/idea?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have a stacktrace of the failure?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
DefaultDnsCacheTest
seems like a flaky test so it could be ignored.- For
PropertiesEndpointGroupTest
, it is not related to your change. Could you try to retest after runninggradle clean
task?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks @ikhoon
- DefaultDnsCacheTest, apparently is flaky, not showing up anymore.
I have not been able to successfully build the master branch locally as these two failures below kept being persistent even after gradle clean
task
- PropertiesEndpointGroupTest. pathWithDefaultPort
- PropertiesEndpointGroupTest. pathWithoutDefaultPort
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see, so what shadedTest
does is basically
- Shade dependencies into a jar
- Unzip the jar into
"${project.buildDir}/classes/java/shaded-test"
- Run tests for classes in the above directory
Can you actually try to navigate to "${project.buildDir}/classes/java/shaded-test"
and compare the file location with the one printed in the error logs?
My guess is the percent encoded url is causing problems, but I'd like to make sure first. If this is indeed the issue, I think it's better to handle this in a separate PR.
After handling the following comment, I'd like to get this PR merged.
#4610 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
update) confirmed locally that the following patch fixes the issue.
- final Path resourcePath = new File(resourceUrl.getFile()).toPath();
+ final Path resourcePath = new File(resourceUrl.toURI().getPath()).toPath();
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The patch works! PR here #4617
This changeset is no-op. Should we close it? |
Let's do so 👍 |
Motivation:
loom compatibility: synchronized should be removed to favour virtual threads friendliness. Resolves part of #4510
Modifications: