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());
}
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());
}
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());
}
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());
}
}
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());
});
}
Aggregations