Search in sources :

Example 16 with OConcurrentModificationException

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

the class OConflictManagementTest method testVersionStrategy.

public void testVersionStrategy() {
    database.setConflictStrategy("version");
    ODocument rootDoc = new ODocument().field("name", "Jay").save();
    ODocument copy = rootDoc.copy();
    rootDoc.field("name", "Jay1");
    rootDoc.save();
    copy.field("name", "Jay2");
    try {
        copy.save();
        Assert.assertTrue(false);
    } catch (OConcurrentModificationException e) {
    }
}
Also used : OConcurrentModificationException(com.orientechnologies.orient.core.exception.OConcurrentModificationException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 17 with OConcurrentModificationException

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

the class OClassIndexManager method onRecordBeforeDelete.

@Override
public RESULT onRecordBeforeDelete(final ODocument iDocument) {
    // Cache the transaction-provided value
    final int version = iDocument.getVersion();
    if (iDocument.fields() == 0 && iDocument.getIdentity().isPersistent()) {
        // FORCE LOADING OF CLASS+FIELDS TO USE IT AFTER ON onRecordAfterDelete METHOD
        iDocument.reload();
        if (// check for record version errors
        version > -1 && iDocument.getVersion() != version)
            if (OFastConcurrentModificationException.enabled())
                throw OFastConcurrentModificationException.instance();
            else
                throw new OConcurrentModificationException(iDocument.getIdentity(), iDocument.getVersion(), version, ORecordOperation.DELETED);
    }
    final OClass class_ = ODocumentInternal.getImmutableSchemaClass(iDocument);
    if (class_ != null) {
        final Collection<OIndex<?>> indexes = class_.getIndexes();
        final TreeMap<OIndex<?>, List<Object>> indexKeysMap = new TreeMap<OIndex<?>, List<Object>>();
        for (final OIndex<?> index : indexes) {
            if (index.getInternal() instanceof OIndexUnique) {
                OIndexRecorder indexRecorder = new OIndexRecorder((OIndexUnique) index.getInternal());
                addIndexEntry(iDocument, iDocument.getIdentity(), indexRecorder);
                indexKeysMap.put(index, indexRecorder.getAffectedKeys());
            }
        }
        if (noTx(iDocument)) {
            final List<Lock[]> locks = new ArrayList<Lock[]>(indexKeysMap.size());
            for (Map.Entry<OIndex<?>, List<Object>> entry : indexKeysMap.entrySet()) {
                final OIndexInternal<?> index = entry.getKey().getInternal();
                locks.add(index.lockKeysForUpdate(entry.getValue()));
            }
            lockedKeys.push(locks);
        }
    }
    return RESULT.RECORD_NOT_CHANGED;
}
Also used : OConcurrentModificationException(com.orientechnologies.orient.core.exception.OConcurrentModificationException) Lock(java.util.concurrent.locks.Lock) OClass(com.orientechnologies.orient.core.metadata.schema.OClass)

Example 18 with OConcurrentModificationException

use of com.orientechnologies.orient.core.exception.OConcurrentModificationException 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 OConcurrentModificationException

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

the class AbstractServerClusterMergeUpdateTest method executeTest.

private void executeTest(ODatabaseDocumentTx db0, ODatabaseDocumentTx db1) {
    db0.activateOnCurrentThread();
    // Event #0: Create new document with value 2L, and version 1
    ODocument doc = new ODocument("Paper").field(FIELD_NAME, 2L);
    db0.save(doc);
    ORID orid = doc.getIdentity().copy();
    // Event #1: Node 0 starts a transaction, and loads document with version 1 and value 2
    db0.begin();
    ODocument doc0 = db0.load(orid);
    // Event #2: Node 1 starts a transaction, loads the document with
    // version 1 and value 2, updates it to value 4, and commit(v2)
    {
        db1.activateOnCurrentThread();
        db1.begin();
        ODocument doc1 = db1.load(orid);
        doc1.field(FIELD_NAME, 4L);
        db1.save(doc1);
        db1.commit();
    }
    // Event #3: Node 1 starts a transaction, loads the document with
    // version 2 and value 4, updates it to value 5, and commit(v3)
    {
        db1.begin();
        ODocument doc1 = db1.load(orid);
        doc1.field(FIELD_NAME, 5L);
        db1.save(doc1);
        db1.commit();
    }
    // Event #4: Node 0 update his document (v1, 2) to value 5(v2), and commit.
    db0.activateOnCurrentThread();
    doc0.field(FIELD_NAME, 5L);
    db0.save(doc0);
    boolean anyConflictException = false;
    try {
        db0.commit();
    // Event #5: An exception SHOULD be raised, even though the value is the same, because the versions aren't.
    } catch (RuntimeException ex) {
        if (ex instanceof OConcurrentModificationException) {
            anyConflictException = true;
        } else {
            throw ex;
        }
    }
    Assert.assertTrue(anyConflictException);
}
Also used : ORID(com.orientechnologies.orient.core.id.ORID) OConcurrentModificationException(com.orientechnologies.orient.core.exception.OConcurrentModificationException) ODocument(com.orientechnologies.orient.core.record.impl.ODocument)

Example 20 with OConcurrentModificationException

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

the class ConcurrentDistributedUpdateTest method createWriter.

protected Callable<Void> createWriter(final int serverId, final int threadId, final String databaseURL) {
    return new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            final String id = serverId + "." + threadId;
            boolean isRunning = true;
            final OrientBaseGraph graph = new OrientGraph(databaseURL);
            try {
                String query = "select from Test where prop2='v2-1'";
                for (int i = 0; i < 100 && isRunning; i++) {
                    if ((i % 25) == 0) {
                        log("[" + id + "] Records Processed: [" + i + "]");
                    }
                    Iterable<Vertex> vtxs = graph.command(new OCommandSQL(query)).execute();
                    boolean update = true;
                    for (Vertex vtx : vtxs) {
                        if (update) {
                            update = true;
                            for (int k = 0; k < 10 && update; k++) {
                                OrientVertex vtx1 = (OrientVertex) vtx;
                                try {
                                    vtx1.setProperty("prop5", "prop55");
                                    graph.commit();
                                    // log("[" + id + "/" + i + "/" + k + "] OK!\n");
                                    break;
                                } catch (OConcurrentModificationException ex) {
                                    vtx1.reload();
                                } catch (ODistributedRecordLockedException ex) {
                                    log("[" + id + "/" + i + "/" + k + "] Distributed lock Exception " + ex + " for vertex " + vtx1 + " \n");
                                    //                    ex.printStackTrace();
                                    update = false;
                                    //                    isRunning = false;
                                    break;
                                } catch (Exception ex) {
                                    log("[" + id + "/" + i + "/" + k + "] Exception " + ex + " for vertex " + vtx1 + "\n\n");
                                    ex.printStackTrace();
                                    isRunning = false;
                                    break;
                                }
                            }
                            if (!isRunning)
                                break;
                        }
                    }
                }
            } catch (Exception ex) {
                System.out.println("ID: [" + id + "]********** Exception " + ex + " \n\n");
                ex.printStackTrace();
            } finally {
                log("[" + id + "] Done................>>>>>>>>>>>>>>>>>>");
                graph.shutdown();
                runningWriters.countDown();
            }
            Assert.assertTrue(isRunning);
            return null;
        }
    };
}
Also used : OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) Vertex(com.tinkerpop.blueprints.Vertex) ODistributedRecordLockedException(com.orientechnologies.orient.server.distributed.task.ODistributedRecordLockedException) OrientGraph(com.tinkerpop.blueprints.impls.orient.OrientGraph) OrientVertex(com.tinkerpop.blueprints.impls.orient.OrientVertex) OrientBaseGraph(com.tinkerpop.blueprints.impls.orient.OrientBaseGraph) OConcurrentModificationException(com.orientechnologies.orient.core.exception.OConcurrentModificationException) Callable(java.util.concurrent.Callable) ODistributedRecordLockedException(com.orientechnologies.orient.server.distributed.task.ODistributedRecordLockedException) OConcurrentModificationException(com.orientechnologies.orient.core.exception.OConcurrentModificationException) OCommandSQL(com.orientechnologies.orient.core.sql.OCommandSQL)

Aggregations

OConcurrentModificationException (com.orientechnologies.orient.core.exception.OConcurrentModificationException)40 ODocument (com.orientechnologies.orient.core.record.impl.ODocument)25 ODatabaseDocumentTx (com.orientechnologies.orient.core.db.document.ODatabaseDocumentTx)11 OIdentifiable (com.orientechnologies.orient.core.db.record.OIdentifiable)11 ORidBag (com.orientechnologies.orient.core.db.record.ridbag.ORidBag)11 OrientVertex (com.tinkerpop.blueprints.impls.orient.OrientVertex)8 ArrayList (java.util.ArrayList)8 Test (org.testng.annotations.Test)8 Vertex (com.tinkerpop.blueprints.Vertex)7 DatabaseAbstractTest (com.orientechnologies.DatabaseAbstractTest)6 Test (org.junit.Test)5 ORID (com.orientechnologies.orient.core.id.ORID)4 OCommandSQL (com.orientechnologies.orient.core.sql.OCommandSQL)4 OrientBaseGraph (com.tinkerpop.blueprints.impls.orient.OrientBaseGraph)4 OrientGraph (com.tinkerpop.blueprints.impls.orient.OrientGraph)4 ORecordNotFoundException (com.orientechnologies.orient.core.exception.ORecordNotFoundException)3 ONeedRetryException (com.orientechnologies.common.concur.ONeedRetryException)2 ODatabaseDocument (com.orientechnologies.orient.core.db.document.ODatabaseDocument)2 OClass (com.orientechnologies.orient.core.metadata.schema.OClass)2 ORecord (com.orientechnologies.orient.core.record.ORecord)2