0

I'm trying to create topics with Kafka-node. First I check if they exist, and if they not exist, I create them. The problem is that when checking if they exist, I get this as result:

{
  error: [ 'LeaderNotAvailable', 'LeaderNotAvailable' ],
  metadata: {},
  clusterMetadata: { controllerId: 1 }
}

Which means that the two topics (test2,test3) that I'm checking don't exist. So I create them, but I get this:

TopicsCrated: [
  { topic: 'test2', error: "Topic 'test2' already exists." },
  { topic: 'test3', error: "Topic 'test3' already exists." }
]

And then I list the topics, but I get an empty Object (no topics).

Then I run the command sudo docker exec broker kafka-topics --bootstrap-server broker:29092 --describe or sudo docker exec broker kafka-topics --bootstrap-server broker:29092 --list and both say that the topics exist. Output:

Topic: test2    TopicId: PIW31TLfSsCyh4ovd7sD4w PartitionCount: 1   ReplicationFactor: 1    Configs: 
    Topic: test2    Partition: 0    Leader: 1   Replicas: 1 Isr: 1
Topic: test3    TopicId: iIS2lIUATSyr-EKrhtzShg PartitionCount: 1   ReplicationFactor: 1    Configs: 
    Topic: test3    Partition: 0    Leader: 1   Replicas: 1 Isr: 1

This is how I check if topics exist (returns tue or false). Data[1] is the 'LeaderNotAvailable' output:

return new Promise((resolve, reject) => {
    this.client?.loadMetadataForTopics(topicsName, (err, data) => {
        console.log(data[1])

        if (data[1].error?.filter(e => e === 'LeaderNotAvailable')?.length > 0) {
            console.info(`===> Topics not exist: ${topicsName.flat()}`)
            reject(false)
        } else {
            console.info(`===> Topics exist: ${topicsName.flat()}`)
            resolve(true)
        }
        if (err) {
            console.warn({
                err: err
            })
            reject(false)
        }
    })
})

This is how I create them. Here is where I get that they already exist:

return new Promise(async (resolve,reject) => {
        const e = await this.TopicsExist(topics.map(t => t.topic)).catch(r => r)
        console.log(e)
        if (!e) {
            this.admin = this.admin || new Kafka.Admin(this.client)
            this.admin.createTopics(topics, (err,data) => {
                console.info('===> Creating topics...')
                if (data) {
                    console.log({topicsCreated: data})
                    resolve(true)
                }
                if (err) {
                    console.log(err.error)
                    reject(false)
                }
            })
        } else {
            console.log('===> Topics already exist')
            reject(false)
        }
    })

I do not understand why first they don't exist, then it says they exist, then if I list them they not exist

ask4you
  • 768
  • 3
  • 14
  • "Leader not available" means exactly that, not that the topics don't exist. Please show the output of `kafka-topics --describe` or `--list` to prove if they really exist or not – OneCricketeer Apr 17 '22 at 13:49
  • It shows that test2 and test3 exist. So, why aren't they being listed? – ask4you Apr 17 '22 at 14:01
  • Can you please [edit] your question to include the output from the commands you've ran? – OneCricketeer Apr 17 '22 at 15:31
  • Yes, see update. – ask4you Apr 17 '22 at 16:51
  • Is your Javascript code running on the host, or in Docker? What are you using as the bootstrap servers? Please see https://stackoverflow.com/questions/51630260/connect-to-kafka-running-in-docker#51634499 – OneCricketeer Apr 17 '22 at 20:20
  • I'm running both js and kafka in docker-compose. Im using `broker:29092` which is one I set in environment variables of my kafka service: `KAFKA_ADVERTISED_LISTENERS: PLAINTEXT://localhost:9092,PLAINTEXT_INTERNAL://broker:29092` – ask4you Apr 17 '22 at 20:42

1 Answers1

0

What is the value of auto.create.topics.enable property on Kafka cluster side

If auto.create.topics.enable is set to true then Kafka will create a new topic when you try to list/check if it exists or not.

bguiz
  • 27,371
  • 47
  • 154
  • 243
  • How can I know that, if I'm using Kafka with a docker-compose and the `confluentic/kafka` image? – ask4you Apr 20 '22 at 10:25
  • I am assuming that it's using this file https://github.com/confluentinc/docker-images/blob/master/kafka/server.properties – Manish Bhatt Apr 20 '22 at 16:19