use of com.orientechnologies.orient.core.exception.ODatabaseException in project orientdb by orientechnologies.
the class OTransactionOptimistic method addRecord.
public void addRecord(final ORecord iRecord, final byte iStatus, final String iClusterName) {
checkTransaction();
if (iStatus != ORecordOperation.LOADED)
changedDocuments.remove(iRecord);
try {
switch(iStatus) {
case ORecordOperation.CREATED:
{
database.checkSecurity(ORule.ResourceGeneric.CLUSTER, ORole.PERMISSION_CREATE, iClusterName);
RESULT res = database.callbackHooks(TYPE.BEFORE_CREATE, iRecord);
if (res == RESULT.RECORD_CHANGED && iRecord instanceof ODocument)
((ODocument) iRecord).validate();
}
break;
case ORecordOperation.LOADED:
/**
* Read hooks already invoked in {@link com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx#executeReadRecord}
*/
break;
case ORecordOperation.UPDATED:
{
database.checkSecurity(ORule.ResourceGeneric.CLUSTER, ORole.PERMISSION_UPDATE, iClusterName);
RESULT res = database.callbackHooks(TYPE.BEFORE_UPDATE, iRecord);
if (res == RESULT.RECORD_CHANGED && iRecord instanceof ODocument)
((ODocument) iRecord).validate();
}
break;
case ORecordOperation.DELETED:
database.checkSecurity(ORule.ResourceGeneric.CLUSTER, ORole.PERMISSION_DELETE, iClusterName);
database.callbackHooks(TYPE.BEFORE_DELETE, iRecord);
break;
}
try {
final ORecordId rid = (ORecordId) iRecord.getIdentity();
if (!rid.isValid()) {
ORecordInternal.onBeforeIdentityChanged(iRecord);
database.assignAndCheckCluster(iRecord, iClusterName);
rid.setClusterPosition(newObjectCounter--);
ORecordInternal.onAfterIdentityChanged(iRecord);
}
ORecordOperation txEntry = getRecordEntry(rid);
if (txEntry == null) {
if (!(rid.isTemporary() && iStatus != ORecordOperation.CREATED)) {
// NEW ENTRY: JUST REGISTER IT
txEntry = new ORecordOperation(iRecord, iStatus);
allEntries.put(rid.copy(), txEntry);
}
} else {
// UPDATE PREVIOUS STATUS
txEntry.record = iRecord;
switch(txEntry.type) {
case ORecordOperation.LOADED:
switch(iStatus) {
case ORecordOperation.UPDATED:
txEntry.type = ORecordOperation.UPDATED;
break;
case ORecordOperation.DELETED:
txEntry.type = ORecordOperation.DELETED;
break;
}
break;
case ORecordOperation.UPDATED:
switch(iStatus) {
case ORecordOperation.DELETED:
txEntry.type = ORecordOperation.DELETED;
break;
}
break;
case ORecordOperation.DELETED:
break;
case ORecordOperation.CREATED:
switch(iStatus) {
case ORecordOperation.DELETED:
allEntries.remove(rid);
// txEntry.type = ORecordOperation.DELETED;
break;
}
break;
}
}
switch(iStatus) {
case ORecordOperation.CREATED:
database.callbackHooks(TYPE.AFTER_CREATE, iRecord);
break;
case ORecordOperation.LOADED:
/**
* Read hooks already invoked in
* {@link com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx#executeReadRecord} .
*/
break;
case ORecordOperation.UPDATED:
database.callbackHooks(TYPE.AFTER_UPDATE, iRecord);
break;
case ORecordOperation.DELETED:
database.callbackHooks(TYPE.AFTER_DELETE, iRecord);
break;
}
// RESET TRACKING
if (iRecord instanceof ODocument && ((ODocument) iRecord).isTrackingChanges()) {
ODocumentInternal.clearTrackData(((ODocument) iRecord));
}
} catch (Exception e) {
switch(iStatus) {
case ORecordOperation.CREATED:
database.callbackHooks(TYPE.CREATE_FAILED, iRecord);
break;
case ORecordOperation.UPDATED:
database.callbackHooks(TYPE.UPDATE_FAILED, iRecord);
break;
case ORecordOperation.DELETED:
database.callbackHooks(TYPE.DELETE_FAILED, iRecord);
break;
}
throw OException.wrapException(new ODatabaseException("Error on saving record " + iRecord.getIdentity()), e);
}
} finally {
switch(iStatus) {
case ORecordOperation.CREATED:
database.callbackHooks(TYPE.FINALIZE_CREATION, iRecord);
break;
case ORecordOperation.UPDATED:
database.callbackHooks(TYPE.FINALIZE_UPDATE, iRecord);
break;
case ORecordOperation.DELETED:
database.callbackHooks(TYPE.FINALIZE_DELETION, iRecord);
break;
}
}
}
use of com.orientechnologies.orient.core.exception.ODatabaseException in project orientdb by orientechnologies.
the class TestExceptionNotOpen method testExceptionNotOpenRemote.
@Test
public void testExceptionNotOpenRemote() {
try {
ODatabaseDocument db = new ODatabaseDocumentTx("remote:127.0.0.1:00");
Assert.fail();
} catch (ODatabaseException e) {
Assert.assertNull(ODatabaseRecordThreadLocal.INSTANCE.getIfDefined());
}
}
use of com.orientechnologies.orient.core.exception.ODatabaseException in project orientdb by orientechnologies.
the class ODistributedAbstractPlugin method getListeningBinaryAddress.
public static String getListeningBinaryAddress(final ODocument cfg) {
String url = cfg.field("publicAddress");
final Collection<Map<String, Object>> listeners = (Collection<Map<String, Object>>) cfg.field("listeners");
if (listeners == null)
throw new ODatabaseException("Cannot connect to a remote node because bad distributed configuration: missing 'listeners' array field");
String listenUrl = null;
for (Map<String, Object> listener : listeners) {
if ((listener.get("protocol")).equals("ONetworkProtocolBinary")) {
listenUrl = (String) listener.get("listen");
break;
}
}
if (url == null)
url = listenUrl;
else {
int pos;
String port;
if ((pos = listenUrl.lastIndexOf(":")) != -1) {
port = listenUrl.substring(pos + 1);
} else {
port = "2424";
}
url += ":" + port;
}
return url;
}
use of com.orientechnologies.orient.core.exception.ODatabaseException in project orientdb by orientechnologies.
the class OLocalClusterWrapperStrategy method readConfiguration.
protected ODistributedConfiguration readConfiguration() {
if (cls.isAbstract())
throw new IllegalArgumentException("Cannot create a new instance of abstract class");
final ODatabaseDocument db = ODatabaseRecordThreadLocal.INSTANCE.get();
final int[] clusterIds = cls.getClusterIds();
final List<String> clusterNames = new ArrayList<String>(clusterIds.length);
for (int c : clusterIds) clusterNames.add(db.getClusterNameById(c).toLowerCase());
ODistributedConfiguration cfg = manager.getDatabaseConfiguration(databaseName);
List<String> bestClusters = cfg.getOwnedClustersByServer(clusterNames, nodeName);
if (bestClusters.isEmpty()) {
// REBALANCE THE CLUSTERS
manager.reassignClustersOwnership(nodeName, databaseName, cfg.modify());
cfg = manager.getDatabaseConfiguration(databaseName);
bestClusters = cfg.getOwnedClustersByServer(clusterNames, nodeName);
if (bestClusters.isEmpty()) {
// FILL THE MAP CLUSTER/SERVERS
final StringBuilder buffer = new StringBuilder();
for (String c : clusterNames) {
if (buffer.length() > 0)
buffer.append(" ");
buffer.append(" ");
buffer.append(c);
buffer.append(":");
buffer.append(cfg.getServers(c, null));
}
ODistributedServerLog.warn(this, manager.getLocalNodeName(), null, ODistributedServerLog.DIRECTION.NONE, "Cannot find best cluster for class '%s'. Configured servers for clusters %s are %s (dCfgVersion=%d)", cls.getName(), clusterNames, buffer.toString(), cfg.getVersion());
throw new ODatabaseException("Cannot find best cluster for class '" + cls.getName() + "' on server '" + nodeName + "'. ClusterStrategy=" + getName() + " dCfgVersion=" + cfg.getVersion());
}
}
db.activateOnCurrentThread();
final int[] newBestClusters = new int[bestClusters.size()];
int i = 0;
for (String c : bestClusters) newBestClusters[i++] = db.getClusterIdByName(c);
this.localScopedClass = new OLocalScopedClass((OClassImpl) cls, newBestClusters);
final ODistributedStorage storage = (ODistributedStorage) manager.getStorage(databaseName);
lastVersion = storage.getConfigurationUpdated();
return cfg;
}
use of com.orientechnologies.orient.core.exception.ODatabaseException in project orientdb by orientechnologies.
the class DistributedDbDropAndReCreateTest method onAfterExecution.
@Override
protected void onAfterExecution() throws Exception {
int s = 0;
do {
ServerRun server = serverInstance.get(0);
ODatabaseDocumentTx db = new ODatabaseDocumentTx(getDatabaseURL(server));
db.open("admin", "admin");
banner("DROPPING DATABASE ON SERVERS");
waitForDatabaseIsOnline(0, "europe-0", getDatabaseName(), 5000);
waitForDatabaseIsOnline(0, "europe-1", getDatabaseName(), 5000);
waitForDatabaseIsOnline(0, "europe-2", getDatabaseName(), 5000);
db.drop();
Thread.sleep(2000);
Assert.assertFalse(server.getServerInstance().getDistributedManager().getConfigurationMap().containsKey(OHazelcastPlugin.CONFIG_DATABASE_PREFIX + getDatabaseName()));
server = serverInstance.get(s);
banner("RE-CREATING DATABASE ON SERVER " + server.getServerId());
db = new ODatabaseDocumentTx(getDatabaseURL(server));
Assert.assertFalse(server.getServerInstance().getDistributedManager().getConfigurationMap().containsKey(OHazelcastPlugin.CONFIG_DATABASE_PREFIX + getDatabaseName()));
for (int retry = 0; retry < 10; retry++) {
try {
db.create();
break;
} catch (ODatabaseException e) {
System.out.println("DB STILL IN THE CLUSTER, WAIT AND RETRY (retry " + retry + ")...");
Thread.sleep(1000);
}
}
db.activateOnCurrentThread();
db.close();
} while (++s < serverInstance.size());
// DROP LAST DATABASE
final ODatabaseDocumentTx db = new ODatabaseDocumentTx(getDatabaseURL(serverInstance.get(serverInstance.size() - 1)));
db.open("admin", "admin");
db.drop();
}
Aggregations