imbajin commented on code in PR #2982:
URL: https://github.com/apache/hugegraph/pull/2982#discussion_r3348946112
##########
hugegraph-server/hugegraph-rocksdb/src/main/java/org/apache/hugegraph/backend/store/rocksdb/RocksDBTable.java:
##########
@@ -224,13 +235,15 @@ protected BackendColumnIterator
getById(RocksDBSessions.Session session, Id id)
return BackendColumnIterator.iterator(col);
}
- protected BackendColumnIterator getByIds(RocksDBSessions.Session session,
Set<Id> ids) {
+ protected BackendColumnIterator getByIds(RocksDBSessions.Session session,
+ Collection<Id> ids) {
if (ids.size() == 1) {
return this.getById(session, ids.iterator().next());
}
- List<byte[]> keys = new ArrayList<>(ids.size());
- for (Id id : ids) {
+ Collection<Id> uniqueIds = ids instanceof Set ? ids : new
LinkedHashSet<>(ids);
Review Comment:
‼️ 这里的全局去重会让 Edge 的公开查询路径吞掉非相邻重复 id,属于用户可见的结果集回归。
完整路径是:
```text
graph.edges(e1, e2, e1)
|
v
GraphTransaction.queryEdgesByIds()
|
| IdQuery.query() 只折叠相邻重复 id
| [e1, e2, e1] => query.idsSize()==3, ids.size()==3
v
edges.isEmpty() && query.idsSize() == ids.size()
|
| 命中 fast path,直接返回 backend iterator
v
RocksDBTables.Edge.queryByIds()
|
v
RocksDBTable.getByIds()
|
| new LinkedHashSet<>(ids) 全局去重
v
[e1, e2, e1] => [e1, e2]
```
也就是说,GraphTransaction 只有在检测到重复 id 时才会回到按原始 `ids` 重建结果的慢路径;但当前 fast path
只能识别相邻重复,识别不了 `e1, e2, e1` 这种非相邻重复。旧的 scan/flat-map 路径会按输入 id 展开 3
次查询,新路径在这里被压成 2 个 key,最终返回结果会少一条。
建议不要在 Edge 的这条 fast path 上做 table-level 全局去重,或者在 transaction 层改成能检测任意重复 id
后再决定是否直接返回 backend iterator。
##########
hugegraph-server/hugegraph-test/src/main/java/org/apache/hugegraph/unit/rocksdb/RocksDBTableQueryByIdsTest.java:
##########
@@ -0,0 +1,403 @@
+/*
+ * 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 org.apache.hugegraph.unit.rocksdb;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+
+import org.apache.commons.lang3.tuple.Pair;
+import org.apache.hugegraph.backend.id.Id;
+import org.apache.hugegraph.backend.id.IdGenerator;
+import org.apache.hugegraph.backend.query.IdQuery;
+import org.apache.hugegraph.backend.store.BackendEntry.BackendColumn;
+import org.apache.hugegraph.backend.store.BackendEntry.BackendColumnIterator;
+import org.apache.hugegraph.backend.store.rocksdb.RocksDBSessions;
+import org.apache.hugegraph.backend.store.rocksdb.RocksDBTables;
+import org.apache.hugegraph.testutil.Assert;
+import org.apache.hugegraph.type.HugeType;
+import org.junit.Before;
+import org.junit.Test;
+import org.rocksdb.RocksDBException;
+
+public class RocksDBTableQueryByIdsTest extends BaseRocksDBUnitTest {
+
+ private static final String DATABASE = "db";
+
+ private TestVertexTable vertexTable;
+ private TestEdgeTable edgeOutTable;
+ private TestEdgeTable edgeInTable;
+
+ @Override
+ @Before
+ public void setup() throws RocksDBException {
+ super.setup();
+ this.vertexTable = new TestVertexTable(DATABASE);
+ this.edgeOutTable = new TestEdgeTable(true, DATABASE);
+ this.edgeInTable = new TestEdgeTable(false, DATABASE);
+ this.rocks.createTable(this.vertexTable.table());
+ this.rocks.createTable(this.edgeOutTable.table());
+ this.rocks.createTable(this.edgeInTable.table());
+ }
+
+ @Test
+ public void testVertexQueryByIdsWithAllExistingIds() {
+ Id id1 = IdGenerator.of("v1");
+ Id id2 = IdGenerator.of("v2");
+ Id id3 = IdGenerator.of("v3");
+
+ this.rocks.session().put(this.vertexTable.table(), id1.asBytes(),
getBytes("value1"));
+ this.rocks.session().put(this.vertexTable.table(), id2.asBytes(),
getBytes("value2"));
+ this.rocks.session().put(this.vertexTable.table(), id3.asBytes(),
getBytes("value3"));
+ this.commit();
+
+ List<Id> ids = Arrays.asList(id1, id2, id3);
+ BackendColumnIterator iter =
this.vertexTable.queryByIds(this.rocks.session(), ids);
+
+ Map<String, String> results = toResultMap(iter);
+
+ Assert.assertEquals(3, results.size());
+ Assert.assertEquals("value1", results.get("v1"));
+ Assert.assertEquals("value2", results.get("v2"));
+ Assert.assertEquals("value3", results.get("v3"));
+ }
+
+ @Test
+ public void testVertexQueryByIdsWithExistingAndMissingIdsMixed() {
+ Id id1 = IdGenerator.of("v1");
+ Id id2 = IdGenerator.of("v2");
+ Id id3 = IdGenerator.of("v3");
+
+ this.rocks.session().put(this.vertexTable.table(), id1.asBytes(),
getBytes("value1"));
+ this.rocks.session().put(this.vertexTable.table(), id3.asBytes(),
getBytes("value3"));
+ this.commit();
+
+ List<Id> ids = Arrays.asList(id1, id2, id3);
+ BackendColumnIterator iter =
this.vertexTable.queryByIds(this.rocks.session(), ids);
+
+ Map<String, String> results = toResultMap(iter);
+
+ Assert.assertEquals(2, results.size());
+ Assert.assertEquals("value1", results.get("v1"));
+ Assert.assertEquals("value3", results.get("v3"));
+ Assert.assertFalse(results.containsKey("v2"));
+ }
+
+ @Test
+ public void testVertexQueryByIdsDedupsDuplicateIds() {
+ Id id1 = IdGenerator.of("v1");
+ Id id2 = IdGenerator.of("v2");
+
+ this.rocks.session().put(this.vertexTable.table(), id1.asBytes(),
getBytes("value1"));
+ this.rocks.session().put(this.vertexTable.table(), id2.asBytes(),
getBytes("value2"));
+ this.commit();
+
+ List<Id> ids = Arrays.asList(id1, id2, id1);
Review Comment:
⚠️ 这个 helper-level 测试把 `[id1, id2, id1]` 固化成“RocksDB table 层去重后只返回 2
条”,但它没有覆盖真正会出问题的公开 Edge 查询路径。
需要补一个从 Graph API / GraphTransaction 进入的回归测试,至少覆盖非相邻重复 edge id:
```java
Object e1 = ...;
Object e2 = ...;
List<Edge> edges = ImmutableList.copyOf(graph.edges(e1, e2, e1));
Assert.assertEquals(3, edges.size());
```
图例:
```text
helper test:
TestEdgeTable.queryByIds([e1, e2, e1])
-> 只验证 table 层去重行为
真实风险路径:
graph.edges(e1, e2, e1)
-> GraphTransaction fast path
-> RocksDB Edge queryByIds
-> table 层去重
-> 返回 2 条而不是 3 条
```
如果保留当前测试,它会让“后端吞掉重复 id”看起来像预期行为;但对 `graph.edges(...)` 的公开语义来说,重复输入应保留重复输出,现有
`graph.edges(id, id)` 测试已经固定了这个契约。
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]