kafka-go icon indicating copy to clipboard operation
kafka-go copied to clipboard

kafka.Conn.Broker() and kafka.Conn.Controller() return different Host value when there is one broker.

Open longyue0521 opened this issue 1 year ago • 1 comments

Describe the bug

A clear and concise description of what the bug is.

kafka.Conn.Broker() and kafka.Conn.Controller() return different Host value when there is one broker.

Kafka Version

  • What version(s) of Kafka are you testing against?
  • What version of kafka-go are you using?

bitnami/kafka 3.6.0 kafka-go v0.4.44

To Reproduce

Resources to reproduce the behavior:

copy from Apache Kafka development setup example section of https://hub.docker.com/r/bitnami/kafka

---
# docker-compose.yaml
#
# Adding a docker-compose file will help the maintainers setup the environment
# to reproduce the issue.
#
# If one the docker-compose files available in the repository may be used,
# mentioning it is also a useful alternative.
version: "3"
services:
  kafka:
    image: 'bitnami/kafka:3.6.0'
    ports:
      - '9092:9092'
    environment:
      - KAFKA_CFG_NODE_ID=0
      - KAFKA_CFG_PROCESS_ROLES=controller,broker
      - KAFKA_CFG_LISTENERS=PLAINTEXT://:9092,CONTROLLER://:9093
      - KAFKA_CFG_LISTENER_SECURITY_PROTOCOL_MAP=CONTROLLER:PLAINTEXT,PLAINTEXT:PLAINTEXT
      - KAFKA_CFG_CONTROLLER_QUORUM_VOTERS=0@kafka:9093
      - KAFKA_CFG_CONTROLLER_LISTENER_NAMES=CONTROLLER
package main

import (
    "fmt"
    "github.com/segmentio/kafka-go"
)

func main() {
         conn, _ := kafkago.Dial("tcp", "localhost:9092")

	broker := conn.Broker()
	fmt.Printf("%#v\n", broker)

	controllerBroker, _ := conn.Controller()
	fmt.Printf("%#v\n", controllerBroker)

        brokers, _ := conn.Brokers()
	log.Printf("%#v\n", brokers)
}

Expected Behavior

A clear and concise description of what you expected to happen.

I think they should be equal/same when there is only one broker

Observed Behavior

A clear and concise description of the behavior you observed.

kafka.Broker{Host:"::1", Port:9092, ID:0, Rack:""}
kafka.Broker{Host:"4a22fb166591", Port:9092, ID:0, Rack:""}
[]kafka.Broker{kafka.Broker{Host:"4a22fb166591", Port:9092, ID:0, Rack:""}}

Additional Context

Add any other context about the problem here.

image

image

longyue0521 avatar Oct 28 '23 15:10 longyue0521

When you test with current version of docker-compose.yaml bug is even more noticeable.

With current docker-compose broker id is set to 1, and when I run your example code I am getting result similar to this:

kafka.Broker{Host:"127.0.0.1", Port:9092, ID:0, Rack:""}
kafka.Broker{Host:"localhost", Port:9092, ID:1, Rack:""}
[]kafka.Broker{kafka.Broker{Host:"localhost", Port:9092, ID:1, Rack:""}}

Call to Broker() is returning default connection parameters. In your case Broker ID was set to 0 and because of that ID looked as expected, but in reality if ID of connected broker is not 0 you will still get ID = 0 for connected broker.

ivanvs avatar Jan 30 '24 00:01 ivanvs