Search in sources :

Example 1 with ONeedRetryException

use of com.orientechnologies.common.concur.ONeedRetryException in project jnosql-diana-driver by eclipse.

the class DefaultOrientDBDocumentCollectionManager method insert.

@Override
public DocumentEntity insert(DocumentEntity entity) {
    requireNonNull(entity, "Entity is required");
    try (ODatabaseDocumentTx tx = pool.acquire()) {
        ODocument document = new ODocument(entity.getName());
        toMap(entity).forEach(document::field);
        try {
            tx.save(document);
        } catch (ONeedRetryException e) {
            document = tx.reload(document);
            Map<String, Object> entityValues = toMap(entity);
            entityValues.put(OrientDBConverter.VERSION_FIELD, document.getVersion());
            entityValues.forEach(document::field);
            tx.save(document);
        }
        updateEntity(entity, document);
        return entity;
    }
}
Also used : ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException) ODatabaseDocumentTx(com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx) Map(java.util.Map) OrientDBConverter.toMap(org.jnosql.diana.orientdb.document.OrientDBConverter.toMap) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 2 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 3 with ONeedRetryException

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

the class ServerClusterAsyncGraphTest method executeTest.

@Override
protected void executeTest() throws Exception {
    {
        OrientGraphFactory factory = new OrientGraphFactory("plocal:target/server0/databases/" + getDatabaseName());
        OrientGraphNoTx g = factory.getNoTx();
        try {
            g.createVertexType("Post");
            g.createVertexType("User");
            g.createEdgeType("Own");
            g.addVertex("class:User");
            g.command(new OCommandSQL("insert into Post (content, timestamp) values('test', 1)")).execute();
        } finally {
            g.shutdown();
        }
    }
    // CHECK VERTEX CREATION ON ALL THE SERVERS
    for (int s = 0; s < SERVERS; ++s) {
        OrientGraphFactory factory2 = new OrientGraphFactory("plocal:target/server" + s + "/databases/" + getDatabaseName());
        OrientGraphNoTx g2 = factory2.getNoTx();
        try {
            Iterable<OrientVertex> result = g2.command(new OCommandSQL("select from Post")).execute();
            Assert.assertTrue(result.iterator().hasNext());
            Assert.assertNotNull(result.iterator().next());
        } finally {
            g2.shutdown();
        }
    }
    {
        OrientGraphFactory factory = new OrientGraphFactory("plocal:target/server0/databases/" + getDatabaseName());
        OrientGraphNoTx g = factory.getNoTx();
        try {
            g.command(new OCommandSQL("create edge Own from (select from User) to (select from Post)").onAsyncReplicationError(new OAsyncReplicationError() {

                @Override
                public ACTION onAsyncReplicationError(Throwable iException, int iRetry) {
                    return iException instanceof ONeedRetryException && iRetry <= 3 ? ACTION.RETRY : ACTION.IGNORE;
                }
            })).execute();
        } finally {
            g.shutdown();
        }
    }
    Thread.sleep(1000);
    // CHECK VERTEX CREATION ON ALL THE SERVERS
    for (int s = 0; s < SERVERS; ++s) {
        OrientGraphFactory factory2 = new OrientGraphFactory("plocal:target/server" + s + "/databases/" + getDatabaseName());
        OrientGraphNoTx g2 = factory2.getNoTx();
        try {
            Iterable<OrientVertex> result = g2.command(new OCommandSQL("select from Own")).execute();
            Assert.assertTrue(result.iterator().hasNext());
            Assert.assertNotNull(result.iterator().next());
            result = g2.command(new OCommandSQL("select from Post")).execute();
            Assert.assertTrue(result.iterator().hasNext());
            final OrientVertex v = result.iterator().next();
            Assert.assertNotNull(v);
            final Iterable<Edge> inEdges = v.getEdges(Direction.IN);
            Assert.assertTrue(inEdges.iterator().hasNext());
            Assert.assertNotNull(inEdges.iterator().next());
            result = g2.command(new OCommandSQL("select from User")).execute();
            Assert.assertTrue(result.iterator().hasNext());
            final OrientVertex v2 = result.iterator().next();
            Assert.assertNotNull(v2);
            final Iterable<Edge> outEdges = v2.getEdges(Direction.OUT);
            Assert.assertTrue(outEdges.iterator().hasNext());
            Assert.assertNotNull(outEdges.iterator().next());
        } finally {
            g2.shutdown();
        }
    }
}
Also used : OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL) OAsyncReplicationError(com.orientechnologies.orient.core.replication.OAsyncReplicationError) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException) OrientGraphFactory(com.tinkerpop.blueprints.impls.orient.OrientGraphFactory) OrientGraphNoTx(com.tinkerpop.blueprints.impls.orient.OrientGraphNoTx) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) Edge(com.tinkerpop.blueprints.Edge)

Example 4 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 5 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)

Aggregations

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