Search in sources :

Example 1 with ODistributedRedirectException

use of com.orientechnologies.orient.enterprise.channel.binary.ODistributedRedirectException in project orientdb by orientechnologies.

the class ODistributedStorage method checkForCluster.

protected String checkForCluster(final ORecord record, final String localNodeName, ODistributedConfiguration dbCfg) {
    if (!(record instanceof ODocument))
        return null;
    final ORecordId rid = (ORecordId) record.getIdentity();
    if (rid.getClusterId() < 0)
        throw new IllegalArgumentException("RID " + rid + " is not valid");
    String clusterName = getClusterNameByRID(rid);
    final String ownerServer = dbCfg.getClusterOwner(clusterName);
    if (ownerServer.equals(localNodeName))
        // NO CHANGES
        return null;
    final OCluster cl = getClusterByName(clusterName);
    final ODatabaseDocumentInternal db = ODatabaseRecordThreadLocal.INSTANCE.get();
    final OClass cls = db.getMetadata().getSchema().getClassByClusterId(cl.getId());
    String newClusterName = null;
    if (cls != null) {
        OClusterSelectionStrategy clSel = cls.getClusterSelection();
        if (!(clSel instanceof OLocalClusterWrapperStrategy)) {
            dManager.propagateSchemaChanges(db);
            clSel = cls.getClusterSelection();
        }
        if (!(clSel instanceof OLocalClusterWrapperStrategy))
            throw new ODistributedException("Cannot install local cluster strategy on class '" + cls.getName() + "'");
        dbCfg = ((OLocalClusterWrapperStrategy) clSel).readConfiguration();
        final String newOwnerNode = dbCfg.getClusterOwner(clusterName);
        if (newOwnerNode.equals(localNodeName))
            // NO CHANGES
            return null;
        // ONLY IF IT'S A CLIENT REQUEST (NON DISTRIBUTED) AND THE AVAILABLE SERVER IS ONLINE, REDIRECT THE REQUEST TO THAT SERVER
        if (!OScenarioThreadLocal.INSTANCE.isRunModeDistributed()) {
            if (dManager.isNodeAvailable(ownerServer, getName())) {
                final String ownerUUID = dManager.getNodeUuidByName(ownerServer);
                if (ownerUUID != null) {
                    final ODocument doc = dManager.getNodeConfigurationByUuid(ownerUUID, true);
                    if (doc != null) {
                        final String ownerServerIPAddress = ODistributedAbstractPlugin.getListeningBinaryAddress(doc);
                        OLogManager.instance().info(this, "Local node '" + localNodeName + "' is not the owner for cluster '" + clusterName + "' (it is '" + ownerServer + "'). Sending a redirect to the client to connect it directly to the owner server");
                        // FORCE THE REDIRECT AGAINST THE SERVER OWNER OF THE CLUSTER
                        throw new ODistributedRedirectException(getDistributedManager().getLocalNodeName(), ownerServer, ownerServerIPAddress, "Local node '" + localNodeName + "' is not the owner for cluster '" + clusterName + "' (it is '" + ownerServer + "')");
                    }
                }
            }
        }
        // FORCE THE RETRY OF THE OPERATION
        throw new ODistributedConfigurationChangedException("Local node '" + localNodeName + "' is not the owner for cluster '" + clusterName + "' (it is '" + ownerServer + "')");
    }
    if (!ownerServer.equals(localNodeName))
        throw new ODistributedException("Error on inserting into cluster '" + clusterName + "' where local node '" + localNodeName + "' is not the master of it, but it is '" + ownerServer + "'");
    // OVERWRITE CLUSTER
    clusterName = newClusterName;
    final ORecordId oldRID = rid.copy();
    rid.setClusterId(db.getClusterIdByName(newClusterName));
    OLogManager.instance().info(this, "Reassigned local cluster '%s' to the record %s. New RID is %s", newClusterName, oldRID, rid);
    return clusterName;
}
Also used : ODistributedRedirectException(com.orientechnologies.orient.enterprise.channel.binary.ODistributedRedirectException) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) OClusterSelectionStrategy(com.orientechnologies.orient.core.metadata.schema.clusterselection.OClusterSelectionStrategy) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 2 with ODistributedRedirectException

use of com.orientechnologies.orient.enterprise.channel.binary.ODistributedRedirectException 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)

Aggregations

ODistributedRedirectException (com.orientechnologies.orient.enterprise.channel.binary.ODistributedRedirectException)2 OOfflineNodeException (com.orientechnologies.common.concur.OOfflineNodeException)1 OInterruptedException (com.orientechnologies.common.concur.lock.OInterruptedException)1 OModificationOperationProhibitedException (com.orientechnologies.common.concur.lock.OModificationOperationProhibitedException)1 OException (com.orientechnologies.common.exception.OException)1 OIOException (com.orientechnologies.common.io.OIOException)1 OChannelBinaryAsynchClient (com.orientechnologies.orient.client.binary.OChannelBinaryAsynchClient)1 ORecordId (com.orientechnologies.orient.core.id.ORecordId)1 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)1 OClusterSelectionStrategy (com.orientechnologies.orient.core.metadata.schema.clusterselection.OClusterSelectionStrategy)1 OTokenException (com.orientechnologies.orient.core.metadata.security.OTokenException)1 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1 OTokenSecurityException (com.orientechnologies.orient.enterprise.channel.binary.OTokenSecurityException)1 IOException (java.io.IOException)1 NamingException (javax.naming.NamingException)1