Search in sources :

Example 1 with ORecordNotFoundException

use of com.orientechnologies.orient.core.exception.ORecordNotFoundException in project orientdb by orientechnologies.

the class SimulateOperationsAgainstServer method updateDocument.

protected void updateDocument(final int threadId, final int iCycle, final String dbUrl, final String className, final int iSkip) {
    final ODatabaseDocumentTx db = getDatabase(dbUrl);
    for (int retry = 0; retry < MAX_RETRY; ++retry) {
        ODocument doc = null;
        try {
            List<OIdentifiable> result = db.query(new OSQLSynchQuery<Object>("select from " + className + " skip " + iSkip + " limit 1"));
            if (result == null || result.isEmpty())
                log(threadId, iCycle, dbUrl, " update no item " + iSkip + " because out of range");
            else {
                doc = (ODocument) result.get(0);
                doc.field("updated", "" + (doc.getVersion() + 1));
                doc.save();
                log(threadId, iCycle, dbUrl, " updated item " + iSkip + " RID=" + result.get(0));
            }
            // OK
            break;
        } catch (OConcurrentModificationException e) {
            log(threadId, iCycle, dbUrl, " concurrent update against record " + doc + ", reload it and retry " + retry + "/" + MAX_RETRY + "...");
            if (doc != null)
                doc.reload(null, true);
        } catch (ORecordNotFoundException e) {
            log(threadId, iCycle, dbUrl, " update no item " + iSkip + " because not found");
            break;
        } finally {
            db.close();
        }
    }
}
Also used : ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException) OConcurrentModificationException(com.orientechnologies.orient.core.exception.OConcurrentModificationException) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 2 with ORecordNotFoundException

use of com.orientechnologies.orient.core.exception.ORecordNotFoundException in project orientdb by orientechnologies.

the class DistributedConfigReloadTest method startCreateDeleteVertex.

private Runnable startCreateDeleteVertex(final int id, final OrientGraphFactory graphFactory, final String className) {
    Runnable th = new Runnable() {

        @Override
        public void run() {
            log("Starting runnable to create index " + className);
            long st = System.currentTimeMillis();
            OrientGraph graph = graphFactory.getTx();
            boolean isSelectSuccessful = true;
            try {
                boolean isRunning = true;
                for (int j = 0; j < 1000000; j++) {
                    isSelectSuccessful = true;
                    String sql = "SELECT FROM " + className;
                    int deleteErrorCounter = 0;
                    try {
                        graph.command(new OCommandSQL(sql)).execute();
                        Iterable<Vertex> vtxs = graph.command(new OCommandSQL(sql)).execute();
                        for (Vertex vtx : vtxs) {
                            boolean needRetry = true;
                            for (int i = 0; i < 10 && needRetry; i++) {
                                try {
                                    vtx.remove();
                                    graph.commit();
                                    needRetry = false;
                                } catch (ONeedRetryException ex) {
                                    try {
                                        ((OrientElement) vtx).reload();
                                    } catch (ORecordNotFoundException e) {
                                        // BY LUCA
                                        log("[" + id + "] Caught [" + e + "] during reload because the record was already deleted, no errors just go ahead");
                                    }
                                } catch (ORecordNotFoundException e) {
                                    // BY LUCA
                                    log("[" + id + "] Caught [" + e + "] because the record was already deleted, no errors just go ahead");
                                } catch (Exception ex) {
                                    log("[" + j + "] Failed to delete vertex  [" + className + "] Vertex: [" + vtx + "] Property 1: [" + vtx.getProperty("property1") + "] Error: [" + ex + "]");
                                    deleteErrorCounter++;
                                    needRetry = false;
                                }
                            }
                        }
                        log(" [" + id + "] Delete vertex : [" + j + "] [" + className + "]");
                    } catch (Exception ex) {
                        log("***************** [" + id + "] Failed to select vertex  [" + className + "][" + ex + "]");
                        isSelectSuccessful = false;
                    }
                    if (isSelectSuccessful) {
                        graph.command(new OCommandSQL(sql)).execute();
                        Iterable<Vertex> vtxs = graph.command(new OCommandSQL(sql)).execute();
                        ArrayList<String> vtxList = new ArrayList();
                        for (Vertex vtx : vtxs) {
                            vtxList.add(vtx.toString());
                        }
                        if (vtxList.size() > 0) {
                            log("########## [" + id + "] Records present after delete vertex  [" + className + "] Error on delete [" + deleteErrorCounter + "] List: " + vtxList + "");
                        } else {
                            log("########## [" + id + "] Records removed after delete vertex  [" + className + "] Error on delete [" + deleteErrorCounter + "]");
                        }
                        boolean showException = true;
                        int counter = 0;
                        for (int i = 1; i < 2000 && isRunning; i++) {
                            if ((i % 2000) == 0) {
                                long et = System.currentTimeMillis();
                                log(" [" + id + "] Total Records Processed: [" + i + "] Time taken for [2000] records: [" + (et - st) / 2000 + "] seconds");
                                st = System.currentTimeMillis();
                            }
                            Vertex vertex = graph.addVertex("class:" + className);
                            try {
                                vertex.setProperty("property1", "value-" + id + "-" + i);
                                vertex.setProperty("property2", "value2-" + (System.currentTimeMillis() + "-" + i));
                                vertex.setProperty("property3", "value3-" + i);
                                vertex.setProperty("property4", "value4-1");
                                vertex.setProperty("prop-6", "value6-" + i);
                                vertex.setProperty("prop-7", "value7-" + i);
                                vertex.setProperty("prop-8", "value7-1");
                                vertex.setProperty("prop-9", "value7-1");
                                vertex.setProperty("prop-10", "value7-1");
                                vertex.setProperty("prop11", "value7-1");
                                vertex.setProperty("prop12", "value7-1");
                                vertex.setProperty("prop13", "value7-1");
                                vertex.setProperty("prop14", System.currentTimeMillis());
                                vertex.setProperty("prop15", System.currentTimeMillis());
                                graph.commit();
                            } catch (ONeedRetryException ex) {
                                if (ex instanceof ONeedRetryException || ex.getCause() instanceof ONeedRetryException) {
                                    log("[" + id + "] OrientDB Retry Exception [" + ex + "]");
                                } else {
                                    log("[" + id + "] OrientDB Exception [" + ex + "]");
                                }
                                if (!(ex instanceof ODistributedConfigurationChangedException || ex.getCause() instanceof ODistributedConfigurationChangedException)) {
                                // reloadVertex(vertex, ex);
                                } else {
                                    log("[" + id + "] ODistributedConfigurationChangedException {} while updating vertex " + vertex);
                                }
                            } catch (ORecordDuplicatedException ex) {
                                // BY LUCA
                                log("[" + id + "] Caught [" + ex + "], no errors just go ahead");
                            } catch (ODistributedException ex) {
                                if (ex.getCause() instanceof ONeedRetryException) {
                                    log("[" + id + "] OrientDB Retry Exception [" + ex + "]");
                                } else {
                                    log("[" + id + "] OrientDB Exception [" + ex + "]");
                                }
                                if (!(ex.getCause() instanceof ODistributedConfigurationChangedException)) {
                                // reloadVertex(vertex, ex);
                                } else {
                                    log("[" + id + "] ODistributedConfigurationChangedException {} while updating vertex " + vertex);
                                }
                            } catch (Exception ex) {
                                if (showException) {
                                    log("[" + id + "] Failed to create record Exception [" + ex + "]");
                                    showException = false;
                                    counter++;
                                }
                            }
                        }
                        log("************ [" + id + "] Total number of errors: [" + counter + "] Delete error counter:[" + deleteErrorCounter + "]");
                    } else {
                        log("#####################  [" + id + "] Select failed. Skipping create..");
                    }
                }
            } finally {
                graph.shutdown();
            }
        }
    };
    return th;
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) ArrayList(java.util.ArrayList) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException) ORecordDuplicatedException(com.orientechnologies.orient.core.storage.ORecordDuplicatedException) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException) OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException) ORecordDuplicatedException(com.orientechnologies.orient.core.storage.ORecordDuplicatedException) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException)

Example 3 with ORecordNotFoundException

use of com.orientechnologies.orient.core.exception.ORecordNotFoundException in project orientdb by orientechnologies.

the class OCommandExecutorScript method executeSQLScript.

protected Object executeSQLScript(final String iText, final ODatabaseDocument db) throws IOException {
    Object lastResult = null;
    int maxRetry = 1;
    context.setVariable("transactionRetries", 0);
    context.setVariable("parentQuery", this);
    for (int retry = 1; retry <= maxRetry; retry++) {
        try {
            try {
                int txBegunAtLine = -1;
                int txBegunAtPart = -1;
                lastResult = null;
                int nestedLevel = 0;
                int skippingScriptsAtNestedLevel = -1;
                final BufferedReader reader = new BufferedReader(new StringReader(iText));
                int line = 0;
                int linePart = 0;
                String lastLine;
                boolean txBegun = false;
                for (; line < txBegunAtLine; ++line) // SKIP PREVIOUS COMMAND AND JUMP TO THE BEGIN IF ANY
                reader.readLine();
                for (; (lastLine = reader.readLine()) != null; ++line) {
                    lastLine = lastLine.trim();
                    // this block is here (and not below, with the other conditions)
                    // just because of the smartSprit() that does not parse correctly a single bracket
                    // final List<String> lineParts = OStringSerializerHelper.smartSplit(lastLine, ';', true);
                    final List<String> lineParts = splitBySemicolon(lastLine);
                    if (line == txBegunAtLine)
                        // SKIP PREVIOUS COMMAND PART AND JUMP TO THE BEGIN IF ANY
                        linePart = txBegunAtPart;
                    else
                        linePart = 0;
                    boolean breakReturn = false;
                    for (; linePart < lineParts.size(); ++linePart) {
                        final String lastCommand = lineParts.get(linePart);
                        if (isIfCondition(lastCommand)) {
                            nestedLevel++;
                            if (skippingScriptsAtNestedLevel >= 0) {
                                // I'm in an (outer) IF that did not match the condition
                                continue;
                            }
                            boolean ifResult = evaluateIfCondition(lastCommand);
                            if (!ifResult) {
                                // if does not match the condition, skip all the inner statements
                                skippingScriptsAtNestedLevel = nestedLevel;
                            }
                            continue;
                        } else if (lastCommand.equals("}")) {
                            nestedLevel--;
                            if (skippingScriptsAtNestedLevel > nestedLevel) {
                                skippingScriptsAtNestedLevel = -1;
                            }
                            continue;
                        } else if (skippingScriptsAtNestedLevel >= 0) {
                            // I'm in an IF that did not match the condition
                            continue;
                        } else if (OStringSerializerHelper.startsWithIgnoreCase(lastCommand, "let ")) {
                            lastResult = executeLet(lastCommand, db);
                        } else if (OStringSerializerHelper.startsWithIgnoreCase(lastCommand, "begin")) {
                            if (txBegun)
                                throw new OCommandSQLParsingException("Transaction already begun");
                            if (db.getTransaction().isActive())
                                // COMMIT ANY ACTIVE TX
                                db.commit();
                            txBegun = true;
                            txBegunAtLine = line;
                            txBegunAtPart = linePart;
                            db.begin();
                            if (lastCommand.length() > "begin ".length()) {
                                String next = lastCommand.substring("begin ".length()).trim();
                                if (OStringSerializerHelper.startsWithIgnoreCase(next, "isolation ")) {
                                    next = next.substring("isolation ".length()).trim();
                                    db.getTransaction().setIsolationLevel(OTransaction.ISOLATION_LEVEL.valueOf(next.toUpperCase(Locale.ENGLISH)));
                                }
                            }
                        } else if ("rollback".equalsIgnoreCase(lastCommand)) {
                            if (!txBegun)
                                throw new OCommandSQLParsingException("Transaction not begun");
                            db.rollback();
                            txBegun = false;
                            txBegunAtLine = -1;
                            txBegunAtPart = -1;
                        } else if (OStringSerializerHelper.startsWithIgnoreCase(lastCommand, "commit")) {
                            if (txBegunAtLine < 0)
                                throw new OCommandSQLParsingException("Transaction not begun");
                            if (retry == 1 && lastCommand.length() > "commit ".length()) {
                                // FIRST CYCLE: PARSE RETRY TIMES OVERWRITING DEFAULT = 1
                                String next = lastCommand.substring("commit ".length()).trim();
                                if (OStringSerializerHelper.startsWithIgnoreCase(next, "retry ")) {
                                    next = next.substring("retry ".length()).trim();
                                    maxRetry = Integer.parseInt(next);
                                }
                            }
                            db.commit();
                            txBegun = false;
                            txBegunAtLine = -1;
                            txBegunAtPart = -1;
                        } else if (OStringSerializerHelper.startsWithIgnoreCase(lastCommand, "sleep ")) {
                            executeSleep(lastCommand);
                        } else if (OStringSerializerHelper.startsWithIgnoreCase(lastCommand, "console.log ")) {
                            executeConsoleLog(lastCommand, db);
                        } else if (OStringSerializerHelper.startsWithIgnoreCase(lastCommand, "console.output ")) {
                            executeConsoleOutput(lastCommand, db);
                        } else if (OStringSerializerHelper.startsWithIgnoreCase(lastCommand, "console.error ")) {
                            executeConsoleError(lastCommand, db);
                        } else if (OStringSerializerHelper.startsWithIgnoreCase(lastCommand, "return ")) {
                            lastResult = getValue(lastCommand.substring("return ".length()), db);
                            // END OF SCRIPT
                            breakReturn = true;
                            break;
                        } else if (lastCommand != null && lastCommand.length() > 0)
                            lastResult = executeCommand(lastCommand, db);
                    }
                    if (breakReturn) {
                        break;
                    }
                }
            } catch (RuntimeException ex) {
                if (db.getTransaction().isActive())
                    db.rollback();
                throw ex;
            }
            // COMPLETED
            break;
        } catch (OTransactionException e) {
            // THIS CASE IS ON UPSERT
            context.setVariable("retries", retry);
            getDatabase().getLocalCache().clear();
            if (retry >= maxRetry)
                throw e;
            waitForNextRetry();
        } catch (ORecordDuplicatedException e) {
            // THIS CASE IS ON UPSERT
            context.setVariable("retries", retry);
            getDatabase().getLocalCache().clear();
            if (retry >= maxRetry)
                throw e;
            waitForNextRetry();
        } catch (ORecordNotFoundException e) {
            // THIS CASE IS ON UPSERT
            context.setVariable("retries", retry);
            getDatabase().getLocalCache().clear();
            if (retry >= maxRetry)
                throw e;
        } catch (ONeedRetryException e) {
            context.setVariable("retries", retry);
            getDatabase().getLocalCache().clear();
            if (retry >= maxRetry)
                throw e;
            waitForNextRetry();
        }
    }
    return lastResult;
}
Also used : OCommandSQLParsingException(com.orientechnologies.orient.core.sql.OCommandSQLParsingException) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException) ORecordDuplicatedException(com.orientechnologies.orient.core.storage.ORecordDuplicatedException) OTransactionException(com.orientechnologies.orient.core.exception.OTransactionException) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException)

Example 4 with ORecordNotFoundException

use of com.orientechnologies.orient.core.exception.ORecordNotFoundException in project orientdb by orientechnologies.

the class OTransactionOptimistic method loadRecordIfVersionIsNotLatest.

@Override
public ORecord loadRecordIfVersionIsNotLatest(ORID rid, final int recordVersion, String fetchPlan, boolean ignoreCache) throws ORecordNotFoundException {
    checkTransaction();
    final ORecord txRecord = getRecord(rid);
    if (txRecord == OTransactionRealAbstract.DELETED_RECORD)
        // DELETED IN TX
        throw new ORecordNotFoundException(rid);
    if (txRecord != null) {
        if (txRecord.getVersion() > recordVersion)
            return txRecord;
        else
            return null;
    }
    if (rid.isTemporary())
        throw new ORecordNotFoundException(rid);
    // DELEGATE TO THE STORAGE, NO TOMBSTONES SUPPORT IN TX MODE
    final ORecord record = database.executeReadRecord((ORecordId) rid, null, recordVersion, fetchPlan, ignoreCache, !ignoreCache, false, OStorage.LOCKING_STRATEGY.NONE, new ODatabaseDocumentTx.SimpleRecordReader(database.isPrefetchRecords()));
    if (record != null && isolationLevel == ISOLATION_LEVEL.REPEATABLE_READ)
        // KEEP THE RECORD IN TX TO ASSURE REPEATABLE READS
        addRecord(record, ORecordOperation.LOADED, null);
    return record;
}
Also used : ORecord(com.orientechnologies.orient.core.record.ORecord) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)

Example 5 with ORecordNotFoundException

use of com.orientechnologies.orient.core.exception.ORecordNotFoundException in project orientdb by orientechnologies.

the class OTransactionOptimistic method reloadRecord.

@Override
public ORecord reloadRecord(ORID rid, ORecord passedRecord, String fetchPlan, boolean ignoreCache, boolean force) {
    checkTransaction();
    final ORecord txRecord = getRecord(rid);
    if (txRecord == OTransactionRealAbstract.DELETED_RECORD)
        // DELETED IN TX
        return null;
    if (txRecord != null) {
        if (passedRecord != null && txRecord != passedRecord)
            OLogManager.instance().warn(this, "Found record in transaction with the same RID %s but different instance. " + "Probably the record has been loaded from another transaction and reused on the current one: reload it " + "from current transaction before to update or delete it", passedRecord.getIdentity());
        return txRecord;
    }
    if (rid.isTemporary())
        return null;
    // DELEGATE TO THE STORAGE, NO TOMBSTONES SUPPORT IN TX MODE
    final ORecord record;
    try {
        final ODatabaseDocumentTx.RecordReader recordReader;
        if (force) {
            recordReader = new ODatabaseDocumentTx.SimpleRecordReader(database.isPrefetchRecords());
        } else {
            recordReader = new ODatabaseDocumentTx.LatestVersionRecordReader();
        }
        ORecord loadedRecord = database.executeReadRecord((ORecordId) rid, passedRecord, -1, fetchPlan, ignoreCache, !ignoreCache, false, OStorage.LOCKING_STRATEGY.NONE, recordReader);
        if (force) {
            record = loadedRecord;
        } else {
            if (loadedRecord == null)
                record = passedRecord;
            else
                record = loadedRecord;
        }
    } catch (ORecordNotFoundException e) {
        return null;
    }
    if (record != null && isolationLevel == ISOLATION_LEVEL.REPEATABLE_READ)
        // KEEP THE RECORD IN TX TO ASSURE REPEATABLE READS
        addRecord(record, ORecordOperation.LOADED, null);
    return record;
}
Also used : ORecord(com.orientechnologies.orient.core.record.ORecord) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException)

Aggregations

ORecordNotFoundException (com.orientechnologies.orient.core.exception.ORecordNotFoundException)28 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)15 ORecord (com.orientechnologies.orient.core.record.ORecord)13 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)8 ORecordId (com.orientechnologies.orient.core.id.ORecordId)8 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)5 ORecordDuplicatedException (com.orientechnologies.orient.core.storage.ORecordDuplicatedException)5 ONeedRetryException (com.orientechnologies.common.concur.ONeedRetryException)4 OException (com.orientechnologies.common.exception.OException)3 OConcurrentModificationException (com.orientechnologies.orient.core.exception.OConcurrentModificationException)3 OTransactionException (com.orientechnologies.orient.core.exception.OTransactionException)3 ORID (com.orientechnologies.orient.core.id.ORID)3 IOException (java.io.IOException)3 ArrayList (java.util.ArrayList)3 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)2 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)2 Collection (java.util.Collection)2 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)1 OPlaceholder (com.orientechnologies.orient.core.db.record.OPlaceholder)1 ORecordOperation (com.orientechnologies.orient.core.db.record.ORecordOperation)1