-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
Why Eureka Client used Synchronized Map for Instance Metadata #1424
Comments
While possible, I do wonder under which scenarios you'd get high contention accessing individual instance metadata in the client? A flame graph would help if you have one handy.
Well it's returned as a map, that's the only contract. I don't think there's an expectation of any sort of type information to be preserved (beyond primitive, array and object) via JSON/XML across different languages, or even within Java.
It's hard to say now, but I'd guess for simplicity's sake, I see no harm swapping this for any other thread safe implementation, if we can show a clear benefit. |
@troshko111 , thank you for your comment. Our scenario is a little tricky, we have a gateway and hundreds of instances for an upstream service, once a request came in, the gateway will try to filter the instances by iterate the instanceInfo list and get metatdata for each instanceInfo, and when the concurrent workload is high, we saw the locked instances of "java.util.Collections$SynchronizedMap" from JFR as many concurrent requests may try to get the metadata at the same time for a single instanceInfo. |
This I went over other usages, and see no explicit contract demanding a synchronized map, in fact xstream serializer uses a My conclusion is that this code is more likely to have a bug than not:
Looks to me this line
Does not seem like we do, also this uses the same type on the server and the client which makes it a bit unfortunate, as the server may actually modify where as the client should not. At any rate, the existing usage does not require a thread safe map there and already uses regular collections in some places, so I think we're good to change that line to use a regular map. |
We are using eureka client 1.x, and recently we found some threads were locked by "java.util.Collections$SynchronizedMap" which was happened while getting data from instanceInfo metadata under a high workload. It seems impact the performance. For the instance metadata, it's returned as a concurrentHashmap from server, but after deserialization, it turned to be a SynchronizedMap.
The below code is under EurekaJacksonCodec class, any reason why we are using synchronizedMap here?
builder.setMetadata(metadataMap == null ? Collections.emptyMap() : Collections.synchronizedMap(metadataMap));
The text was updated successfully, but these errors were encountered: