This is an automated email from the ASF dual-hosted git repository.

liubao pushed a commit to branch 1.3.x
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git


The following commit(s) were added to refs/heads/1.3.x by this push:
     new e204352  merge #2677 into 1.3.x branch (#2686)
e204352 is described below

commit e2043529758ca639adc0b6ddc56408507e3f1129
Author: ette <[email protected]>
AuthorDate: Thu Jan 6 11:04:33 2022 +0800

    merge #2677 into 1.3.x branch (#2686)
---
 .../PriorityInstancePropertyDiscoveryFilter.java   | 199 +++++++++++++++++++++
 ...ecomb.serviceregistry.discovery.DiscoveryFilter |   3 +-
 ...riorityInstancePropertyDiscoveryFilterTest.java | 123 +++++++++++++
 3 files changed, 324 insertions(+), 1 deletion(-)

diff --git 
a/handlers/handler-loadbalance/src/main/java/org/apache/servicecomb/loadbalance/filter/PriorityInstancePropertyDiscoveryFilter.java
 
b/handlers/handler-loadbalance/src/main/java/org/apache/servicecomb/loadbalance/filter/PriorityInstancePropertyDiscoveryFilter.java
new file mode 100644
index 0000000..d3758cd
--- /dev/null
+++ 
b/handlers/handler-loadbalance/src/main/java/org/apache/servicecomb/loadbalance/filter/PriorityInstancePropertyDiscoveryFilter.java
@@ -0,0 +1,199 @@
+/*
+ * 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.filter;
+
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Objects;
+import java.util.Optional;
+
+import javax.validation.constraints.NotNull;
+
+import org.apache.commons.lang3.StringUtils;
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.serviceregistry.RegistryUtils;
+import 
org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
+import 
org.apache.servicecomb.serviceregistry.discovery.AbstractDiscoveryFilter;
+import org.apache.servicecomb.serviceregistry.discovery.DiscoveryContext;
+import org.apache.servicecomb.serviceregistry.discovery.DiscoveryTreeNode;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import com.netflix.config.DynamicPropertyFactory;
+
+/**
+ * Instance property with priority filter
+ */
+public class PriorityInstancePropertyDiscoveryFilter extends 
AbstractDiscoveryFilter {
+
+  private static final Logger LOGGER = 
LoggerFactory.getLogger(PriorityInstancePropertyDiscoveryFilter.class);
+
+  private static final String ALL_INSTANCE = "allInstance";
+
+  private String propertyKey;
+
+  @Override
+  protected void init(DiscoveryContext context, DiscoveryTreeNode parent) {
+    propertyKey = DynamicPropertyFactory.getInstance()
+        
.getStringProperty("servicecomb.loadbalance.filter.priorityInstanceProperty.key",
 "environment")
+        .get();
+
+    // group all instance by property
+    Map<String, MicroserviceInstance> instances = parent.data();
+    Map<String, Map<String, MicroserviceInstance>> groupByProperty = new 
HashMap<>();
+    for (MicroserviceInstance microserviceInstance : instances.values()) {
+      String propertyValue = new PriorityInstanceProperty(propertyKey, 
microserviceInstance).getPropertyValue();
+      groupByProperty.computeIfAbsent(propertyValue, key -> new HashMap<>())
+          .put(microserviceInstance.getInstanceId(), microserviceInstance);
+    }
+    Map<String, DiscoveryTreeNode> children = new HashMap<>();
+    for (Map.Entry<String, Map<String, MicroserviceInstance>> entry : 
groupByProperty.entrySet()) {
+      children.put(entry.getKey(),
+          new DiscoveryTreeNode().subName(parent, 
entry.getKey()).data(entry.getValue()));
+    }
+    children.put(ALL_INSTANCE, new DiscoveryTreeNode().subName(parent, 
ALL_INSTANCE).data(instances));
+    parent.children(children);
+  }
+
+  @Override
+  protected String findChildName(DiscoveryContext context, DiscoveryTreeNode 
parent) {
+    Invocation invocation = context.getInputParameters();
+
+    // context property has precedence over instance property
+    String initPropertyValue = invocation.getContext()
+        .computeIfAbsent("x-" + propertyKey,
+            key -> new PriorityInstanceProperty(propertyKey, 
RegistryUtils.getMicroserviceInstance())
+                .getPropertyValue());
+
+    PriorityInstanceProperty currentProperty = 
context.getContextParameter(propertyKey);
+    // start with initial value, then search with priority
+    if (Objects.isNull(currentProperty)) {
+      currentProperty = new PriorityInstanceProperty(propertyKey, 
initPropertyValue);
+      while (!parent.children().containsKey(currentProperty.getPropertyValue())
+          && currentProperty.hasChildren()) {
+        currentProperty = currentProperty.child();
+      }
+    } else {
+      if (currentProperty.hasChildren()) {
+        currentProperty = currentProperty.child();
+      }
+    }
+    LOGGER.debug("Discovery instance filter by {}", 
currentProperty.toString());
+    context.putContextParameter(propertyKey, currentProperty);
+
+    // stop push filter stack if property is empty
+    if (currentProperty.isEmpty()) {
+      return currentProperty.getPropertyValue();
+    }
+    context.pushRerunFilter();
+    return currentProperty.getPropertyValue();
+  }
+
+  @Override
+  public boolean enabled() {
+    return DynamicPropertyFactory.getInstance()
+        
.getBooleanProperty("servicecomb.loadbalance.filter.priorityInstanceProperty.enabled",
 false)
+        .get();
+  }
+
+  @Override
+  public int getOrder() {
+    return new InstancePropertyDiscoveryFilter().getOrder() + 1;
+  }
+
+  class PriorityInstanceProperty {
+    private static final int MAX_LENGTH = 10000;
+
+    private static final String SEPARATOR = ".";
+
+    private final String propertyKey;
+
+    private final String propertyVal;
+
+    /**
+     * Constructor
+     *
+     * @param key property key
+     * @param value property value
+     */
+    public PriorityInstanceProperty(@NotNull String key, String value) {
+      propertyKey = key;
+      if (Objects.isNull(value)) {
+        value = StringUtils.EMPTY;
+      }
+      if (value.length() > MAX_LENGTH) {
+        throw new IllegalArgumentException("property value exceed max length");
+      }
+      propertyVal = value;
+    }
+
+    /**
+     * Constructor
+     *
+     * @param key property key
+     * @param microserviceInstance instance
+     */
+    public PriorityInstanceProperty(@NotNull String key, @NotNull 
MicroserviceInstance microserviceInstance) {
+      this(key, 
Optional.ofNullable(microserviceInstance.getProperties().get(key)).orElse(StringUtils.EMPTY));
+    }
+
+    /**
+     * whether property is empty
+     *
+     * @return result
+     */
+    public boolean isEmpty() {
+      return StringUtils.isEmpty(propertyVal);
+    }
+
+    /**
+     * does property have lower priority children
+     *
+     * @return result
+     */
+    public boolean hasChildren() {
+      return StringUtils.isNotEmpty(propertyVal);
+    }
+
+    /**
+     * get lower priority child
+     *
+     * @return result
+     */
+    public PriorityInstanceProperty child() {
+      if (propertyVal.contains(SEPARATOR)) {
+        return new PriorityInstanceProperty(propertyKey, 
StringUtils.substringBeforeLast(propertyVal, SEPARATOR));
+      }
+      return new PriorityInstanceProperty(propertyKey, StringUtils.EMPTY);
+    }
+
+    /**
+     * get property value
+     *
+     * @return propertyVal
+     */
+    public String getPropertyValue() {
+      return propertyVal;
+    }
+
+    @Override
+    public String toString() {
+      return "PriorityInstanceProperty{key=" + propertyKey + ", value=" + 
propertyVal + '}';
+    }
+  }
+}
diff --git 
a/handlers/handler-loadbalance/src/main/resources/META-INF/services/org.apache.servicecomb.serviceregistry.discovery.DiscoveryFilter
 
b/handlers/handler-loadbalance/src/main/resources/META-INF/services/org.apache.servicecomb.serviceregistry.discovery.DiscoveryFilter
index c191ce8..3a22561 100644
--- 
a/handlers/handler-loadbalance/src/main/resources/META-INF/services/org.apache.servicecomb.serviceregistry.discovery.DiscoveryFilter
+++ 
b/handlers/handler-loadbalance/src/main/resources/META-INF/services/org.apache.servicecomb.serviceregistry.discovery.DiscoveryFilter
@@ -17,4 +17,5 @@
 
 org.apache.servicecomb.loadbalance.filter.ZoneAwareDiscoveryFilter
 org.apache.servicecomb.loadbalance.filter.IsolationDiscoveryFilter
-org.apache.servicecomb.loadbalance.filter.InstancePropertyDiscoveryFilter
\ No newline at end of file
+org.apache.servicecomb.loadbalance.filter.InstancePropertyDiscoveryFilter
+org.apache.servicecomb.loadbalance.filter.PriorityInstancePropertyDiscoveryFilter
\ No newline at end of file
diff --git 
a/handlers/handler-loadbalance/src/test/java/org/apache/servicecomb/loadbalance/filter/PriorityInstancePropertyDiscoveryFilterTest.java
 
b/handlers/handler-loadbalance/src/test/java/org/apache/servicecomb/loadbalance/filter/PriorityInstancePropertyDiscoveryFilterTest.java
new file mode 100644
index 0000000..f231878
--- /dev/null
+++ 
b/handlers/handler-loadbalance/src/test/java/org/apache/servicecomb/loadbalance/filter/PriorityInstancePropertyDiscoveryFilterTest.java
@@ -0,0 +1,123 @@
+/*
+ * 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.filter;
+
+import java.util.Collections;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.serviceregistry.RegistryUtils;
+import 
org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
+import org.apache.servicecomb.serviceregistry.discovery.DiscoveryContext;
+import org.apache.servicecomb.serviceregistry.discovery.DiscoveryTreeNode;
+import org.hamcrest.collection.IsIterableContainingInAnyOrder;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Test;
+
+import com.google.common.collect.Sets;
+
+import mockit.Mock;
+import mockit.MockUp;
+
+/**
+ * Test for PriorityInstancePropertyDiscoveryFilter
+ */
+public class PriorityInstancePropertyDiscoveryFilterTest {
+
+  public static final String PROPERTY_KEY = "environment";
+
+  private PriorityInstancePropertyDiscoveryFilter filter;
+
+  private Map<String, MicroserviceInstance> instances;
+
+  private MicroserviceInstance self;
+
+  @Before
+  public void setUp() {
+    filter = new PriorityInstancePropertyDiscoveryFilter();
+    instances = new HashMap<>();
+    self = new MicroserviceInstance();
+    self.setInstanceId("self");
+    MicroserviceInstance instance1 = new MicroserviceInstance();
+    instance1.setInstanceId("instance.empty");
+    MicroserviceInstance instance2 = new MicroserviceInstance();
+    instance2.getProperties().put(PROPERTY_KEY, "local");
+    instance2.setInstanceId("instance.local");
+    MicroserviceInstance instance3 = new MicroserviceInstance();
+    instance3.getProperties().put(PROPERTY_KEY, "local.feature1");
+    instance3.setInstanceId("instance.local.feature1");
+    MicroserviceInstance instance4 = new MicroserviceInstance();
+    instance4.getProperties().put(PROPERTY_KEY, "local.feature1.sprint1");
+    instance4.setInstanceId("instance.local.feature1.sprint1");
+
+    instances.put(instance1.getInstanceId(), instance1);
+    instances.put(instance2.getInstanceId(), instance2);
+    instances.put(instance3.getInstanceId(), instance3);
+    instances.put(instance4.getInstanceId(), instance4);
+
+    new MockUp<RegistryUtils>() {
+      @Mock
+      public MicroserviceInstance getMicroserviceInstance() {
+        return self;
+      }
+    };
+  }
+
+  @Test
+  public void testGetFilteredListOfServers() {
+
+    //complete match
+    executeTest("", Sets.newHashSet("instance.empty"));
+    executeTest("local", Sets.newHashSet("instance.local"));
+    executeTest("local.feature1", Sets.newHashSet("instance.local.feature1"));
+    executeTest("local.feature1.sprint1", 
Sets.newHashSet("instance.local.feature1.sprint1"));
+
+    //priority match
+    executeTest("test", Sets.newHashSet("instance.empty"));
+    executeTest("local.feature2", Sets.newHashSet("instance.local"));
+    executeTest("local.feature1.sprint2", 
Sets.newHashSet("instance.local.feature1"));
+    executeTest("local.feature2.sprint1", Sets.newHashSet("instance.local"));
+    executeTest("local.feature1.sprint2.temp", 
Sets.newHashSet("instance.local.feature1"));
+
+    //none match
+    MicroserviceInstance instance1 = instances.remove("instance.empty");
+    executeTest("", Collections.emptySet());
+    executeTest("foo", Collections.emptySet());
+    instances.put("instance.empty", instance1);
+  }
+
+
+  private void executeTest(String selfProperty, Set<String> 
expectedMatchedKeys) {
+    Invocation invocation = new Invocation();
+    DiscoveryContext discoveryContext = new DiscoveryContext();
+    discoveryContext.setInputParameters(invocation);
+    self.getProperties().put(PROPERTY_KEY, selfProperty);
+
+    DiscoveryTreeNode parent = new DiscoveryTreeNode();
+    parent.name("parent");
+    parent.data(instances);
+
+    DiscoveryTreeNode node = filter.discovery(discoveryContext, parent);
+    Map<String, MicroserviceInstance> filterInstance = node.data();
+    Assert.assertThat(filterInstance.keySet(),
+        
IsIterableContainingInAnyOrder.containsInAnyOrder(expectedMatchedKeys.toArray()));
+  }
+}
\ No newline at end of file

Reply via email to