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

Support concurrent behavior on MetricsRetryListener #467

Closed
huisam opened this issue Sep 10, 2024 · 4 comments · Fixed by #468
Closed

Support concurrent behavior on MetricsRetryListener #467

huisam opened this issue Sep 10, 2024 · 4 comments · Fixed by #468
Labels
Milestone

Comments

@huisam
Copy link
Contributor

huisam commented Sep 10, 2024

Hello Spring team. I was glad to hear that official support MetricsRetryListener class by this issue #458

But I have a question about this class behavior.

Question

The retryContextToSample field is considered for reusable many retry operations.
And the RetryListener can be registered by bean from RetryConfiguration class.

If the MetricsRetryListener registered by bean, the MetricsRetryListener can called by concurrent retry operations.

In conclusion, my question is why retryContextToSample was defined to IdentityHashMap ?

private final Map<RetryContext, Timer.Sample> retryContextToSample = new IdentityHashMap<>();

On IdentityHashMap javadoc, it doesn't support thread safe behavior.

Suggestion

Can you support the thread safe behavior from the retryContextToSample?

private final Map<RetryContext, Timer.Sample> retryContextToSample = new ConcurrentHashMap<>();

Discussion

Please feel free to comment on this issue. If my suggestion is wrong, then I will close this issue.

@artembilan artembilan added this to the 2.0.9 milestone Sep 10, 2024
@artembilan
Copy link
Member

See discussion in the PR how we have introduced an IdentityHashMap: #459 (comment).
Well, since it complains that put/remove are not thread-safe, than I'll wrap it into a Lock.

Thank you for pointing that out!

@artembilan artembilan added the bug label Sep 10, 2024
@artembilan
Copy link
Member

After thinking one more time, the Collections.synchronizedMap() wrapping would be enough: we just don't do any IO operations over there to worry about platform thread pinning.

Feel free to contribute the fix!

@huisam
Copy link
Contributor Author

huisam commented Sep 11, 2024

I see. I understood about the IdentityHashMap background.
I think the same way you think. It will be enough to wrapping the IdentityHashMap by Collections.synchronizedMap for supporting concurrent behavior.

Then I will submit the pull request, please review the pull request for next release!

@huisam huisam changed the title Does MetricsRetryListener can be thread safe? Support MetricsRetryListener concurrent behavior Sep 11, 2024
@huisam huisam changed the title Support MetricsRetryListener concurrent behavior Support concurrent behavior on MetricsRetryListener Sep 11, 2024
huisam added a commit to huisam/spring-retry that referenced this issue Sep 11, 2024
huisam added a commit to huisam/spring-retry that referenced this issue Sep 11, 2024
@basovnik
Copy link

@artembilan I am facing the issue that should be fixed by this commit. When are you going to release it?

j.l.IllegalStateException: No 'Timer.Sample' registered for '[RetryContext: count=0, lastException=null, exhausted=false]'. Was the 'open()' called?
	at o.s.util.Assert.state(Assert.java:97)
	at o.s.r.s.MetricsRetryListener.close(MetricsRetryListener.java:113)
	at o.s.r.s.RetryTemplate.doCloseInterceptors(RetryTemplate.java:619)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants