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

Cleanup usages of stopwatch #16478

Merged
merged 5 commits into from
May 27, 2024
Merged

Cleanup usages of stopwatch #16478

merged 5 commits into from
May 27, 2024

Conversation

kfaraz
Copy link
Contributor

@kfaraz kfaraz commented May 21, 2024

Description

The Druid Stopwatch currently exposes synchronized start() and stop() methods but this is not adequate as start() may still be called before stop() has been called throwing an exception This stopwatch is already running.

Moreover, there doesn't seem to be a typical use case of a Stopwatch that requires thread safety in the first place. In Druid, ChangeRequestHttpSyncer seems to be the only place that may use the stopwatch in an unsafe manner. All other usages simply create a fresh instance in a method.

All the usages in ChangeRequestHttpSyncer are always accessed inside a startStopLock. The two places which were not synchronized on the startStopLock have been updated to do so.

@kfaraz kfaraz requested a review from abhishekrb19 May 22, 2024 02:50
Copy link
Contributor

@abhishekrb19 abhishekrb19 left a comment

Choose a reason for hiding this comment

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

Thanks for the changes! The Stopwatch changes make sense to me. I left a few suggestions and questions on the usages of synchronized blocks in ChangeRequestHttpSyncer.

}

private void sync()
private void sendSyncRequest()
{
if (!startStopLock.awaitStarted(1, TimeUnit.MILLISECONDS)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Pretty much all the current usages of awaitStarted is preceded by synchronized (startStopLock). Curious why this one doesn't have. I see this runs in an executor thread, but I'm wondering if the new synchronized block added below in line 242 should be moved up here.

Copy link
Contributor Author

@kfaraz kfaraz May 23, 2024

Choose a reason for hiding this comment

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

I am not entirely sure if the startStopLock is really being used in the correct way in this entire class. Calling various methods on this lock (LifecycleLock) doesn't require synchronizing on it. So left this as is until we plan to revisit this code.

@@ -410,6 +420,7 @@ private void addNextSyncToWorkQueue()
}
}

@GuardedBy("startStopLock")
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it be better to just have the lock synchronization in this method directly instead of relying on the different callers to do the right thing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, fair enough. I guess the only part which really needs the synchronization is the update of the failed attempt count and the stopwatch reset.

Btw, @GuardedBy forces the callers to do the right thing as it throws a compile-time error if not called from within the proper synchronization.

kfaraz and others added 2 commits May 23, 2024 09:36
Co-authored-by: Abhishek Radhakrishnan <abhishek.rb19@gmail.com>
@kfaraz kfaraz merged commit 9d77ef0 into apache:master May 27, 2024
87 checks passed
@kfaraz
Copy link
Contributor Author

kfaraz commented May 27, 2024

Thanks for the review, @abhishekrb19 !

@kfaraz kfaraz deleted the cleanup_stopwatch branch May 27, 2024 17:38
ektravel pushed a commit to ektravel/druid that referenced this pull request May 29, 2024
Changes:
- Remove synchronized methods from `Stopwatch`
- Access stopwatch methods in `ChangeRequestHttpSyncer` inside a lock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants