Skip to content

Commit

Permalink
Merge d3f39df into b81196f
Browse files Browse the repository at this point in the history
  • Loading branch information
xuyiyun0929 committed Jun 12, 2018
2 parents b81196f + d3f39df commit 896532e
Show file tree
Hide file tree
Showing 16 changed files with 573 additions and 45 deletions.
@@ -0,0 +1,35 @@
/*
* 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.foundation.common.event;

public class AlarmEvent {

Type type;

public AlarmEvent(Type type) {
this.type = type;
}

public Type getType() {
return this.type;
}

public enum Type {
OPEN,
CLOSE
};
}
Expand Up @@ -31,8 +31,7 @@ private CommandKey() {
}

public static HystrixCommandGroupKey toHystrixCommandGroupKey(String type, Invocation invocation) {
return HystrixCommandGroupKey.Factory
.asKey(type + "." + invocation.getOperationMeta().getMicroserviceQualifiedName());
return CustomizeCommandGroupKey.asKey(type, invocation);
}

public static HystrixCommandKey toHystrixCommandKey(String type, Invocation invocation) {
Expand Down
@@ -0,0 +1,73 @@
/*
* 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.bizkeeper;

import org.apache.servicecomb.core.Invocation;

import com.netflix.hystrix.HystrixCommandGroupKey;
import com.netflix.hystrix.HystrixKey;
import com.netflix.hystrix.util.InternMap;

public class CustomizeCommandGroupKey extends HystrixKey.HystrixKeyDefault implements HystrixCommandGroupKey {

private String invocationType;

private String microserviceName;

private String schema;

private String operation;

public CustomizeCommandGroupKey(Invocation invocation) {
super(invocation.getInvocationType().name() + "." + invocation.getOperationMeta().getMicroserviceQualifiedName());
this.invocationType = invocation.getInvocationType().name();
this.microserviceName = invocation.getMicroserviceName();
this.schema = invocation.getSchemaId();
this.operation = invocation.getOperationName();
}

private static final InternMap<Invocation, CustomizeCommandGroupKey> intern =
new InternMap<Invocation, CustomizeCommandGroupKey>(
new InternMap.ValueConstructor<Invocation, CustomizeCommandGroupKey>() {
@Override
public CustomizeCommandGroupKey create(Invocation invocation) {
return new CustomizeCommandGroupKey(invocation);
}
});


public static HystrixCommandGroupKey asKey(String type, Invocation invocation) {
return intern.interned(invocation);
}

public String getInvocationType() {
return invocationType;
}

public String getMicroserviceName() {
return microserviceName;
}

public String getOperation() {
return operation;
}

public String getSchema() {
return schema;
}
}
@@ -0,0 +1,112 @@
/*
* 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.bizkeeper.event;


import org.apache.servicecomb.bizkeeper.CustomizeCommandGroupKey;
import org.apache.servicecomb.foundation.common.event.AlarmEvent;

import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixCommandMetrics;

public class CircutBreakerEvent extends AlarmEvent {

private String role;

private String microservice;

private String schema;

private String operation;

//当前总请求数
private long currentTotalRequest;

//当前请求出错计数
private long currentErrorCount;

//当前请求出错百分比
private long currentErrorPercentage;

private int requestVolumeThreshold;

private int sleepWindowInMilliseconds;

private int errorThresholdPercentage;

public CircutBreakerEvent(HystrixCommandKey commandKey, Type type) {
super(type);
HystrixCommandMetrics hystrixCommandMetrics =
HystrixCommandMetrics.getInstance(commandKey);
if (hystrixCommandMetrics != null) {
CustomizeCommandGroupKey customizeCommandGroupKey =
(CustomizeCommandGroupKey) hystrixCommandMetrics.getCommandGroup();
this.microservice = customizeCommandGroupKey.getMicroserviceName();
this.role = customizeCommandGroupKey.getInvocationType();
this.schema = customizeCommandGroupKey.getSchema();
this.operation = customizeCommandGroupKey.getOperation();
this.currentTotalRequest = hystrixCommandMetrics.getHealthCounts().getTotalRequests();
this.currentErrorPercentage = hystrixCommandMetrics.getHealthCounts().getErrorCount();
this.currentErrorCount = hystrixCommandMetrics.getHealthCounts().getErrorPercentage();
this.requestVolumeThreshold = hystrixCommandMetrics.getProperties().circuitBreakerRequestVolumeThreshold().get();
this.sleepWindowInMilliseconds =
hystrixCommandMetrics.getProperties().circuitBreakerSleepWindowInMilliseconds().get();
this.errorThresholdPercentage =
hystrixCommandMetrics.getProperties().circuitBreakerErrorThresholdPercentage().get();
}
}

public String getRole() {
return role;
}

public String getMicroservice() {
return microservice;
}

public String getSchema() {
return schema;
}

public String getOperation() {
return operation;
}

public long getCurrentTotalRequest() {
return currentTotalRequest;
}

public long getCurrentErrorCount() {
return currentErrorCount;
}

public long getCurrentErrorPercentage() {
return currentErrorPercentage;
}

public int getRequestVolumeThreshold() {
return requestVolumeThreshold;
}

public int getSleepWindowInMilliseconds() {
return sleepWindowInMilliseconds;
}

public int getErrorThresholdPercentage() {
return errorThresholdPercentage;
}
}
@@ -0,0 +1,61 @@
/*
* 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.bizkeeper.event;

import java.util.concurrent.atomic.AtomicBoolean;

import org.apache.servicecomb.foundation.common.event.EventManager;
import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx;
import org.apache.servicecomb.foundation.common.event.AlarmEvent.Type;

import com.google.common.eventbus.EventBus;
import com.netflix.hystrix.HystrixCommandKey;
import com.netflix.hystrix.HystrixEventType;
import com.netflix.hystrix.strategy.eventnotifier.HystrixEventNotifier;

public class CircutBreakerEventNotifier extends HystrixEventNotifier {

/**
* 使用circuitFlag来记录被熔断的接口
*/
private ConcurrentHashMapEx<String, AtomicBoolean> circuitFlag = new ConcurrentHashMapEx<>();

EventBus eventBus = EventManager.getEventBus();

@Override
public void markEvent(HystrixEventType eventType, HystrixCommandKey key) {
String keyName = key.name();
AtomicBoolean flag = circuitFlag.computeIfAbsent(keyName, k -> new AtomicBoolean());
switch (eventType) {
case SHORT_CIRCUITED:
if (flag.compareAndSet(false, true)) {
eventBus.post(new CircutBreakerEvent(key, Type.OPEN));
}
break;

case SUCCESS:
if (flag.compareAndSet(true, false)) {
eventBus.post(new CircutBreakerEvent(key, Type.CLOSE));
}
break;

default:
break;
}
}
}
@@ -0,0 +1 @@
hystrix.plugin.HystrixEventNotifier.implementation=org.apache.servicecomb.bizkeeper.event.CircutBreakerEventNotifier
Expand Up @@ -19,6 +19,7 @@

import org.apache.servicecomb.core.Invocation;
import org.apache.servicecomb.core.definition.OperationMeta;
import org.apache.servicecomb.swagger.invocation.InvocationType;
import org.apache.servicecomb.swagger.invocation.Response;
import org.apache.servicecomb.swagger.invocation.exception.InvocationException;
import org.junit.Assert;
Expand All @@ -39,7 +40,7 @@ public void testGetCacheKeyProvider() {
Invocation invocation = Mockito.mock(Invocation.class);
Mockito.when(invocation.getOperationMeta()).thenReturn(Mockito.mock(OperationMeta.class));
Mockito.when(invocation.getOperationMeta().getMicroserviceQualifiedName()).thenReturn("test1");

Mockito.when(invocation.getInvocationType()).thenReturn(InvocationType.PRODUCER);
HystrixCommandProperties.Setter setter = HystrixCommandProperties.Setter()
.withRequestCacheEnabled(true)
.withRequestLogEnabled(false);
Expand Down Expand Up @@ -72,7 +73,7 @@ public void testResumeWithFallbackProvider() {
Invocation invocation = Mockito.mock(Invocation.class);
Mockito.when(invocation.getOperationMeta()).thenReturn(Mockito.mock(OperationMeta.class));
Mockito.when(invocation.getOperationMeta().getMicroserviceQualifiedName()).thenReturn("test1");

Mockito.when(invocation.getInvocationType()).thenReturn(InvocationType.PRODUCER);
HystrixCommandProperties.Setter setter = HystrixCommandProperties.Setter()
.withRequestCacheEnabled(true)
.withRequestLogEnabled(false);
Expand All @@ -93,7 +94,7 @@ public void testConstructProvider() {
Invocation invocation = Mockito.mock(Invocation.class);
Mockito.when(invocation.getOperationMeta()).thenReturn(Mockito.mock(OperationMeta.class));
Mockito.when(invocation.getOperationMeta().getMicroserviceQualifiedName()).thenReturn("test1");

Mockito.when(invocation.getInvocationType()).thenReturn(InvocationType.PRODUCER);
HystrixCommandProperties.Setter setter = HystrixCommandProperties.Setter()
.withRequestCacheEnabled(true)
.withRequestLogEnabled(false);
Expand All @@ -114,7 +115,7 @@ public void testGetCacheKeyWithContextInitializedProvider() {
Invocation invocation = Mockito.mock(Invocation.class);
Mockito.when(invocation.getOperationMeta()).thenReturn(Mockito.mock(OperationMeta.class));
Mockito.when(invocation.getOperationMeta().getMicroserviceQualifiedName()).thenReturn("test1");

Mockito.when(invocation.getInvocationType()).thenReturn(InvocationType.PRODUCER);
HystrixCommandProperties.Setter setter = HystrixCommandProperties.Setter()
.withRequestCacheEnabled(true)
.withRequestLogEnabled(false);
Expand All @@ -136,7 +137,7 @@ public void testGetCacheKeyConsumer() {
Invocation invocation = Mockito.mock(Invocation.class);
Mockito.when(invocation.getOperationMeta()).thenReturn(Mockito.mock(OperationMeta.class));
Mockito.when(invocation.getOperationMeta().getMicroserviceQualifiedName()).thenReturn("test1");

Mockito.when(invocation.getInvocationType()).thenReturn(InvocationType.CONSUMER);
HystrixCommandProperties.Setter setter = HystrixCommandProperties.Setter()
.withRequestCacheEnabled(true)
.withRequestLogEnabled(false);
Expand Down Expand Up @@ -169,7 +170,7 @@ public void testResumeWithFallbackConsumer() {
Invocation invocation = Mockito.mock(Invocation.class);
Mockito.when(invocation.getOperationMeta()).thenReturn(Mockito.mock(OperationMeta.class));
Mockito.when(invocation.getOperationMeta().getMicroserviceQualifiedName()).thenReturn("test1");

Mockito.when(invocation.getInvocationType()).thenReturn(InvocationType.CONSUMER);
HystrixCommandProperties.Setter setter = HystrixCommandProperties.Setter()
.withRequestCacheEnabled(true)
.withRequestLogEnabled(false);
Expand All @@ -190,7 +191,7 @@ public void testConstructConsumer() {
Invocation invocation = Mockito.mock(Invocation.class);
Mockito.when(invocation.getOperationMeta()).thenReturn(Mockito.mock(OperationMeta.class));
Mockito.when(invocation.getOperationMeta().getMicroserviceQualifiedName()).thenReturn("test1");

Mockito.when(invocation.getInvocationType()).thenReturn(InvocationType.CONSUMER);
HystrixCommandProperties.Setter setter = HystrixCommandProperties.Setter()
.withRequestCacheEnabled(true)
.withRequestLogEnabled(false);
Expand All @@ -211,7 +212,7 @@ public void testGetCacheKeyWithContextInitializedConsumer() {
Invocation invocation = Mockito.mock(Invocation.class);
Mockito.when(invocation.getOperationMeta()).thenReturn(Mockito.mock(OperationMeta.class));
Mockito.when(invocation.getOperationMeta().getMicroserviceQualifiedName()).thenReturn("test1");

Mockito.when(invocation.getInvocationType()).thenReturn(InvocationType.CONSUMER);
HystrixCommandProperties.Setter setter = HystrixCommandProperties.Setter()
.withRequestCacheEnabled(true)
.withRequestLogEnabled(false);
Expand Down

0 comments on commit 896532e

Please sign in to comment.