iotdb icon indicating copy to clipboard operation
iotdb copied to clipboard

Fix test SchemaRegionBasicTest

Open kabo87777 opened this issue 3 months ago • 0 comments

Fix non-deterministic test: testGetMatchedDevices in SchemaRegionBasicTest

Description

This PR fixes a non-deterministic test in SchemaRegionBasicTest.testGetMatchedDevices that was failing intermittently due to non-deterministic iteration order in the underlying schema region implementation.

Way to Reproduce

The non-deterministic behavior can be reproduced using NonDex, a tool that detects non-deterministic test behavior:

mvn -pl iotdb-core/datanode edu.illinois:nondex-maven-plugin:2.1.7:nondex \
    -Dtest=org.apache.iotdb.db.metadata.schemaRegion.SchemaRegionBasicTest \
    -DnondexRuns=3

Before the fix:

  • Test would fail with NonDex seed 974622
  • Error message : java.lang.AssertionError: expected:<[ShowDevicesResult{name='root.laptop.d1, rawNodes = null, isAligned = false, templateId = -1}, ShowDevicesResult{name='root.laptop.d1.s2, rawNodes = null, isAligned = false, templateId = -1}]> but was:<[ShowDevicesResult{name='root.laptop.d1, rawNodes = null, isAligned = false, templateId = -1}, ShowDevicesResult{name='root.laptop.d2, rawNodes = null, isAligned = false, templateId = -1}]>

Root Cause

The test was using limit 2 in the getMatchedDevices call, which returned different sets of devices depending on the non-deterministic iteration order of ConcurrentHashMap used throughout the schema region implementation:

  1. Non-deterministic data structure: The schema region uses ConcurrentHashMap for storing child nodes in the MTree implementation
  2. Iteration order dependency: ConcurrentHashMap.values().iterator() does not guarantee consistent ordering
  3. Limit causing different results: With limit 2, the test would get different subsets of the 3 matching devices (root.laptop.d1, root.laptop.d1.s2, root.laptop.d2) depending on iteration order
  4. NonDex detection: NonDex shuffles collection iteration order, exposing this non-deterministic behavior

Fix

  • Removed the limit parameter: Changed from limit 2 to limit 0 (no limit) to retrieve all matching devices
  • Updated expected results: Include all 3 devices that match the filter "laptop.d":
    • root.laptop.d1
    • root.laptop.d1.s2
    • root.laptop.d2
  • Maintained HashSet comparison: The test already uses HashSet comparison which is order-independent

This PR has:

  • [ ] been self-reviewed.
    • [ ] concurrent read
    • [ ] concurrent write
    • [ ] concurrent read and write
  • [ ] added documentation for new or modified features or behaviors.
  • [ ] added Javadocs for most classes and all non-trivial methods.
  • [ ] added or updated version, license, or notice information
  • [ ] added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
  • [ ] added unit tests or modified existing tests to cover new code paths, ensuring the threshold for code coverage.
  • [ ] added integration tests.
  • [ ] been tested in a test IoTDB cluster.

Key changed/added classes (or packages if there are too many classes) in this PR

kabo87777 avatar Oct 05 '25 23:10 kabo87777