com.alibaba.csp.sentinel.util.TimeUtil Java Examples

The following examples show how to use com.alibaba.csp.sentinel.util.TimeUtil. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: BucketLeapArrayTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testListWindowsResetOld() throws Exception {
    final int windowLengthInMs = 100;
    final int intervalInMs = 1000;
    final int sampleCount = intervalInMs / windowLengthInMs;

    BucketLeapArray leapArray = new BucketLeapArray(sampleCount, intervalInMs);
    long time = TimeUtil.currentTimeMillis();

    Set<WindowWrap<MetricBucket>> windowWraps = new HashSet<WindowWrap<MetricBucket>>();

    windowWraps.add(leapArray.currentWindow(time));
    windowWraps.add(leapArray.currentWindow(time + windowLengthInMs));

    List<WindowWrap<MetricBucket>> list = leapArray.list();
    for (WindowWrap<MetricBucket> wrap : list) {
        assertTrue(windowWraps.contains(wrap));
    }

    Thread.sleep(windowLengthInMs + intervalInMs);

    // This will replace the deprecated bucket, so all deprecated buckets will be reset.
    leapArray.currentWindow(time + windowLengthInMs + intervalInMs).value().addPass(1);

    assertEquals(1, leapArray.list().size());
}
 
Example #2
Source File: ClusterMetricNodeGenerator.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
public static ClusterMetricNode paramToMetricNode(long flowId) {
    ParamFlowRule rule = ClusterParamFlowRuleManager.getParamRuleById(flowId);
    if (rule == null) {
        return null;
    }
    ClusterParamMetric metric = ClusterParamMetricStatistics.getMetric(flowId);
    if (metric == null) {
        return new ClusterMetricNode().setFlowId(flowId)
            .setResourceName(rule.getResource())
            .setTimestamp(TimeUtil.currentTimeMillis())
            .setTopParams(new HashMap<Object, Double>(0));
    }
    return new ClusterMetricNode()
        .setFlowId(flowId)
        .setResourceName(rule.getResource())
        .setTimestamp(TimeUtil.currentTimeMillis())
        .setTopParams(metric.getTopValues(5));
}
 
Example #3
Source File: ClusterMetricNodeGenerator.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
public static ClusterMetricNode flowToMetricNode(long flowId) {
    FlowRule rule = ClusterFlowRuleManager.getFlowRuleById(flowId);
    if (rule == null) {
        return null;
    }
    ClusterMetric metric = ClusterMetricStatistics.getMetric(flowId);
    if (metric == null) {
        return new ClusterMetricNode().setFlowId(flowId)
            .setResourceName(rule.getResource());
    }
    return new ClusterMetricNode()
        .setFlowId(flowId)
        .setResourceName(rule.getResource())
        .setBlockQps(metric.getAvg(ClusterFlowEvent.BLOCK))
        .setPassQps(metric.getAvg(ClusterFlowEvent.PASS))
        .setTimestamp(TimeUtil.currentTimeMillis());
}
 
Example #4
Source File: InMemoryMetricsRepository.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Override
public void save(MetricEntity entity) {
    if (entity == null || StringUtil.isBlank(entity.getApp())) {
        return;
    }
    readWriteLock.writeLock().lock();
    try {
        allMetrics.computeIfAbsent(entity.getApp(), e -> new HashMap<>(16))
                .computeIfAbsent(entity.getResource(), e -> new LinkedHashMap<Long, MetricEntity>() {
                    @Override
                    protected boolean removeEldestEntry(Entry<Long, MetricEntity> eldest) {
                        // Metric older than {@link #MAX_METRIC_LIVE_TIME_MS} will be removed.
                        return eldest.getKey() < TimeUtil.currentTimeMillis() - MAX_METRIC_LIVE_TIME_MS;
                    }
                }).put(entity.getTimestamp().getTime(), entity);
    } finally {
        readWriteLock.writeLock().unlock();
    }

}
 
Example #5
Source File: ParamFlowCheckerTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testPassLocalCheckForArray() throws InterruptedException {
    final String resourceName = "testPassLocalCheckForArray";
    final ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
    int paramIdx = 0;
    double globalThreshold = 1;

    ParamFlowRule rule = new ParamFlowRule(resourceName).setParamIdx(paramIdx)
            .setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER).setCount(globalThreshold);

    TimeUtil.currentTimeMillis();

    String v1 = "a", v2 = "B", v3 = "Cc";
    Object arr = new String[]{v1, v2, v3};
    ParameterMetric metric = new ParameterMetric();
    ParameterMetricStorage.getMetricsMap().put(resourceWrapper.getName(), metric);
    metric.getRuleTimeCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper<Object, AtomicLong>(4000));

    assertTrue(ParamFlowChecker.passCheck(resourceWrapper, rule, 1, arr));
    assertFalse(ParamFlowChecker.passCheck(resourceWrapper, rule, 1, arr));
}
 
Example #6
Source File: BucketLeapArrayTest.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Test
public void testMultiThreadUpdateEmptyWindow() throws Exception {
    final long time = TimeUtil.currentTimeMillis();
    final int nThreads = 16;
    final BucketLeapArray leapArray = new BucketLeapArray(sampleCount, intervalInMs);
    final CountDownLatch latch = new CountDownLatch(nThreads);
    Runnable task = new Runnable() {
        @Override
        public void run() {
            leapArray.currentWindow(time).value().addPass(1);
            latch.countDown();
        }
    };

    for (int i = 0; i < nThreads; i++) {
        new Thread(task).start();
    }

    latch.await();

    assertEquals(nThreads, leapArray.currentWindow(time).value().pass());
}
 
Example #7
Source File: ClusterStateManager.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
/**
 * The interval between two change operations should be greater than {@code MIN_INTERVAL} (by default 10s).
 * Or we need to wait for a while.
 */
private static void sleepIfNeeded() {
    if (lastModified <= 0) {
        return;
    }
    long now = TimeUtil.currentTimeMillis();
    long durationPast = now - lastModified;
    long estimated = durationPast - MIN_INTERVAL;
    if (estimated < 0) {
        try {
            Thread.sleep(-estimated);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
 
Example #8
Source File: ClusterMetricNodeGenerator.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
public static ClusterMetricNode paramToMetricNode(long flowId) {
    ParamFlowRule rule = ClusterParamFlowRuleManager.getParamRuleById(flowId);
    if (rule == null) {
        return null;
    }
    ClusterParamMetric metric = ClusterParamMetricStatistics.getMetric(flowId);
    if (metric == null) {
        return new ClusterMetricNode().setFlowId(flowId)
            .setResourceName(rule.getResource())
            .setTimestamp(TimeUtil.currentTimeMillis())
            .setTopParams(new HashMap<Object, Double>(0));
    }
    return new ClusterMetricNode()
        .setFlowId(flowId)
        .setResourceName(rule.getResource())
        .setTimestamp(TimeUtil.currentTimeMillis())
        .setTopParams(metric.getTopValues(5));
}
 
Example #9
Source File: MetricExitCallback.java    From Sentinel with Apache License 2.0 6 votes vote down vote up
@Override
public void onExit(Context context, ResourceWrapper resourceWrapper, int count, Object... args) {
    for (MetricExtension m : MetricExtensionProvider.getMetricExtensions()) {
        if (context.getCurEntry().getBlockError() != null) {
            continue;
        }
        String resource = resourceWrapper.getName();
        long realRt = TimeUtil.currentTimeMillis() - context.getCurEntry().getCreateTimestamp();
        m.addRt(resource, realRt, args);
        m.addSuccess(resource, count, args);
        m.decreaseThreadNum(resource, args);

        Throwable ex = context.getCurEntry().getError();
        if (ex != null) {
            m.addException(resource, count, ex);
        }
    }
}
 
Example #10
Source File: ClusterStateManager.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
/**
 * The interval between two change operations should be greater than {@code MIN_INTERVAL} (by default 10s).
 * Or we need to wait for a while.
 */
private static void sleepIfNeeded() {
    if (lastModified <= 0) {
        return;
    }
    long now = TimeUtil.currentTimeMillis();
    long durationPast = now - lastModified;
    long estimated = durationPast - MIN_INTERVAL;
    if (estimated < 0) {
        try {
            Thread.sleep(-estimated);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
 
Example #11
Source File: BucketLeapArrayTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
@Test
public void testListWindowsResetOld() throws Exception {
    final int windowLengthInMs = 100;
    final int intervalInMs = 1000;
    final int sampleCount = intervalInMs / windowLengthInMs;

    BucketLeapArray leapArray = new BucketLeapArray(sampleCount, intervalInMs);
    long time = TimeUtil.currentTimeMillis();

    Set<WindowWrap<MetricBucket>> windowWraps = new HashSet<WindowWrap<MetricBucket>>();

    windowWraps.add(leapArray.currentWindow(time));
    windowWraps.add(leapArray.currentWindow(time + windowLengthInMs));

    List<WindowWrap<MetricBucket>> list = leapArray.list();
    for (WindowWrap<MetricBucket> wrap : list) {
        assertTrue(windowWraps.contains(wrap));
    }

    Thread.sleep(windowLengthInMs + intervalInMs);

    // This will replace the deprecated bucket, so all deprecated buckets will be reset.
    leapArray.currentWindow(time + windowLengthInMs + intervalInMs).value().addPass(1);

    assertEquals(1, leapArray.list().size());
}
 
Example #12
Source File: MetricExitCallbackTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 6 votes vote down vote up
@Test
public void onExit() {
    FakeMetricExtension extension = new FakeMetricExtension();
    MetricExtensionProvider.addMetricExtension(extension);

    MetricExitCallback exitCallback = new MetricExitCallback();
    StringResourceWrapper resourceWrapper = new StringResourceWrapper("resource", EntryType.OUT);
    int count = 2;
    Object[] args = {"args1", "args2"};
    extension.rt = 20;
    extension.success = 6;
    extension.thread = 10;
    Context context = mock(Context.class);
    Entry entry = mock(Entry.class);
    when(entry.getError()).thenReturn(null);
    when(entry.getCreateTime()).thenReturn(TimeUtil.currentTimeMillis() - 100);
    when(context.getCurEntry()).thenReturn(entry);
    exitCallback.onExit(context, resourceWrapper, count, args);
    Assert.assertEquals(120, extension.rt, 10);
    Assert.assertEquals(extension.success, 6 + count);
    Assert.assertEquals(extension.thread, 10 - 1);
}
 
Example #13
Source File: BucketLeapArrayTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testLeapArrayWindowStart() {
    BucketLeapArray leapArray = new BucketLeapArray(sampleCount, intervalInMs);
    long firstTime = TimeUtil.currentTimeMillis();
    long previousWindowStart = firstTime - firstTime % windowLengthInMs;

    WindowWrap<MetricBucket> window = leapArray.currentWindow(firstTime);

    assertEquals(windowLengthInMs, window.windowLength());
    assertEquals(previousWindowStart, window.windowStart());
}
 
Example #14
Source File: FutureBucketLeapArrayTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Test
public void testFutureMetricLeapArray() {
    FutureBucketLeapArray array = new FutureBucketLeapArray(sampleCount, intervalInMs);

    long currentTime = TimeUtil.currentTimeMillis();
    for (int i = 0; i < intervalInSec * 1000; i = i + windowLengthInMs) {
        array.currentWindow(i + currentTime).value().addPass(1);
        assertEquals(array.values(i + currentTime).size(), 0);
    }
}
 
Example #15
Source File: BucketLeapArrayTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Test
public void testNewWindow() {
    BucketLeapArray leapArray = new BucketLeapArray(sampleCount, intervalInMs);
    long time = TimeUtil.currentTimeMillis();
    WindowWrap<MetricBucket> window = leapArray.currentWindow(time);

    assertEquals(window.windowLength(), windowLengthInMs);
    assertEquals(window.windowStart(), time - time % windowLengthInMs);
    assertNotNull(window.value());
    assertEquals(0L, window.value().pass());
}
 
Example #16
Source File: BucketLeapArrayTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Test
public void testListWindowsNewBucket() throws Exception {
    final int windowLengthInMs = 100;
    final int intervalInSec = 1;
    final int intervalInMs = intervalInSec * 1000;
    final int sampleCount = intervalInMs / windowLengthInMs;

    BucketLeapArray leapArray = new BucketLeapArray(sampleCount, intervalInMs);
    long time = TimeUtil.currentTimeMillis();

    Set<WindowWrap<MetricBucket>> windowWraps = new HashSet<WindowWrap<MetricBucket>>();

    windowWraps.add(leapArray.currentWindow(time));
    windowWraps.add(leapArray.currentWindow(time + windowLengthInMs));

    Thread.sleep(intervalInMs + windowLengthInMs * 3);

    List<WindowWrap<MetricBucket>> list = leapArray.list();
    for (WindowWrap<MetricBucket> wrap : list) {
        assertTrue(windowWraps.contains(wrap));
    }

    // This won't hit deprecated bucket, so no deprecated buckets will be reset.
    // But deprecated buckets can be filtered when collecting list.
    leapArray.currentWindow(TimeUtil.currentTimeMillis()).value().addPass(1);

    assertEquals(1, leapArray.list().size());
}
 
Example #17
Source File: BucketLeapArrayTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testNewWindow() {
    BucketLeapArray leapArray = new BucketLeapArray(sampleCount, intervalInMs);
    long time = TimeUtil.currentTimeMillis();
    WindowWrap<MetricBucket> window = leapArray.currentWindow(time);

    assertEquals(window.windowLength(), windowLengthInMs);
    assertEquals(window.windowStart(), time - time % windowLengthInMs);
    assertNotNull(window.value());
    assertEquals(0L, window.value().pass());
}
 
Example #18
Source File: BucketLeapArrayTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testListWindowsNewBucket() throws Exception {
    final int windowLengthInMs = 100;
    final int intervalInSec = 1;
    final int intervalInMs = intervalInSec * 1000;
    final int sampleCount = intervalInMs / windowLengthInMs;

    BucketLeapArray leapArray = new BucketLeapArray(sampleCount, intervalInMs);
    long time = TimeUtil.currentTimeMillis();

    Set<WindowWrap<MetricBucket>> windowWraps = new HashSet<WindowWrap<MetricBucket>>();

    windowWraps.add(leapArray.currentWindow(time));
    windowWraps.add(leapArray.currentWindow(time + windowLengthInMs));

    Thread.sleep(intervalInMs + windowLengthInMs * 3);

    List<WindowWrap<MetricBucket>> list = leapArray.list();
    for (WindowWrap<MetricBucket> wrap : list) {
        assertTrue(windowWraps.contains(wrap));
    }

    // This won't hit deprecated bucket, so no deprecated buckets will be reset.
    // But deprecated buckets can be filtered when collecting list.
    leapArray.currentWindow(TimeUtil.currentTimeMillis()).value().addPass(1);

    assertEquals(1, leapArray.list().size());
}
 
Example #19
Source File: StatisticNodeTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Override
public Object call() throws Exception {
    while (true) {
        node.increaseThreadNum();
        node.addPassRequest(1);

        long startTime = TimeUtil.currentTimeMillis();
        bizMethod();

        node.decreaseThreadNum();

        // decrease one ThreadNum, so curThreadNum should less than THREAD_COUNT
        assertTrue(node.curThreadNum() < THREAD_COUNT);

        long rt = TimeUtil.currentTimeMillis() - startTime;
        node.addRtAndSuccess(rt, 1);

        // wait random 0.5 second for simulate method call interval,
        // otherwise the curThreadNum will always be THREAD_COUNT at the beginning
        sleep(RANDOM.nextInt(500));

        bizExecuteCount--;
        if (bizExecuteCount <= 0) {
            break;
        }
    }

    return null;
}
 
Example #20
Source File: SendMetricCommandHandler.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
/**
 * add current cpu usage and load to the metric list.
 *
 * @param list metric list, should not be null
 */
private void addCpuUsageAndLoad(List<MetricNode> list) {
    long time = TimeUtil.currentTimeMillis() / 1000 * 1000;
    double load = SystemRuleManager.getCurrentSystemAvgLoad();
    double usage = SystemRuleManager.getCurrentCpuUsage();
    if (load > 0) {
        MetricNode loadNode = toNode(load, time, Constants.SYSTEM_LOAD_RESOURCE_NAME);
        list.add(loadNode);
    }
    if (usage > 0) {
        MetricNode usageNode = toNode(usage, time, Constants.CPU_USAGE_RESOURCE_NAME);
        list.add(usageNode);
    }
}
 
Example #21
Source File: WarmUpFlowDemo.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    long start = System.currentTimeMillis();
    System.out.println("begin to statistic!!!");
    long oldTotal = 0;
    long oldPass = 0;
    long oldBlock = 0;
    while (!stop) {
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
        }

        long globalTotal = total.get();
        long oneSecondTotal = globalTotal - oldTotal;
        oldTotal = globalTotal;

        long globalPass = pass.get();
        long oneSecondPass = globalPass - oldPass;
        oldPass = globalPass;

        long globalBlock = block.get();
        long oneSecondBlock = globalBlock - oldBlock;
        oldBlock = globalBlock;

        System.out.println(TimeUtil.currentTimeMillis() + ", total:" + oneSecondTotal
            + ", pass:" + oneSecondPass
            + ", block:" + oneSecondBlock);
        if (seconds-- <= 0) {
            stop = true;
        }
    }

    long cost = System.currentTimeMillis() - start;
    System.out.println("time cost: " + cost + " ms");
    System.out.println("total:" + total.get() + ", pass:" + pass.get()
        + ", block:" + block.get());
    System.exit(0);
}
 
Example #22
Source File: RtDegradeDemo.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Override
public void run() {
    long start = System.currentTimeMillis();
    System.out.println("begin to statistic!!!");
    long oldTotal = 0;
    long oldPass = 0;
    long oldBlock = 0;

    while (!stop) {
        try {
            TimeUnit.SECONDS.sleep(1);
        } catch (InterruptedException e) {
        }

        long globalTotal = total.get();
        long oneSecondTotal = globalTotal - oldTotal;
        oldTotal = globalTotal;

        long globalPass = pass.get();
        long oneSecondPass = globalPass - oldPass;
        oldPass = globalPass;

        long globalBlock = block.get();
        long oneSecondBlock = globalBlock - oldBlock;
        oldBlock = globalBlock;

        System.out.println(TimeUtil.currentTimeMillis() + ", total:" + oneSecondTotal
            + ", pass:" + oneSecondPass + ", block:" + oneSecondBlock);

        if (seconds-- <= 0) {
            stop = true;
        }
    }

    long cost = System.currentTimeMillis() - start;
    System.out.println("time cost: " + cost + " ms");
    System.out.println("total:" + total.get() + ", pass:" + pass.get()
        + ", block:" + block.get());
    System.exit(0);
}
 
Example #23
Source File: ParamFlowCheckerTest.java    From Sentinel with Apache License 2.0 5 votes vote down vote up
@Test
public void testSingleValueCheckQpsWithExceptionItems() throws InterruptedException {
    final String resourceName = "testSingleValueCheckQpsWithExceptionItems";
    final ResourceWrapper resourceWrapper = new StringResourceWrapper(resourceName, EntryType.IN);
    TimeUtil.currentTimeMillis();
    int paramIdx = 0;

    long globalThreshold = 5L;
    int thresholdB = 0;
    int thresholdD = 7;

    ParamFlowRule rule = new ParamFlowRule();
    rule.setResource(resourceName);
    rule.setCount(globalThreshold);
    rule.setParamIdx(paramIdx);
    rule.setControlBehavior(RuleConstant.CONTROL_BEHAVIOR_RATE_LIMITER);

    String valueA = "valueA";
    String valueB = "valueB";
    String valueC = "valueC";
    String valueD = "valueD";

    // Directly set parsed map for test.
    Map<Object, Integer> map = new HashMap<Object, Integer>();
    map.put(valueB, thresholdB);
    map.put(valueD, thresholdD);
    rule.setParsedHotItems(map);

    ParameterMetric metric = new ParameterMetric();
    ParameterMetricStorage.getMetricsMap().put(resourceWrapper.getName(), metric);
    metric.getRuleTimeCounterMap().put(rule, new ConcurrentLinkedHashMapWrapper<Object, AtomicLong>(4000));

    assertTrue(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueA));
    assertFalse(ParamFlowChecker.passSingleValueCheck(resourceWrapper, rule, 1, valueB));
    TimeUnit.SECONDS.sleep(3);
}
 
Example #24
Source File: RateLimiterControllerTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Test
public void testPaceController_normal() throws InterruptedException {
    RateLimiterController paceController = new RateLimiterController(500, 10d);
    Node node = mock(Node.class);

    long start = TimeUtil.currentTimeMillis();
    for (int i = 0; i < 6; i++) {
        assertTrue(paceController.canPass(node, 1));
    }
    long end = TimeUtil.currentTimeMillis();
    assertTrue((end - start) > 400);
}
 
Example #25
Source File: ClusterStateManager.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
private static void setStop() {
    if (mode == CLUSTER_NOT_STARTED) {
        return;
    }
    RecordLog.info("[ClusterStateManager] Changing cluster mode to not-started");
    mode = CLUSTER_NOT_STARTED;

    sleepIfNeeded();
    lastModified = TimeUtil.currentTimeMillis();

    stopClient();
    stopServer();
}
 
Example #26
Source File: BucketLeapArrayTest.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Test
public void testLeapArrayWindowStart() {
    BucketLeapArray leapArray = new BucketLeapArray(sampleCount, intervalInMs);
    long firstTime = TimeUtil.currentTimeMillis();
    long previousWindowStart = firstTime - firstTime % windowLengthInMs;

    WindowWrap<MetricBucket> window = leapArray.currentWindow(firstTime);

    assertEquals(windowLengthInMs, window.windowLength());
    assertEquals(previousWindowStart, window.windowStart());
}
 
Example #27
Source File: ClusterStateManager.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
/**
 * <p>
 * Set current mode to server mode. If Sentinel currently works in client mode,
 * it will be turned off. Then the cluster server will be started.
 * </p>
 */
public static boolean setToServer() {
    if (mode == CLUSTER_SERVER) {
        return true;
    }
    mode = CLUSTER_SERVER;
    sleepIfNeeded();
    lastModified = TimeUtil.currentTimeMillis();
    return startServer();
}
 
Example #28
Source File: ClusterStateManager.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
/**
 * <p>
 * Set current mode to client mode. If Sentinel currently works in server mode,
 * it will be turned off. Then the cluster client will be started.
 * </p>
 */
public static boolean setToClient() {
    if (mode == CLUSTER_CLIENT) {
        return true;
    }
    mode = CLUSTER_CLIENT;
    sleepIfNeeded();
    lastModified = TimeUtil.currentTimeMillis();
    return startClient();
}
 
Example #29
Source File: MetricExitCallback.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Override
public void onExit(Context context, ResourceWrapper resourceWrapper, int count, Object... args) {
    for (MetricExtension m : MetricExtensionProvider.getMetricExtensions()) {
        if (context.getCurEntry().getError() == null) {
            long realRt = TimeUtil.currentTimeMillis() - context.getCurEntry().getCreateTime();
            m.addRt(resourceWrapper.getName(), realRt, args);
            m.addSuccess(resourceWrapper.getName(), count, args);
            m.decreaseThreadNum(resourceWrapper.getName(), args);
        }
    }
}
 
Example #30
Source File: StatisticSlot.java    From Sentinel-Dashboard-Nacos with Apache License 2.0 5 votes vote down vote up
@Override
public void exit(Context context, ResourceWrapper resourceWrapper, int count, Object... args) {
    DefaultNode node = (DefaultNode)context.getCurNode();

    if (context.getCurEntry().getError() == null) {
        // Calculate response time (max RT is TIME_DROP_VALVE).
        long rt = TimeUtil.currentTimeMillis() - context.getCurEntry().getCreateTime();
        if (rt > Constants.TIME_DROP_VALVE) {
            rt = Constants.TIME_DROP_VALVE;
        }

        // Record response time and success count.
        node.addRtAndSuccess(rt, count);
        if (context.getCurEntry().getOriginNode() != null) {
            context.getCurEntry().getOriginNode().addRtAndSuccess(rt, count);
        }

        node.decreaseThreadNum();

        if (context.getCurEntry().getOriginNode() != null) {
            context.getCurEntry().getOriginNode().decreaseThreadNum();
        }

        if (resourceWrapper.getType() == EntryType.IN) {
            Constants.ENTRY_NODE.addRtAndSuccess(rt, count);
            Constants.ENTRY_NODE.decreaseThreadNum();
        }
    } else {
        // Error may happen.
    }

    // Handle exit event with registered exit callback handlers.
    Collection<ProcessorSlotExitCallback> exitCallbacks = StatisticSlotCallbackRegistry.getExitCallbacks();
    for (ProcessorSlotExitCallback handler : exitCallbacks) {
        handler.onExit(context, resourceWrapper, count, args);
    }

    fireExit(context, resourceWrapper, count);
}