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;
}
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);
}
Aggregations