-
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
base: 2.8.x
Are you sure you want to change the base?
Changes from 12 commits
3cd16f6
7864c4d
f034a64
54114ea
8c4e8fd
5e533e1
2af2bf1
6afd6f9
2928893
df413ae
df7902e
3191fd4
46b427e
2a14a5f
92c5022
8adf9d5
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,215 @@ | ||
/* | ||
* 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.google.common.util.concurrent.ThreadFactoryBuilder; | ||
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.ArrayList; | ||
import java.util.Collections; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Random; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.Executors; | ||
import java.util.concurrent.ScheduledExecutorService; | ||
import java.util.concurrent.TimeUnit; | ||
import java.util.stream.Collectors; | ||
|
||
public class WarmUpDiscoveryFilter implements ServerListFilterExt { | ||
private static final Logger LOGGER = LoggerFactory.getLogger(WarmUpDiscoveryFilter.class); | ||
|
||
private static final String IGNORE_WARN_UP_TIME = "servicecomb.loadbalance.filter.service.warmup.ignoreWarmUpTime"; | ||
|
||
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"; | ||
|
||
// provider run time, the unit is milliseconds | ||
private final long ignoreWarmUpTime; | ||
|
||
private final Random random = new Random(); | ||
|
||
private final Map<String, Long> instanceInvokeTime = new ConcurrentHashMap<>(); | ||
|
||
public WarmUpDiscoveryFilter() { | ||
ScheduledExecutorService executor = Executors.newScheduledThreadPool(1, | ||
new ThreadFactoryBuilder() | ||
.setNameFormat("warm-up-cache-refresh-%d") | ||
.build()); | ||
ignoreWarmUpTime = getIgnoreWarmUpTime(); | ||
executor.scheduleAtFixedRate(this::refreshMapCache, 0, 10, TimeUnit.MINUTES); | ||
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. Maybe do not need a timer. You can remove not exist instance id every more 10 minutes when choosing server(condition e.g. not exist and last accessed time is more than 30 minutes). 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 |
||
} | ||
|
||
private long getIgnoreWarmUpTime() { | ||
return DynamicPropertyFactory.getInstance() | ||
.getLongProperty(IGNORE_WARN_UP_TIME, 30 * 60 * 1000) | ||
.get(); | ||
} | ||
|
||
private void refreshMapCache() { | ||
List<String> removeKeys = new ArrayList<>(); | ||
for (Map.Entry<String, Long> entry : instanceInvokeTime.entrySet()) { | ||
if (System.currentTimeMillis() - entry.getValue() > ignoreWarmUpTime) { | ||
removeKeys.add(entry.getKey()); | ||
} | ||
} | ||
if (CollectionUtils.isEmpty(removeKeys)) { | ||
return; | ||
} | ||
removeKeys.forEach(instanceInvokeTime::remove); | ||
} | ||
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. Is it removing not exist instance? Seems remove invoked instance will cause problem. 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 |
||
|
||
@Override | ||
public int getOrder() { | ||
return ORDER_WARM_UP; | ||
} | ||
|
||
@Override | ||
public boolean enabled() { | ||
return DynamicPropertyFactory.getInstance() | ||
.getBooleanProperty(WARM_UP_FILTER_ENABLED, false) | ||
.get(); | ||
} | ||
|
||
@Override | ||
public List<ServiceCombServer> getFilteredListOfServers(List<ServiceCombServer> servers, | ||
Invocation invocation) { | ||
if (servers.size() <= 1) { | ||
return servers; | ||
} | ||
List<ServiceCombServer> notInvokedInstances = new ArrayList<>(); | ||
if (CollectionUtils.isEmpty(existNeedWarmUpInstances(servers, notInvokedInstances))) { | ||
return servers; | ||
} | ||
|
||
// If exist not invoked instances, choose one that let it into warm up. | ||
if (!CollectionUtils.isEmpty(notInvokedInstances)) { | ||
setInstanceStartInvokeTime(notInvokedInstances.get(0)); | ||
return Collections.singletonList(notInvokedInstances.get(0)); | ||
} | ||
int[] weights = new int[servers.size()]; | ||
int totalWeight = 0; | ||
int index = 0; | ||
for (ServiceCombServer server : servers) { | ||
weights[index] = calculate(server.getInstance().getProperties(), server.getInstance().getInstanceId()); | ||
totalWeight += weights[index++]; | ||
} | ||
return chooseServer(totalWeight, weights, servers); | ||
} | ||
|
||
private List<ServiceCombServer> existNeedWarmUpInstances(List<ServiceCombServer> servers, | ||
List<ServiceCombServer> notInvokedInstances) { | ||
return servers.stream() | ||
.filter(server -> isInstanceNeedWarmUp(server, notInvokedInstances)) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
private boolean isInstanceNeedWarmUp(ServiceCombServer server, List<ServiceCombServer> notInvokedInstances) { | ||
Map<String, String> properties = server.getInstance().getProperties(); | ||
String instanceId = server.getInstance().getInstanceId(); | ||
if (isUpIgnoreWarmUpTime(properties)) { | ||
return false; | ||
} | ||
Long invokeTime = instanceInvokeTime.get(instanceId); | ||
|
||
// instance have not been invoked need to be warn-up | ||
if (invokeTime == null) { | ||
notInvokedInstances.add(server); | ||
return true; | ||
} | ||
final long warmUpTime = Long.parseLong(properties.getOrDefault(WARM_TIME_KEY, DEFAULT_WARM_UP_TIME)); | ||
return System.currentTimeMillis() - invokeTime < warmUpTime; | ||
} | ||
|
||
private boolean isUpIgnoreWarmUpTime(Map<String, String> properties) { | ||
String registerTimeStr = properties.get(InstancePropertiesConst.REGISTER_TIME_KEY); | ||
long registerTime = Long.parseLong(StringUtils.isEmpty(registerTimeStr) ? "0" : registerTimeStr); | ||
|
||
// Provider run time greater than 30 minute, can be regarded as don't need warn up. | ||
// To ensure that the consumer is restarted but the provider is not restarted. | ||
return System.currentTimeMillis() - registerTime > ignoreWarmUpTime; | ||
} | ||
|
||
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 void setInstanceStartInvokeTime(ServiceCombServer server) { | ||
instanceInvokeTime.putIfAbsent(server.getInstance().getInstanceId(), System.currentTimeMillis()); | ||
} | ||
|
||
private int calculate(Map<String, String> properties, String instanceId) { | ||
if (instanceInvokeTime.get(instanceId) == null) { | ||
return INSTANCE_WEIGHT; | ||
} | ||
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)); | ||
return calculateWeight(instanceInvokeTime.get(instanceId), warmUpTime, warmUpCurve); | ||
} | ||
|
||
private int calculateWeight(long invokeStartTime, long warmUpTime, int warmUpCurve) { | ||
if (warmUpTime <= 0) { | ||
return INSTANCE_WEIGHT; | ||
} | ||
if (warmUpCurve <= 0) { | ||
warmUpCurve = Integer.parseInt(DEFAULT_WARM_UP_CURVE); | ||
} | ||
// calculated in milliseconds | ||
final long runTime = System.currentTimeMillis() - invokeStartTime; | ||
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); | ||
} | ||
} |
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