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

Read the clock fewer times during message routing #408

Merged
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
24 changes: 18 additions & 6 deletions pulsar/default_router.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ func NewDefaultRouter(
lastChangeTimestamp: math.MinInt64,
}

readClockAfterNumMessages := uint32(maxBatchingMessages / 10)
return func(message *ProducerMessage, numPartitions uint32) int {
if numPartitions == 1 {
// When there are no partitions, don't even bother
Expand All @@ -72,23 +73,34 @@ func NewDefaultRouter(
// Note that it is possible that we skip more than one partition if multiple goroutines increment
// currentPartitionCursor at the same time. If that happens it shouldn't be a problem because we only want to
// spread the data on different partitions but not necessarily in a specific sequence.
var now int64
size := uint32(len(message.Payload))
previousMessageCount := atomic.LoadUint32(&state.msgCounter)
previousBatchingMaxSize := atomic.LoadUint32(&state.cumulativeBatchSize)
previousLastChange := atomic.LoadInt64(&state.lastChangeTimestamp)
if (previousMessageCount >= uint32(maxBatchingMessages-1)) ||
(size >= uint32(maxBatchingSize)-previousBatchingMaxSize) ||
(time.Now().UnixNano()-previousLastChange >= maxBatchingDelay.Nanoseconds()) {

messageCountReached := previousMessageCount >= uint32(maxBatchingMessages-1)
sizeReached := (size >= uint32(maxBatchingSize)-previousBatchingMaxSize)
durationReached := false
if readClockAfterNumMessages == 0 || previousMessageCount%readClockAfterNumMessages == 0 {
now = time.Now().UnixNano()
durationReached = now-previousLastChange >= maxBatchingDelay.Nanoseconds()
}
if messageCountReached || sizeReached || durationReached {
atomic.AddUint32(&state.currentPartitionCursor, 1)
atomic.StoreInt64(&state.lastChangeTimestamp, time.Now().UnixNano())
atomic.StoreUint32(&state.cumulativeBatchSize, 0)
atomic.StoreUint32(&state.msgCounter, 0)
atomic.StoreUint32(&state.cumulativeBatchSize, 0)
if now != 0 {
atomic.StoreInt64(&state.lastChangeTimestamp, now)
}
return int(state.currentPartitionCursor % numPartitions)
}

atomic.StoreInt64(&state.lastChangeTimestamp, time.Now().UnixNano())
atomic.AddUint32(&state.msgCounter, 1)
atomic.AddUint32(&state.cumulativeBatchSize, size)
if now != 0 {
atomic.StoreInt64(&state.lastChangeTimestamp, now)
}
return int(state.currentPartitionCursor % numPartitions)
}
}
45 changes: 45 additions & 0 deletions pulsar/default_router_bench_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// 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 (
"testing"
"time"

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

var (
targetPartition int
)

func BenchmarkDefaultRouter(b *testing.B) {
const (
numPartitions = uint32(200)
maxBatchingMessages = 2000
maxBatchingSize = 524288
maxBatchingDelay = 100 * time.Millisecond
)
msg := &ProducerMessage{
Payload: []byte("message 1"),
}
router := NewDefaultRouter(internal.JavaStringHash, maxBatchingMessages, maxBatchingSize, maxBatchingDelay, false)
for i := 0; i < b.N; i++ {
targetPartition = router(msg, numPartitions)
}
}
2 changes: 1 addition & 1 deletion pulsar/default_router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func TestDefaultRouterRoutingBecauseBatchingDisabled(t *testing.T) {

func TestDefaultRouterRoutingBecauseMaxPublishDelayReached(t *testing.T) {
maxPublishDelay := time.Nanosecond * 10
router := NewDefaultRouter(internal.JavaStringHash, 20, 100, maxPublishDelay, false)
router := NewDefaultRouter(internal.JavaStringHash, 10, 100, maxPublishDelay, false)
const numPartitions = uint32(3)
p1 := router(&ProducerMessage{
Payload: []byte("message 1"),
Expand Down