-
Notifications
You must be signed in to change notification settings - Fork 866
/
ORemoteConnectionManager.java
executable file
·258 lines (213 loc) · 9.74 KB
/
ORemoteConnectionManager.java
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
/*
*
* * Copyright 2014 Orient Technologies LTD (info(at)orientechnologies.com)
* *
* * Licensed 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.
* *
* * For more information: http://www.orientechnologies.com
*
*/
package com.orientechnologies.orient.client.remote;
import com.orientechnologies.common.concur.resource.OResourcePool;
import com.orientechnologies.common.concur.resource.OResourcePoolListener;
import com.orientechnologies.common.exception.OException;
import com.orientechnologies.common.io.OIOException;
import com.orientechnologies.common.log.OLogManager;
import com.orientechnologies.orient.core.config.OContextConfiguration;
import com.orientechnologies.orient.core.config.OGlobalConfiguration;
import com.orientechnologies.orient.enterprise.channel.OChannel;
import com.orientechnologies.orient.enterprise.channel.binary.OChannelBinaryAsynchClient;
import com.orientechnologies.orient.enterprise.channel.binary.OChannelBinaryProtocol;
import com.orientechnologies.orient.enterprise.channel.binary.OChannelListener;
import com.orientechnologies.orient.enterprise.channel.binary.ORemoteServerEventListener;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
* Manages network connections against OrientDB servers. All the connection pools are managed in a Map<url,pool>, but in the future
* we could have a unique pool per sever and manage database connections over the protocol.
*
* @author Luca Garulli (l.garulli--at--orientechnologies.com)
*/
public class ORemoteConnectionManager implements OChannelListener {
public static final String PARAM_MAX_POOL = "maxpool";
protected final ConcurrentHashMap<String, OResourcePool<String, OChannelBinaryAsynchClient>> connections;
protected final long timeout;
public ORemoteConnectionManager(final int iMaxConnectionPerURL, final long iTimeout) {
connections = new ConcurrentHashMap<String, OResourcePool<String, OChannelBinaryAsynchClient>>();
timeout = iTimeout;
}
public void close() {
for (Map.Entry<String, OResourcePool<String, OChannelBinaryAsynchClient>> entry : connections.entrySet()) {
closePool(entry.getValue());
}
connections.clear();
}
public OChannelBinaryAsynchClient acquire(String iServerURL, final OContextConfiguration clientConfiguration,
final Map<String, Object> iConfiguration, final ORemoteServerEventListener iListener) {
OResourcePool<String, OChannelBinaryAsynchClient> pool = connections.get(iServerURL);
if (pool == null) {
int maxPool = OGlobalConfiguration.CLIENT_CHANNEL_MAX_POOL.getValueAsInteger();
if (iConfiguration != null && iConfiguration.size() > 0) {
if (iConfiguration.containsKey(PARAM_MAX_POOL))
maxPool = Integer.parseInt(iConfiguration.get(PARAM_MAX_POOL).toString());
}
pool = new OResourcePool<String, OChannelBinaryAsynchClient>(maxPool,
new OResourcePoolListener<String, OChannelBinaryAsynchClient>() {
@Override
public OChannelBinaryAsynchClient createNewResource(final String iKey, final Object... iAdditionalArgs) {
return createNetworkConnection(iKey, (OContextConfiguration) iAdditionalArgs[0],
(Map<String, Object>) iAdditionalArgs[1], (ORemoteServerEventListener) iAdditionalArgs[2]);
}
@Override
public boolean reuseResource(final String iKey, final Object[] iAdditionalArgs, final OChannelBinaryAsynchClient iValue) {
return iValue.isConnected();
}
});
final OResourcePool<String, OChannelBinaryAsynchClient> prev = connections.putIfAbsent(iServerURL, pool);
if (prev != null) {
// ALREADY PRESENT, DESTROY IT AND GET THE ALREADY EXISTENT OBJ
pool.close();
pool = prev;
}
}
try {
// RETURN THE RESOURCE
OChannelBinaryAsynchClient ret = pool.getResource(iServerURL, timeout, clientConfiguration, iConfiguration, iListener);
return ret;
} catch (RuntimeException e) {
// ERROR ON RETRIEVING THE INSTANCE FROM THE POOL
throw e;
} catch (Exception e) {
// ERROR ON RETRIEVING THE INSTANCE FROM THE POOL
OLogManager.instance().debug(this, "Error on retrieving the connection from pool: " + iServerURL, e);
}
return null;
}
public void release(final OChannelBinaryAsynchClient conn) {
final OResourcePool<String, OChannelBinaryAsynchClient> pool = connections.get(conn.getServerURL());
if (pool != null) {
if (!conn.isConnected()) {
OLogManager.instance().debug(this, "Network connection pool is receiving a closed connection to reuse: discard it");
remove(conn);
} else {
pool.returnResource(conn);
}
}
}
public void remove(final OChannelBinaryAsynchClient conn) {
try {
conn.unlock();
} catch (Exception e) {
OLogManager.instance().debug(this, "Cannot unlock connection lock", e);
}
try {
conn.close();
} catch (Exception e) {
OLogManager.instance().debug(this, "Cannot close connection", e);
}
final OResourcePool<String, OChannelBinaryAsynchClient> pool = connections.get(conn.getServerURL());
if (pool == null)
throw new IllegalStateException("Connection cannot be released because the pool doesn't exist anymore");
pool.remove(conn);
}
@Override
public void onChannelClose(final OChannel channel) {
OChannelBinaryAsynchClient conn = (OChannelBinaryAsynchClient) channel;
final OResourcePool<String, OChannelBinaryAsynchClient> pool = connections.get(conn.getServerURL());
if (pool == null)
throw new IllegalStateException("Connection cannot be released because the pool doesn't exist anymore");
pool.remove(conn);
}
public Set<String> getURLs() {
return connections.keySet();
}
public int getMaxResources(final String url) {
final OResourcePool<String, OChannelBinaryAsynchClient> pool = connections.get(url);
if (pool == null)
return 0;
return pool.getMaxResources();
}
public int getAvailableConnections(final String url) {
final OResourcePool<String, OChannelBinaryAsynchClient> pool = connections.get(url);
if (pool == null)
return 0;
return pool.getAvailableResources();
}
public int getReusableConnections(final String url) {
final OResourcePool<String, OChannelBinaryAsynchClient> pool = connections.get(url);
if (pool == null)
return 0;
return pool.getInPoolResources();
}
public int getCreatedInstancesInPool(final String url) {
final OResourcePool<String, OChannelBinaryAsynchClient> pool = connections.get(url);
if (pool == null)
return 0;
return pool.getCreatedInstances();
}
public void closePool(final String url) {
final OResourcePool<String, OChannelBinaryAsynchClient> pool = connections.remove(url);
if (pool == null)
return;
closePool(pool);
}
protected void closePool(final OResourcePool<String, OChannelBinaryAsynchClient> pool) {
final List<OChannelBinaryAsynchClient> conns = new ArrayList<OChannelBinaryAsynchClient>(pool.getAllResources());
for (OChannelBinaryAsynchClient c : conns)
try {
// Unregister the listener that make the connection return to the closing pool.
c.unregisterListener(this);
c.close();
} catch (Exception e) {
OLogManager.instance().debug(this, "Cannot close binary channel", e);
}
pool.close();
}
protected OChannelBinaryAsynchClient createNetworkConnection(String iServerURL, final OContextConfiguration clientConfiguration,
Map<String, Object> iAdditionalArg, final ORemoteServerEventListener asynchEventListener) throws OIOException {
if (iServerURL == null)
throw new IllegalArgumentException("server url is null");
// TRY WITH CURRENT URL IF ANY
try {
OLogManager.instance().debug(this, "Trying to connect to the remote host %s...", iServerURL);
final String serverURL;
final String databaseName;
int sepPos = iServerURL.indexOf("/");
if (sepPos > -1) {
// REMOVE DATABASE NAME IF ANY
serverURL = iServerURL.substring(0, sepPos);
databaseName = iServerURL.substring(sepPos + 1);
} else {
serverURL = iServerURL;
databaseName = null;
}
sepPos = serverURL.indexOf(":");
final String remoteHost = serverURL.substring(0, sepPos);
final int remotePort = Integer.parseInt(serverURL.substring(sepPos + 1));
final OChannelBinaryAsynchClient ch = new OChannelBinaryAsynchClient(remoteHost, remotePort, databaseName,
clientConfiguration, OChannelBinaryProtocol.CURRENT_PROTOCOL_VERSION, asynchEventListener);
// REGISTER MYSELF AS LISTENER TO REMOVE THE CHANNEL FROM THE POOL IN CASE OF CLOSING
ch.registerListener(this);
return ch;
} catch (OIOException e) {
// RE-THROW IT
throw e;
} catch (Exception e) {
OLogManager.instance().debug(this, "Error on connecting to %s", e, iServerURL);
throw OException.wrapException(new OIOException("Error on connecting to " + iServerURL), e);
}
}
}