From 5e27a5aadd415e208e89fb528be7b047e0d78642 Mon Sep 17 00:00:00 2001 From: Zhangmei Li Date: Mon, 29 Oct 2018 19:42:08 +0800 Subject: [PATCH] Make task-list api not return task_input and task_result improve #139 Change-Id: Iac6d1ef85f66efcd5478bcbca2a7fc54a3160f37 --- .../baidu/hugegraph/api/job/GremlinAPI.java | 4 ++- .../com/baidu/hugegraph/api/job/TaskAPI.java | 14 +++------- .../hugegraph/api/traversers/EdgesAPI.java | 2 +- .../hugegraph/api/traversers/VerticesAPI.java | 2 +- .../hugegraph/auth/StandardAuthenticator.java | 6 +++-- .../baidu/hugegraph/schema/SchemaManager.java | 8 +++--- .../com/baidu/hugegraph/task/HugeTask.java | 8 ++++-- .../hugegraph/task/HugeTaskScheduler.java | 27 ++++++++++++++++--- 8 files changed, 46 insertions(+), 25 deletions(-) diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/job/GremlinAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/job/GremlinAPI.java index 48133ef39c..b6b1f20e9f 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/job/GremlinAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/job/GremlinAPI.java @@ -154,7 +154,9 @@ public String name() { @Override public void checkCreate(boolean isBatch) { E.checkArgumentNotNull(this.gremlin, - "The gremlin script can't be null"); + "The gremlin parameter can't be null"); + E.checkArgumentNotNull(this.language, + "The language parameter can't be null"); E.checkArgument(this.aliases == null || this.aliases.isEmpty(), "There is no need to pass gremlin aliases"); } diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/job/TaskAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/job/TaskAPI.java index fe43c83e3e..240b672d05 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/job/TaskAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/job/TaskAPI.java @@ -47,6 +47,7 @@ import com.baidu.hugegraph.server.RestServer; import com.baidu.hugegraph.task.HugeTask; import com.baidu.hugegraph.task.HugeTaskScheduler; +import com.baidu.hugegraph.util.E; import com.baidu.hugegraph.util.Log; import com.codahale.metrics.annotation.Timed; import com.google.common.collect.ImmutableMap; @@ -82,7 +83,7 @@ public Map> list(@Context GraphManager manager, List tasks = new ArrayList<>(); while (itor.hasNext()) { - tasks.add(itor.next().asMap()); + tasks.add(itor.next().asMap(false)); } return ImmutableMap.of("tasks", tasks); } @@ -111,15 +112,8 @@ public void delete(@Context GraphManager manager, HugeGraph g = graph(manager, graph); HugeTaskScheduler scheduler = g.taskScheduler(); - - HugeTask task = scheduler.task(IdGenerator.of(id)); - if (task.completed()) { - scheduler.deleteTask(IdGenerator.of(id)); - } else { - throw new BadRequestException(String.format( - "Can't delete task '%s' with unstable status '%s'", - id, task.status())); - } + HugeTask task = scheduler.deleteTask(IdGenerator.of(id)); + E.checkArgument(task != null, "There is no task with id '%s'", id); } @PUT diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/EdgesAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/EdgesAPI.java index c751a8a501..b786f8268b 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/EdgesAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/EdgesAPI.java @@ -63,7 +63,7 @@ public String list(@Context GraphManager manager, LOG.debug("Graph [{}] get edges by ids: {}", graph, stringIds); E.checkArgument(stringIds != null && !stringIds.isEmpty(), - "Ids can't be null or empty"); + "The ids parameter can't be null or empty"); Object[] ids = new Id[stringIds.size()]; for (int i = 0; i < ids.length; i++) { diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/VerticesAPI.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/VerticesAPI.java index d7b2fa00cf..d002d14327 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/VerticesAPI.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/api/traversers/VerticesAPI.java @@ -63,7 +63,7 @@ public String list(@Context GraphManager manager, LOG.debug("Graph [{}] get vertices by ids: {}", graph, stringIds); E.checkArgument(stringIds != null && !stringIds.isEmpty(), - "Ids can't be null or empty"); + "The ids parameter can't be null or empty"); Object[] ids = new Id[stringIds.size()]; for (int i = 0; i < ids.length; i++) { diff --git a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/StandardAuthenticator.java b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/StandardAuthenticator.java index 6ff59a31c8..7eae73f12a 100644 --- a/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/StandardAuthenticator.java +++ b/hugegraph-api/src/main/java/com/baidu/hugegraph/auth/StandardAuthenticator.java @@ -104,8 +104,10 @@ public AuthenticatedUser authenticate(final Map credentials) * @return String No permission if return ROLE_NONE else return a role */ public String authenticate(final String username, final String password) { - E.checkArgumentNotNull(username, "The username can't be null"); - E.checkArgumentNotNull(password, "The password can't be null"); + E.checkArgument(username != null, + "The username parameter can't be null"); + E.checkArgument(password != null, + "The password parameter can't be null"); String role; if (password.equals(this.tokens.get(username))) { diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/SchemaManager.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/SchemaManager.java index 1da193262a..a99ac287dc 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/SchemaManager.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/schema/SchemaManager.java @@ -59,28 +59,28 @@ public IndexLabel.Builder indexLabel(String name) { } public PropertyKey getPropertyKey(String name) { - E.checkArgumentNotNull(name, "Name can't be null"); + E.checkArgumentNotNull(name, "The name parameter can't be null"); PropertyKey propertyKey = this.transaction.getPropertyKey(name); checkExists(HugeType.PROPERTY_KEY, propertyKey, name); return propertyKey; } public VertexLabel getVertexLabel(String name) { - E.checkArgumentNotNull(name, "Name can't be null"); + E.checkArgumentNotNull(name, "The name parameter can't be null"); VertexLabel vertexLabel = this.transaction.getVertexLabel(name); checkExists(HugeType.VERTEX_LABEL, vertexLabel, name); return vertexLabel; } public EdgeLabel getEdgeLabel(String name) { - E.checkArgumentNotNull(name, "Name can't be null"); + E.checkArgumentNotNull(name, "The name parameter can't be null"); EdgeLabel edgeLabel = this.transaction.getEdgeLabel(name); checkExists(HugeType.EDGE_LABEL, edgeLabel, name); return edgeLabel; } public IndexLabel getIndexLabel(String name) { - E.checkArgumentNotNull(name, "Name can't be null"); + E.checkArgumentNotNull(name, "The name parameter can't be null"); IndexLabel indexLabel = this.transaction.getIndexLabel(name); checkExists(HugeType.INDEX_LABEL, indexLabel, name); return indexLabel; diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/task/HugeTask.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/task/HugeTask.java index 461612361a..21989d2c15 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/task/HugeTask.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/task/HugeTask.java @@ -351,6 +351,10 @@ protected Object[] asArray() { } public Map asMap() { + return this.asMap(true); + } + + public Map asMap(boolean withDetails) { E.checkState(this.type != null, "Task type can't be null"); E.checkState(this.name != null, "Task name can't be null"); @@ -372,10 +376,10 @@ public Map asMap() { if (this.update != null) { map.put(Hidden.unHide(P.UPDATE), this.update); } - if (this.input != null) { + if (withDetails && this.input != null) { map.put(Hidden.unHide(P.INPUT), this.input); } - if (this.result != null) { + if (withDetails && this.result != null) { map.put(Hidden.unHide(P.RESULT), this.result); } diff --git a/hugegraph-core/src/main/java/com/baidu/hugegraph/task/HugeTaskScheduler.java b/hugegraph-core/src/main/java/com/baidu/hugegraph/task/HugeTaskScheduler.java index d02fbe5fde..f012d5d5c5 100644 --- a/hugegraph-core/src/main/java/com/baidu/hugegraph/task/HugeTaskScheduler.java +++ b/hugegraph-core/src/main/java/com/baidu/hugegraph/task/HugeTaskScheduler.java @@ -138,8 +138,10 @@ private Future submitTask(HugeTask task) { public void cancel(HugeTask task) { E.checkArgumentNotNull(task, "Task can't be null"); - this.tasks.remove(task.id()); - task.cancel(true); + if (!task.completed()) { + task.cancel(true); + this.remove(task.id()); + } } protected void remove(Id id) { @@ -212,17 +214,34 @@ public Iterator> findTask(Status status, long limit) { public HugeTask deleteTask(Id id) { HugeTask task = this.tasks.get(id); + /* + * Tasks are removed from memory after completed at most time, + * but there is a tiny gap between tasks are completed and + * removed from memory. + * We assume tasks only in memory may be incomplete status, + * in fact, it is also possible to appear on the backend tasks + * when the database status is inconsistent. + */ if (task != null) { E.checkState(task.completed(), - "Can't delete task '%s' in status %s", + "Can't delete incomplete task '%s' in status '%s'. " + + "Please try to cancel the task first", id, task.status()); + this.remove(id); } return this.submit(() -> { + HugeTask result = null; Iterator vertices = this.tx().queryVertices(id); if (vertices.hasNext()) { - this.tx().removeVertex((HugeVertex) vertices.next()); + HugeVertex vertex = (HugeVertex) vertices.next(); + result = HugeTask.fromVertex(vertex); + E.checkState(result.completed(), + "Can't delete incomplete task '%s' in status '%s'", + id, result.status()); + this.tx().removeVertex(vertex); assert !vertices.hasNext(); } + return result; }); }