-
Notifications
You must be signed in to change notification settings - Fork 596
optimize: Optimize rockdb batch query performance #2982
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
5ba6d4a
f5405a0
939ace0
502c7df
352f66b
0d9052d
45298f9
223fb28
ce4e2cb
6fe08a7
0391069
ce802b9
c06225c
a99491b
10cb0a4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -182,7 +182,9 @@ protected BackendColumnIterator queryById(RocksDBSessions.Session session, Id id | |
| @Override | ||
| protected BackendColumnIterator queryByIds(RocksDBSessions.Session session, | ||
| Collection<Id> ids) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 PR 描述与实际改动不完全匹配 PR 描述提到 "improves the performance of Gremlin queries... when using RPC-based backends such as HBase and HStore",但代码改动全在 |
||
| // TODO: use getByIds() after batch version multi-get is ready | ||
| if (!session.hasChanges()) { | ||
| return this.getByIds(session, ids); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you think it makes sense, we could either cover HStore in this PR as well, or keep this PR scoped to RocksDB and follow up with a separate PR for HStore so the behavior and performance trade-offs can be reviewed independently.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks! Let's keep this PR focused on RocksDB for now, and I'll handle HStore in a separate follow-up PR.
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 当 但 需要确认: |
||
| } | ||
| return super.queryByIds(session, ids); | ||
| } | ||
| } | ||
|
|
@@ -208,6 +210,15 @@ public static Edge in(String database) { | |
| protected BackendColumnIterator queryById(RocksDBSessions.Session session, Id id) { | ||
| return this.getById(session, id); | ||
| } | ||
|
|
||
| @Override | ||
| protected BackendColumnIterator queryByIds(RocksDBSessions.Session session, | ||
| Collection<Id> ids) { | ||
| if (!session.hasChanges()) { | ||
| return this.getByIds(session, ids); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Testing the fallback path (session.hasChanges() = true) is not feasible in unit tests, as RocksDBStdSessions asserts !this.hasChanges() on all read operations (get/scan) when assertions are enabled. |
||
| } | ||
| return super.queryByIds(session, ids); | ||
| } | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
这样可以减少重复,也降低后续维护时两处不一致的风险。 |
||
|
|
||
| public static class IndexTable extends RocksDBTable { | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -50,6 +50,7 @@ | |||||||||||
| import org.apache.hugegraph.unit.id.SplicingIdGeneratorTest; | ||||||||||||
| import org.apache.hugegraph.unit.mysql.MysqlUtilTest; | ||||||||||||
| import org.apache.hugegraph.unit.mysql.WhereBuilderTest; | ||||||||||||
| import org.apache.hugegraph.unit.rocksdb.RocksDBTableQueryByIdsTest; | ||||||||||||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 import 未按字母序排列
Suggested change
|
||||||||||||
| import org.apache.hugegraph.unit.rocksdb.RocksDBCountersTest; | ||||||||||||
| import org.apache.hugegraph.unit.rocksdb.RocksDBSessionTest; | ||||||||||||
| import org.apache.hugegraph.unit.rocksdb.RocksDBSessionsTest; | ||||||||||||
|
|
@@ -141,6 +142,7 @@ | |||||||||||
| RocksDBSessionsTest.class, | ||||||||||||
| RocksDBSessionTest.class, | ||||||||||||
| RocksDBCountersTest.class, | ||||||||||||
| RocksDBTableQueryByIdsTest.class, | ||||||||||||
|
|
||||||||||||
| /* utils */ | ||||||||||||
| VersionTest.class, | ||||||||||||
|
|
||||||||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,195 @@ | ||
| /* | ||
| * 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.Arrays; | ||
| import java.util.Collection; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
|
|
||
| import org.apache.hugegraph.backend.id.Id; | ||
| import org.apache.hugegraph.backend.id.IdGenerator; | ||
| 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.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 edgeTable; | ||
|
|
||
| @Override | ||
| @Before | ||
| public void setup() throws RocksDBException { | ||
| super.setup(); | ||
| this.vertexTable = new TestVertexTable(DATABASE); | ||
| this.edgeTable = new TestEdgeTable(DATABASE); | ||
| this.rocks.createTable(this.vertexTable.table()); | ||
| this.rocks.createTable(this.edgeTable.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 testVertexQueryByIdsWithDuplicateIds() { | ||
| 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); | ||
| BackendColumnIterator iter = this.vertexTable.queryByIds(this.rocks.session(), ids); | ||
|
|
||
| Map<String, Integer> countMap = new HashMap<>(); | ||
| Map<String, String> results = new HashMap<>(); | ||
| while (iter.hasNext()) { | ||
| BackendColumn col = iter.next(); | ||
| String key = getString(col.name); | ||
| results.put(key, getString(col.value)); | ||
| countMap.put(key, countMap.getOrDefault(key, 0) + 1); | ||
| } | ||
|
|
||
| Assert.assertEquals(2, results.size()); | ||
| Assert.assertEquals("value1", results.get("v1")); | ||
| Assert.assertEquals("value2", results.get("v2")); | ||
| // Verify duplicate ids produce duplicate results | ||
| Assert.assertEquals(Integer.valueOf(2), countMap.get("v1")); | ||
| Assert.assertEquals(Integer.valueOf(1), countMap.get("v2")); | ||
| } | ||
|
|
||
| @Test | ||
| public void testEdgeQueryByIdsWithAllExistingIds() { | ||
| Id id1 = IdGenerator.of("e1"); | ||
| Id id2 = IdGenerator.of("e2"); | ||
|
|
||
| this.rocks.session().put(this.edgeTable.table(), id1.asBytes(), getBytes("edge-value1")); | ||
| this.rocks.session().put(this.edgeTable.table(), id2.asBytes(), getBytes("edge-value2")); | ||
| this.commit(); | ||
|
|
||
| List<Id> ids = Arrays.asList(id1, id2); | ||
| BackendColumnIterator iter = this.edgeTable.queryByIds(this.rocks.session(), ids); | ||
|
|
||
| Map<String, String> results = toResultMap(iter); | ||
|
|
||
| Assert.assertEquals(2, results.size()); | ||
| Assert.assertEquals("edge-value1", results.get("e1")); | ||
| Assert.assertEquals("edge-value2", results.get("e2")); | ||
| } | ||
|
|
||
| /** | ||
| * NOTE: Testing the fallback path (session.hasChanges() == true) is not | ||
| * feasible here because both the optimized multi-get path and the fallback | ||
| * scan-based path ultimately delegate to session.get() / session.scan(), | ||
| * which have a pre-existing assertion `assert !this.hasChanges()` in | ||
| * RocksDBStdSessions. This assertion is disabled in production but fires | ||
| * during unit tests when assertions are enabled. The dispatch logic itself | ||
| * is covered by the implementation in RocksDBTables.Vertex/Edge.queryByIds(). | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
当 session 有未提交变更时走 核心分支逻辑(if/else)只测了一半。建议考虑用 mock session 或在集成测试中补充覆盖 |
||
| */ | ||
|
|
||
| private Map<String, String> toResultMap(BackendColumnIterator iter) { | ||
| Map<String, String> results = new HashMap<>(); | ||
| while (iter.hasNext()) { | ||
| BackendColumn col = iter.next(); | ||
| results.put(getString(col.name), getString(col.value)); | ||
| } | ||
| return results; | ||
| } | ||
|
|
||
| /** | ||
| * Subclass that exposes the protected queryByIds for testing. | ||
| */ | ||
| private static class TestVertexTable extends RocksDBTables.Vertex { | ||
|
|
||
| public TestVertexTable(String database) { | ||
| super(database); | ||
| } | ||
|
|
||
| @Override | ||
| public BackendColumnIterator queryByIds(RocksDBSessions.Session session, | ||
| Collection<Id> ids) { | ||
| return super.queryByIds(session, ids); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Subclass that exposes the protected queryByIds for testing. | ||
| */ | ||
| private static class TestEdgeTable extends RocksDBTables.Edge { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🧹 只测了 out edge 方向
|
||
|
|
||
| public TestEdgeTable(String database) { | ||
| super(true, database); | ||
| } | ||
|
|
||
| @Override | ||
| public BackendColumnIterator queryByIds(RocksDBSessions.Session session, | ||
| Collection<Id> ids) { | ||
| return super.queryByIds(session, ids); | ||
| } | ||
| } | ||
| } | ||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Set<Id>→Collection<Id>语义变化需注意原方法接收
Set<Id>(天然去重),改为Collection<Id>后,传入List时若含重复 ID,RocksDBmultiGet会对同一 key 重复查询并返回重复结果。测试
testVertexQueryByIdsWithDuplicateIds验证了这个行为(id1 返回 2 次),但这与原Set语义不一致。需要确认上层IdQuery的 ids 是否可能含重复——如果含重复,行为变更可能导致上层重复处理数据。建议:在
getByIds入口去重以保持原语义,或者明确文档说明Collection含重复返回的行为变更。