Search in sources :

Example 16 with TaskScheduler

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

the class TaskCoreTest method testGremlinJobWithFailure.

@Test
public void testGremlinJobWithFailure() throws TimeoutException {
    HugeGraph graph = graph();
    TaskScheduler scheduler = graph.taskScheduler();
    JobBuilder<Object> builder = JobBuilder.of(graph);
    builder.name("test-job-gremlin").input("").job(new GremlinJob());
    HugeTask<Object> task = builder.schedule();
    task = scheduler.waitUntilTaskCompleted(task.id(), 10);
    Assert.assertEquals(TaskStatus.FAILED, task.status());
    Assert.assertContains("Can't read json", task.result());
    builder = JobBuilder.of(graph);
    builder.name("test-job-gremlin").job(new GremlinJob());
    task = builder.schedule();
    scheduler.waitUntilTaskCompleted(task.id(), 10);
    task = scheduler.task(task.id());
    Assert.assertEquals(TaskStatus.FAILED, task.status());
    Assert.assertContains("The input can't be null", task.result());
    builder = JobBuilder.of(graph);
    builder.name("test-job-gremlin").input("{}").job(new GremlinJob());
    task = builder.schedule();
    task = scheduler.waitUntilTaskCompleted(task.id(), 10);
    Assert.assertEquals(TaskStatus.FAILED, task.status());
    Assert.assertContains("Invalid gremlin value 'null'", task.result());
    builder = JobBuilder.of(graph);
    builder.name("test-job-gremlin").input("{\"gremlin\":8}").job(new GremlinJob());
    task = builder.schedule();
    task = scheduler.waitUntilTaskCompleted(task.id(), 10);
    Assert.assertEquals(TaskStatus.FAILED, task.status());
    Assert.assertContains("Invalid gremlin value '8'", task.result());
    builder = JobBuilder.of(graph);
    builder.name("test-job-gremlin").input("{\"gremlin\":\"\"}").job(new GremlinJob());
    task = builder.schedule();
    task = scheduler.waitUntilTaskCompleted(task.id(), 10);
    Assert.assertEquals(TaskStatus.FAILED, task.status());
    Assert.assertContains("Invalid bindings value 'null'", task.result());
    builder = JobBuilder.of(graph);
    builder.name("test-job-gremlin").input("{\"gremlin\":\"\", \"bindings\":\"\"}").job(new GremlinJob());
    task = builder.schedule();
    task = scheduler.waitUntilTaskCompleted(task.id(), 10);
    Assert.assertEquals(TaskStatus.FAILED, task.status());
    Assert.assertContains("Invalid bindings value ''", task.result());
    builder = JobBuilder.of(graph);
    builder.name("test-job-gremlin").input("{\"gremlin\":\"\", \"bindings\":{}}").job(new GremlinJob());
    task = builder.schedule();
    task = scheduler.waitUntilTaskCompleted(task.id(), 10);
    Assert.assertEquals(TaskStatus.FAILED, task.status());
    Assert.assertContains("Invalid language value 'null'", task.result());
    builder = JobBuilder.of(graph);
    builder.name("test-job-gremlin").input("{\"gremlin\":\"\", \"bindings\":{}, \"language\":{}}").job(new GremlinJob());
    task = builder.schedule();
    task = scheduler.waitUntilTaskCompleted(task.id(), 10);
    Assert.assertEquals(TaskStatus.FAILED, task.status());
    Assert.assertContains("Invalid language value '{}'", task.result());
    builder = JobBuilder.of(graph);
    builder.name("test-job-gremlin").input("{\"gremlin\":\"\", \"bindings\":{}, \"language\":\"\"}").job(new GremlinJob());
    task = builder.schedule();
    task = scheduler.waitUntilTaskCompleted(task.id(), 10);
    Assert.assertEquals(TaskStatus.FAILED, task.status());
    Assert.assertContains("Invalid aliases value 'null'", task.result());
    builder = JobBuilder.of(graph);
    builder.name("test-job-gremlin").input("{\"gremlin\":\"\", \"bindings\":{}, " + "\"language\":\"test\", \"aliases\":{}}").job(new GremlinJob());
    task = builder.schedule();
    task = scheduler.waitUntilTaskCompleted(task.id(), 10);
    Assert.assertEquals(TaskStatus.FAILED, task.status());
    Assert.assertContains("test is not an available GremlinScriptEngine", task.result());
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) GremlinJob(com.baidu.hugegraph.job.GremlinJob) TaskScheduler(com.baidu.hugegraph.task.TaskScheduler) Test(org.junit.Test)

Example 17 with TaskScheduler

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

the class TaskCoreTest method testGremlinJobAndRestore.

@Test
public void testGremlinJobAndRestore() throws Exception {
    HugeGraph graph = graph();
    TaskScheduler scheduler = graph.taskScheduler();
    String gremlin = "println('task start');" + "for(int i=gremlinJob.progress(); i<=10; i++) {" + "  gremlinJob.updateProgress(i);" + "  Thread.sleep(200); " + "  println('sleep=>'+i);" + "}; 100;";
    HugeTask<Object> task = runGremlinJob(gremlin);
    sleepAWhile(200 * 6);
    task = scheduler.task(task.id());
    scheduler.cancel(task);
    task = scheduler.task(task.id());
    Assert.assertEquals(TaskStatus.CANCELLING, task.status());
    task = scheduler.waitUntilTaskCompleted(task.id(), 10);
    Assert.assertEquals(TaskStatus.CANCELLED, task.status());
    Assert.assertTrue("progress=" + task.progress(), 0 < task.progress() && task.progress() < 10);
    Assert.assertEquals(0, task.retries());
    Assert.assertEquals(null, task.result());
    HugeTask<Object> finalTask = task;
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        Whitebox.invoke(scheduler.getClass(), "restore", scheduler, finalTask);
    }, e -> {
        Assert.assertContains("No need to restore completed task", e.getMessage());
    });
    HugeTask<Object> task2 = scheduler.task(task.id());
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        Whitebox.invoke(scheduler.getClass(), "restore", scheduler, task2);
    }, e -> {
        Assert.assertContains("No need to restore completed task", e.getMessage());
    });
    Whitebox.setInternalState(task2, "status", TaskStatus.RUNNING);
    Whitebox.invoke(scheduler.getClass(), "restore", scheduler, task2);
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        Whitebox.invoke(scheduler.getClass(), "restore", scheduler, task2);
    }, e -> {
        Assert.assertContains("is already in the queue", e.getMessage());
    });
    scheduler.waitUntilTaskCompleted(task2.id(), 10);
    sleepAWhile(500);
    Assert.assertEquals(10, task2.progress());
    Assert.assertEquals(1, task2.retries());
    Assert.assertEquals("100", task2.result());
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) TaskScheduler(com.baidu.hugegraph.task.TaskScheduler) Test(org.junit.Test)

Example 18 with TaskScheduler

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

the class TaskCoreTest method testGremlinJobWithScript.

@Test
public void testGremlinJobWithScript() throws TimeoutException {
    HugeGraph graph = graph();
    TaskScheduler scheduler = graph.taskScheduler();
    String script = "schema=graph.schema();" + "schema.propertyKey('name').asText().ifNotExist().create();" + "schema.propertyKey('age').asInt().ifNotExist().create();" + "schema.propertyKey('lang').asText().ifNotExist().create();" + "schema.propertyKey('date').asDate().ifNotExist().create();" + "schema.propertyKey('price').asInt().ifNotExist().create();" + "schema.vertexLabel('person1').properties('name','age').ifNotExist().create();" + "schema.vertexLabel('person2').properties('name','age').ifNotExist().create();" + "schema.edgeLabel('knows').sourceLabel('person1').targetLabel('person2')." + "properties('date').ifNotExist().create();" + "for(int i = 0; i < 1000; i++) {" + "  p1=graph.addVertex(T.label,'person1','name','p1-'+i,'age',29);" + "  p2=graph.addVertex(T.label,'person2','name','p2-'+i,'age',27);" + "  p1.addEdge('knows',p2,'date','2016-01-10');" + "}";
    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("[]", task.result());
    script = "g.V().count()";
    task = runGremlinJob(script);
    task = scheduler.waitUntilTaskCompleted(task.id(), 10);
    Assert.assertEquals(TaskStatus.SUCCESS, task.status());
    Assert.assertEquals("[2000]", task.result());
    script = "g.V().hasLabel('person1').count()";
    task = runGremlinJob(script);
    task = scheduler.waitUntilTaskCompleted(task.id(), 10);
    Assert.assertEquals(TaskStatus.SUCCESS, task.status());
    Assert.assertEquals("[1000]", task.result());
    script = "g.V().hasLabel('person2').count()";
    task = runGremlinJob(script);
    task = scheduler.waitUntilTaskCompleted(task.id(), 10);
    Assert.assertEquals(TaskStatus.SUCCESS, task.status());
    Assert.assertEquals("[1000]", task.result());
    script = "g.E().count()";
    task = runGremlinJob(script);
    task = scheduler.waitUntilTaskCompleted(task.id(), 10);
    Assert.assertEquals(TaskStatus.SUCCESS, task.status());
    Assert.assertEquals("[1000]", task.result());
    script = "g.E().hasLabel('knows').count()";
    task = runGremlinJob(script);
    task = scheduler.waitUntilTaskCompleted(task.id(), 10);
    Assert.assertEquals(TaskStatus.SUCCESS, task.status());
    Assert.assertEquals("[1000]", task.result());
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) TaskScheduler(com.baidu.hugegraph.task.TaskScheduler) Test(org.junit.Test)

Example 19 with TaskScheduler

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

the class TaskCoreTest method setup.

@Before
@Override
public void setup() {
    super.setup();
    HugeGraph graph = graph();
    TaskScheduler scheduler = graph.taskScheduler();
    Iterator<HugeTask<Object>> iter = scheduler.tasks(null, -1, null);
    while (iter.hasNext()) {
        scheduler.delete(iter.next().id());
    }
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) HugeTask(com.baidu.hugegraph.task.HugeTask) TaskScheduler(com.baidu.hugegraph.task.TaskScheduler) Before(org.junit.Before)

Example 20 with TaskScheduler

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

the class TaskCoreTest method testTaskWithFailure.

@Test
public void testTaskWithFailure() throws TimeoutException {
    HugeGraph graph = graph();
    TaskScheduler scheduler = graph.taskScheduler();
    TaskCallable<Integer> callable = new TaskCallable<Integer>() {

        @Override
        public Integer call() throws Exception {
            sleepAWhile();
            return 125;
        }

        @Override
        protected void done() {
            scheduler.save(this.task());
        }
    };
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        new HugeTask<>(null, null, callable);
    }, e -> {
        Assert.assertContains("Task id can't be null", e.getMessage());
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        Id id = IdGenerator.of("88888");
        new HugeTask<>(id, null, callable);
    }, e -> {
        Assert.assertContains("Invalid task id type, it must be number", e.getMessage());
    });
    Assert.assertThrows(NullPointerException.class, () -> {
        Id id = IdGenerator.of(88888);
        new HugeTask<>(id, null, null);
    });
    Assert.assertThrows(IllegalStateException.class, () -> {
        Id id = IdGenerator.of(88888);
        HugeTask<?> task2 = new HugeTask<>(id, null, callable);
        task2.name("test-task");
        scheduler.schedule(task2);
    }, e -> {
        Assert.assertContains("Task type can't be null", e.getMessage());
    });
    Assert.assertThrows(IllegalStateException.class, () -> {
        Id id = IdGenerator.of(88888);
        HugeTask<?> task2 = new HugeTask<>(id, null, callable);
        task2.type("test");
        scheduler.schedule(task2);
    }, e -> {
        Assert.assertContains("Task name can't be null", e.getMessage());
    });
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) HugeTask(com.baidu.hugegraph.task.HugeTask) Id(com.baidu.hugegraph.backend.id.Id) TaskScheduler(com.baidu.hugegraph.task.TaskScheduler) TaskCallable(com.baidu.hugegraph.task.TaskCallable) Test(org.junit.Test)

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