Skip to content

Commit

Permalink
fix bug of aysnc left index delete
Browse files Browse the repository at this point in the history
1. use element id as task name
2. check whether element is already deleted
3. use async mode for HugeGraphServer

Change-Id: Id8471712e2cd9b5f7cff4639b27e6c198e4d1f01
fixed: #247
  • Loading branch information
zhoney committed Jan 15, 2019
1 parent e84ce7a commit 5b348be
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ protected Id asyncRemoveIndexLeft(ConditionQuery query,
HugeElement element) {
RemoveLeftIndexJob callable = new RemoveLeftIndexJob(query, element);
HugeTask<?> task = EphemeralJobBuilder.of(this.graph())
.name(element.name())
.name(element.id().asString())
.job(callable)
.schedule();
return task.id();
Expand Down Expand Up @@ -1199,6 +1199,9 @@ private boolean deletedByError(HugeElement element,
Collection<Id> ilFields,
Map<PropertyKey, Object> incorrectPKs) {
HugeElement elem = this.newestElement(element);
if (elem == null) {
return false;
}
for (Map.Entry<PropertyKey, Object> e : incorrectPKs.entrySet()) {
PropertyKey pk = e.getKey();
Object value = e.getValue();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,12 +93,15 @@ public VertexLabel schemaLabel() {

@Override
public String name() {
assert this.label.idStrategy() == IdStrategy.PRIMARY_KEY;
E.checkState(this.label.idStrategy() == IdStrategy.PRIMARY_KEY,
"Only primary key vertex has name, " +
"but got '%s' with id strategy '%s'",
this, this.label.idStrategy());
if (this.name == null) {
if (this.id != null) {
String[] parts = SplicingIdGenerator.parse(this.id);
E.checkState(parts.length == 2,
"Invalid vertex id '%s'", this.id);
"Invalid primary key vertex id '%s'", this.id);
this.name = parts[1];
} else {
assert this.id == null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
IndexLabelApiTest.class,
VertexApiTest.class,
EdgeApiTest.class,
TaskApiTest.class,
GremlinApiTest.class,
MetricsApiTest.class
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,20 @@ protected static void initEdgeLabel() {
+ "}");
}

protected static void initIndexLabel() {
String path = URL_PREFIX + SCHEMA_ILS;

client.post(path, "{\n"
+ "\"name\": \"personByCity\",\n"
+ "\"base_type\": \"VERTEX_LABEL\",\n"
+ "\"base_value\": \"person\",\n"
+ "\"index_type\": \"SECONDARY\",\n"
+ "\"fields\": [\n"
+ "\"city\"\n"
+ "]\n"
+ "}");
}

protected static void initVertex() {
String path = URL_PREFIX + GRAPH_VERTEX;

Expand Down Expand Up @@ -422,15 +436,32 @@ protected static String assertResponseStatus(int status,
return content;
}

public static Object assertJsonContains(String response, String key) {
public static <T> T assertJsonContains(String response, String key) {
Map<?, ?> json = JsonUtil.fromJson(response, Map.class);
return assertMapContains(json, key);
}

public static Object assertMapContains(Map<?, ?> map, String key) {
@SuppressWarnings("unchecked")
public static <T> T assertMapContains(Map<?, ?> map, String key) {
String message = String.format("Expect contains key '%s' in %s",
key, map);
Assert.assertTrue(message, map.containsKey(key));
return map.get(key);
return (T) map.get(key);
}

@SuppressWarnings("unchecked")
public static Map<?, ?> assertArrayContains(List<Map<?, ?>> list,
String key, Object value) {
String message = String.format("Expect contains {'%s':'%s'} in list %s",
key, value, list);
Map<?, ?> found = null;
for (Map<?, ?> map : list) {
if (map.get(key).equals(value)) {
found = map;
break;
}
}
Assert.assertTrue(message, found != null);
return found;
}
}
103 changes: 103 additions & 0 deletions hugegraph-test/src/main/java/com/baidu/hugegraph/api/TaskApiTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
/*
* Copyright 2017 HugeGraph Authors
*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with this
* work for additional information regarding copyright ownership. The ASF
* licenses this file to You under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/

package com.baidu.hugegraph.api;

import java.util.List;
import java.util.Map;

import javax.ws.rs.core.Response;

import org.junit.Before;
import org.junit.Test;

import com.baidu.hugegraph.testutil.Assert;

import jersey.repackaged.com.google.common.collect.ImmutableList;
import jersey.repackaged.com.google.common.collect.ImmutableMap;

public class TaskApiTest extends BaseApiTest {

private static String path = "/graphs/hugegraph/tasks/";
private static String rebuildPath =
"/graphs/hugegraph/jobs/rebuild/indexlabels/personByCity";
private static String personByCity = "personByCity";
private static Map<String, Object> personByCityIL = ImmutableMap.of(
"name", "personByCity",
"base_type", "VERTEX_LABEL",
"base_value", "person",
"index_type", "SECONDARY",
"fields", ImmutableList.of("city"));

@Before
public void prepareSchema() {
BaseApiTest.initPropertyKey();
BaseApiTest.initVertexLabel();
BaseApiTest.initIndexLabel();
}

@Test
public void testList() {
int taskId = this.rebuild();

Response r = client().get(path);
String content = assertResponseStatus(200, r);
List<Map<?, ?>> tasks = assertJsonContains(content, "tasks");
assertArrayContains(tasks, "id", taskId);

this.waitTaskSuccess(taskId);
r = client().get(path, ImmutableMap.of("status", "RUNNING"));
content = assertResponseStatus(200, r);
tasks = assertJsonContains(content, "tasks");
Assert.assertTrue(tasks.isEmpty());
}

@Test
public void testGet() {
int taskId = this.rebuild();

Response r = client().get(path, String.valueOf(taskId));
String content = assertResponseStatus(200, r);
assertJsonContains(content, "id");
}

@Test
public void testDelete() {
int taskId = this.rebuild();

this.waitTaskSuccess(taskId);
Response r = client().delete(path, String.valueOf(taskId));
assertResponseStatus(204, r);
}

private int rebuild() {
Response r = client().put(rebuildPath, personByCity, personByCityIL);
String content = assertResponseStatus(202, r);
return assertJsonContains(content, "task_id");
}

private void waitTaskSuccess(int task) {
String status;
do {
Response r = client().get(path, String.valueOf(task));
String content = assertResponseStatus(200, r);
status = assertJsonContains(content, "task_status");
} while (!status.equals("success"));
}
}

0 comments on commit 5b348be

Please sign in to comment.