Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ClusterAdmin DescribeCluster api return controllerID invalid #1511

Closed
antsbean opened this issue Oct 12, 2019 · 2 comments
Closed

ClusterAdmin DescribeCluster api return controllerID invalid #1511

antsbean opened this issue Oct 12, 2019 · 2 comments

Comments

@antsbean
Copy link
Contributor

antsbean commented Oct 12, 2019

Versions

Please specify real version numbers or git SHAs, not just "Latest" since that changes fairly regularly.

Sarama Kafka Go
v1.24 2.0.0 or 2.2.1 1.13
Configuration

What configuration values are you using for Sarama and Kafka?

	cfg := sarama.NewConfig()
	var err error
	cfg.Version, err = sarama.ParseKafkaVersion("2.0.0")
Logs

none

Problem Description
type KafkaUtil struct {
	ca    sarama.ClusterAdmin
	addrs []string
	cfg   *sarama.Config
}

func NewKafkaUtil(addrs []string, cfg *sarama.Config) (*KafkaUtil, error) {
	if !cfg.Version.IsAtLeast(sarama.V2_0_0_0) {
		log.Warn("at least kafka version 2.0")
		return nil, sarama.ErrUnsupportedVersion
	}
	ca, err := sarama.NewClusterAdmin(addrs, cfg)
	if err != nil {
		return nil, err
	}
	kafkaUtil := &KafkaUtil{ca: ca, addrs: addrs, cfg: cfg}

	return kafkaUtil, nil
}



// GetDefaultReplicationFactor get kafka cluster default replicate factor
func (k *KafkaUtil) GetDefaultReplicationFactor() (int16, error) {
	_, controllerID, err := k.ca.DescribeCluster()
	if err != nil {
		return 0, err
	}
	var defaultReplicateFactor int
	configs, err := k.ca.DescribeConfig(sarama.ConfigResource{Type: sarama.ClusterResource,
		Name:        fmt.Sprintf("%d", controllerID),
		ConfigNames: []string{"default.replication.factor"}})
	if err != nil {
		return 0, err
	}
	if defaultReplicateFactor, err = strconv.Atoi(configs[0].Value); err != nil {
		return 0, err
	}
	return int16(defaultReplicateFactor), nil
}

k.ca.DescribeConfig return controllerID always is -1, this will lead to get kafka configs failed by DescribeConfig, but k.getController() get controllerID is ok

func (k *KafkaUtil) getController() (*sarama.Broker, error) {
	client, err := sarama.NewClient(k.addrs, k.cfg)
	if err != nil {
		return nil, err
	}
	//make sure we can retrieve the controller
	controller, err := client.Controller()
	if err != nil {
                client.Close()
		return nil, err
	}
	return controller, nil
}
// GetDefaultReplicationFactor get kafka cluster default replicate factor
func (k *KafkaUtil) GetDefaultReplicationFactor() (int16, error) {
	b, err := k.getController()
	if err != nil {
		return 0, err
	}
	defer b.Close()
	controllerID := b.ID()
	var defaultReplicateFactor int
	configs, err := k.ca.DescribeConfig(sarama.ConfigResource{Type: sarama.ClusterResource,
		Name:        fmt.Sprintf("%d", controllerID),
		ConfigNames: []string{"default.replication.factor"}})
	if err != nil {
		return 0, err
	}
	if defaultReplicateFactor, err = strconv.Atoi(configs[0].Value); err != nil {
		return 0, err
	}
	return int16(defaultReplicateFactor), nil
}
@antsbean antsbean changed the title ClusterAmind DescribeCluster api return controllerID invalid ClusterAdmin DescribeCluster api return controllerID invalid Oct 12, 2019
@weeco
Copy link
Contributor

weeco commented Oct 20, 2019

I observed the same. Addtionally the RackID seems to be always empty, even though they are actually set for my Brokers.

@antsbean
Copy link
Contributor Author

I observed the same. Addtionally the RackID seems to be always empty, even though they are actually set for my Brokers.

I had fixed this problem, please see pr

bai added a commit that referenced this issue Oct 24, 2019
fixed ClusterAdmin DescribeCluster api return controllerID invalid #1511
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

No branches or pull requests

2 participants