apache / incubator-hugegraph

A graph database that supports more than 100+ billion data, high performance and scalability (Include OLTP Engine & REST-API & Backends)
https://hugegraph.apache.org
Apache License 2.0
2.63k stars 517 forks source link

[Question] create edge use uuid as vertices id, but tips "Invalid vertex id" #2542

Closed aaawuanjun closed 4 months ago

aaawuanjun commented 4 months ago

Problem Type (问题类型)

rest-api (结果不合预期)

Before submit

Environment (环境信息)

Your Question (问题描述)

create edge: image

vertices id is exist: image

Vertex/Edge example (问题点 / 边数据举例)

# create edge
## request:
http://172.16.12.150:8080/graphs/hugegraph/graph/edges

request body:

{
  "label": "dsToTaskLineageEdgeLabelTTI",
  "inV": "a9a0e424-129b-487d-a089-6372673ebedc",
  "inVLabel": "taskLineageVertexLabel",
  "outV": "3b567246-d408-461f-b481-a899d1477e0d",
  "outVLabel": "dsLineageVertexLabel",
  "properties": {
      "date": "20171210",
      "weight": 0.4
  }
}

## response:
```text
{
    "exception": "class java.lang.IllegalArgumentException",
    "message": "Invalid vertex id '3b567246-d408-461f-b481-a899d1477e0d'",
    "cause": ""
}


### Schema [VertexLabel, EdgeLabel, IndexLabel] (元数据结构)

_No response_
dosubot[bot] commented 4 months ago

The "Invalid vertex id" error you're encountering when creating an edge using UUIDs as vertex IDs in HugeGraph version 1.0.0 is likely due to a known issue in the checkAndParseVertexId method in the VertexAPI class. This method incorrectly handles UUIDs by removing a leading "U" if it exists, which can lead to incorrect parsing and handling of UUIDs.

Here are the steps to resolve this issue:

  1. Ensure Vertex IDs Exist: You've already verified that the vertex IDs exist, which is good. This step ensures that the IDs you are using are valid and present in the database.

  2. Check UUID Handling in VertexAPI: The checkAndParseVertexId method in the VertexAPI class has a bug that incorrectly handles UUIDs. This method checks if the idValue starts with "U\"", and if so, it removes the leading "U" before parsing the UUID. This can lead to incorrect parsing and handling of UUIDs.

  3. Test Case for UUIDs: The VertexApiTest class includes a test case testGetWithCustomizeUuidId that demonstrates the correct usage of UUIDs as vertex IDs. This test case creates a vertex with a UUID, retrieves it, and verifies that the ID, label, and properties are correctly handled.

  4. Workaround: As a workaround, you can ensure that the UUIDs are correctly formatted and handled by the API. You might need to modify the request to ensure that the UUIDs are correctly parsed. For example, you can try prefixing the UUIDs with "U" to match the expected format in the checkAndParseVertexId method.

    Modified Request Body:

    {
     "label": "dsToTaskLineageEdgeLabelTTI",
     "inV": "U\"a9a0e424-129b-487d-a089-6372673ebedc\"",
     "inVLabel": "taskLineageVertexLabel",
     "outV": "U\"3b567246-d408-461f-b481-a899d1477e0d\"",
     "outVLabel": "dsLineageVertexLabel",
     "properties": {
         "date": "20171210",
         "weight": 0.4
     }
    }

By ensuring that the UUIDs are correctly formatted and handled, you should be able to resolve the "Invalid vertex id" error when creating an edge using UUIDs as vertex IDs. If the issue persists, you may need to look into updating to a newer version of HugeGraph where this bug might have been fixed.

#### Details **Sources** - [hugegraph-client/src/test/java/org/apache/hugegraph/api/EdgeApiTest.java](https://github.com/apache/incubator-hugegraph-toolchain/blob/master/hugegraph-client/src/test/java/org/apache/hugegraph/api/EdgeApiTest.java) - [hugegraph-hubble/hubble-be/src/main/java/org/apache/hugegraph/service/graph/GraphService.java](https://github.com/apache/incubator-hugegraph-toolchain/blob/master/hugegraph-hubble/hubble-be/src/main/java/org/apache/hugegraph/service/graph/GraphService.java) - [hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/graph/EdgeAPI.java](https://github.com/apache/incubator-hugegraph/blob/master/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/graph/EdgeAPI.java) - [hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/graph/VertexAPI.java](https://github.com/apache/incubator-hugegraph/blob/master/hugegraph-server/hugegraph-api/src/main/java/org/apache/hugegraph/api/graph/VertexAPI.java) - [hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/id/IdGenerator.java](https://github.com/apache/incubator-hugegraph/blob/master/hugegraph-server/hugegraph-core/src/main/java/org/apache/hugegraph/backend/id/IdGenerator.java) - [hugegraph-client/src/test/java/org/apache/hugegraph/api/VertexApiTest.java](https://github.com/apache/incubator-hugegraph-toolchain/blob/master/hugegraph-client/src/test/java/org/apache/hugegraph/api/VertexApiTest.java)
**About Dosu** This response is meant to be useful and save you time. It is **not meant to be a precise solution**, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant.

To continue the conversation, mention @dosu.

aaawuanjun commented 4 months ago

it does not work, the code version is 1.3.0

from belows, it produce exception, it does not judge U prefix

    private static Vertex getVertex(HugeGraph graph, Object id, String label) {
        HugeVertex vertex;
        try {
            vertex = (HugeVertex) graph.vertices(id).next();
        } catch (NoSuchElementException e) {
            throw new IllegalArgumentException(String.format("Invalid vertex id '%s'", id));
        }
        if (label != null && !vertex.label().equals(label)) {
            throw new IllegalArgumentException(String.format(
                    "The label of vertex '%s' is unmatched, users expect " +
                    "label '%s', actual label stored is '%s'",
                    id, label, vertex.label()));
        }
        // Clone a new vertex to support multi-thread access
        return vertex.copy();
    }