liubao68 commented on a change in pull request #1400: [SCB-1407] Add gray 
release feature 
URL: 
https://github.com/apache/servicecomb-java-chassis/pull/1400#discussion_r348340940
 
 

 ##########
 File path: 
handlers/handler-router/src/main/java/org/apache/servicecomb/router/cache/RouterRuleCache.java
 ##########
 @@ -0,0 +1,121 @@
+/*
+ * 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.router.cache;
+
+import com.google.common.collect.Interner;
+import com.google.common.collect.Interners;
+import com.netflix.config.DynamicPropertyFactory;
+import com.netflix.config.DynamicStringProperty;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.ConcurrentHashMap;
+import org.apache.servicecomb.router.model.PolicyRuleItem;
+import org.apache.servicecomb.router.model.ServiceInfoCache;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.util.CollectionUtils;
+import org.yaml.snakeyaml.Yaml;
+
+/**
+ * @Author GuoYl123
+ * @Date 2019/10/17
+ **/
+public class RouterRuleCache {
+
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(RouterRuleCache.class);
+
+  private static ConcurrentHashMap<String, ServiceInfoCache> 
serviceInfoCacheMap = new ConcurrentHashMap<>();
+
+  private static final String ROUTE_RULE = "servicecomb.routeRule.%s";
+
+  private static Interner<String> servicePool = Interners.newWeakInterner();
+
+  /**
+   * 每次序列化额外缓存,配置更新时触发回调函数 返回false即初始化规则失败: 1. 规则解析错误 2. 规则为空
+   *
+   * @param targetServiceName
+   * @return
+   */
+  public static boolean doInit(String targetServiceName) {
+    if (serviceInfoCacheMap.containsKey(targetServiceName)) {
+      return true;
+    }
+    //这里使用guava包装String.intern():因为String.intern()分配在Old Generation,容易引发fullgc
+    synchronized (servicePool.intern(targetServiceName)) {
+      //Yaml not thread-safe
+      Yaml yaml = new Yaml();
+      DynamicStringProperty ruleStr = 
DynamicPropertyFactory.getInstance().getStringProperty(
+          String.format(ROUTE_RULE, targetServiceName), null, () -> {
+            refresh(targetServiceName);
+            DynamicStringProperty tepRuleStr = 
DynamicPropertyFactory.getInstance()
+                .getStringProperty(String.format(ROUTE_RULE, 
targetServiceName), null);
+            if (tepRuleStr.get() == null) {
+              return;
+            }
+            try {
+              List<PolicyRuleItem> temList = Arrays
+                  .asList(yaml.loadAs(tepRuleStr.get(), 
PolicyRuleItem[].class));
+              RouterRuleCache.addAllRule(targetServiceName, temList);
+            } catch (Exception e) {
+              LOGGER.error("route management Serialization failed {}", 
e.getMessage());
+              return;
+            }
 
 Review comment:
   duplicate code can extract a method.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

Reply via email to