You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@pulsar.apache.org by "BewareMyPower (via GitHub)" <gi...@apache.org> on 2023/08/29 09:10:22 UTC
[GitHub] [pulsar-client-cpp] BewareMyPower opened a new pull request, #309: Avoid calling serializeSingleMessageInBatchWithPayload each time a message is added
BewareMyPower opened a new pull request, #309:
URL: https://github.com/apache/pulsar-client-cpp/pull/309
### Motivation
Currently, each time a message is added to the batch message container,
`serializeSingleMessageInBatchWithPayload` will be called. In this
method, if the payload buffer's size is not enough, it will grow twice.
After batch is cleared, the payload buffer will be reset. For example,
here is a typical buffer size increament during a period of a batch:
```
increase buffer size from 0 to 1033
increase buffer size from 1033 to 2066
increase buffer size from 2066 to 4132
increase buffer size from 3099 to 6198
increase buffer size from 5165 to 10330
increase buffer size from 9297 to 18594
increase buffer size from 17561 to 35122
increase buffer size from 34089 to 68178
increase buffer size from 67145 to 134290
```
### Modifications
Refactor the `MessageAndCallbackBatch` design, in `add` method, only
store the message and callback. Provide a `createOpSendMsg` method to
serialize the messages and callbacks into a `OpSendMsg`.
### Documentation
<!-- DO NOT REMOVE THIS SECTION. CHECK THE PROPER BOX ONLY. -->
- [ ] `doc-required`
(Your PR needs to update docs and you will update later)
- [x] `doc-not-needed`
(Please explain why)
- [ ] `doc`
(Your PR contains doc changes)
- [ ] `doc-complete`
(Docs have been already added)
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar-client-cpp] BewareMyPower closed pull request #309: Avoid calling serializeSingleMessageInBatchWithPayload each time a message is added
Posted by "BewareMyPower (via GitHub)" <gi...@apache.org>.
BewareMyPower closed pull request #309: Avoid calling serializeSingleMessageInBatchWithPayload each time a message is added
URL: https://github.com/apache/pulsar-client-cpp/pull/309
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar-client-cpp] BewareMyPower merged pull request #309: Avoid calling serializeSingleMessageInBatchWithPayload each time a message is added
Posted by "BewareMyPower (via GitHub)" <gi...@apache.org>.
BewareMyPower merged PR #309:
URL: https://github.com/apache/pulsar-client-cpp/pull/309
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar-client-cpp] shibd commented on a diff in pull request #309: Avoid calling serializeSingleMessageInBatchWithPayload each time a message is added
Posted by "shibd (via GitHub)" <gi...@apache.org>.
shibd commented on code in PR #309:
URL: https://github.com/apache/pulsar-client-cpp/pull/309#discussion_r1312830633
##########
lib/MessageAndCallbackBatch.cc:
##########
@@ -22,59 +22,95 @@
#include "ClientConnection.h"
#include "Commands.h"
-#include "LogUtils.h"
-#include "MessageImpl.h"
-
-DECLARE_LOG_OBJECT()
+#include "CompressionCodec.h"
+#include "MessageCrypto.h"
+#include "OpSendMsg.h"
+#include "PulsarApi.pb.h"
namespace pulsar {
+MessageAndCallbackBatch::MessageAndCallbackBatch() {}
+
+MessageAndCallbackBatch::~MessageAndCallbackBatch() {}
+
void MessageAndCallbackBatch::add(const Message& msg, const SendCallback& callback) {
- if (empty()) {
- msgImpl_.reset(new MessageImpl);
- Commands::initBatchMessageMetadata(msg, msgImpl_->metadata);
+ if (callbacks_.empty()) {
+ metadata_.reset(new proto::MessageMetadata);
+ Commands::initBatchMessageMetadata(msg, *metadata_);
+ sequenceId_ = metadata_->sequence_id();
}
- LOG_DEBUG(" Before serialization payload size in bytes = " << msgImpl_->payload.readableBytes());
- sequenceId_ = Commands::serializeSingleMessageInBatchWithPayload(msg, msgImpl_->payload,
- ClientConnection::getMaxMessageSize());
- LOG_DEBUG(" After serialization payload size in bytes = " << msgImpl_->payload.readableBytes());
+ messages_.emplace_back(msg);
callbacks_.emplace_back(callback);
-
- ++messagesCount_;
messagesSize_ += msg.getLength();
}
+std::unique_ptr<OpSendMsg> MessageAndCallbackBatch::createOpSendMsg(
+ uint64_t producerId, const ProducerConfiguration& producerConfig, MessageCrypto* crypto) {
+ auto callback = createSendCallback();
+ if (empty()) {
+ return OpSendMsg::create(ResultOperationNotSupported, std::move(callback));
+ }
+
+ // The magic number 64 is just an estimated size increment after setting some fields of the
+ // SingleMessageMetadata. It does not have to be accurate because it's only used to reduce the
+ // reallocation of the payload buffer.
+ static const size_t kEstimatedHeaderSize =
Review Comment:
Why do we need to estimate metadata size here? We can get the actual metadata size of each msg. Right?
This estimation way may always resize the buffer at least once, right?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar-client-cpp] BewareMyPower commented on a diff in pull request #309: Avoid calling serializeSingleMessageInBatchWithPayload each time a message is added
Posted by "BewareMyPower (via GitHub)" <gi...@apache.org>.
BewareMyPower commented on code in PR #309:
URL: https://github.com/apache/pulsar-client-cpp/pull/309#discussion_r1312867199
##########
lib/MessageAndCallbackBatch.cc:
##########
@@ -22,59 +22,95 @@
#include "ClientConnection.h"
#include "Commands.h"
-#include "LogUtils.h"
-#include "MessageImpl.h"
-
-DECLARE_LOG_OBJECT()
+#include "CompressionCodec.h"
+#include "MessageCrypto.h"
+#include "OpSendMsg.h"
+#include "PulsarApi.pb.h"
namespace pulsar {
+MessageAndCallbackBatch::MessageAndCallbackBatch() {}
+
+MessageAndCallbackBatch::~MessageAndCallbackBatch() {}
+
void MessageAndCallbackBatch::add(const Message& msg, const SendCallback& callback) {
- if (empty()) {
- msgImpl_.reset(new MessageImpl);
- Commands::initBatchMessageMetadata(msg, msgImpl_->metadata);
+ if (callbacks_.empty()) {
+ metadata_.reset(new proto::MessageMetadata);
+ Commands::initBatchMessageMetadata(msg, *metadata_);
+ sequenceId_ = metadata_->sequence_id();
}
- LOG_DEBUG(" Before serialization payload size in bytes = " << msgImpl_->payload.readableBytes());
- sequenceId_ = Commands::serializeSingleMessageInBatchWithPayload(msg, msgImpl_->payload,
- ClientConnection::getMaxMessageSize());
- LOG_DEBUG(" After serialization payload size in bytes = " << msgImpl_->payload.readableBytes());
+ messages_.emplace_back(msg);
callbacks_.emplace_back(callback);
-
- ++messagesCount_;
messagesSize_ += msg.getLength();
}
+std::unique_ptr<OpSendMsg> MessageAndCallbackBatch::createOpSendMsg(
+ uint64_t producerId, const ProducerConfiguration& producerConfig, MessageCrypto* crypto) {
+ auto callback = createSendCallback();
+ if (empty()) {
+ return OpSendMsg::create(ResultOperationNotSupported, std::move(callback));
+ }
+
+ // The magic number 64 is just an estimated size increment after setting some fields of the
+ // SingleMessageMetadata. It does not have to be accurate because it's only used to reduce the
+ // reallocation of the payload buffer.
+ static const size_t kEstimatedHeaderSize =
Review Comment:
I uses an estimated size just to avoid calling `MessageMetadata::ByteSizeLong()` repeatedly in `serializeSingleMessageInBatchWithPayload`.
I observed the overhead of `ByteSizeLong` cannot be ignored when I ran perf tests:
<img width="581" alt="image" src="https://github.com/apache/pulsar-client-cpp/assets/18204803/ea72f255-6e88-4675-84ea-d87e7530e028">
However, the flamegraph is based on a debug library. After rechecking the flamegraph for a release library, I found it does not take much time. So I think we can simply use an accurate size here just like you suggested.
I will fix it ASAP.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar-client-cpp] BewareMyPower commented on pull request #309: Avoid calling serializeSingleMessageInBatchWithPayload each time a message is added
Posted by "BewareMyPower (via GitHub)" <gi...@apache.org>.
BewareMyPower commented on PR #309:
URL: https://github.com/apache/pulsar-client-cpp/pull/309#issuecomment-1697089822
This PR is based on https://github.com/apache/pulsar-client-cpp/pull/308, please review #308 first.
3.3.0, the `SharedBuffer::allocate` takes 5.47% CPU in `sendAsync`.
<img width="663" alt="image" src="https://github.com/apache/pulsar-client-cpp/assets/18204803/029cfd41-9414-41cd-a287-499ed33a6dfd">
Now, it only takes 1.07% CPU in `createOpSendMsg`:
<img width="1244" alt="image" src="https://github.com/apache/pulsar-client-cpp/assets/18204803/2faed895-7d9b-4f0a-9ab8-761237a0557b">
The throughput improvement. (batch-size: 1000, max-batch-publish-delay-in-ms: 1) In this case, `batchMessageAndSend` happens most in the batch timer callback.
3.3.0:
```
2023-08-29 09:16:23.578 INFO [140065621355136] PerfProducer:415 | Throughput produced: 78904.9 msg/s --- 616.444 Mbit/s --- Lat avg: 4.19447 ms -- Lat 99pct: 12.3828 ms
2023-08-29 09:16:43.578 INFO [140065621355136] PerfProducer:415 | Throughput produced: 78045.6 msg/s --- 609.732 Mbit/s --- Lat avg: 4.05874 ms -- Lat 99pct: 5.81648 ms
2023-08-29 09:17:03.578 INFO [140065621355136] PerfProducer:415 | Throughput produced: 78022.9 msg/s --- 609.554 Mbit/s --- Lat avg: 4.09393 ms -- Lat 99pct: 7.35203 ms
2023-08-29 09:17:23.578 INFO [140065621355136] PerfProducer:415 | Throughput produced: 78186.4 msg/s --- 610.831 Mbit/s --- Lat avg: 4.1141 ms -- Lat 99pct: 6.19954 ms
```
After this patch:
```
2023-08-29 09:19:47.382 INFO [140459705907968] PerfProducer:415 | Throughput produced: 79756.1 msg/s --- 623.095 Mbit/s --- Lat avg: 4.15424 ms -- Lat 99pct: 11.4696 ms
2023-08-29 09:20:07.382 INFO [140459705907968] PerfProducer:415 | Throughput produced: 79218.1 msg/s --- 618.891 Mbit/s --- Lat avg: 4.03885 ms -- Lat 99pct: 7.21177 ms
2023-08-29 09:20:27.383 INFO [140459705907968] PerfProducer:415 | Throughput produced: 79229.6 msg/s --- 618.982 Mbit/s --- Lat avg: 4.05724 ms -- Lat 99pct: 8.21727 ms
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org
[GitHub] [pulsar-client-cpp] BewareMyPower commented on pull request #309: Avoid calling serializeSingleMessageInBatchWithPayload each time a message is added
Posted by "BewareMyPower (via GitHub)" <gi...@apache.org>.
BewareMyPower commented on PR #309:
URL: https://github.com/apache/pulsar-client-cpp/pull/309#issuecomment-1702689151
@shibd I replaced the `serializeSingleMessageInBatchWithPayload` method with `serializeSingleMessagesToBatchPayload`, PTAL again.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: commits-unsubscribe@pulsar.apache.org
For queries about this service, please contact Infrastructure at:
users@infra.apache.org