From 410b37318ed57896ea65929df4dbdde0d040e231 Mon Sep 17 00:00:00 2001 From: hexiaofeng Date: Fri, 10 May 2024 08:16:38 +0800 Subject: [PATCH] fix unit filter error after changed the filter order. --- .../agent/governance/invoke/RouteTarget.java | 9 ++- .../invoke/filter/route/UnitRouteFilter.java | 59 ++++++++++++------- 2 files changed, 44 insertions(+), 24 deletions(-) diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/invoke/RouteTarget.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/invoke/RouteTarget.java index 4e26e894..d669713c 100644 --- a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/invoke/RouteTarget.java +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/invoke/RouteTarget.java @@ -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 { /** @@ -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 endpoints; @@ -93,8 +98,8 @@ public class RouteTarget { public RouteTarget(List 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; diff --git a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/invoke/filter/route/UnitRouteFilter.java b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/invoke/filter/route/UnitRouteFilter.java index b1ebc1bf..e271701b 100644 --- a/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/invoke/filter/route/UnitRouteFilter.java +++ b/joylive-core/joylive-governance-api/src/main/java/com/jd/live/agent/governance/invoke/filter/route/UnitRouteFilter.java @@ -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; @@ -62,11 +63,12 @@ public class UnitRouteFilter implements RouteFilter { @Override public void filter(OutboundInvocation 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) { @@ -83,32 +85,30 @@ public void filter(OutboundInvocation invocation, * * @param 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 RouteTarget route(OutboundInvocation invocation) { + private RouteTarget route(OutboundInvocation invocation, + List 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)); } } @@ -116,14 +116,18 @@ private RouteTarget route(OutboundInvocation invo * 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 endpoints, + LiveMetadata liveMetadata, + ServiceLivePolicy livePolicy, + UnitRoute targetRoute) { + EndpointGroup instanceGroup = new EndpointGroup(endpoints); Election election = getPreferUnits(liveMetadata, targetRoute, new CandidateBuilder(invocation, instanceGroup)); List candidates = election.getCandidates(); if (election.isEmpty()) { @@ -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 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)); @@ -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 endpoints, + LiveMetadata liveMetadata) { Unit unit = liveMetadata.getCenterUnit(); UnitRule rule = liveMetadata.getUnitRule(); UnitRoute unitRoute = unit == null || rule == null ? null : rule.getUnitRoute(unit.getCode()); @@ -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); } /** @@ -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 units = liveSpace == null ? null : liveSpace.getUnits(); if (units == null || units.isEmpty()) { @@ -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 units) { + private Election getPreferUnitsWithoutCenter(LiveMetadata liveMetadata, + UnitRoute targetRoute, + CandidateBuilder builder, + List units) { Election result = new Election(); Unit localUnit = targetRoute != null ? targetRoute.getUnit() : liveMetadata.getCurrentUnit(); Candidate localCandidate = targetRoute != null ? builder.build(targetRoute) : builder.build(localUnit);