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

[Issue 763][producer] Fix deadlock in Producer Send when message fails to encode. #762

Merged
merged 2 commits into from
Apr 29, 2022
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
4 changes: 4 additions & 0 deletions pulsar/error.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,8 @@ const (
SeekFailed
// ProducerClosed means producer already been closed
ProducerClosed
// SchemaFailure means the payload could not be encoded using the Schema
SchemaFailure
)

// Error implement error interface, composed of two parts: msg and result.
Expand Down Expand Up @@ -205,6 +207,8 @@ func getResultStr(r Result) string {
return "SeekFailed"
case ProducerClosed:
return "ProducerClosed"
case SchemaFailure:
return "SchemaFailure"
default:
return fmt.Sprintf("Result(%d)", r)
}
Expand Down
2 changes: 2 additions & 0 deletions pulsar/producer_partition.go
Original file line number Diff line number Diff line change
Expand Up @@ -419,6 +419,8 @@ func (p *partitionProducer) internalSend(request *sendRequest) {
var schemaPayload []byte
schemaPayload, err = p.options.Schema.Encode(msg.Value)
if err != nil {
p.publishSemaphore.Release()
request.callback(nil, request.msg, newError(SchemaFailure, err.Error()))
p.log.WithError(err).Errorf("Schema encode message failed %s", msg.Value)
return
}
Expand Down
44 changes: 44 additions & 0 deletions pulsar/producer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -924,6 +924,50 @@ func TestMaxMessageSize(t *testing.T) {
}
}

func TestFailedSchemaEncode(t *testing.T) {
client, err := NewClient(ClientOptions{
URL: lookupURL,
})

assert.Nil(t, err)
defer client.Close()

topic := newTopicName()
ctx := context.Background()

// create producer
producer, err := client.CreateProducer(ProducerOptions{
Topic: topic,
Schema: NewAvroSchema("{\"type\":\"string\"}", nil),
})

assert.Nil(t, err)
defer producer.Close()

var wg sync.WaitGroup
wg.Add(1)
go func() {
// producer should send return an error as message is Int64, but schema is String
mid, err := producer.Send(ctx, &ProducerMessage{
Value: int64(1),
})
assert.NotNil(t, err)
assert.Nil(t, mid)
wg.Done()
}()

wg.Add(1)
// producer should send return an error as message is Int64, but schema is String
producer.SendAsync(ctx, &ProducerMessage{
Value: int64(1),
}, func(messageID MessageID, producerMessage *ProducerMessage, err error) {
assert.NotNil(t, err)
assert.Nil(t, messageID)
wg.Done()
})
wg.Wait()
}

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