From d0af6e060304ecefe91f03d031f2be6978ab1a7e Mon Sep 17 00:00:00 2001 From: yanshaoqiang Date: Mon, 12 Sep 2022 21:54:02 +0800 Subject: [PATCH 1/3] use `caffeine` instead of `ConcurrentLinkedHashMap` (#2871) --- pom.xml | 6 + .../metric/ClusterParameterLeapArray.java | 4 +- .../sentinel-parameter-flow-control/pom.xml | 5 + .../block/flow/param/ParameterMetric.java | 11 +- .../cache/CaffeineCacheMapWrapper.java | 104 ++++++++++++++++++ .../slots/statistic/data/ParamMapBucket.java | 9 +- .../flow/param/ParamFlowCheckerTest.java | 15 ++- .../param/ParamFlowDefaultCheckerTest.java | 23 ++-- .../block/flow/param/ParamFlowSlotTest.java | 9 +- ...amFlowThrottleRateLimitingCheckerTest.java | 6 +- .../statistic/data/ParamMapBucketTest.java | 29 ++++- 11 files changed, 184 insertions(+), 37 deletions(-) create mode 100644 sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/statistic/cache/CaffeineCacheMapWrapper.java diff --git a/pom.xml b/pom.xml index a2ac3fd7b5..c07f137d34 100755 --- a/pom.xml +++ b/pom.xml @@ -43,6 +43,7 @@ 1.2.83_noneautotype 1.3.2 + 2.9.3 4.12 @@ -219,6 +220,11 @@ ${powermock.version} test + + com.github.ben-manes.caffeine + caffeine + ${caffeine.version} + diff --git a/sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/flow/statistic/metric/ClusterParameterLeapArray.java b/sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/flow/statistic/metric/ClusterParameterLeapArray.java index 2b5fabc4e7..b71b737b80 100644 --- a/sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/flow/statistic/metric/ClusterParameterLeapArray.java +++ b/sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/flow/statistic/metric/ClusterParameterLeapArray.java @@ -18,7 +18,7 @@ import com.alibaba.csp.sentinel.slots.statistic.base.LeapArray; import com.alibaba.csp.sentinel.slots.statistic.base.WindowWrap; import com.alibaba.csp.sentinel.slots.statistic.cache.CacheMap; -import com.alibaba.csp.sentinel.slots.statistic.cache.ConcurrentLinkedHashMapWrapper; +import com.alibaba.csp.sentinel.slots.statistic.cache.CaffeineCacheMapWrapper; import com.alibaba.csp.sentinel.util.AssertUtil; /** @@ -38,7 +38,7 @@ public ClusterParameterLeapArray(int sampleCount, int intervalInMs, int maxCapac @Override public CacheMap newEmptyBucket(long timeMillis) { - return new ConcurrentLinkedHashMapWrapper<>(maxCapacity); + return new CaffeineCacheMapWrapper<>(maxCapacity); } @Override diff --git a/sentinel-extension/sentinel-parameter-flow-control/pom.xml b/sentinel-extension/sentinel-parameter-flow-control/pom.xml index 2c96d84aae..68ef15d830 100644 --- a/sentinel-extension/sentinel-parameter-flow-control/pom.xml +++ b/sentinel-extension/sentinel-parameter-flow-control/pom.xml @@ -29,6 +29,11 @@ 1.4.2 + + com.github.ben-manes.caffeine + caffeine + + junit junit diff --git a/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParameterMetric.java b/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParameterMetric.java index ee29728a37..d802c4dd73 100644 --- a/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParameterMetric.java +++ b/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParameterMetric.java @@ -24,6 +24,7 @@ import com.alibaba.csp.sentinel.log.RecordLog; import com.alibaba.csp.sentinel.slots.statistic.cache.CacheMap; +import com.alibaba.csp.sentinel.slots.statistic.cache.CaffeineCacheMapWrapper; import com.alibaba.csp.sentinel.slots.statistic.cache.ConcurrentLinkedHashMapWrapper; /** @@ -93,11 +94,15 @@ public void clearForRule(ParamFlowRule rule) { } public void initialize(ParamFlowRule rule) { + initialize(rule, true); + } + + public void initialize(ParamFlowRule rule, boolean useCaffeine) { if (!ruleTimeCounters.containsKey(rule)) { synchronized (lock) { if (ruleTimeCounters.get(rule) == null) { long size = Math.min(BASE_PARAM_MAX_CAPACITY * rule.getDurationInSec(), TOTAL_MAX_CAPACITY); - ruleTimeCounters.put(rule, new ConcurrentLinkedHashMapWrapper(size)); + ruleTimeCounters.put(rule, useCaffeine ? new CaffeineCacheMapWrapper<>(size) : new ConcurrentLinkedHashMapWrapper<>(size)); } } } @@ -106,7 +111,7 @@ public void initialize(ParamFlowRule rule) { synchronized (lock) { if (ruleTokenCounter.get(rule) == null) { long size = Math.min(BASE_PARAM_MAX_CAPACITY * rule.getDurationInSec(), TOTAL_MAX_CAPACITY); - ruleTokenCounter.put(rule, new ConcurrentLinkedHashMapWrapper(size)); + ruleTokenCounter.put(rule, useCaffeine ? new CaffeineCacheMapWrapper<>(size) : new ConcurrentLinkedHashMapWrapper<>(size)); } } } @@ -115,7 +120,7 @@ public void initialize(ParamFlowRule rule) { synchronized (lock) { if (threadCountMap.get(rule.getParamIdx()) == null) { threadCountMap.put(rule.getParamIdx(), - new ConcurrentLinkedHashMapWrapper(THREAD_COUNT_MAX_CAPACITY)); + useCaffeine ? new CaffeineCacheMapWrapper<>(THREAD_COUNT_MAX_CAPACITY) : new ConcurrentLinkedHashMapWrapper<>(THREAD_COUNT_MAX_CAPACITY)); } } } diff --git a/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/statistic/cache/CaffeineCacheMapWrapper.java b/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/statistic/cache/CaffeineCacheMapWrapper.java new file mode 100644 index 0000000000..2dd19e4dd5 --- /dev/null +++ b/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/statistic/cache/CaffeineCacheMapWrapper.java @@ -0,0 +1,104 @@ +/* + * Copyright 1999-2018 Alibaba Group Holding Ltd. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.alibaba.csp.sentinel.slots.statistic.cache; + +import com.github.benmanes.caffeine.cache.Cache; +import com.github.benmanes.caffeine.cache.Caffeine; + +import java.util.Comparator; +import java.util.LinkedHashSet; +import java.util.Set; +import java.util.concurrent.ConcurrentMap; +import java.util.stream.Collectors; + +/** + * A {@link Cache} wrapper for the universal {@link CacheMap}. + * + * @author shaoqiangyan + * + */ +public class CaffeineCacheMapWrapper implements CacheMap { + + + private final Cache map; + + public CaffeineCacheMapWrapper(long size) { + if (size <= 0) { + throw new IllegalArgumentException("Cache max capacity should be positive: " + size); + } + this.map = Caffeine.newBuilder() + .maximumSize(size) + .build(); + } + + public CaffeineCacheMapWrapper(Cache map) { + if (map == null) { + throw new IllegalArgumentException("Invalid map instance"); + } + this.map = map; + } + + @Override + public boolean containsKey(T key) { + return asMap().containsKey(key); + } + + @Override + public R get(T key) { + return map.getIfPresent(key); + } + + @Override + public R remove(T key) { + return asMap().remove(key); + } + + @Override + public R put(T key, R value) { + return asMap().put(key, value); + } + + @Override + public R putIfAbsent(T key, R value) { + return asMap().putIfAbsent(key, value); + } + + @Override + public long size() { + return asMap().size(); + } + + @Override + public void clear() { + map.invalidateAll(); + } + + @Override + @SuppressWarnings("unchecked") + public Set keySet(boolean ascending) { + Comparator comparator; + if (ascending) { + comparator = (Comparator) Comparator.naturalOrder(); + } else { + comparator = (Comparator) Comparator.reverseOrder(); + } + return asMap().keySet().stream().sorted(comparator).collect(Collectors.toCollection(LinkedHashSet::new)); + } + + private ConcurrentMap asMap() { + return map.asMap(); + } +} diff --git a/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/statistic/data/ParamMapBucket.java b/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/statistic/data/ParamMapBucket.java index 24e1d1d055..52a5b4499a 100644 --- a/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/statistic/data/ParamMapBucket.java +++ b/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/statistic/data/ParamMapBucket.java @@ -20,6 +20,7 @@ import com.alibaba.csp.sentinel.slots.block.flow.param.RollingParamEvent; import com.alibaba.csp.sentinel.slots.statistic.cache.CacheMap; +import com.alibaba.csp.sentinel.slots.statistic.cache.CaffeineCacheMapWrapper; import com.alibaba.csp.sentinel.slots.statistic.cache.ConcurrentLinkedHashMapWrapper; import com.alibaba.csp.sentinel.util.AssertUtil; @@ -38,15 +39,19 @@ public ParamMapBucket() { } @SuppressWarnings("unchecked") - public ParamMapBucket(int capacity) { + public ParamMapBucket(int capacity, boolean useCaffeine) { AssertUtil.isTrue(capacity > 0, "capacity should be positive"); RollingParamEvent[] events = RollingParamEvent.values(); this.data = new CacheMap[events.length]; for (RollingParamEvent event : events) { - data[event.ordinal()] = new ConcurrentLinkedHashMapWrapper(capacity); + data[event.ordinal()] = useCaffeine ? new CaffeineCacheMapWrapper<>(capacity) : new ConcurrentLinkedHashMapWrapper<>(capacity); } } + public ParamMapBucket(int capacity) { + this(capacity, true); + } + public void reset() { for (RollingParamEvent event : RollingParamEvent.values()) { data[event.ordinal()].clear(); diff --git a/sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowCheckerTest.java b/sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowCheckerTest.java index bc545c6f84..72a8bd64ad 100644 --- a/sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowCheckerTest.java +++ b/sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowCheckerTest.java @@ -19,7 +19,7 @@ import com.alibaba.csp.sentinel.slotchain.ResourceWrapper; import com.alibaba.csp.sentinel.slotchain.StringResourceWrapper; import com.alibaba.csp.sentinel.slots.block.RuleConstant; -import com.alibaba.csp.sentinel.slots.statistic.cache.ConcurrentLinkedHashMapWrapper; +import com.alibaba.csp.sentinel.slots.statistic.cache.CaffeineCacheMapWrapper; import com.alibaba.csp.sentinel.util.TimeUtil; import org.junit.After; import org.junit.Before; @@ -30,7 +30,6 @@ import java.util.List; import java.util.Map; import java.util.concurrent.TimeUnit; -import java.util.concurrent.atomic.AtomicLong; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertTrue; @@ -89,7 +88,7 @@ public void testSingleValueCheckQpsWithExceptionItems() throws InterruptedExcept ParameterMetric metric = new ParameterMetric(); ParameterMetricStorage.getMetricsMap().put(resourceWrapper.getName(), metric); - metric.getRuleTimeCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper(4000)); + metric.getRuleTimeCounterMap().put(rule, new CaffeineCacheMapWrapper<>(4000)); assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA)); assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueB)); @@ -157,8 +156,8 @@ public void testPassLocalCheckForCollection() throws InterruptedException { List list = Arrays.asList(v1, v2, v3); ParameterMetric metric = new ParameterMetric(); ParameterMetricStorage.getMetricsMap().put(resourceWrapper.getName(), metric); - metric.getRuleTimeCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper(4000)); - metric.getRuleTokenCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper(4000)); + metric.getRuleTimeCounterMap().put(rule, new CaffeineCacheMapWrapper<>(4000)); + metric.getRuleTokenCounterMap().put(rule, new CaffeineCacheMapWrapper<>(4000)); assertTrue(ParamFlowChecker.passCheck(resourceWrapper, rule, 1, list)); assertFalse(ParamFlowChecker.passCheck(resourceWrapper, rule, 1, list)); @@ -180,7 +179,7 @@ public void testPassLocalCheckForArray() throws InterruptedException { Object arr = new String[]{v1, v2, v3}; ParameterMetric metric = new ParameterMetric(); ParameterMetricStorage.getMetricsMap().put(resourceWrapper.getName(), metric); - metric.getRuleTimeCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper(4000)); + metric.getRuleTimeCounterMap().put(rule, new CaffeineCacheMapWrapper<>(4000)); assertTrue(ParamFlowChecker.passCheck(resourceWrapper, rule, 1, arr)); assertFalse(ParamFlowChecker.passCheck(resourceWrapper, rule, 1, arr)); @@ -214,8 +213,8 @@ public Object paramFlowKey() { Object[] args = new Object[]{new User(1, "Bob", "Hangzhou"), 10, "Demo"}; ParameterMetric metric = new ParameterMetric(); ParameterMetricStorage.getMetricsMap().put(resourceWrapper.getName(), metric); - metric.getRuleTimeCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper(4000)); - metric.getRuleTokenCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper(4000)); + metric.getRuleTimeCounterMap().put(rule, new CaffeineCacheMapWrapper<>(4000)); + metric.getRuleTokenCounterMap().put(rule, new CaffeineCacheMapWrapper<>(4000)); assertTrue(ParamFlowChecker.passCheck(resourceWrapper, rule, 1, args)); assertFalse(ParamFlowChecker.passCheck(resourceWrapper, rule, 1, args)); diff --git a/sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowDefaultCheckerTest.java b/sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowDefaultCheckerTest.java index 581c8522fd..bdd0b94ece 100644 --- a/sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowDefaultCheckerTest.java +++ b/sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowDefaultCheckerTest.java @@ -23,7 +23,6 @@ import java.util.concurrent.ThreadLocalRandom; import java.util.concurrent.TimeUnit; import java.util.concurrent.atomic.AtomicInteger; -import java.util.concurrent.atomic.AtomicLong; import org.junit.After; import org.junit.Before; @@ -32,7 +31,7 @@ import com.alibaba.csp.sentinel.EntryType; import com.alibaba.csp.sentinel.slotchain.ResourceWrapper; import com.alibaba.csp.sentinel.slotchain.StringResourceWrapper; -import com.alibaba.csp.sentinel.slots.statistic.cache.ConcurrentLinkedHashMapWrapper; +import com.alibaba.csp.sentinel.slots.statistic.cache.CaffeineCacheMapWrapper; import com.alibaba.csp.sentinel.test.AbstractTimeBasedTest; import com.alibaba.csp.sentinel.util.TimeUtil; @@ -59,9 +58,9 @@ public void testCheckQpsWithLongIntervalAndHighThreshold() { String valueA = "valueA"; ParameterMetric metric = new ParameterMetric(); ParameterMetricStorage.getMetricsMap().put(resourceWrapper.getName(), metric); - metric.getRuleTimeCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper(4000)); + metric.getRuleTimeCounterMap().put(rule, new CaffeineCacheMapWrapper<>(4000)); metric.getRuleTokenCounterMap().put(rule, - new ConcurrentLinkedHashMapWrapper(4000)); + new CaffeineCacheMapWrapper<>(4000)); // We mock the time directly to avoid unstable behaviour. setCurrentMillis(System.currentTimeMillis()); @@ -97,9 +96,9 @@ public void testParamFlowDefaultCheckSingleQps() { String valueA = "valueA"; ParameterMetric metric = new ParameterMetric(); ParameterMetricStorage.getMetricsMap().put(resourceWrapper.getName(), metric); - metric.getRuleTimeCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper(4000)); + metric.getRuleTimeCounterMap().put(rule, new CaffeineCacheMapWrapper<>(4000)); metric.getRuleTokenCounterMap().put(rule, - new ConcurrentLinkedHashMapWrapper(4000)); + new CaffeineCacheMapWrapper<>(4000)); // We mock the time directly to avoid unstable behaviour. setCurrentMillis(System.currentTimeMillis()); @@ -137,9 +136,9 @@ public void testParamFlowDefaultCheckSingleQpsWithBurst() throws InterruptedExce String valueA = "valueA"; ParameterMetric metric = new ParameterMetric(); ParameterMetricStorage.getMetricsMap().put(resourceWrapper.getName(), metric); - metric.getRuleTimeCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper(4000)); + metric.getRuleTimeCounterMap().put(rule, new CaffeineCacheMapWrapper<>(4000)); metric.getRuleTokenCounterMap().put(rule, - new ConcurrentLinkedHashMapWrapper(4000)); + new CaffeineCacheMapWrapper<>(4000)); // We mock the time directly to avoid unstable behaviour. setCurrentMillis(System.currentTimeMillis()); @@ -207,9 +206,9 @@ public void testParamFlowDefaultCheckQpsInDifferentDuration() throws Interrupted String valueA = "helloWorld"; ParameterMetric metric = new ParameterMetric(); ParameterMetricStorage.getMetricsMap().put(resourceWrapper.getName(), metric); - metric.getRuleTimeCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper(4000)); + metric.getRuleTimeCounterMap().put(rule, new CaffeineCacheMapWrapper<>(4000)); metric.getRuleTokenCounterMap().put(rule, - new ConcurrentLinkedHashMapWrapper(4000)); + new CaffeineCacheMapWrapper<>(4000)); // We mock the time directly to avoid unstable behaviour. setCurrentMillis(System.currentTimeMillis()); @@ -260,9 +259,9 @@ public void testParamFlowDefaultCheckSingleValueCheckQpsMultipleThreads() throws final String valueA = "valueA"; ParameterMetric metric = new ParameterMetric(); ParameterMetricStorage.getMetricsMap().put(resourceWrapper.getName(), metric); - metric.getRuleTimeCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper(4000)); + metric.getRuleTimeCounterMap().put(rule, new CaffeineCacheMapWrapper<>(4000)); metric.getRuleTokenCounterMap().put(rule, - new ConcurrentLinkedHashMapWrapper(4000)); + new CaffeineCacheMapWrapper<>(4000)); int threadCount = 40; final CountDownLatch waitLatch = new CountDownLatch(threadCount); diff --git a/sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowSlotTest.java b/sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowSlotTest.java index aadef74abd..91e00448b7 100644 --- a/sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowSlotTest.java +++ b/sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowSlotTest.java @@ -16,17 +16,15 @@ package com.alibaba.csp.sentinel.slots.block.flow.param; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertNull; -import static org.junit.Assert.assertSame; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; import java.util.Collections; -import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; +import com.alibaba.csp.sentinel.slots.statistic.cache.CaffeineCacheMapWrapper; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -35,7 +33,6 @@ import com.alibaba.csp.sentinel.slotchain.ResourceWrapper; import com.alibaba.csp.sentinel.slotchain.StringResourceWrapper; import com.alibaba.csp.sentinel.slots.statistic.cache.CacheMap; -import com.alibaba.csp.sentinel.slots.statistic.cache.ConcurrentLinkedHashMapWrapper; import com.alibaba.csp.sentinel.util.TimeUtil; /** @@ -99,8 +96,8 @@ public void testEntryWhenParamFlowExists() throws Throwable { ParameterMetric metric = mock(ParameterMetric.class); - CacheMap map = new ConcurrentLinkedHashMapWrapper<>(4000); - CacheMap map2 = new ConcurrentLinkedHashMapWrapper<>(4000); + CacheMap map = new CaffeineCacheMapWrapper<>(4000); + CacheMap map2 = new CaffeineCacheMapWrapper<>(4000); when(metric.getRuleTimeCounter(rule)).thenReturn(map); when(metric.getRuleTokenCounter(rule)).thenReturn(map2); map.put(argToGo, new AtomicLong(TimeUtil.currentTimeMillis())); diff --git a/sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowThrottleRateLimitingCheckerTest.java b/sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowThrottleRateLimitingCheckerTest.java index 8d9cfde48a..02454503ba 100644 --- a/sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowThrottleRateLimitingCheckerTest.java +++ b/sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParamFlowThrottleRateLimitingCheckerTest.java @@ -21,6 +21,7 @@ import java.util.concurrent.atomic.AtomicInteger; import java.util.concurrent.atomic.AtomicLong; +import com.alibaba.csp.sentinel.slots.statistic.cache.CaffeineCacheMapWrapper; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -29,7 +30,6 @@ import com.alibaba.csp.sentinel.slotchain.ResourceWrapper; import com.alibaba.csp.sentinel.slotchain.StringResourceWrapper; import com.alibaba.csp.sentinel.slots.block.RuleConstant; -import com.alibaba.csp.sentinel.slots.statistic.cache.ConcurrentLinkedHashMapWrapper; import com.alibaba.csp.sentinel.util.TimeUtil; import static org.junit.Assert.assertEquals; @@ -57,7 +57,7 @@ public void testSingleValueThrottleCheckQps() throws Exception { String valueA = "valueA"; ParameterMetric metric = new ParameterMetric(); ParameterMetricStorage.getMetricsMap().put(resourceWrapper.getName(), metric); - metric.getRuleTimeCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper(4000)); + metric.getRuleTimeCounterMap().put(rule, new CaffeineCacheMapWrapper(4000)); long currentTime = TimeUtil.currentTimeMillis(); long endTime = currentTime + rule.getDurationInSec() * 1000; @@ -101,7 +101,7 @@ public void testSingleValueThrottleCheckQpsMultipleThreads() throws Exception { final String valueA = "valueA"; ParameterMetric metric = new ParameterMetric(); ParameterMetricStorage.getMetricsMap().put(resourceWrapper.getName(), metric); - metric.getRuleTimeCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper(4000)); + metric.getRuleTimeCounterMap().put(rule, new CaffeineCacheMapWrapper(4000)); int threadCount = 40; System.out.println(metric.getRuleTimeCounter(rule)); diff --git a/sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/statistic/data/ParamMapBucketTest.java b/sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/statistic/data/ParamMapBucketTest.java index 5935b81982..b4e858c3d5 100644 --- a/sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/statistic/data/ParamMapBucketTest.java +++ b/sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/statistic/data/ParamMapBucketTest.java @@ -19,6 +19,10 @@ import org.junit.Test; +import java.util.Arrays; +import java.util.List; +import java.util.Set; + import static org.junit.Assert.*; /** @@ -37,7 +41,7 @@ public void testAddEviction() { } String lastParam = "param-end"; bucket.add(RollingParamEvent.REQUEST_PASSED, 1, lastParam); - assertEquals(0, bucket.get(RollingParamEvent.REQUEST_PASSED, "param-0")); + assertEquals(0, bucket.get(RollingParamEvent.REQUEST_PASSED, "param-00")); assertEquals(1, bucket.get(RollingParamEvent.REQUEST_PASSED, "param-1")); assertEquals(1, bucket.get(RollingParamEvent.REQUEST_PASSED, lastParam)); } @@ -74,4 +78,27 @@ public void testAddGetResetCommon() { assertEquals(0, bucket.get(RollingParamEvent.REQUEST_PASSED, paramB)); assertEquals(0, bucket.get(RollingParamEvent.REQUEST_PASSED, paramC)); } + + @Test + public void testOrder() { + ParamMapBucket bucket = new ParamMapBucket(); + double paramA = 1.1d; + double paramB = 2.2d; + double paramC = -3.2d; + bucket.add(RollingParamEvent.REQUEST_PASSED, 3, paramA); + bucket.add(RollingParamEvent.REQUEST_PASSED, 1, paramB); + bucket.add(RollingParamEvent.REQUEST_PASSED, 1, paramC); + Set ascSet = bucket.ascendingKeySet(RollingParamEvent.REQUEST_PASSED); + List ascList = Arrays.asList(paramC, paramA, paramB); + int i = 0; + for(Object o : ascSet) { + assertEquals(ascList.get(i++), o); + } + List descList = Arrays.asList(paramB, paramA, paramC); + Set descSet = bucket.descendingKeySet(RollingParamEvent.REQUEST_PASSED); + i = 0; + for(Object o : descSet) { + assertEquals(descList.get(i++), o); + } + } } \ No newline at end of file From 4df261f31107ed81fb1f3f4a73cddcc9dbb38cd1 Mon Sep 17 00:00:00 2001 From: yanshaoqiang Date: Thu, 15 Sep 2022 10:31:08 +0800 Subject: [PATCH 2/3] fix: testAddEviction use sleep --- .../sentinel/slots/statistic/data/ParamMapBucketTest.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/statistic/data/ParamMapBucketTest.java b/sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/statistic/data/ParamMapBucketTest.java index b4e858c3d5..075ccddc2f 100644 --- a/sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/statistic/data/ParamMapBucketTest.java +++ b/sentinel-extension/sentinel-parameter-flow-control/src/test/java/com/alibaba/csp/sentinel/slots/statistic/data/ParamMapBucketTest.java @@ -22,6 +22,7 @@ import java.util.Arrays; import java.util.List; import java.util.Set; +import java.util.concurrent.TimeUnit; import static org.junit.Assert.*; @@ -34,14 +35,15 @@ public class ParamMapBucketTest { @Test - public void testAddEviction() { + public void testAddEviction() throws InterruptedException { ParamMapBucket bucket = new ParamMapBucket(); for (int i = 0; i < ParamMapBucket.DEFAULT_MAX_CAPACITY; i++) { bucket.add(RollingParamEvent.REQUEST_PASSED, 1, "param-" + i); } String lastParam = "param-end"; bucket.add(RollingParamEvent.REQUEST_PASSED, 1, lastParam); - assertEquals(0, bucket.get(RollingParamEvent.REQUEST_PASSED, "param-00")); + TimeUnit.MILLISECONDS.sleep(10L); + assertEquals(0, bucket.get(RollingParamEvent.REQUEST_PASSED, "param-0")); assertEquals(1, bucket.get(RollingParamEvent.REQUEST_PASSED, "param-1")); assertEquals(1, bucket.get(RollingParamEvent.REQUEST_PASSED, lastParam)); } From ab6385b2c74060c5697e783d7e1d2ecb12ca8795 Mon Sep 17 00:00:00 2001 From: yanshaoqiang Date: Tue, 27 Sep 2022 15:25:14 +0800 Subject: [PATCH 3/3] use caffeine instead of concurrentlinkedhashmap-lru --- .../statistic/concurrent/TokenCacheNode.java | 2 +- .../concurrent/TokenCacheNodeManager.java | 17 ++-- .../expire/RegularExpireStrategy.java | 6 +- .../concurrent/TokenCacheNodeManagerTest.java | 2 - .../sentinel-parameter-flow-control/pom.xml | 6 -- .../block/flow/param/ParameterMetric.java | 11 +-- .../slots/statistic/cache/CacheMap.java | 4 + .../cache/ConcurrentLinkedHashMapWrapper.java | 96 ------------------- .../slots/statistic/data/ParamMapBucket.java | 9 +- 9 files changed, 19 insertions(+), 134 deletions(-) delete mode 100644 sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/statistic/cache/ConcurrentLinkedHashMapWrapper.java diff --git a/sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/flow/statistic/concurrent/TokenCacheNode.java b/sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/flow/statistic/concurrent/TokenCacheNode.java index be734ad306..3bf7fbd3e5 100644 --- a/sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/flow/statistic/concurrent/TokenCacheNode.java +++ b/sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/flow/statistic/concurrent/TokenCacheNode.java @@ -21,7 +21,7 @@ /** * We use TokenCacheNodeManager to store the tokenId, whose the underlying storage structure - * is ConcurrentLinkedHashMap, Its storage node is TokenCacheNode. In order to operate the nowCalls value when + * is Caffeine, Its storage node is TokenCacheNode. In order to operate the nowCalls value when * the expired tokenId is deleted regularly, we need to store the flowId in TokenCacheNode. * * @author yunfeiyanggzq diff --git a/sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/flow/statistic/concurrent/TokenCacheNodeManager.java b/sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/flow/statistic/concurrent/TokenCacheNodeManager.java index 890f62e6da..2fe3ef5c16 100644 --- a/sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/flow/statistic/concurrent/TokenCacheNodeManager.java +++ b/sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/flow/statistic/concurrent/TokenCacheNodeManager.java @@ -16,9 +16,9 @@ package com.alibaba.csp.sentinel.cluster.flow.statistic.concurrent; import com.alibaba.csp.sentinel.cluster.flow.statistic.concurrent.expire.RegularExpireStrategy; +import com.alibaba.csp.sentinel.slots.statistic.cache.CacheMap; +import com.alibaba.csp.sentinel.slots.statistic.cache.CaffeineCacheMapWrapper; import com.alibaba.csp.sentinel.util.AssertUtil; -import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap; -import com.googlecode.concurrentlinkedhashmap.Weighers; import java.util.Set; @@ -26,7 +26,7 @@ * @author yunfeiyanggzq */ public class TokenCacheNodeManager { - private static ConcurrentLinkedHashMap TOKEN_CACHE_NODE_MAP; + private static CacheMap TOKEN_CACHE_NODE_MAP; private static final int DEFAULT_CONCURRENCY_LEVEL = 16; @@ -40,11 +40,7 @@ public static void prepare(int concurrencyLevel, int maximumWeightedCapacity) { AssertUtil.isTrue(concurrencyLevel > 0, "concurrencyLevel must be positive"); AssertUtil.isTrue(maximumWeightedCapacity > 0, "maximumWeightedCapacity must be positive"); - TOKEN_CACHE_NODE_MAP = new ConcurrentLinkedHashMap.Builder() - .concurrencyLevel(concurrencyLevel) - .maximumWeightedCapacity(maximumWeightedCapacity) - .weigher(Weighers.singleton()) - .build(); + TOKEN_CACHE_NODE_MAP = new CaffeineCacheMapWrapper<>(maximumWeightedCapacity); // Start the task of regularly clearing expired keys RegularExpireStrategy strategy = new RegularExpireStrategy(TOKEN_CACHE_NODE_MAP); strategy.startClearTaskRegularly(); @@ -52,8 +48,7 @@ public static void prepare(int concurrencyLevel, int maximumWeightedCapacity) { public static TokenCacheNode getTokenCacheNode(long tokenId) { - //use getQuietly to prevent disorder - return TOKEN_CACHE_NODE_MAP.getQuietly(tokenId); + return TOKEN_CACHE_NODE_MAP.get(tokenId); } public static void putTokenCacheNode(long tokenId, TokenCacheNode cacheNode) { @@ -69,7 +64,7 @@ public static TokenCacheNode removeTokenCacheNode(long tokenId) { } public static int getSize() { - return TOKEN_CACHE_NODE_MAP.size(); + return (int) TOKEN_CACHE_NODE_MAP.size(); } public static Set getCacheKeySet() { diff --git a/sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/flow/statistic/concurrent/expire/RegularExpireStrategy.java b/sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/flow/statistic/concurrent/expire/RegularExpireStrategy.java index 24fd42fbfc..6e388fd7d2 100644 --- a/sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/flow/statistic/concurrent/expire/RegularExpireStrategy.java +++ b/sentinel-cluster/sentinel-cluster-server-default/src/main/java/com/alibaba/csp/sentinel/cluster/flow/statistic/concurrent/expire/RegularExpireStrategy.java @@ -21,8 +21,8 @@ import com.alibaba.csp.sentinel.cluster.server.connection.ConnectionManager; import com.alibaba.csp.sentinel.concurrent.NamedThreadFactory; import com.alibaba.csp.sentinel.log.RecordLog; +import com.alibaba.csp.sentinel.slots.statistic.cache.CacheMap; import com.alibaba.csp.sentinel.util.AssertUtil; -import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap; import java.util.ArrayList; import java.util.List; @@ -62,14 +62,14 @@ public class RegularExpireStrategy implements ExpireStrategy { /** * the local cache of tokenId */ - private ConcurrentLinkedHashMap localCache; + private CacheMap localCache; @SuppressWarnings("PMD.ThreadPoolCreationRule") private static ScheduledExecutorService executor = Executors.newScheduledThreadPool(1, new NamedThreadFactory("regular clear expired token thread", true)); - public RegularExpireStrategy(ConcurrentLinkedHashMap localCache) { + public RegularExpireStrategy(CacheMap localCache) { AssertUtil.isTrue(localCache != null, " local cache can't be null"); this.localCache = localCache; } diff --git a/sentinel-cluster/sentinel-cluster-server-default/src/test/java/com/alibaba/csp/sentinel/cluster/flow/statistic/concurrent/TokenCacheNodeManagerTest.java b/sentinel-cluster/sentinel-cluster-server-default/src/test/java/com/alibaba/csp/sentinel/cluster/flow/statistic/concurrent/TokenCacheNodeManagerTest.java index 8ca4f97a40..c06fd6b946 100644 --- a/sentinel-cluster/sentinel-cluster-server-default/src/test/java/com/alibaba/csp/sentinel/cluster/flow/statistic/concurrent/TokenCacheNodeManagerTest.java +++ b/sentinel-cluster/sentinel-cluster-server-default/src/test/java/com/alibaba/csp/sentinel/cluster/flow/statistic/concurrent/TokenCacheNodeManagerTest.java @@ -16,8 +16,6 @@ package com.alibaba.csp.sentinel.cluster.flow.statistic.concurrent; import com.alibaba.csp.sentinel.cluster.flow.rule.ClusterFlowRuleManager; -import com.alibaba.csp.sentinel.cluster.flow.statistic.concurrent.TokenCacheNode; -import com.alibaba.csp.sentinel.cluster.flow.statistic.concurrent.TokenCacheNodeManager; import com.alibaba.csp.sentinel.slots.block.ClusterRuleConstant; import com.alibaba.csp.sentinel.slots.block.RuleConstant; import com.alibaba.csp.sentinel.slots.block.flow.ClusterFlowConfig; diff --git a/sentinel-extension/sentinel-parameter-flow-control/pom.xml b/sentinel-extension/sentinel-parameter-flow-control/pom.xml index 68ef15d830..211fd1b769 100644 --- a/sentinel-extension/sentinel-parameter-flow-control/pom.xml +++ b/sentinel-extension/sentinel-parameter-flow-control/pom.xml @@ -23,12 +23,6 @@ provided - - com.googlecode.concurrentlinkedhashmap - concurrentlinkedhashmap-lru - 1.4.2 - - com.github.ben-manes.caffeine caffeine diff --git a/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParameterMetric.java b/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParameterMetric.java index d802c4dd73..1ba5372720 100644 --- a/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParameterMetric.java +++ b/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/block/flow/param/ParameterMetric.java @@ -25,7 +25,6 @@ import com.alibaba.csp.sentinel.log.RecordLog; import com.alibaba.csp.sentinel.slots.statistic.cache.CacheMap; import com.alibaba.csp.sentinel.slots.statistic.cache.CaffeineCacheMapWrapper; -import com.alibaba.csp.sentinel.slots.statistic.cache.ConcurrentLinkedHashMapWrapper; /** * Metrics for frequent ("hot spot") parameters. @@ -94,15 +93,11 @@ public void clearForRule(ParamFlowRule rule) { } public void initialize(ParamFlowRule rule) { - initialize(rule, true); - } - - public void initialize(ParamFlowRule rule, boolean useCaffeine) { if (!ruleTimeCounters.containsKey(rule)) { synchronized (lock) { if (ruleTimeCounters.get(rule) == null) { long size = Math.min(BASE_PARAM_MAX_CAPACITY * rule.getDurationInSec(), TOTAL_MAX_CAPACITY); - ruleTimeCounters.put(rule, useCaffeine ? new CaffeineCacheMapWrapper<>(size) : new ConcurrentLinkedHashMapWrapper<>(size)); + ruleTimeCounters.put(rule, new CaffeineCacheMapWrapper<>(size)); } } } @@ -111,7 +106,7 @@ public void initialize(ParamFlowRule rule, boolean useCaffeine) { synchronized (lock) { if (ruleTokenCounter.get(rule) == null) { long size = Math.min(BASE_PARAM_MAX_CAPACITY * rule.getDurationInSec(), TOTAL_MAX_CAPACITY); - ruleTokenCounter.put(rule, useCaffeine ? new CaffeineCacheMapWrapper<>(size) : new ConcurrentLinkedHashMapWrapper<>(size)); + ruleTokenCounter.put(rule, new CaffeineCacheMapWrapper<>(size)); } } } @@ -120,7 +115,7 @@ public void initialize(ParamFlowRule rule, boolean useCaffeine) { synchronized (lock) { if (threadCountMap.get(rule.getParamIdx()) == null) { threadCountMap.put(rule.getParamIdx(), - useCaffeine ? new CaffeineCacheMapWrapper<>(THREAD_COUNT_MAX_CAPACITY) : new ConcurrentLinkedHashMapWrapper<>(THREAD_COUNT_MAX_CAPACITY)); + new CaffeineCacheMapWrapper<>(THREAD_COUNT_MAX_CAPACITY)); } } } diff --git a/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/statistic/cache/CacheMap.java b/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/statistic/cache/CacheMap.java index 4039632b7d..6d0aa76a9d 100644 --- a/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/statistic/cache/CacheMap.java +++ b/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/statistic/cache/CacheMap.java @@ -42,4 +42,8 @@ public interface CacheMap { void clear(); Set keySet(boolean ascending); + + default Set keySet() { + return keySet(true); + } } diff --git a/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/statistic/cache/ConcurrentLinkedHashMapWrapper.java b/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/statistic/cache/ConcurrentLinkedHashMapWrapper.java deleted file mode 100644 index 8b25e0c04c..0000000000 --- a/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/statistic/cache/ConcurrentLinkedHashMapWrapper.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * Copyright 1999-2018 Alibaba Group Holding Ltd. - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package com.alibaba.csp.sentinel.slots.statistic.cache; - -import java.util.Set; - -import com.googlecode.concurrentlinkedhashmap.ConcurrentLinkedHashMap; -import com.googlecode.concurrentlinkedhashmap.Weighers; - -/** - * A {@link ConcurrentLinkedHashMap} wrapper for the universal {@link CacheMap}. - * - * @author Eric Zhao - * @since 0.2.0 - */ -public class ConcurrentLinkedHashMapWrapper implements CacheMap { - - private static final int DEFAULT_CONCURRENCY_LEVEL = 16; - - private final ConcurrentLinkedHashMap map; - - public ConcurrentLinkedHashMapWrapper(long size) { - if (size <= 0) { - throw new IllegalArgumentException("Cache max capacity should be positive: " + size); - } - this.map = new ConcurrentLinkedHashMap.Builder() - .concurrencyLevel(DEFAULT_CONCURRENCY_LEVEL) - .maximumWeightedCapacity(size) - .weigher(Weighers.singleton()) - .build(); - } - - public ConcurrentLinkedHashMapWrapper(ConcurrentLinkedHashMap map) { - if (map == null) { - throw new IllegalArgumentException("Invalid map instance"); - } - this.map = map; - } - - @Override - public boolean containsKey(T key) { - return map.containsKey(key); - } - - @Override - public R get(T key) { - return map.get(key); - } - - @Override - public R remove(T key) { - return map.remove(key); - } - - @Override - public R put(T key, R value) { - return map.put(key, value); - } - - @Override - public R putIfAbsent(T key, R value) { - return map.putIfAbsent(key, value); - } - - @Override - public long size() { - return map.weightedSize(); - } - - @Override - public void clear() { - map.clear(); - } - - @Override - public Set keySet(boolean ascending) { - if (ascending) { - return map.ascendingKeySet(); - } else { - return map.descendingKeySet(); - } - } -} diff --git a/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/statistic/data/ParamMapBucket.java b/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/statistic/data/ParamMapBucket.java index 52a5b4499a..3064218c9d 100644 --- a/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/statistic/data/ParamMapBucket.java +++ b/sentinel-extension/sentinel-parameter-flow-control/src/main/java/com/alibaba/csp/sentinel/slots/statistic/data/ParamMapBucket.java @@ -21,7 +21,6 @@ import com.alibaba.csp.sentinel.slots.block.flow.param.RollingParamEvent; import com.alibaba.csp.sentinel.slots.statistic.cache.CacheMap; import com.alibaba.csp.sentinel.slots.statistic.cache.CaffeineCacheMapWrapper; -import com.alibaba.csp.sentinel.slots.statistic.cache.ConcurrentLinkedHashMapWrapper; import com.alibaba.csp.sentinel.util.AssertUtil; /** @@ -39,19 +38,15 @@ public ParamMapBucket() { } @SuppressWarnings("unchecked") - public ParamMapBucket(int capacity, boolean useCaffeine) { + public ParamMapBucket(int capacity) { AssertUtil.isTrue(capacity > 0, "capacity should be positive"); RollingParamEvent[] events = RollingParamEvent.values(); this.data = new CacheMap[events.length]; for (RollingParamEvent event : events) { - data[event.ordinal()] = useCaffeine ? new CaffeineCacheMapWrapper<>(capacity) : new ConcurrentLinkedHashMapWrapper<>(capacity); + data[event.ordinal()] = new CaffeineCacheMapWrapper<>(capacity); } } - public ParamMapBucket(int capacity) { - this(capacity, true); - } - public void reset() { for (RollingParamEvent event : RollingParamEvent.values()) { data[event.ordinal()].clear();