-
Notifications
You must be signed in to change notification settings - Fork 822
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[#4598] Supports traffic warm-up when the service is just started #4599
Open
chengyouling
wants to merge
16
commits into
apache:2.8.x
Choose a base branch
from
chengyouling:2.8.x-warm
base: 2.8.x
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 9 commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
3cd16f6
[#4598] Supports traffic warm-up when the service is just started
chengyouling 7864c4d
调整预热默认时间
chengyouling f034a64
解决本地注册无注册时间导致测试失败问题
chengyouling 54114ea
关闭edge测试中预热
chengyouling 8c4e8fd
关闭edge测试中预热
chengyouling 5e533e1
set register time to instance property
chengyouling 2af2bf1
change test warm time
chengyouling 6afd6f9
修复测试用例异常
chengyouling 2928893
调整权重计算逻辑
chengyouling df413ae
增加服务被调用时间计算权重
chengyouling df7902e
调整运行时间使用
chengyouling 3191fd4
调整为默认不使用预热
chengyouling 46b427e
删除注册时间计算权重
chengyouling 2a14a5f
增加实例预热期间最大流量
chengyouling 92c5022
fixed checkstyle
chengyouling 8adf9d5
调整为动态获取预热时间、预热实例最大流量等配置
chengyouling File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
22 changes: 22 additions & 0 deletions
22
...egistry/src/main/java/org/apache/servicecomb/registry/config/InstancePropertiesConst.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,22 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 org.apache.servicecomb.registry.config; | ||
|
||
public class InstancePropertiesConst { | ||
public static final String REGISTER_TIME_KEY = "registerTime"; | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If can make it simple, do not depends on register time? I think first access time is quite enough. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. fixed |
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
142 changes: 142 additions & 0 deletions
142
...nce/src/main/java/org/apache/servicecomb/loadbalance/filterext/WarmUpDiscoveryFilter.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,142 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You 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 org.apache.servicecomb.loadbalance.filterext; | ||
|
||
import com.netflix.config.DynamicPropertyFactory; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.servicecomb.core.Invocation; | ||
import org.apache.servicecomb.loadbalance.ServerListFilterExt; | ||
import org.apache.servicecomb.loadbalance.ServiceCombServer; | ||
import org.apache.servicecomb.registry.config.InstancePropertiesConst; | ||
import org.slf4j.Logger; | ||
import org.slf4j.LoggerFactory; | ||
import org.springframework.util.CollectionUtils; | ||
|
||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Random; | ||
import java.util.stream.Collectors; | ||
|
||
public class WarmUpDiscoveryFilter implements ServerListFilterExt { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(WarmUpDiscoveryFilter.class); | ||
|
||
private static final int INSTANCE_WEIGHT = 100; | ||
|
||
// Default time for warm up, the unit is milliseconds | ||
private static final String DEFAULT_WARM_UP_TIME = "30000"; | ||
|
||
private static final String WARM_TIME_KEY = "warmupTime"; | ||
|
||
private static final String WARM_CURVE_KEY = "warmupCurve"; | ||
|
||
// Preheat calculates curve value | ||
private static final String DEFAULT_WARM_UP_CURVE = "2"; | ||
|
||
private final Random random = new Random(); | ||
|
||
@Override | ||
public int getOrder() { | ||
return ORDER_WARM_UP; | ||
} | ||
|
||
@Override | ||
public boolean enabled() { | ||
return DynamicPropertyFactory.getInstance() | ||
.getBooleanProperty(WARM_UP_FILTER_ENABLED, true) | ||
.get(); | ||
} | ||
|
||
@Override | ||
public List<ServiceCombServer> getFilteredListOfServers(List<ServiceCombServer> servers, | ||
Invocation invocation) { | ||
if (servers.size() <= 1) { | ||
return servers; | ||
} | ||
if (CollectionUtils.isEmpty(existNeedWarmUpInstances(servers))) { | ||
return servers; | ||
} | ||
int[] weights = new int[servers.size()]; | ||
int totalWeight = 0; | ||
int index = 0; | ||
for (ServiceCombServer server : servers) { | ||
weights[index] = calculate(server.getInstance().getProperties()); | ||
totalWeight += weights[index++]; | ||
} | ||
return chooseServer(totalWeight, weights, servers); | ||
} | ||
|
||
private List<ServiceCombServer> existNeedWarmUpInstances(List<ServiceCombServer> servers) { | ||
return servers.stream() | ||
.filter(server -> isInstanceNeedWarmUp(server.getInstance().getProperties())) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private boolean isInstanceNeedWarmUp(Map<String, String> properties) { | ||
final long warmUpTime = Long.parseLong(properties.getOrDefault(WARM_TIME_KEY, DEFAULT_WARM_UP_TIME)); | ||
String registerTimeStr = properties.get(InstancePropertiesConst.REGISTER_TIME_KEY); | ||
final long registerTime = Long.parseLong(StringUtils.isEmpty(registerTimeStr) ? "0" : registerTimeStr); | ||
return registerTime != 0L && System.currentTimeMillis() - registerTime < warmUpTime; | ||
} | ||
|
||
private List<ServiceCombServer> chooseServer(int totalWeight, int[] weights, List<ServiceCombServer> servers) { | ||
if (totalWeight <= 0) { | ||
return servers; | ||
} | ||
int position = random.nextInt(totalWeight); | ||
for (int i = 0; i < weights.length; i++) { | ||
position -= weights[i]; | ||
if (position < 0) { | ||
if (LOGGER.isDebugEnabled()) { | ||
LOGGER.debug("warm up choose service instance: " + servers.get(i).getInstance().getInstanceId()); | ||
} | ||
return Collections.singletonList(servers.get(i)); | ||
} | ||
} | ||
return servers; | ||
} | ||
|
||
private int calculate(Map<String, String> properties) { | ||
final int warmUpCurve = Integer.parseInt(properties.getOrDefault(WARM_CURVE_KEY, DEFAULT_WARM_UP_CURVE)); | ||
final long warmUpTime = Long.parseLong(properties.getOrDefault(WARM_TIME_KEY, DEFAULT_WARM_UP_TIME)); | ||
String registerTimeStr = properties.get(InstancePropertiesConst.REGISTER_TIME_KEY); | ||
final long registerTime = Long.parseLong(StringUtils.isEmpty(registerTimeStr) ? "0" : registerTimeStr); | ||
return calculateWeight(registerTime, warmUpTime, warmUpCurve); | ||
} | ||
|
||
private int calculateWeight(long registerTime, long warmUpTime, int warmUpCurve) { | ||
if (warmUpTime <= 0 || registerTime <= 0) { | ||
return INSTANCE_WEIGHT; | ||
} | ||
if (warmUpCurve <= 0) { | ||
warmUpCurve = Integer.parseInt(DEFAULT_WARM_UP_CURVE); | ||
} | ||
// calculated in seconds | ||
final long runTime = System.currentTimeMillis() - registerTime; | ||
if (runTime > 0 && runTime < warmUpTime) { | ||
return calculateWarmUpWeight(runTime, warmUpTime, warmUpCurve); | ||
} | ||
return INSTANCE_WEIGHT; | ||
} | ||
|
||
private int calculateWarmUpWeight(double runtime, double warmUpTime, int warmUpCurve) { | ||
final int round = (int) Math.round(Math.pow(runtime / warmUpTime, warmUpCurve) * INSTANCE_WEIGHT); | ||
return round < 1 ? 1 : Math.min(round, INSTANCE_WEIGHT); | ||
} | ||
} |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Distributed time may not always same for different instance, and weighted for instance is not only depended on instance startup time, but time accessed of the consumer.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
add provider invoke time to calculate weight