Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix alias governance rule #336

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,21 @@
import com.tencent.polaris.api.config.consumer.CircuitBreakerConfig;
import com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreaker;
import com.tencent.polaris.api.plugin.circuitbreaker.ResourceStat;
import com.tencent.polaris.api.plugin.circuitbreaker.entity.AbstractResource;
import com.tencent.polaris.api.plugin.circuitbreaker.entity.InstanceResource;
import com.tencent.polaris.api.plugin.circuitbreaker.entity.MethodResource;
import com.tencent.polaris.api.plugin.circuitbreaker.entity.Resource;
import com.tencent.polaris.api.plugin.circuitbreaker.entity.ServiceResource;
import com.tencent.polaris.api.plugin.circuitbreaker.entity.SubsetResource;
import com.tencent.polaris.api.plugin.compose.Extensions;
import com.tencent.polaris.api.pojo.CircuitBreakerStatus;
import com.tencent.polaris.api.pojo.CircuitBreakerStatus.Status;
import com.tencent.polaris.api.pojo.HalfOpenStatus;
import com.tencent.polaris.api.pojo.ServiceEventKey;
import com.tencent.polaris.api.pojo.ServiceEventKey.EventType;
import com.tencent.polaris.api.pojo.ServiceKey;
import com.tencent.polaris.api.pojo.ServiceRule;
import com.tencent.polaris.api.utils.StringUtils;
import com.tencent.polaris.circuitbreak.api.CircuitBreakAPI;
import com.tencent.polaris.circuitbreak.api.FunctionalDecorator;
import com.tencent.polaris.circuitbreak.api.InvokeHandler;
Expand All @@ -34,11 +44,20 @@
import com.tencent.polaris.client.api.BaseEngine;
import com.tencent.polaris.client.api.SDKContext;
import com.tencent.polaris.client.api.ServiceCallResultListener;
import com.tencent.polaris.client.flow.BaseFlow;
import com.tencent.polaris.client.flow.DefaultServiceResourceProvider;
import com.tencent.polaris.client.util.CommonValidator;
import com.tencent.polaris.logging.LoggerFactory;
import org.slf4j.Logger;

import java.util.List;
import java.util.Objects;
import java.util.Optional;

public class DefaultCircuitBreakAPI extends BaseEngine implements CircuitBreakAPI {

private static Logger LOG = LoggerFactory.getLogger(DefaultCircuitBreakAPI.class);

private ServiceCallResultChecker checker;

public DefaultCircuitBreakAPI(SDKContext sdkContext) {
Expand Down Expand Up @@ -112,7 +131,52 @@ public static void report(ResourceStat reportStat, Extensions extensions) {
if (null == circuitBreaker) {
return;
}
circuitBreaker.report(reportStat);
Resource resource = replaceResource(reportStat.getResource(), extensions);
if (LOG.isDebugEnabled()) {
LOG.debug("[CircuitBreaker] report resource old info : {} new info : {}", reportStat.getResource(), resource);
}
ResourceStat copyStat = new ResourceStat(resource, reportStat.getRetCode(), reportStat.getDelay(), reportStat.getRetStatus());
circuitBreaker.report(copyStat);
}


private static Resource replaceResource(Resource resource, Extensions extensions) {
try {
if (!(resource instanceof AbstractResource)) {
return resource;
}
ServiceKey callerService = resource.getCallerService();
if (Objects.isNull(callerService)) {
return resource;
}
if (StringUtils.isAllEmpty(callerService.getService(), callerService.getNamespace())) {
return resource;
}
DefaultServiceResourceProvider provider = new DefaultServiceResourceProvider(extensions);
ServiceRule rule = provider.getServiceRule(new ServiceEventKey(callerService, EventType.CIRCUIT_BREAKING));
ServiceKey serviceKey = Optional.ofNullable(rule.getAliasFor()).orElse(callerService);

if (resource instanceof InstanceResource) {
InstanceResource old = (InstanceResource) resource;
return new InstanceResource(old.getService(), old.getHost(), old.getPort(), serviceKey);
}
if (resource instanceof MethodResource) {
MethodResource old = (MethodResource) resource;
return new MethodResource(old.getService(), old.getMethod(), serviceKey);
}
if (resource instanceof ServiceResource) {
ServiceResource old = (ServiceResource) resource;
return new ServiceResource(old.getService(), serviceKey);
}
if (resource instanceof SubsetResource) {
SubsetResource old = (SubsetResource) resource;
return new SubsetResource(old.getService(), old.getSubset(), old.getMetadata(), serviceKey);
}
return resource;
} catch (Throwable ex) {
LOG.error("[CircuitBreaker] replace resource caller_service info fail", ex);
return resource;
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,11 @@ public interface ServiceRule {
* @return revision
*/
String getRevision();

/**
* 获取服务原始名称信息
*
* @return {@link ServiceKey}
*/
ServiceKey getAliasFor();
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,12 @@
import com.google.protobuf.TextFormat;
import com.tencent.polaris.api.pojo.RegistryCacheValue;
import com.tencent.polaris.api.pojo.ServiceEventKey.EventType;
import com.tencent.polaris.api.pojo.ServiceKey;
import com.tencent.polaris.api.pojo.ServiceRule;
import com.tencent.polaris.api.utils.StringUtils;
import com.tencent.polaris.specification.api.v1.service.manage.ServiceProto.Service;

import java.util.Objects;

/**
* 通过PB对象封装的服务信息
Expand All @@ -43,12 +48,21 @@ public class ServiceRuleByProto implements ServiceRule, RegistryCacheValue {

private final EventType eventType;

public ServiceRuleByProto(Message ruleValue, String revision, boolean loadFromFile, EventType eventType) {
private ServiceKey aliasFor;

public ServiceRuleByProto(Message ruleValue, Service aliasFor, String revision, boolean loadFromFile, EventType eventType) {
this.ruleValue = ruleValue;
this.revision = revision;
this.loadFromFile = loadFromFile;
this.initialized = true;
this.eventType = eventType;
if (Objects.nonNull(aliasFor)) {
this.aliasFor = new ServiceKey(aliasFor.getNamespace().getValue(), aliasFor.getName().getValue());
}
}

public ServiceRuleByProto(Message ruleValue, String revision, boolean loadFromFile, EventType eventType) {
this(ruleValue, null, revision, loadFromFile, eventType);
}

public ServiceRuleByProto() {
Expand All @@ -69,6 +83,14 @@ public String getRevision() {
return revision;
}

@Override
public ServiceKey getAliasFor() {
if (StringUtils.isAllEmpty(aliasFor.getService(), aliasFor.getNamespace())) {
return null;
}
return aliasFor;
}


@Override
public boolean isLoadedFromFile() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ public class ServicesByProto implements Services, RegistryCacheValue {

private final int hashCode;

private String revision;

private ServiceKey svcKey;

public ServicesByProto() {
Expand All @@ -63,6 +65,9 @@ public ServicesByProto(ResponseProto.DiscoverResponse response, boolean loadFrom

this.services = new ArrayList<>();
this.svcKey = new ServiceKey("", "");
if (Objects.nonNull(response.getService())) {
this.revision = response.getService().getRevision().getValue();
}

if (CollectionUtils.isNotEmpty(tmpServices)) {
ServiceProto.Service svc = tmpServices.get(0);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public abstract class AbstractResource implements Resource {

protected final ServiceKey service;

protected final ServiceKey callerService;
protected ServiceKey callerService;

public AbstractResource(ServiceKey service, ServiceKey callerService) {
this.service = service;
Expand All @@ -41,6 +41,10 @@ public ServiceKey getService() {
return service;
}

public void setCallerService(ServiceKey callerService) {
this.callerService = callerService;
}

@Override
public boolean equals(Object o) {
if (this == o) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,9 +43,9 @@
public class RouteInfo {

//源服务信息
private final SourceService sourceService;
private SourceService sourceService;
//目的服务信息
private final ServiceMetadata destService;
private ServiceMetadata destService;

//源路由规则
private ServiceRule sourceRouteRule;
Expand Down Expand Up @@ -132,6 +132,14 @@ public RouteInfo(SourceService sourceService, ServiceMetadata destService, Strin
this(sourceService, null, destService, null, method, serviceConfig);
}

public void setSourceService(SourceService sourceService) {
this.sourceService = sourceService;
}

public void setDestService(ServiceMetadata destService) {
this.destService = destService;
}

public MetadataFailoverType getMetadataFailoverType() {
return metadataFailoverType;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,44 @@
import com.tencent.polaris.api.plugin.PluginType;
import com.tencent.polaris.api.plugin.circuitbreaker.CircuitBreaker;
import com.tencent.polaris.api.plugin.circuitbreaker.ResourceStat;
import com.tencent.polaris.api.plugin.circuitbreaker.entity.AbstractResource;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里的类是引入了但是没有使用?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

看样子是的,我fix下

import com.tencent.polaris.api.plugin.circuitbreaker.entity.InstanceResource;
import com.tencent.polaris.api.plugin.circuitbreaker.entity.MethodResource;
import com.tencent.polaris.api.plugin.circuitbreaker.entity.Resource;
import com.tencent.polaris.api.plugin.circuitbreaker.entity.ServiceResource;
import com.tencent.polaris.api.plugin.circuitbreaker.entity.SubsetResource;
import com.tencent.polaris.api.plugin.common.InitContext;
import com.tencent.polaris.api.plugin.common.PluginTypes;
import com.tencent.polaris.api.plugin.compose.Extensions;
import com.tencent.polaris.api.plugin.detect.HealthChecker;
import com.tencent.polaris.api.pojo.CircuitBreakerStatus;
import com.tencent.polaris.api.pojo.RetStatus;
import com.tencent.polaris.api.pojo.ServiceEventKey;
import com.tencent.polaris.api.pojo.ServiceEventKey.EventType;
import com.tencent.polaris.api.pojo.ServiceKey;
import com.tencent.polaris.api.pojo.ServiceResourceProvider;
import com.tencent.polaris.api.pojo.ServiceRule;
import com.tencent.polaris.api.utils.StringUtils;
import com.tencent.polaris.client.flow.DefaultServiceResourceProvider;
import com.tencent.polaris.client.util.NamedThreadFactory;
import com.tencent.polaris.logging.LoggerFactory;
import com.tencent.polaris.specification.api.v1.fault.tolerance.CircuitBreakerProto.Level;
import org.slf4j.Logger;

import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.function.Function;

public class PolarisCircuitBreaker extends Destroyable implements CircuitBreaker {

private static final Logger LOG = LoggerFactory.getLogger(PolarisCircuitBreaker.class);

private final Map<Level, Map<Resource, ResourceCounters>> countersCache = new HashMap<>();

private final Map<Resource, ResourceHealthChecker> healthCheckCache = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.tencent.polaris.client.pojo.ServiceRuleByProto;
import com.tencent.polaris.specification.api.v1.fault.tolerance.CircuitBreakerProto.CircuitBreaker;
import com.tencent.polaris.specification.api.v1.service.manage.ResponseProto.DiscoverResponse;
import com.tencent.polaris.specification.api.v1.service.manage.ServiceProto.Service;

public class CircuitBreakCacheHandler extends AbstractCacheHandler {

Expand All @@ -48,6 +49,7 @@ public RegistryCacheValue messageToCacheValue(RegistryCacheValue oldValue, Objec
if (null != circuitBreaker) {
revision = circuitBreaker.getRevision().getValue();
}
return new ServiceRuleByProto(circuitBreaker, revision, isCacheLoaded, getTargetEventType());
Service aliasFor = discoverResponse.getAliasFor();
return new ServiceRuleByProto(circuitBreaker, aliasFor, revision, isCacheLoaded, getTargetEventType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.tencent.polaris.client.pojo.ServiceRuleByProto;
import com.tencent.polaris.specification.api.v1.fault.tolerance.FaultDetectorProto.FaultDetector;
import com.tencent.polaris.specification.api.v1.service.manage.ResponseProto.DiscoverResponse;
import com.tencent.polaris.specification.api.v1.service.manage.ServiceProto.Service;

public class FaultDetectCacheHandler extends AbstractCacheHandler {

Expand All @@ -48,6 +49,7 @@ public RegistryCacheValue messageToCacheValue(RegistryCacheValue oldValue, Objec
if (null != faultDetector) {
revision = faultDetector.getRevision();
}
return new ServiceRuleByProto(faultDetector, revision, isCacheLoaded, getTargetEventType());
Service aliasFor = discoverResponse.getAliasFor();
return new ServiceRuleByProto(faultDetector, aliasFor, revision, isCacheLoaded, getTargetEventType());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.tencent.polaris.api.pojo.ServiceEventKey.EventType;
import com.tencent.polaris.client.pojo.ServiceRuleByProto;
import com.tencent.polaris.specification.api.v1.service.manage.ResponseProto.DiscoverResponse;
import com.tencent.polaris.specification.api.v1.service.manage.ServiceProto.Service;
import com.tencent.polaris.specification.api.v1.traffic.manage.RoutingProto.Routing;

public class RoutingCacheHandler extends AbstractCacheHandler {
Expand All @@ -39,7 +40,8 @@ public RegistryCacheValue messageToCacheValue(RegistryCacheValue oldValue, Objec
if (null != routing) {
revision = routing.getRevision().getValue();
}
return new ServiceRuleByProto(routing, revision, isCacheLoaded, getTargetEventType());
Service aliasFor = discoverResponse.getAliasFor();
return new ServiceRuleByProto(routing, aliasFor, revision, isCacheLoaded, getTargetEventType());
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.tencent.polaris.api.pojo.ServiceEventKey.EventType;
import com.tencent.polaris.client.pojo.ServiceInstancesByProto;
import com.tencent.polaris.specification.api.v1.service.manage.ResponseProto.DiscoverResponse;
import com.tencent.polaris.specification.api.v1.service.manage.ServiceProto.Service;

public class ServiceInstancesCacheHandler extends AbstractCacheHandler {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import com.tencent.polaris.client.pojo.ServicesByProto;
import com.tencent.polaris.specification.api.v1.service.manage.ResponseProto.DiscoverResponse;

import java.util.Objects;


public class ServicesCacheHandler extends AbstractCacheHandler {

Expand All @@ -33,7 +35,10 @@ public EventType getTargetEventType() {

@Override
protected String getRevision(DiscoverResponse discoverResponse) {
return "";
if (Objects.isNull(discoverResponse.getService())) {
return "";
}
return discoverResponse.getService().getRevision().getValue();
}

@Override
Expand Down
Loading