alibaba / Sentinel

A powerful flow control component enabling reliability, resilience and monitoring for microservices. (面向云原生微服务的高可用流控防护组件)
https://sentinelguard.io/
Apache License 2.0
22.28k stars 7.99k forks source link

基于ConcurrentLinkedHashMap实现的CacheMap会导致在高CPU核数下,单个接口产生的ParameterMetric对象很大的问题 #2565

Open LeoMe1 opened 2 years ago

LeoMe1 commented 2 years ago

Issue Description

ConcurrentLinkedHashMap在构造二维数组时会根据CPU核心数来计算出当前数组的一维下标长度,当核数为96时,下标为128,此时单个ParameterMetric对象大小接近1M,如下图所示:

image

当接口调用量较大时,此处将会占用大量内存,sentinel是否有考虑过这块的内存使用,或者切成其他Cache?毕竟googlecode.concurrentlinkedhashmap作者也建议使用Caffeine了.

Tell us your environment

springboot 2.3.10 sentinel 1.7.2 springcloudgateway 2.2.6

Anything else we need to know?

sentinel在后续版本中是否有意向对这块进行修改?

sczyh30 commented 2 years ago

Refer https://github.com/alibaba/Sentinel/issues/1031#issuecomment-530674172

There are some legacy reasons for using concurrentlinkedhashmap. Since the min-supported JDK has been polished to 1.8, we may replace it with Caffeine. Contributions are welcomed!


这一块使用 concurrentlinkedhashmap 有历史原因,目前 Sentinel 支持的最低 JDK 版本为 1.8,因此可以考虑替换为 Caffeine. 欢迎社区一起讨论与贡献

Zhang-0952 commented 2 years ago

Could I fix it?

LeoMe1 commented 2 years ago

Could I fix it?

sure!

ben-manes commented 2 years ago

Caffeine fixes this by lazily growing an array of ring buffers based on contention (failed CAS operations). This saves memory if under low concurrency while scaling under high load. See Java’s LongAdder which does this for counters and was heavily borrowed from.