Skip to content

feat(client): expose ClusterID via Client interface - #3694

Open
shashank-reddy-nr wants to merge 1 commit into
IBM:mainfrom
shashank-reddy-nr:feature/client-cluster-id
Open

feat(client): expose ClusterID via Client interface#3694
shashank-reddy-nr wants to merge 1 commit into
IBM:mainfrom
shashank-reddy-nr:feature/client-cluster-id

Conversation

@shashank-reddy-nr

Copy link
Copy Markdown

Summary

Adds ClusterID() string to the Client interface, surfacing the cluster ID that brokers already return in MetadataResponse (protocol version ≥ 2, Kafka ≥ 0.10.1.0) but that was never stored or accessible to callers.

Changes

  • client.go — adds clusterID string field to the client struct; stores the value in updateMetadata() under the existing client.lock RWMutex (same pattern as controllerID); adds ClusterID() string to the Client interface; implements (*client).ClusterID() with RLock.
  • mockresponses.go — adds clusterID field and SetClusterID() builder to MockMetadataResponse so tests can configure the mock; populates MetadataResponse.ClusterID in For().
  • client_test.go — adds TestClientClusterID with two subtests: one at V0_10_1_0 that asserts the value round-trips correctly, and one at V0_10_0_0 that asserts an empty string is returned when the broker does not include a cluster ID.
  • async_producer_test.go — adds the ClusterID() string stub to stubLeaderClient to satisfy the updated interface.

Design notes

  • Returns string (not (string, error)): the cluster ID is either present or not; returning empty string for pre-0.10.1 brokers is consistent with how controllerID behaves before any metadata has been received.
  • nopCloserClient requires no changes — it embeds the Client interface by value and inherits the new method automatically.
  • Nil-pointer guard: MetadataResponse.ClusterID is *string; the store path checks != nil && != "" before overwriting, so a nil pointer from an old-version response never clears a previously cached value.

Add ClusterID() string to the Client interface, backed by the ClusterID
field already present in MetadataResponse (protocol version >= 2,
Kafka >= 0.10.1). The value is stored on each successful metadata
refresh under the existing client.lock RWMutex, consistent with how
controllerID is handled. Returns an empty string for brokers that
predate Kafka 0.10.1 or before the first metadata response is received.

Also add SetClusterID to MockMetadataResponse so tests can configure
the mock to return a cluster ID, and add stubLeaderClient.ClusterID()
to satisfy the updated interface in async_producer_test.go.

Assisted-by: Claude Sonnet 4.6
Comment thread client.go
Comment on lines +38 to +41
// ClusterID returns the cluster ID reported by the broker in the most recent
// metadata response. It returns an empty string if the broker has not yet
// responded or if the Kafka version predates 0.10.1 (MetadataResponse v2).
ClusterID() string

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is expanding an exported interface, and thus a breaking change.

Comment thread client.go
Comment on lines +1116 to +1118
if data.ClusterID != nil && *data.ClusterID != "" {
client.clusterID = *data.ClusterID
}

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test against *data.ClusterID != "" is unnecessary. We can simply assign a zero value as a no-op.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants