Java Code Examples for com.alibaba.csp.sentinel.util.TimeUtil#currentTimeMillis()
The following examples show how to use
com.alibaba.csp.sentinel.util.TimeUtil#currentTimeMillis() .
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: DefaultController.java From Sentinel with Apache License 2.0 | 6 votes |
@Override public boolean canPass(Node node, int acquireCount, boolean prioritized) { int curCount = avgUsedTokens(node); if (curCount + acquireCount > count) { if (prioritized && grade == RuleConstant.FLOW_GRADE_QPS) { long currentTime; long waitInMs; currentTime = TimeUtil.currentTimeMillis(); waitInMs = node.tryOccupyNext(currentTime, acquireCount, count); if (waitInMs < OccupyTimeoutProperty.getOccupyTimeout()) { node.addWaitingRequest(currentTime + waitInMs, acquireCount); node.addOccupiedPass(acquireCount); sleep(waitInMs); // PriorityWaitException indicates that the request will pass after waiting for {@link @waitInMs}. throw new PriorityWaitException(waitInMs); } } return false; } return true; }
Example 2
Source File: ParamFlowCheckerTest.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 6 votes |
@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 3
Source File: DefaultController.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 6 votes |
@Override public boolean canPass(Node node, int acquireCount, boolean prioritized) { int curCount = avgUsedTokens(node); if (curCount + acquireCount > count) { if (prioritized && grade == RuleConstant.FLOW_GRADE_QPS) { long currentTime; long waitInMs; currentTime = TimeUtil.currentTimeMillis(); waitInMs = node.tryOccupyNext(currentTime, acquireCount, count); if (waitInMs < OccupyTimeoutProperty.getOccupyTimeout()) { node.addWaitingRequest(currentTime + waitInMs, acquireCount); node.addOccupiedPass(acquireCount); sleep(waitInMs); // PriorityWaitException indicates that the request will pass after waiting for {@link @waitInMs}. throw new PriorityWaitException(waitInMs); } } return false; } return true; }
Example 4
Source File: BucketLeapArrayTest.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 6 votes |
@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 5
Source File: FutureBucketLeapArrayTest.java From Sentinel with Apache License 2.0 | 5 votes |
@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 6
Source File: BucketLeapArrayTest.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
@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 7
Source File: BucketLeapArrayTest.java From Sentinel with Apache License 2.0 | 5 votes |
@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 8
Source File: ClusterStateManager.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
/** * <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 9
Source File: ClusterStateManager.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
/** * <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 10
Source File: BucketLeapArrayTest.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
@Test public void testGetPreviousWindow() { BucketLeapArray leapArray = new BucketLeapArray(sampleCount, intervalInMs); long time = TimeUtil.currentTimeMillis(); WindowWrap<MetricBucket> previousWindow = leapArray.currentWindow(time); assertNull(leapArray.getPreviousWindow(time)); long nextTime = time + windowLengthInMs; assertSame(previousWindow, leapArray.getPreviousWindow(nextTime)); long longTime = time + 11 * windowLengthInMs; assertNull(leapArray.getPreviousWindow(longTime)); }
Example 11
Source File: BucketLeapArrayTest.java From Sentinel with Apache License 2.0 | 5 votes |
@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 12
Source File: StatisticNodeTest.java From Sentinel with Apache License 2.0 | 5 votes |
@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 13
Source File: FutureBucketLeapArrayTest.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 5 votes |
@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 14
Source File: ClusterStateManager.java From Sentinel with Apache License 2.0 | 5 votes |
/** * <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 15
Source File: FooServiceImpl.java From sentinel-guides with Apache License 2.0 | 5 votes |
@Override public long getCurrentTime() { // Simulate slow invocations randomly. if (ThreadLocalRandom.current().nextInt(0, 100) > 70) { try { Thread.sleep(300); } catch (InterruptedException e) { e.printStackTrace(); } } return TimeUtil.currentTimeMillis(); }
Example 16
Source File: Entry.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 4 votes |
public Entry(ResourceWrapper resourceWrapper) { this.resourceWrapper = resourceWrapper; this.createTime = TimeUtil.currentTimeMillis(); }
Example 17
Source File: RateLimiterController.java From Sentinel with Apache License 2.0 | 4 votes |
@Override public boolean canPass(Node node, int acquireCount, boolean prioritized) { // Pass when acquire count is less or equal than 0. if (acquireCount <= 0) { return true; } // Reject when count is less or equal than 0. // Otherwise,the costTime will be max of long and waitTime will overflow in some cases. if (count <= 0) { return false; } long currentTime = TimeUtil.currentTimeMillis(); // Calculate the interval between every two requests. long costTime = Math.round(1.0 * (acquireCount) / count * 1000); // Expected pass time of this request. long expectedTime = costTime + latestPassedTime.get(); if (expectedTime <= currentTime) { // Contention may exist here, but it's okay. latestPassedTime.set(currentTime); return true; } else { // Calculate the time to wait. long waitTime = costTime + latestPassedTime.get() - TimeUtil.currentTimeMillis(); if (waitTime > maxQueueingTimeMs) { return false; } else { long oldTime = latestPassedTime.addAndGet(costTime); try { waitTime = oldTime - TimeUtil.currentTimeMillis(); if (waitTime > maxQueueingTimeMs) { latestPassedTime.addAndGet(-costTime); return false; } // in race condition waitTime may <= 0 if (waitTime > 0) { Thread.sleep(waitTime); } return true; } catch (InterruptedException e) { } } } return false; }
Example 18
Source File: ParamFlowChecker.java From Sentinel-Dashboard-Nacos with Apache License 2.0 | 4 votes |
static boolean passThrottleLocalCheck(ResourceWrapper resourceWrapper, ParamFlowRule rule, int acquireCount, Object value) { ParameterMetric metric = getParameterMetric(resourceWrapper); CacheMap<Object, AtomicLong> timeRecorderMap = metric == null ? null : metric.getRuleTimeCounter(rule); if (timeRecorderMap == null) { return true; } // Calculate max token count (threshold) Set<Object> exclusionItems = rule.getParsedHotItems().keySet(); long tokenCount = (long)rule.getCount(); if (exclusionItems.contains(value)) { tokenCount = rule.getParsedHotItems().get(value); } if (tokenCount == 0) { return false; } long costTime = Math.round(1.0 * 1000 * acquireCount * rule.getDurationInSec() / tokenCount); while (true) { long currentTime = TimeUtil.currentTimeMillis(); AtomicLong timeRecorder = timeRecorderMap.putIfAbsent(value, new AtomicLong(currentTime)); if (timeRecorder == null) { return true; } //AtomicLong timeRecorder = timeRecorderMap.get(value); long lastPassTime = timeRecorder.get(); long expectedTime = lastPassTime + costTime; if (expectedTime <= currentTime || expectedTime - currentTime < rule.getMaxQueueingTimeMs()) { AtomicLong lastPastTimeRef = timeRecorderMap.get(value); if (lastPastTimeRef.compareAndSet(lastPassTime, currentTime)) { long waitTime = expectedTime - currentTime; if (waitTime > 0) { lastPastTimeRef.set(expectedTime); try { TimeUnit.MILLISECONDS.sleep(waitTime); } catch (InterruptedException e) { RecordLog.warn("passThrottleLocalCheck: wait interrupted", e); } } return true; } else { Thread.yield(); } } else { return false; } } }
Example 19
Source File: ParamFlowChecker.java From Sentinel with Apache License 2.0 | 4 votes |
static boolean passThrottleLocalCheck(ResourceWrapper resourceWrapper, ParamFlowRule rule, int acquireCount, Object value) { ParameterMetric metric = getParameterMetric(resourceWrapper); CacheMap<Object, AtomicLong> timeRecorderMap = metric == null ? null : metric.getRuleTimeCounter(rule); if (timeRecorderMap == null) { return true; } // Calculate max token count (threshold) Set<Object> exclusionItems = rule.getParsedHotItems().keySet(); long tokenCount = (long)rule.getCount(); if (exclusionItems.contains(value)) { tokenCount = rule.getParsedHotItems().get(value); } if (tokenCount == 0) { return false; } long costTime = Math.round(1.0 * 1000 * acquireCount * rule.getDurationInSec() / tokenCount); while (true) { long currentTime = TimeUtil.currentTimeMillis(); AtomicLong timeRecorder = timeRecorderMap.putIfAbsent(value, new AtomicLong(currentTime)); if (timeRecorder == null) { return true; } //AtomicLong timeRecorder = timeRecorderMap.get(value); long lastPassTime = timeRecorder.get(); long expectedTime = lastPassTime + costTime; if (expectedTime <= currentTime || expectedTime - currentTime < rule.getMaxQueueingTimeMs()) { AtomicLong lastPastTimeRef = timeRecorderMap.get(value); if (lastPastTimeRef.compareAndSet(lastPassTime, currentTime)) { long waitTime = expectedTime - currentTime; if (waitTime > 0) { lastPastTimeRef.set(expectedTime); try { TimeUnit.MILLISECONDS.sleep(waitTime); } catch (InterruptedException e) { RecordLog.warn("passThrottleLocalCheck: wait interrupted", e); } } return true; } else { Thread.yield(); } } else { return false; } } }
Example 20
Source File: WarmUpRateLimiterController.java From Sentinel with Apache License 2.0 | 4 votes |
@Override public boolean canPass(Node node, int acquireCount, boolean prioritized) { long previousQps = (long) node.previousPassQps(); syncToken(previousQps); long currentTime = TimeUtil.currentTimeMillis(); long restToken = storedTokens.get(); long costTime = 0; long expectedTime = 0; if (restToken >= warningToken) { long aboveToken = restToken - warningToken; // current interval = restToken*slope+1/count double warmingQps = Math.nextUp(1.0 / (aboveToken * slope + 1.0 / count)); costTime = Math.round(1.0 * (acquireCount) / warmingQps * 1000); } else { costTime = Math.round(1.0 * (acquireCount) / count * 1000); } expectedTime = costTime + latestPassedTime.get(); if (expectedTime <= currentTime) { latestPassedTime.set(currentTime); return true; } else { long waitTime = costTime + latestPassedTime.get() - currentTime; if (waitTime > timeoutInMs) { return false; } else { long oldTime = latestPassedTime.addAndGet(costTime); try { waitTime = oldTime - TimeUtil.currentTimeMillis(); if (waitTime > timeoutInMs) { latestPassedTime.addAndGet(-costTime); return false; } if (waitTime > 0) { Thread.sleep(waitTime); } return true; } catch (InterruptedException e) { } } } return false; }