Search in sources :

Example 51 with SqlgGraph

use of org.umlg.sqlg.structure.SqlgGraph in project sqlg by pietermartin.

the class TestMultipleThreadMultipleJvm method testConcurrentModificationException.

@Test
public void testConcurrentModificationException() throws Exception {
    // number graphs, pretending its a separate jvm
    int NUMBER_OF_GRAPHS = 3;
    int NUMBER_OF_SCHEMAS = 100;
    // Pre-create all the graphs
    List<SqlgGraph> graphs = new ArrayList<>();
    for (int i = 0; i < NUMBER_OF_GRAPHS; i++) {
        graphs.add(SqlgGraph.open(configuration));
    }
    logger.info(String.format("Done firing up %d graphs", NUMBER_OF_GRAPHS));
    try {
        ExecutorService insertPoolPerGraph = Executors.newFixedThreadPool(NUMBER_OF_GRAPHS);
        CompletionService<SqlgGraph> insertPoolPerGraphsExecutorCompletionService = new ExecutorCompletionService<>(insertPoolPerGraph);
        List<Future<SqlgGraph>> results = new ArrayList<>();
        logger.info("starting inserting data");
        for (final SqlgGraph sqlgGraphAsync : graphs) {
            for (int i = 0; i < NUMBER_OF_SCHEMAS; i++) {
                final int count = i;
                results.add(insertPoolPerGraphsExecutorCompletionService.submit(() -> {
                    // noinspection Duplicates
                    try {
                        for (int j = 0; j < 10; j++) {
                            Vertex v1 = sqlgGraphAsync.addVertex(T.label, "schema_" + count + "." + "tableOut_" + count, "name", "asdasd", "age", 1);
                            Vertex v2 = sqlgGraphAsync.addVertex(T.label, "schema_" + count + "." + "tableIn_" + count, "name", "asdasd", "age", 1);
                            v1.addEdge("edge_" + count, v2, "name", "asdasd", "age", 1);
                            sqlgGraphAsync.tx().commit();
                        }
                    } catch (Exception e) {
                        sqlgGraphAsync.tx().rollback();
                        throw new RuntimeException(e);
                    }
                    return sqlgGraphAsync;
                }));
            }
        }
        insertPoolPerGraph.shutdown();
        AtomicBoolean keepReading = new AtomicBoolean(true);
        ExecutorService readPoolPerGraph = Executors.newFixedThreadPool(NUMBER_OF_GRAPHS);
        CompletionService<SqlgGraph> readPoolPerGraphsExecutorCompletionService = new ExecutorCompletionService<>(readPoolPerGraph);
        List<Future<SqlgGraph>> readResults = new ArrayList<>();
        logger.info("starting reading data");
        for (final SqlgGraph sqlgGraphAsync : graphs) {
            for (int i = 0; i < NUMBER_OF_SCHEMAS; i++) {
                readResults.add(readPoolPerGraphsExecutorCompletionService.submit(() -> {
                    // noinspection Duplicates
                    try {
                        while (keepReading.get()) {
                            sqlgGraphAsync.getTopology().getAllTables();
                            sqlgGraphAsync.getTopology().getAllEdgeForeignKeys();
                            Thread.sleep(100);
                        }
                    } catch (Exception e) {
                        sqlgGraphAsync.tx().rollback();
                        throw new RuntimeException(e);
                    }
                    return sqlgGraphAsync;
                }));
            }
        }
        readPoolPerGraph.shutdown();
        for (Future<SqlgGraph> result : results) {
            result.get(30, TimeUnit.SECONDS);
        }
        keepReading.set(false);
        for (Future<SqlgGraph> result : readResults) {
            result.get(30, TimeUnit.SECONDS);
        }
        logger.info("starting querying data");
        List<Vertex> vertices = this.sqlgGraph.traversal().V().out().toList();
        this.sqlgGraph.tx().rollback();
        for (SqlgGraph graph : graphs) {
            logger.info("assert querying data");
            assertEquals(vertices, graph.traversal().V().out().toList());
            graph.tx().rollback();
        }
    } finally {
        for (SqlgGraph graph : graphs) {
            graph.close();
        }
    }
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) IOException(java.io.IOException) PropertyVetoException(java.beans.PropertyVetoException) ConfigurationException(org.apache.commons.configuration.ConfigurationException) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 52 with SqlgGraph

use of org.umlg.sqlg.structure.SqlgGraph in project sqlg by pietermartin.

the class TestSqlgSchema method testSqlgSchemaExist.

@Test
public void testSqlgSchemaExist() throws Exception {
    Vertex person = this.sqlgGraph.addVertex(T.label, "Person", "name", "John");
    Vertex dog = this.sqlgGraph.addVertex(T.label, "Dog", "name", "Snowy");
    person.addEdge("pet", dog, "createdOn", LocalDateTime.now());
    this.sqlgGraph.tx().commit();
    Assert.assertEquals(2, this.sqlgGraph.traversal().V().count().next(), 0);
    this.sqlgGraph.close();
    try (SqlgGraph sqlgGraph1 = SqlgGraph.open(configuration)) {
        GraphTraversalSource traversalSource = sqlgGraph1.traversal().withStrategies(TopologyStrategy.build().selectFrom(sqlgGraph1.getTopology().getSqlgSchemaAbstractLabels()).create());
        // Assert the schema
        List<Vertex> schemas = traversalSource.V().hasLabel(SQLG_SCHEMA + "." + SQLG_SCHEMA_SCHEMA).toList();
        Assert.assertEquals(2, schemas.size());
        Assert.assertEquals(sqlgGraph1.getSqlDialect().getPublicSchema(), schemas.get(0).value("name"));
        // Assert the vertex labels
        List<Vertex> vertexLabels = traversalSource.V().hasLabel(SQLG_SCHEMA + "." + SQLG_SCHEMA_VERTEX_LABEL).toList();
        Assert.assertEquals(2, vertexLabels.size());
        Assert.assertEquals(1, vertexLabels.stream().filter(v -> v.value("name").equals("Person")).count());
        Assert.assertEquals(1, vertexLabels.stream().filter(v -> v.value("name").equals("Dog")).count());
        // Assert the edge labels
        List<Vertex> edgeLabels = traversalSource.V().hasLabel(SQLG_SCHEMA + "." + SQLG_SCHEMA_EDGE_LABEL).toList();
        Assert.assertEquals(1, edgeLabels.size());
        Assert.assertEquals(1, edgeLabels.stream().filter(v -> v.value("name").equals("pet")).count());
        // Assert the Person's properties
        List<Vertex> vertexLabelPersons = traversalSource.V().hasLabel(SQLG_SCHEMA + "." + SQLG_SCHEMA_VERTEX_LABEL).has("name", "Person").toList();
        Assert.assertEquals(1, vertexLabelPersons.size());
        Vertex vertexLabelPerson = vertexLabelPersons.get(0);
        List<Vertex> personProperties = traversalSource.V(vertexLabelPerson).out(SQLG_SCHEMA_VERTEX_PROPERTIES_EDGE).toList();
        Assert.assertEquals(1, personProperties.size());
        Assert.assertEquals("name", personProperties.get(0).value("name"));
        Assert.assertEquals("STRING", personProperties.get(0).value("type"));
        // Assert the Dog's properties
        List<Vertex> vertexLabelDogs = traversalSource.V().hasLabel(SQLG_SCHEMA + "." + SQLG_SCHEMA_VERTEX_LABEL).has("name", "Dog").toList();
        Assert.assertEquals(1, vertexLabelDogs.size());
        Vertex vertexLabelDog = vertexLabelDogs.get(0);
        List<Vertex> dogProperties = traversalSource.V(vertexLabelDog).out(SQLG_SCHEMA_VERTEX_PROPERTIES_EDGE).toList();
        Assert.assertEquals(1, dogProperties.size());
        Assert.assertEquals("name", dogProperties.get(0).value("name"));
        Assert.assertEquals("STRING", personProperties.get(0).value("type"));
        // Assert the pet edge's properties
        List<Vertex> edgeLabelPets = traversalSource.V().hasLabel(SQLG_SCHEMA + "." + SQLG_SCHEMA_EDGE_LABEL).has("name", "pet").toList();
        Assert.assertEquals(1, edgeLabelPets.size());
        Vertex edgeLabelPet = edgeLabelPets.get(0);
        List<Vertex> petProperties = traversalSource.V(edgeLabelPet).out(SQLG_SCHEMA_EDGE_PROPERTIES_EDGE).toList();
        Assert.assertEquals(1, petProperties.size());
        Assert.assertEquals("createdOn", petProperties.get(0).value("name"));
        Assert.assertEquals("LOCALDATETIME", petProperties.get(0).value("type"));
        // assert that the topology edges are also queryable
        List<Edge> edges = traversalSource.V().hasLabel("sqlg_schema.schema").outE().toList();
        Assert.assertEquals(2, edges.size());
        edges = traversalSource.V().hasLabel("sqlg_schema.schema").out().outE("out_edges").toList();
        Assert.assertEquals(1, edges.size());
        edges = traversalSource.V().hasLabel("sqlg_schema.schema").out().outE("in_edges").toList();
        Assert.assertEquals(1, edges.size());
        edges = traversalSource.V().hasLabel("sqlg_schema.schema").out().outE("vertex_property").toList();
        Assert.assertEquals(2, edges.size());
        edges = traversalSource.V().hasLabel("sqlg_schema.schema").out().out("out_edges").outE("edge_property").toList();
        Assert.assertEquals(1, edges.size());
        List<Edge> topologyEdges = traversalSource.E().toList();
        Assert.assertEquals(7, topologyEdges.size());
    }
}
Also used : GraphTraversalSource(org.apache.tinkerpop.gremlin.process.traversal.dsl.graph.GraphTraversalSource) Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) Edge(org.apache.tinkerpop.gremlin.structure.Edge) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 53 with SqlgGraph

use of org.umlg.sqlg.structure.SqlgGraph in project sqlg by pietermartin.

the class TestDeadLock method testDeadlock1DifferentGraphs.

@Test
public void testDeadlock1DifferentGraphs() throws Exception {
    Vertex v1 = sqlgGraph.addVertex(T.label, "t1", "name", "n1");
    Vertex v2 = sqlgGraph.addVertex(T.label, "t1", "name", "n2");
    Vertex v3 = sqlgGraph.addVertex(T.label, "t2", "name", "n3");
    v1.addEdge("e1", v2);
    sqlgGraph.tx().commit();
    Object o1 = new Object();
    Object o2 = new Object();
    AtomicInteger ok = new AtomicInteger(0);
    Thread t1 = new Thread(() -> {
        try {
            synchronized (o1) {
                o1.wait();
            }
            GraphTraversal<Vertex, Vertex> gt = sqlgGraph.traversal().V().hasLabel("t1").out("e1");
            int cnt = 0;
            // this lock the E_e1 table and then request topology read lock
            while (gt.hasNext()) {
                gt.next();
                synchronized (o2) {
                    o2.notify();
                }
                if (cnt == 0) {
                    synchronized (o1) {
                        o1.wait(1000);
                    }
                }
                cnt++;
            }
            sqlgGraph.tx().commit();
            ok.incrementAndGet();
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail(e.getMessage());
        }
    }, "thread-1");
    t1.start();
    Thread t2 = new Thread(() -> {
        try {
            try (SqlgGraph sqlgGraph1 = SqlgGraph.open(getConfigurationClone())) {
                Vertex v1b = sqlgGraph1.vertices(v1.id()).next();
                Vertex v3b = sqlgGraph1.vertices(v3.id()).next();
                synchronized (o2) {
                    o2.wait();
                }
                // this locks the topology and then tries to modify the E_e1 table
                v1b.addEdge("e1", v3b);
                synchronized (o1) {
                    o1.notify();
                }
                sqlgGraph1.tx().commit();
                ok.incrementAndGet();
            }
        } catch (Exception e) {
            e.printStackTrace();
            Assert.fail(e.getMessage());
        }
    }, "thread-2");
    t2.start();
    Thread.sleep(1000);
    synchronized (o1) {
        o1.notifyAll();
    }
    t1.join();
    t2.join();
    Assert.assertEquals(2, ok.get());
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 54 with SqlgGraph

use of org.umlg.sqlg.structure.SqlgGraph in project sqlg by pietermartin.

the class TestTopologyMultipleGraphs method testGratefulDeadAcrossGraphs.

@Test
public void testGratefulDeadAcrossGraphs() {
    try (SqlgGraph sqlgGraph1 = SqlgGraph.open(configuration)) {
        loadGratefulDead();
        Thread.sleep(1000);
        assertTrue(this.sqlgGraph.getTopology().equals(sqlgGraph1.getTopology()));
    } catch (Exception e) {
        fail(e.getMessage());
    }
}
Also used : SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) IOException(java.io.IOException) PropertyVetoException(java.beans.PropertyVetoException) ConfigurationException(org.apache.commons.configuration.ConfigurationException) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Example 55 with SqlgGraph

use of org.umlg.sqlg.structure.SqlgGraph in project sqlg by pietermartin.

the class TestTopologyUpgrade method testUpgradeMultipleInOutEdges.

@Test
public void testUpgradeMultipleInOutEdges() throws Exception {
    Vertex a1 = this.sqlgGraph.addVertex(T.label, "A", "name", "a1");
    Vertex b1 = this.sqlgGraph.addVertex(T.label, "B", "name", "b1");
    Vertex c1 = this.sqlgGraph.addVertex(T.label, "C", "name", "c1");
    Object a1Id = a1.id();
    Object b1Id = b1.id();
    a1.addEdge("ab", b1, "weight", 5);
    a1.addEdge("ab", c1, "weight", 6);
    b1.addEdge("ba", a1, "wtf", "wtf1");
    b1.addEdge("ba", c1, "wtf", "wtf1");
    this.sqlgGraph.tx().commit();
    // Delete the topology
    dropSqlgSchema(this.sqlgGraph);
    this.sqlgGraph.tx().commit();
    this.sqlgGraph.close();
    // topology will be recreated
    try (SqlgGraph sqlgGraph1 = SqlgGraph.open(configuration)) {
        Assert.assertEquals(2, sqlgGraph1.traversal().V(a1Id).out().count().next().intValue());
        Assert.assertEquals(2, sqlgGraph1.traversal().V(b1Id).out().count().next().intValue());
    }
}
Also used : Vertex(org.apache.tinkerpop.gremlin.structure.Vertex) SqlgGraph(org.umlg.sqlg.structure.SqlgGraph) BaseTest(org.umlg.sqlg.test.BaseTest) Test(org.junit.Test)

Aggregations

SqlgGraph (org.umlg.sqlg.structure.SqlgGraph)75 Test (org.junit.Test)68 BaseTest (org.umlg.sqlg.test.BaseTest)64 Vertex (org.apache.tinkerpop.gremlin.structure.Vertex)44 ConfigurationException (org.apache.commons.configuration.ConfigurationException)10 PropertyVetoException (java.beans.PropertyVetoException)9 IOException (java.io.IOException)9 Connection (java.sql.Connection)7 Edge (org.apache.tinkerpop.gremlin.structure.Edge)6 ResultSet (java.sql.ResultSet)4 Configuration (org.apache.commons.configuration.Configuration)4 Traversal (org.apache.tinkerpop.gremlin.process.traversal.Traversal)4 ReducingBarrierStep (org.apache.tinkerpop.gremlin.process.traversal.step.util.ReducingBarrierStep)4 Statement (java.sql.Statement)3 PropertyType (org.umlg.sqlg.structure.PropertyType)3 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)2 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)2 URL (java.net.URL)2 DatabaseMetaData (java.sql.DatabaseMetaData)2 PreparedStatement (java.sql.PreparedStatement)2