forked from apache/rocketmq-clients
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProducerImpl.h
More file actions
183 lines (140 loc) · 6.03 KB
/
ProducerImpl.h
File metadata and controls
183 lines (140 loc) · 6.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
* 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.
*/
#pragma once
#include <memory>
#include <string>
#include <system_error>
#include "ClientImpl.h"
#include "MixAll.h"
#include "PublishInfoCallback.h"
#include "PublishStats.h"
#include "SendContext.h"
#include "TopicPublishInfo.h"
#include "TransactionImpl.h"
#include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h"
#include "rocketmq/Message.h"
#include "rocketmq/SendCallback.h"
#include "rocketmq/SendReceipt.h"
#include "rocketmq/TransactionChecker.h"
ROCKETMQ_NAMESPACE_BEGIN
class ProducerImpl : virtual public ClientImpl, public std::enable_shared_from_this<ProducerImpl> {
public:
explicit ProducerImpl();
~ProducerImpl() override;
void prepareHeartbeatData(HeartbeatRequest& request) override;
void start() override;
void shutdown() override;
/**
* Note we require application to transfer ownership of the message
* to send to avoid concurrent modification during sent.
*
* Regardless of the send result, SendReceipt would have the std::unique_ptr<const Message>,
* facilitating application to conduct customized retry policy.
*/
SendReceipt send(MessageConstPtr message, std::error_code& ec) noexcept;
/**
* Note we require application to transfer ownership of the message
* to send to avoid concurrent modification during sent.
*
* Regardless of the send result, SendReceipt would have the std::unique_ptr<const Message>,
* facilitating application to conduct customized retry policy.
*/
void send(MessageConstPtr message, SendCallback callback);
void setTransactionChecker(TransactionChecker checker);
std::unique_ptr<TransactionImpl> beginTransaction() {
auto producer = std::weak_ptr<ProducerImpl>(shared_from_this());
return absl::make_unique<TransactionImpl>(producer);
}
/**
* Note we require application to transfer ownership of the message
* to send to avoid concurrent modification during sent.
*/
SendReceipt send(MessageConstPtr message, std::error_code& ec, Transaction& transaction);
/**
* Check if the RPC client for the target host is isolated or not
* @param endpoint Address of target host.
* @return true if client is active; false otherwise.
*/
bool isEndpointIsolated(const std::string& endpoint) LOCKS_EXCLUDED(isolated_endpoints_mtx_);
/**
* Note: This function is purpose-made public such that the whole isolate/add-back mechanism can be properly tested.
* @param target Endpoint of the target host
*/
void isolateEndpoint(const std::string& target) LOCKS_EXCLUDED(isolated_endpoints_mtx_);
std::size_t maxAttemptTimes() const {
return max_attempt_times_;
}
void maxAttemptTimes(std::size_t times) {
max_attempt_times_ = times;
}
int getFailedTimes() const {
return failed_times_;
}
void setFailedTimes(int times) {
failed_times_ = times;
}
uint32_t compressBodyThreshold() const {
return compress_body_threshold_;
}
void compressBodyThreshold(uint32_t threshold) {
compress_body_threshold_ = threshold;
}
void sendImpl(std::shared_ptr<SendContext> callback);
void buildClientSettings(rmq::Settings& settings) override;
void topicsOfInterest(std::vector<std::string> &topics) override LOCKS_EXCLUDED(topics_mtx_);
void withTopics(const std::vector<std::string> &topics) LOCKS_EXCLUDED(topics_mtx_);
const PublishStats& stats() const {
return stats_;
}
bool endTransaction0(const MiniTransaction& transaction, TransactionState resolution);
protected:
std::shared_ptr<ClientImpl> self() override {
return shared_from_this();
}
void onOrphanedTransactionalMessage(MessageConstSharedPtr message) override;
void notifyClientTermination() override;
private:
absl::flat_hash_map<std::string, TopicPublishInfoPtr> topic_publish_info_table_ GUARDED_BY(topic_publish_info_mtx_);
absl::Mutex topic_publish_info_mtx_; // protects topic_publish_info_
std::size_t max_attempt_times_{MixAll::MAX_SEND_MESSAGE_ATTEMPT_TIMES_};
int32_t failed_times_{0}; // only for test
uint32_t compress_body_threshold_;
TransactionChecker transaction_checker_;
std::vector<std::string> topics_ GUARDED_BY(topics_mtx_);
absl::Mutex topics_mtx_;
PublishStats stats_;
/**
* @brief Acquire PublishInfo for the given topic.
* Generally speaking, it first checks presence of the desired info in local cache, aka, topic_publish_table_;
* If not found, query name servers.
*/
void getPublishInfoAsync(const std::string& topic, const PublishInfoCallback& cb)
LOCKS_EXCLUDED(topic_publish_info_mtx_);
void cachePublishInfo(const std::string&, TopicPublishInfoPtr info) LOCKS_EXCLUDED(topic_publish_info_mtx_);
TopicPublishInfoPtr getPublishInfo(const std::string& topic);
void wrapSendMessageRequest(const Message& message,
SendMessageRequest& request,
const rmq::MessageQueue& message_queue);
bool isRunning() const;
void ensureRunning(std::error_code& ec) const noexcept;
void validate(const Message& message, std::error_code& ec);
void send0(MessageConstPtr message, const SendCallback& callback, std::vector<rmq::MessageQueue> list);
void isolatedEndpoints(absl::flat_hash_set<std::string>& endpoints) LOCKS_EXCLUDED(isolated_endpoints_mtx_);
friend class ProducerBuilder;
};
ROCKETMQ_NAMESPACE_END