Search in sources :

Example 1 with OClusterSelectionStrategy

use of com.orientechnologies.orient.core.metadata.schema.clusterselection.OClusterSelectionStrategy 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)

Aggregations

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 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)1 ODistributedRedirectException (com.orientechnologies.orient.enterprise.channel.binary.ODistributedRedirectException)1