use of com.orientechnologies.orient.core.storage.ORecordDuplicatedException in project orientdb by orientechnologies.
the class DistributedConfigReloadTest method startCreateDeleteVertex.
private Runnable startCreateDeleteVertex(final int id, final OrientGraphFactory graphFactory, final String className) {
Runnable th = new Runnable() {
@Override
public void run() {
log("Starting runnable to create index " + className);
long st = System.currentTimeMillis();
OrientGraph graph = graphFactory.getTx();
boolean isSelectSuccessful = true;
try {
boolean isRunning = true;
for (int j = 0; j < 1000000; j++) {
isSelectSuccessful = true;
String sql = "SELECT FROM " + className;
int deleteErrorCounter = 0;
try {
graph.command(new OCommandSQL(sql)).execute();
Iterable<Vertex> vtxs = graph.command(new OCommandSQL(sql)).execute();
for (Vertex vtx : vtxs) {
boolean needRetry = true;
for (int i = 0; i < 10 && needRetry; i++) {
try {
vtx.remove();
graph.commit();
needRetry = false;
} catch (ONeedRetryException ex) {
try {
((OrientElement) vtx).reload();
} catch (ORecordNotFoundException e) {
// BY LUCA
log("[" + id + "] Caught [" + e + "] during reload because the record was already deleted, no errors just go ahead");
}
} catch (ORecordNotFoundException e) {
// BY LUCA
log("[" + id + "] Caught [" + e + "] because the record was already deleted, no errors just go ahead");
} catch (Exception ex) {
log("[" + j + "] Failed to delete vertex [" + className + "] Vertex: [" + vtx + "] Property 1: [" + vtx.getProperty("property1") + "] Error: [" + ex + "]");
deleteErrorCounter++;
needRetry = false;
}
}
}
log(" [" + id + "] Delete vertex : [" + j + "] [" + className + "]");
} catch (Exception ex) {
log("***************** [" + id + "] Failed to select vertex [" + className + "][" + ex + "]");
isSelectSuccessful = false;
}
if (isSelectSuccessful) {
graph.command(new OCommandSQL(sql)).execute();
Iterable<Vertex> vtxs = graph.command(new OCommandSQL(sql)).execute();
ArrayList<String> vtxList = new ArrayList();
for (Vertex vtx : vtxs) {
vtxList.add(vtx.toString());
}
if (vtxList.size() > 0) {
log("########## [" + id + "] Records present after delete vertex [" + className + "] Error on delete [" + deleteErrorCounter + "] List: " + vtxList + "");
} else {
log("########## [" + id + "] Records removed after delete vertex [" + className + "] Error on delete [" + deleteErrorCounter + "]");
}
boolean showException = true;
int counter = 0;
for (int i = 1; i < 2000 && isRunning; i++) {
if ((i % 2000) == 0) {
long et = System.currentTimeMillis();
log(" [" + id + "] Total Records Processed: [" + i + "] Time taken for [2000] records: [" + (et - st) / 2000 + "] seconds");
st = System.currentTimeMillis();
}
Vertex vertex = graph.addVertex("class:" + className);
try {
vertex.setProperty("property1", "value-" + id + "-" + i);
vertex.setProperty("property2", "value2-" + (System.currentTimeMillis() + "-" + i));
vertex.setProperty("property3", "value3-" + i);
vertex.setProperty("property4", "value4-1");
vertex.setProperty("prop-6", "value6-" + i);
vertex.setProperty("prop-7", "value7-" + i);
vertex.setProperty("prop-8", "value7-1");
vertex.setProperty("prop-9", "value7-1");
vertex.setProperty("prop-10", "value7-1");
vertex.setProperty("prop11", "value7-1");
vertex.setProperty("prop12", "value7-1");
vertex.setProperty("prop13", "value7-1");
vertex.setProperty("prop14", System.currentTimeMillis());
vertex.setProperty("prop15", System.currentTimeMillis());
graph.commit();
} catch (ONeedRetryException ex) {
if (ex instanceof ONeedRetryException || ex.getCause() instanceof ONeedRetryException) {
log("[" + id + "] OrientDB Retry Exception [" + ex + "]");
} else {
log("[" + id + "] OrientDB Exception [" + ex + "]");
}
if (!(ex instanceof ODistributedConfigurationChangedException || ex.getCause() instanceof ODistributedConfigurationChangedException)) {
//reloadVertex(vertex, ex);
} else {
log("[" + id + "] ODistributedConfigurationChangedException {} while updating vertex " + vertex);
}
} catch (ORecordDuplicatedException ex) {
// BY LUCA
log("[" + id + "] Caught [" + ex + "], no errors just go ahead");
} catch (ODistributedException ex) {
if (ex.getCause() instanceof ONeedRetryException) {
log("[" + id + "] OrientDB Retry Exception [" + ex + "]");
} else {
log("[" + id + "] OrientDB Exception [" + ex + "]");
}
if (!(ex.getCause() instanceof ODistributedConfigurationChangedException)) {
//reloadVertex(vertex, ex);
} else {
log("[" + id + "] ODistributedConfigurationChangedException {} while updating vertex " + vertex);
}
} catch (Exception ex) {
if (showException) {
log("[" + id + "] Failed to create record Exception [" + ex + "]");
showException = false;
counter++;
}
}
}
log("************ [" + id + "] Total number of errors: [" + counter + "] Delete error counter:[" + deleteErrorCounter + "]");
} else {
log("##################### [" + id + "] Select failed. Skipping create..");
}
}
} finally {
graph.shutdown();
}
}
};
return th;
}
use of com.orientechnologies.orient.core.storage.ORecordDuplicatedException in project orientdb by orientechnologies.
the class OIndexOneValue method checkEntry.
@Override
public ODocument checkEntry(final OIdentifiable record, Object key) {
key = getCollatingValue(key);
final ODatabase database = getDatabase();
final boolean txIsActive = database.getTransaction().isActive();
if (!txIsActive)
keyLockManager.acquireSharedLock(key);
try {
// CHECK IF ALREADY EXIST
final OIdentifiable indexedRID = get(key);
if (indexedRID != null && !indexedRID.getIdentity().equals(record.getIdentity())) {
final Boolean mergeSameKey = metadata != null && (Boolean) metadata.field(OIndex.MERGE_KEYS);
if (mergeSameKey != null && mergeSameKey)
return (ODocument) indexedRID.getRecord();
else
throw new ORecordDuplicatedException(String.format("Cannot index record %s: found duplicated key '%s' in index '%s' previously assigned to the record %s", record, key, getName(), indexedRID), getName(), indexedRID.getIdentity());
}
return null;
} finally {
if (!txIsActive)
keyLockManager.releaseSharedLock(key);
}
}
use of com.orientechnologies.orient.core.storage.ORecordDuplicatedException in project orientdb by orientechnologies.
the class OCommandExecutorScript method executeSQLScript.
protected Object executeSQLScript(final String iText, final ODatabaseDocument db) throws IOException {
Object lastResult = null;
int maxRetry = 1;
context.setVariable("transactionRetries", 0);
context.setVariable("parentQuery", this);
for (int retry = 1; retry <= maxRetry; retry++) {
try {
try {
int txBegunAtLine = -1;
int txBegunAtPart = -1;
lastResult = null;
int nestedLevel = 0;
int skippingScriptsAtNestedLevel = -1;
final BufferedReader reader = new BufferedReader(new StringReader(iText));
int line = 0;
int linePart = 0;
String lastLine;
boolean txBegun = false;
for (; line < txBegunAtLine; ++line) // SKIP PREVIOUS COMMAND AND JUMP TO THE BEGIN IF ANY
reader.readLine();
for (; (lastLine = reader.readLine()) != null; ++line) {
lastLine = lastLine.trim();
// this block is here (and not below, with the other conditions)
// just because of the smartSprit() that does not parse correctly a single bracket
// final List<String> lineParts = OStringSerializerHelper.smartSplit(lastLine, ';', true);
final List<String> lineParts = splitBySemicolon(lastLine);
if (line == txBegunAtLine)
// SKIP PREVIOUS COMMAND PART AND JUMP TO THE BEGIN IF ANY
linePart = txBegunAtPart;
else
linePart = 0;
boolean breakReturn = false;
for (; linePart < lineParts.size(); ++linePart) {
final String lastCommand = lineParts.get(linePart);
if (isIfCondition(lastCommand)) {
nestedLevel++;
if (skippingScriptsAtNestedLevel >= 0) {
// I'm in an (outer) IF that did not match the condition
continue;
}
boolean ifResult = evaluateIfCondition(lastCommand);
if (!ifResult) {
// if does not match the condition, skip all the inner statements
skippingScriptsAtNestedLevel = nestedLevel;
}
continue;
} else if (lastCommand.equals("}")) {
nestedLevel--;
if (skippingScriptsAtNestedLevel > nestedLevel) {
skippingScriptsAtNestedLevel = -1;
}
continue;
} else if (skippingScriptsAtNestedLevel >= 0) {
// I'm in an IF that did not match the condition
continue;
} else if (OStringSerializerHelper.startsWithIgnoreCase(lastCommand, "let ")) {
lastResult = executeLet(lastCommand, db);
} else if (OStringSerializerHelper.startsWithIgnoreCase(lastCommand, "begin")) {
if (txBegun)
throw new OCommandSQLParsingException("Transaction already begun");
if (db.getTransaction().isActive())
// COMMIT ANY ACTIVE TX
db.commit();
txBegun = true;
txBegunAtLine = line;
txBegunAtPart = linePart;
db.begin();
if (lastCommand.length() > "begin ".length()) {
String next = lastCommand.substring("begin ".length()).trim();
if (OStringSerializerHelper.startsWithIgnoreCase(next, "isolation ")) {
next = next.substring("isolation ".length()).trim();
db.getTransaction().setIsolationLevel(OTransaction.ISOLATION_LEVEL.valueOf(next.toUpperCase()));
}
}
} else if ("rollback".equalsIgnoreCase(lastCommand)) {
if (!txBegun)
throw new OCommandSQLParsingException("Transaction not begun");
db.rollback();
txBegun = false;
txBegunAtLine = -1;
txBegunAtPart = -1;
} else if (OStringSerializerHelper.startsWithIgnoreCase(lastCommand, "commit")) {
if (txBegunAtLine < 0)
throw new OCommandSQLParsingException("Transaction not begun");
if (retry == 1 && lastCommand.length() > "commit ".length()) {
// FIRST CYCLE: PARSE RETRY TIMES OVERWRITING DEFAULT = 1
String next = lastCommand.substring("commit ".length()).trim();
if (OStringSerializerHelper.startsWithIgnoreCase(next, "retry ")) {
next = next.substring("retry ".length()).trim();
maxRetry = Integer.parseInt(next);
}
}
db.commit();
txBegun = false;
txBegunAtLine = -1;
txBegunAtPart = -1;
} else if (OStringSerializerHelper.startsWithIgnoreCase(lastCommand, "sleep ")) {
executeSleep(lastCommand);
} else if (OStringSerializerHelper.startsWithIgnoreCase(lastCommand, "console.log ")) {
executeConsoleLog(lastCommand, db);
} else if (OStringSerializerHelper.startsWithIgnoreCase(lastCommand, "console.output ")) {
executeConsoleOutput(lastCommand, db);
} else if (OStringSerializerHelper.startsWithIgnoreCase(lastCommand, "console.error ")) {
executeConsoleError(lastCommand, db);
} else if (OStringSerializerHelper.startsWithIgnoreCase(lastCommand, "return ")) {
lastResult = getValue(lastCommand.substring("return ".length()), db);
// END OF SCRIPT
breakReturn = true;
break;
} else if (lastCommand != null && lastCommand.length() > 0)
lastResult = executeCommand(lastCommand, db);
}
if (breakReturn) {
break;
}
}
} catch (RuntimeException ex) {
if (db.getTransaction().isActive())
db.rollback();
throw ex;
}
// COMPLETED
break;
} catch (OTransactionException e) {
// THIS CASE IS ON UPSERT
context.setVariable("retries", retry);
getDatabase().getLocalCache().clear();
if (retry >= maxRetry)
throw e;
waitForNextRetry();
} catch (ORecordDuplicatedException e) {
// THIS CASE IS ON UPSERT
context.setVariable("retries", retry);
getDatabase().getLocalCache().clear();
if (retry >= maxRetry)
throw e;
waitForNextRetry();
} catch (ORecordNotFoundException e) {
// THIS CASE IS ON UPSERT
context.setVariable("retries", retry);
getDatabase().getLocalCache().clear();
if (retry >= maxRetry)
throw e;
} catch (ONeedRetryException e) {
context.setVariable("retries", retry);
getDatabase().getLocalCache().clear();
if (retry >= maxRetry)
throw e;
waitForNextRetry();
}
}
return lastResult;
}
use of com.orientechnologies.orient.core.storage.ORecordDuplicatedException in project orientdb by orientechnologies.
the class AsyncIndexRemoteTest method dbClient1.
protected void dbClient1() {
OrientBaseGraph graph = new OrientGraphNoTx(getRemoteURL());
try {
graph.command(new OCommandSQL("create class SMS")).execute();
graph.command(new OCommandSQL("create property SMS.type string")).execute();
graph.command(new OCommandSQL("create property SMS.lang string")).execute();
graph.command(new OCommandSQL("create property SMS.source integer")).execute();
graph.command(new OCommandSQL("create property SMS.content string")).execute();
graph.command(new OCommandSQL("alter property SMS.lang min 2")).execute();
graph.command(new OCommandSQL("alter property SMS.lang max 2")).execute();
graph.command(new OCommandSQL("create index sms_keys ON SMS (type, lang) unique")).execute();
graph.command(new OCommandSQL("insert into sms (type, lang, source, content) values ( 'notify', 'en', 1, 'This is a test')")).execute();
try {
graph.command(new OCommandSQL("insert into sms (type, lang, source, content) values ( 'notify', 'en', 1, 'This is a test')")).execute();
Assert.fail("violated unique index was not raised");
} catch (ORecordDuplicatedException e) {
}
final Iterable<OrientVertex> result = graph.command(new OSQLSynchQuery<OrientVertex>("select count(*) from SMS")).execute();
Assert.assertEquals(1, ((Number) result.iterator().next().getProperty("count")).intValue());
} catch (Throwable e) {
if (exceptionInThread == null) {
exceptionInThread = e;
}
} finally {
OLogManager.instance().info(this, "Shutting down db1");
graph.shutdown();
}
// CHECK ON THE 2ND NODE
OrientBaseGraph graph2 = new OrientGraphNoTx(getRemoteURL2());
try {
try {
graph2.command(new OCommandSQL("insert into sms (type, lang, source, content) values ( 'notify', 'en', 1, 'This is a test')")).execute();
Assert.fail("violated unique index was not raised");
} catch (ORecordDuplicatedException e) {
}
final Iterable<OrientVertex> result = graph2.command(new OSQLSynchQuery<OrientVertex>("select count(*) from SMS")).execute();
Assert.assertEquals(1, ((Number) result.iterator().next().getProperty("count")).intValue());
} catch (Throwable e) {
if (exceptionInThread == null) {
exceptionInThread = e;
}
} finally {
OLogManager.instance().info(this, "Shutting down db2");
graph2.shutdown();
}
// CHECK ON THE 2ND NODE
OrientBaseGraph graph3 = new OrientGraphNoTx(getRemoteURL3());
try {
try {
graph3.command(new OCommandSQL("insert into sms (type, lang, source, content) values ( 'notify', 'en', 1, 'This is a test')")).execute();
Assert.fail("violated unique index was not raised");
} catch (ORecordDuplicatedException e) {
}
final Iterable<OrientVertex> result = graph3.command(new OSQLSynchQuery<OrientVertex>("select count(*) from SMS")).execute();
Assert.assertEquals(1, ((Number) result.iterator().next().getProperty("count")).intValue());
} catch (Throwable e) {
if (exceptionInThread == null) {
exceptionInThread = e;
}
} finally {
OLogManager.instance().info(this, "Shutting down db3");
graph3.shutdown();
}
}
use of com.orientechnologies.orient.core.storage.ORecordDuplicatedException in project orientdb by orientechnologies.
the class AsyncIndexTest method dbClient1.
protected void dbClient1() {
OrientBaseGraph graph = new OrientGraphNoTx(getLocalURL());
try {
graph.command(new OCommandSQL("create class SMS")).execute();
graph.command(new OCommandSQL("create property SMS.type string")).execute();
graph.command(new OCommandSQL("create property SMS.lang string")).execute();
graph.command(new OCommandSQL("create property SMS.source integer")).execute();
graph.command(new OCommandSQL("create property SMS.content string")).execute();
graph.command(new OCommandSQL("alter property SMS.lang min 2")).execute();
graph.command(new OCommandSQL("alter property SMS.lang max 2")).execute();
graph.command(new OCommandSQL("create index sms_keys ON SMS (type, lang) unique")).execute();
graph.command(new OCommandSQL("insert into sms (type, lang, source, content) values ( 'notify', 'en', 1, 'This is a test')")).execute();
try {
graph.command(new OCommandSQL("insert into sms (type, lang, source, content) values ( 'notify', 'en', 1, 'This is a test')")).execute();
Assert.fail("violated unique index was not raised");
} catch (ORecordDuplicatedException e) {
}
final Iterable<OrientVertex> result = graph.command(new OSQLSynchQuery<OrientVertex>("select count(*) from SMS")).execute();
Assert.assertEquals(1, ((Number) result.iterator().next().getProperty("count")).intValue());
} catch (Throwable e) {
if (exceptionInThread == null) {
exceptionInThread = e;
}
} finally {
OLogManager.instance().info(this, "Shutting down db1");
graph.shutdown();
}
// CHECK ON THE OTHER NODE
OrientBaseGraph graph2 = new OrientGraphNoTx(getLocalURL2());
try {
try {
graph2.command(new OCommandSQL("insert into sms (type, lang, source, content) values ( 'notify', 'en', 1, 'This is a test')")).execute();
Assert.fail("violated unique index was not raised");
} catch (ORecordDuplicatedException e) {
}
final Iterable<OrientVertex> result = graph2.command(new OSQLSynchQuery<OrientVertex>("select count(*) from SMS")).execute();
Assert.assertEquals(1, ((Number) result.iterator().next().getProperty("count")).intValue());
} catch (Throwable e) {
if (exceptionInThread == null) {
exceptionInThread = e;
}
} finally {
OLogManager.instance().info(this, "Shutting down db2");
graph2.shutdown();
}
}
Aggregations