Search in sources :

Example 6 with ONeedRetryException

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

the class DistributedDatabaseCRUDTest method startSQLUpdateThread.

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

        @Override
        public void run() {
            log("Starting runnable for sql update thread for property " + propertyValue);
            long st = System.currentTimeMillis();
            try {
                boolean isRunning = true;
                for (int i = 1; i < 10000000 && isRunning; i++) {
                    if ((i % 100) == 0) {
                        long et = System.currentTimeMillis();
                        log(" [" + id + "] Total Records Processed: [" + i + "] Time taken for [100] records: [" + (et - st) / 1000 + "] seconds");
                        st = System.currentTimeMillis();
                    }
                    OrientGraph graph = graphFactory.getTx();
                    if (!graph.getRawGraph().getURL().startsWith("remote:"))
                        Assert.assertTrue(graph.getVertexType("TestNode").getClusterSelection() instanceof OLocalClusterWrapperStrategy);
                    try {
                        boolean update = true;
                        boolean isException = false;
                        Exception tex = null;
                        String sql = "Update TestNode set prop5='" + String.valueOf(System.currentTimeMillis()) + "'" + ", prop-7='value7-1', prop-8='value8-1', prop-9='value9-1',prop-10='value10-1', prop11='value11-1'" + ", prop-07='value07-1', prop-08='value08-1', prop-09='value09-1',prop-010='value010-1', prop011='value011-1'" + ", prop12='vaue12-1', prop13='value13-1'" + ", updateTime='" + new Date().toString() + "' where property4='" + propertyValue + "'";
                        int k = 1;
                        for (; k <= 100 && update; k++) {
                            try {
                                graph.command(new OCommandSQL(sql)).execute();
                                if (isException) {
                                // log("********** [" + id + "][" + k + "] Update success after distributed lock Exception");
                                }
                                update = 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 + "][" + propertyValue + "][ Retry: " + k + "] OrientDB Exception [" + ex + "]");
                                        try {
                                            Thread.sleep(new Random().nextInt(500));
                                        } catch (InterruptedException e) {
                                            e.printStackTrace();
                                        }
                                    } else {
                                        log("[" + id + "][ Retry: " + k + "] Failed to update. OrientDB Exception [" + ex + "]");
                                    }
                                    isException = true;
                                } else {
                                    tex = ex;
                                    log("[" + id + "][" + k + "] Failed non OrientDB Exception [" + ex + "]");
                                }
                                if (update) {
                                    log("*******#################******* [" + id + "][ Retry: " + k + "] Failed to update after Exception [" + ((tex != null) ? tex : "----") + "] for vertex with property4='" + propertyValue + "'");
                                }
                            }
                        }
                    } 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 : 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 7 with ONeedRetryException

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

the class ServerClusterSchemaTest method executeTest.

@Override
protected void executeTest() throws Exception {
    for (int s = 0; s < SERVERS; ++s) {
        OrientGraphFactory factory = new OrientGraphFactory("plocal:target/server" + s + "/databases/" + getDatabaseName());
        OrientGraphNoTx g = factory.getNoTx();
        try {
            System.out.println("Creating vertex class Client" + s + " against server " + g + "...");
            OrientVertexType t = g.createVertexType("Client" + s);
            t.createProperty("name", OType.STRING).setMandatory(true);
            System.out.println("Creating vertex class Knows" + s + " against server " + g + "...");
            g.createEdgeType("Knows" + s);
        } finally {
            g.shutdown();
        }
    }
    for (int s = 0; s < SERVERS; ++s) {
        System.out.println("Checking vertices classes on server " + s + "...");
        OrientGraphFactory factory = new OrientGraphFactory("plocal:target/server" + s + "/databases/" + getDatabaseName());
        OrientGraphNoTx g = factory.getNoTx();
        try {
            for (int i = 0; i < SERVERS; ++i) {
                Assert.assertNotNull(g.getVertexType("Client" + i));
                Assert.assertNotNull(g.getEdgeType("Knows" + i));
            }
        } finally {
            g.shutdown();
        }
    }
    for (int s = 0; s < SERVERS; ++s) {
        System.out.println("Add vertices on server " + s + "...");
        OrientGraphFactory factory = new OrientGraphFactory("plocal:target/server" + s + "/databases/" + getDatabaseName());
        OrientGraphNoTx g = factory.getNoTx();
        try {
            for (int i = 0; i < SERVERS; ++i) {
                try {
                    final OrientVertex v = g.addVertex("class:" + "Client" + i);
                    Assert.assertTrue(false);
                } catch (OValidationException e) {
                // EXPECTED
                }
            }
        } finally {
            g.shutdown();
        }
    }
    for (int s = 0; s < SERVERS; ++s) {
        System.out.println("Add vertices in TX on server " + s + "...");
        OrientGraphFactory factory = new OrientGraphFactory("plocal:target/server" + s + "/databases/" + getDatabaseName());
        OrientGraph g = factory.getTx();
        try {
            for (int i = 0; i < SERVERS; ++i) {
                try {
                    final OrientVertex v = g.addVertex("class:" + "Client" + i);
                    g.commit();
                    Assert.assertTrue(false);
                } catch (ONeedRetryException e) {
                // EXPECTED
                } catch (OValidationException e) {
                // EXPECTED
                }
            }
        } finally {
            g.shutdown();
        }
    }
}
Also used : OValidationException(com.orientechnologies.orient.core.exception.OValidationException) OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException) OrientGraphFactory(com.tinkerpop.blueprints.impls.orient.OrientGraphFactory) OrientGraphNoTx(com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx) OrientVertexType(com.tinkerpop.blueprints.impls.orient.OrientVertexType) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex)

Example 8 with ONeedRetryException

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

the class IndexConcurrencyTest method deleteSubTree.

public static void deleteSubTree(String parentName) {
    ODatabaseDocumentTx db = new ODatabaseDocumentTx(url).open("admin", "admin");
    for (int i = 0; ; ++i) {
        try {
            final List<ODocument> result = db.command(new OCommandSQL("select from Person where name = '" + parentName + "'")).execute();
            ODocument parent = result.get(0);
            if (parent == null) {
                db.close();
                return;
            }
            db.begin();
            Collection<OIdentifiable> out = parent.field("out");
            if (out.size() > 0) {
                OIdentifiable edge = out.iterator().next();
                if (edge != null) {
                    out.remove(edge);
                    final List<OIdentifiable> result2 = db.command(new OCommandSQL("traverse out from " + edge.getIdentity())).execute();
                    for (OIdentifiable d : result2) {
                        db.delete(d.getIdentity());
                    }
                }
            }
            parent.save();
            db.commit();
            break;
        } catch (ONeedRetryException e) {
            System.out.println("Concurrency change, retry " + i);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    db.close();
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) OIdentifiable(com.orientechnologies.orient.core.db.record.OIdentifiable) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException) IOException(java.io.IOException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 9 with ONeedRetryException

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

the class OBaseWorkload method executeOperation.

protected List<OBaseWorkLoadContext> executeOperation(final ODatabaseIdentifier dbIdentifier, final OWorkLoadResult result, final OStressTesterSettings settings, final OCallable<Void, OBaseWorkLoadContext> callback) {
    if (result.total == 0)
        return null;
    final int concurrencyLevel = settings.concurrencyLevel;
    final int operationsPerTransaction = settings.operationsPerTransaction;
    final int totalPerThread = result.total / concurrencyLevel;
    final int totalPerLastThread = totalPerThread + result.total % concurrencyLevel;
    final Long[] operationTiming = new Long[result.total];
    final List<OBaseWorkLoadContext> contexts = new ArrayList<OBaseWorkLoadContext>(concurrencyLevel);
    final Thread[] thread = new Thread[concurrencyLevel];
    for (int t = 0; t < concurrencyLevel; ++t) {
        final int currentThread = t;
        final OBaseWorkLoadContext context = getContext();
        contexts.add(context);
        thread[t] = new Thread(new Runnable() {

            @Override
            public void run() {
                context.threadId = currentThread;
                context.totalPerThread = context.threadId < concurrencyLevel - 1 ? totalPerThread : totalPerLastThread;
                context.init(dbIdentifier, operationsPerTransaction);
                init(context);
                try {
                    final int startIdx = totalPerThread * context.threadId;
                    final AtomicInteger operationsExecutedInTx = new AtomicInteger();
                    for (final AtomicInteger i = new AtomicInteger(); i.get() < context.totalPerThread; i.incrementAndGet()) {
                        ODatabaseDocumentTx.executeWithRetries(new OCallable<Object, Integer>() {

                            @Override
                            public Object call(final Integer retry) {
                                if (retry > 0) {
                                    i.addAndGet(operationsExecutedInTx.get() * -1);
                                    if (i.get() < 0)
                                        i.set(0);
                                    operationsExecutedInTx.set(0);
                                }
                                context.currentIdx = startIdx + i.get();
                                final long startOp = System.nanoTime();
                                try {
                                    try {
                                        return callback.call(context);
                                    } finally {
                                        operationsExecutedInTx.incrementAndGet();
                                        if (operationsPerTransaction > 0 && (i.get() + 1) % operationsPerTransaction == 0 || i.get() == context.totalPerThread - 1) {
                                            commitTransaction(context);
                                            operationsExecutedInTx.set(0);
                                            beginTransaction(context);
                                        }
                                    }
                                } catch (ONeedRetryException e) {
                                    result.conflicts.incrementAndGet();
                                    manageNeedRetryException(context, e);
                                    if (operationsPerTransaction > 0)
                                        beginTransaction(context);
                                    throw e;
                                } catch (Exception e) {
                                    errors.add(e.toString());
                                    if (errors.size() > MAX_ERRORS) {
                                        e.printStackTrace();
                                        return null;
                                    }
                                } finally {
                                    operationTiming[context.currentIdx] = System.nanoTime() - startOp;
                                }
                                return null;
                            }
                        }, 10);
                        if (settings.delay > 0)
                            try {
                                Thread.sleep(settings.delay);
                            } catch (InterruptedException e) {
                                Thread.currentThread().interrupt();
                            }
                    }
                    if (operationsPerTransaction > 0)
                        commitTransaction(context);
                } finally {
                    context.close();
                }
            }
        });
    }
    final long startTime = System.currentTimeMillis();
    // START ALL THE THREADS
    for (int t = 0; t < concurrencyLevel; ++t) {
        thread[t].start();
    }
    // WAIT FOR ALL THE THREADS
    for (int t = 0; t < concurrencyLevel; ++t) {
        try {
            thread[t].join();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
    // STOP THE COUNTER
    result.totalTime = System.currentTimeMillis() - startTime;
    Arrays.sort(operationTiming);
    result.throughputAvgNs = (int) (result.totalTime * 1000000 / operationTiming.length);
    // COMPUTE THE TOTAL COST OF OPERATIONS ONLY
    result.totalTimeOperationsNs = 0;
    for (long l : operationTiming) result.totalTimeOperationsNs += l;
    result.latencyMinNs = operationTiming[0];
    result.latencyMaxNs = operationTiming[operationTiming.length - 1];
    result.latencyAvgNs = (int) (result.totalTimeOperationsNs / operationTiming.length);
    result.latencyPercentileAvg = getPercentile(operationTiming, result.latencyAvgNs);
    result.latencyPercentile99Ns = operationTiming[(int) (operationTiming.length * 99f / 100f)];
    result.latencyPercentile99_9Ns = operationTiming[(int) (operationTiming.length * 99.9f / 100f)];
    return contexts;
}
Also used : ArrayList(java.util.ArrayList) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger)

Example 10 with ONeedRetryException

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

the class ODistributedStorage method readRecord.

public OStorageOperationResult<ORawBuffer> readRecord(final ORecordId iRecordId, final String iFetchPlan, final boolean iIgnoreCache, final boolean prefetchRecords, final ORecordCallback<ORawBuffer> iCallback) {
    final ORawBuffer memCopy = localDistributedDatabase.getRecordIfLocked(iRecordId);
    if (memCopy != null)
        return new OStorageOperationResult<ORawBuffer>(memCopy);
    try {
        final String clusterName = getClusterNameByRID(iRecordId);
        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.readRecord(iRecordId, iFetchPlan, iIgnoreCache, prefetchRecords, iCallback);
                }
            });
        }
        // DISTRIBUTE IT
        final ODistributedResponse response = dManager.sendRequest(getName(), Collections.singleton(clusterName), nodes, new OReadRecordTask(iRecordId), dManager.getNextMessageIdCounter(), EXECUTION_MODE.RESPONSE, null, null);
        final Object dResult = response != null ? response.getPayload() : null;
        if (dResult instanceof ONeedRetryException)
            throw (ONeedRetryException) dResult;
        else if (dResult instanceof Exception)
            throw OException.wrapException(new ODistributedException("Error on execution distributed read record"), (Exception) dResult);
        return new OStorageOperationResult<ORawBuffer>((ORawBuffer) dResult);
    } 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, iRecordId);
        // 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)

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