Search in sources :

Example 21 with ODatabaseException

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;
        }
    }
}
Also used : ORecordOperation(com.orientechnologies.orient.core.db.record.ORecordOperation) ODatabaseException(com.orientechnologies.orient.core.exception.ODatabaseException) RESULT(com.orientechnologies.orient.core.hook.ORecordHook.RESULT) ORecordId(com.orientechnologies.orient.core.id.ORecordId) OException(com.orientechnologies.common.exception.OException) OStorageException(com.orientechnologies.orient.core.exception.OStorageException) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException) OTransactionException(com.orientechnologies.orient.core.exception.OTransactionException) ODatabaseException(com.orientechnologies.orient.core.exception.ODatabaseException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 22 with ODatabaseException

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());
    }
}
Also used : ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODatabaseException(com.orientechnologies.orient.core.exception.ODatabaseException) Test(org.testng.annotations.Test)

Example 23 with ODatabaseException

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;
}
Also used : ODatabaseException(com.orientechnologies.orient.core.exception.ODatabaseException) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap) OClusterPositionMap(com.orientechnologies.orient.core.storage.impl.local.paginated.OClusterPositionMap)

Example 24 with ODatabaseException

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;
}
Also used : ArrayList(java.util.ArrayList) OClassImpl(com.orientechnologies.orient.core.metadata.schema.OClassImpl) ODatabaseException(com.orientechnologies.orient.core.exception.ODatabaseException) ODatabaseDocument(com.orientechnologies.orient.core.db.document.ODatabaseDocument) ODistributedConfiguration(com.orientechnologies.orient.server.distributed.ODistributedConfiguration)

Example 25 with ODatabaseException

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();
}
Also used : ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ODatabaseException(com.orientechnologies.orient.core.exception.ODatabaseException)

Aggregations

ODatabaseException (com.orientechnologies.orient.core.exception.ODatabaseException)28 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)7 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)7 OException (com.orientechnologies.common.exception.OException)5 ORecordId (com.orientechnologies.orient.core.id.ORecordId)5 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)5 ORecordNotFoundException (com.orientechnologies.orient.core.exception.ORecordNotFoundException)4 IOException (java.io.IOException)4 ONeedRetryException (com.orientechnologies.common.concur.ONeedRetryException)3 ODatabaseDocumentInternal (com.orientechnologies.orient.core.db.ODatabaseDocumentInternal)3 OCommandExecutionException (com.orientechnologies.orient.core.exception.OCommandExecutionException)3 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)3 OStorage (com.orientechnologies.orient.core.storage.OStorage)3 OStorageException (com.orientechnologies.orient.core.exception.OStorageException)2 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)2 ORecord (com.orientechnologies.orient.core.record.ORecord)2 OLocalClusterWrapperStrategy (com.orientechnologies.orient.server.distributed.impl.OLocalClusterWrapperStrategy)2 ArrayList (java.util.ArrayList)2 Test (org.testng.annotations.Test)2 OMultiCollectionIterator (com.orientechnologies.common.collection.OMultiCollectionIterator)1