use of com.thinkaurelius.titan.core.TitanTransaction in project titan by thinkaurelius.
the class TitanGraphTest method testConsistencyEnforcement.
/* ==================================================================================
CONSISTENCY
==================================================================================*/
/**
* Tests the correct application of ConsistencyModifiers across transactional boundaries
*/
@Test
public void testConsistencyEnforcement() {
PropertyKey uid = makeVertexIndexedUniqueKey("uid", Integer.class);
PropertyKey name = makeKey("name", String.class);
mgmt.setConsistency(uid, ConsistencyModifier.LOCK);
mgmt.setConsistency(name, ConsistencyModifier.LOCK);
mgmt.setConsistency(mgmt.getGraphIndex("uid"), ConsistencyModifier.LOCK);
EdgeLabel knows = mgmt.makeEdgeLabel("knows").multiplicity(Multiplicity.SIMPLE).make();
EdgeLabel spouse = mgmt.makeEdgeLabel("spouse").multiplicity(Multiplicity.ONE2ONE).make();
EdgeLabel connect = mgmt.makeEdgeLabel("connect").multiplicity(Multiplicity.MULTI).make();
EdgeLabel related = mgmt.makeEdgeLabel("related").multiplicity(Multiplicity.MULTI).make();
mgmt.setConsistency(knows, ConsistencyModifier.LOCK);
mgmt.setConsistency(spouse, ConsistencyModifier.LOCK);
mgmt.setConsistency(related, ConsistencyModifier.FORK);
finishSchema();
name = tx.getPropertyKey("name");
connect = tx.getEdgeLabel("connect");
related = tx.getEdgeLabel("related");
TitanVertex v1 = tx.addVertex("uid", 1);
TitanVertex v2 = tx.addVertex("uid", 2);
TitanVertex v3 = tx.addVertex("uid", 3);
Edge e1 = v1.addEdge(connect.name(), v2, name.name(), "e1");
Edge e2 = v1.addEdge(related.name(), v2, name.name(), "e2");
newTx();
v1 = getV(tx, v1);
/*
==== check fork, no fork behavior
*/
long e1id = getId(e1);
long e2id = getId(e2);
e1 = getOnlyElement(v1.query().direction(Direction.OUT).labels("connect").edges());
assertEquals("e1", e1.value("name"));
assertEquals(e1id, getId(e1));
e2 = getOnlyElement(v1.query().direction(Direction.OUT).labels("related").edges());
assertEquals("e2", e2.value("name"));
assertEquals(e2id, getId(e2));
//Update edges - one should simply update, the other fork
e1.property("name", "e1.2");
e2.property("name", "e2.2");
newTx();
v1 = getV(tx, v1);
e1 = getOnlyElement(v1.query().direction(Direction.OUT).labels("connect").edges());
assertEquals("e1.2", e1.value("name"));
//should have same id
assertEquals(e1id, getId(e1));
e2 = getOnlyElement(v1.query().direction(Direction.OUT).labels("related").edges());
assertEquals("e2.2", e2.value("name"));
//should have different id since forked
assertNotEquals(e2id, getId(e2));
clopen();
/*
=== check cross transaction
*/
final Random random = new Random();
final long[] vids = { getId(v1), getId(v2), getId(v3) };
//1) Index uniqueness
executeLockConflictingTransactionJobs(graph, new TransactionJob() {
private int pos = 0;
@Override
public void run(TitanTransaction tx) {
TitanVertex u = getV(tx, vids[pos++]);
u.property(VertexProperty.Cardinality.single, "uid", 5);
}
});
//2) Property out-uniqueness
executeLockConflictingTransactionJobs(graph, new TransactionJob() {
@Override
public void run(TitanTransaction tx) {
TitanVertex u = getV(tx, vids[0]);
u.property(VertexProperty.Cardinality.single, "name", "v" + random.nextInt(10));
}
});
//3) knows simpleness
executeLockConflictingTransactionJobs(graph, new TransactionJob() {
@Override
public void run(TitanTransaction tx) {
TitanVertex u1 = getV(tx, vids[0]), u2 = getV(tx, vids[1]);
u1.addEdge("knows", u2);
}
});
//4) knows one2one (in 2 separate configurations)
executeLockConflictingTransactionJobs(graph, new TransactionJob() {
private int pos = 1;
@Override
public void run(TitanTransaction tx) {
TitanVertex u1 = getV(tx, vids[0]), u2 = getV(tx, vids[pos++]);
u1.addEdge("spouse", u2);
}
});
executeLockConflictingTransactionJobs(graph, new TransactionJob() {
private int pos = 1;
@Override
public void run(TitanTransaction tx) {
TitanVertex u1 = getV(tx, vids[pos++]), u2 = getV(tx, vids[0]);
u1.addEdge("spouse", u2);
}
});
//######### TRY INVALID CONSISTENCY
try {
//Fork does not apply to constrained types
mgmt.setConsistency(mgmt.getPropertyKey("name"), ConsistencyModifier.FORK);
fail();
} catch (IllegalArgumentException e) {
}
}
use of com.thinkaurelius.titan.core.TitanTransaction in project titan by thinkaurelius.
the class TitanIndexTest 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();
TitanVertex 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
TitanTransaction vertexDeleter = graph.newTransaction();
TitanTransaction 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 com.thinkaurelius.titan.core.TitanTransaction in project titan by thinkaurelius.
the class TitanGraphPerformanceMemoryTest method testMemoryLeakage.
@Test
public void testMemoryLeakage() {
long memoryBaseline = 0;
SummaryStatistics stats = new SummaryStatistics();
int numRuns = 25;
for (int r = 0; r < numRuns; r++) {
if (r == 1 || r == (numRuns - 1)) {
memoryBaseline = MemoryAssess.getMemoryUse();
stats.addValue(memoryBaseline);
// System.out.println("Memory before run "+(r+1)+": " + memoryBaseline / 1024 + " KB");
}
for (int t = 0; t < 1000; t++) {
graph.addVertex(null);
graph.rollback();
TitanTransaction tx = graph.newTransaction();
tx.addVertex();
tx.rollback();
}
if (r == 1 || r == (numRuns - 1)) {
memoryBaseline = MemoryAssess.getMemoryUse();
stats.addValue(memoryBaseline);
// System.out.println("Memory after run " + (r + 1) + ": " + memoryBaseline / 1024 + " KB");
}
clopen();
}
System.out.println("Average: " + stats.getMean() + " Std. Dev: " + stats.getStandardDeviation());
assertTrue(stats.getStandardDeviation() < stats.getMin());
}
use of com.thinkaurelius.titan.core.TitanTransaction in project titan by thinkaurelius.
the class TitanGraphPerformanceMemoryTest method testTransactionalMemory.
@Test
public void testTransactionalMemory() throws Exception {
graph.makeKey("uid").dataType(Long.class).indexed(Vertex.class).single(TypeMaker.UniquenessConsistency.NO_LOCK).unique(TypeMaker.UniquenessConsistency.NO_LOCK).make();
graph.makeKey("name").dataType(String.class).single(TypeMaker.UniquenessConsistency.NO_LOCK).make();
TitanKey time = graph.makeKey("time").dataType(Integer.class).single(TypeMaker.UniquenessConsistency.NO_LOCK).make();
graph.makeLabel("friend").signature(time).directed().make();
graph.commit();
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(new Runnable() {
@Override
public void run() {
for (int r = 0; r < rounds; r++) {
TitanTransaction tx = graph.newTransaction();
TitanVertex previous = null;
for (int c = 0; c < commitSize; c++) {
TitanVertex v = tx.addVertex();
long uid = uidCounter.incrementAndGet();
v.setProperty("uid", uid);
v.setProperty("name", "user" + uid);
if (previous != null) {
v.addEdge("friend", previous).setProperty("time", Math.abs(random.nextInt()));
}
previous = v;
}
tx.commit();
}
}
});
writeThreads[t].start();
}
for (int t = 0; t < writeThreads.length; t++) {
writeThreads[t].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(new Runnable() {
@Override
public void run() {
TitanTransaction tx = graph.newTransaction();
long ruid = random.nextInt(maxUID) + 1;
tx.getVertex("uid", ruid).setProperty("name", fixedName);
for (int t = 1; t <= trials; t++) {
TitanVertex v = tx.getVertex("uid", random.nextInt(maxUID) + 1);
assertEquals(2, Iterables.size(v.getProperties()));
int count = 0;
for (TitanEdge e : v.getEdges()) {
count++;
assertTrue(((Number) e.getProperty("time")).longValue() >= 0);
}
assertTrue(count <= 2);
// if (t%(trials/10)==0) System.out.println(t);
}
assertEquals(fixedName, tx.getVertex("uid", ruid).getProperty("name"));
tx.commit();
}
});
readThreads[t].start();
}
for (int t = 0; t < readThreads.length; t++) {
readThreads[t].join();
}
System.out.println("Read time for " + (trials * readThreads.length) + " vertex lookups: " + (System.currentTimeMillis() - start));
}
use of com.thinkaurelius.titan.core.TitanTransaction in project titan by thinkaurelius.
the class TitanGraphPerformanceMemoryTest method testSingleTxDeepTraversals.
@Test
public void testSingleTxDeepTraversals() throws Exception {
// 1) Write random graph (copied from above)
final int numV = 5000;
final int numE = 50;
final String label = "knows";
final Random random = new Random();
final long[] vids = writeGraph(numV, numE, label);
final int repetitions = 1000;
long start = System.currentTimeMillis();
TitanTransaction tx = graph.buildTransaction().readOnly().start();
for (int r = 0; r < repetitions; r++) {
TitanVertex v = tx.getVertex(vids[random.nextInt(numV)]);
assertTrue((int) Math.pow(numE, 2) <= Iterables.size(new GremlinPipeline<Vertex, Vertex>(v).both(label).both(label)));
assertEquals((int) Math.pow(numE, 2), Iterables.size(new GremlinPipeline<Vertex, Vertex>(v).out(label).out(label)));
}
tx.commit();
System.out.println("Time in ms for [" + (repetitions) + "] traversals in single tx: " + (System.currentTimeMillis() - start));
}
Aggregations