Skip to content

Commit

Permalink
Update lane route
Browse files Browse the repository at this point in the history
  • Loading branch information
hexiaofeng committed Nov 7, 2024
1 parent 420c430 commit 5d274ac
Show file tree
Hide file tree
Showing 9 changed files with 233 additions and 81 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,21 @@ public static boolean isNotBlank(CharSequence cs) {
return !isBlank(cs);
}

/**
* Checks if two CharSequences are equal or if one of them is an empty string.
*
* @param cs1 the first CharSequence to compare
* @param cs2 the second CharSequence to compare
* @return true if the CharSequences are equal or if one of them is an empty string, false otherwise
*/
public static boolean isEqualsOrEmpty(CharSequence cs1, CharSequence cs2) {
if (cs1 != null && cs1.length() > 0) {
return cs1.equals(cs2);
} else {
return cs2 == null || cs2.length() == 0;
}
}

/**
* Splits the provided source string into an array of strings, using the provided character as the delimiter.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@
@Setter
public class LaneConfig {

private boolean modifyMQGroupEnabled = true;
private boolean fallbackLocationIfNoSpace = true;

private boolean autoJoinEnabled = true;
private boolean modifyMQGroupEnabled = true;

private Set<String> topics;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
import java.util.List;
import java.util.Set;

import static com.jd.live.agent.core.util.StringUtils.isEqualsOrEmpty;

/**
* Represents an endpoint in a distributed system, providing methods to access its properties and match against tag conditions.
* Endpoints are fundamental entities that can represent services, nodes, or instances within a system.
Expand Down Expand Up @@ -319,7 +321,7 @@ default boolean isCell(String liveSpaceId, String cell) {
* @return true if the lane space ID matches, false otherwise.
*/
default boolean isLaneSpace(String laneSpaceId) {
return laneSpaceId != null && laneSpaceId.equals(getLaneSpaceId());
return isEqualsOrEmpty(getLaneSpaceId(), laneSpaceId);
}

/**
Expand All @@ -329,7 +331,7 @@ default boolean isLaneSpace(String laneSpaceId) {
* @return true if the lane matches, false otherwise.
*/
default boolean isLane(String lane) {
return lane != null && lane.equals(getLane());
return isEqualsOrEmpty(getLane(), lane);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.jd.live.agent.governance.event.TrafficEvent.Direction;
import com.jd.live.agent.governance.event.TrafficEvent.TrafficEventBuilder;
import com.jd.live.agent.governance.invoke.metadata.LiveDomainMetadata;
import com.jd.live.agent.governance.invoke.metadata.parser.LaneMetadataParser.GatewayInboundLaneMetadataParser;
import com.jd.live.agent.governance.invoke.metadata.parser.LaneMetadataParser.HttpInboundLaneMetadataParser;
import com.jd.live.agent.governance.invoke.metadata.parser.LiveMetadataParser.GatewayInboundLiveMetadataParser;
import com.jd.live.agent.governance.invoke.metadata.parser.LiveMetadataParser.HttpInboundLiveMetadataParser;
Expand Down Expand Up @@ -234,6 +235,12 @@ protected void parsePolicy() {
super.parsePolicy();
}

@Override
protected LaneParser createLaneParser() {
return new GatewayInboundLaneMetadataParser(request, context.getGovernanceConfig().getLaneConfig(),
context.getApplication(), governancePolicy, domainPolicy, this);
}

@Override
protected LiveParser createLiveParser() {
return new GatewayInboundLiveMetadataParser(request, context.getGovernanceConfig().getLiveConfig(),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ protected TrafficEventBuilder configure(TrafficEventBuilder builder) {
UnitRule unitRule = liveMetadata.getRule();
Unit localUnit = liveMetadata.getLocalUnit();
Cell localCell = liveMetadata.getLocalCell();
LaneSpace laneSpace = laneMetadata.getLaneSpace();
Lane localLane = laneMetadata.getCurrentLane();
LaneSpace laneSpace = laneMetadata.getTargetSpace();
Lane localLane = laneMetadata.getLocalLane();
Lane targetLane = laneMetadata.getTargetLane();
URI uri = policyId == null ? null : policyId.getUri();
return builder.liveSpaceId(liveSpace == null ? null : liveSpace.getId()).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.jd.live.agent.core.extension.annotation.ConditionalOnProperty;
import com.jd.live.agent.core.extension.annotation.Extension;
import com.jd.live.agent.core.inject.annotation.Injectable;
import com.jd.live.agent.core.instance.GatewayRole;
import com.jd.live.agent.governance.config.GovernanceConfig;
import com.jd.live.agent.governance.invoke.OutboundInvocation;
import com.jd.live.agent.governance.invoke.RouteTarget;
Expand All @@ -45,45 +44,38 @@ public class LaneFilter implements RouteFilter {

@Override
public <T extends OutboundRequest> void filter(OutboundInvocation<T> invocation, RouteFilterChain chain) {
LaneMetadata laneMetadata = invocation.getLaneMetadata();
LaneSpace laneSpace = laneMetadata.getLaneSpace();
Lane targetLane = laneMetadata.getTargetLane();
// Retrieve the current route target
RouteTarget target = invocation.getRouteTarget();
LaneMetadata metadata = invocation.getLaneMetadata();
LaneSpace laneSpace = metadata.getTargetSpace();

// Check if a target lane is specified
if (targetLane != null) {
// Get the application and gateway role from the invocation context
GatewayRole gatewayRole = invocation.getGateway();
// Retrieve the service policy from the service metadata
if (laneSpace != null) {
// Retrieve the service policy from the service metadata
ServicePolicy servicePolicy = invocation.getServiceMetadata().getServicePolicy();
Boolean autoJoin = gatewayRole == GatewayRole.FRONTEND
? Boolean.TRUE
: (servicePolicy == null ? null : servicePolicy.getAutoLaneEnabled());
autoJoin = autoJoin == null ? laneMetadata.getLaneConfig().isAutoJoinEnabled() : autoJoin;

LanePolicy lanePolicy = servicePolicy == null ? null : servicePolicy.getLanePolicy(laneSpace.getId());
String redirect = lanePolicy == null ? null : lanePolicy.getTarget(targetLane.getCode());
Lane routeLane = redirect == null || redirect.isEmpty()
? (autoJoin ? targetLane : null)
: laneSpace.getOrDefault(redirect);

if (routeLane != null) {
// Retrieve the current route target
RouteTarget target = invocation.getRouteTarget();
String redirect = lanePolicy == null ? null : lanePolicy.getTarget(metadata.getTargetLaneId());
Lane lane = redirect == null || redirect.isEmpty() ? metadata.getTargetLane() : laneSpace.getOrDefault(redirect);
lane = lane == null ? laneSpace.getDefaultLane() : lane;

// Check if there is a default lane and if the target lane is different
if (lane != null) {
Lane defaultLane = laneSpace.getDefaultLane();
boolean withDefaultLane = defaultLane != null && targetLane != defaultLane;
boolean withDefaultLane = defaultLane != null && lane != defaultLane;

String code = lane.getCode();
// Filter the route target based on the lane space ID and route lane code
int count = target.filter(e -> e.isLane(laneSpace.getId(), routeLane.getCode()), -1, !withDefaultLane);

int count = target.filter(e -> e.isLane(laneSpace.getId(), code), -1, !withDefaultLane);
// If no matches and a default lane exists, use the default lane
if (count <= 0 && withDefaultLane) {
target.filter(e -> e.isLane(laneSpace.getId(), defaultLane.getCode()), -1, true);
}
} else {
String code = redirect == null || redirect.isEmpty() ? metadata.getTargetLaneId() : redirect;
target.filter(e -> e.isLane(laneSpace.getId(), code), -1, true);
}
} else {
target.filter(e -> e.isLaneSpace(null), -1, true);
}

// Proceed with the next filter in the chain
chain.filter(invocation);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,43 @@ public class LaneMetadata {
private LaneConfig laneConfig;

/**
* The lane space context for this invocation.
* The target lance space id for this invocation
*/
private LaneSpace laneSpace;
private String targetSpaceId;

/**
* The current lane context for this invocation.
* The target lane space context for this invocation.
*/
private Lane currentLane;
private LaneSpace targetSpace;

/**
* The target lane id for this invocation
*/
private String targetLaneId;

/**
* The target lane for this invocation.
*/
private Lane targetLane;

/**
* The local lane space id for this invocation.
*/
private String localSpaceId;

/**
* The local lance space for this invocation.
*/
private LaneSpace localSpace;

/**
* The local lane id for this invocation.
*/
private String localLaneId;

/**
* The local lane for this invocation.
*/
private Lane localLane;

}
Loading

0 comments on commit 5d274ac

Please sign in to comment.