|
| 1 | +/* |
| 2 | + * Copyright 2010-2012 Luca Garulli (l.garulli--at--orientechnologies.com) |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.orientechnologies.orient.client.remote; |
| 17 | + |
| 18 | +import com.orientechnologies.common.concur.resource.OResourcePool; |
| 19 | +import com.orientechnologies.common.concur.resource.OResourcePoolListener; |
| 20 | +import com.orientechnologies.common.io.OIOException; |
| 21 | +import com.orientechnologies.common.log.OLogManager; |
| 22 | +import com.orientechnologies.orient.core.config.OContextConfiguration; |
| 23 | +import com.orientechnologies.orient.core.config.OGlobalConfiguration; |
| 24 | +import com.orientechnologies.orient.enterprise.channel.OChannel; |
| 25 | +import com.orientechnologies.orient.enterprise.channel.binary.OChannelBinaryAsynchClient; |
| 26 | +import com.orientechnologies.orient.enterprise.channel.binary.OChannelBinaryProtocol; |
| 27 | +import com.orientechnologies.orient.enterprise.channel.binary.OChannelListener; |
| 28 | +import com.orientechnologies.orient.enterprise.channel.binary.ORemoteServerEventListener; |
| 29 | + |
| 30 | +import java.util.ArrayList; |
| 31 | +import java.util.List; |
| 32 | +import java.util.Map; |
| 33 | +import java.util.concurrent.ConcurrentHashMap; |
| 34 | + |
| 35 | +/** |
| 36 | + * Manages network connections against OrientDB servers. All the connection pools are managed in a Map<url,pool>, but in the future |
| 37 | + * we could have a unique pool per sever and manage database connections over the protocol. |
| 38 | + * |
| 39 | + * @author Luca Garulli (l.garulli--at--orientechnologies.com) |
| 40 | + */ |
| 41 | +public class ORemoteConnectionManager implements OChannelListener { |
| 42 | + public static final String PARAM_MAX_POOL = "maxpool"; |
| 43 | + |
| 44 | + protected final ConcurrentHashMap<String, OResourcePool<String, OChannelBinaryAsynchClient>> connections; |
| 45 | + protected final long timeout; |
| 46 | + |
| 47 | + public ORemoteConnectionManager(final int iMaxConnectionPerURL, final long iTimeout) { |
| 48 | + connections = new ConcurrentHashMap<String, OResourcePool<String, OChannelBinaryAsynchClient>>(); |
| 49 | + timeout = iTimeout; |
| 50 | + } |
| 51 | + |
| 52 | + public void close() { |
| 53 | + for (Map.Entry<String, OResourcePool<String, OChannelBinaryAsynchClient>> entry : connections.entrySet()) |
| 54 | + entry.getValue().close(); |
| 55 | + |
| 56 | + connections.clear(); |
| 57 | + } |
| 58 | + |
| 59 | + public OChannelBinaryAsynchClient acquire(final String iServerURL, final OContextConfiguration clientConfiguration, |
| 60 | + final Map<String, Object> iConfiguration, final ORemoteServerEventListener iListener) { |
| 61 | + OResourcePool<String, OChannelBinaryAsynchClient> pool = connections.get(iServerURL); |
| 62 | + if (pool == null) { |
| 63 | + int maxPool = OGlobalConfiguration.CLIENT_CHANNEL_MAX_POOL.getValueAsInteger(); |
| 64 | + |
| 65 | + if (iConfiguration != null && iConfiguration.size() > 0) { |
| 66 | + if (iConfiguration.containsKey(PARAM_MAX_POOL)) |
| 67 | + maxPool = Integer.parseInt(iConfiguration.get(PARAM_MAX_POOL).toString()); |
| 68 | + } |
| 69 | + |
| 70 | + pool = new OResourcePool<String, OChannelBinaryAsynchClient>(maxPool, |
| 71 | + new OResourcePoolListener<String, OChannelBinaryAsynchClient>() { |
| 72 | + @Override |
| 73 | + public OChannelBinaryAsynchClient createNewResource(final String iKey, final Object... iAdditionalArgs) { |
| 74 | + return createNetworkConnection(iKey, (OContextConfiguration) iAdditionalArgs[0], |
| 75 | + (Map<String, Object>) iAdditionalArgs[1], (ORemoteServerEventListener) iAdditionalArgs[2]); |
| 76 | + } |
| 77 | + |
| 78 | + @Override |
| 79 | + public boolean reuseResource(final String iKey, final Object[] iAdditionalArgs, final OChannelBinaryAsynchClient iValue) { |
| 80 | + return true; |
| 81 | + } |
| 82 | + }); |
| 83 | + |
| 84 | + final OResourcePool<String, OChannelBinaryAsynchClient> prev = connections.putIfAbsent(iServerURL, pool); |
| 85 | + if (prev != null) { |
| 86 | + // ALREADY PRESENT, DESTROY IT AND GET THE ALREADY EXISTENT OBJ |
| 87 | + pool.close(); |
| 88 | + pool = prev; |
| 89 | + } |
| 90 | + } |
| 91 | + |
| 92 | + return pool.getResource(iServerURL, timeout, clientConfiguration, iConfiguration, iListener); |
| 93 | + } |
| 94 | + |
| 95 | + public void release(final OChannelBinaryAsynchClient conn) { |
| 96 | + final OResourcePool<String, OChannelBinaryAsynchClient> pool = connections.get(conn.getServerURL()); |
| 97 | + if (pool != null) { |
| 98 | + if (!conn.isConnected()) { |
| 99 | + OLogManager.instance().debug(this, "Network connection pool is receiving a closed connection to reuse: discard it"); |
| 100 | + pool.remove(conn); |
| 101 | + } else |
| 102 | + pool.returnResource(conn); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + public void remove(final OChannelBinaryAsynchClient conn) { |
| 107 | + if (conn.isConnected()) { |
| 108 | + try { |
| 109 | + conn.unlock(); |
| 110 | + } catch (Exception e) { |
| 111 | + } |
| 112 | + |
| 113 | + try { |
| 114 | + conn.close(); |
| 115 | + } catch (Exception e) { |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + final OResourcePool<String, OChannelBinaryAsynchClient> pool = connections.get(conn.getServerURL()); |
| 120 | + if (pool == null) |
| 121 | + throw new IllegalStateException("Connection cannot be released because the pool doesn't exist anymore"); |
| 122 | + |
| 123 | + pool.remove(conn); |
| 124 | + } |
| 125 | + |
| 126 | + @Override |
| 127 | + public void onChannelClose(final OChannel channel) { |
| 128 | + remove((OChannelBinaryAsynchClient) channel); |
| 129 | + } |
| 130 | + |
| 131 | + public int getMaxResources(final String url) { |
| 132 | + final OResourcePool<String, OChannelBinaryAsynchClient> pool = connections.get(url); |
| 133 | + if (pool == null) |
| 134 | + return 0; |
| 135 | + |
| 136 | + return pool.getMaxResources(); |
| 137 | + } |
| 138 | + |
| 139 | + public int getAvailableConnections(final String url) { |
| 140 | + final OResourcePool<String, OChannelBinaryAsynchClient> pool = connections.get(url); |
| 141 | + if (pool == null) |
| 142 | + return 0; |
| 143 | + |
| 144 | + return pool.getAvailableResources(); |
| 145 | + } |
| 146 | + |
| 147 | + public void closePool(final String url) { |
| 148 | + final OResourcePool<String, OChannelBinaryAsynchClient> pool = connections.remove(url); |
| 149 | + if (pool == null) |
| 150 | + return; |
| 151 | + |
| 152 | + closePool(pool); |
| 153 | + } |
| 154 | + |
| 155 | + protected void closePool(final OResourcePool<String, OChannelBinaryAsynchClient> pool) { |
| 156 | + final List<OChannelBinaryAsynchClient> conns = new ArrayList<OChannelBinaryAsynchClient>(pool.getResources()); |
| 157 | + for (OChannelBinaryAsynchClient c : conns) |
| 158 | + try { |
| 159 | + c.close(); |
| 160 | + } catch (Exception e) { |
| 161 | + } |
| 162 | + } |
| 163 | + |
| 164 | + protected OChannelBinaryAsynchClient createNetworkConnection(String iServerURL, final OContextConfiguration clientConfiguration, |
| 165 | + Map<String, Object> iAdditionalArg, final ORemoteServerEventListener asynchEventListener) throws OIOException { |
| 166 | + if (iServerURL == null) |
| 167 | + throw new IllegalArgumentException("server url is null"); |
| 168 | + |
| 169 | + // TRY WITH CURRENT URL IF ANY |
| 170 | + try { |
| 171 | + OLogManager.instance().debug(this, "Trying to connect to the remote host %s...", iServerURL); |
| 172 | + |
| 173 | + final String serverURL; |
| 174 | + final String databaseName; |
| 175 | + int sepPos = iServerURL.indexOf("/"); |
| 176 | + if (sepPos > -1) { |
| 177 | + // REMOVE DATABASE NAME IF ANY |
| 178 | + serverURL = iServerURL.substring(0, sepPos); |
| 179 | + databaseName = iServerURL.substring(sepPos + 1); |
| 180 | + } else { |
| 181 | + serverURL = iServerURL; |
| 182 | + databaseName = null; |
| 183 | + } |
| 184 | + |
| 185 | + sepPos = serverURL.indexOf(":"); |
| 186 | + final String remoteHost = serverURL.substring(0, sepPos); |
| 187 | + final int remotePort = Integer.parseInt(serverURL.substring(sepPos + 1)); |
| 188 | + |
| 189 | + final OChannelBinaryAsynchClient ch = new OChannelBinaryAsynchClient(remoteHost, remotePort, databaseName, |
| 190 | + clientConfiguration, OChannelBinaryProtocol.CURRENT_PROTOCOL_VERSION, asynchEventListener); |
| 191 <
85AD
/td> | + |
| 192 | + // REGISTER MYSELF AS LISTENER TO REMOVE THE CHANNEL FROM THE POOL IN CASE OF CLOSING |
| 193 | + ch.registerListener(this); |
| 194 | + |
| 195 | + return ch; |
| 196 | + |
| 197 | + } catch (OIOException e) { |
| 198 | + // RE-THROW IT |
| 199 | + throw e; |
| 200 | + } catch (Exception e) { |
| 201 | + OLogManager.instance().debug(this, "Error on connecting to %s", e, iServerURL); |
| 202 | + throw new OIOException("Error on connecting to " + iServerURL, e); |
| 203 | + } |
| 204 | + } |
| 205 | +} |
0 commit comments