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 172] Add key based batcher #400

Merged
merged 6 commits into from
Dec 1, 2020
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
2 changes: 2 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ require (
github.com/klauspost/compress v1.10.8
github.com/kr/pretty v0.2.0 // indirect
github.com/linkedin/goavro/v2 v2.9.8
github.com/modern-go/concurrent v0.0.0-20180306012644-bacd9c7ef1dd // indirect
github.com/modern-go/reflect2 v1.0.1 // indirect
github.com/pierrec/lz4 v2.0.5+incompatible
github.com/pkg/errors v0.9.1
github.com/prometheus/client_golang v1.7.1
Expand Down
44 changes: 44 additions & 0 deletions pulsar/batcher_builder.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.

package pulsar

import (
"errors"

"github.com/apache/pulsar-client-go/pulsar/internal"
)

type BatcherBuilderType int

const (
DefaultBatchBuilder BatcherBuilderType = iota
KeyBasedBatchBuilder
)

func GetBatcherBuilderProvider(typ BatcherBuilderType) (
internal.BatcherBuilderProvider, error,
) {
switch typ {
case DefaultBatchBuilder:
return internal.NewBatchBuilder, nil
case KeyBasedBatchBuilder:
return internal.NewKeyBasedBatchBuilder, nil
default:
return nil, errors.New("unsupported batcher builder provider type")
}
}
167 changes: 167 additions & 0 deletions pulsar/consumer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1712,3 +1712,170 @@ func TestConsumerName(t *testing.T) {

assert.Equal(consumerName, consumer.Name())
}

func TestKeyBasedBatchProducerConsumerKeyShared(t *testing.T) {
const MsgBatchCount = 100
client, err := NewClient(ClientOptions{
URL: lookupURL,
})
assert.Nil(t, err)
defer client.Close()

topic := "persistent://public/default/test-key-based-batch-with-key-shared"

consumer1, err := client.Subscribe(ConsumerOptions{
Topic: topic,
SubscriptionName: "sub-1",
Type: KeyShared,
})
assert.Nil(t, err)
defer consumer1.Close()

consumer2, err := client.Subscribe(ConsumerOptions{
Topic: topic,
SubscriptionName: "sub-1",
Type: KeyShared,
})
assert.Nil(t, err)
defer consumer2.Close()

// create producer
producer, err := client.CreateProducer(ProducerOptions{
Topic: topic,
DisableBatching: false,
BatcherBuilderType: KeyBasedBatchBuilder,
BatchingMaxMessages: 10,
})
assert.Nil(t, err)
defer producer.Close()

ctx := context.Background()
keys := []string{"key1", "key2", "key3"}
for i := 0; i < MsgBatchCount; i++ {
for _, k := range keys {
producer.SendAsync(ctx, &ProducerMessage{
Key: k,
Payload: []byte(fmt.Sprintf("value-%d", i)),
}, func(id MessageID, producerMessage *ProducerMessage, err error) {
assert.Nil(t, err)
},
)
}
}

receivedConsumer1 := 0
receivedConsumer2 := 0
consumer1Keys := make(map[string]int)
consumer2Keys := make(map[string]int)
for (receivedConsumer1 + receivedConsumer2) < 300 {
select {
case cm, ok := <-consumer1.Chan():
if !ok {
break
}
receivedConsumer1++
cnt := 0
if _, has := consumer1Keys[cm.Key()]; has {
cnt = consumer1Keys[cm.Key()]
}
assert.Equal(
t, fmt.Sprintf("value-%d", cnt),
string(cm.Payload()),
)
consumer1Keys[cm.Key()] = cnt + 1
consumer1.Ack(cm.Message)
case cm, ok := <-consumer2.Chan():
if !ok {
break
}
receivedConsumer2++
cnt := 0
if _, has := consumer2Keys[cm.Key()]; has {
cnt = consumer2Keys[cm.Key()]
}
assert.Equal(
t, fmt.Sprintf("value-%d", cnt),
string(cm.Payload()),
)
consumer2Keys[cm.Key()] = cnt + 1
consumer2.Ack(cm.Message)
}
}

assert.NotEqual(t, 0, receivedConsumer1)
assert.NotEqual(t, 0, receivedConsumer2)
assert.Equal(t, len(consumer1Keys)*MsgBatchCount, receivedConsumer1)
assert.Equal(t, len(consumer2Keys)*MsgBatchCount, receivedConsumer2)

fmt.Printf("TestKeyBasedBatchProducerConsumerKeyShared received messages consumer1: %d consumser2: %d\n",
receivedConsumer1, receivedConsumer2)
assert.Equal(t, 300, receivedConsumer1+receivedConsumer2)

fmt.Printf("TestKeyBasedBatchProducerConsumerKeyShared received messages keys consumer1: %v consumser2: %v\n",
consumer1Keys, consumer2Keys)
}
freeznet marked this conversation as resolved.
Show resolved Hide resolved

func TestOrderingOfKeyBasedBatchProducerConsumerKeyShared(t *testing.T) {
const MsgBatchCount = 10
client, err := NewClient(ClientOptions{
URL: lookupURL,
})
assert.Nil(t, err)
defer client.Close()

topic := "persistent://public/default/test-ordering-of-key-based-batch-with-key-shared"

consumer1, err := client.Subscribe(ConsumerOptions{
Topic: topic,
SubscriptionName: "sub-1",
Type: KeyShared,
})
assert.Nil(t, err)
defer consumer1.Close()

// create producer
producer, err := client.CreateProducer(ProducerOptions{
Topic: topic,
DisableBatching: false,
BatcherBuilderType: KeyBasedBatchBuilder,
BatchingMaxMessages: 30,
BatchingMaxPublishDelay: time.Second * 5,
})
assert.Nil(t, err)
defer producer.Close()

ctx := context.Background()
keys := []string{"key1", "key2", "key3"}
for i := 0; i < MsgBatchCount; i++ {
for _, k := range keys {
producer.SendAsync(ctx, &ProducerMessage{
Key: k,
Payload: []byte(fmt.Sprintf("value-%d", i)),
}, func(id MessageID, producerMessage *ProducerMessage, err error) {
assert.Nil(t, err)
},
)
}
}

var receivedKey string
var receivedMessageIndex int
for i := 0; i < len(keys)*MsgBatchCount; i++ {
cm, ok := <-consumer1.Chan()
if !ok {
break
}
if receivedKey != cm.Key() {
receivedKey = cm.Key()
receivedMessageIndex = 0
}
assert.Equal(
t, fmt.Sprintf("value-%d", receivedMessageIndex%10),
string(cm.Payload()),
)
consumer1.Ack(cm.Message)
receivedMessageIndex++
}

// TODO: add OrderingKey support, see GH issue #401
}
Loading