Search in sources :

Example 26 with ONeedRetryException

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

the class OGraphImporterMTAPITest method main.

public static void main(String[] args) throws IOException, InterruptedException {
    OGlobalConfiguration.ENVIRONMENT_LOCK_MANAGER_CONCURRENCY_LEVEL.setValue(64);
    // String dbUrl = "memory:amazonReviews";
    final String dbUrl = "plocal:/temp/databases/amazonReviews";
    final File f = new File("/temp/databases/amazonReviews");
    if (f.exists())
        OFileUtils.deleteRecursively(f);
    final OrientGraph roGraph = new OrientGraph(dbUrl, "admin", "admin");
    final OrientGraphNoTx graph = new OrientGraphNoTx(dbUrl, "admin", "admin");
    OrientVertexType user = graph.createVertexType("User", 64);
    user.createProperty("uid", OType.STRING);
    user.createIndex("User.uid", OClass.INDEX_TYPE.UNIQUE.toString(), (OProgressListener) null, (ODocument) null, "AUTOSHARDING", new String[] { "uid" });
    OrientVertexType product = graph.createVertexType("Product", 64);
    product.createProperty("uid", OType.STRING);
    product.createIndex("Product.uid", OClass.INDEX_TYPE.UNIQUE.toString(), (OProgressListener) null, (ODocument) null, "AUTOSHARDING", new String[] { "uid" });
    graph.createEdgeType("Reviewed");
    final File file = new File("/Users/luca/Downloads/ratings_Books.csv");
    final BufferedReader br = new BufferedReader(new FileReader(file));
    final AtomicLong retry = new AtomicLong();
    Orient.instance().scheduleTask(new TimerTask() {

        @Override
        public void run() {
            roGraph.makeActive();
            final long vertexCount = roGraph.countVertices();
            final long edgeCount = roGraph.countEdges();
            System.out.println(String.format("%d vertices=%d %d/sec edges=%d %d/sec retry=%d", row, vertexCount, ((vertexCount - lastVertexCount) * 1000 / 2000), edgeCount, ((edgeCount - lastEdgeCount) * 1000 / 2000), retry.get()));
            lastVertexCount = vertexCount;
            lastEdgeCount = edgeCount;
        }
    }, 2000, 2000);
    final ArrayBlockingQueue<String> queue = new ArrayBlockingQueue<String>(10000);
    final Thread[] threads = new Thread[parallel];
    for (int i = 0; i < parallel; ++i) {
        threads[i] = new Thread() {

            @Override
            public void run() {
                final OrientGraph localGraph = new OrientGraph(dbUrl, "admin", "admin", false);
                final OIndex<?> userIndex = localGraph.getRawGraph().getMetadata().getIndexManager().getIndex("User.uid");
                final OIndex<?> productIndex = localGraph.getRawGraph().getMetadata().getIndexManager().getIndex("Product.uid");
                localGraph.begin();
                for (int i = 0; ; ++i) {
                    final String line;
                    try {
                        line = queue.take();
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                        return;
                    }
                    final String[] parts = line.split(",");
                    if (parts.length != 4) {
                        // SKIP IT
                        System.out.print("Skipped invalid line " + i + ": " + line);
                        continue;
                    }
                    Map<String, Object> properties = new HashMap<String, Object>();
                    properties.put("score", new Float(parts[2]).intValue());
                    properties.put("date", Long.parseLong(parts[3]));
                    for (int localRetry = 0; localRetry < 100; ++localRetry) {
                        try {
                            final Object k1 = userIndex.get(parts[0]);
                            OrientVertex v1;
                            if (k1 == null) {
                                v1 = localGraph.addVertex("class:User", "uid", parts[0]);
                            } else
                                v1 = localGraph.getVertex(k1);
                            final Object k2 = productIndex.get(parts[1]);
                            OrientVertex v2;
                            if (k2 == null) {
                                v2 = localGraph.addVertex("class:Product", "uid", parts[1]);
                            } else
                                v2 = localGraph.getVertex(k2);
                            final OrientEdge edge = localGraph.addEdge(null, v1, v2, "Reviewed");
                            edge.setProperties(properties);
                            if (i % 2 == 0) {
                                localGraph.commit();
                                localGraph.begin();
                            }
                            break;
                        } catch (ONeedRetryException e) {
                            // RETRY
                            retry.incrementAndGet();
                        } catch (ORecordDuplicatedException e) {
                            // RETRY
                            retry.incrementAndGet();
                        }
                    }
                }
            }
        };
    }
    for (int i = 0; i < parallel; ++i) threads[i].start();
    try {
        for (String line; (line = br.readLine()) != null; ) {
            row++;
            queue.put(line);
        }
    } finally {
        br.close();
    }
    for (int i = 0; i < parallel; ++i) threads[i].join();
    graph.shutdown();
    roGraph.shutdown();
}
Also used : OIndex(com.orientechnologies.orient.core.index.OIndex) TimerTask(java.util.TimerTask) ArrayBlockingQueue(java.util.concurrent.ArrayBlockingQueue) FileReader(java.io.FileReader) AtomicLong(java.util.concurrent.atomic.AtomicLong) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException) ORecordDuplicatedException(com.orientechnologies.orient.core.storage.ORecordDuplicatedException) BufferedReader(java.io.BufferedReader) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map)

Example 27 with ONeedRetryException

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

the class IndexConcurrencyTest method addSubTree.

public static void addSubTree(String parentName, char startLetter) {
    ODatabaseDocumentTx db = new ODatabaseDocumentTx(url).open("admin", "admin");
    for (int i = 0; ; ++i) {
        try {
            ODocument parent;
            final List<ODocument> result = db.command(new OCommandSQL("select from Person where name = '" + parentName + "'")).execute();
            parent = result.get(0);
            if (parent == null) {
                db.close();
                return;
            }
            String newName = parentName;
            newName += Character.toString(startLetter);
            StringBuilder newIdentifier = new StringBuilder(newName);
            newIdentifier.setCharAt(0, 'B');
            db.begin();
            PersonTree tree = new PersonTree();
            tree.SetRoot(parent);
            ODocument child = tree.AddChild(parent, newName, newIdentifier.toString());
            buildTree(tree, child.getIdentity(), newName, subnodes, depth - 1, startLetter);
            db.commit();
            break;
        } catch (ONeedRetryException e) {
            System.out.println("Concurrency change, retry " + i);
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    // printPersons("After addSubTree", db);
    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) ONeedRetryException(com.orientechnologies.common.concur.ONeedRetryException) IOException(java.io.IOException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

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