Skip to content
Navigation Menu
{{ message }}
-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathConnectionPool.cpp
More file actions
312 lines (280 loc) · 9.92 KB
/
Copy pathConnectionPool.cpp
File metadata and controls
312 lines (280 loc) · 9.92 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/* This file is part of VoltDB.
* Copyright (C) 2008-2016 VoltDB Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*/
#include "ConnectionPool.h"
#include <cassert>
#include <exception>
#include <iostream>
#include <boost/scoped_ptr.hpp>
#include <cstdio>
#include "InvocationResponse.hpp"
#include "ClientConfig.h"
namespace voltdb {
/*
* Global connection pool instance that is intialized when the library is loaded and deleted on unload
*/
static ConnectionPool *gPool;
/*
* A delegating listener that forwards to another listener that can be NULL. It is also possible to change
* the listener being delegated to on the fly.
*/
class DelegatingStatusListener : public StatusListener {
public:
StatusListener *m_listener;
bool m_connectionLost;
DelegatingStatusListener() : m_listener(NULL), m_connectionLost(false) {}
bool uncaughtException(
std::exception exception,
boost::shared_ptr<voltdb::ProcedureCallback> callback,
InvocationResponse response) {
if (m_listener != NULL) {
return m_listener->uncaughtException(exception, callback, response);
}
return false;
}
bool connectionLost(std::string hostname, int32_t connectionsLeft) {
m_connectionLost = true;
if (m_listener != NULL) {
bool retval = m_listener->connectionLost(hostname, connectionsLeft);
return retval;
}
return false;
}
bool connectionActive(std::string hostname, int32_t connectionsActive) {
if (m_listener != NULL) {
bool retval = m_listener->connectionActive(hostname, connectionsActive);
return retval;
} else {
return false;
}
}
bool backpressure(bool hasBackpressure) {
if (m_listener != NULL) {
return m_listener->backpressure(hasBackpressure);
}
return false;
}
};
/*
* A record for information associated with a client such as the delegating listener
* and identifier. This can be used to return the client to the ClientMap after a script exits.
*/
class ClientStuff {
public:
ClientStuff(
voltdb::Client client,
std::string identifier,
DelegatingStatusListener *listener) :
m_identifier(identifier), m_listener(listener), m_client(client)
{}
std::string m_identifier;
DelegatingStatusListener *m_listener;
voltdb::Client m_client;
};
/*
* Short hand for an exception safe lock acquisition
*/
class LockGuard {
public:
LockGuard(pthread_mutex_t &mutex) {
m_mutex = &mutex;
pthread_mutex_lock(m_mutex);
}
~LockGuard() {
pthread_mutex_unlock(m_mutex);
}
private:
pthread_mutex_t *m_mutex;
};
/*
* Cleanup function used by thread local ptr to a list of clients
* that were borrowed from the pool. It unsets the listener and returns the client
* to the pool.
*/
void cleanupOnScriptEnd(void *ptr) {
if (gPool != NULL) {
LockGuard guard(gPool->m_lock);
ClientSet *clients = reinterpret_cast<ClientSet*>(ptr);
if (clients != NULL) {
boost::scoped_ptr<ClientSet> guard(clients);
for(ClientSet::iterator i = clients->begin(); i != clients->end(); i++) {
(*i)->m_listener->m_listener = NULL;
gPool->m_clients[(*i)->m_identifier].push_back(*i);
}
pthread_setspecific(gPool->m_borrowedClients, NULL);
}
}
}
ConnectionPool::ConnectionPool() {
pthread_mutex_init(&m_lock, NULL);
pthread_key_create(&m_borrowedClients, &cleanupOnScriptEnd);
}
/*
* Destructor specifically deletes the thread local pointer because the cleanup method
* needs acess to the members of ConnectionPool.
*/
ConnectionPool::~ConnectionPool() {
pthread_mutex_destroy(&m_lock);
pthread_key_delete(m_borrowedClients);
}
/*
* Retrieve a client that is connected and authenticated
* to the specified hostname and port. Will reuse an existing connection if one is available.
*/
voltdb::Client
ConnectionPool::acquireClient(
std::string hostname,
std::string username,
std::string password,
StatusListener *listener,
unsigned short port,
ClientAuthHashScheme sha)
throw (voltdb::Exception, voltdb::ConnectException, voltdb::LibEventException) {
LockGuard guard(m_lock);
ClientSet *clients = reinterpret_cast<ClientSet*>(pthread_getspecific(m_borrowedClients));
if (clients == NULL) {
clients = new ClientSet();
pthread_setspecific( m_borrowedClients, static_cast<const void *>(clients));
}
char portBytes[16];
unsigned int portInt = port;
snprintf(portBytes, 16, "%d", portInt);
std::string identifier = hostname + "," + std::string(portBytes) + "," + username + "," + password;
// if a thread calls acquireClient() multiple times with the same identifier, reuse the same client
for (ClientSet::iterator i = clients->begin(); i != clients->end(); i++) {
if ((*i)->m_identifier == identifier) {
return (*i)->m_client;
}
}
std::vector<boost::shared_ptr<ClientStuff> > *clientStuffs = &m_clients[identifier];
while (clientStuffs->size() > 0) {
boost::shared_ptr<ClientStuff> clientStuff = clientStuffs->back();
clientStuffs->pop_back();
// run the event loop once to verify the connection is still available
clientStuff->m_client.runOnce();
if (clientStuff->m_listener->m_connectionLost) {
// if this connection is lost, try the next
continue;
} else {
// otherwise return this connection
clientStuff->m_listener->m_listener = listener;
clients->push_back(clientStuff);
return clientStuff->m_client;
}
}
// no connection available, make a new one
DelegatingStatusListener *delegatingListener = new DelegatingStatusListener();
Client client = voltdb::Client::create(ClientConfig( username, password, delegatingListener, sha));
client.createConnection(hostname, port);
boost::shared_ptr<ClientStuff> stuff(new ClientStuff(client, identifier, delegatingListener));
stuff->m_listener->m_listener = listener;
clients->push_back(stuff);
return client;
}
voltdb::Client
ConnectionPool::acquireClient(
std::string hostname,
std::string username,
std::string password,
unsigned short port,
ClientAuthHashScheme sha)
throw (voltdb::Exception, voltdb::ConnectException, voltdb::LibEventException) {
return acquireClient(hostname, username, password, NULL, port, sha);
}
void ConnectionPool::returnClient(Client client) throw (voltdb::Exception) {
LockGuard guard(m_lock);
ClientSet *clients = reinterpret_cast<ClientSet*>(pthread_getspecific(m_borrowedClients));
if (clients == NULL) {
throw MisplacedClientException();
}
for (ClientSet::iterator i = clients->begin(); i != clients->end(); i++) {
if ((*i)->m_client == client) {
(*i)->m_listener->m_listener = NULL;
m_clients[(*i)->m_identifier].push_back(*i);
clients->erase(i);
return;
}
}
throw MisplacedClientException();
}
void ConnectionPool::closeClientConnection(Client client) throw (voltdb::Exception) {
LockGuard guard(m_lock);
ClientSet *clients = reinterpret_cast<ClientSet*>(pthread_getspecific(m_borrowedClients));
if (clients == NULL) {
//No clients closing a stale object or not owned by this thread.
return;
}
for (ClientSet::iterator i = clients->begin(); i != clients->end(); i++) {
if ((*i)->m_client == client) {
client.close();
(*i)->m_listener->m_listener = NULL;
clients->erase(i);
return;
}
}
}
/*
* Return the number of clients held by this thread
*/
int ConnectionPool::numClientsBorrowed() {
ClientSet *clients = reinterpret_cast<ClientSet*>(pthread_getspecific(m_borrowedClients));
if (clients != NULL) {
return clients->size();
}
return 0;
}
/*
* Release any unreleased clients associated with this thread/script
*/
void ConnectionPool::onScriptEnd() {
cleanupOnScriptEnd(pthread_getspecific(m_borrowedClients));
}
/*
* Invoked by the external language/script environment when the library is loaded.
* Creates the connection pool
*/
void onLoad() {
assert(gPool == NULL);
gPool = new ConnectionPool();
}
/*
* Invoked by the external language/script environment when the library is unloaded.
* Deletes the connection pool closing all connections. Does not drain connections.
*/
void onUnload() {
assert(gPool != NULL);
delete gPool;
gPool = NULL;
}
/*
* Invoked by the external language/script environment when a script ends. Causes
* all connections acquired by this thread to be returned.
*/
void onScriptEnd() {
assert(gPool != NULL);
gPool->onScriptEnd();
}
voltdb::ConnectionPool* ConnectionPool::pool() {
assert(gPool != NULL);
return gPool;
}
}
You can’t perform that action at this time.
