Skip to content

Commit

Permalink
fix unit filter error after changed the filter order.
Browse files Browse the repository at this point in the history
  • Loading branch information
hexiaofeng committed May 10, 2024
1 parent f653a33 commit 410b373
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 24 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
* used in a workflow or task scheduling system. It provides functionalities to
* handle a list of endpoints, perform actions on units, and manage routing logic.
*/
@Getter
public class RouteTarget {

/**
Expand All @@ -51,32 +50,38 @@ public class RouteTarget {
/**
* The unit group associated with the unit in this route target.
*/
@Getter
private final UnitGroup unitGroup;

/**
* The unit that is the subject of the action in this route target.
*/
@Getter
private final Unit unit;

/**
* The action to be performed on the unit.
*/
@Getter
private final UnitAction unitAction;

/**
* The route associated with the unit.
*/
@Getter
private final UnitRoute unitRoute;

/**
* The cell route associated with this route target.
*/
@Getter
@Setter
private CellRoute cellRoute;

/**
* A list of endpoints that this route target is currently operating on.
*/
@Getter
@Setter
private List<? extends Endpoint> endpoints;

Expand All @@ -93,8 +98,8 @@ public class RouteTarget {
public RouteTarget(List<? extends Endpoint> instances, EndpointGroup instanceGroup,
Unit unit, UnitAction unitAction, UnitRoute unitRoute, CellRoute cellRoute) {
this.instances = instances == null && instanceGroup != null ? instanceGroup.getEndpoints() : instances;
this.instanceGroup = instanceGroup == null && instances != null ? new EndpointGroup(instances) : instanceGroup;
this.unit = unit == null && unitRoute != null ? unitRoute.getUnit() : unit;
this.instanceGroup = instanceGroup == null && instances != null && this.unit != null ? new EndpointGroup(instances) : instanceGroup;
this.unitGroup = this.instanceGroup == null || this.unit == null ? null : this.instanceGroup.getUnitGroup(this.unit.getCode());
this.unitAction = unitAction;
this.unitRoute = unitRoute;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.jd.live.agent.governance.config.ServiceConfig;
import com.jd.live.agent.governance.event.TrafficEvent;
import com.jd.live.agent.governance.event.TrafficEvent.ActionType;
import com.jd.live.agent.governance.instance.Endpoint;
import com.jd.live.agent.governance.instance.EndpointGroup;
import com.jd.live.agent.governance.instance.UnitGroup;
import com.jd.live.agent.governance.invoke.OutboundInvocation;
Expand Down Expand Up @@ -62,11 +63,12 @@ public class UnitRouteFilter implements RouteFilter {

@Override
public <T extends OutboundRequest> void filter(OutboundInvocation<T> invocation, RouteFilterChain chain) {
RouteTarget target = invocation.getRouteTarget();
LiveSpace liveSpace = invocation.getLiveMetadata().getLiveSpace();
if (liveSpace != null) {
invocation.setInstances(RouteTarget.filter(invocation.getInstances(), e -> e.isLiveSpace(liveSpace.getId())));
target.filter(e -> e.isLiveSpace(liveSpace.getId()), 0, true);
}
RouteTarget target = route(invocation);
target = route(invocation, target.getEndpoints());
invocation.setRouteTarget(target);
UnitAction action = target.getUnitAction();
if (action.getType() == UnitActionType.FORWARD) {
Expand All @@ -83,47 +85,49 @@ public <T extends OutboundRequest> void filter(OutboundInvocation<T> invocation,
*
* @param <T> The type parameter of the outbound request.
* @param invocation The outbound invocation containing the request and related metadata.
* @param endpoints A list of potential endpoints to which the request can be forwarded if routing conditions are satisfied.
* @return The route target that specifies the instances to route the request to.
*/
private <T extends OutboundRequest> RouteTarget route(OutboundInvocation<T> invocation) {
private <T extends OutboundRequest> RouteTarget route(OutboundInvocation<T> invocation,
List<? extends Endpoint> endpoints) {
LiveMetadata liveMetadata = invocation.getLiveMetadata();
ServiceMetadata serviceMetadata = invocation.getServiceMetadata();
ServicePolicy servicePolicy = serviceMetadata.getServicePolicy();
ServiceLivePolicy livePolicy = servicePolicy == null ? null : servicePolicy.getLivePolicy();
UnitPolicy unitPolicy = livePolicy == null ? UnitPolicy.NONE : livePolicy.getUnitPolicy();
if (liveMetadata.getUnitRule() == null) {
// The unitization is not enabled
return RouteTarget.forward(invocation.getInstances());
}
UnitPolicy unitPolicy = livePolicy == null || liveMetadata.getUnitRule() == null ? UnitPolicy.NONE : livePolicy.getUnitPolicy();
switch (unitPolicy) {
case NONE:
// Arbitrary call
return RouteTarget.forward(invocation.getInstances());
return RouteTarget.forward(endpoints);
case CENTER:
// Guiding center unit
return routeCenter(invocation, liveMetadata);
return routeCenter(invocation, endpoints, liveMetadata);
case UNIT:
// Guiding standard unit
return routeUnit(invocation, liveMetadata, getUnitRoute(invocation));
return routeUnit(invocation, endpoints, liveMetadata, getUnitRoute(invocation));
case PREFER_LOCAL_UNIT:
default:
// Priority principle for current unit
return routeLocal(invocation, liveMetadata, livePolicy, getUnitRoute(invocation));
return routeLocal(invocation, endpoints, liveMetadata, livePolicy, getUnitRoute(invocation));
}
}

/**
* Routes an outbound invocation to a local unit based on the provided live metadata, live policy, and target route.
*
* @param invocation The outbound invocation to be routed.
* @param endpoints A list of potential endpoints to which the request can be forwarded if routing conditions are satisfied.
* @param liveMetadata The live metadata associated with the request.
* @param livePolicy The service live policy used to determine routing behavior.
* @param targetRoute The target unit route determined by the routing logic.
* @return The route target indicating the action to be taken (forward, reject, etc.).
*/
private RouteTarget routeLocal(OutboundInvocation<?> invocation, LiveMetadata liveMetadata,
ServiceLivePolicy livePolicy, UnitRoute targetRoute) {
EndpointGroup instanceGroup = new EndpointGroup(invocation.getInstances());
private RouteTarget routeLocal(OutboundInvocation<?> invocation,
List<? extends Endpoint> endpoints,
LiveMetadata liveMetadata,
ServiceLivePolicy livePolicy,
UnitRoute targetRoute) {
EndpointGroup instanceGroup = new EndpointGroup(endpoints);
Election election = getPreferUnits(liveMetadata, targetRoute, new CandidateBuilder(invocation, instanceGroup));
List<Candidate> candidates = election.getCandidates();
if (election.isEmpty()) {
Expand Down Expand Up @@ -185,11 +189,15 @@ private UnitRoute getUnitRoute(OutboundInvocation<?> invocation) {
* Routes the outbound request to the specified unit if it is accessible.
*
* @param invocation The outbound invocation containing the request and related metadata.
* @param endpoints A list of potential endpoints to which the request can be forwarded if routing conditions are satisfied.
* @param liveMetadata The live metadata associated with the request.
* @param targetRoute The target unit route determined by the routing logic.
* @return The route target indicating the instances to forward the request to, or a rejection if the unit is not accessible.
*/
private RouteTarget routeUnit(OutboundInvocation<?> invocation, LiveMetadata liveMetadata, UnitRoute targetRoute) {
private RouteTarget routeUnit(OutboundInvocation<?> invocation,
List<? extends Endpoint> endpoints,
LiveMetadata liveMetadata,
UnitRoute targetRoute) {
if (targetRoute == null) {
String variable = liveMetadata.getVariable();
return RouteTarget.reject(invocation.getError(variable == null || variable.isEmpty() ? REJECT_NO_VARIABLE : REJECT_NO_UNIT_ROUTE));
Expand All @@ -198,17 +206,20 @@ private RouteTarget routeUnit(OutboundInvocation<?> invocation, LiveMetadata liv
if (!invocation.isAccessible(unit)) {
return RouteTarget.reject(unit, invocation.getError(REJECT_UNIT_NOT_ACCESSIBLE, unit.getCode()));
}
return RouteTarget.forward(invocation.getInstances(), targetRoute);
return RouteTarget.forward(endpoints, targetRoute);
}

/**
* Routes the outbound request to the center unit if it is accessible and a valid unit route is found.
*
* @param invocation The outbound invocation containing the request and related metadata.
* @param endpoints A list of potential endpoints to which the request can be forwarded if routing conditions are satisfied.
* @param liveMetadata The live metadata associated with the request.
* @return The route target indicating the instances to forward the request to, or a rejection if the center unit is not accessible or no unit route is found.
*/
private RouteTarget routeCenter(OutboundInvocation<?> invocation, LiveMetadata liveMetadata) {
private RouteTarget routeCenter(OutboundInvocation<?> invocation,
List<? extends Endpoint> endpoints,
LiveMetadata liveMetadata) {
Unit unit = liveMetadata.getCenterUnit();
UnitRule rule = liveMetadata.getUnitRule();
UnitRoute unitRoute = unit == null || rule == null ? null : rule.getUnitRoute(unit.getCode());
Expand All @@ -219,7 +230,7 @@ private RouteTarget routeCenter(OutboundInvocation<?> invocation, LiveMetadata l
} else if (unitRoute == null) {
return RouteTarget.reject(unit, invocation.getError(REJECT_NO_UNIT_ROUTE, unit.getCode()));
}
return RouteTarget.forward(invocation.getInstances(), unitRoute);
return RouteTarget.forward(endpoints, unitRoute);
}

/**
Expand All @@ -230,7 +241,9 @@ private RouteTarget routeCenter(OutboundInvocation<?> invocation, LiveMetadata l
* @param builder The candidate builder used to build election candidates.
* @return An election object containing the preferred units for the election.
*/
private Election getPreferUnits(LiveMetadata liveMetadata, UnitRoute targetRoute, CandidateBuilder builder) {
private Election getPreferUnits(LiveMetadata liveMetadata,
UnitRoute targetRoute,
CandidateBuilder builder) {
LiveSpace liveSpace = liveMetadata.getLiveSpace();
List<Unit> units = liveSpace == null ? null : liveSpace.getUnits();
if (units == null || units.isEmpty()) {
Expand All @@ -253,8 +266,10 @@ private Election getPreferUnits(LiveMetadata liveMetadata, UnitRoute targetRoute
* @param units The list of available units to consider for the election.
* @return An election object containing the preferred units for the election.
*/
private Election getPreferUnitsWithoutCenter(LiveMetadata liveMetadata, UnitRoute targetRoute,
CandidateBuilder builder, List<Unit> units) {
private Election getPreferUnitsWithoutCenter(LiveMetadata liveMetadata,
UnitRoute targetRoute,
CandidateBuilder builder,
List<Unit> units) {
Election result = new Election();
Unit localUnit = targetRoute != null ? targetRoute.getUnit() : liveMetadata.getCurrentUnit();
Candidate localCandidate = targetRoute != null ? builder.build(targetRoute) : builder.build(localUnit);
Expand Down

0 comments on commit 410b373

Please sign in to comment.