Search in sources :

Example 16 with StringResourceWrapper

use of com.alibaba.csp.sentinel.slotchain.StringResourceWrapper in project Sentinel by alibaba.

the class ParamFlowCheckerTest method testPassLocalCheckForComplexParam.

@Test
public void testPassLocalCheckForComplexParam() throws InterruptedException {
    class User implements ParamFlowArgument {

        Integer id;

        String name;

        String address;

        public User(Integer id, String name, String address) {
            this.id = id;
            this.name = name;
            this.address = address;
        }

        @Override
        public Object paramFlowKey() {
            return name;
        }
    }
    final String resourceName = "testPassLocalCheckForComplexParam";
    final ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
    int paramIdx = 0;
    double globalThreshold = 1;
    ParamFlowRule rule = new ParamFlowRule(resourceName).setParamIdx(paramIdx).setCount(globalThreshold);
    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<Object, AtomicLong>(4000));
    metric.getRuleTokenCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper<Object, AtomicLong>(4000));
    assertTrue(ParamFlowChecker.passCheck(resourceWrapper, rule, 1, args));
    assertFalse(ParamFlowChecker.passCheck(resourceWrapper, rule, 1, args));
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) AtomicLong(java.util.concurrent.atomic.AtomicLong) Test(org.junit.Test)

Example 17 with StringResourceWrapper

use of com.alibaba.csp.sentinel.slotchain.StringResourceWrapper in project Sentinel by alibaba.

the class ParamFlowDefaultCheckerTest method testParamFlowDefaultCheckSingleQpsWithBurst.

@Test
public void testParamFlowDefaultCheckSingleQpsWithBurst() throws InterruptedException {
    final String resourceName = "testParamFlowDefaultCheckSingleQpsWithBurst";
    final ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
    int paramIdx = 0;
    long threshold = 5L;
    ParamFlowRule rule = new ParamFlowRule();
    rule.setResource(resourceName);
    rule.setCount(threshold);
    rule.setParamIdx(paramIdx);
    rule.setBurstCount(3);
    String valueA = "valueA";
    ParameterMetric metric = new ParameterMetric();
    ParameterMetricStorage.getMetricsMap().put(resourceWrapper.getName(), metric);
    metric.getRuleTimeCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper<Object, AtomicLong>(4000));
    metric.getRuleTokenCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper<Object, AtomicLong>(4000));
    // We mock the time directly to avoid unstable behaviour.
    setCurrentMillis(System.currentTimeMillis());
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    sleep(1002);
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    sleep(1002);
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    sleep(2000);
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    sleep(1002);
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) AtomicLong(java.util.concurrent.atomic.AtomicLong) AbstractTimeBasedTest(com.alibaba.csp.sentinel.test.AbstractTimeBasedTest) Test(org.junit.Test)

Example 18 with StringResourceWrapper

use of com.alibaba.csp.sentinel.slotchain.StringResourceWrapper in project Sentinel by alibaba.

the class ParamFlowDefaultCheckerTest method testCheckQpsWithLongIntervalAndHighThreshold.

@Test
public void testCheckQpsWithLongIntervalAndHighThreshold() {
    // This test case is intended to avoid number overflow.
    final String resourceName = "testCheckQpsWithLongIntervalAndHighThreshold";
    final ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
    int paramIdx = 0;
    // Set a large threshold.
    long threshold = 25000L;
    ParamFlowRule rule = new ParamFlowRule(resourceName).setCount(threshold).setParamIdx(paramIdx);
    String valueA = "valueA";
    ParameterMetric metric = new ParameterMetric();
    ParameterMetricStorage.getMetricsMap().put(resourceWrapper.getName(), metric);
    metric.getRuleTimeCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper<Object, AtomicLong>(4000));
    metric.getRuleTokenCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper<Object, AtomicLong>(4000));
    // We mock the time directly to avoid unstable behaviour.
    setCurrentMillis(System.currentTimeMillis());
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    // 24 hours passed.
    // This can make `toAddCount` larger that Integer.MAX_VALUE.
    sleep(1000 * 60 * 60 * 24);
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    // 48 hours passed.
    sleep(1000 * 60 * 60 * 48);
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) AtomicLong(java.util.concurrent.atomic.AtomicLong) AbstractTimeBasedTest(com.alibaba.csp.sentinel.test.AbstractTimeBasedTest) Test(org.junit.Test)

Example 19 with StringResourceWrapper

use of com.alibaba.csp.sentinel.slotchain.StringResourceWrapper in project Sentinel by alibaba.

the class ParamFlowDefaultCheckerTest method testParamFlowDefaultCheckSingleValueCheckQpsMultipleThreads.

@Test
public void testParamFlowDefaultCheckSingleValueCheckQpsMultipleThreads() throws Exception {
    // In this test case we use the actual time.
    useActualTime();
    final String resourceName = "testParamFlowDefaultCheckSingleValueCheckQpsMultipleThreads";
    final ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
    int paramIdx = 0;
    long threshold = 5L;
    final ParamFlowRule rule = new ParamFlowRule();
    rule.setResource(resourceName);
    rule.setCount(threshold);
    rule.setParamIdx(paramIdx);
    final String valueA = "valueA";
    ParameterMetric metric = new ParameterMetric();
    ParameterMetricStorage.getMetricsMap().put(resourceWrapper.getName(), metric);
    metric.getRuleTimeCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper<Object, AtomicLong>(4000));
    metric.getRuleTokenCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper<Object, AtomicLong>(4000));
    int threadCount = 40;
    final CountDownLatch waitLatch = new CountDownLatch(threadCount);
    final AtomicInteger successCount = new AtomicInteger();
    for (int i = 0; i < threadCount; i++) {
        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                if (ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA)) {
                    successCount.incrementAndGet();
                }
                waitLatch.countDown();
            }
        });
        t.setName("sentinel-simulate-traffic-task-" + i);
        t.start();
    }
    waitLatch.await();
    assertEquals(successCount.get(), threshold);
    successCount.set(0);
    System.out.println("testParamFlowDefaultCheckSingleValueCheckQpsMultipleThreads: sleep for 3 seconds");
    TimeUnit.SECONDS.sleep(3);
    successCount.set(0);
    final CountDownLatch waitLatch1 = new CountDownLatch(threadCount);
    final long currentTime = TimeUtil.currentTimeMillis();
    final long endTime = currentTime + rule.getDurationInSec() * 1000 - 1;
    for (int i = 0; i < threadCount; i++) {
        Thread t = new Thread(new Runnable() {

            @Override
            public void run() {
                long currentTime1 = currentTime;
                while (currentTime1 <= endTime) {
                    if (ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA)) {
                        successCount.incrementAndGet();
                    }
                    try {
                        TimeUnit.MILLISECONDS.sleep(ThreadLocalRandom.current().nextInt(20));
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    currentTime1 = TimeUtil.currentTimeMillis();
                }
                waitLatch1.countDown();
            }
        });
        t.setName("sentinel-simulate-traffic-task-" + i);
        t.start();
    }
    waitLatch1.await();
    assertEquals(successCount.get(), threshold);
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AbstractTimeBasedTest(com.alibaba.csp.sentinel.test.AbstractTimeBasedTest) Test(org.junit.Test)

Example 20 with StringResourceWrapper

use of com.alibaba.csp.sentinel.slotchain.StringResourceWrapper in project Sentinel by alibaba.

the class ParamFlowSlotTest method testEntryWhenParamFlowExists.

@Test
public void testEntryWhenParamFlowExists() throws Throwable {
    String resourceName = "testEntryWhenParamFlowExists";
    ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
    long argToGo = 1L;
    double count = 1;
    ParamFlowRule rule = new ParamFlowRule(resourceName).setCount(count).setBurstCount(0).setParamIdx(0);
    ParamFlowRuleManager.loadRules(Collections.singletonList(rule));
    ParameterMetric metric = mock(ParameterMetric.class);
    CacheMap<Object, AtomicLong> map = new ConcurrentLinkedHashMapWrapper<>(4000);
    CacheMap<Object, AtomicLong> map2 = new ConcurrentLinkedHashMapWrapper<>(4000);
    when(metric.getRuleTimeCounter(rule)).thenReturn(map);
    when(metric.getRuleTokenCounter(rule)).thenReturn(map2);
    map.put(argToGo, new AtomicLong(TimeUtil.currentTimeMillis()));
    // Insert the mock metric to control pass or block.
    ParameterMetricStorage.getMetricsMap().put(resourceWrapper.getName(), metric);
    // The first entry will pass.
    paramFlowSlot.entry(null, resourceWrapper, null, 1, false, argToGo);
    // The second entry will be blocked.
    try {
        paramFlowSlot.entry(null, resourceWrapper, null, 1, false, argToGo);
    } catch (ParamFlowException ex) {
        assertEquals(String.valueOf(argToGo), ex.getMessage());
        assertEquals(resourceName, ex.getResourceName());
        return;
    }
    fail("The second entry should be blocked");
}
Also used : StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) ResourceWrapper(com.alibaba.csp.sentinel.slotchain.ResourceWrapper) StringResourceWrapper(com.alibaba.csp.sentinel.slotchain.StringResourceWrapper) AtomicLong(java.util.concurrent.atomic.AtomicLong) ConcurrentLinkedHashMapWrapper(com.alibaba.csp.sentinel.slots.statistic.cache.ConcurrentLinkedHashMapWrapper) Test(org.junit.Test)

Aggregations

StringResourceWrapper (com.alibaba.csp.sentinel.slotchain.StringResourceWrapper)48 Test (org.junit.Test)43 ResourceWrapper (com.alibaba.csp.sentinel.slotchain.ResourceWrapper)31 Context (com.alibaba.csp.sentinel.context.Context)17 AtomicLong (java.util.concurrent.atomic.AtomicLong)12 AbstractTimeBasedTest (com.alibaba.csp.sentinel.test.AbstractTimeBasedTest)7 BlockException (com.alibaba.csp.sentinel.slots.block.BlockException)6 NullContext (com.alibaba.csp.sentinel.context.NullContext)5 DefaultNode (com.alibaba.csp.sentinel.node.DefaultNode)4 HashMap (java.util.HashMap)3 Entry (com.alibaba.csp.sentinel.Entry)2 EntranceNode (com.alibaba.csp.sentinel.node.EntranceNode)2 Node (com.alibaba.csp.sentinel.node.Node)2 Function (com.alibaba.csp.sentinel.util.function.Function)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 ClusterNode (com.alibaba.csp.sentinel.node.ClusterNode)1 FlowException (com.alibaba.csp.sentinel.slots.block.flow.FlowException)1 ConcurrentLinkedHashMapWrapper (com.alibaba.csp.sentinel.slots.statistic.cache.ConcurrentLinkedHashMapWrapper)1 Random (java.util.Random)1