{"id":"ec27dc95c26aab67","repo":"apache/kafka","slug":"clientinstanceid-not-set","errorCode":null,"errorMessage":"clientInstanceId not set","messagePattern":"clientInstanceId not set","errorType":"exception","errorClass":"UnsupportedOperationException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/clients/producer/MockProducer.java","lineNumber":441,"sourceCode":"        this.injectTimeoutExceptionCounter = injectTimeoutExceptionCounter;\n    }\n\n    /**\n     * Sets the client instance ID for this mock producer.\n     *\n     * @param instanceId The client instance ID to set\n     */\n    public void setClientInstanceId(final Uuid instanceId) {\n        clientInstanceId = instanceId;\n    }\n\n    @Override\n    public Uuid clientInstanceId(Duration timeout) {\n        if (telemetryDisabled) {\n            throw new IllegalStateException();\n        }\n        if (clientInstanceId == null) {\n            throw new UnsupportedOperationException(\"clientInstanceId not set\");\n        }\n        if (injectTimeoutExceptionCounter != 0) {\n            // -1 is used as \"infinite\"\n            if (injectTimeoutExceptionCounter > 0) {\n                --injectTimeoutExceptionCounter;\n            }\n            throw new TimeoutException(\"TimeoutExceptions are successfully injected for test.\");\n        }\n\n        return clientInstanceId;\n    }\n\n    public Map<MetricName, Metric> metrics() {\n        return mockMetrics;\n    }\n\n    /**\n     * Set a mock metric for testing purpose","sourceCodeStart":423,"sourceCodeEnd":459,"githubUrl":"https://github.com/apache/kafka/blob/c31c9215e131f8c17e79f8901b48c13ee6aa8e7a/clients/src/main/java/org/apache/kafka/clients/producer/MockProducer.java#L423-L459","documentation":"Thrown by MockProducer.clientInstanceId(Duration) as UnsupportedOperationException when telemetry is enabled but no client instance id has been configured. The mock does not generate a telemetry clientInstanceId on its own (unlike the real producer which obtains one via the InitProducerId/telemetry handshake); the test must supply one via setClientInstanceId(Uuid). It surfaces an unconfigured-telemetry call deterministically rather than letting the test proceed with null.","triggerScenarios":"Calling producer.clientInstanceId(timeout) without first calling setClientInstanceId(Uuid) and while telemetry is not disabled (disableTelemetry() not called). The throw site is line 441, reached only if telemetryDisabled is false and clientInstanceId is null.","commonSituations":"A test for the telemetry/subscription code path that forgot to seed the client instance id; code under test newly calling clientInstanceId() after a version upgrade that introduced telemetry; a test that expected auto-initialization mirroring the real producer but used the mock, which has no broker handshake.","solutions":["Call producer.setClientInstanceId(Uuid.randomUuid()) (or a fixed Uuid) before exercising code that calls clientInstanceId().","If the test does not care about telemetry, call producer.disableTelemetry() to short-circuit the method (it throws a bare IllegalStateException instead, signalling telemetry is off).","If testing the not-set path itself, assert UnsupportedOperationException is thrown and do not seed the id.","Update the production wrapper under test to tolerate an unset client instance id (e.g. treat telemetry as optional) if that matches the intended contract."],"exampleFix":"// before\nMockProducer<String,String> p = new MockProducer<>();\np.clientInstanceId(Duration.ofSeconds(1)); // UnsupportedOperationException\n\n// after\nMockProducer<String,String> p = new MockProducer<>();\np.setClientInstanceId(Uuid.randomUuid());\np.clientInstanceId(Duration.ofSeconds(1));","handlingStrategy":"validation","validationCode":"// Set the client instance id before requesting it, or skip telemetry.\nproducer.setClientInstanceId(Uuid.randomUuid());\n// or, if you never want telemetry: producer.disableClientId(); // via the\n// telemetryDisabled flag, set through the constructor option.\n\n// Guard:\nUuid callClientInstanceId(MockProducer<?,?> p, Duration timeout) {\n    if (p.clientInstanceId(timeout) == null) { // only safe if you've stored it\n        throw new IllegalStateException(\"clientInstanceId not configured\");\n    }\n    return p.clientInstanceId(timeout);\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Call setClientInstanceId(...) once during producer setup, before any code path calls clientInstanceId().","Centralize producer construction in a factory that always configures the instance id.","If telemetry is optional, gate clientInstanceId() callers behind a 'telemetryEnabled' flag.","Document that clientInstanceId() is unsupported until configured, so callers don't rely on a default."],"tags":["mock-producer","telemetry","client-instance-id","test-harness"],"analyzedSha":"c31c9215e131f8c17e79f8901b48c13ee6aa8e7a","analyzedAt":"2026-08-03T12:34:05.770Z","schemaVersion":2}