Skip to content

fix(java): preserve null bitmaps when editing deserialized tablets - #1188

Open
DaZuiZui wants to merge 5 commits into
apache:developfrom
DaZuiZui:fix/preserve-deserialized-tablet-bitmaps
Open

DaZuiZui wants to merge 5 commits into
apache:developfrom
DaZuiZui:fix/preserve-deserialized-tablet-bitmaps

Conversation

@DaZuiZui

@DaZuiZui DaZuiZui commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Fixes #1187

What changed

Keep the restored null bitmaps on the first addValue or addTimestamp after Tablet.deserialize. A column with no nulls has no serialized bitmap, so the first edit creates an all-unmarked bitmap for that column. Fresh tablets still start with all cells marked null on first API use. Resetting a restored tablet before its first API edit still allows callers to refill the backing arrays directly.

The change is confined to the Java SDK and leaves the serialized format unchanged.

Verification

  • Wrote the edited tablet to a new TsFile, closed the writer and read the file back. Changing the first row of [70, 80, null] to 71 produced [71, null, null] on 42168eae and [71, 80, null] with this change.
  • Editing an all-non-null column threw NullPointerException on the same baseline. With this change the result was [71, 80, 90].
  • Added regression tests for preserved values and nulls, omitted bitmaps, a timestamp-only edit, and reuse after reset().
  • The full ./mvnw -P with-java clean verify build passed. All 21 tests in TabletTest passed, including the four new regression tests.

@JackieTien97 JackieTien97 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I verified the fix for #1187 with an IoTDB insert, flush, and readback. Editing the first row preserved the value 80 in the second row. I’m requesting changes because a timestamp-only write can still persist a stale value, editing an active row can now throw with a trimmed bitmap, and two tablet-building workloads regressed in JMH. The inline comments include the reproductions and measurements against 42168eae.


maxRowNumber = Math.max(preferredCapacity, Math.max(maxRowNumber, rowSize + another.rowSize));
rowSize = rowSize + another.rowSize;
int firstReservedRow = Math.max(prevCapacity, rowSize);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[P1] Use the previous logical capacity when marking reserved rows. IoTDB’s SessionUtils.filterNullColumns reuses the backing arrays and passes the active row count to the Tablet constructor. I reproduced this with two active rows, arrays of length eight, and 733 left in value slot 3. After editing row 0 to enter AUTO, I appended one row with append(other, 8) and called only addTimestamp(3, 40). prevCapacity is eight, so this calculation skips the null mark for row 3. An IoTDB insertTablet, FLUSH, and query returned (40, 733). The same sequence on the base commit had no value at time 40.

Please capture maxRowNumber before updating it and use the old logical capacity together with the post-append row count to identify the newly reserved rows. A regression test with oversized backing arrays would catch this silent write. IoTDB caller

if (firstNullRow == 0) {
bitMaps[i].markAll();
} else {
bitMaps[i].markRange(firstNullRow, maxRowNumber - firstNullRow);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[P2] Handle bitmaps trimmed to the active row count. I deserialized a 64-row tablet, called setRowSize(2), and replaced the bitmap for its single column with oldMap.getRegion(0, 2). This tablet serializes successfully. On the base commit, addValue("s", 0, 71) produces [71, null]. Here the same edit calls markRange(2, 62) on a two-bit bitmap and throws IndexOutOfBoundsException while updating row 0.

Please extend an existing bitmap before marking beyond its size, or defer marking until the reserved rows are used. A test that trims both the row count and the bitmap before the first edit would cover this case.

}

private void initBitMapsWithApiUsage() {
if (bitMapMode != BitMapMode.AUTO || bitMaps == null) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[P2] Address the regression in name-based inserts on a reused tablet. IoTDB’s records-to-tablet conversion calls addTimestamp once per row and addValue(String, int, Object) for each measurement. I benchmarked that loop with reset() between batches, 1,024 rows × 16 INT32 columns, and one null per 16 cells. On an M2 Pro with JDK 21.0.8, single-threaded JMH measured 144.37 µs/batch on base and 153.42 µs/batch on this commit (+6.3%). I alternated revisions over five pairs of independent JVM forks with identical dependencies. All five PR forks were slower. The timed operation ends before RPC encoding.

Please profile the steady-state case with the bitmaps already initialized and recheck this workload after adjusting the fast path. IoTDB write loop

rowSize = rowSize + another.rowSize;
int firstReservedRow = Math.max(prevCapacity, rowSize);
if (bitMapMode == BitMapMode.AUTO && firstReservedRow < maxRowNumber) {
initializeBitMapsFromRow(firstReservedRow);

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[P2] Preserve sparse bitmaps when append only reserves capacity. After IoTDB compacts an API-filled tablet with no nulls, bitMaps can be null while the mode is still AUTO. Growing the tablet here allocates a full bitmap for every column, even when both input tablets contain only present values and the caller only needs to append.

I measured filling 16 rows × 64 BOOLEAN columns, compacting the bitmaps, and appending another 16 rows with preferred capacity 4,096. The workload allocated 37,136 extra bytes per batch. Across four independent JVM forks per revision on JDK 21, median time went from 18.04 to 21.23 µs/batch (+17.7%). Please defer bitmap allocation until a reserved row needs null flags and cover both append-only and timestamp-only writes. IoTDB bitmap compaction

Comment on lines +94 to +98
private enum BitMapMode {
DIRECT,
POPULATED,
AUTO
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Better to add some comments explaining the behavior of each mode.

Use the previous logical capacity when growing existing bitmaps. Preserve values written ahead of their timestamps and extend trimmed maps before marking new rows. Keep omitted bitmaps sparse during append and avoid bitmap preparation on the normal API write path.

Cover oversized backing arrays, trimmed and partially compacted bitmaps, and timestamp-only writes. Update the Java SDK reference. CLI behavior and the wire format are unchanged.

Verified with JDK 21 and ./mvnw -P with-java clean verify (27 TabletTest cases). A TsFile write/readback retained values at times 10, 20 and 30 and yielded no stale point at time 40.

Single-thread JMH, Apple M4, JDK 21.0.10, median of three forks. Name-based inserts took 160.87 / 174.32 / 144.47 us per batch on base / reviewed / fixed. Compact append took 14.47 / 17.69 / 14.78 us and allocated 306576 / 343712 / 306584 bytes per batch.
@jt2594838

Copy link
Copy Markdown
Contributor

No additional comments besides the ones already put by JackieTien97.

However, Tablet is initially designed as a data exchange structure, instead of a data processing structure,
which means the receiver is not expected to modify the received tablets.

Some problems are also historical: the first version of Tablet makes its members public instead of exposing APIs.
As a result, in the later encapsulation, we cannot make the fields private because it will break the compatibility,
although we do not wish people to manipulate its fields directly anymore.

@DaZuiZui

DaZuiZui commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

@jt2594838 @JackieTien97 thank review

@DaZuiZui
DaZuiZui requested a review from JackieTien97 December 4, 2026 09:14
@DaZuiZui

DaZuiZui commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

I've pushed the requested changes and would appreciate another review. Thanks!

@JackieTien97 JackieTien97 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Thanks for the updates! I verified that the oversized backing array case, trimmed bitmaps, compacted sparse append, and BitMapMode documentation are all resolved on 54137b51.

Requesting changes for two remaining issues:

  1. getBitMaps() permanently sets bitMapsShared = true, which disables the bitMapsReady fast path for all subsequent reused batches once a tablet has been written via TsFileWriter or encoded by IoTDB Session.insertTablet (+34% to 14.4× regression depending on addTimestamp/addValue order).
  2. The @TsFileApi reference-typed addValue / addObjectPathValue overloads (String, byte[], LocalDate, object path) still return early on null, silently leaving the previous non-null value in place when editing a deserialized or populated Tablet.

Comment on lines +1189 to +1192
if (bitMaps != null) {
bitMapsShared = true;
bitMapsReady = false;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[P1] Avoid permanently disabling bitMapsReady when getBitMaps() is read during flushes or RPC encoding. Every TsFile write (AlignedChunkGroupWriterImpl / NonAlignedChunkGroupWriterImpl) and IoTDB Session insert (TabletEncoder.encodeBitMap / SessionUtils) calls tablet.getBitMaps() to inspect null flags before tablet.reset(). Setting bitMapsShared = true here is sticky for the rest of the tablet's lifetime, so both initializeBitMapsForWrite() (bitMapsReady = !bitMapsShared) and reset() (bitMapsReady = ... && !bitMapsShared) keep bitMapsReady == false on every subsequent batch:

  • In the canonical addTimestamp(r, ts) → addValue(r, c, intVal) → getBitMaps() → reset() loop (1,024 rows × 32 INT32 columns, 3 alternating JVM runs), addTimestamp sees rowIndex >= rowSize on every row and scans all column bitmaps 1,024 times per batch while updateBitMap enters prepareColumnBitMap on every cell, regressing steady-state time from 88 µs/batch on 42168eae to 118 µs/batch on 54137b51 (+34%).
  • When values are written before their row timestamp (addValue(curRow, c, intVal) → addTimestamp(curRow, ts) → getBitMaps() → reset(), as in IoTDB relational tablet routing at Session.java:3155-3161), rowIndex >= rowSize stays true for every cell in the row, invoking initializeBitMapsForWrite() rows × (cols + 1) times (O(rows × cols²)), which regresses from 112 µs/batch on 42168eae to 1,613 µs/batch on 54137b51 (14.4× slower).

Clearing bitMapsReady = false in getBitMaps() / setBitMaps() without keeping a sticky bitMapsShared flag (so initializeBitMapsForWrite() and reset() can re-arm bitMapsReady = true once all column bitmaps are non-null and sized to maxRowNumber) still catches pre-write array edits like target.getBitMaps()[0] = ... while keeping reused tablets on the fast path.

initBitMapsWithApiUsage();
}

public void addValue(final String measurementId, final int rowIndex, final Object value) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

[P2] Mark null bitmaps in the @TsFileApi reference-typed addValue and addObjectPathValue overloads when val == null. addValue(String, int, Object) handles null via updateBitMap(rowIndex, indexOfSchema, value == null), but the @TsFileApi typed overloads—addValue(int, int, String) (L437), addObjectPathValue(int, int, String) (L459), addObjectPathValue(int, int, byte[]) (L478), addValue(int, int, byte[]) (L493), and addValue(int, int, LocalDate) (L513)—all do if (val == null) { return; }.

That early return only worked on fresh tablets whose bitmaps had already been marked all-null. On a deserialized or populated Tablet (including table-model callers using @TsFileApi), calling e.g. copy.addValue(0, 0, (String) null) or copy.addValue(0, 0, (LocalDate) null) neither transitions POPULATED → AUTO nor marks the cell null, silently retaining the old non-null value in row 0. When val == null (after validating the column type, including in addObjectPathValue(int, int, String)), please clear the slot (Binary.EMPTY_VALUE / DateType.EMPTY_DATE) and call updateBitMap(rowIndex, columnIndex, true).

@DaZuiZui
DaZuiZui requested a review from JackieTien97 December 4, 2026 09:52
@DaZuiZui

DaZuiZui commented Dec 4, 2026

Copy link
Copy Markdown
Contributor Author

thank review~~~

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.

bug: editing a deserialized Java Tablet clears values in untouched rows

3 participants