Search in sources :

Example 1 with OChannelBinaryAsynchClient

use of com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient in project orientdb by orientechnologies.

the class OStorageRemote method getNetwork.

public OChannelBinaryAsynchClient getNetwork(final String iCurrentURL) {
    OChannelBinaryAsynchClient network;
    do {
        try {
            network = connectionManager.acquire(iCurrentURL, clientConfiguration, connectionOptions, asynchEventListener);
        } catch (OIOException cause) {
            throw cause;
        } catch (Exception cause) {
            throw OException.wrapException(new OStorageException("Cannot open a connection to remote server: " + iCurrentURL), cause);
        }
        if (!network.tryLock()) {
            // CANNOT LOCK IT, MAYBE HASN'T BE CORRECTLY UNLOCKED BY PREVIOUS USER?
            OLogManager.instance().error(this, "Removing locked network channel '%s' (connected=%s)...", iCurrentURL, network.isConnected());
            connectionManager.remove(network);
            network = null;
        }
    } while (network == null);
    return network;
}
Also used : 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)

Example 2 with OChannelBinaryAsynchClient

use of com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient in project orientdb by orientechnologies.

the class OStorageRemote method openRemoteDatabase.

protected String openRemoteDatabase(String currentURL) {
    do {
        do {
            OChannelBinaryAsynchClient network = null;
            try {
                network = getNetwork(currentURL);
                openRemoteDatabase(network);
                connectionManager.release(network);
                return currentURL;
            } catch (OIOException e) {
                if (network != null) {
                    // REMOVE THE NETWORK CONNECTION IF ANY
                    connectionManager.remove(network);
                }
                OLogManager.instance().debug(this, "Cannot open database with url " + currentURL, e);
            } catch (OException e) {
                connectionManager.release(network);
                // PROPAGATE ANY OTHER ORIENTDB EXCEPTION
                throw e;
            } catch (Exception e) {
                if (network != null) {
                    // REMOVE THE NETWORK CONNECTION IF ANY
                    try {
                        connectionManager.remove(network);
                    } catch (Exception ex) {
                        // IGNORE ANY EXCEPTION
                        OLogManager.instance().debug(this, "Cannot remove connection or database url=" + currentURL, e);
                    }
                }
                OLogManager.instance().error(this, "Cannot open database url=" + currentURL, e);
            }
        } while (connectionManager.getReusableConnections(currentURL) > 0);
        currentURL = useNewServerURL(currentURL);
    } while (currentURL != null);
    // REFILL ORIGINAL SERVER LIST
    parseServerURLs();
    synchronized (serverURLs) {
        throw new OStorageException("Cannot create a connection to remote server address(es): " + serverURLs);
    }
}
Also used : OException(com.orientechnologies.common.exception.OException) 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)

Example 3 with OChannelBinaryAsynchClient

use of com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient in project orientdb by orientechnologies.

the class OStorageRemote method createRecord.

public OStorageOperationResult<OPhysicalPosition> createRecord(final ORecordId iRid, final byte[] iContent, final int iRecordVersion, final byte iRecordType, final int iMode, final ORecordCallback<Long> iCallback) {
    final OSBTreeCollectionManager collectionManager = ODatabaseRecordThreadLocal.INSTANCE.get().getSbTreeCollectionManager();
    ORecordCallback<OPhysicalPosition> realCallback = null;
    if (iCallback != null) {
        realCallback = new ORecordCallback<OPhysicalPosition>() {

            @Override
            public void call(ORecordId iRID, OPhysicalPosition iParameter) {
                iCallback.call(iRID, iParameter.clusterPosition);
            }
        };
    }
    final ORecordId idCopy = iRid.copy();
    // The Upper layer require to return this also if it not really receive response from the network
    final OPhysicalPosition ppos = new OPhysicalPosition(iRecordType);
    asyncNetworkOperation(new OStorageRemoteOperationWrite() {

        @Override
        public void execute(final OChannelBinaryAsynchClient network, final OStorageRemoteSession session, int mode) throws IOException {
            try {
                beginRequest(network, OChannelBinaryProtocol.REQUEST_RECORD_CREATE, session);
                network.writeShort((short) iRid.getClusterId());
                network.writeBytes(iContent);
                network.writeByte(iRecordType);
                network.writeByte((byte) mode);
            } finally {
                endRequest(network);
            }
        }
    }, new OStorageRemoteOperationRead<OPhysicalPosition>() {

        @Override
        public OPhysicalPosition execute(OChannelBinaryAsynchClient network, OStorageRemoteSession session) throws IOException {
            // SYNCHRONOUS
            try {
                beginResponse(network, session);
                // FIRST READ THE ENTIRE RESPONSE
                short clusterId = network.readShort();
                final long clPos = network.readLong();
                final int recVer = network.readVersion();
                final Map<OBonsaiCollectionPointer, OPair<Long, Long>> collectionChanges = readCollectionChanges(network);
                // APPLY CHANGES
                ppos.clusterPosition = clPos;
                ppos.recordVersion = recVer;
                // THIS IS A COMPATIBILITY FIX TO AVOID TO FILL THE CLUSTER ID IN CASE OF ASYNC
                if (iMode == 0) {
                    iRid.setClusterId(clusterId);
                    iRid.setClusterPosition(ppos.clusterPosition);
                }
                idCopy.setClusterId(clusterId);
                idCopy.setClusterPosition(ppos.clusterPosition);
                updateCollection(collectionChanges, collectionManager);
                return ppos;
            } finally {
                endResponse(network);
            }
        }
    }, iMode, idCopy, realCallback, "Error on create record in cluster " + iRid.getClusterId());
    return new OStorageOperationResult<OPhysicalPosition>(ppos);
}
Also used : OIOException(com.orientechnologies.common.io.OIOException) IOException(java.io.IOException) OChannelBinaryAsynchClient(com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient) ORecordId(com.orientechnologies.orient.core.id.ORecordId) OSBTreeCollectionManager(com.orientechnologies.orient.core.db.record.ridbag.sbtree.OSBTreeCollectionManager)

Example 4 with OChannelBinaryAsynchClient

use of com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient in project orientdb by orientechnologies.

the class OStorageRemote method close.

public void close(final boolean iForce, boolean onDelete) {
    if (status == STATUS.CLOSED)
        return;
    stateLock.acquireWriteLock();
    try {
        if (status == STATUS.CLOSED)
            return;
        final OStorageRemoteSession session = getCurrentSession();
        if (session != null) {
            final Collection<OStorageRemoteNodeSession> nodes = session.getAllServerSessions();
            if (!nodes.isEmpty()) {
                for (OStorageRemoteNodeSession nodeSession : nodes) {
                    OChannelBinaryAsynchClient network = null;
                    try {
                        network = getNetwork(nodeSession.getServerURL());
                        network.beginRequest(OChannelBinaryProtocol.REQUEST_DB_CLOSE, session);
                        endRequest(network);
                        connectionManager.release(network);
                    } catch (OIOException ex) {
                        // IGNORING IF THE SERVER IS DOWN OR NOT REACHABLE THE SESSION IS AUTOMATICALLY CLOSED.
                        OLogManager.instance().debug(this, "Impossible to comunicate to the server for close: %s", ex);
                        connectionManager.remove(network);
                    } catch (IOException ex) {
                        // IGNORING IF THE SERVER IS DOWN OR NOT REACHABLE THE SESSION IS AUTOMATICALLY CLOSED.
                        OLogManager.instance().debug(this, "Impossible to comunicate to the server for close: %s", ex);
                        connectionManager.remove(network);
                    }
                }
                session.close();
                sessions.remove(session);
                if (!checkForClose(iForce))
                    return;
            } else {
                if (!iForce)
                    return;
            }
        }
        status = STATUS.CLOSING;
        // CLOSE ALL THE CONNECTIONS
        for (String url : serverURLs) {
            connectionManager.closePool(url);
        }
        sbTreeCollectionManager.close();
        super.close(iForce, onDelete);
        status = STATUS.CLOSED;
        Orient.instance().unregisterStorage(this);
    } finally {
        stateLock.releaseWriteLock();
    }
}
Also used : OIOException(com.orientechnologies.common.io.OIOException) IOException(java.io.IOException) OChannelBinaryAsynchClient(com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient) OIOException(com.orientechnologies.common.io.OIOException)

Example 5 with OChannelBinaryAsynchClient

use of com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient in project orientdb by orientechnologies.

the class OStorageRemote method reopenRemoteDatabase.

protected String reopenRemoteDatabase() throws IOException {
    String currentURL = getCurrentServerURL();
    do {
        do {
            final OChannelBinaryAsynchClient network = getNetwork(currentURL);
            try {
                OStorageRemoteSession session = getCurrentSession();
                OStorageRemoteNodeSession nodeSession = session.getOrCreateServerSession(network.getServerURL());
                if (nodeSession == null || !nodeSession.isValid()) {
                    openRemoteDatabase(network);
                    connectionManager.release(network);
                    return network.getServerURL();
                } else {
                    try {
                        network.writeByte(OChannelBinaryProtocol.REQUEST_DB_REOPEN);
                        network.writeInt(nodeSession.getSessionId());
                        network.writeBytes(nodeSession.getToken());
                    } finally {
                        endRequest(network);
                    }
                    final int sessionId;
                    try {
                        byte[] newToken = network.beginResponse(nodeSession.getSessionId(), true);
                        sessionId = network.readInt();
                        if (newToken != null && newToken.length > 0) {
                            nodeSession.setSession(sessionId, newToken);
                        } else {
                            nodeSession.setSession(sessionId, nodeSession.getToken());
                        }
                        OLogManager.instance().debug(this, "Client connected to %s with session id=%d", network.getServerURL(), sessionId);
                        return currentURL;
                    } finally {
                        endResponse(network);
                        connectionManager.release(network);
                    }
                }
            } catch (OIOException e) {
                if (network != null) {
                    // REMOVE THE NETWORK CONNECTION IF ANY
                    connectionManager.remove(network);
                }
                OLogManager.instance().error(this, "Cannot open database with url " + currentURL, e);
            } catch (OOfflineNodeException e) {
                if (network != null) {
                    // REMOVE THE NETWORK CONNECTION IF ANY
                    connectionManager.remove(network);
                }
                OLogManager.instance().debug(this, "Cannot open database with url " + currentURL, e);
            } catch (OSecurityException ex) {
                OLogManager.instance().debug(this, "Invalidate token for url=%s", ex, currentURL);
                OStorageRemoteSession session = getCurrentSession();
                session.removeServerSession(currentURL);
                if (network != null) {
                    // REMOVE THE NETWORK CONNECTION IF ANY
                    try {
                        connectionManager.remove(network);
                    } catch (Exception e) {
                        // IGNORE ANY EXCEPTION
                        OLogManager.instance().debug(this, "Cannot remove connection or database url=" + currentURL, e);
                    }
                }
            } catch (OException e) {
                connectionManager.release(network);
                // PROPAGATE ANY OTHER ORIENTDB EXCEPTION
                throw e;
            } catch (Exception e) {
                OLogManager.instance().debug(this, "Cannot open database with url " + currentURL, e);
                if (network != null) {
                    // REMOVE THE NETWORK CONNECTION IF ANY
                    try {
                        connectionManager.remove(network);
                    } catch (Exception ex) {
                        // IGNORE ANY EXCEPTION
                        OLogManager.instance().debug(this, "Cannot remove connection or database url=" + currentURL, e);
                    }
                }
            }
        } while (connectionManager.getAvailableConnections(currentURL) > 0);
        currentURL = useNewServerURL(currentURL);
    } while (currentURL != null);
    // REFILL ORIGINAL SERVER LIST
    parseServerURLs();
    synchronized (serverURLs) {
        throw new OStorageException("Cannot create a connection to remote server address(es): " + serverURLs);
    }
}
Also used : OOfflineNodeException(com.orientechnologies.common.concur.OOfflineNodeException) OException(com.orientechnologies.common.exception.OException) 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)

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