chengyouling commented on code in PR #4599: URL: https://github.com/apache/servicecomb-java-chassis/pull/4599#discussion_r1857554081
########## handlers/handler-loadbalance/src/main/java/org/apache/servicecomb/loadbalance/filterext/WarmUpDiscoveryFilter.java: ########## @@ -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); + } + + 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); + } Review Comment: fixed ########## foundations/foundation-registry/src/main/java/org/apache/servicecomb/registry/config/InstancePropertiesConst.java: ########## @@ -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"; +} Review Comment: fixed -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
