Skip to content

Refactor: Decouple load balance metrics collection from DividePlugin #6426

Description

@hengyuss

Is there an existing issue for this?

  • I have searched the existing issues

Current Behavior

Currently, DividePlugin contains strategy-specific callback logic for P2C and ShortestResponse load balance algorithms:

if (ruleHandle.getLoadBalance().equals(P2C)) {
    return chain.execute(exchange)
        .doOnSuccess(e -> responseTrigger(upstream))
        .doOnError(throwable -> responseTrigger(upstream));
} else if (ruleHandle.getLoadBalance().equals(SHORTEST_RESPONSE)) {
    beginTime = System.currentTimeMillis();
    return chain.execute(exchange)
        .doOnSuccess(e -> successResponseTrigger(upstream));
}
return chain.execute(exchange);

This design has several issues:

  1. Violates Single Responsibility Principle: DividePlugin's core responsibility is selecting an upstream and writing routing info into exchange. Metrics collection for specific load balance strategies should not be mixed in.
  2. Violates Open/Closed Principle: Adding a new load balance strategy that requires runtime metrics (e.g., adaptive load balancing) would require modifying DividePlugin, rather than simply implementing a new LoadBalance class.
  3. Thread safety issue: beginTime is an instance variable shared across concurrent requests, which is not thread-safe. It should be stored per-request (e.g., in exchange attributes).

Expected Behavior

Add onSuccess and onError callback methods to the LoadBalance interface with default empty implementations:

public interface LoadBalance {
    Upstream select(List<Upstream> upstreamList, LoadBalanceData data);

    default void onSuccess(Upstream upstream) {}

    default void onError(Upstream upstream) {}
}

return chain.execute(exchange)
    .doOnSuccess(e -> loadBalance.onSuccess(upstream))
    .doOnError(t -> loadBalance.onError(upstream));

Steps To Reproduce

No response

Environment

ShenYu version(s):latest

Debug logs

No response

Anything else?

i will do it

Metadata

Metadata

Assignees

No one assigned

    Labels

    type: bugSomething isn't working

    Type

    No type

    Fields

    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions