use of com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient in project orientdb by orientechnologies.
the class ORemoteConnectionPool method createNetworkConnection.
protected OChannelBinaryAsynchClient createNetworkConnection(String iServerURL, final OContextConfiguration clientConfiguration, Map<String, Object> iAdditionalArg) 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;
if (iServerURL.startsWith(OEngineRemote.PREFIX))
iServerURL = iServerURL.substring(OEngineRemote.PREFIX.length());
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, listener);
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);
}
}
use of com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient in project orientdb by orientechnologies.
the class OServerAdmin method networkAdminOperation.
protected <T> T networkAdminOperation(final OStorageRemoteOperation<T> operation, final String errorMessage) {
OChannelBinaryAsynchClient network = null;
try {
//TODO:replace this api with one that get connection for only the specified url.
String serverUrl = storage.getNextAvailableServerURL(false, session);
do {
try {
network = storage.getNetwork(serverUrl);
} catch (OException e) {
serverUrl = storage.useNewServerURL(serverUrl);
if (serverUrl == null)
throw e;
}
} while (network == null);
T res = operation.execute(network, storage.getCurrentSession());
storage.connectionManager.release(network);
return res;
} catch (Exception e) {
if (network != null)
storage.connectionManager.release(network);
storage.close(true, false);
throw OException.wrapException(new OStorageException(errorMessage), e);
}
}
use of com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient in project orientdb by orientechnologies.
the class OStorageRemote method baseNetworkOperation.
public <T> T baseNetworkOperation(final OStorageRemoteOperation<T> operation, final String errorMessage, int retry) {
OStorageRemoteSession session = getCurrentSession();
if (session.commandExecuting)
throw new ODatabaseException("Cannot execute the request because an asynchronous operation is in progress. Please use a different connection");
String serverUrl = null;
do {
OChannelBinaryAsynchClient network = null;
if (serverUrl == null)
serverUrl = getNextAvailableServerURL(false, session);
do {
try {
network = getNetwork(serverUrl);
} catch (OException e) {
serverUrl = useNewServerURL(serverUrl);
if (serverUrl == null)
throw e;
}
} while (network == null);
try {
// In case i do not have a token or i'm switching between server i've to execute a open operation.
OStorageRemoteNodeSession nodeSession = session.getServerSession(network.getServerURL());
if (nodeSession == null || !nodeSession.isValid()) {
openRemoteDatabase(network);
if (!network.tryLock()) {
connectionManager.release(network);
continue;
}
}
return operation.execute(network, session);
} catch (ODistributedRedirectException e) {
connectionManager.release(network);
OLogManager.instance().debug(this, "Redirecting the request from server '%s' to the server '%s' because %s", e.getFromServer(), e.toString(), e.getMessage());
// RECONNECT TO THE SERVER SUGGESTED IN THE EXCEPTION
serverUrl = e.getToServerAddress();
} catch (OModificationOperationProhibitedException mope) {
connectionManager.release(network);
handleDBFreeze();
serverUrl = null;
} catch (OTokenException e) {
connectionManager.release(network);
session.removeServerSession(network.getServerURL());
if (--retry <= 0)
throw OException.wrapException(new OStorageException(errorMessage), e);
serverUrl = null;
} catch (OTokenSecurityException e) {
connectionManager.release(network);
session.removeServerSession(network.getServerURL());
if (--retry <= 0)
throw OException.wrapException(new OStorageException(errorMessage), e);
serverUrl = null;
} catch (OOfflineNodeException e) {
connectionManager.release(network);
// Remove the current url because the node is offline
synchronized (serverURLs) {
serverURLs.remove(serverUrl);
}
for (OStorageRemoteSession activeSession : sessions) {
// Not thread Safe ...
activeSession.removeServerSession(serverUrl);
}
serverUrl = null;
} catch (IOException e) {
connectionManager.release(network);
retry = handleIOException(retry, network, e);
serverUrl = null;
} catch (OIOException e) {
connectionManager.release(network);
retry = handleIOException(retry, network, e);
serverUrl = null;
} catch (OException e) {
connectionManager.release(network);
throw e;
} catch (Exception e) {
connectionManager.release(network);
throw OException.wrapException(new OStorageException(errorMessage), e);
}
} while (true);
}
use of com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient in project orientdb by orientechnologies.
the class ORemoteConnectionManager method closePool.
protected void closePool(ORemoteConnectionPool pool) {
final List<OChannelBinaryAsynchClient> conns = new ArrayList<OChannelBinaryAsynchClient>(pool.getPool().getAllResources());
for (OChannelBinaryAsynchClient c : conns) try {
// Unregister the listener that make the connection return to the closing pool.
c.close();
} catch (Exception e) {
OLogManager.instance().debug(this, "Cannot close binary channel", e);
}
pool.getPool().close();
}
use of com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient in project orientdb by orientechnologies.
the class ORemoteConnectionManager method acquire.
public OChannelBinaryAsynchClient acquire(String iServerURL, final OContextConfiguration clientConfiguration, final Map<String, Object> iConfiguration, final OStorageRemoteAsynchEventListener iListener) {
if (iServerURL.startsWith(OEngineRemote.PREFIX))
iServerURL = iServerURL.substring(OEngineRemote.PREFIX.length());
if (iServerURL.endsWith("/"))
iServerURL = iServerURL.substring(0, iServerURL.length() - 1);
long localTimeout = timeout;
ORemoteConnectionPool 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());
if (iConfiguration.containsKey(PARAM_MAX_POOL))
maxPool = Integer.parseInt(iConfiguration.get(PARAM_MAX_POOL).toString());
}
if (clientConfiguration != null) {
final Object max = clientConfiguration.getValue(OGlobalConfiguration.CLIENT_CHANNEL_MAX_POOL);
if (max != null)
maxPool = Integer.parseInt(max.toString());
final Object netLockTimeout = clientConfiguration.getValue(OGlobalConfiguration.NETWORK_LOCK_TIMEOUT);
if (netLockTimeout != null)
localTimeout = Integer.parseInt(netLockTimeout.toString());
}
pool = new ORemoteConnectionPool(maxPool, iListener != null);
final ORemoteConnectionPool prev = connections.putIfAbsent(iServerURL, pool);
if (prev != null) {
// ALREADY PRESENT, DESTROY IT AND GET THE ALREADY EXISTENT OBJ
pool.getPool().close();
pool = prev;
}
}
try {
// RETURN THE RESOURCE
OChannelBinaryAsynchClient ret = pool.acquire(iServerURL, localTimeout, 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;
}
Aggregations