|
@@ -58,7 +58,7 @@ func NewClient(id string, addrs []string, config *ClientConfig) (client *Client,
|
|
|
client.leaders = make(map[string]map[int32]int32)
|
|
|
|
|
|
|
|
|
- err = client.refreshTopics(make([]string, 0), client.config.MetadataRetries)
|
|
|
+ err = client.RefreshAllMetadata()
|
|
|
if err != nil {
|
|
|
client.Close()
|
|
|
return nil, err
|
|
@@ -87,43 +87,70 @@ func (client *Client) Close() error {
|
|
|
return nil
|
|
|
}
|
|
|
|
|
|
-
|
|
|
-
|
|
|
-
|
|
|
-func (client *Client) leader(topic string, partition_id int32) (*Broker, error) {
|
|
|
- leader := client.cachedLeader(topic, partition_id)
|
|
|
+
|
|
|
+func (client *Client) Partitions(topic string) ([]int32, error) {
|
|
|
+ partitions := client.cachedPartitions(topic)
|
|
|
|
|
|
- if leader == nil {
|
|
|
- err := client.refreshTopic(topic)
|
|
|
+ if partitions == nil {
|
|
|
+ err := client.RefreshTopicMetadata(topic)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
- leader = client.cachedLeader(topic, partition_id)
|
|
|
+ partitions = client.cachedPartitions(topic)
|
|
|
}
|
|
|
|
|
|
- if leader == nil {
|
|
|
- return nil, UNKNOWN_TOPIC_OR_PARTITION
|
|
|
+ if partitions == nil {
|
|
|
+ return nil, NoSuchTopic
|
|
|
}
|
|
|
|
|
|
- return leader, nil
|
|
|
+ return partitions, nil
|
|
|
}
|
|
|
|
|
|
-func (client *Client) partitions(topic string) ([]int32, error) {
|
|
|
- partitions := client.cachedPartitions(topic)
|
|
|
+
|
|
|
+func (client *Client) Topics() ([]string, error) {
|
|
|
+ client.lock.RLock()
|
|
|
+ defer client.lock.RUnlock()
|
|
|
|
|
|
- if partitions == nil {
|
|
|
- err := client.refreshTopic(topic)
|
|
|
+ ret := make([]string, 0, len(client.leaders))
|
|
|
+ for topic, _ := range client.leaders {
|
|
|
+ ret = append(ret, topic)
|
|
|
+ }
|
|
|
+
|
|
|
+ return ret, nil
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func (client *Client) RefreshTopicMetadata(topics ...string) error {
|
|
|
+ return client.refreshMetadata(topics, client.config.MetadataRetries)
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+func (client *Client) RefreshAllMetadata() error {
|
|
|
+
|
|
|
+ return client.refreshMetadata(make([]string, 0), client.config.MetadataRetries)
|
|
|
+}
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+
|
|
|
+func (client *Client) leader(topic string, partition_id int32) (*Broker, error) {
|
|
|
+ leader := client.cachedLeader(topic, partition_id)
|
|
|
+
|
|
|
+ if leader == nil {
|
|
|
+ err := client.RefreshTopicMetadata(topic)
|
|
|
if err != nil {
|
|
|
return nil, err
|
|
|
}
|
|
|
- partitions = client.cachedPartitions(topic)
|
|
|
+ leader = client.cachedLeader(topic, partition_id)
|
|
|
}
|
|
|
|
|
|
- if partitions == nil {
|
|
|
- return nil, NoSuchTopic
|
|
|
+ if leader == nil {
|
|
|
+ return nil, UNKNOWN_TOPIC_OR_PARTITION
|
|
|
}
|
|
|
|
|
|
- return partitions, nil
|
|
|
+ return leader, nil
|
|
|
}
|
|
|
|
|
|
func (client *Client) disconnectBroker(broker *Broker) {
|
|
@@ -147,16 +174,9 @@ func (client *Client) disconnectBroker(broker *Broker) {
|
|
|
go broker.Close()
|
|
|
}
|
|
|
|
|
|
-func (client *Client) refreshTopic(topic string) error {
|
|
|
- tmp := make([]string, 1)
|
|
|
- tmp[0] = topic
|
|
|
-
|
|
|
- return client.refreshTopics(tmp, client.config.MetadataRetries)
|
|
|
-}
|
|
|
-
|
|
|
|
|
|
|
|
|
-func (client *Client) refreshTopics(topics []string, retries int) error {
|
|
|
+func (client *Client) refreshMetadata(topics []string, retries int) error {
|
|
|
for broker := client.any(); broker != nil; broker = client.any() {
|
|
|
response, err := broker.GetMetadata(client.id, &MetadataRequest{Topics: topics})
|
|
|
|
|
@@ -174,7 +194,7 @@ func (client *Client) refreshTopics(topics []string, retries int) error {
|
|
|
return LEADER_NOT_AVAILABLE
|
|
|
}
|
|
|
time.Sleep(client.config.WaitForElection)
|
|
|
- return client.refreshTopics(retry, retries-1)
|
|
|
+ return client.refreshMetadata(retry, retries-1)
|
|
|
}
|
|
|
case EncodingError:
|
|
|
|