apache/kafka · error · UnsupportedOperationException

clientInstanceId not set

Error message

clientInstanceId not set

What it means

MockShareConsumer is the share-group test double. Unlike a real consumer it never auto-generates a clientInstanceId; the value is null until the test injects one via setClientInstanceId(Uuid). clientInstanceId(Duration) is annotated @Override but its body simply throws UnsupportedOperationException when the field is still null, rather than blocking for the timeout.

Source

Thrown at clients/src/main/java/org/apache/kafka/clients/consumer/MockShareConsumer.java:140

        return new HashMap<>();
    }

    @Override
    public synchronized void commitAsync() {
    }

    @Override
    public void setAcknowledgementCommitCallback(AcknowledgementCommitCallback callback) {
    }

    public synchronized void setClientInstanceId(final Uuid clientInstanceId) {
        this.clientInstanceId = clientInstanceId;
    }

    @Override
    public synchronized Uuid clientInstanceId(Duration timeout) {
        if (clientInstanceId == null) {
            throw new UnsupportedOperationException("clientInstanceId not set");
        }

        return clientInstanceId;
    }

    @Override
    public synchronized Map<MetricName, ? extends Metric> metrics() {
        ensureNotClosed();
        return Map.of();
    }

    @Override
    public Optional<Integer> acquisitionLockTimeoutMs() {
        return Optional.empty();
    }

    @Override
    public void registerMetricForSubscription(KafkaMetric metric) {

View on GitHub (pinned to 996fb4585a)

Solutions

  1. Call mockShareConsumer.setClientInstanceId(Uuid.randomUuid()) (or a deterministic Uuid) before any code path that reads clientInstanceId.
  2. If you do not need clientInstanceId in the test, avoid calling it and any helper that reads it.
  3. If the assertion must run before the id is known, restructure the test so setClientInstanceId runs in setup before the action under test.

Example fix

// before
MockShareConsumer<K,V> c = new MockShareConsumer<>();
Uuid id = c.clientInstanceId(Duration.ofSeconds(5)); // -> UnsupportedOperationException

// after
MockShareConsumer<K,V> c = new MockShareConsumer<>();
c.setClientInstanceId(Uuid.METRIC_GROUP_ID_OFFSET_FETCHER); // or Uuid.randomUuid()
Uuid id = c.clientInstanceId(Duration.ofSeconds(5));
Defensive patterns

Strategy: validation

Validate before calling

// Seed the id before any code path that reads it.
if (mockShareConsumer.clientInstanceIdField == null /* or expose a hasClientInstanceId() */) {
    mockShareConsumer.setClientInstanceId(Uuid.randomUuid());
}

Type guard

null

Try / catch

// Treat UnsupportedOperationException as 'not yet set' and seed + retry once.
try {
    return mock.clientInstanceId(Duration.ofSeconds(5));
} catch (UnsupportedOperationException e) {
    mock.setClientInstanceId(Uuid.randomUuid());
    return mock.clientInstanceId(Duration.ofSeconds(5));
}

Prevention

When it happens

Trigger: Calling mockShareConsumer.clientInstanceId(Duration) before any call to mockShareConsumer.setClientInstanceId(Uuid).

Common situations: Test code copied from a KafkaConsumer test that calls clientInstanceId for metrics/group assertions; running a test that expects a stable group member id without seeding it; or invoking metrics()-dependent paths that surface clientInstanceId.

Related errors


AI-assisted analysis of apache/kafka@996fb4585a (2026-08-11). Data as JSON: /api/errors/f86d8296e3b47822. Report an issue: GitHub.