-
Notifications
You must be signed in to change notification settings - Fork 8.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add "warmup with rate limiting" implementation for traffic shaping (#220
) - Add a new kind of control behavior `warm up + rate limiter`, behaving as both warm up and pace control (cherry picked from commit 08f2a71)
- Loading branch information
1 parent
5d05ded
commit 1c9d2db
Showing
9 changed files
with
378 additions
and
21 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
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
85 changes: 85 additions & 0 deletions
85
...ava/com/alibaba/csp/sentinel/slots/block/flow/controller/WarmUpRateLimiterController.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,85 @@ | ||
/* | ||
* 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.block.flow.controller; | ||
|
||
import java.util.concurrent.atomic.AtomicLong; | ||
|
||
import com.alibaba.csp.sentinel.node.Node; | ||
import com.alibaba.csp.sentinel.util.TimeUtil; | ||
|
||
/** | ||
* @author jialiang.linjl | ||
*/ | ||
public class WarmUpRateLimiterController extends WarmUpController { | ||
|
||
final int timeOutInMs; | ||
final AtomicLong latestPassedTime = new AtomicLong(-1); | ||
|
||
/** | ||
* @param count | ||
* @param warmUpPeriodSec | ||
*/ | ||
public WarmUpRateLimiterController(double count, int warmUpPeriodSec, int timeOutMs, int coldFactor) { | ||
super(count, warmUpPeriodSec, coldFactor); | ||
this.timeOutInMs = timeOutMs; | ||
} | ||
|
||
@Override | ||
public boolean canPass(Node node, int acquireCount) { | ||
long previousQps = 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; | ||
} | ||
Thread.sleep(waitTime); | ||
return true; | ||
} catch (InterruptedException e) { | ||
} | ||
} | ||
} | ||
return false; | ||
} | ||
} | ||
|
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
49 changes: 49 additions & 0 deletions
49
.../test/java/com/alibaba/csp/sentinel/slots/block/flow/WarmUpRateLimiterControllerTest.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,49 @@ | ||
package com.alibaba.csp.sentinel.slots.block.flow; | ||
|
||
import com.alibaba.csp.sentinel.node.Node; | ||
import com.alibaba.csp.sentinel.node.StatisticNode; | ||
import com.alibaba.csp.sentinel.slots.block.flow.controller.WarmUpRateLimiterController; | ||
|
||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.assertFalse; | ||
import static org.junit.Assert.assertTrue; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
/** | ||
* @author CarpenterLee | ||
*/ | ||
public class WarmUpRateLimiterControllerTest { | ||
|
||
@Test | ||
public void testPace() throws InterruptedException { | ||
WarmUpRateLimiterController controller = new WarmUpRateLimiterController(10, 10, 1000, 3); | ||
|
||
Node node = mock(Node.class); | ||
|
||
when(node.passQps()).thenReturn(100L); | ||
when(node.previousPassQps()).thenReturn(100L); | ||
|
||
assertTrue(controller.canPass(node, 1)); | ||
|
||
long start = System.currentTimeMillis(); | ||
assertTrue(controller.canPass(node, 1)); | ||
long cost = System.currentTimeMillis() - start; | ||
assertTrue(cost >= 100 && cost <= 110); | ||
} | ||
|
||
@Test | ||
public void testPaceCanNotPass() throws InterruptedException { | ||
WarmUpRateLimiterController controller = new WarmUpRateLimiterController(10, 10, 10, 3); | ||
|
||
Node node = mock(Node.class); | ||
|
||
when(node.passQps()).thenReturn(100L); | ||
when(node.previousPassQps()).thenReturn(100L); | ||
|
||
assertTrue(controller.canPass(node, 1)); | ||
|
||
assertFalse(controller.canPass(node, 1)); | ||
} | ||
} |
Oops, something went wrong.