|
22 | 22 |
|
23 | 23 | #include "ClientConnection.h" |
24 | 24 | #include "Commands.h" |
25 | | -#include "LogUtils.h" |
26 | | -#include "MessageImpl.h" |
27 | | - |
28 | | -DECLARE_LOG_OBJECT() |
| 25 | +#include "CompressionCodec.h" |
| 26 | +#include "MessageCrypto.h" |
| 27 | +#include "OpSendMsg.h" |
| 28 | +#include "PulsarApi.pb.h" |
29 | 29 |
|
30 | 30 | namespace pulsar { |
31 | 31 |
|
| 32 | +MessageAndCallbackBatch::MessageAndCallbackBatch() {} |
| 33 | + |
| 34 | +MessageAndCallbackBatch::~MessageAndCallbackBatch() {} |
| 35 | + |
32 | 36 | void MessageAndCallbackBatch::add(const Message& msg, const SendCallback& callback) { |
33 | | - if (empty()) { |
34 | | - msgImpl_.reset(new MessageImpl); |
35 | | - Commands::initBatchMessageMetadata(msg, msgImpl_->metadata); |
| 37 | + if (callbacks_.empty()) { |
| 38 | + metadata_.reset(new proto::MessageMetadata); |
| 39 | + Commands::initBatchMessageMetadata(msg, *metadata_); |
36 | 40 | } |
37 | | - LOG_DEBUG(" Before serialization payload size in bytes = " << msgImpl_->payload.readableBytes()); |
38 | | - sequenceId_ = Commands::serializeSingleMessageInBatchWithPayload(msg, msgImpl_->payload, |
39 | | - ClientConnection::getMaxMessageSize()); |
40 | | - LOG_DEBUG(" After serialization payload size in bytes = " << msgImpl_->payload.readableBytes()); |
| 41 | + messages_.emplace_back(msg); |
41 | 42 | callbacks_.emplace_back(callback); |
42 | | - |
43 | | - ++messagesCount_; |
44 | 43 | messagesSize_ += msg.getLength(); |
45 | 44 | } |
46 | 45 |
|
| 46 | +std::unique_ptr<OpSendMsg> MessageAndCallbackBatch::createOpSendMsg( |
| 47 | + uint64_t producerId, const ProducerConfiguration& producerConfig, MessageCrypto* crypto) { |
| 48 | + auto callback = createSendCallback(); |
| 49 | + if (empty()) { |
| 50 | + return OpSendMsg::create(ResultOperationNotSupported, std::move(callback)); |
| 51 | + } |
| 52 | + |
| 53 | + // TODO: Store payload as a field and support shrinking |
| 54 | + SharedBuffer payload; |
| 55 | + auto sequenceId = Commands::serializeSingleMessagesToBatchPayload(payload, messages_); |
| 56 | + metadata_->set_sequence_id(sequenceId); |
| 57 | + metadata_->set_num_messages_in_batch(messages_.size()); |
| 58 | + auto compressionType = producerConfig.getCompressionType(); |
| 59 | + if (compressionType != CompressionNone) { |
| 60 | + metadata_->set_compression(static_cast<proto::CompressionType>(compressionType)); |
| 61 | + metadata_->set_uncompressed_size(payload.readableBytes()); |
| 62 | + } |
| 63 | + payload = CompressionCodecProvider::getCodec(compressionType).encode(payload); |
| 64 | + |
| 65 | + if (producerConfig.isEncryptionEnabled() && crypto) { |
| 66 | + SharedBuffer encryptedPayload; |
| 67 | + if (!crypto->encrypt(producerConfig.getEncryptionKeys(), producerConfig.getCryptoKeyReader(), |
| 68 | + *metadata_, payload, encryptedPayload)) { |
| 69 | + return OpSendMsg::create(ResultCryptoError, std::move(callback)); |
| 70 | + } |
| 71 | + payload = encryptedPayload; |
| 72 | + } |
| 73 | + |
| 74 | + if (payload.readableBytes() > ClientConnection::getMaxMessageSize()) { |
| 75 | + return OpSendMsg::create(ResultMessageTooBig, std::move(callback)); |
| 76 | + } |
| 77 | + |
| 78 | + auto op = OpSendMsg::create(*metadata_, callbacks_.size(), messagesSize_, producerConfig.getSendTimeout(), |
| 79 | + std::move(callback), nullptr, producerId, payload); |
| 80 | + clear(); |
| 81 | + return op; |
| 82 | +} |
| 83 | + |
47 | 84 | void MessageAndCallbackBatch::clear() { |
48 | | - msgImpl_.reset(); |
| 85 | + messages_.clear(); |
49 | 86 | callbacks_.clear(); |
50 | | - messagesCount_ = 0; |
51 | 87 | messagesSize_ = 0; |
52 | 88 | } |
53 | 89 |
|
54 | 90 | static void completeSendCallbacks(const std::vector<SendCallback>& callbacks, Result result, |
55 | 91 | const MessageId& id) { |
56 | 92 | int32_t numOfMessages = static_cast<int32_t>(callbacks.size()); |
57 | | - LOG_DEBUG("Batch complete [Result = " << result << "] [numOfMessages = " << numOfMessages << "]"); |
58 | 93 | for (int32_t i = 0; i < numOfMessages; i++) { |
59 | 94 | callbacks[i](result, MessageIdBuilder::from(id).batchIndex(i).batchSize(numOfMessages).build()); |
60 | 95 | } |
61 | 96 | } |
62 | 97 |
|
63 | | -void MessageAndCallbackBatch::complete(Result result, const MessageId& id) const { |
64 | | - completeSendCallbacks(callbacks_, result, id); |
65 | | -} |
66 | | - |
67 | | -SendCallback MessageAndCallbackBatch::createSendCallback(const FlushCallback& flushCallback) const { |
| 98 | +SendCallback MessageAndCallbackBatch::createSendCallback() const { |
68 | 99 | const auto& callbacks = callbacks_; |
69 | | - if (flushCallback) { |
70 | | - return [callbacks, flushCallback](Result result, const MessageId& id) { |
71 | | - completeSendCallbacks(callbacks, result, id); |
72 | | - flushCallback(result); |
73 | | - }; |
74 | | - } else { |
75 | | - return [callbacks] // save a copy of `callbacks_` |
76 | | - (Result result, const MessageId& id) { completeSendCallbacks(callbacks, result, id); }; |
77 | | - } |
| 100 | + return [callbacks](Result result, const MessageId& id) { completeSendCallbacks(callbacks, result, id); }; |
78 | 101 | } |
79 | 102 |
|
80 | 103 | } // namespace pulsar |
0 commit comments