Search in sources :

Example 1 with GremlinJob

use of com.baidu.hugegraph.job.GremlinJob in project incubator-hugegraph by apache.

the class TaskCoreTest method testGremlinJobWithError.

@Test
public void testGremlinJobWithError() throws TimeoutException {
    HugeGraph graph = graph();
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        JobBuilder.of(graph).job(new GremlinJob()).schedule();
    }, e -> {
        Assert.assertContains("Job name can't be null", e.getMessage());
    });
    Assert.assertThrows(IllegalArgumentException.class, () -> {
        JobBuilder.of(graph).name("test-job-gremlin").schedule();
    }, e -> {
        Assert.assertContains("Job callable can't be null", e.getMessage());
    });
    // Test failure task with big input
    int length = 8 * 1024 * 1024;
    Random random = new Random();
    StringBuilder sb = new StringBuilder(length);
    for (int i = 0; i < length; i++) {
        sb.append("node:").append(random.nextInt(1000));
    }
    String bigInput = sb.toString();
    Assert.assertThrows(HugeException.class, () -> {
        runGremlinJob(bigInput);
    }, e -> {
        Assert.assertContains("Task input size", e.getMessage());
        Assert.assertContains("exceeded limit 16777216 bytes", e.getMessage());
    });
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) GremlinJob(com.baidu.hugegraph.job.GremlinJob) Random(java.util.Random) Test(org.junit.Test)

Example 2 with GremlinJob

use of com.baidu.hugegraph.job.GremlinJob 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 3 with GremlinJob

use of com.baidu.hugegraph.job.GremlinJob in project incubator-hugegraph by apache.

the class GremlinAPI method post.

@POST
@Timed
@Status(Status.CREATED)
@Consumes(APPLICATION_JSON)
@Produces(APPLICATION_JSON_WITH_CHARSET)
@RolesAllowed({ "admin", "$owner=$graph $action=gremlin_execute" })
public Map<String, Id> post(@Context GraphManager manager, @PathParam("graph") String graph, GremlinRequest request) {
    LOG.debug("Graph [{}] schedule gremlin job: {}", graph, request);
    checkCreatingBody(request);
    GREMLIN_JOB_INPUT_HISTOGRAM.update(request.gremlin.length());
    HugeGraph g = graph(manager, graph);
    request.aliase(graph, "graph");
    JobBuilder<Object> builder = JobBuilder.of(g);
    builder.name(request.name()).input(request.toJson()).job(new GremlinJob());
    return ImmutableMap.of("task_id", builder.schedule().id());
}
Also used : HugeGraph(com.baidu.hugegraph.HugeGraph) GremlinJob(com.baidu.hugegraph.job.GremlinJob) Status(com.baidu.hugegraph.api.filter.StatusFilter.Status) RolesAllowed(jakarta.annotation.security.RolesAllowed) POST(jakarta.ws.rs.POST) Consumes(jakarta.ws.rs.Consumes) Produces(jakarta.ws.rs.Produces) Timed(com.codahale.metrics.annotation.Timed)

Example 4 with GremlinJob

use of com.baidu.hugegraph.job.GremlinJob in project incubator-hugegraph by apache.

the class SecurityManagerTest method runGremlinJob.

private static String runGremlinJob(String gremlin) {
    JobBuilder<Object> builder = JobBuilder.of(graph);
    Map<String, Object> input = new HashMap<>();
    input.put("gremlin", gremlin);
    input.put("bindings", ImmutableMap.of());
    input.put("language", "gremlin-groovy");
    input.put("aliases", ImmutableMap.of());
    builder.name("test-gremlin-job").input(JsonUtil.toJson(input)).job(new GremlinJob());
    HugeTask<?> task = builder.schedule();
    try {
        task = graph.taskScheduler().waitUntilTaskCompleted(task.id(), 10);
    } catch (TimeoutException e) {
        throw new HugeException("Wait for task timeout: %s", e, task);
    }
    return task.result();
}
Also used : GremlinJob(com.baidu.hugegraph.job.GremlinJob) HashMap(java.util.HashMap) HugeException(com.baidu.hugegraph.HugeException) TimeoutException(java.util.concurrent.TimeoutException)

Example 5 with GremlinJob

use of com.baidu.hugegraph.job.GremlinJob 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)

Aggregations

GremlinJob (com.baidu.hugegraph.job.GremlinJob)6 HugeGraph (com.baidu.hugegraph.HugeGraph)5 Test (org.junit.Test)3 GremlinRequest (com.baidu.hugegraph.api.job.GremlinAPI.GremlinRequest)2 TaskScheduler (com.baidu.hugegraph.task.TaskScheduler)2 HugeException (com.baidu.hugegraph.HugeException)1 Status (com.baidu.hugegraph.api.filter.StatusFilter.Status)1 Timed (com.codahale.metrics.annotation.Timed)1 RolesAllowed (jakarta.annotation.security.RolesAllowed)1 Consumes (jakarta.ws.rs.Consumes)1 POST (jakarta.ws.rs.POST)1 Produces (jakarta.ws.rs.Produces)1 HashMap (java.util.HashMap)1 Random (java.util.Random)1 TimeoutException (java.util.concurrent.TimeoutException)1