Skip to content

Add metadata property to configure Batching in Pulsar #1707

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

Merged
merged 13 commits into from
May 7, 2022
Merged
21 changes: 13 additions & 8 deletions pubsub/pulsar/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,18 @@ limitations under the License.

package pulsar

import "time"

type pulsarMetadata struct {
Host string `json:"host"`
ConsumerID string `json:"consumerID"`
EnableTLS bool `json:"enableTLS"`
DisableBatching bool `json:"disableBatching"`
Tenant string `json:"tenant"`
Namespace string `json:"namespace"`
Persistent bool `json:"persistent"`
Token string `json:"token"`
Host string `json:"host"`
ConsumerID string `json:"consumerID"`
EnableTLS bool `json:"enableTLS"`
DisableBatching bool `json:"disableBatching"`
BatchingMaxPublishDelay time.Duration `json:"batchingMaxPublishDelay"`
BatchingMaxSize uint `json:"batchingMaxSize"`
BatchingMaxMessages uint `json:"batchingMaxMessages"`
Tenant string `json:"tenant"`
Namespace string `json:"namespace"`
Persistent bool `json:"persistent"`
Token string `json:"token"`
}
71 changes: 59 additions & 12 deletions pubsub/pulsar/pulsar.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@ import (
)

const (
host = "host"
consumerID = "consumerID"
enableTLS = "enableTLS"
deliverAt = "deliverAt"
deliverAfter = "deliverAfter"
disableBatching = "disableBatching"
tenant = "tenant"
namespace = "namespace"
persistent = "persistent"
host = "host"
consumerID = "consumerID"
enableTLS = "enableTLS"
deliverAt = "deliverAt"
deliverAfter = "deliverAfter"
disableBatching = "disableBatching"
batchingMaxPublishDelay = "batchingMaxPublishDelay"
batchingMaxSize = "batchingMaxSize"
batchingMaxMessages = "batchingMaxMessages"
tenant = "tenant"
namespace = "namespace"
persistent = "persistent"

defaultTenant = "public"
defaultNamespace = "default"
Expand All @@ -50,6 +53,13 @@ const (
topicFormat = "%s://%s/%s/%s"
persistentStr = "persistent"
nonPersistentStr = "non-persistent"

// defaultBatchingMaxPublishDelay init default for maximum delay to batch messages.
defaultBatchingMaxPublishDelay = 10 * time.Millisecond
// defaultMaxMessages init default num of entries in per batch.
defaultMaxMessages = 1000
// defaultMaxBatchSize init default for maximum number of bytes per batch.
defaultMaxBatchSize = 128 * 1024
)

type Pulsar struct {
Expand Down Expand Up @@ -92,7 +102,30 @@ func parsePulsarMetadata(meta pubsub.Metadata) (*pulsarMetadata, error) {
}
m.DisableBatching = disableBatching
}

m.BatchingMaxPublishDelay = defaultBatchingMaxPublishDelay
if val, ok := meta.Properties[batchingMaxPublishDelay]; ok {
batchingMaxPublishDelay, err := formatDuration(val)
if err != nil {
return nil, errors.New("pulsar error: invalid value for batchingMaxPublishDelay")
}
m.BatchingMaxPublishDelay = batchingMaxPublishDelay
}
m.BatchingMaxMessages = defaultMaxMessages
if val, ok := meta.Properties[batchingMaxMessages]; ok {
batchingMaxMessages, err := strconv.ParseUint(val, 10, 64)
if err != nil {
return nil, errors.New("pulsar error: invalid value for batchingMaxMessages")
}
m.BatchingMaxMessages = uint(batchingMaxMessages)
}
m.BatchingMaxSize = defaultMaxBatchSize
if val, ok := meta.Properties[batchingMaxSize]; ok {
batchingMaxSize, err := strconv.ParseUint(val, 10, 64)
if err != nil {
return nil, errors.New("pulsar error: invalid value for batchingMaxSize")
}
m.BatchingMaxSize = uint(batchingMaxSize)
}
if val, ok := meta.Properties[persistent]; ok && val != "" {
per, err := strconv.ParseBool(val)
if err != nil {
Expand Down Expand Up @@ -179,8 +212,11 @@ func (p *Pulsar) Publish(req *pubsub.PublishRequest) error {
if cache == nil {
p.logger.Debugf("creating producer for topic %s, full topic name in pulsar is %s", req.Topic, topic)
producer, err = p.client.CreateProducer(pulsar.ProducerOptions{
Topic: topic,
DisableBatching: p.metadata.DisableBatching,
Topic: topic,
DisableBatching: p.metadata.DisableBatching,
BatchingMaxPublishDelay: p.metadata.BatchingMaxPublishDelay,
BatchingMaxMessages: p.metadata.BatchingMaxMessages,
BatchingMaxSize: p.metadata.BatchingMaxSize,
})
if err != nil {
return err
Expand Down Expand Up @@ -317,3 +353,14 @@ func (p *Pulsar) formatTopic(topic string) string {
}
return fmt.Sprintf(topicFormat, persist, p.metadata.Tenant, p.metadata.Namespace, topic)
}

func formatDuration(durationString string) (time.Duration, error) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shubham1172 Is there a better way?

Copy link
Member

@shubham1172 shubham1172 May 6, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@saber-wang we can make it simpler by always expecting a number.

The metadata can be batchingMaxPublishDelayMs to clearly indicate that it should be in milliseconds.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@berndverst this comment was still pending.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shubham1172 Need to change to such a design?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That would have been cleaner and simpler but Bernd has already approved/merged this PR. If we have enough consensus, we can create another PR on top to change that I guess.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@shubham1172 I don't have a better idea. I can create a new rp to modify it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @saber-wang. Let's see what Bernd/@daixiang0 have to say.

if val, err := strconv.Atoi(durationString); err == nil {
return time.Duration(val) * time.Millisecond, nil
}

// Convert it by parsing
d, err := time.ParseDuration(durationString)

return d, err
}
12 changes: 9 additions & 3 deletions pubsub/pulsar/pulsar_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,12 @@ import (
func TestParsePulsarMetadata(t *testing.T) {
m := pubsub.Metadata{}
m.Properties = map[string]string{
"host": "a",
"enableTLS": "false",
"disableBatching": "true",
"host": "a",
"enableTLS": "false",
"disableBatching": "true",
"batchingMaxPublishDelay": "5s",
"batchingMaxSize": "100",
"batchingMaxMessages": "200",
}
meta, err := parsePulsarMetadata(m)

Expand All @@ -37,6 +40,9 @@ func TestParsePulsarMetadata(t *testing.T) {
assert.Equal(t, true, meta.DisableBatching)
assert.Equal(t, defaultTenant, meta.Tenant)
assert.Equal(t, defaultNamespace, meta.Namespace)
assert.Equal(t, 5*time.Second, meta.BatchingMaxPublishDelay)
assert.Equal(t, uint(100), meta.BatchingMaxSize)
assert.Equal(t, uint(200), meta.BatchingMaxMessages)
}

func TestParsePublishMetadata(t *testing.T) {
Expand Down