[GitHub] liubao68 commented on a change in pull request #543: [SCB-324] Fault-Injection handler implementation
liubao68 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_r165897787 ## File path: handlers/handler-fault-injection/src/main/java/org/apache/servicecomb/faultinjection/FaultInjectionConst.java ## @@ -0,0 +1,52 @@ +/* + * 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; + +/** + * Handles the all constant values for fault injection. + */ +public class FaultInjectionConst { + public static final int FAULT_INJECTION_DELAY_DEFAULT = 5; + + public static final int FAULT_INJECTION_DELAY_PERCENTAGE_DEFAULT = 100; + + public static final int FAULT_INJECTION_ABORT_PERCENTAGE_DEFAULT = 100; + + public static final int FAULT_INJECTION_ABORT_ERROR_MSG_DEFAULT = 421; + + public static final int FAULT_INJECTION_CFG_NULL = -1; + + public static final String CONSUMER_FAULTINJECTION = "cse.governance.Consumer."; + + public static final String CONSUMER_FAULTINJECTION_OPERATION = "cse.governance.Consumer.operations."; + + public static final String CONSUMER_FAULTINJECTION_SCHEMAS = "cse.governance.Consumer.schemas."; + + public static final String CONSUMER_FAULTINJECTION_GLOBAL = "cse.governance.Consumer._global."; Review comment: This is different from the docs: global: cse.governance.Consumer.global.policy.fault.protocols.rest. Service: cse.governance.Consumer.MyTestServiceName.policy.fault.protocols.rest. Schema: cse.governance.Consumer.MyTestServiceName.schemas.MySchemaName.policy.fault.protocols.rest. Operations: cse.governance.Consumer.MyTestServiceName.schemas.MySchemaName.operations.MyOperation.policy.fault.protocols.rest. Please confirm. Your layers may have conflicts because each service may have same operation 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: us...@infra.apache.org With regards, Apache Git Services
[GitHub] liubao68 commented on a change in pull request #543: [SCB-324] Fault-Injection handler implementation
liubao68 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_r165898966 ## File path: handlers/handler-fault-injection/src/main/java/org/apache/servicecomb/faultinjection/FaultInjectionHandler.java ## @@ -0,0 +1,216 @@ +/* + * 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 { +// 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); +if (isAbort) { + // get the config values related to delay percentage. + int errorCode = getFaultInjectionConfig(invocation, + "abort.httpStatus", + FAULT_INJECTION_ABORT_ERROR_MSG_DEFAULT); + // if request need to be abort then return failure with given error code + CommonExceptionData errorData = new CommonExceptionData("aborted by fault inject"); + asyncResp.consumerFail( + new InvocationException(new HttpStatus(errorCode, "aborted by fault inject"), errorData)); + return; +} + +// if no delay and no abort then continue to next handler. +invocation.next(asyncResp); + } + + /** + * It will check the delay/abort condition based on request count and percentage + * received. + * + * @param reqCount + * @param percentage + * @param key + * @return true/false + */ + private boolean checkFaultInjectionDelayAndAbort(long reqCount, int percentage) { +/* + * Example: delay/abort percentage configured is 10% and Get the count(suppose + * if it is 10th request) from map and calculate resultNew(10th request) and + * requestOld(9th request). Like this for every request it will calculate + * current request count and previous count. if both not matched need to add + * delay/abort otherwise no need to add. + */ + +// calculate the value with current request count. +
[GitHub] liubao68 commented on a change in pull request #543: [SCB-324] Fault-Injection handler implementation
liubao68 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_r165897787 ## File path: handlers/handler-fault-injection/src/main/java/org/apache/servicecomb/faultinjection/FaultInjectionConst.java ## @@ -0,0 +1,52 @@ +/* + * 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; + +/** + * Handles the all constant values for fault injection. + */ +public class FaultInjectionConst { + public static final int FAULT_INJECTION_DELAY_DEFAULT = 5; + + public static final int FAULT_INJECTION_DELAY_PERCENTAGE_DEFAULT = 100; + + public static final int FAULT_INJECTION_ABORT_PERCENTAGE_DEFAULT = 100; + + public static final int FAULT_INJECTION_ABORT_ERROR_MSG_DEFAULT = 421; + + public static final int FAULT_INJECTION_CFG_NULL = -1; + + public static final String CONSUMER_FAULTINJECTION = "cse.governance.Consumer."; + + public static final String CONSUMER_FAULTINJECTION_OPERATION = "cse.governance.Consumer.operations."; + + public static final String CONSUMER_FAULTINJECTION_SCHEMAS = "cse.governance.Consumer.schemas."; + + public static final String CONSUMER_FAULTINJECTION_GLOBAL = "cse.governance.Consumer._global."; Review comment: This is different from the docs: global: cse.governance.Consumer.global.policy.fault.protocols.rest. Service: cse.governance.Consumer.MyTestServiceName.policy.fault.protocols.rest. Schema: cse.governance.Consumer.MyTestServiceName.schemas.MySchemaName.policy.fault.protocols.rest. Operations: cse.governance.Consumer.MyTestServiceName.schemas.MySchemaName.operations.MyOperation.policy.fault.protocols.rest. Please confirm. Your layers may have conflicts because each service may have some operation 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: us...@infra.apache.org With regards, Apache Git Services
[GitHub] liubao68 commented on a change in pull request #543: [SCB-324] Fault-Injection handler implementation
liubao68 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_r165896361 ## File path: handlers/handler-fault-injection/src/main/java/org/apache/servicecomb/faultinjection/FaultInjectionHandler.java ## @@ -0,0 +1,216 @@ +/* + * 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 { +// prepare the key and lookup for request count. +String key = invocation.getTransport().getName() + invocation.getMicroserviceQualifiedName(); Review comment: transport may be empty 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: us...@infra.apache.org With regards, Apache Git Services
[GitHub] liubao68 commented on a change in pull request #543: [SCB-324] Fault-Injection handler implementation
liubao68 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_r165895737 ## File path: handlers/handler-fault-injection/src/main/java/org/apache/servicecomb/faultinjection/FaultInjectionConfig.java ## @@ -0,0 +1,58 @@ +/* + * 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.LinkedList; +import java.util.List; +import java.util.concurrent.atomic.AtomicInteger; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import com.netflix.config.DynamicIntProperty; +import com.netflix.config.DynamicPropertyFactory; + +/** + * Handles the fault injection configuration read from micro service file/config + * center. + */ +public final class FaultInjectionConfig { + + private static final Logger LOGGER = LoggerFactory.getLogger(FaultInjectionConfig.class); + + // key is configuration parameter. + public static List cfgCallback = new LinkedList<>(); + + public int getConfigVal(String config, int defaultValue) { +DynamicIntProperty dynamicIntProperty = DynamicPropertyFactory.getInstance().getIntProperty(config, +defaultValue); +if (!cfgCallback.contains(config)) { + cfgCallback.add(config); + dynamicIntProperty.addCallback(() -> { +int newValue = dynamicIntProperty.get(); +String cfgName = dynamicIntProperty.getName(); + +//store the value in config center map and check for next requests. +FaultInjectionUtil.setConfigCenterValue(cfgName, new AtomicInteger(newValue)); +LOGGER.info("{} changed from {} to {}", cfgName, dynamicIntProperty, newValue); Review comment: dynamicIntProperty is not an integer 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: us...@infra.apache.org With regards, Apache Git Services