Search in sources :

Example 16 with ONeedRetryException

use of com.orientechnologies.common.concur.ONeedRetryException in project orientdb by orientechnologies.

the class ODistributedStorage method readRecordIfVersionIsNotLatest.

@Override
public OStorageOperationResult<ORawBuffer> readRecordIfVersionIsNotLatest(final ORecordId rid, final String fetchPlan, final boolean ignoreCache, final int recordVersion) throws ORecordNotFoundException {
    final ORawBuffer memCopy = localDistributedDatabase.getRecordIfLocked(rid);
    if (memCopy != null)
        return new OStorageOperationResult<ORawBuffer>(memCopy);
    try {
        final String clusterName = getClusterNameByRID(rid);
        final ODistributedConfiguration dbCfg = distributedConfiguration;
        final List<String> nodes = dbCfg.getServers(clusterName, null);
        final int availableNodes = nodes.size();
        // CHECK IF LOCAL NODE OWNS THE DATA AND READ-QUORUM = 1: GET IT LOCALLY BECAUSE IT'S FASTER
        final String localNodeName = dManager.getLocalNodeName();
        if (nodes.isEmpty() || nodes.contains(dManager.getLocalNodeName()) && dbCfg.getReadQuorum(clusterName, availableNodes, localNodeName) <= 1) {
            // DON'T REPLICATE
            return (OStorageOperationResult<ORawBuffer>) OScenarioThreadLocal.executeAsDistributed(new Callable() {

                @Override
                public Object call() throws Exception {
                    return wrapped.readRecordIfVersionIsNotLatest(rid, fetchPlan, ignoreCache, recordVersion);
                }
            });
        }
        // DISTRIBUTE IT
        final Object result = dManager.sendRequest(getName(), Collections.singleton(clusterName), nodes, new OReadRecordIfNotLatestTask(rid, recordVersion), dManager.getNextMessageIdCounter(), EXECUTION_MODE.RESPONSE, null, null).getPayload();
        if (result instanceof ONeedRetryException)
            throw (ONeedRetryException) result;
        else if (result instanceof Exception)
            throw OException.wrapException(new ODistributedException("Error on execution distributed read record"), (Exception) result);
        return new OStorageOperationResult<ORawBuffer>((ORawBuffer) result);
    } catch (ONeedRetryException e) {
        // PASS THROUGH
        throw e;
    } catch (HazelcastInstanceNotActiveException e) {
        throw new OOfflineNodeException("Hazelcast instance is not available");
    } catch (HazelcastException e) {
        throw new OOfflineNodeException("Hazelcast instance is not available");
    } catch (Exception e) {
        handleDistributedException("Cannot route read record operation for %s to the distributed node", e, rid);
        // UNREACHABLE
        return null;
    }
}
Also used : HazelcastException(com.hazelcast.core.HazelcastException) OOfflineNodeException(com.orientechnologies.common.concur.OOfflineNodeException) OCallable(com.orientechnologies.common.util.OCallable) Callable(java.util.concurrent.Callable) HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) OException(com.orientechnologies.common.exception.OException) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException) HazelcastException(com.hazelcast.core.HazelcastException) ODistributedRedirectException(com.orientechnologies.orient.enterprise.channel.binary.ODistributedRedirectException) OIOException(com.orientechnologies.common.io.OIOException) OOfflineNodeException(com.orientechnologies.common.concur.OOfflineNodeException) HazelcastInstanceNotActiveException(com.hazelcast.core.HazelcastInstanceNotActiveException) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException)

Example 17 with ONeedRetryException

use of com.orientechnologies.common.concur.ONeedRetryException in project orientdb by orientechnologies.

the class ODistributedTransactionManager method processCommitResult.

protected boolean processCommitResult(final String localNodeName, final OTransaction iTx, final OTxTask txTask, final Set<String> involvedClusters, final Iterable<ORecordOperation> tmpEntries, final Collection<String> nodes, final int autoRetryDelay, final ODistributedRequestId reqId, final ODistributedResponse dResponse, final boolean isLastRetry) throws InterruptedException {
    final Object result = dResponse.getPayload();
    if (result instanceof OTxTaskResult) {
        final OTxTaskResult txResult = (OTxTaskResult) result;
        final List<Object> list = txResult.results;
        for (int i = 0; i < txTask.getTasks().size(); ++i) {
            final Object o = list.get(i);
            final OAbstractRecordReplicatedTask task = txTask.getTasks().get(i);
            if (task instanceof OCreateRecordTask) {
                final OCreateRecordTask t = (OCreateRecordTask) task;
                iTx.updateIdentityAfterCommit(t.getRid(), ((OPlaceholder) o).getIdentity());
                final ORecord rec = iTx.getRecord(t.getRid());
                if (rec != null)
                    ORecordInternal.setVersion(rec, ((OPlaceholder) o).getVersion());
            } else if (task instanceof OUpdateRecordTask) {
                final OUpdateRecordTask t = (OUpdateRecordTask) task;
                ORecordInternal.setVersion(iTx.getRecord(t.getRid()), (Integer) o);
            } else if (task instanceof ODeleteRecordTask) {
            }
        }
        // RESET DIRTY FLAGS TO AVOID CALLING AUTO-SAVE
        for (ORecordOperation op : tmpEntries) {
            final ORecord record = op.getRecord();
            if (record != null)
                ORecordInternal.unsetDirty(record);
        }
        if (ODistributedServerLog.isDebugEnabled())
            ODistributedServerLog.debug(this, localNodeName, null, ODistributedServerLog.DIRECTION.NONE, "Distributed transaction %s completed", reqId);
        sendTxCompleted(localNodeName, involvedClusters, nodes, reqId, true, txTask.getPartitionKey());
    } else if (result instanceof ODistributedRecordLockedException) {
        if (ODistributedServerLog.isDebugEnabled())
            ODistributedServerLog.debug(this, localNodeName, nodes.toString(), ODistributedServerLog.DIRECTION.IN, "Distributed transaction %s error: record %s is locked by %s", reqId, ((ODistributedRecordLockedException) result).getRid(), ((ODistributedRecordLockedException) result).getLockHolder());
        // if this is the last retry (and it failed), we don't need to wait anymore
        if (autoRetryDelay > 0 && !isLastRetry)
            Thread.sleep(autoRetryDelay / 2 + new Random().nextInt(autoRetryDelay));
        // RETRY
        return false;
    } else if (result instanceof Exception) {
        // EXCEPTION: LOG IT AND ADD AS NESTED EXCEPTION
        if (ODistributedServerLog.isDebugEnabled())
            ODistributedServerLog.debug(this, localNodeName, nodes.toString(), ODistributedServerLog.DIRECTION.IN, "Distributed transaction %s received error: %s", reqId, result, result.toString());
        // LET TO THE CALLER TO UNDO IT
        if (result instanceof OTransactionException || result instanceof ONeedRetryException)
            throw (RuntimeException) result;
        if (result instanceof ORecordNotFoundException) {
            // REPAIR THE RECORD IMMEDIATELY
            localDistributedDatabase.getDatabaseRepairer().repairRecord((ORecordId) ((ORecordNotFoundException) result).getRid());
            // RETRY
            return false;
        }
        throw OException.wrapException(new OTransactionException("Error on committing distributed transaction"), (Exception) result);
    } else {
        // UNKNOWN RESPONSE TYPE
        if (ODistributedServerLog.isDebugEnabled())
            ODistributedServerLog.info(this, localNodeName, nodes.toString(), ODistributedServerLog.DIRECTION.IN, "Distributed transaction %s error, received unknown response type: %s", reqId, result);
        // ROLLBACK TX
        storage.executeUndoOnLocalServer(dResponse.getRequestId(), txTask);
        sendTxCompleted(localNodeName, involvedClusters, nodes, dResponse.getRequestId(), false, txTask.getPartitionKey());
        throw new OTransactionException("Error on committing distributed transaction, received unknown response type " + result);
    }
    return true;
}
Also used : OPlaceholder(com.orientechnologies.orient.core.db.record.OPlaceholder) OException(com.orientechnologies.common.exception.OException) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException) IOException(java.io.IOException) ORecordOperation(com.orientechnologies.orient.core.db.record.ORecordOperation) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException) ORecord(com.orientechnologies.orient.core.record.ORecord)

Example 18 with ONeedRetryException

use of com.orientechnologies.common.concur.ONeedRetryException in project orientdb by orientechnologies.

the class HALocalGraphTest method startThread.

private Runnable startThread(final int id, final OrientGraphFactory graphFactory) {
    Runnable th = new Runnable() {

        @Override
        public void run() {
            // OrientBaseGraph graph = new OrientGraph(getDBURL());
            // OrientGraph graph = graphFactory.getTx();
            boolean useSQL = false;
            StringBuilder sb = new StringBuilder(".");
            for (int m = 0; m < id; m++) {
                sb.append(".");
            }
            long st = System.currentTimeMillis();
            try {
                String query = "select from Test where prop2='v2-1'";
                boolean isRunning = true;
                for (int i = 1; i < TOTAL_CYCLES_PER_THREAD && isRunning; i++) {
                    if ((i % 2500) == 0) {
                        long et = System.currentTimeMillis();
                        log(sb.toString() + " [" + id + "] Total Records Processed: [" + i + "] Current: [2500] Time taken: [" + (et - st) / 1000 + "] seconds");
                        st = System.currentTimeMillis();
                    }
                    if (sleep > 0)
                        Thread.sleep(sleep);
                    OrientGraph graph = graphFactory.getTx();
                    if (!graph.getRawGraph().getURL().startsWith("remote:"))
                        Assert.assertTrue(graph.getVertexType("Test").getClusterSelection() instanceof OLocalClusterWrapperStrategy);
                    try {
                        if (useSQL) {
                            boolean update = true;
                            boolean isException = false;
                            String sql = "Update Test set prop5='" + String.valueOf(System.currentTimeMillis()) + "', updateTime='" + new Date().toString() + "' where prop2='v2-1'";
                            for (int k = 0; k < 10 && update; k++) {
                                try {
                                    graph.command(new OCommandSQL(sql)).execute();
                                    if (isException) {
                                        log("********** [" + id + "][" + k + "] Update success after distributed lock Exception");
                                    }
                                    update = false;
                                    break;
                                } catch (ONeedRetryException ex) {
                                    if (ex instanceof OConcurrentModificationException || ex.getCause() instanceof OConcurrentModificationException) {
                                    } else {
                                        isException = true;
                                        log("*$$$$$$$$$$$$$$ [" + id + "][" + k + "] Distributed Exception: [" + ex + "] Cause: [" + (ex.getCause() != null ? ex.getCause() : "--") + "] ");
                                    }
                                } catch (ODistributedException ex) {
                                    if (ex.getCause() instanceof OConcurrentModificationException) {
                                    } else {
                                        isException = true;
                                        log("*$$$$$$$$$$$$$$ [" + id + "][" + k + "] Distributed Exception: [" + ex + "] Cause: [" + (ex.getCause() != null ? ex.getCause() : "--") + "] ");
                                    }
                                } catch (Exception ex) {
                                    log("[" + id + "][" + k + "] Exception " + ex);
                                }
                            }
                        } else {
                            boolean retry = true;
                            Iterable<Vertex> vtxs = null;
                            for (int k = 0; k < 100 && retry; k++) try {
                                vtxs = graph.command(new OCommandSQL(query)).execute();
                                break;
                            } catch (ONeedRetryException e) {
                            // RETRY
                            }
                            for (Vertex vtx : vtxs) {
                                if (retry) {
                                    retry = true;
                                    boolean isException = false;
                                    for (int k = 0; k < 100 && retry; k++) {
                                        OrientVertex vtx1 = (OrientVertex) vtx;
                                        try {
                                            vtx1.setProperty("prop5", "prop55");
                                            vtx1.setProperty("updateTime", new Date().toString());
                                            vtx1.setProperty("blob", new byte[1000]);
                                            graph.commit();
                                            if (isException) {
                                            // log("********** [" + id + "][" + k + "] Update success after distributed lock Exception for vertex " +
                                            // vtx1);
                                            }
                                            retry = false;
                                            break;
                                        } catch (OConcurrentModificationException ex) {
                                            vtx1.reload();
                                        } catch (ONeedRetryException ex) {
                                            if (ex instanceof ODistributedRecordLockedException) {
                                                if (k > 20)
                                                    log("*$$$$$$$$$$$$$$ [" + id + "][" + k + "] ODistributedRecordLockedException: [" + ex + "] Cause: [" + (ex.getCause() != null ? ex.getCause() : "--") + "] for vertex " + vtx1);
                                                vtx1.reload();
                                            } else if (ex instanceof ONeedRetryException || ex.getCause() instanceof ONeedRetryException) {
                                                vtx1.reload();
                                            } else {
                                                if (ex.getCause() instanceof ConcurrentModificationException) {
                                                    ex.printStackTrace();
                                                }
                                                log("*$$$$$$$$$$$$$$ [" + id + "][" + k + "] Distributed Exception: [" + ex + "] Cause: [" + (ex.getCause() != null ? ex.getCause() : "--") + "] for vertex " + vtx1);
                                            }
                                            // log("*** [" + id + "][" + k + "] Distributed Exception: [" + ex + "] Cause: [" + (ex.getCause() != null ?
                                            // ex.getCause() : "--") + "] for vertex " + vtx1);
                                            isException = true;
                                        } catch (ODistributedException ex) {
                                            if (ex.getCause() instanceof ONeedRetryException) {
                                                vtx1.reload();
                                            } else {
                                                if (ex.getCause() instanceof ConcurrentModificationException) {
                                                    ex.printStackTrace();
                                                }
                                                log("*$$$$$$$$$$$$$$ [" + id + "][" + k + "] Distributed Exception: [" + ex + "] Cause: [" + (ex.getCause() != null ? ex.getCause() : "--") + "] for vertex " + vtx1);
                                            }
                                            // log("*** [" + id + "][" + k + "] Distributed Exception: [" + ex + "] Cause: [" + (ex.getCause() != null ?
                                            // ex.getCause() : "--") + "] for vertex " + vtx1);
                                            isException = true;
                                        } catch (Exception ex) {
                                            log("[" + id + "][" + k + "] Exception " + ex + " for vertex " + vtx1);
                                        }
                                    }
                                    if (retry) {
                                        log("********** [" + id + "] Failed to update after Exception for vertex " + vtx);
                                    }
                                }
                            }
                        }
                    } finally {
                        graph.shutdown();
                    }
                    operations.incrementAndGet();
                }
            } catch (Exception ex) {
                System.out.println("ID: [" + id + "]********** Exception " + ex + " \n\n");
                ex.printStackTrace();
            } finally {
                log("[" + id + "] Done................>>>>>>>>>>>>>>>>>>");
            }
        }
    };
    return th;
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) ODistributedRecordLockedException(com.orientechnologies.orient.server.distributed.task.ODistributedRecordLockedException) OConcurrentModificationException(com.orientechnologies.orient.core.exception.OConcurrentModificationException) OConcurrentModificationException(com.orientechnologies.orient.core.exception.OConcurrentModificationException) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException) ODistributedRecordLockedException(com.orientechnologies.orient.server.distributed.task.ODistributedRecordLockedException) OConcurrentModificationException(com.orientechnologies.orient.core.exception.OConcurrentModificationException) OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OLocalClusterWrapperStrategy(com.orientechnologies.orient.server.distributed.impl.OLocalClusterWrapperStrategy) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException)

Example 19 with ONeedRetryException

use of com.orientechnologies.common.concur.ONeedRetryException in project orientdb by orientechnologies.

the class DistributedDatabaseCRUDTest method startVertexUpdateThread.

private Runnable startVertexUpdateThread(final int id, final OrientGraphFactory graphFactory, final String propertyValue) {
    Runnable th = new Runnable() {

        @Override
        public void run() {
            log("Starting runnable for vertex update thread for property value " + propertyValue);
            long st = System.currentTimeMillis();
            try {
                // String query = "select from TestNode where property4='value4-1'";
                String query = "select from TestNode where property4='" + propertyValue + "'";
                boolean isRunning = true;
                for (int i = 1; i < 100000000 && isRunning; i++) {
                    if ((i % 100) == 0) {
                        long et = System.currentTimeMillis();
                        log(" Total Records Processed: [" + i + "] Time taken for [100] records: [" + (et - st) / 1000 + "] seconds");
                        st = System.currentTimeMillis();
                    }
                    OrientGraph graph = graphFactory.getTx();
                    try {
                        Iterable<Vertex> vtxs = graph.command(new OCommandSQL(query)).execute();
                        boolean retry = true;
                        for (Vertex vtx : vtxs) {
                            if (retry) {
                                retry = true;
                                boolean isException = false;
                                Exception tex = null;
                                int k = 1;
                                for (; k <= 100 && retry; k++) {
                                    if (!graph.getRawGraph().getURL().startsWith("remote:"))
                                        Assert.assertTrue(graph.getVertexType("TestNode").getClusterSelection() instanceof OLocalClusterWrapperStrategy);
                                    OrientVertex vtx1 = (OrientVertex) vtx;
                                    try {
                                        vtx1.setProperty("prop5", "prop55");
                                        vtx1.setProperty("updateTime", new Date().toString());
                                        graph.commit();
                                        if (isException) {
                                        // log("********** [" + id + "][" + k + "] Update success after distributed lock Exception for vertex " +
                                        // vtx1);
                                        }
                                        retry = false;
                                        break;
                                    } catch (Exception ex) {
                                        if (ex instanceof ODatabaseException || ex instanceof ONeedRetryException || ex instanceof ODistributedException) {
                                            tex = ex;
                                            if (ex instanceof ONeedRetryException || ex.getCause() instanceof ONeedRetryException) {
                                                // log("[" + id + "][" + vtx + "][ Retry: " + k + "] OrientDB Exception [" + ex + "]");
                                                try {
                                                    Thread.sleep(new Random().nextInt(500));
                                                } catch (InterruptedException e) {
                                                    e.printStackTrace();
                                                }
                                                vtx1.reload();
                                            } else {
                                                log("[" + id + "][" + vtx + "][ Retry: " + k + "] Failed to update. OrientDB Exception [" + ex + "]");
                                            }
                                            isException = true;
                                        } else {
                                            tex = ex;
                                            log("[" + id + "][" + k + "] Failed to update non OrientDB Exception [" + ex + "] for vertex [" + vtx1 + "]");
                                        }
                                        Thread.sleep(200);
                                    }
                                }
                                if (retry) {
                                    log("*******#################******* [" + id + "][" + vtx + "][ Retry: " + k + "] Failed to update after Exception [" + ((tex != null) ? tex : "----") + "]");
                                }
                            }
                        }
                    } finally {
                        graph.shutdown();
                    }
                }
            } catch (Exception ex) {
                System.out.println("ID: [" + id + "]********** Exception " + ex + " \n\n");
                ex.printStackTrace();
            } finally {
                log("[" + id + "] Done................>>>>>>>>>>>>>>>>>>");
            }
        }
    };
    return th;
}
Also used : Vertex(com.tinkerpop.blueprints.Vertex) ODatabaseException(com.orientechnologies.orient.core.exception.ODatabaseException) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException) ODatabaseException(com.orientechnologies.orient.core.exception.ODatabaseException) OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OLocalClusterWrapperStrategy(com.orientechnologies.orient.server.distributed.impl.OLocalClusterWrapperStrategy) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException)

Example 20 with ONeedRetryException

use of com.orientechnologies.common.concur.ONeedRetryException in project orientdb by orientechnologies.

the class OETLPipeline method execute.

protected Object execute(final OExtractedItem source) {
    int retry = 0;
    do {
        try {
            Object current = source.payload;
            context.setVariable("extractedNum", source.num);
            context.setVariable("extractedPayload", source.payload);
            for (OTransformer t : transformers) {
                current = t.transform(current);
                if (current == null) {
                    if (logLevel == DEBUG) {
                        OLogManager.instance().warn(this, "Transformer [%s] returned null, skip rest of pipeline execution", t);
                        break;
                    }
                }
            }
            if (current != null)
                // LOAD
                loader.load(this, current, context);
            return current;
        } catch (ONeedRetryException e) {
            loader.rollback(this);
            retry++;
            processor.out(INFO, "Error in pipeline execution, retry = %d/%d (exception=%s)", retry, maxRetries, e);
        } catch (OETLProcessHaltedException e) {
            processor.out(ERROR, "Pipeline execution halted");
            processor.getStats().incrementErrors();
            loader.rollback(this);
            throw e;
        } catch (Exception e) {
            processor.out(ERROR, "Error in Pipeline execution: %s", e);
            processor.getStats().incrementErrors();
            if (!haltOnError) {
                return null;
            }
            loader.rollback(this);
            throw OException.wrapException(new OETLProcessHaltedException("Halt"), e);
        }
    } while (retry < maxRetries);
    return this;
}
Also used : ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException) OTransformer(com.orientechnologies.orient.etl.transformer.OTransformer) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException) OException(com.orientechnologies.common.exception.OException)

Aggregations

ONeedRetryException (com.orientechnologies.common.concur.ONeedRetryException)27 OException (com.orientechnologies.common.exception.OException)9 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)8 HazelcastException (com.hazelcast.core.HazelcastException)6 HazelcastInstanceNotActiveException (com.hazelcast.core.HazelcastInstanceNotActiveException)6 OOfflineNodeException (com.orientechnologies.common.concur.OOfflineNodeException)6 OIOException (com.orientechnologies.common.io.OIOException)6 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)6 ODistributedRedirectException (com.orientechnologies.orient.enterprise.channel.binary.ODistributedRedirectException)6 OCallable (com.orientechnologies.common.util.OCallable)5 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)4 ORecordId (com.orientechnologies.orient.core.id.ORecordId)4 ORecordDuplicatedException (com.orientechnologies.orient.core.storage.ORecordDuplicatedException)4 Vertex (com.tinkerpop.blueprints.Vertex)4 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)4 Callable (java.util.concurrent.Callable)4 OPlaceholder (com.orientechnologies.orient.core.db.record.OPlaceholder)3 ORecordNotFoundException (com.orientechnologies.orient.core.exception.ORecordNotFoundException)3 OLocalClusterWrapperStrategy (com.orientechnologies.orient.server.distributed.impl.OLocalClusterWrapperStrategy)3 IOException (java.io.IOException)3