forked from alibaba/Sentinel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add occupy mechanism for future buckets of sliding window to support …
…"prioritized requests final pass" (alibaba#568) * Rename: MetricsLeapArray -> BucketLeapArray * Add implementation for `FutureBucketLeapArray`, a kind of `BucketLeapArray` that only reserves for future buckets, which is used for calculating occupied future tokens. * Add OccupiableBucketLeapArray that combines common BucketLeapArray with FutureBucketLeapArray. The rollingNumberInSecond in StatisticNode now uses OccupiableBucketLeapArray by default. * Add OccupySupport interface. Node now implements OccupySupport interface. * Add occupy-related methods in Metric and ArrayMetric. * Handle prioritized requests in default traffic shaping controller. * Update default occupyTimeout to 500ms Signed-off-by: Eric Zhao <[email protected]>
- Loading branch information
Showing
28 changed files
with
957 additions
and
132 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/OccupySupport.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* Copyright 1999-2019 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.node; | ||
|
||
/** | ||
* @author Eric Zhao | ||
* @since 1.5.0 | ||
*/ | ||
public interface OccupySupport { | ||
|
||
/** | ||
* Try to occupy latter time windows' tokens. If occupy success, a value less than | ||
* {@code occupyTimeout} in {@link OccupyTimeoutProperty} will be return. | ||
* | ||
* <p> | ||
* Each time we occupy tokens of the future window, current thread should sleep for the | ||
* corresponding time for smoothing QPS. We can't occupy tokens of the future with unlimited, | ||
* the sleep time limit is {@code occupyTimeout} in {@link OccupyTimeoutProperty}. | ||
* </p> | ||
* | ||
* @param currentTime current time millis. | ||
* @param acquireCount tokens count to acquire. | ||
* @param threshold qps threshold. | ||
* @return time should sleep. Time >= {@code occupyTimeout} in {@link OccupyTimeoutProperty} means | ||
* occupy fail, in this case, the request should be rejected immediately. | ||
*/ | ||
long tryOccupyNext(long currentTime, int acquireCount, double threshold); | ||
|
||
/** | ||
* Get current waiting amount. Useful for debug. | ||
* | ||
* @return current waiting amount | ||
*/ | ||
long waiting(); | ||
|
||
/** | ||
* Add request that occupied. | ||
* | ||
* @param futureTime future timestamp that the acquireCount should be added on. | ||
* @param acquireCount tokens count. | ||
*/ | ||
void addWaitingRequest(long futureTime, int acquireCount); | ||
|
||
/** | ||
* Add occupied pass request, which represents pass requests that borrow the latter windows' token. | ||
* | ||
* @param acquireCount tokens count. | ||
*/ | ||
void addOccupiedPass(int acquireCount); | ||
|
||
/** | ||
* Get current occupied pass QPS. | ||
* | ||
* @return current occupied pass QPS | ||
*/ | ||
double occupiedPassQps(); | ||
} |
79 changes: 79 additions & 0 deletions
79
sentinel-core/src/main/java/com/alibaba/csp/sentinel/node/OccupyTimeoutProperty.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright 1999-2019 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.node; | ||
|
||
import com.alibaba.csp.sentinel.log.RecordLog; | ||
import com.alibaba.csp.sentinel.property.SentinelProperty; | ||
import com.alibaba.csp.sentinel.property.SimplePropertyListener; | ||
|
||
/** | ||
* @author jialiang.linjl | ||
* @author Carpenter Lee | ||
* @since 1.5.0 | ||
*/ | ||
public class OccupyTimeoutProperty { | ||
|
||
/** | ||
* <p> | ||
* Max occupy timeout in milliseconds. Requests with priority can occupy tokens of the future statistic | ||
* window, and {@code occupyTimeout} limit the max time length that can be occupied. | ||
* </p> | ||
* <p> | ||
* Note that the timeout value should never be greeter than {@link IntervalProperty#INTERVAL}. | ||
* </p> | ||
* DO NOT MODIFY this value directly, use {@link #updateTimeout(int)}, | ||
* otherwise the modification will not take effect. | ||
*/ | ||
private static volatile int occupyTimeout = 500; | ||
|
||
public static void register2Property(SentinelProperty<Integer> property) { | ||
property.addListener(new SimplePropertyListener<Integer>() { | ||
@Override | ||
public void configUpdate(Integer value) { | ||
if (value != null) { | ||
updateTimeout(value); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
public static int getOccupyTimeout() { | ||
return occupyTimeout; | ||
} | ||
|
||
/** | ||
* Update the timeout value.</br> | ||
* Note that the time out should never greeter than {@link IntervalProperty#INTERVAL}, | ||
* or it will be ignored. | ||
* | ||
* @param newInterval new value. | ||
*/ | ||
public static void updateTimeout(int newInterval) { | ||
if (newInterval < 0) { | ||
RecordLog.warn("[OccupyTimeoutProperty] Illegal timeout value will be ignored: " + occupyTimeout); | ||
return; | ||
} | ||
if (newInterval > IntervalProperty.INTERVAL) { | ||
RecordLog.warn("[OccupyTimeoutProperty] Illegal timeout value will be ignored: " + occupyTimeout | ||
+ ", should <= " + IntervalProperty.INTERVAL); | ||
return; | ||
} | ||
if (newInterval != occupyTimeout) { | ||
occupyTimeout = newInterval; | ||
} | ||
RecordLog.info("[OccupyTimeoutProperty] occupyTimeout updated to: " + occupyTimeout); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.