Skip to content

Commit

Permalink
Make task-list api not return task_input and task_result
Browse files Browse the repository at this point in the history
improve #139

Change-Id: Iac6d1ef85f66efcd5478bcbca2a7fc54a3160f37
  • Loading branch information
javeme committed Nov 5, 2018
1 parent d9075cf commit 9c2c366
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 27 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,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");
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import java.util.Map;

import javax.inject.Singleton;
import javax.ws.rs.BadRequestException;
import javax.ws.rs.DELETE;
import javax.ws.rs.DefaultValue;
import javax.ws.rs.GET;
Expand All @@ -45,6 +44,7 @@
import com.baidu.hugegraph.task.HugeTask;
import com.baidu.hugegraph.task.HugeTaskScheduler;
import com.baidu.hugegraph.task.Status;
import com.baidu.hugegraph.util.E;
import com.baidu.hugegraph.util.Log;
import com.codahale.metrics.annotation.Timed;
import com.google.common.collect.ImmutableMap;
Expand Down Expand Up @@ -78,7 +78,7 @@ public Map<String, List<Object>> list(@Context GraphManager manager,

List<Object> tasks = new ArrayList<>();
while (itor.hasNext()) {
tasks.add(itor.next().asMap());
tasks.add(itor.next().asMap(false));
}
return ImmutableMap.of("tasks", tasks);
}
Expand Down Expand Up @@ -107,16 +107,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);
}

private static Status parseStatus(String status) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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++) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,10 @@ public AuthenticatedUser authenticate(final Map<String, String> 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))) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -346,6 +346,10 @@ protected Object[] asArray() {
}

public Map<String, Object> asMap() {
return this.asMap(true);
}

public Map<String, Object> 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");

Expand All @@ -367,10 +371,10 @@ public Map<String, Object> 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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,10 @@ private <V> Future<?> submitTask(HugeTask<V> task) {

public <V> void cancel(HugeTask<V> task) {
E.checkArgumentNotNull(task, "Task can't be null");
this.tasks.remove(task.id());
task.cancel(false);
if (!task.completed()) {
task.cancel(false);
this.remove(task.id());
}
}

protected void remove(Id id) {
Expand Down Expand Up @@ -212,17 +214,34 @@ public <V> Iterator<HugeTask<V>> findTask(Status status, long limit) {

public <V> HugeTask<V> 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<V> result = null;
Iterator<Vertex> 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;
});
}

Expand Down

0 comments on commit 9c2c366

Please sign in to comment.