[ 
https://issues.apache.org/jira/browse/SCB-1061?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=16708047#comment-16708047
 ] 

ASF GitHub Bot commented on SCB-1061:
-------------------------------------

liubao68 closed pull request #1019: [SCB-1061]Provide a way to using 
handlers(e.g. LoabalanceHanler) outs…
URL: https://github.com/apache/servicecomb-java-chassis/pull/1019
 
 
   

This is a PR merged from a forked repository.
As GitHub hides the original diff on merge, it is displayed below for
the sake of provenance:

As this is a foreign pull request (from a fork), the diff is supplied
below (as it won't show otherwise due to GitHub magic):

diff --git a/core/src/main/java/org/apache/servicecomb/core/Invocation.java 
b/core/src/main/java/org/apache/servicecomb/core/Invocation.java
index 32fa68b5e..46c3c4a2c 100644
--- a/core/src/main/java/org/apache/servicecomb/core/Invocation.java
+++ b/core/src/main/java/org/apache/servicecomb/core/Invocation.java
@@ -109,6 +109,10 @@ public long getStartExecutionTime() {
     return invocationStageTrace.getStartExecution();
   }
 
+  public Invocation() {
+    // An empty invocation, used to mock or some other scenario do not need 
operation information.
+  }
+
   public Invocation(ReferenceConfig referenceConfig, OperationMeta 
operationMeta, Object[] swaggerArguments) {
     this.invocationType = InvocationType.CONSUMER;
     this.referenceConfig = referenceConfig;
diff --git 
a/core/src/main/java/org/apache/servicecomb/core/NonSwaggerInvocation.java 
b/core/src/main/java/org/apache/servicecomb/core/NonSwaggerInvocation.java
new file mode 100644
index 000000000..785c90c93
--- /dev/null
+++ b/core/src/main/java/org/apache/servicecomb/core/NonSwaggerInvocation.java
@@ -0,0 +1,77 @@
+/*
+ * 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.core;
+
+import org.apache.servicecomb.swagger.invocation.AsyncResponse;
+
+public class NonSwaggerInvocation extends Invocation {
+  private String appId;
+
+  private String microserviceName;
+
+  private String versionRule;
+
+  private Handler nextHandler;
+
+  public NonSwaggerInvocation(String appId, String microserviceName, String 
versionRule, Handler nextHandler) {
+    this.appId = appId;
+    this.microserviceName = microserviceName;
+    this.versionRule = versionRule;
+    this.nextHandler = nextHandler;
+  }
+
+  @Override
+  public String getSchemaId() {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public String getOperationName() {
+    throw new UnsupportedOperationException();
+  }
+
+  @Override
+  public String getInvocationQualifiedName() {
+    return microserviceName;
+  }
+
+  @Override
+  public String getConfigTransportName() {
+    return Const.RESTFUL;
+  }
+
+  @Override
+  public String getMicroserviceName() {
+    return microserviceName;
+  }
+
+  @Override
+  public String getAppId() {
+    return appId;
+  }
+
+  @Override
+  public String getMicroserviceVersionRule() {
+    return versionRule;
+  }
+
+  @Override
+  public void next(AsyncResponse asyncResp) throws Exception {
+    nextHandler.handle(this, asyncResp);
+  }
+}
diff --git 
a/handlers/handler-loadbalance/src/main/java/org/apache/servicecomb/loadbalance/LoadbalanceHandler.java
 
b/handlers/handler-loadbalance/src/main/java/org/apache/servicecomb/loadbalance/LoadbalanceHandler.java
index 8547af52c..20d31a452 100644
--- 
a/handlers/handler-loadbalance/src/main/java/org/apache/servicecomb/loadbalance/LoadbalanceHandler.java
+++ 
b/handlers/handler-loadbalance/src/main/java/org/apache/servicecomb/loadbalance/LoadbalanceHandler.java
@@ -150,6 +150,9 @@ public Thread newThread(Runnable r) {
 
   private String strategy = null;
 
+  public LoadbalanceHandler(DiscoveryTree discoveryTree) {
+    this.discoveryTree = discoveryTree;
+  }
 
   public LoadbalanceHandler() {
     preCheck();
@@ -317,7 +320,8 @@ public void onExecutionSuccess(ExecutionContext<Invocation> 
context, Response re
       public void onExecutionFailed(ExecutionContext<Invocation> context, 
Throwable finalException,
           ExecutionInfo info) {
         LOGGER.error("Invoke all server failed. Operation {}, e={}",
-            context.getRequest().getInvocationQualifiedName(), 
ExceptionUtils.getExceptionMessageWithoutTrace(finalException));
+            context.getRequest().getInvocationQualifiedName(),
+            ExceptionUtils.getExceptionMessageWithoutTrace(finalException));
         if (orginExecutor != null) {
           orginExecutor.execute(() -> {
             fail(finalException);
@@ -379,7 +383,7 @@ private void fail(Throwable finalException) {
               }
             });
           } catch (Exception e) {
-            LOGGER.error("execution error, msg is " + e.getMessage());
+            LOGGER.error("execution error, msg is {}", 
ExceptionUtils.getExceptionMessageWithoutTrace(e));
             f.onError(e);
           }
         });
diff --git 
a/handlers/handler-loadbalance/src/test/java/org/apache/servicecomb/loadbalance/TestLoadBalanceHandler2.java
 
b/handlers/handler-loadbalance/src/test/java/org/apache/servicecomb/loadbalance/TestLoadBalanceHandler2.java
index 339c522be..d98d672a3 100644
--- 
a/handlers/handler-loadbalance/src/test/java/org/apache/servicecomb/loadbalance/TestLoadBalanceHandler2.java
+++ 
b/handlers/handler-loadbalance/src/test/java/org/apache/servicecomb/loadbalance/TestLoadBalanceHandler2.java
@@ -27,6 +27,7 @@
 
 import org.apache.servicecomb.core.CseContext;
 import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.core.NonSwaggerInvocation;
 import org.apache.servicecomb.core.SCBEngine;
 import org.apache.servicecomb.core.Transport;
 import org.apache.servicecomb.core.definition.MicroserviceMeta;
@@ -35,15 +36,20 @@
 import org.apache.servicecomb.core.provider.consumer.ReferenceConfig;
 import org.apache.servicecomb.core.transport.TransportManager;
 import org.apache.servicecomb.foundation.test.scaffolding.config.ArchaiusUtils;
+import org.apache.servicecomb.loadbalance.filter.IsolationDiscoveryFilter;
+import org.apache.servicecomb.loadbalance.filter.ServerDiscoveryFilter;
+import org.apache.servicecomb.loadbalance.filter.ZoneAwareDiscoveryFilter;
 import org.apache.servicecomb.serviceregistry.RegistryUtils;
 import org.apache.servicecomb.serviceregistry.ServiceRegistry;
 import org.apache.servicecomb.serviceregistry.api.registry.DataCenterInfo;
 import 
org.apache.servicecomb.serviceregistry.api.registry.MicroserviceInstance;
 import org.apache.servicecomb.serviceregistry.cache.InstanceCacheManager;
+import org.apache.servicecomb.serviceregistry.discovery.DiscoveryTree;
 import org.apache.servicecomb.serviceregistry.discovery.DiscoveryTreeNode;
 import org.apache.servicecomb.swagger.invocation.AsyncResponse;
 import org.junit.After;
 import org.junit.Assert;
+import org.junit.Before;
 import org.junit.BeforeClass;
 import org.junit.Test;
 import org.mockito.Mockito;
@@ -58,6 +64,12 @@ public static void beforeClass() {
     //prepare for defineEndpointAndHandle
     
ArchaiusUtils.setProperty("servicecomb.loadbalance.userDefinedEndpoint.enabled",
 "true");
     // avoid mock
+
+  }
+
+  @Before
+  public void setUp() {
+    // clear up load balance stats
     ServiceCombLoadBalancerStats.INSTANCE.init();
   }
 
@@ -202,6 +214,147 @@ public void testZoneAwareAndIsolationFilterWorks() throws 
Exception {
     Assert.assertEquals(server.getEndpoint().getEndpoint(), 
"rest://localhost:9091");
   }
 
+  @Test
+  public void testZoneAwareAndIsolationFilterUsingMockedInvocationWorks() 
throws Exception {
+    Invocation invocation = new NonSwaggerInvocation("testApp", 
"testMicroserviceName", "0.0.0+", (inv, aysnc) -> {
+      aysnc.success("OK");
+    });
+
+    InstanceCacheManager instanceCacheManager = 
Mockito.mock(InstanceCacheManager.class);
+    ServiceRegistry serviceRegistry = Mockito.mock(ServiceRegistry.class);
+    TransportManager transportManager = Mockito.mock(TransportManager.class);
+    Transport transport = Mockito.mock(Transport.class);
+    
ArchaiusUtils.setProperty("servicecomb.loadbalance.filter.operation.enabled", 
"false");
+
+    // set up data
+    MicroserviceInstance myself = new MicroserviceInstance();
+    DataCenterInfo info = new DataCenterInfo();
+    info.setName("test");
+    info.setRegion("test-Region");
+    info.setAvailableZone("test-zone");
+    myself.setDataCenterInfo(info);
+
+    MicroserviceInstance allmatchInstance = new MicroserviceInstance();
+    info = new DataCenterInfo();
+    info.setName("test");
+    info.setRegion("test-Region");
+    info.setAvailableZone("test-zone");
+    List<String> allMatchEndpoint = new ArrayList<>();
+    allMatchEndpoint.add("rest://localhost:7090");
+    allmatchInstance.setEndpoints(allMatchEndpoint);
+    allmatchInstance.setDataCenterInfo(info);
+    allmatchInstance.setInstanceId("allmatchInstance");
+
+    MicroserviceInstance regionMatchInstance = new MicroserviceInstance();
+    info = new DataCenterInfo();
+    info.setName("test");
+    info.setRegion("test-Region");
+    info.setAvailableZone("test-zone2");
+    List<String> regionMatchEndpoint = new ArrayList<>();
+    regionMatchEndpoint.add("rest://localhost:7091");
+    regionMatchInstance.setEndpoints(regionMatchEndpoint);
+    regionMatchInstance.setDataCenterInfo(info);
+    regionMatchInstance.setInstanceId("regionMatchInstance");
+
+    MicroserviceInstance noneMatchInstance = new MicroserviceInstance();
+    info = new DataCenterInfo();
+    info.setName("test");
+    info.setRegion("test-Region2");
+    info.setAvailableZone("test-zone2");
+    List<String> noMatchEndpoint = new ArrayList<>();
+    noMatchEndpoint.add("rest://localhost:7092");
+    noneMatchInstance.setEndpoints(noMatchEndpoint);
+    noneMatchInstance.setDataCenterInfo(info);
+    noneMatchInstance.setInstanceId("noneMatchInstance");
+
+    Map<String, MicroserviceInstance> data = new HashMap<>();
+    DiscoveryTreeNode parent = new 
DiscoveryTreeNode().name("parent").data(data);
+    CseContext.getInstance().setTransportManager(transportManager);
+
+    RegistryUtils.setServiceRegistry(serviceRegistry);
+
+    when(serviceRegistry.getMicroserviceInstance()).thenReturn(myself);
+    
when(serviceRegistry.getInstanceCacheManager()).thenReturn(instanceCacheManager);
+    when(instanceCacheManager.getOrCreateVersionedCache("testApp", 
"testMicroserviceName", "0.0.0+"))
+        .thenReturn(parent);
+    when(transportManager.findTransport("rest")).thenReturn(transport);
+
+    LoadbalanceHandler handler = null;
+    LoadBalancer loadBalancer = null;
+    ServiceCombServer server = null;
+
+    DiscoveryTree discoveryTree = new DiscoveryTree();
+    discoveryTree.addFilter(new IsolationDiscoveryFilter());
+    discoveryTree.addFilter(new ZoneAwareDiscoveryFilter());
+    discoveryTree.addFilter(new ServerDiscoveryFilter());
+    discoveryTree.sort();
+    handler = new LoadbalanceHandler(discoveryTree);
+    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
+    server = loadBalancer.chooseServer(invocation);
+    Assert.assertEquals(server, null);
+
+    data.put("noneMatchInstance", noneMatchInstance);
+    parent.cacheVersion(1);
+    handler = new LoadbalanceHandler();
+    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
+    server = loadBalancer.chooseServer(invocation);
+    Assert.assertEquals(server.getEndpoint().getEndpoint(), 
"rest://localhost:7092");
+    handler.handle(invocation, (response) -> {
+      Assert.assertEquals(response.getResult(), "OK");
+    });
+
+    data.put("regionMatchInstance", regionMatchInstance);
+    parent.cacheVersion(parent.cacheVersion() + 1);
+    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
+    server = loadBalancer.chooseServer(invocation);
+    Assert.assertEquals(server.getEndpoint().getEndpoint(), 
"rest://localhost:7091");
+    handler.handle(invocation, (response) -> {
+      Assert.assertEquals(response.getResult(), "OK");
+    });
+
+    data.put("allmatchInstance", allmatchInstance);
+    parent.cacheVersion(parent.cacheVersion() + 1);
+    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
+    server = loadBalancer.chooseServer(invocation);
+    Assert.assertEquals(server.getEndpoint().getEndpoint(), 
"rest://localhost:7090");
+
+    ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
+    ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
+    ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
+    ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server);
+    ServiceCombLoadBalancerStats.INSTANCE.markFailure(server);
+
+    //if errorThresholdPercentage is 0,that means errorThresholdPercentage is 
not active.
+    
ArchaiusUtils.setProperty("servicecomb.loadbalance.isolation.errorThresholdPercentage",
 "0");
+    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
+    server = loadBalancer.chooseServer(invocation);
+    Assert.assertEquals(server.getEndpoint().getEndpoint(), 
"rest://localhost:7090");
+
+    //if errorThresholdPercentage greater than 0, it will activate.
+    
ArchaiusUtils.setProperty("servicecomb.loadbalance.isolation.errorThresholdPercentage",
 "20");
+    
ArchaiusUtils.setProperty("servicecomb.loadbalance.isolation.minIsolationTime", 
"10");
+    ServiceCombServer server2 = server;
+    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
+    server = loadBalancer.chooseServer(invocation);
+    Assert.assertEquals(server.getEndpoint().getEndpoint(), 
"rest://localhost:7091");
+    ServiceCombLoadBalancerStats.INSTANCE.markSuccess(server2);
+    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
+    server = loadBalancer.chooseServer(invocation);
+    Assert.assertEquals(server.getEndpoint().getEndpoint(), 
"rest://localhost:7091");
+    TimeUnit.MILLISECONDS.sleep(20);
+    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
+    server = loadBalancer.chooseServer(invocation);
+    Assert.assertEquals(server.getEndpoint().getEndpoint(), 
"rest://localhost:7090");
+    ServiceCombLoadBalancerStats.INSTANCE.markFailure(server2);
+    ServiceCombLoadBalancerStats.INSTANCE.markFailure(server2);
+    loadBalancer = handler.getOrCreateLoadBalancer(invocation);
+    server = loadBalancer.chooseServer(invocation);
+    Assert.assertEquals(server.getEndpoint().getEndpoint(), 
"rest://localhost:7091");
+    handler.handle(invocation, (response) -> {
+      Assert.assertEquals(response.getResult(), "OK");
+    });
+  }
+
   @Test
   public void testConfigEndpoint() {
     ReferenceConfig referenceConfig = Mockito.mock(ReferenceConfig.class);
diff --git 
a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/RestServerVerticle.java
 
b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/RestServerVerticle.java
index a17f2874a..3ac2ee7a2 100644
--- 
a/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/RestServerVerticle.java
+++ 
b/transports/transport-rest/transport-rest-vertx/src/main/java/org/apache/servicecomb/transport/rest/vertx/RestServerVerticle.java
@@ -17,6 +17,7 @@
 
 package org.apache.servicecomb.transport.rest.vertx;
 
+import java.nio.channels.ClosedChannelException;
 import java.util.List;
 import java.util.Set;
 
@@ -104,7 +105,12 @@ public void start(Future<Void> startFuture) throws 
Exception {
         }
       });
       httpServer.exceptionHandler(e -> {
-        LOGGER.error("Unexpected error in server.{}", 
ExceptionUtils.getExceptionMessageWithoutTrace(e));
+        if(e instanceof ClosedChannelException) {
+          // This is quite normal in between browser and ege, so do not print 
out.
+          LOGGER.debug("Unexpected error in server.{}", 
ExceptionUtils.getExceptionMessageWithoutTrace(e));
+        } else {
+          LOGGER.error("Unexpected error in server.{}", 
ExceptionUtils.getExceptionMessageWithoutTrace(e));
+        }
       });
       startListen(httpServer, startFuture);
     } catch (Throwable e) {


 

----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
[email protected]


> Provide a way to using handlers(e.g. LoabalanceHanler) outside handler chain
> ----------------------------------------------------------------------------
>
>                 Key: SCB-1061
>                 URL: https://issues.apache.org/jira/browse/SCB-1061
>             Project: Apache ServiceComb
>          Issue Type: New Feature
>            Reporter: liubao
>            Assignee: liubao
>            Priority: Major
>
> In Edge Service, users want to dispatch requests to non ServiceComb services 
> like tomcat webpage. And users implement a Dispatcher, and want to maintain 
> isolation, loadbalance features as ServiceComb client did. 
>  
> Here is an example on how to use : 
> https://github.com/huaweicse/cse-java-chassis-samples/blob/master/porter/gateway-service/src/main/java/com/huawei/cse/porter/gateway/UiDispatcher.java



--
This message was sent by Atlassian JIRA
(v7.6.3#76005)

Reply via email to