Skip to content

Support Key-based batcher for producer #418

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 2 commits into from
May 26, 2025
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
6 changes: 6 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ export interface ProducerConfig {
chunkingEnabled?: boolean;
schema?: SchemaInfo;
accessMode?: ProducerAccessMode;
batchingType?: ProducerBatchType;
}

export class Producer {
Expand Down Expand Up @@ -163,6 +164,7 @@ export class Message {
getEventTimestamp(): number;
getRedeliveryCount(): number;
getPartitionKey(): string;
getOrderingKey(): string;
}

export class MessageId {
Expand Down Expand Up @@ -271,6 +273,10 @@ export type CompressionType =
'ZSTD' |
'SNAPPY';

export type ProducerBatchType =
'DefaultBatching' |
'KeyBasedBatching';

export type ProducerCryptoFailureAction =
'FAIL' |
'SEND';
Expand Down
11 changes: 10 additions & 1 deletion src/Message.cc
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,8 @@ Napi::Object Message::Init(Napi::Env env, Napi::Object exports) {
InstanceMethod("getPublishTimestamp", &Message::GetPublishTimestamp),
InstanceMethod("getEventTimestamp", &Message::GetEventTimestamp),
InstanceMethod("getRedeliveryCount", &Message::GetRedeliveryCount),
InstanceMethod("getPartitionKey", &Message::GetPartitionKey)});
InstanceMethod("getPartitionKey", &Message::GetPartitionKey),
InstanceMethod("getOrderingKey", &Message::GetOrderingKey)});

constructor = Napi::Persistent(func);
constructor.SuppressDestruct();
Expand Down Expand Up @@ -138,6 +139,14 @@ Napi::Value Message::GetPartitionKey(const Napi::CallbackInfo &info) {
return Napi::String::New(env, pulsar_message_get_partitionKey(this->cMessage.get()));
}

Napi::Value Message::GetOrderingKey(const Napi::CallbackInfo &info) {
Napi::Env env = info.Env();
if (!ValidateCMessage(env)) {
return env.Null();
}
return Napi::String::New(env, pulsar_message_get_orderingKey(this->cMessage.get()));
}

bool Message::ValidateCMessage(Napi::Env env) {
if (this->cMessage.get()) {
return true;
Expand Down
1 change: 1 addition & 0 deletions src/Message.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class Message : public Napi::ObjectWrap<Message> {
Napi::Value GetPublishTimestamp(const Napi::CallbackInfo &info);
Napi::Value GetEventTimestamp(const Napi::CallbackInfo &info);
Napi::Value GetPartitionKey(const Napi::CallbackInfo &info);
Napi::Value GetOrderingKey(const Napi::CallbackInfo &info);
Napi::Value GetRedeliveryCount(const Napi::CallbackInfo &info);
bool ValidateCMessage(Napi::Env env);

Expand Down
16 changes: 16 additions & 0 deletions src/ProducerConfig.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
#include "SchemaInfo.h"
#include "ProducerConfig.h"
#include <map>
#include "pulsar/ProducerConfiguration.h"

static const std::string CFG_TOPIC = "topic";
static const std::string CFG_PRODUCER_NAME = "producerName";
Expand All @@ -40,6 +41,11 @@ static const std::string CFG_ENCRYPTION_KEY = "encryptionKey";
static const std::string CFG_CRYPTO_FAILURE_ACTION = "cryptoFailureAction";
static const std::string CFG_CHUNK_ENABLED = "chunkingEnabled";
static const std::string CFG_ACCESS_MODE = "accessMode";
static const std::string CFG_BATCHING_TYPE = "batchingType";

struct _pulsar_producer_configuration {
pulsar::ProducerConfiguration conf;
};

static const std::map<std::string, pulsar_partitions_routing_mode> MESSAGE_ROUTING_MODE = {
{"UseSinglePartition", pulsar_UseSinglePartition},
Expand Down Expand Up @@ -71,6 +77,11 @@ static std::map<std::string, pulsar_producer_access_mode> PRODUCER_ACCESS_MODE =
{"ExclusiveWithFencing", pulsar_ProducerAccessModeExclusiveWithFencing},
};

static std::map<std::string, pulsar::ProducerConfiguration::BatchingType> PRODUCER_BATCHING_TYPE = {
{"DefaultBatching", pulsar::ProducerConfiguration::DefaultBatching},
{"KeyBasedBatching", pulsar::ProducerConfiguration::KeyBasedBatching},
};

ProducerConfig::ProducerConfig(const Napi::Object& producerConfig) : topic("") {
this->cProducerConfig = std::shared_ptr<pulsar_producer_configuration_t>(
pulsar_producer_configuration_create(), pulsar_producer_configuration_free);
Expand Down Expand Up @@ -208,6 +219,11 @@ ProducerConfig::ProducerConfig(const Napi::Object& producerConfig) : topic("") {
pulsar_producer_configuration_set_access_mode(this->cProducerConfig.get(),
PRODUCER_ACCESS_MODE.at(accessMode));
}

std::string batchingType = producerConfig.Get(CFG_BATCHING_TYPE).ToString().Utf8Value();
if (PRODUCER_BATCHING_TYPE.count(batchingType)) {
this->cProducerConfig.get()->conf.setBatchingType(PRODUCER_BATCHING_TYPE.at(batchingType));
}
}

ProducerConfig::~ProducerConfig() {}
Expand Down
114 changes: 114 additions & 0 deletions tests/end_to_end.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@
});

let consumer2Recv = 0;
while (true) {

Check warning on line 314 in tests/end_to_end.test.js

View workflow job for this annotation

GitHub Actions / Run unit tests (3.10)

Unexpected constant condition

Check warning on line 314 in tests/end_to_end.test.js

View workflow job for this annotation

GitHub Actions / Run unit tests (3.10)

Unexpected constant condition
try {
const msg = await consumer2.receive(3000);
await new Promise((resolve) => setTimeout(resolve, 10));
Expand Down Expand Up @@ -356,7 +356,7 @@
topic,
startMessageId: Pulsar.MessageId.earliest(),
receiverQueueSize: 10,
listener: async (message, reader) => {

Check warning on line 359 in tests/end_to_end.test.js

View workflow job for this annotation

GitHub Actions / Run unit tests (3.10)

'message' is defined but never used

Check warning on line 359 in tests/end_to_end.test.js

View workflow job for this annotation

GitHub Actions / Run unit tests (3.10)

'reader' is defined but never used

Check warning on line 359 in tests/end_to_end.test.js

View workflow job for this annotation

GitHub Actions / Run unit tests (3.10)

'message' is defined but never used
await new Promise((resolve) => setTimeout(resolve, 10));
reader1Recv += 1;
},
Expand Down Expand Up @@ -388,7 +388,7 @@
await client.close();
});

test('Message Listener error handling', async () => {

Check warning on line 391 in tests/end_to_end.test.js

View workflow job for this annotation

GitHub Actions / Run unit tests (3.10)

Test has no assertions
const client = new Pulsar.Client({
serviceUrl: 'pulsar://localhost:6650',
});
Expand Down Expand Up @@ -424,7 +424,7 @@
subscription: 'sync',
subscriptionType: 'Shared',
subscriptionInitialPosition: 'Earliest',
listener: (message, messageConsumer) => {

Check warning on line 427 in tests/end_to_end.test.js

View workflow job for this annotation

GitHub Actions / Run unit tests (3.10)

'message' is defined but never used

Check warning on line 427 in tests/end_to_end.test.js

View workflow job for this annotation

GitHub Actions / Run unit tests (3.10)

'messageConsumer' is defined but never used
throw new Error('consumer1 callback expected error');
},
});
Expand All @@ -434,7 +434,7 @@
subscription: 'async',
subscriptionType: 'Shared',
subscriptionInitialPosition: 'Earliest',
listener: async (message, messageConsumer) => {

Check warning on line 437 in tests/end_to_end.test.js

View workflow job for this annotation

GitHub Actions / Run unit tests (3.10)

'message' is defined but never used

Check warning on line 437 in tests/end_to_end.test.js

View workflow job for this annotation

GitHub Actions / Run unit tests (3.10)

'messageConsumer' is defined but never used
throw new Error('consumer2 callback expected error');
},
});
Expand Down Expand Up @@ -1330,4 +1330,118 @@
await client.close();
});
});
describe('KeyBasedBatchingTest', () => {
let client;
let producer;
let consumer;
let topicName;

beforeAll(async () => {
client = new Pulsar.Client({
serviceUrl: 'pulsar://localhost:6650',
});
});

afterAll(async () => {
await client.close();
});

beforeEach(async () => {
topicName = `KeyBasedBatchingTest-${Date.now()}`;
});

afterEach(async () => {
if (producer) await producer.close();
if (consumer) await consumer.close();
});

const initProducer = async (maxMessages) => {
producer = await client.createProducer({
topic: topicName,
batchingEnabled: true,
batchingMaxMessages: maxMessages,
batchingType: 'KeyBasedBatching',
batchingMaxPublishDelayMs: 3600 * 1000,
});
};

const initConsumer = async () => {
consumer = await client.subscribe({
topic: topicName,
subscription: 'SubscriptionName',
subscriptionType: 'Exclusive',
});
};

const receiveAndAck = async () => {
const msg = await consumer.receive();
await consumer.acknowledge(msg);
return msg;
};

test('testSequenceId', async () => {
await initProducer(6);
await initConsumer();

// 0. Send 6 messages, use different keys and order
producer.send({ data: Buffer.from('0'), partitionKey: 'A' });
producer.send({ data: Buffer.from('1'), partitionKey: 'B' });
producer.send({ data: Buffer.from('2'), partitionKey: 'C' });
producer.send({ data: Buffer.from('3'), partitionKey: 'B' });
producer.send({ data: Buffer.from('4'), partitionKey: 'C' });
producer.send({ data: Buffer.from('5'), partitionKey: 'A' });
await producer.flush();

// 1. Receive all messages
const received = [];
for (let i = 0; i < 6; i += 1) {
const msg = await receiveAndAck();
received.push({
key: msg.getPartitionKey().toString(),
value: msg.getData().toString(),
});
}

// 2. Verify message order (based on key dictionary order)
const expected = [
{ key: 'B', value: '1' },
{ key: 'B', value: '3' },
{ key: 'C', value: '2' },
{ key: 'C', value: '4' },
{ key: 'A', value: '0' },
{ key: 'A', value: '5' },
];

expect(received).toEqual(expected);
});

test('testOrderingKeyPriority', async () => {
await initProducer(3);
await initConsumer();

// 1. Send 3 messages to verify orderingKey takes precedence over partitionKey
producer.send({
data: Buffer.from('0'),
orderingKey: 'A',
partitionKey: 'B',
});
producer.send({ data: Buffer.from('2'), orderingKey: 'B' });
producer.send({ data: Buffer.from('1'), orderingKey: 'A' });
await producer.flush();

// 2. Receive messages and verify their order and keys
const msg1 = await receiveAndAck();
expect(msg1.getData().toString()).toBe('2');
expect(msg1.getOrderingKey().toString()).toBe('B');

const msg2 = await receiveAndAck();
expect(msg2.getData().toString()).toBe('0');
expect(msg2.getOrderingKey()).toBe('A');
expect(msg2.getPartitionKey()).toBe('B');

const msg3 = await receiveAndAck();
expect(msg3.getData().toString()).toBe('1');
expect(msg3.getOrderingKey().toString()).toBe('A');
});
});
})();
Loading