grpc/grpc-java · error · IllegalArgumentException

AndMatcher must have at least 2 predicates

Error message

AndMatcher must have at least 2 predicates

What it means

An AndMatcherEvaluator represents a PredicateList under and_matcher; an AND over fewer than 2 predicates is a no-op, so the constructor throws IllegalArgumentException when the proto contains fewer than 2 predicates.

Source

Thrown at xds/src/main/java/io/grpc/xds/internal/matcher/PredicateEvaluator.java:143

    }
    
    @Override 
    boolean evaluate(MatchContext context) {
      for (PredicateEvaluator e : evaluators) {
        if (e.evaluate(context)) {
          return true;
        }
      }
      return false;
    }
  }

  private static final class AndMatcherEvaluator extends PredicateEvaluator {
    private final List<PredicateEvaluator> evaluators;
    
    AndMatcherEvaluator(Predicate.PredicateList proto) {
      if (proto.getPredicateCount() < 2) {
        throw new IllegalArgumentException("AndMatcher must have at least 2 predicates");
      }
      this.evaluators = new ArrayList<>(proto.getPredicateCount());
      for (Predicate p : proto.getPredicateList()) {
        evaluators.add(PredicateEvaluator.fromProto(p));
      }
    }
    
    @Override 
    boolean evaluate(MatchContext context) {
      for (PredicateEvaluator e : evaluators) {
        if (!e.evaluate(context)) {
          return false;
        }
      }
      return true;
    }
  }

View on GitHub (pinned to 64daddc1f3)

Solutions

  1. Ensure and_matcher contains at least 2 predicates in the config
  2. Replace a single-predicate and_matcher with the predicate directly
  3. Pre-validate that predicate lists under and_matcher have count >= 2 before parsing

Example fix

// before
and_matcher:
  predicate:
  - single_predicate: { ... }
// after
and_matcher:
  predicate:
  - single_predicate: { ... }
  - single_predicate: { ... }
Defensive patterns

Strategy: validation

Validate before calling

if (proto.getPredicateCount() < 2) {
  throw new IllegalArgumentException("and_matcher requires >= 2 predicates");
}

Try / catch

try { new PredicateEvaluator.AndMatcherEvaluator(proto); } catch (IllegalArgumentException e) { log.warn("and_matcher with <2 predicates: " + e.getMessage()); }

Prevention

When it happens

Trigger: Building AndMatcherEvaluator from a Predicate.PredicateList proto with getPredicateCount() < 2 — e.g. and_matcher set to an empty list after predicates were removed, or a lone predicate wrapped in and_matcher unnecessarily.

Common situations: Config templating that emits empty and_matcher placeholders; management servers pruning predicates without collapsing the and_matcher; hand-written matcher trees with vestigial and_matcher wrappers.

Understand the failure class

Background: "value must be between 0 and 1" / "out of range" / "must not be negative" errors: fixing range-validation failures across open-source libraries — this error's family across 42 libraries.

Related errors


AI-assisted analysis of grpc/grpc-java@64daddc1f3 (2026-09-08). Data as JSON: /api/errors/b8229630f27f9d79. Report an issue: GitHub.