servicecomb-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From GitBox <...@apache.org>
Subject [GitHub] wujimin commented on a change in pull request #543: [SCB-324] Fault-Injection handler implementation
Date Wed, 28 Feb 2018 03:13:38 GMT
wujimin commented on a change in pull request #543: [SCB-324] Fault-Injection handler implementation
URL: https://github.com/apache/incubator-servicecomb-java-chassis/pull/543#discussion_r171133509
 
 

 ##########
 File path: handlers/handler-fault-injection/src/main/java/org/apache/servicecomb/faultinjection/FaultInjectionHandler.java
 ##########
 @@ -0,0 +1,222 @@
+/*
+ * 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.faultinjection;
+
+import java.util.Map;
+import java.util.concurrent.atomic.AtomicInteger;
+import java.util.concurrent.atomic.AtomicLong;
+
+import org.apache.servicecomb.core.Handler;
+import org.apache.servicecomb.core.Invocation;
+import org.apache.servicecomb.swagger.invocation.AsyncResponse;
+import org.apache.servicecomb.swagger.invocation.context.HttpStatus;
+import org.apache.servicecomb.swagger.invocation.exception.CommonExceptionData;
+import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
+import static org.apache.servicecomb.faultinjection.FaultInjectionConst.*;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Fault injection handler which injects the delay/abort for requests based on
+ * the percentage configured in service file.
+ *
+ */
+public class FaultInjectionHandler implements Handler {
+  private static final Logger LOGGER = LoggerFactory.getLogger(FaultInjectionHandler.class);
+
+  @Override
+  public void handle(Invocation invocation, AsyncResponse asyncResp) throws Exception {
+
+    //if no transport then call next handler.
+    if ((invocation.getTransport() != null) && (invocation.getTransport().getName()
!= null)) {
+
+      // prepare the key and lookup for request count.
+      String key = invocation.getTransport().getName() + invocation.getMicroserviceQualifiedName();
+
+      AtomicLong reqCount = FaultInjectionUtil.getOperMetTotalReq(key);
+      long reqCountCurrent = reqCount.get();
+      // increment the request count here after checking the delay/abort condition.
+      reqCount.incrementAndGet();
+
+      // get the config values related to delay percentage.
+      int delayPercent = getFaultInjectionConfig(invocation,
+          "delay.percent",
+          FAULT_INJECTION_DELAY_PERCENTAGE_DEFAULT);
+
+      // check fault delay condition.
+      boolean isDelay = checkFaultInjectionDelayAndAbort(reqCountCurrent, delayPercent);
+      if (isDelay) {
+        LOGGER.info("delay is added for the request by fault inject handler");
+        long delay = getFaultInjectionConfig(invocation,
+            "delay.fixedDelay",
+            FAULT_INJECTION_DELAY_DEFAULT);
+
+        Thread.sleep(delay);
+      }
+
+      // get the config values related to delay.
+      int abortPercent = getFaultInjectionConfig(invocation,
+          "abort.percent",
+          FAULT_INJECTION_ABORT_PERCENTAGE_DEFAULT);
+
+      // check fault delay condition.
+      boolean isAbort = checkFaultInjectionDelayAndAbort(reqCountCurrent, abortPercent);
 
 Review comment:
   percent is one type condition
   but not the only condition
   
   we should build mechanism for condition and action, not just hard code

----------------------------------------------------------------
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:
users@infra.apache.org


With regards,
Apache Git Services

Mime
View raw message