-
Notifications
You must be signed in to change notification settings - Fork 336
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Fix] Fix the key-based batch can't guarantee the ordering when flush…
…ing (#1252) ### Motivation When using key-based producing, the key-based batcher cannot guarantee the sequence ID order when flushing messages. This leads to frequent disconnections between the client and the broker, resulting in poor performance. Here is the related log: ``` time="2024-07-19T03:38:07Z" level=warning msg="Received send error from server: [PersistenceError] : [Cannot determine whether the message is a duplicate at this time]" time="2024-07-19T03:38:07Z" level=warning msg="Connection was closed" cnx="xxx" producerID=1 producer_name=xxx topic="xxx" time="2024-07-19T03:38:07Z" level=warning msg="Failed to write on connection" error="use of closed network connection" local_addr="xxx" remote_addr="xxx" ``` The broker receives unordered messages and returns a SendError to the client. This causes the client to reconnect to the broker. ### Modifications - Refactor the BatchBuilder interface. - Sort batches before flushing in the key-based batcher builder. (cherry picked from commit e2fd4c9)
- Loading branch information
1 parent
81eca35
commit 73fd374
Showing
4 changed files
with
133 additions
and
60 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// 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 internal | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/apache/pulsar-client-go/pulsar/internal/compression" | ||
pb "github.com/apache/pulsar-client-go/pulsar/internal/pulsar_proto" | ||
"github.com/apache/pulsar-client-go/pulsar/log" | ||
"github.com/sirupsen/logrus" | ||
"github.com/stretchr/testify/assert" | ||
"google.golang.org/protobuf/proto" | ||
) | ||
|
||
type mockBufferPool struct { | ||
} | ||
|
||
func (m *mockBufferPool) GetBuffer() Buffer { | ||
return nil | ||
} | ||
|
||
type mockEncryptor struct { | ||
} | ||
|
||
func (m *mockEncryptor) Encrypt(_ []byte, _ *pb.MessageMetadata) ([]byte, error) { | ||
return []byte("test"), nil | ||
} | ||
|
||
func TestKeyBasedBatcherOrdering(t *testing.T) { | ||
keyBatcher, err := NewKeyBasedBatchBuilder( | ||
1000, | ||
1000, | ||
1000, | ||
"test", | ||
1, | ||
pb.CompressionType_NONE, | ||
compression.Level(0), | ||
&mockBufferPool{}, | ||
log.NewLoggerWithLogrus(logrus.StandardLogger()), | ||
&mockEncryptor{}, | ||
) | ||
if err != nil { | ||
assert.Fail(t, "Failed to create key based batcher") | ||
} | ||
|
||
sequenceID := uint64(0) | ||
for i := 0; i < 10; i++ { | ||
metadata := &pb.SingleMessageMetadata{ | ||
OrderingKey: []byte(fmt.Sprintf("key-%d", i)), | ||
PayloadSize: proto.Int32(0), | ||
} | ||
assert.True(t, keyBatcher.Add(metadata, &sequenceID, []byte("test"), nil, nil, time.Now(), | ||
nil, false, false, 0, 0)) | ||
} | ||
|
||
batches := keyBatcher.FlushBatches() | ||
for i := 1; i < len(batches); i++ { | ||
if batches[i].SequenceID <= batches[i-1].SequenceID { | ||
t.Errorf("Batch id is not incremental at index %d: %d <= %d", i, batches[i].SequenceID, batches[i-1].SequenceID) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters