use of org.janusgraph.core.JanusGraphTransaction in project janusgraph by JanusGraph.
the class JanusGraphTest method testIndexUpdateSyncWithMultipleInstances.
@Category({ BrittleTests.class })
@Test
public void testIndexUpdateSyncWithMultipleInstances() throws InterruptedException {
clopen(option(LOG_SEND_DELAY, MANAGEMENT_LOG), Duration.ofMillis(0), option(KCVSLog.LOG_READ_LAG_TIME, MANAGEMENT_LOG), Duration.ofMillis(50), option(LOG_READ_INTERVAL, MANAGEMENT_LOG), Duration.ofMillis(250));
StandardJanusGraph graph2 = (StandardJanusGraph) JanusGraphFactory.open(config);
JanusGraphTransaction tx2;
mgmt.makePropertyKey("name").dataType(String.class).make();
finishSchema();
tx.addVertex("name", "v1");
newTx();
evaluateQuery(tx.query().has("name", "v1"), ElementCategory.VERTEX, 1, new boolean[] { false, true });
tx2 = graph2.newTransaction();
evaluateQuery(tx2.query().has("name", "v1"), ElementCategory.VERTEX, 1, new boolean[] { false, true });
// Leave tx2 open to delay acknowledgement
mgmt.buildIndex("theIndex", Vertex.class).addKey(mgmt.getPropertyKey("name")).buildCompositeIndex();
mgmt.commit();
JanusGraphTransaction tx3 = graph2.newTransaction();
tx3.addVertex("name", "v2");
tx3.commit();
newTx();
tx.addVertex("name", "v3");
tx.commit();
finishSchema();
try {
mgmt.updateIndex(mgmt.getGraphIndex("theIndex"), SchemaAction.ENABLE_INDEX);
// Open tx2 should not make this possible
fail();
} catch (IllegalArgumentException ignored) {
}
finishSchema();
// Release transaction and wait a little for registration which should make enabling possible
tx2.commit();
mgmt.rollback();
assertTrue(ManagementSystem.awaitGraphIndexStatus(graph, "theIndex").status(SchemaStatus.REGISTERED).timeout(TestGraphConfigs.getSchemaConvergenceTime(ChronoUnit.SECONDS), ChronoUnit.SECONDS).call().getSucceeded());
finishSchema();
mgmt.updateIndex(mgmt.getGraphIndex("theIndex"), SchemaAction.ENABLE_INDEX);
finishSchema();
tx2 = graph2.newTransaction();
// Should be added to index but index not yet enabled
tx2.addVertex("name", "v4");
tx2.commit();
newTx();
evaluateQuery(tx.query().has("name", "v1"), ElementCategory.VERTEX, 0, new boolean[] { true, true }, "theIndex");
evaluateQuery(tx.query().has("name", "v2"), ElementCategory.VERTEX, 1, new boolean[] { true, true }, "theIndex");
evaluateQuery(tx.query().has("name", "v3"), ElementCategory.VERTEX, 1, new boolean[] { true, true }, "theIndex");
evaluateQuery(tx.query().has("name", "v4"), ElementCategory.VERTEX, 1, new boolean[] { true, true }, "theIndex");
tx2 = graph2.newTransaction();
evaluateQuery(tx2.query().has("name", "v1"), ElementCategory.VERTEX, 0, new boolean[] { true, true }, "theIndex");
evaluateQuery(tx2.query().has("name", "v2"), ElementCategory.VERTEX, 1, new boolean[] { true, true }, "theIndex");
evaluateQuery(tx2.query().has("name", "v3"), ElementCategory.VERTEX, 1, new boolean[] { true, true }, "theIndex");
evaluateQuery(tx2.query().has("name", "v4"), ElementCategory.VERTEX, 1, new boolean[] { true, true }, "theIndex");
tx2.commit();
// Finally test retrieving and closing open instances
Set<String> openInstances = mgmt.getOpenInstances();
assertEquals(2, openInstances.size());
assertTrue(openInstances.contains(graph.getConfiguration().getUniqueGraphId() + "(current)"));
assertTrue(openInstances.contains(graph2.getConfiguration().getUniqueGraphId()));
try {
mgmt.forceCloseInstance(graph.getConfiguration().getUniqueGraphId());
// Cannot close current instance
fail();
} catch (IllegalArgumentException ignored) {
}
mgmt.forceCloseInstance(graph2.getConfiguration().getUniqueGraphId());
graph2.close();
}
use of org.janusgraph.core.JanusGraphTransaction in project janusgraph by JanusGraph.
the class JanusGraphIndexTest method testNestedWrites.
private void testNestedWrites(String initialValue, String updatedValue) throws BackendException {
// This method touches a single vertex with multiple transactions,
// leading to deadlock under BDB and cascading test failures. Check for
// the hasTxIsolation() store feature, which is currently true for BDB
// but false for HBase/Cassandra. This is kind of a hack; a more robust
// approach might implement different methods/assertions depending on
// whether the store is capable of deadlocking or detecting conflicting
// writes and aborting a transaction.
Backend b = null;
try {
b = graph.getConfiguration().getBackend();
if (b.getStoreFeatures().hasTxIsolation()) {
log.info("Skipping " + getClass().getSimpleName() + "." + methodName.getMethodName());
return;
}
} finally {
if (null != b)
b.close();
}
final String propName = "foo";
// Write schema and one vertex
PropertyKey prop = makeKey(propName, String.class);
createExternalVertexIndex(prop, INDEX);
finishSchema();
JanusGraphVertex v = graph.addVertex();
if (null != initialValue)
v.property(VertexProperty.Cardinality.single, propName, initialValue);
graph.tx().commit();
Object id = v.id();
// Open two transactions and modify the same vertex
JanusGraphTransaction vertexDeleter = graph.newTransaction();
JanusGraphTransaction propDeleter = graph.newTransaction();
getV(vertexDeleter, id).remove();
if (null == updatedValue)
getV(propDeleter, id).property(propName).remove();
else
getV(propDeleter, id).property(VertexProperty.Cardinality.single, propName, updatedValue);
vertexDeleter.commit();
propDeleter.commit();
// The vertex must not exist after deletion
graph.tx().rollback();
assertEquals(null, getV(graph, id));
assertEmpty(graph.query().has(propName).vertices());
if (null != updatedValue)
assertEmpty(graph.query().has(propName, updatedValue).vertices());
graph.tx().rollback();
}
use of org.janusgraph.core.JanusGraphTransaction in project janusgraph by JanusGraph.
the class JanusGraphIterativeBenchmark method loadData.
public void loadData(final int numVertices, final int numThreads) throws Exception {
makeKey("w", Integer.class);
PropertyKey time = makeKey("t", Long.class);
((StandardEdgeLabelMaker) mgmt.makeEdgeLabel("l")).sortKey(time).make();
finishSchema();
final int maxQueue = 1000;
final int verticesPerTask = 1000;
final int maxWeight = 10;
final int maxTime = 10000;
final BlockingQueue<Runnable> tasks = new ArrayBlockingQueue<>(maxQueue);
ExecutorService exe = Executors.newFixedThreadPool(numThreads);
for (int i = 0; i < numVertices / verticesPerTask; i++) {
while (tasks.size() >= maxQueue) Thread.sleep(maxQueue);
assert tasks.size() < maxQueue;
exe.submit(() -> {
final JanusGraphTransaction tx = graph.newTransaction();
final JanusGraphVertex[] vs = new JanusGraphVertex[verticesPerTask];
for (int j = 0; j < verticesPerTask; j++) {
vs[j] = tx.addVertex();
vs[j].property(VertexProperty.Cardinality.single, "w", random.nextInt(maxWeight));
}
for (int j = 0; j < verticesPerTask * 10; j++) {
final JanusGraphEdge e = vs[random.nextInt(verticesPerTask)].addEdge("l", vs[random.nextInt(verticesPerTask)]);
e.property("t", random.nextInt(maxTime));
}
System.out.print(".");
tx.commit();
});
}
exe.shutdown();
exe.awaitTermination(numVertices / 1000, TimeUnit.SECONDS);
if (!exe.isTerminated())
System.err.println("Could not load data in time");
System.out.println("Loaded " + numVertices + "vertices");
}
use of org.janusgraph.core.JanusGraphTransaction in project janusgraph by JanusGraph.
the class JanusGraphIoTest method addGeoshape.
private void addGeoshape(Function<Geoshape, Geoshape> makeGeoshape) {
JanusGraphTransaction tx = graph.newTransaction();
graph.traversal().E().has("place").toList().forEach(e -> {
Geoshape place = (Geoshape) e.property("place").value();
e.property("shape", makeGeoshape.apply(place));
});
tx.commit();
}
use of org.janusgraph.core.JanusGraphTransaction in project janusgraph by JanusGraph.
the class JanusGraphPerformanceMemoryTest method testTransactionalMemory.
@Test
public void testTransactionalMemory() throws Exception {
makeVertexIndexedUniqueKey("uid", Long.class);
makeKey("name", String.class);
PropertyKey time = makeKey("time", Integer.class);
mgmt.makeEdgeLabel("friend").signature(time).directed().make();
finishSchema();
final Random random = new Random();
final int rounds = 100;
final int commitSize = 1500;
final AtomicInteger uidCounter = new AtomicInteger(0);
Thread[] writeThreads = new Thread[4];
long start = System.currentTimeMillis();
for (int t = 0; t < writeThreads.length; t++) {
writeThreads[t] = new Thread(() -> {
for (int r = 0; r < rounds; r++) {
JanusGraphTransaction tx = graph.newTransaction();
JanusGraphVertex previous = null;
for (int c = 0; c < commitSize; c++) {
JanusGraphVertex v = tx.addVertex();
long uid = uidCounter.incrementAndGet();
v.property(VertexProperty.Cardinality.single, "uid", uid);
v.property(VertexProperty.Cardinality.single, "name", "user" + uid);
if (previous != null) {
v.addEdge("friend", previous, "time", Math.abs(random.nextInt()));
}
previous = v;
}
tx.commit();
}
});
writeThreads[t].start();
}
for (final Thread writeThread : writeThreads) {
writeThread.join();
}
System.out.println("Write time for " + (rounds * commitSize * writeThreads.length) + " vertices & edges: " + (System.currentTimeMillis() - start));
final int maxUID = uidCounter.get();
final int trials = 1000;
final String fixedName = "john";
Thread[] readThreads = new Thread[Runtime.getRuntime().availableProcessors() * 2];
start = System.currentTimeMillis();
for (int t = 0; t < readThreads.length; t++) {
readThreads[t] = new Thread(() -> {
JanusGraphTransaction tx = graph.newTransaction();
long randomUniqueId = random.nextInt(maxUID) + 1;
getVertex(tx, "uid", randomUniqueId).property(VertexProperty.Cardinality.single, "name", fixedName);
for (int t1 = 1; t1 <= trials; t1++) {
JanusGraphVertex v = getVertex(tx, "uid", random.nextInt(maxUID) + 1);
assertCount(2, v.properties());
int count = 0;
for (Object e : v.query().direction(Direction.BOTH).edges()) {
count++;
assertTrue(((JanusGraphEdge) e).<Integer>value("time") >= 0);
}
assertTrue(count <= 2);
// if (t%(trials/10)==0) System.out.println(t);
}
assertEquals(fixedName, getVertex(tx, "uid", randomUniqueId).value("name"));
tx.commit();
});
readThreads[t].start();
}
for (final Thread readThread : readThreads) {
readThread.join();
}
System.out.println("Read time for " + (trials * readThreads.length) + " vertex lookups: " + (System.currentTimeMillis() - start));
}
Aggregations