Skip to content

How to properly use "weighted_target_experimental" load balancing policy?Β #12265

@davidecaroselli

Description

@davidecaroselli

Hi gRPC Java community!

I'm trying to configure my gRPC Java application to use weighted_target_experimental load balancing policy, however I cannot find anywhere how to specify the targetName for a given EquivalentAddressGroup. Here's my setup, and what I'm trying to achieve.

I have a custom NameResolver implementation, with the core update policy right now looking like this:

private void refreshInternal() throws Throwable {
    List<EquivalentAddressGroup> addresses = new ArrayList<>();

    for (GrpcNode server : registry.nodes()) {
        EquivalentAddressGroup addressGroup = new EquivalentAddressGroup(server.addr());
        addresses.add(addressGroup);
    }

    ResolutionResult result = ResolutionResult.newBuilder()
        .setAddressesOrError(StatusOr.fromValue(addresses))
        .build();

    this.listener2.onResult(result);
}

So, for what I have understand, the first thing to do is provide a configuration for WeightedTargetLoadBalancerProvider in ResolutionResult like this:

String config = """
    {
        "loadBalancingConfig": [{
            "weighted_target_experimental": {
                "targets": {
                    "target_a": { "weight": 75, "childPolicy": [{ "round_robin": {} }] },
                    "target_b": { "weight": 25, "childPolicy": [{ "round_robin": {} }] }
                }
            }
        }]
    }
    """;

Map<?, ?> parsedConfig = new Gson().fromJson(config, Map.class);

ResolutionResult result = ResolutionResult.newBuilder()
    .setServiceConfig(ConfigOrError.fromConfig(parsedConfig))
    .setAddressesOrError(StatusOr.fromValue(addresses))
    .build();

But now, my question: as far as I understand, I'm supposed to specify the target name (target_a or target_b) as an attribute to EquivalentAddressGroup, so something like this:

EquivalentAddressGroup addressGroup = new EquivalentAddressGroup(server.addr(),
    Attributes.newBuilder()
        .set(/* ... attribute key ... */, "target_a")
        .build());

However, I cannot find any documentation on how to properly do this last bit.

By looking at the source code of WeightedTargetLoadBalancer it looks like the addresses are filtered by an AddressFilter.filter() which, internally, gets the target name from the EquivalentAddressGroup like this:
source code

PathChain pathChain = address.getAttributes().get(PATH_CHAIN_KEY);

However, neither PATH_CHAIN_KEY, PathChain class are accessible as they are private or package-local.
So what's the correct way to attach a "target" to an EquivalentAddressGroup?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions