Skip to content

Commit

Permalink
Improve filter performance
Browse files Browse the repository at this point in the history
  • Loading branch information
hexiaofeng committed Jan 20, 2025
1 parent 0ee9e54 commit 355eefb
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ public Chain(final Collection<? extends RouteFilter> filters) {
* @param <T> The type of the outbound request.
*/
@Override
public <T extends OutboundRequest> void filter(OutboundInvocation<T> invocation) {
public <T extends OutboundRequest> void filter(final OutboundInvocation<T> invocation) {
if (index < size) {
if (logger.isDebugEnabled()) {
log(index - 1, invocation);
Expand All @@ -101,7 +101,7 @@ public <T extends OutboundRequest> void filter(OutboundInvocation<T> invocation)
}
}

private <T extends OutboundRequest> void log(int index, OutboundInvocation<T> invocation) {
private <T extends OutboundRequest> void log(final int index, final OutboundInvocation<T> invocation) {
if (index < 0) {
logger.debug("Before apply any filter, endpoint size: " + invocation.getEndpointSize());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
public class CellFilter implements RouteFilter {

@Override
public <T extends OutboundRequest> void filter(OutboundInvocation<T> invocation, RouteFilterChain chain) {
public <T extends OutboundRequest> void filter(final OutboundInvocation<T> invocation, final RouteFilterChain chain) {
RouteTarget target = invocation.getRouteTarget();
UnitAction action = target.getUnitAction();
if (action.getType() == UnitActionType.FORWARD && forward(invocation, target)) {
Expand All @@ -73,7 +73,7 @@ public <T extends OutboundRequest> void filter(OutboundInvocation<T> invocation,
* @param target The RouteTarget where the invocation should be directed.
* @return true if the routing decision was successful and endpoints were set, false otherwise.
*/
private boolean forward(OutboundInvocation<?> invocation, RouteTarget target) {
private boolean forward(final OutboundInvocation<?> invocation, final RouteTarget target) {
ServiceMetadata serviceMetadata = invocation.getServiceMetadata();
ServiceConfig serviceConfig = serviceMetadata.getServiceConfig();
ServiceLivePolicy livePolicy = serviceMetadata.getServiceLivePolicy();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
public class RetryFilter implements RouteFilter {

@Override
public <T extends OutboundRequest> void filter(OutboundInvocation<T> invocation, RouteFilterChain chain) {
public <T extends OutboundRequest> void filter(final OutboundInvocation<T> invocation, final RouteFilterChain chain) {
RouteTarget target = invocation.getRouteTarget();
// Get the set of attempted endpoint IDs from the request
Set<String> attempts = invocation.getRequest().getAttempts();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@
public class UnitFilter implements RouteFilter {

@Override
public <T extends OutboundRequest> void filter(OutboundInvocation<T> invocation, RouteFilterChain chain) {
public <T extends OutboundRequest> void filter(final OutboundInvocation<T> invocation, final RouteFilterChain chain) {
RouteTarget target = route(invocation);
invocation.setRouteTarget(target);
UnitAction action = target.getUnitAction();
Expand All @@ -76,7 +76,7 @@ public <T extends OutboundRequest> void filter(OutboundInvocation<T> invocation,
* @param invocation The outbound invocation containing the request and related metadata.
* @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(final OutboundInvocation<T> invocation) {
UnitRule rule = invocation.getLiveMetadata().getRule();
UnitPolicy policy = rule == null ? UnitPolicy.NONE : invocation.getServiceMetadata().getUnitPolicy();
switch (policy) {
Expand All @@ -102,7 +102,7 @@ private <T extends OutboundRequest> RouteTarget route(OutboundInvocation<T> invo
* @param invocation The outbound invocation to be routed.
* @return The route target indicating the action to be taken (forward, reject, etc.).
*/
private RouteTarget routeNone(OutboundInvocation<?> invocation) {
private RouteTarget routeNone(final OutboundInvocation<?> invocation) {
LiveMetadata metadata = invocation.getLiveMetadata();
String targetSpaceId = metadata.getTargetSpaceId();
UnitRule rule = metadata.getRule();
Expand All @@ -119,7 +119,7 @@ private RouteTarget routeNone(OutboundInvocation<?> invocation) {
* @param routes the list of unit routes
* @return a set of available unit codes, or null if no routes are provided or all units are inaccessible
*/
private Set<String> getAvailableUnits(OutboundInvocation<?> invocation, List<UnitRoute> routes) {
private Set<String> getAvailableUnits(final OutboundInvocation<?> invocation, final List<UnitRoute> routes) {
if (routes != null && !routes.isEmpty()) {
Set<String> availableUnits = new HashSet<>(routes.size());
for (UnitRoute route : routes) {
Expand All @@ -138,7 +138,7 @@ private Set<String> getAvailableUnits(OutboundInvocation<?> invocation, List<Uni
* @param invocation The outbound invocation containing the request and related metadata.
* @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) {
private RouteTarget routeCenter(final OutboundInvocation<?> invocation) {
LiveMetadata metadata = invocation.getLiveMetadata();
Unit unit = metadata.getTargetCenter();
UnitRule rule = metadata.getRule();
Expand All @@ -161,7 +161,7 @@ private RouteTarget routeCenter(OutboundInvocation<?> invocation) {
* @param invocation The outbound invocation containing the request and related metadata.
* @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) {
private RouteTarget routeUnit(final OutboundInvocation<?> invocation) {
UnitRoute route = getUnitRoute(invocation);
if (route == null) {
String variable = invocation.getLiveMetadata().getVariable();
Expand All @@ -182,7 +182,7 @@ private RouteTarget routeUnit(OutboundInvocation<?> invocation) {
* @param invocation The outbound invocation to be routed.
* @return The route target indicating the action to be taken (forward, reject, etc.).
*/
private RouteTarget routeLocal(OutboundInvocation<?> invocation) {
private RouteTarget routeLocal(final OutboundInvocation<?> invocation) {
String targetSpaceId = invocation.getLiveMetadata().getTargetSpaceId();
EndpointGroup group = new EndpointGroup(invocation.getRouteTarget().filtrate(e -> e.isLiveSpace(targetSpaceId)));
Election election = getPreferUnits(invocation, group);
Expand Down Expand Up @@ -236,7 +236,7 @@ private RouteTarget routeLocal(OutboundInvocation<?> invocation) {
* @param invocation The outbound invocation containing the request and related metadata.
* @return The unit route determined by the unit rule and the provided variable function.
*/
private UnitRoute getUnitRoute(OutboundInvocation<?> invocation) {
private UnitRoute getUnitRoute(final OutboundInvocation<?> invocation) {
LiveMetadata liveMetadata = invocation.getLiveMetadata();
UnitRule rule = liveMetadata.getRule();
if (rule == null) {
Expand All @@ -254,7 +254,7 @@ private UnitRoute getUnitRoute(OutboundInvocation<?> invocation) {
* @param group The endpoint group used to build election candidates.
* @return An election object containing the preferred units for the election.
*/
private Election getPreferUnits(OutboundInvocation<?> invocation, EndpointGroup group) {
private Election getPreferUnits(final OutboundInvocation<?> invocation, final EndpointGroup group) {
UnitRoute route = getUnitRoute(invocation);
LiveMetadata metadata = invocation.getLiveMetadata();
LiveSpace liveSpace = metadata.getTargetSpace();
Expand All @@ -279,7 +279,7 @@ private Election getPreferUnits(OutboundInvocation<?> invocation, EndpointGroup
* @param builder The candidate builder used to build election candidates.
* @return An election object containing the preferred units for the election, including the center unit.
*/
private Election getPreferUnitsWithCenter(LiveMetadata metadata, UnitRoute route, CandidateBuilder builder) {
private Election getPreferUnitsWithCenter(final LiveMetadata metadata, final UnitRoute route, final CandidateBuilder builder) {
Election result = new Election();
Unit localUnit = metadata.getTargetLocalUnit();
localUnit = localUnit == null && route != null ? route.getUnit() : localUnit;
Expand All @@ -306,7 +306,8 @@ private Election getPreferUnitsWithCenter(LiveMetadata metadata, UnitRoute route
* @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 metadata, UnitRoute route, CandidateBuilder builder, List<Unit> units) {
private Election getPreferUnitsWithoutCenter(final LiveMetadata metadata, final UnitRoute route,
final CandidateBuilder builder, final List<Unit> units) {
Election result = new Election();
Unit localUnit = metadata.getTargetLocalUnit();
localUnit = localUnit == null && route != null ? route.getUnit() : localUnit;
Expand Down

0 comments on commit 355eefb

Please sign in to comment.