Skip to content

Fix TsFileResource endTime error when insert empty tablet and flush#11404

Merged
xingtanzjr merged 2 commits intomasterfrom
empty_tablet
Oct 28, 2023
Merged

Fix TsFileResource endTime error when insert empty tablet and flush#11404
xingtanzjr merged 2 commits intomasterfrom
empty_tablet

Conversation

@HTHou
Copy link
Contributor

@HTHou HTHou commented Oct 27, 2023

Description

When data is written using the insertTablet() interface and both values are null, exactly the data written is flushed. An empty TsFile file will be generated, the space cannot be deleted, and the generated resource file will be incorrect, and the endTime of the device will be Long.MIN_VALUE. This in turn affects query and merge mechanisms.

Based on this, this PR has the following two optimizations:

  1. In the flush process, it is added that if the resource file is empty, the generated TsFile and.resource file are deleted
  2. Enhance the judgment logic when there is no valid data in MemTable. If there is no valid data, delete the related empty Device data from the resource.

How to test

Execute the following code.

  public static void main(String[] args)
      throws IoTDBConnectionException, StatementExecutionException {
    session =
        new Session.Builder()
            .host(LOCAL_HOST)
            .port(6667)
            .username("root")
            .password("root")
            .version(Version.V_1_0)
            .build();
    session.open(false);
    insertTablet();
    session.executeNonQueryStatement("flush");
    session.close();
  }


  private static void insertTablet() throws IoTDBConnectionException, StatementExecutionException {
    // The schema of measurements of one device
    // only measurementId and data type in MeasurementSchema take effects in Tablet
    List<MeasurementSchema> schemaList = new ArrayList<>();
    schemaList.add(new MeasurementSchema("s1", TSDataType.INT64));
    schemaList.add(new MeasurementSchema("s2", TSDataType.INT64));
    schemaList.add(new MeasurementSchema("s3", TSDataType.INT64));

    Tablet tablet = new Tablet(ROOT_SG1_D1, schemaList, 100);

    // Method 1 to add tablet data
    long timestamp = System.currentTimeMillis();

    for (long row = 0; row < 100; row++) {
      int rowIndex = tablet.rowSize++;
      tablet.addTimestamp(rowIndex, timestamp);
      for (int s = 0; s < 3; s++) {
        tablet.addValue(schemaList.get(s).getMeasurementId(), rowIndex, null);
      }
      if (tablet.rowSize == tablet.getMaxRowNumber()) {
        session.insertTablet(tablet, true);
        tablet.reset();
      }
      timestamp++;
    }

    if (tablet.rowSize != 0) {
      session.insertTablet(tablet);
      tablet.reset();
    }
  }

Then print the TsFileResource file, you will see the endTime is Long.MIN_VALUE.

try {
tsFileProcessor.close();
if (tsFileProcessor.isEmpty()) {
if (tsFileProcessor.isEmpty() || tsFileProcessor.getTsFileResource().isEmpty()) {
Copy link
Member

Choose a reason for hiding this comment

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

hi , tsfile exsit and resource does not exsit, will delete tsfile?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

In this case, tsfile contains no data and resource file exists.
I'll update the PR description later.

Copy link
Member

Choose a reason for hiding this comment

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

ok

@HTHou HTHou changed the title Fix TsFileresource error when insert empty tablet and flush Fix TsFileResource endTime error when insert empty tablet and flush Oct 27, 2023
@sonarqubecloud
Copy link

SonarCloud Quality Gate failed.    Quality Gate failed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 0 Code Smells

0.0% 0.0% Coverage
0.0% 0.0% Duplication

idea Catch issues before they fail your Quality Gate with our IDE extension sonarlint SonarLint

@HTHou HTHou marked this pull request as ready for review October 27, 2023 09:17
Copy link
Member

@wangchao316 wangchao316 left a comment

Choose a reason for hiding this comment

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

LGTM

@codecov-commenter
Copy link

codecov-commenter commented Oct 27, 2023

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 48.59%. Comparing base (e0271e5) to head (856173d).
⚠️ Report is 3721 commits behind head on master.

Additional details and impacted files
@@             Coverage Diff              @@
##             master   #11404      +/-   ##
============================================
+ Coverage     48.58%   48.59%   +0.01%     
- Complexity    24434    24454      +20     
============================================
  Files          2817     2817              
  Lines        173009   173012       +3     
  Branches      20662    20663       +1     
============================================
+ Hits          84049    84072      +23     
+ Misses        88960    88940      -20     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@xingtanzjr xingtanzjr merged commit e45c251 into master Oct 28, 2023
@HTHou HTHou deleted the empty_tablet branch October 28, 2023 09:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants