Search in sources :

Example 1 with TaskScheduler

use of com.baidu.hugegraph.task.TaskScheduler in project incubator-hugegraph by apache.

the class TestGraph method clearSchema.

@Watched
protected void clearSchema() {
    // Clear schema and graph data will be cleared at same time
    SchemaManager schema = this.graph.schema();
    schema.getIndexLabels().stream().forEach(elem -> {
        schema.indexLabel(elem.name()).remove();
    });
    schema.getEdgeLabels().stream().forEach(elem -> {
        schema.edgeLabel(elem.name()).remove();
    });
    schema.getVertexLabels().stream().forEach(elem -> {
        schema.vertexLabel(elem.name()).remove();
    });
    schema.getPropertyKeys().stream().forEach(elem -> {
        schema.propertyKey(elem.name()).remove();
    });
    TaskScheduler scheduler = this.graph.taskScheduler();
    scheduler.tasks(null, -1, null).forEachRemaining(elem -> {
        scheduler.delete(elem.id());
    });
}
Also used : SchemaManager(com.baidu.hugegraph.schema.SchemaManager) TaskScheduler(com.baidu.hugegraph.task.TaskScheduler) Watched(com.baidu.hugegraph.perf.PerfUtil.Watched)

Example 2 with TaskScheduler

use of com.baidu.hugegraph.task.TaskScheduler in project incubator-hugegraph by apache.

the class TaskCoreTest method testEphemeralJob.

@Test
public void testEphemeralJob() throws TimeoutException {
    HugeGraph graph = graph();
    TaskScheduler scheduler = graph.taskScheduler();
    EphemeralJobBuilder<Object> builder = EphemeralJobBuilder.of(graph);
    builder.name("test-job-ephemeral").job(new EphemeralJob<Object>() {

        @Override
        public String type() {
            return "test";
        }

        @Override
        public Object execute() throws Exception {
            sleepAWhile();
            return ImmutableMap.of("k1", 13579, "k2", "24680");
        }
    });
    HugeTask<Object> task = builder.schedule();
    Assert.assertEquals("test-job-ephemeral", task.name());
    Assert.assertEquals("test", task.type());
    Assert.assertFalse(task.completed());
    HugeTask<?> task2 = scheduler.waitUntilTaskCompleted(task.id(), 10);
    Assert.assertEquals(TaskStatus.SUCCESS, task.status());
    Assert.assertEquals("{\"k1\":13579,\"k2\":\"24680\"}", task.result());
    Assert.assertEquals(TaskStatus.SUCCESS, task2.status());
    Assert.assertEquals("{\"k1\":13579,\"k2\":\"24680\"}", task2.result());
    Assert.assertThrows(NotFoundException.class, () -> {
        scheduler.waitUntilTaskCompleted(task.id(), 10);
    });
    Assert.assertThrows(NotFoundException.class, () -> {
        scheduler.task(task.id());
    });
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) TaskScheduler(com.baidu.hugegraph.task.TaskScheduler) TimeoutException(java.util.concurrent.TimeoutException) NotFoundException(com.baidu.hugegraph.exception.NotFoundException) HugeException(com.baidu.hugegraph.HugeException) Test(org.junit.Test)

Example 3 with TaskScheduler

use of com.baidu.hugegraph.task.TaskScheduler in project incubator-hugegraph by apache.

the class TaskCoreTest method testGremlinJob.

@Test
public void testGremlinJob() throws TimeoutException {
    HugeGraph graph = graph();
    TaskScheduler scheduler = graph.taskScheduler();
    GremlinRequest request = new GremlinRequest();
    request.gremlin("sleep(100); 3 + 5");
    JobBuilder<Object> builder = JobBuilder.of(graph);
    builder.name("test-job-gremlin").input(request.toJson()).job(new GremlinJob());
    HugeTask<Object> task = builder.schedule();
    Assert.assertEquals("test-job-gremlin", task.name());
    Assert.assertEquals("gremlin", task.type());
    Assert.assertFalse(task.completed());
    Assert.assertNull(task.result());
    task = scheduler.waitUntilTaskCompleted(task.id(), 10);
    Assert.assertEquals("test-job-gremlin", task.name());
    Assert.assertEquals("gremlin", task.type());
    Assert.assertEquals(TaskStatus.SUCCESS, task.status());
    Assert.assertEquals("8", task.result());
    task = scheduler.task(task.id());
    Assert.assertEquals("test-job-gremlin", task.name());
    Assert.assertEquals("gremlin", task.type());
    Assert.assertEquals(TaskStatus.SUCCESS, task.status());
    Assert.assertEquals("8", task.result());
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) GremlinJob(com.baidu.hugegraph.job.GremlinJob) GremlinRequest(com.baidu.hugegraph.api.job.GremlinAPI.GremlinRequest) TaskScheduler(com.baidu.hugegraph.task.TaskScheduler) Test(org.junit.Test)

Example 4 with TaskScheduler

use of com.baidu.hugegraph.task.TaskScheduler in project incubator-hugegraph by apache.

the class TaskCoreTest method testGremlinJobWithSerializedResults.

@Test
public void testGremlinJobWithSerializedResults() throws TimeoutException {
    HugeGraph graph = graph();
    TaskScheduler scheduler = graph.taskScheduler();
    String script = "schema=graph.schema();" + "schema.propertyKey('name').asText().ifNotExist().create();" + "schema.vertexLabel('char').useCustomizeNumberId()" + "      .properties('name').ifNotExist().create();" + "schema.edgeLabel('next').sourceLabel('char').targetLabel('char')" + "      .properties('name').ifNotExist().create();" + "g.addV('char').property(id,1).property('name','A').as('a')" + " .addV('char').property(id,2).property('name','B').as('b')" + " .addV('char').property(id,3).property('name','C').as('c')" + " .addV('char').property(id,4).property('name','D').as('d')" + " .addV('char').property(id,5).property('name','E').as('e')" + " .addV('char').property(id,6).property('name','F').as('f')" + " .addE('next').from('a').to('b').property('name','ab')" + " .addE('next').from('b').to('c').property('name','bc')" + " .addE('next').from('b').to('d').property('name','bd')" + " .addE('next').from('c').to('d').property('name','cd')" + " .addE('next').from('c').to('e').property('name','ce')" + " .addE('next').from('d').to('e').property('name','de')" + " .addE('next').from('e').to('f').property('name','ef')" + " .addE('next').from('f').to('d').property('name','fd')" + " .iterate();" + "g.tx().commit(); g.E().count();";
    HugeTask<Object> task = runGremlinJob(script);
    task = scheduler.waitUntilTaskCompleted(task.id(), 10);
    Assert.assertEquals("test-gremlin-job", task.name());
    Assert.assertEquals("gremlin", task.type());
    Assert.assertEquals(TaskStatus.SUCCESS, task.status());
    Assert.assertEquals("[8]", task.result());
    Id edgeLabelId = graph.schema().getEdgeLabel("next").id();
    script = "g.V(1).outE().inV().path()";
    task = runGremlinJob(script);
    task = scheduler.waitUntilTaskCompleted(task.id(), 10);
    Assert.assertEquals(TaskStatus.SUCCESS, task.status());
    String expected = String.format("[{\"labels\":[[],[],[]],\"objects\":[" + "{\"id\":1,\"label\":\"char\",\"type\":\"vertex\"," + "\"properties\":{\"name\":\"A\"}}," + "{\"id\":\"L1>%s>>L2\",\"label\":\"next\",\"type\":\"edge\",\"outV\":1," + "\"outVLabel\":\"char\",\"inV\":2,\"" + "inVLabel\":\"char\",\"properties\":{\"name\":\"ab\"}}," + "{\"id\":2,\"label\":\"char\",\"type\":\"vertex\"," + "\"properties\":{\"name\":\"B\"}}" + "]}]", edgeLabelId);
    Assert.assertEquals(expected, task.result());
    script = "g.V(1).out().out().path()";
    task = runGremlinJob(script);
    task = scheduler.waitUntilTaskCompleted(task.id(), 10);
    Assert.assertEquals(TaskStatus.SUCCESS, task.status());
    expected = "[{\"labels\":[[],[],[]],\"objects\":[" + "{\"id\":1,\"label\":\"char\",\"type\":\"vertex\"," + "\"properties\":{\"name\":\"A\"}}," + "{\"id\":2,\"label\":\"char\",\"type\":\"vertex\"," + "\"properties\":{\"name\":\"B\"}}," + "{\"id\":3,\"label\":\"char\",\"type\":\"vertex\"," + "\"properties\":{\"name\":\"C\"}}]}," + "{\"labels\":[[],[],[]],\"objects\":[" + "{\"id\":1,\"label\":\"char\",\"type\":\"vertex\"," + "\"properties\":{\"name\":\"A\"}}," + "{\"id\":2,\"label\":\"char\",\"type\":\"vertex\"," + "\"properties\":{\"name\":\"B\"}}," + "{\"id\":4,\"label\":\"char\",\"type\":\"vertex\"," + "\"properties\":{\"name\":\"D\"}}]}]";
    Assert.assertEquals(expected, task.result());
    script = "g.V(1).outE().inV().tree()";
    task = runGremlinJob(script);
    task = scheduler.waitUntilTaskCompleted(task.id(), 10);
    Assert.assertEquals(TaskStatus.SUCCESS, task.status());
    expected = String.format("[[{\"key\":{\"id\":1,\"label\":\"char\",\"type\":\"vertex\"," + "\"properties\":{\"name\":\"A\"}}," + "\"value\":[" + "{\"key\":{\"id\":\"L1>%s>>L2\",\"label\":\"next\",\"type\":\"edge\",\"outV\":1," + "\"outVLabel\":\"char\",\"inV\":2,\"inVLabel\":\"char\"," + "\"properties\":{\"name\":\"ab\"}}," + "\"value\":[{\"key\":{\"id\":2,\"label\":\"char\",\"type\":\"vertex\"," + "\"properties\":{\"name\":\"B\"}},\"value\":[]}]}]}]]", edgeLabelId);
    Assert.assertEquals(expected, task.result());
    script = "g.V(1).out().out().tree()";
    task = runGremlinJob(script);
    task = scheduler.waitUntilTaskCompleted(task.id(), 10);
    Assert.assertEquals(TaskStatus.SUCCESS, task.status());
    expected = "[[{\"key\":{\"id\":1,\"label\":\"char\",\"type\":\"vertex\"," + "\"properties\":{\"name\":\"A\"}}," + "\"value\":[{\"key\":{\"id\":2,\"label\":\"char\",\"type\":\"vertex\"," + "\"properties\":{\"name\":\"B\"}}," + "\"value\":[" + "{\"key\":{\"id\":3,\"label\":\"char\",\"type\":\"vertex\",\"properties\":" + "{\"name\":\"C\"}},\"value\":[]}," + "{\"key\":{\"id\":4,\"label\":\"char\",\"type\":\"vertex\",\"properties\":" + "{\"name\":\"D\"}},\"value\":[]}]}]}]]";
    Assert.assertEquals(expected, task.result());
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) Id(com.baidu.hugegraph.backend.id.Id) TaskScheduler(com.baidu.hugegraph.task.TaskScheduler) Test(org.junit.Test)

Example 5 with TaskScheduler

use of com.baidu.hugegraph.task.TaskScheduler in project incubator-hugegraph by apache.

the class TaskAPI method delete.

@DELETE
@Timed
@Path("{id}")
public void delete(@Context GraphManager manager, @PathParam("graph") String graph, @PathParam("id") long id) {
    LOG.debug("Graph [{}] delete task: {}", graph, id);
    TaskScheduler scheduler = graph(manager, graph).taskScheduler();
    HugeTask<?> task = scheduler.delete(IdGenerator.of(id));
    E.checkArgument(task != null, "There is no task with id '%s'", id);
}
Also used : TaskScheduler(com.baidu.hugegraph.task.TaskScheduler) Path(jakarta.ws.rs.Path) DELETE(jakarta.ws.rs.DELETE) Timed(com.codahale.metrics.annotation.Timed)

Aggregations

TaskScheduler (com.baidu.hugegraph.task.TaskScheduler)21 HugeGraph (com.baidu.hugegraph.HugeGraph)10 Test (org.junit.Test)10 HugeTask (com.baidu.hugegraph.task.HugeTask)8 Id (com.baidu.hugegraph.backend.id.Id)7 Timed (com.codahale.metrics.annotation.Timed)4 Path (jakarta.ws.rs.Path)3 Produces (jakarta.ws.rs.Produces)3 HugeException (com.baidu.hugegraph.HugeException)2 GremlinJob (com.baidu.hugegraph.job.GremlinJob)2 SchemaManager (com.baidu.hugegraph.schema.SchemaManager)2 TaskCallable (com.baidu.hugegraph.task.TaskCallable)2 GET (jakarta.ws.rs.GET)2 TimeoutException (java.util.concurrent.TimeoutException)2 Status (com.baidu.hugegraph.api.filter.StatusFilter.Status)1 GremlinRequest (com.baidu.hugegraph.api.job.GremlinAPI.GremlinRequest)1 NotFoundException (com.baidu.hugegraph.exception.NotFoundException)1 Watched (com.baidu.hugegraph.perf.PerfUtil.Watched)1 TaskStatus (com.baidu.hugegraph.task.TaskStatus)1 BadRequestException (jakarta.ws.rs.BadRequestException)1