forked from apache/pulsar-client-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathClientImpl.h
More file actions
197 lines (145 loc) · 7.48 KB
/
Copy pathClientImpl.h
File metadata and controls
197 lines (145 loc) · 7.48 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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
/**
* 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.
*/
#ifndef LIB_CLIENTIMPL_H_
#define LIB_CLIENTIMPL_H_
#include <pulsar/Client.h>
#include <atomic>
#include <memory>
#include "ConnectionPool.h"
#include "Future.h"
#include "LookupDataResult.h"
#include "MemoryLimitController.h"
#include "ProtoApiEnums.h"
#include "ServiceNameResolver.h"
#include "SynchronizedHashMap.h"
namespace pulsar {
class PulsarFriend;
class ClientImpl;
typedef std::shared_ptr<ClientImpl> ClientImplPtr;
typedef std::weak_ptr<ClientImpl> ClientImplWeakPtr;
class ReaderImpl;
typedef std::shared_ptr<ReaderImpl> ReaderImplPtr;
typedef std::weak_ptr<ReaderImpl> ReaderImplWeakPtr;
class TableViewImpl;
typedef std::shared_ptr<TableViewImpl> TableViewImplPtr;
class ConsumerImplBase;
typedef std::weak_ptr<ConsumerImplBase> ConsumerImplBaseWeakPtr;
class ClientConnection;
using ClientConnectionPtr = std::shared_ptr<ClientConnection>;
class LookupService;
using LookupServicePtr = std::shared_ptr<LookupService>;
class ProducerImplBase;
using ProducerImplBaseWeakPtr = std::weak_ptr<ProducerImplBase>;
class ConsumerImplBase;
using ConsumerImplBaseWeakPtr = std::weak_ptr<ConsumerImplBase>;
class TopicName;
using TopicNamePtr = std::shared_ptr<TopicName>;
using NamespaceTopicsPtr = std::shared_ptr<std::vector<std::string>>;
std::string generateRandomName();
class ClientImpl : public std::enable_shared_from_this<ClientImpl> {
public:
ClientImpl(const std::string& serviceUrl, const ClientConfiguration& clientConfiguration);
~ClientImpl();
/**
* @param autoDownloadSchema When it is true, Before creating a producer, it will try to get the schema
* that exists for the topic.
*/
void createProducerAsync(const std::string& topic, ProducerConfiguration conf,
CreateProducerCallback callback, bool autoDownloadSchema = false);
void subscribeAsync(const std::string& topic, const std::string& subscriptionName,
const ConsumerConfiguration& conf, SubscribeCallback callback);
void subscribeAsync(const std::vector<std::string>& topics, const std::string& subscriptionName,
const ConsumerConfiguration& conf, SubscribeCallback callback);
void subscribeWithRegexAsync(const std::string& regexPattern, const std::string& subscriptionName,
const ConsumerConfiguration& conf, SubscribeCallback callback);
void createReaderAsync(const std::string& topic, const MessageId& startMessageId,
const ReaderConfiguration& conf, ReaderCallback callback);
void createTableViewAsync(const std::string& topic, const TableViewConfiguration& conf,
TableViewCallback callback);
void getPartitionsForTopicAsync(const std::string& topic, GetPartitionsCallback callback);
Future<Result, ClientConnectionPtr> getConnection(const std::string& topic, size_t key);
void closeAsync(CloseCallback callback);
void shutdown();
MemoryLimitController& getMemoryLimitController();
uint64_t newProducerId();
uint64_t newConsumerId();
uint64_t newRequestId();
uint64_t getNumberOfProducers();
uint64_t getNumberOfConsumers();
const ClientConfiguration& getClientConfig() const;
const ClientConfiguration& conf() const;
ExecutorServiceProviderPtr getIOExecutorProvider();
ExecutorServiceProviderPtr getListenerExecutorProvider();
ExecutorServiceProviderPtr getPartitionListenerExecutorProvider();
LookupServicePtr getLookup();
void cleanupProducer(ProducerImplBase* address) { producers_.remove(address); }
void cleanupConsumer(ConsumerImplBase* address) { consumers_.remove(address); }
std::shared_ptr<std::atomic<uint64_t>> getRequestIdGenerator() const { return requestIdGenerator_; }
ConnectionPool& getConnectionPool() noexcept { return pool_; }
friend class PulsarFriend;
private:
void handleCreateProducer(const Result result, const LookupDataResultPtr partitionMetadata,
TopicNamePtr topicName, ProducerConfiguration conf,
CreateProducerCallback callback);
void handleSubscribe(const Result result, const LookupDataResultPtr partitionMetadata,
TopicNamePtr topicName, const std::string& consumerName, ConsumerConfiguration conf,
SubscribeCallback callback);
void handleReaderMetadataLookup(const Result result, const LookupDataResultPtr partitionMetadata,
TopicNamePtr topicName, MessageId startMessageId,
ReaderConfiguration conf, ReaderCallback callback);
void handleGetPartitions(const Result result, const LookupDataResultPtr partitionMetadata,
TopicNamePtr topicName, GetPartitionsCallback callback);
void handleProducerCreated(Result result, ProducerImplBaseWeakPtr producerWeakPtr,
CreateProducerCallback callback, ProducerImplBasePtr producer);
void handleConsumerCreated(Result result, ConsumerImplBaseWeakPtr consumerWeakPtr,
SubscribeCallback callback, ConsumerImplBasePtr consumer);
typedef std::shared_ptr<int> SharedInt;
void handleClose(Result result, SharedInt remaining, ResultCallback callback);
void createPatternMultiTopicsConsumer(const Result result, const NamespaceTopicsPtr topics,
const std::string& regexPattern,
CommandGetTopicsOfNamespace_Mode mode,
const std::string& consumerName, const ConsumerConfiguration& conf,
SubscribeCallback callback);
static std::string getClientVersion(const ClientConfiguration& clientConfiguration);
enum State
{
Open,
Closing,
Closed
};
std::mutex mutex_;
State state_;
ServiceNameResolver serviceNameResolver_;
ClientConfiguration clientConfiguration_;
MemoryLimitController memoryLimitController_;
ExecutorServiceProviderPtr ioExecutorProvider_;
ExecutorServiceProviderPtr listenerExecutorProvider_;
ExecutorServiceProviderPtr partitionListenerExecutorProvider_;
LookupServicePtr lookupServicePtr_;
ConnectionPool pool_;
uint64_t producerIdGenerator_;
uint64_t consumerIdGenerator_;
std::shared_ptr<std::atomic<uint64_t>> requestIdGenerator_{std::make_shared<std::atomic<uint64_t>>(0)};
SynchronizedHashMap<ProducerImplBase*, ProducerImplBaseWeakPtr> producers_;
SynchronizedHashMap<ConsumerImplBase*, ConsumerImplBaseWeakPtr> consumers_;
std::atomic<Result> closingError;
friend class Client;
};
} /* namespace pulsar */
#endif /* LIB_CLIENTIMPL_H_ */