Skip to content

Commit

Permalink
Merge pull request #89
Browse files Browse the repository at this point in the history
Fix #88
  • Loading branch information
chenzhiguo authored Oct 14, 2024
2 parents f35b9cb + 349fe6f commit df6a3c7
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,10 @@ public class GovernanceConfig {

public static final String CONFIG_COUNTER_ENABLED = CONFIG_SWITCH_COUNTER + ENABLED;

public static final String CONFIG_ROUTER = CONFIG_AGENT_GOVERNANCE + ".router";
public static final String CONFIG_ROUTER_SPRING = CONFIG_ROUTER + ".spring";
public static final String CONFIG_ROUTER_SPRING_DISCOVERY_DISABLES = CONFIG_ROUTER_SPRING + ".discovery.disables";

@Config("live")
private LiveConfig liveConfig = new LiveConfig();

Expand Down
3 changes: 3 additions & 0 deletions joylive-package/src/main/assembly/config/config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,9 @@ agent:
modifyMQGroupEnabled: ${CONFIG_LANE_MODIFY_MQ_GROUP:false}
router:
virtual: ${CONFIG_VIRTUAL_SIZE:500}
spring:
discovery:
disables: ${CONFIG_SPRING_DISCOVERY_DISABLES}
phevos:
groupExpression: ${unit}-${cell}-${group}
transmission:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.jd.live.agent.core.bytekit.matcher.MatcherBuilder;
import com.jd.live.agent.core.extension.annotation.*;
import com.jd.live.agent.core.inject.annotation.Config;
import com.jd.live.agent.core.inject.annotation.Inject;
import com.jd.live.agent.core.inject.annotation.Injectable;
import com.jd.live.agent.core.plugin.definition.InterceptorDefinition;
Expand All @@ -26,6 +27,8 @@
import com.jd.live.agent.governance.invoke.InvocationContext;
import com.jd.live.agent.plugin.router.springcloud.v3.interceptor.ServiceInstanceListSupplierInterceptor;

import java.util.Set;

/**
* ServiceInstanceListSupplierPluginDefinition
*
Expand All @@ -36,10 +39,10 @@
@Extension(value = "ServiceInstanceListSupplierPluginDefinition_v3")
@ConditionalOnProperties(value = {
@ConditionalOnProperty(name = {
GovernanceConfig.CONFIG_FLOW_CONTROL_ENABLED,
GovernanceConfig.CONFIG_LIVE_ENABLED,
GovernanceConfig.CONFIG_LANE_ENABLED
}, matchIfMissing = true, relation = ConditionalRelation.OR),
@ConditionalOnProperty(name = GovernanceConfig.CONFIG_FLOW_CONTROL_ENABLED, value = "false"),
@ConditionalOnProperty(name = GovernanceConfig.CONFIG_LIVE_SPRING_ENABLED, matchIfMissing = true)
}, relation = ConditionalRelation.AND)
@ConditionalOnClass(ServiceInstanceListSupplierDefinition.TYPE_SERVICE_INSTANCE_LIST_SUPPLIER)
Expand All @@ -57,14 +60,20 @@ public class ServiceInstanceListSupplierDefinition extends PluginDefinitionAdapt
@Inject(InvocationContext.COMPONENT_INVOCATION_CONTEXT)
private InvocationContext context;

@Config(GovernanceConfig.CONFIG_ROUTER_SPRING_DISCOVERY_DISABLES)
private Set<String> disableDiscovery;

@Config(GovernanceConfig.CONFIG_FLOW_CONTROL_ENABLED)
private boolean flowControlEnabled;

public ServiceInstanceListSupplierDefinition() {
// enhance default method. so isImplementOf is not used.
this.matcher = () -> MatcherBuilder.isSubTypeOf(TYPE_SERVICE_INSTANCE_LIST_SUPPLIER);
this.interceptors = new InterceptorDefinition[]{
new InterceptorDefinitionAdapter(
MatcherBuilder.named(METHOD_GET).
and(MatcherBuilder.arguments(ARGUMENTS_GET)),
() -> new ServiceInstanceListSupplierInterceptor(context)
() -> new ServiceInstanceListSupplierInterceptor(context, disableDiscovery, flowControlEnabled)
)
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@
import org.springframework.cloud.client.loadbalancer.Request;
import org.springframework.cloud.client.loadbalancer.RequestDataContext;
import org.springframework.cloud.client.loadbalancer.RetryableRequestContext;
import org.springframework.cloud.loadbalancer.core.DelegatingServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;
import reactor.core.publisher.Flux;

import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
* ServiceInstanceListSupplierInterceptor
Expand All @@ -53,21 +56,40 @@ public class ServiceInstanceListSupplierInterceptor extends InterceptorAdaptor {

private final InvocationContext context;

public ServiceInstanceListSupplierInterceptor(InvocationContext context) {
private final Set<String> disableDiscovery;

private final boolean flowControlEnabled;

public ServiceInstanceListSupplierInterceptor(InvocationContext context, Set<String> disableDiscovery, boolean flowControlEnabled) {
this.context = context;
this.disableDiscovery = disableDiscovery;
this.flowControlEnabled = flowControlEnabled;
}

@Override
public void onEnter(ExecutableContext ctx) {
if (LOCK.get() == null) {
MethodContext mc = (MethodContext) ctx;
ServiceInstanceListSupplier target = (ServiceInstanceListSupplier) ctx.getTarget();
if (target instanceof DelegatingServiceInstanceListSupplier
&& disableDiscovery != null
&& !disableDiscovery.isEmpty()
&& disableDiscovery.contains(target.getClass().getName())) {
// disable
DelegatingServiceInstanceListSupplier delegating = (DelegatingServiceInstanceListSupplier) target;
Request<?> request = ctx.getArgument(0);
Object result = delegating.getDelegate().get(request);
mc.setResult(result);
mc.setSkip(true);
}
if (!flowControlEnabled && LOCK.get() == null) {
// Prevent duplicate calls
LOCK.set(ctx.getId());
}
}

@Override
public void onExit(ExecutableContext ctx) {
if (LOCK.get() == ctx.getId()) {
if (!flowControlEnabled && LOCK.get() == ctx.getId()) {
LOCK.remove();
}
}
Expand All @@ -81,7 +103,7 @@ public void onExit(ExecutableContext ctx) {
@SuppressWarnings("unchecked")
@Override
public void onSuccess(ExecutableContext ctx) {
if (LOCK.get() == ctx.getId()) {
if (!flowControlEnabled && LOCK.get() == ctx.getId()) {
MethodContext mc = (MethodContext) ctx;
Object[] arguments = ctx.getArguments();
Object result = mc.getResult();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import com.jd.live.agent.core.bytekit.matcher.MatcherBuilder;
import com.jd.live.agent.core.extension.annotation.*;
import com.jd.live.agent.core.inject.annotation.Config;
import com.jd.live.agent.core.inject.annotation.Inject;
import com.jd.live.agent.core.inject.annotation.Injectable;
import com.jd.live.agent.core.plugin.definition.InterceptorDefinition;
Expand All @@ -26,6 +27,8 @@
import com.jd.live.agent.governance.invoke.InvocationContext;
import com.jd.live.agent.plugin.router.springcloud.v4.interceptor.ServiceInstanceListSupplierInterceptor;

import java.util.Set;

/**
* ServiceInstanceListSupplierPluginDefinition
*
Expand All @@ -36,10 +39,10 @@
@Extension(value = "ServiceInstanceListSupplierPluginDefinition_v3")
@ConditionalOnProperties(value = {
@ConditionalOnProperty(name = {
GovernanceConfig.CONFIG_FLOW_CONTROL_ENABLED,
GovernanceConfig.CONFIG_LIVE_ENABLED,
GovernanceConfig.CONFIG_LANE_ENABLED
}, matchIfMissing = true, relation = ConditionalRelation.OR),
@ConditionalOnProperty(name = GovernanceConfig.CONFIG_FLOW_CONTROL_ENABLED, value = "false"),
@ConditionalOnProperty(name = GovernanceConfig.CONFIG_LIVE_SPRING_ENABLED, matchIfMissing = true)
}, relation = ConditionalRelation.AND)
@ConditionalOnClass(ServiceInstanceListSupplierDefinition.TYPE_SERVICE_INSTANCE_LIST_SUPPLIER)
Expand All @@ -57,14 +60,20 @@ public class ServiceInstanceListSupplierDefinition extends PluginDefinitionAdapt
@Inject(InvocationContext.COMPONENT_INVOCATION_CONTEXT)
private InvocationContext context;

@Config(GovernanceConfig.CONFIG_ROUTER_SPRING_DISCOVERY_DISABLES)
private Set<String> disableDiscovery;

@Config(GovernanceConfig.CONFIG_FLOW_CONTROL_ENABLED)
private boolean flowControlEnabled;

public ServiceInstanceListSupplierDefinition() {
// enhance default method. so isImplementOf is not used.
this.matcher = () -> MatcherBuilder.isSubTypeOf(TYPE_SERVICE_INSTANCE_LIST_SUPPLIER);
this.interceptors = new InterceptorDefinition[]{
new InterceptorDefinitionAdapter(
MatcherBuilder.named(METHOD_GET).
and(MatcherBuilder.arguments(ARGUMENTS_GET)),
() -> new ServiceInstanceListSupplierInterceptor(context)
() -> new ServiceInstanceListSupplierInterceptor(context, disableDiscovery, flowControlEnabled)
)
};
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@
import org.springframework.cloud.client.loadbalancer.Request;
import org.springframework.cloud.client.loadbalancer.RequestDataContext;
import org.springframework.cloud.client.loadbalancer.RetryableRequestContext;
import org.springframework.cloud.loadbalancer.core.DelegatingServiceInstanceListSupplier;
import org.springframework.cloud.loadbalancer.core.ServiceInstanceListSupplier;
import org.springframework.http.HttpRequest;
import org.springframework.http.HttpStatus;
import reactor.core.publisher.Flux;

import java.util.Collections;
import java.util.List;
import java.util.Set;

/**
* ServiceInstanceListSupplierInterceptor
Expand All @@ -53,21 +56,40 @@ public class ServiceInstanceListSupplierInterceptor extends InterceptorAdaptor {

private final InvocationContext context;

public ServiceInstanceListSupplierInterceptor(InvocationContext context) {
private final Set<String> disableDiscovery;

private final boolean flowControlEnabled;

public ServiceInstanceListSupplierInterceptor(InvocationContext context, Set<String> disableDiscovery, boolean flowControlEnabled) {
this.context = context;
this.disableDiscovery = disableDiscovery;
this.flowControlEnabled = flowControlEnabled;
}

@Override
public void onEnter(ExecutableContext ctx) {
if (LOCK.get() == null) {
MethodContext mc = (MethodContext) ctx;
ServiceInstanceListSupplier target = (ServiceInstanceListSupplier) ctx.getTarget();
if (target instanceof DelegatingServiceInstanceListSupplier
&& disableDiscovery != null
&& !disableDiscovery.isEmpty()
&& disableDiscovery.contains(target.getClass().getName())) {
// disable
DelegatingServiceInstanceListSupplier delegating = (DelegatingServiceInstanceListSupplier) target;
Request<?> request = ctx.getArgument(0);
Object result = delegating.getDelegate().get(request);
mc.setResult(result);
mc.setSkip(true);
}
if (!flowControlEnabled && LOCK.get() == null) {
// Prevent duplicate calls
LOCK.set(ctx.getId());
}
}

@Override
public void onExit(ExecutableContext ctx) {
if (LOCK.get() == ctx.getId()) {
if (!flowControlEnabled && LOCK.get() == ctx.getId()) {
LOCK.remove();
}
}
Expand All @@ -81,7 +103,7 @@ public void onExit(ExecutableContext ctx) {
@SuppressWarnings("unchecked")
@Override
public void onSuccess(ExecutableContext ctx) {
if (LOCK.get() == ctx.getId()) {
if (!flowControlEnabled && LOCK.get() == ctx.getId()) {
MethodContext mc = (MethodContext) ctx;
Object[] arguments = ctx.getArguments();
Object result = mc.getResult();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,9 @@ agent:
modifyMQGroupEnabled: ${CONFIG_LANE_MODIFY_MQ_GROUP:true}
router:
virtual: ${CONFIG_VIRTUAL_SIZE:500}
spring:
discovery:
disables: ${CONFIG_SPRING_DISCOVERY_DISABLES}
phevos:
groupExpression: ${unit}-${cell}-${group}
transmission:
Expand Down

0 comments on commit df6a3c7

Please sign in to comment.