Search in sources :

Example 11 with OChannelBinaryAsynchClient

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);
    }
}
Also used : OChannelBinaryAsynchClient(com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient) OIOException(com.orientechnologies.common.io.OIOException) OIOException(com.orientechnologies.common.io.OIOException) OException(com.orientechnologies.common.exception.OException)

Example 12 with OChannelBinaryAsynchClient

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);
    }
}
Also used : OStorageException(com.orientechnologies.orient.core.exception.OStorageException) OException(com.orientechnologies.common.exception.OException) IOException(java.io.IOException) OChannelBinaryAsynchClient(com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient) OException(com.orientechnologies.common.exception.OException) IOException(java.io.IOException) OStorageException(com.orientechnologies.orient.core.exception.OStorageException) OModificationOperationProhibitedException(com.orientechnologies.common.concur.lock.OModificationOperationProhibitedException)

Example 13 with OChannelBinaryAsynchClient

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);
}
Also used : OOfflineNodeException(com.orientechnologies.common.concur.OOfflineNodeException) OException(com.orientechnologies.common.exception.OException) OIOException(com.orientechnologies.common.io.OIOException) IOException(java.io.IOException) OIOException(com.orientechnologies.common.io.OIOException) IOException(java.io.IOException) OChannelBinaryAsynchClient(com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient) OIOException(com.orientechnologies.common.io.OIOException) OException(com.orientechnologies.common.exception.OException) NamingException(javax.naming.NamingException) OTokenException(com.orientechnologies.orient.core.metadata.security.OTokenException) ODistributedRedirectException(com.orientechnologies.orient.enterprise.channel.binary.ODistributedRedirectException) OInterruptedException(com.orientechnologies.common.concur.lock.OInterruptedException) OTokenSecurityException(com.orientechnologies.orient.enterprise.channel.binary.OTokenSecurityException) OIOException(com.orientechnologies.common.io.OIOException) IOException(java.io.IOException) OOfflineNodeException(com.orientechnologies.common.concur.OOfflineNodeException) OModificationOperationProhibitedException(com.orientechnologies.common.concur.lock.OModificationOperationProhibitedException) OTokenSecurityException(com.orientechnologies.orient.enterprise.channel.binary.OTokenSecurityException) OTokenException(com.orientechnologies.orient.core.metadata.security.OTokenException) ODistributedRedirectException(com.orientechnologies.orient.enterprise.channel.binary.ODistributedRedirectException) OModificationOperationProhibitedException(com.orientechnologies.common.concur.lock.OModificationOperationProhibitedException)

Example 14 with OChannelBinaryAsynchClient

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();
}
Also used : ArrayList(java.util.ArrayList) OChannelBinaryAsynchClient(com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient)

Example 15 with OChannelBinaryAsynchClient

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;
}
Also used : OChannelBinaryAsynchClient(com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient)

Aggregations

OChannelBinaryAsynchClient (com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient)21 IOException (java.io.IOException)12 OIOException (com.orientechnologies.common.io.OIOException)9 OModificationOperationProhibitedException (com.orientechnologies.common.concur.lock.OModificationOperationProhibitedException)6 OException (com.orientechnologies.common.exception.OException)6 OOfflineNodeException (com.orientechnologies.common.concur.OOfflineNodeException)4 OInterruptedException (com.orientechnologies.common.concur.lock.OInterruptedException)4 ORecordId (com.orientechnologies.orient.core.id.ORecordId)4 OTokenException (com.orientechnologies.orient.core.metadata.security.OTokenException)4 ODistributedRedirectException (com.orientechnologies.orient.enterprise.channel.binary.ODistributedRedirectException)4 OTokenSecurityException (com.orientechnologies.orient.enterprise.channel.binary.OTokenSecurityException)4 NamingException (javax.naming.NamingException)4 Test (org.testng.annotations.Test)4 Test (org.junit.Test)3 OSBTreeCollectionManager (com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeCollectionManager)2 OChannelListener (com.orientechnologies.orient.enterprise.channel.binary.OChannelListener)2 HashSet (java.util.HashSet)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 OCommandRequestAsynch (com.orientechnologies.orient.core.command.OCommandRequestAsynch)1 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)1