Skip to content
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,14 @@ protected List<String> tableNames() {
.collect(Collectors.toList());
}

protected List<String> getTableNamesExcludingMeta() {
// Exclude meta table to preserve system metadata during graph clear
return this.tables.entrySet().stream()
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Consider defensive programming: check for null HugeType

While unlikely, if e.getKey() is null, this comparison would fail with NPE. Consider using Objects.equals() or put the enum constant on the left side.

Suggested change
return this.tables.entrySet().stream()
.filter(e -> !HugeType.META.equals(e.getKey()))

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The current code 'HugeType.META == e.getKey()' is already NPE-safe regardless of which operand is null, as the '==' operator never throws NullPointerException.

.filter(e -> !(HugeType.META == e.getKey()))
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

‼️ Critical: Potential NullPointerException

The filter condition HugeType.META == e.getKey() may not correctly handle null keys, though this is unlikely in practice. However, the main concern is whether HugeType.META is actually the correct type to filter out.

Consider adding a comment explaining why META type should be preserved during truncation to improve code maintainability.

Suggested change
.filter(e -> !(HugeType.META == e.getKey()))
protected List<String> truncatedTableNames() {
// Exclude META table to preserve system metadata (e.g., version info) during graph clear
return this.tables.entrySet().stream()
.filter(e -> !(HugeType.META == e.getKey()))
.map(e -> e.getValue().table())
.collect(Collectors.toList());
}

.map(e -> e.getValue().table())
Comment on lines +119 to +121
Copy link

Copilot AI Dec 9, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This filter condition !(HugeType.META == e.getKey()) is redundant because HugeType.META is never registered via registerTableManager() and therefore will never exist in the this.tables map. The meta table is added separately in HbaseSystemStore.tableNames() (line 582) by calling this.meta.table().

The fix works correctly because truncatedTableNames() simply doesn't include the meta table at all (since it's not in the map), but the explicit filter is misleading and suggests META might be in the map. Consider removing the filter:

protected List<String> truncatedTableNames() {
    // Exclude meta table to preserve system metadata during graph clear
    return this.tables.values().stream()
                      .map(BackendTable::table)
                      .collect(Collectors.toList());
}
Suggested change
return this.tables.entrySet().stream()
.filter(e -> !(HugeType.META == e.getKey()))
.map(e -> e.getValue().table())
return this.tables.values().stream()
.map(BackendTable::table)

Copilot uses AI. Check for mistakes.
.collect(Collectors.toList());
}

public String namespace() {
return this.namespace;
}
Expand Down Expand Up @@ -371,7 +379,7 @@ public void truncate() {
};

// Truncate tables
List<String> tables = this.tableNames();
List<String> tables = this.getTableNamesExcludingMeta();
Map<String, Future<Void>> futures = new HashMap<>(tables.size());

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import org.apache.hugegraph.meta.MetaManager;
import org.apache.hugegraph.meta.PdMetaDriver;
import org.apache.hugegraph.testutil.Utils;
import org.apache.hugegraph.unit.hbase.HbaseUnitTest;
import org.apache.hugegraph.util.Log;
import org.junit.AfterClass;
import org.junit.Assert;
Expand All @@ -47,7 +48,9 @@
TaskCoreTest.class,
AuthTest.class,
MultiGraphsTest.class,
RamTableTest.class
RamTableTest.class,
/* hbase */
HbaseUnitTest.class,
})
public class CoreTestSuite {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
import org.apache.hugegraph.unit.core.SerialEnumTest;
import org.apache.hugegraph.unit.core.SystemSchemaStoreTest;
import org.apache.hugegraph.unit.core.TraversalUtilTest;
import org.apache.hugegraph.unit.hbase.HbaseUnitTest;
import org.apache.hugegraph.unit.id.EdgeIdTest;
import org.apache.hugegraph.unit.id.IdTest;
import org.apache.hugegraph.unit.id.IdUtilTest;
Expand Down Expand Up @@ -142,6 +143,7 @@
RocksDBSessionTest.class,
RocksDBCountersTest.class,


/* utils */
VersionTest.class,
JsonUtilTest.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
/*
* 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.hbase;

import org.apache.commons.configuration2.Configuration;
import org.apache.hugegraph.backend.store.hbase.HbaseSessions;
import org.apache.hugegraph.backend.store.hbase.HbaseStoreProvider;
import org.apache.hugegraph.config.HugeConfig;
import org.apache.hugegraph.testutil.Utils;
import org.apache.hugegraph.unit.BaseUnitTest;
import org.junit.After;
import org.junit.Before;
import org.junit.Assume;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Naming: Space missing in class declaration

Minor style issue:

Suggested change
public class BaseHbaseUnitTest extends BaseUnitTest {

import java.io.IOException;

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Missing space after class declaration

Suggested change
public class BaseHbaseUnitTest extends BaseUnitTest {

public class BaseHbaseUnitTest extends BaseUnitTest {

private static final String GRAPH_NAME = "test_graph";

protected HugeConfig config;
protected HbaseStoreProvider provider;
protected HbaseSessions sessions;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Missing @Before annotation

The method is named setup() but the annotation @Before is missing. JUnit won't automatically call this method before each test. This could cause tests to fail or behave unexpectedly.

Suggested change
protected HbaseSessions sessions;
@Before
public void setup() throws IOException {


@Before
public void setup() throws IOException {
Configuration conf = Utils.getConf();
String backend = conf.getString("backend", "memory");
// Only run HBase related tests when backend is hbase
Assume.assumeTrue("Skip HBase tests when backend is not hbase",
"hbase".equalsIgnoreCase(backend));
this.config = new HugeConfig(conf);
this.provider = new HbaseStoreProvider();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

‼️ Critical: Potential resource leak in setup()

If any exception occurs after opening stores but before sessions.open(), the opened stores won't be properly closed. This can lead to resource leaks in test execution.

Suggested change
this.provider = new HbaseStoreProvider();
@Before
public void setup() throws IOException {
Configuration conf = Utils.getConf();
this.config = new HugeConfig(conf);
this.provider = new HbaseStoreProvider();
try {
this.provider.open(GRAPH_NAME);
this.provider.loadSystemStore(config).open(config);
this.provider.loadGraphStore(config).open(config);
this.provider.loadSchemaStore(config).open(config);
this.provider.init();
this.sessions = new HbaseSessions(config, GRAPH_NAME,
this.provider.loadGraphStore(config).store());
this.sessions.open();
} catch (Exception e) {
tearDown();
throw e;
}
}

try {
this.provider.open(GRAPH_NAME);
this.provider.loadSystemStore(config).open(config);
this.provider.loadGraphStore(config).open(config);
this.provider.loadSchemaStore(config).open(config);
this.provider.init();
this.sessions = new HbaseSessions(config, GRAPH_NAME, this.provider.loadGraphStore(config).store());
this.sessions.open();
} catch (Exception e) {
tearDown();
LOG.warn("Failed to init Hbasetest ", e);
}

}

@After
public void tearDown() {
if (this.sessions != null) {
try {
this.sessions.close();
} catch (Exception e) {
LOG.warn("Failed to close sessions ", e);
}
}
if (this.provider != null) {
// ensure back is clear
this.provider.truncate();
try {
this.provider.close();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🧹 Code style: inconsistent spacing

Missing space after catch keyword.

Suggested change
this.provider.close();
} catch (Exception e) {

} catch (Exception e) {
LOG.warn("Failed to close provider ", e);
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
* 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.hbase;

import org.apache.hadoop.hbase.client.Result;
import org.apache.hugegraph.backend.store.BackendEntry.BackendIterator;
import org.apache.hugegraph.backend.store.BackendStore;
import org.apache.hugegraph.testutil.Assert;
import org.apache.hugegraph.backend.store.hbase.HbaseSessions;
import org.apache.hugegraph.util.StringEncoding;
import org.junit.Test;

import java.nio.charset.StandardCharsets;

public class HbaseUnitTest extends BaseHbaseUnitTest {

@Test
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Incomplete Test Coverage

The test only verifies that meta version is preserved, but doesn't verify the actual truncation behavior. Consider adding assertions to verify:

  1. Data tables are actually truncated (e.g., insert some data, truncate, verify data is gone)
  2. Meta table content remains intact
  3. The graph can be used normally after truncation

Example enhancement:

@Test
public void testHbaseMetaVersionAfterTruncate() {
    BackendStore systemStore = this.provider.loadSystemStore(config);
    BackendStore graphStore = this.provider.loadGraphStore(config);
    
    // Record initial version
    String beforeVersion = systemStore.storedVersion();
    
    // Insert some test data to verify truncation
    // ... add test data insertion code ...
    
    // Perform truncation
    this.provider.truncate();
    
    // Verify version preserved
    String afterVersion = systemStore.storedVersion();
    Assert.assertEquals(beforeVersion, afterVersion);
    
    // Verify data tables are empty
    // ... add verification code ...
}

public void testHbaseMetaVersionAfterTruncate() {
BackendStore systemStore = this.provider.loadSystemStore(config);

// Record system version before truncation
String beforeVersion = systemStore.storedVersion();

HbaseSessions.Session testsession = this.sessions.session();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Test isolation concern: verify test data cleanup

The test inserts data with specific row keys (row_trunc_v, row_trunc_oe, row_trunc_ie). If this test runs multiple times or fails mid-execution, residual data might affect subsequent runs.

Consider:

  1. Using unique row keys per test run (e.g., append timestamp/UUID)
  2. Adding explicit cleanup in @Before setup
  3. Verifying the @After teardown properly cleans all test data


// Insert test data
testsession.put("g_v", "f".getBytes(StandardCharsets.UTF_8),
"row_trunc_v".getBytes(StandardCharsets.UTF_8), StringEncoding.encode("q"),
StringEncoding.encode("v"));
testsession.put("g_oe", "f".getBytes(StandardCharsets.UTF_8),
"row_trunc_oe".getBytes(StandardCharsets.UTF_8),
StringEncoding.encode("q"), StringEncoding.encode("v"));
testsession.put("g_ie", "f".getBytes(StandardCharsets.UTF_8),
"row_trunc_ie".getBytes(StandardCharsets.UTF_8),
StringEncoding.encode("q"), StringEncoding.encode("v"));
testsession.commit();

// Verify data insertion success
try (
BackendIterator<Result> vIterator = testsession.get("g_v", "f".getBytes(
StandardCharsets.UTF_8), "row_trunc_v".getBytes(StandardCharsets.UTF_8));
BackendIterator<Result> oeIterator = testsession.get("g_oe", "f".getBytes(
StandardCharsets.UTF_8), "row_trunc_oe".getBytes(StandardCharsets.UTF_8));
BackendIterator<Result> ieIterator = testsession.get("g_ie", "f".getBytes(
StandardCharsets.UTF_8), "row_trunc_ie".getBytes(StandardCharsets.UTF_8));
) {
Assert.assertTrue("data should exist", vIterator.hasNext());
Assert.assertTrue("data should exist", oeIterator.hasNext());
Assert.assertTrue("data should exist", ieIterator.hasNext());
}
// Execute truncate operation, clears all graph data but preserves system tables
this.provider.truncate();

// Verify system version remains unchanged after truncation
String afterVersion = systemStore.storedVersion();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Missing space after comma in Assert.assertNotNull

Suggested change
String afterVersion = systemStore.storedVersion();
Assert.assertNotNull("System metadata version should exist", afterVersion);

Assert.assertNotNull("System metadata version should exist", afterVersion);
Assert.assertEquals("System metadata version should remain unchanged after truncation",
beforeVersion, afterVersion);

// Verify data has been cleared
try (
BackendIterator<Result> vIterator = testsession.get("g_v", "f".getBytes(
StandardCharsets.UTF_8), "row_trunc_v".getBytes(StandardCharsets.UTF_8));
BackendIterator<Result> oeIterator = testsession.get("g_oe", "f".getBytes(
StandardCharsets.UTF_8), "row_trunc_oe".getBytes(StandardCharsets.UTF_8));
BackendIterator<Result> ieIterator = testsession.get("g_ie", "f".getBytes(
StandardCharsets.UTF_8), "row_trunc_ie".getBytes(StandardCharsets.UTF_8));
) {
Assert.assertFalse("data should not exist", vIterator.hasNext());
Assert.assertFalse("data should not exist", oeIterator.hasNext());
Assert.assertFalse("data should not exist", ieIterator.hasNext());
}
}
}
Loading