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;
}
}
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;
}
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();
}
}
}
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;
}
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();
}
}
}
Aggregations