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