Search in sources :

Example 21 with ORecordNotFoundException

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

the class LocalPaginatedStorageIncrementalSync method deleteRecord.

private boolean deleteRecord(Random random) {
    originalDB.activateOnCurrentThread();
    final int clusterId = originalDB.getClusterIdByName("Sample");
    final long[] rids = originalDB.getStorage().getClusterDataRange(clusterId);
    if (rids[0] == -1)
        return false;
    boolean deleted = false;
    while (!deleted) {
        final int deleteIndex = random.nextInt(rids.length);
        final ORecordId recordId = new ORecordId(clusterId, rids[deleteIndex]);
        try {
            final ODocument document = originalDB.load(recordId);
            if (document != null) {
                document.delete();
                deleted = true;
            }
        } catch (ORecordNotFoundException e) {
            deleted = false;
        }
    }
    return true;
}
Also used : ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 22 with ORecordNotFoundException

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

the class OConflictResolverDatabaseRepairer method repairRecords.

private boolean repairRecords(final ODatabaseDocumentInternal db, final List<ORecordId> rids) {
    final ODistributedConfiguration dCfg = dManager.getDatabaseConfiguration(databaseName);
    final int maxAutoRetry = OGlobalConfiguration.DISTRIBUTED_CONCURRENT_TX_MAX_AUTORETRY.getValueAsInteger();
    final int autoRetryDelay = OGlobalConfiguration.DISTRIBUTED_CONCURRENT_TX_AUTORETRY_DELAY.getValueAsInteger();
    final ODistributedRequestId requestId = new ODistributedRequestId(dManager.getLocalNodeId(), dManager.getNextMessageIdCounter());
    final ODistributedDatabase localDistributedDatabase = dManager.getMessageService().getDatabase(databaseName);
    final ODistributedTxContext ctx = localDistributedDatabase.registerTxContext(requestId);
    try {
        // ACQUIRE LOCKS WITH A LARGER TIMEOUT
        ODistributedTransactionManager.acquireMultipleRecordLocks(this, dManager, localDistributedDatabase, rids, maxAutoRetry, autoRetryDelay, null, ctx, 2000);
        try {
            final Set<String> clusterNames = new HashSet();
            for (ORecordId rid : rids) clusterNames.add(db.getClusterNameById(rid.getClusterId()));
            final Collection<String> involvedServers = dCfg.getServers(clusterNames);
            final Set<String> nonLocalServers = new HashSet<String>(involvedServers);
            nonLocalServers.remove(dManager.getLocalNodeName());
            if (nonLocalServers.isEmpty())
                return true;
            // CREATE LOCAL RESULT
            final OTxTaskResult localResult = new OTxTaskResult();
            for (ORecordId rid : rids) {
                final OStorageOperationResult<ORawBuffer> res;
                if (rid.getClusterPosition() > -1)
                    res = db.getStorage().readRecord(rid, null, true, false, null);
                else
                    res = null;
                if (res != null)
                    localResult.results.add(res.getResult());
                else
                    localResult.results.add(null);
            }
            ODistributedServerLog.debug(this, dManager.getLocalNodeName(), involvedServers.toString(), ODistributedServerLog.DIRECTION.OUT, "Auto repairing records %s on servers %s (reqId=%s)...", rids, involvedServers, requestId);
            // CREATE TX TASK
            final ORepairRecordsTask tx = new ORepairRecordsTask();
            for (ORecordId rid : rids) tx.add(new OReadRecordTask(rid));
            ODistributedResponse response = dManager.sendRequest(databaseName, clusterNames, nonLocalServers, tx, requestId.getMessageId(), ODistributedRequest.EXECUTION_MODE.RESPONSE, localResult, null);
            // MAP OF OCompletedTxTask SERVER/RECORDS. RECORD == NULL MEANS DELETE
            final Map<String, OCompleted2pcTask> repairMap = new HashMap<String, OCompleted2pcTask>(rids.size());
            for (String server : involvedServers) {
                final OCompleted2pcTask completedTask = new OCompleted2pcTask(requestId, false, tx.getPartitionKey());
                repairMap.put(server, completedTask);
            }
            try {
                if (response != null) {
                    final Object payload = response.getPayload();
                    if (payload instanceof Map) {
                        final Map<String, Object> map = (Map<String, Object>) payload;
                        // BROWSE FROM LOCAL RESULT
                        for (int i = 0; i < localResult.results.size(); ++i) {
                            final Map<Object, List<String>> groupedResult = new HashMap<Object, List<String>>();
                            final ORecordId rid = rids.get(i);
                            for (Map.Entry<String, Object> entry : map.entrySet()) {
                                if (entry.getValue() instanceof Throwable) {
                                    // ABORT IT
                                    ODistributedServerLog.info(this, dManager.getLocalNodeName(), entry.getKey(), ODistributedServerLog.DIRECTION.IN, "Error on auto repairing record %s on servers %s (error=%s)", rid, entry.getKey(), entry.getValue());
                                    return false;
                                }
                                final OTxTaskResult v = (OTxTaskResult) entry.getValue();
                                final Object remoteValue = v.results.get(i);
                                List<String> group = groupedResult.get(remoteValue);
                                if (group == null) {
                                    group = new ArrayList<String>();
                                    groupedResult.put(remoteValue, group);
                                }
                                group.add(entry.getKey());
                            }
                            if (groupedResult.size() == 1)
                                // NO CONFLICT, SKIP IT
                                continue;
                            ODocument config = null;
                            // EXECUTE THE CONFLICT RESOLVE PIPELINE: CONTINUE UNTIL THE WINNER IS NOT NULL (=RESOLVED)
                            Object winner = null;
                            Map<Object, List<String>> candidates = groupedResult;
                            for (ODistributedConflictResolver conflictResolver : conflictResolvers) {
                                final ODistributedConflictResolver.OConflictResult conflictResult = conflictResolver.onConflict(databaseName, db.getClusterNameById(rid.getClusterId()), rid, dManager, candidates, config);
                                winner = conflictResult.winner;
                                if (winner != null)
                                    // FOUND WINNER
                                    break;
                                candidates = conflictResult.candidates;
                            }
                            if (winner == null)
                                // NO WINNER, SKIP IT
                                continue;
                            for (Map.Entry<Object, List<String>> entry : groupedResult.entrySet()) {
                                final Object value = entry.getKey();
                                final List<String> servers = entry.getValue();
                                for (String server : servers) {
                                    ODistributedServerLog.debug(this, dManager.getLocalNodeName(), server, ODistributedServerLog.DIRECTION.OUT, "Preparing fix for record %s on servers %s, value=%s...", rid, server, winner);
                                    if (!winner.equals(value)) {
                                        final OCompleted2pcTask completedTask = repairMap.get(server);
                                        if (winner instanceof ORawBuffer && value instanceof ORawBuffer) {
                                            // UPDATE THE RECORD
                                            final ORawBuffer winnerRecord = (ORawBuffer) winner;
                                            completedTask.addFixTask(new OFixUpdateRecordTask(rid, winnerRecord.buffer, ORecordVersionHelper.setRollbackMode(winnerRecord.version), winnerRecord.recordType));
                                        } else if (winner instanceof ORecordNotFoundException && value instanceof ORawBuffer) {
                                            // DELETE THE RECORD
                                            completedTask.addFixTask(new OFixCreateRecordTask(rid, -1));
                                        } else if (value instanceof Throwable) {
                                        // MANAGE EXCEPTION
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            } finally {
                int repaired = 0;
                for (Map.Entry<String, OCompleted2pcTask> entry : repairMap.entrySet()) {
                    final String server = entry.getKey();
                    final OCompleted2pcTask task = entry.getValue();
                    repaired += task.getFixTasks().size();
                    if (dManager.getLocalNodeName().equals(server))
                        // EXECUTE IT LOCALLY
                        dManager.executeOnLocalNode(requestId, task, db);
                    else {
                        // EXECUTE REMOTELY
                        final List<String> servers = new ArrayList<String>();
                        servers.add(server);
                        // FILTER ONLY THE SERVER ONLINE
                        dManager.getAvailableNodes(servers, databaseName);
                        if (!servers.isEmpty()) {
                            response = dManager.sendRequest(databaseName, clusterNames, servers, task, dManager.getNextMessageIdCounter(), ODistributedRequest.EXECUTION_MODE.RESPONSE, null, null);
                        }
                    }
                }
                if (repaired == 0)
                    ODistributedServerLog.debug(this, dManager.getLocalNodeName(), involvedServers.toString(), ODistributedServerLog.DIRECTION.OUT, "Auto repairing completed. No fix is needed (reqId=%s)", repaired, requestId);
                else
                    ODistributedServerLog.info(this, dManager.getLocalNodeName(), involvedServers.toString(), ODistributedServerLog.DIRECTION.OUT, "Auto repairing completed. Sent %d fix messages for %d records (reqId=%s)", repaired, rids.size(), requestId);
            }
        } finally {
            // RELEASE LOCKS AND REMOVE TX CONTEXT
            localDistributedDatabase.popTxContext(requestId);
            ctx.destroy();
        }
    } catch (Throwable e) {
        ODistributedServerLog.debug(this, dManager.getLocalNodeName(), null, ODistributedServerLog.DIRECTION.NONE, "Error executing auto repairing (error=%s, reqId=%s)", e.toString(), requestId);
        return false;
    }
    return true;
}
Also used : ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ODistributedConflictResolver(com.orientechnologies.orient.server.distributed.conflict.ODistributedConflictResolver) ORawBuffer(com.orientechnologies.orient.core.storage.ORawBuffer) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument) ORecordId(com.orientechnologies.orient.core.id.ORecordId) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) ConcurrentMap(java.util.concurrent.ConcurrentMap)

Example 23 with ORecordNotFoundException

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

the class SimulateOperationsAgainstServer method deleteDocument.

protected void deleteDocument(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, " delete no item " + iSkip + " because out of range");
            else {
                doc = result.get(0).getRecord();
                doc.delete();
                log(threadId, iCycle, dbUrl, " deleted item " + iSkip + " RID=" + result.get(0));
            }
            break;
        } catch (OConcurrentModificationException e) {
            log(threadId, iCycle, dbUrl, " concurrent delete against record " + doc + ", reload it and retry " + retry + "/" + MAX_RETRY + "...");
            if (doc != null)
                doc.reload(null, true);
        } catch (ORecordNotFoundException e) {
            log(threadId, iCycle, dbUrl, " delete no item " + iSkip + " because not found");
        } 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 24 with ORecordNotFoundException

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

the class OGraphRepair method repairEdges.

protected void repairEdges(OrientBaseGraph graph, ORepairStats stats, OCommandOutputListener outputListener, Map<String, List<String>> options) {
    final ODatabaseDocumentTx db = graph.getRawGraph();
    final OMetadata metadata = db.getMetadata();
    final OSchema schema = metadata.getSchema();
    final OrientConfigurableGraph.Settings settings = graph.settings;
    final boolean useVertexFieldsForEdgeLabels = settings.isUseVertexFieldsForEdgeLabels();
    final OClass edgeClass = schema.getClass(OrientEdgeType.CLASS_NAME);
    if (edgeClass != null) {
        final long countEdges = db.countClass(edgeClass.getName());
        long skipEdges = 0l;
        if (options != null && options.get("-skipEdges") != null) {
            skipEdges = Long.parseLong(options.get("-skipEdges").get(0));
        }
        message(outputListener, "Scanning " + countEdges + " edges (skipEdges=" + skipEdges + ")...\n");
        long parsedEdges = 0l;
        final long beginTime = System.currentTimeMillis();
        for (ODocument edge : db.browseClass(edgeClass.getName())) {
            final ORID edgeId = edge.getIdentity();
            parsedEdges++;
            if (skipEdges > 0 && parsedEdges <= skipEdges)
                continue;
            stats.scannedEdges++;
            if (eventListener != null)
                eventListener.onScannedEdge(edge);
            if (outputListener != null && stats.scannedEdges % 100000 == 0) {
                long speedPerSecond = (long) (parsedEdges / ((System.currentTimeMillis() - beginTime) / 1000.0));
                if (speedPerSecond < 1)
                    speedPerSecond = 1;
                final long remaining = (countEdges - parsedEdges) / speedPerSecond;
                message(outputListener, "+ edges: scanned " + stats.scannedEdges + ", removed " + stats.removedEdges + " (estimated remaining time " + remaining + " secs)\n");
            }
            boolean outVertexMissing = false;
            String removalReason = "";
            final OIdentifiable out = OrientEdge.getConnection(edge, Direction.OUT);
            if (out == null)
                outVertexMissing = true;
            else {
                ODocument outVertex;
                try {
                    outVertex = out.getRecord();
                } catch (ORecordNotFoundException e) {
                    outVertex = null;
                }
                if (outVertex == null)
                    outVertexMissing = true;
                else {
                    final String outFieldName = OrientVertex.getConnectionFieldName(Direction.OUT, edge.getClassName(), useVertexFieldsForEdgeLabels);
                    final Object outEdges = outVertex.field(outFieldName);
                    if (outEdges == null)
                        outVertexMissing = true;
                    else if (outEdges instanceof ORidBag) {
                        if (!((ORidBag) outEdges).contains(edgeId))
                            outVertexMissing = true;
                    } else if (outEdges instanceof Collection) {
                        if (!((Collection) outEdges).contains(edgeId))
                            outVertexMissing = true;
                    } else if (outEdges instanceof OIdentifiable) {
                        if (((OIdentifiable) outEdges).getIdentity().equals(edgeId))
                            outVertexMissing = true;
                    }
                }
            }
            if (outVertexMissing)
                removalReason = "missing outgoing vertex (" + out + ")";
            boolean inVertexMissing = false;
            final OIdentifiable in = OrientEdge.getConnection(edge, Direction.IN);
            if (in == null)
                inVertexMissing = true;
            else {
                ODocument inVertex;
                try {
                    inVertex = in.getRecord();
                } catch (ORecordNotFoundException e) {
                    inVertex = null;
                }
                if (inVertex == null)
                    inVertexMissing = true;
                else {
                    final String inFieldName = OrientVertex.getConnectionFieldName(Direction.IN, edge.getClassName(), useVertexFieldsForEdgeLabels);
                    final Object inEdges = inVertex.field(inFieldName);
                    if (inEdges == null)
                        inVertexMissing = true;
                    else if (inEdges instanceof ORidBag) {
                        if (!((ORidBag) inEdges).contains(edgeId))
                            inVertexMissing = true;
                    } else if (inEdges instanceof Collection) {
                        if (!((Collection) inEdges).contains(edgeId))
                            inVertexMissing = true;
                    } else if (inEdges instanceof OIdentifiable) {
                        if (((OIdentifiable) inEdges).getIdentity().equals(edgeId))
                            inVertexMissing = true;
                    }
                }
            }
            if (inVertexMissing) {
                if (!removalReason.isEmpty())
                    removalReason += ", ";
                removalReason += "missing incoming vertex (" + in + ")";
            }
            if (outVertexMissing || inVertexMissing) {
                try {
                    message(outputListener, "+ deleting corrupted edge " + edge + " because " + removalReason + "\n");
                    edge.delete();
                    stats.removedEdges++;
                    if (eventListener != null)
                        eventListener.onRemovedEdge(edge);
                } catch (Exception e) {
                    message(outputListener, "Error on deleting edge " + edge.getIdentity() + " (" + e.getMessage() + ")");
                }
            }
        }
        message(outputListener, "Scanning edges completed\n");
    }
}
Also used : OSchema(com.orientechnologies.orient.core.metadata.schema.OSchema) ORidBag(com.orientechnologies.orient.core.db.record.ridbag.ORidBag) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException) OMetadata(com.orientechnologies.orient.core.metadata.OMetadata) OClass(com.orientechnologies.orient.core.metadata.schema.OClass) ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException) Collection(java.util.Collection) ORID(com.orientechnologies.orient.core.id.ORID) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 25 with ORecordNotFoundException

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

the class OrientGraph method addEdgeInternal.

OrientEdge addEdgeInternal(final OrientVertex currentVertex, String label, final OrientVertex inVertex, final String iClassName, final String iClusterName, final Object... fields) {
    if (currentVertex.checkDeletedInTx())
        throw new ORecordNotFoundException(currentVertex.getIdentity(), "The vertex " + currentVertex.getIdentity() + " has been deleted");
    if (inVertex.checkDeletedInTx())
        throw new ORecordNotFoundException(inVertex.getIdentity(), "The vertex " + inVertex.getIdentity() + " has been deleted");
    autoStartTransaction();
    // TEMPORARY STATIC LOCK TO AVOID MT PROBLEMS AGAINST OMVRBTreeRID
    final ODocument outDocument = currentVertex.getRecord();
    if (outDocument == null)
        throw new IllegalArgumentException("source vertex is invalid (rid=" + currentVertex.getIdentity() + ")");
    if (!ODocumentInternal.getImmutableSchemaClass(outDocument).isVertexType())
        throw new IllegalArgumentException("source record is not a vertex");
    ODocument inDocument = inVertex.getRecord();
    if (inDocument == null)
        throw new IllegalArgumentException("destination vertex is invalid (rid=" + inVertex.getIdentity() + ")");
    if (!ODocumentInternal.getImmutableSchemaClass(outDocument).isVertexType())
        throw new IllegalArgumentException("destination record is not a vertex");
    OIdentifiable to;
    OIdentifiable from;
    label = OrientBaseGraph.encodeClassName(label);
    if (label == null && iClassName != null)
        // RETRO-COMPATIBILITY WITH THE SYNTAX CLASS:<CLASS-NAME>
        label = OrientBaseGraph.encodeClassName(iClassName);
    if (isUseClassForEdgeLabel()) {
        final OrientEdgeType edgeType = getEdgeType(label);
        if (edgeType == null)
            // AUTO CREATE CLASS
            createEdgeType(label);
        else
            // OVERWRITE CLASS NAME BECAUSE ATTRIBUTES ARE CASE SENSITIVE
            label = edgeType.getName();
    }
    final String outFieldName = currentVertex.getConnectionFieldName(Direction.OUT, label, settings.isUseVertexFieldsForEdgeLabels());
    final String inFieldName = currentVertex.getConnectionFieldName(Direction.IN, label, settings.isUseVertexFieldsForEdgeLabels());
    // null check has to go here.
    if (label == null)
        throw ExceptionFactory.edgeLabelCanNotBeNull();
    OrientEdge edge = null;
    if (currentVertex.canCreateDynamicEdge(outDocument, inDocument, outFieldName, inFieldName, fields, label)) {
        // CREATE A LIGHTWEIGHT DYNAMIC EDGE
        from = currentVertex.rawElement;
        to = inDocument;
        if (edge == null) {
            if (settings.isKeepInMemoryReferences())
                edge = getEdgeInstance(from.getIdentity(), to.getIdentity(), label);
            else
                edge = getEdgeInstance(from, to, label);
        }
    } else {
        if (edge == null) {
            // CREATE THE EDGE DOCUMENT TO STORE FIELDS TOO
            edge = getEdgeInstance(label, fields);
            if (settings.isKeepInMemoryReferences())
                edge.getRecord().fields(OrientBaseGraph.CONNECTION_OUT, currentVertex.rawElement.getIdentity(), OrientBaseGraph.CONNECTION_IN, inDocument.getIdentity());
            else
                edge.getRecord().fields(OrientBaseGraph.CONNECTION_OUT, currentVertex.rawElement, OrientBaseGraph.CONNECTION_IN, inDocument);
        }
        from = edge.getRecord();
        to = edge.getRecord();
    }
    if (settings.isKeepInMemoryReferences()) {
        // USES REFERENCES INSTEAD OF DOCUMENTS
        from = from.getIdentity();
        to = to.getIdentity();
    }
    // OUT-VERTEX ---> IN-VERTEX/EDGE
    currentVertex.createLink(this, outDocument, to, outFieldName);
    // IN-VERTEX ---> OUT-VERTEX/EDGE
    currentVertex.createLink(this, inDocument, from, inFieldName);
    // THE DIRTY MANAGER MANAGE THE SAVE OF ALL LINKED ENTITIES.
    if (!edge.isLightweight())
        edge.save(iClusterName);
    else
        outDocument.save();
    return edge;
}
Also used : ORecordNotFoundException(com.orientechnologies.orient.core.exception.ORecordNotFoundException) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Aggregations

ORecordNotFoundException (com.orientechnologies.orient.core.exception.ORecordNotFoundException)27 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)15 ORecord (com.orientechnologies.orient.core.record.ORecord)11 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)3 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