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

When topic is terminated. Client must not retry connecting to broker. #1128

Merged
merged 1 commit into from
Nov 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions pulsar/producer_partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,27 @@ func (p *partitionProducer) reconnectToBroker() {
break
}

if strings.Contains(errMsg, "TopicTerminatedError") {
p.log.Info("Topic was terminated, failing pending messages, will not reconnect")
pendingItems := p.pendingQueue.ReadableSlice()
for _, item := range pendingItems {
pi := item.(*pendingItem)
if pi != nil {
pi.Lock()
requests := pi.sendRequests
for _, req := range requests {
sr := req.(*sendRequest)
if sr != nil {
sr.done(nil, newError(TopicTerminated, err.Error()))
}
}
pi.Unlock()
}
}
p.setProducerState(producerClosing)
break
}

if maxRetry > 0 {
maxRetry--
}
Expand Down
56 changes: 56 additions & 0 deletions pulsar/producer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1158,6 +1158,62 @@ func TestFailedSchemaEncode(t *testing.T) {
wg.Wait()
}

func TestTopicTermination(t *testing.T) {
client, err := NewClient(ClientOptions{
URL: serviceURL,
})
assert.NoError(t, err)
defer client.Close()

topicName := newTopicName()
consumer, err := client.Subscribe(ConsumerOptions{
Topic: topicName,
SubscriptionName: "send_timeout_sub",
})
assert.Nil(t, err)
defer consumer.Close() // subscribe but do nothing

producer, err := client.CreateProducer(ProducerOptions{
Topic: topicName,
SendTimeout: 2 * time.Second,
})
assert.Nil(t, err)
defer producer.Close()

afterCh := time.After(5 * time.Second)
terminatedChan := make(chan bool)
go func() {
for {
_, err := producer.Send(context.Background(), &ProducerMessage{
Payload: make([]byte, 1024),
})
if err != nil {
e := err.(*Error)
if e.result == TopicTerminated {
terminatedChan <- true
} else {
terminatedChan <- false
}
}
time.Sleep(1 * time.Millisecond)
}
}()

terminateURL := adminURL + "/admin/v2/persistent/public/default/" + topicName + "/terminate"
log.Info(terminateURL)
makeHTTPCall(t, http.MethodPost, terminateURL, "")

for {
select {
case d := <-terminatedChan:
assert.Equal(t, d, true)
return
case <-afterCh:
assert.Fail(t, "Time is up. Topic should have been terminated by now")
}
}
}

func TestSendTimeout(t *testing.T) {
quotaURL := adminURL + "/admin/v2/namespaces/public/default/backlogQuota"
quotaFmt := `{"limit": "%d", "policy": "producer_request_hold"}`
Expand Down