diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/memtable/AbstractMemTable.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/memtable/AbstractMemTable.java index 9924a174253b2..62aae4628ab44 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/memtable/AbstractMemTable.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/memtable/AbstractMemTable.java @@ -190,12 +190,12 @@ private IWritableMemChunkGroup createMemChunkGroupIfNotExistAndGet( private IWritableMemChunkGroup createAlignedMemChunkGroupIfNotExistAndGet( IDeviceID deviceId, List schemaList) { - List filteredSchemaList = - schemaList.stream().filter(Objects::nonNull).collect(Collectors.toList()); IWritableMemChunkGroup memChunkGroup = memTableMap.computeIfAbsent( deviceId, k -> { + List filteredSchemaList = + schemaList.stream().filter(Objects::nonNull).collect(Collectors.toList()); seriesNumber += filteredSchemaList.size(); return new AlignedWritableMemChunkGroup(filteredSchemaList, encryptParameter); }); diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/memtable/AlignedWritableMemChunk.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/memtable/AlignedWritableMemChunk.java index af576b92c57bb..8b46ed12f57a3 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/memtable/AlignedWritableMemChunk.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/memtable/AlignedWritableMemChunk.java @@ -224,9 +224,27 @@ protected void handoverAlignedTvList() { @Override public void writeAlignedPoints( long insertTime, Object[] objectValue, List schemaList) { - Object[] reorderedValue = - checkAndReorderColumnValuesInInsertPlan(schemaList, objectValue, null).left; - putAlignedRow(insertTime, reorderedValue); + int columnCount = ensureColumnsAndCount(schemaList); + if (columnCount == list.getTsDataTypes().size() + && isIdentityColumnMapping(schemaList, columnCount)) { + // The insert plan already covers every device column in device order, so the sparse mapping + // arrays would only add an O(columns) allocation per row. + list.putAlignedValue(insertTime, objectValue); + } else { + int[] destColumns = new int[columnCount]; + Object[] sparseValues = new Object[columnCount]; + int position = 0; + for (int i = 0; i < schemaList.size(); i++) { + IMeasurementSchema measurementSchema = schemaList.get(i); + if (measurementSchema == null) { + continue; + } + destColumns[position] = measurementIndexMap.get(measurementSchema.getMeasurementName()); + sparseValues[position] = objectValue[i]; + position++; + } + list.putAlignedValue(insertTime, sparseValues, destColumns); + } if (TVLIST_SORT_THRESHOLD > 0 && list.rowCount() >= TVLIST_SORT_THRESHOLD) { handoverAlignedTvList(); } @@ -241,49 +259,75 @@ public void writeAlignedTablet( int start, int end, TSStatus[] results) { - Pair pair = - checkAndReorderColumnValuesInInsertPlan(schemaList, valueList, bitMaps); - Object[] reorderedColumnValues = pair.left; - BitMap[] reorderedBitMaps = pair.right; - putAlignedTablet(times, reorderedColumnValues, reorderedBitMaps, start, end, results); + int columnCount = ensureColumnsAndCount(schemaList); + int[] destColumns = new int[columnCount]; + Object[] sparseValues = new Object[columnCount]; + BitMap[] sparseBitMaps = bitMaps == null ? null : new BitMap[columnCount]; + int position = 0; + for (int i = 0; i < schemaList.size(); i++) { + IMeasurementSchema measurementSchema = schemaList.get(i); + if (measurementSchema == null) { + continue; + } + destColumns[position] = measurementIndexMap.get(measurementSchema.getMeasurementName()); + sparseValues[position] = valueList[i]; + if (sparseBitMaps != null) { + sparseBitMaps[position] = bitMaps[i]; + } + position++; + } + list.putAlignedValues(times, sparseValues, sparseBitMaps, destColumns, start, end, results); if (TVLIST_SORT_THRESHOLD > 0 && list.rowCount() >= TVLIST_SORT_THRESHOLD) { handoverAlignedTvList(); } } /** - * Register missing measurements in insertion order, then reorder values and bitmaps into the - * memchunk's column order. + * Register missing measurements in insertion order and return the number of columns written by + * this insert plan. + * + *

Only the written columns are materialized by the caller, so a single-measurement write to a + * wide aligned device no longer allocates device-wide value and bitmap arrays. * * @param schemaListInInsertPlan Contains all existed schema in InsertPlan. If some timeseries * have been deleted, there will be null in its slot. - * @return values and bitmaps aligned with this memchunk's schema list */ - private Pair checkAndReorderColumnValuesInInsertPlan( - List schemaListInInsertPlan, Object[] columnValues, BitMap[] bitMaps) { - Object[] reorderedColumnValues = new Object[schemaList.size()]; - BitMap[] reorderedBitMaps = bitMaps == null ? null : new BitMap[schemaList.size()]; + private int ensureColumnsAndCount(List schemaListInInsertPlan) { for (int i = 0; i < schemaListInInsertPlan.size(); i++) { IMeasurementSchema measurementSchema = schemaListInInsertPlan.get(i); - if (measurementSchema != null) { - Integer index = measurementIndexMap.get(measurementSchema.getMeasurementName()); - if (index == null) { - // Keep writes without new measurements on a single-pass path. Only the first missing - // measurement triggers a batch extension and one resize of the reordered arrays. - extendColumnsInInsertPlan(schemaListInInsertPlan, i); - reorderedColumnValues = Arrays.copyOf(reorderedColumnValues, schemaList.size()); - if (reorderedBitMaps != null) { - reorderedBitMaps = Arrays.copyOf(reorderedBitMaps, schemaList.size()); - } - index = measurementIndexMap.get(measurementSchema.getMeasurementName()); - } - reorderedColumnValues[index] = columnValues[i]; - if (bitMaps != null) { - reorderedBitMaps[index] = bitMaps[i]; - } + if (measurementSchema != null + && !measurementIndexMap.containsKey(measurementSchema.getMeasurementName())) { + // Only the first missing measurement triggers a batch extension. + extendColumnsInInsertPlan(schemaListInInsertPlan, i); + break; } } - return new Pair<>(reorderedColumnValues, reorderedBitMaps); + int columnCount = 0; + for (int i = 0; i < schemaListInInsertPlan.size(); i++) { + if (schemaListInInsertPlan.get(i) != null) { + columnCount++; + } + } + return columnCount; + } + + /** + * Whether every column of the insert plan maps to the device column with the same position. Such + * a plan can be written through the dense path without building a column mapping. + */ + private boolean isIdentityColumnMapping( + List schemaListInInsertPlan, int columnCount) { + if (schemaListInInsertPlan.size() != columnCount) { + return false; + } + for (int i = 0; i < schemaListInInsertPlan.size(); i++) { + IMeasurementSchema measurementSchema = schemaListInInsertPlan.get(i); + if (measurementSchema == null + || measurementIndexMap.get(measurementSchema.getMeasurementName()) != i) { + return false; + } + } + return true; } private void extendColumnsInInsertPlan( @@ -589,6 +633,9 @@ public void encodeWorkingAlignedTVList( if (activeSchemaList.isEmpty() && ignoreAllNullRows) { return; } + // Flush reads the raw value/bitmap containers, so the null tails of the open block must be + // materialized before encoding. + ((AlignedTVList) workingListForFlush).materializeOpenBlockForRead(); BitMap allValueColDeletedMap; AlignedTVList alignedWorkingListForFlush = (AlignedTVList) workingListForFlush; @@ -1000,6 +1047,9 @@ private void encodeWithComputedChunkCap( if (activeSchemaList.isEmpty() && ignoreAllNullRows) { return; } + // Flush reads the raw value/bitmap containers, so the null tails of the open block must be + // materialized before encoding. + ((AlignedTVList) workingListForFlush).materializeOpenBlockForRead(); AlignedChunkWriterImpl alignedChunkWriter = new AlignedChunkWriterImpl(activeSchemaList, encryptParameter); diff --git a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/datastructure/AlignedTVList.java b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/datastructure/AlignedTVList.java index 749756f7adcfe..3eba1bfd2a781 100644 --- a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/datastructure/AlignedTVList.java +++ b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/utils/datastructure/AlignedTVList.java @@ -58,12 +58,14 @@ import java.io.DataInputStream; import java.io.IOException; import java.lang.reflect.Array; +import java.util.AbstractList; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.HashSet; import java.util.List; import java.util.Objects; +import java.util.RandomAccess; import java.util.Set; import java.util.stream.Collectors; import java.util.stream.IntStream; @@ -92,6 +94,25 @@ public abstract class AlignedTVList extends TVList { private long materializedBitmapMemoryCost; private long arrayMemCostWithoutPrimitiveArraysAndIndex; + // Cached result of calculateContainerRamCost(null). Structural changes are rare (one per block or + // per new column) compared with writes, so invalidating this cache on structural changes keeps + // the per-write memory reconciliation O(1) instead of re-estimating every column. + private long structureRamCost; + private boolean structureRamCostDirty = true; + + // Per-column state of the block that is currently being appended to. Because rows are + // append-only, + // the last row a column wrote is enough to derive its nulls: every row between two writes of the + // same column in the same block is null, and the unwritten tail is materialized when the block + // closes or before the block is read. Two bytes per column replace the previous long epoch plus + // the growable column-index array. + private short[] blockLastRow; + // Columns that have at least one materialized value array in the open block. + private long[] touchedColumns; + private int touchedColumnCount; + // Whether this list may contain a null value at all. When false, readers skip all null handling. + private boolean hasNull; + /** * A fully prepared partial clone. All allocations and validations are completed before this plan * is returned, so {@link #commit()} only moves already captured references and updates primitive @@ -166,6 +187,8 @@ public synchronized void commit() { dataTypes = types; memoryBinaryChunkSize = new long[dataTypes.size()]; materializedValueArrayCounts = new int[dataTypes.size()]; + blockLastRow = new short[dataTypes.size()]; + touchedColumns = new long[(dataTypes.size() + 63) >>> 6]; values = new ArrayList<>(types.size()); for (int i = 0; i < types.size(); i++) { @@ -243,6 +266,18 @@ public TVList getTvListByColumnIndex( alignedTvList.timeColDeletedMap = this.timeColDeletedMap; alignedTvList.timeDeletedCnt = this.timeDeletedCnt; alignedTvList.materializedBitmapMemoryCost = calculateBitmapRamCost(bitMaps, null); + // Keep the per-column open-block progress so the projected list reports the same nulls as the + // source for the block that is still being appended to. + alignedTvList.hasNull = hasNull; + alignedTvList.blockLastRow = new short[columnIndexList.size()]; + alignedTvList.touchedColumns = new long[(columnIndexList.size() + 63) >>> 6]; + for (int i = 0; i < columnIndexList.size(); i++) { + int sourceColumnIndex = columnIndexList.get(i); + if (sourceColumnIndex != -1 && isTouched(sourceColumnIndex)) { + alignedTvList.blockLastRow[i] = blockLastRow[sourceColumnIndex]; + alignedTvList.markTouched(i); + } + } for (int i = 0; i < columnIndexList.size(); i++) { int columnIndex = columnIndexList.get(i); if (columnIndex != -1 && values.get(i) != null) { @@ -269,6 +304,10 @@ public synchronized AlignedTVList cloneForFlushSort() { Arrays.copyOf(materializedValueArrayCounts, materializedValueArrayCounts.length); cloneList.materializedValueArrayMemCost = materializedValueArrayMemCost; cloneList.materializedBitmapMemoryCost = materializedBitmapMemoryCost; + cloneList.hasNull = hasNull; + cloneList.blockLastRow = Arrays.copyOf(blockLastRow, blockLastRow.length); + cloneList.touchedColumns = Arrays.copyOf(touchedColumns, touchedColumns.length); + cloneList.touchedColumnCount = touchedColumnCount; return cloneList; } @@ -281,6 +320,10 @@ public synchronized AlignedTVList clone() { cloneList.materializedValueArrayCounts = Arrays.copyOf(materializedValueArrayCounts, materializedValueArrayCounts.length); cloneList.materializedValueArrayMemCost = materializedValueArrayMemCost; + cloneList.hasNull = hasNull; + cloneList.blockLastRow = Arrays.copyOf(blockLastRow, blockLastRow.length); + cloneList.touchedColumns = Arrays.copyOf(touchedColumns, touchedColumns.length); + cloneList.touchedColumnCount = touchedColumnCount; return cloneList; } @@ -405,9 +448,23 @@ private synchronized void commitPartialClone(PartialClonePlan plan) { materializedBitmapMemoryCost = plan.sourceBitmapMemoryCost; plan.cloneList.materializedBitmapMemoryCost = plan.cloneBitmapMemoryCost; + // Both lists address the same rows, so the open-block progress is identical until the source + // releases a moved column. + plan.cloneList.hasNull = hasNull; + plan.cloneList.blockLastRow = Arrays.copyOf(blockLastRow, blockLastRow.length); + plan.cloneList.touchedColumns = Arrays.copyOf(touchedColumns, touchedColumns.length); + plan.cloneList.touchedColumnCount = touchedColumnCount; + for (int i = 0; i < dataTypes.size(); i++) { + if (!plan.retainedColumns.contains(i)) { + clearTouched(i); + blockLastRow[i] = 0; + } + } // Refresh the cached per-block cost after the retained columns changed on both lists. refreshArrayMemCostWithoutPrimitiveArrays(); plan.cloneList.refreshArrayMemCostWithoutPrimitiveArrays(); + invalidateStructureRamCost(); + plan.cloneList.invalidateStructureRamCost(); } /** @@ -444,6 +501,8 @@ public synchronized void releaseNonQueryColumns(Set columnsToKeep) { } values.set(i, null); memoryBinaryChunkSize[i] = 0; + clearTouched(i); + blockLastRow[i] = 0; // Release bitmap memory for non-query columns if (bitMaps != null && bitMaps.get(i) != null) { @@ -462,6 +521,7 @@ public synchronized void releaseNonQueryColumns(Set columnsToKeep) { materializedBitmapMemoryCost = calculateBitmapRamCost(bitMaps, columnsToKeep); // Refresh the cached per-block cost after releasing columns refreshArrayMemCostWithoutPrimitiveArrays(); + invalidateStructureRamCost(); } @SuppressWarnings("squid:S3776") // Suppress high Cognitive Complexity warning @@ -479,13 +539,46 @@ public synchronized void putAlignedValue(long timestamp, Object[] value) { markNullValue(i, arrayIndex, elementIndex); continue; } - TSDataType dataType = dataTypes.get(i); - Object valueArray = getOrCreateValueArray(i, arrayIndex, elementIndex); - Type.fromTsDataType(dataType).addValue(elementIndex, columnValue, valueArray); - if (dataType.isBinary()) { - memoryBinaryChunkSize[i] += getBinarySize((Binary) columnValue); + writeCell(i, arrayIndex, elementIndex, columnValue); + } + if (indices != null) { + indices.get(arrayIndex)[elementIndex] = rowCount; + } + rowCount++; + if (sorted) { + if (rowCount > 1 && timestamp < getTime(rowCount - 2)) { + sorted = false; + segmentMovedMap = null; + invalidateStructureRamCost(); + } else { + seqRowCount++; } } + } + + /** + * Write one aligned row whose value array only contains the columns being written. + * + * @param destColumns {@code destColumns[i]} is the column index in this list that receives {@code + * value[i]}. This avoids allocating and scanning a device-wide array when a write only + * touches a few measurement columns. + */ + public synchronized void putAlignedValue(long timestamp, Object[] value, int[] destColumns) { + checkExpansion(); + int arrayIndex = rowCount / ARRAY_SIZE; + int elementIndex = rowCount % ARRAY_SIZE; + maxTime = Math.max(maxTime, timestamp); + minTime = Math.min(minTime, timestamp); + timestamps.get(arrayIndex)[elementIndex] = timestamp; + for (int i = 0; i < destColumns.length; i++) { + int columnIndex = destColumns[i]; + Object columnValue = value[i]; + if (columnValue == null) { + markNullValue(columnIndex, arrayIndex, elementIndex); + continue; + } + writeCell(columnIndex, arrayIndex, elementIndex, columnValue); + } if (indices != null) { indices.get(arrayIndex)[elementIndex] = rowCount; } @@ -494,6 +587,7 @@ public synchronized void putAlignedValue(long timestamp, Object[] value) { if (rowCount > 1 && timestamp < getTime(rowCount - 2)) { sorted = false; segmentMovedMap = null; + invalidateStructureRamCost(); } else { seqRowCount++; } @@ -614,12 +708,14 @@ public void extendColumns(List newDataTypes) { if (newDataTypes.isEmpty()) { return; } + int existingBlockCount = timestamps.size(); + // The block the next row will be appended to. The new column is null for every block before it, + // and that prefix is never materialized: storing its length is enough because rows are + // appended. + int targetBlock = rowCount / ARRAY_SIZE; for (TSDataType dataType : newDataTypes) { - List columnValue = new ArrayList<>(timestamps.size()); - for (int i = 0; i < timestamps.size(); i++) { - // New columns contain only implicit nulls until their first non-null write. - columnValue.add(null); - } + List columnValue = + new NullPrefixedValueList(targetBlock, existingBlockCount - targetBlock); if (bitMaps != null) { bitMaps.add(null); } @@ -630,6 +726,11 @@ public void extendColumns(List newDataTypes) { refreshArrayMemCostWithoutPrimitiveArrays(); memoryBinaryChunkSize = Arrays.copyOf(memoryBinaryChunkSize, dataTypes.size()); materializedValueArrayCounts = Arrays.copyOf(materializedValueArrayCounts, dataTypes.size()); + blockLastRow = Arrays.copyOf(blockLastRow, dataTypes.size()); + long[] grownTouched = new long[(dataTypes.size() + 63) >>> 6]; + System.arraycopy(touchedColumns, 0, grownTouched, 0, touchedColumns.length); + touchedColumns = grownTouched; + invalidateStructureRamCost(); } private Object getObjectByValueIndex(int rowIndex, int columnIndex) { @@ -788,26 +889,148 @@ public boolean isNullValue(int unsortedRowIndex, int columnIndex) { if (values.get(columnIndex).get(arrayIndex) == null) { return true; } + if (!hasNull) { + return false; + } + int elementIndex = unsortedRowIndex % ARRAY_SIZE; + if (arrayIndex == timestamps.size() - 1 && elementIndex >= blockLastRow[columnIndex]) { + // The open block's unwritten tail is not materialized until the block closes or is read. + return true; + } if (bitMaps == null || bitMaps.get(columnIndex) == null || bitMaps.get(columnIndex).get(arrayIndex) == null) { return false; } - int elementIndex = unsortedRowIndex % ARRAY_SIZE; List columnBitMaps = bitMaps.get(columnIndex); return columnBitMaps.get(arrayIndex).isMarked(elementIndex); } + private boolean isTouched(int columnIndex) { + return (touchedColumns[columnIndex >>> 6] & (1L << (columnIndex & 63))) != 0; + } + + private void markTouched(int columnIndex) { + int word = columnIndex >>> 6; + long bit = 1L << (columnIndex & 63); + if ((touchedColumns[word] & bit) == 0) { + touchedColumns[word] |= bit; + touchedColumnCount++; + } + } + + private void clearTouched(int columnIndex) { + int word = columnIndex >>> 6; + long bit = 1L << (columnIndex & 63); + if ((touchedColumns[word] & bit) != 0) { + touchedColumns[word] &= ~bit; + touchedColumnCount--; + } + } + + /** Mark the rows [from, toExclusive) of the open block as null for one column. */ + private void markNullRange(int columnIndex, int arrayIndex, int from, int toExclusive) { + if (from >= toExclusive) { + return; + } + getBitMap(columnIndex, arrayIndex).markRange(from, toExclusive - from); + hasNull = true; + } + + /** + * Materialize the unwritten tail of every column that has a value array in the open block. The + * tail is the null range [last written row, upToExclusive); it is normally materialized when the + * block closes, and also before the block is read or copied. + */ + private void materializeOpenBlockTails(int upToExclusive) { + int arrayIndex = timestamps.size() - 1; + if (arrayIndex < 0) { + return; + } + int limit = Math.min(upToExclusive, ARRAY_SIZE); + for (int columnIndex = 0; columnIndex < dataTypes.size(); columnIndex++) { + if (!isTouched(columnIndex)) { + continue; + } + int lastRow = blockLastRow[columnIndex]; + if (lastRow < limit) { + markNullRange(columnIndex, arrayIndex, lastRow, limit); + } + } + } + + private void resetOpenBlockState() { + if (touchedColumnCount > 0) { + Arrays.fill(blockLastRow, (short) 0); + Arrays.fill(touchedColumns, 0L); + touchedColumnCount = 0; + } + } + + /** + * Write one non-null cell and materialize the null rows this column skipped since its previous + * write in the same block. + */ + private void writeCell(int columnIndex, int arrayIndex, int elementIndex, Object columnValue) { + TSDataType dataType = dataTypes.get(columnIndex); + Object valueArray = getOrCreateValueArray(columnIndex, arrayIndex, elementIndex); + Type.fromTsDataType(dataType).addValue(elementIndex, columnValue, valueArray); + if (dataType.isBinary()) { + memoryBinaryChunkSize[columnIndex] += getBinarySize((Binary) columnValue); + } + int lastRow = blockLastRow[columnIndex]; + if (lastRow != 0 && lastRow < elementIndex) { + markNullRange(columnIndex, arrayIndex, lastRow, elementIndex); + } + blockLastRow[columnIndex] = (short) (elementIndex + 1); + markTouched(columnIndex); + if (touchedColumnCount < dataTypes.size()) { + // A column without a value array in this block is entirely null. + hasNull = true; + } + } + + /** + * Record the rows [elementIdx, elementIdx + len) written for the columns of a batch write. Null + * cells were already masked by {@link #markNullBitmapRange}, so only the per-column progress and + * the gaps before the batch need to be recorded here. + */ + private void recordWrittenSegment(int[] destColumns, int arrayIndex, int elementIdx, int len) { + int columnCount = getColumnCount(destColumns); + for (int j = 0; j < columnCount; j++) { + int columnIndex = getColumnIndex(destColumns, j); + if (values.get(columnIndex).get(arrayIndex) == null) { + continue; + } + int lastRow = blockLastRow[columnIndex]; + if (lastRow != 0 && lastRow < elementIdx) { + markNullRange(columnIndex, arrayIndex, lastRow, elementIdx); + } + blockLastRow[columnIndex] = (short) (elementIdx + len); + } + } + public List> getValues() { return values; } + /** + * Materialize the null tails of the open block so that readers which use the raw value and bitmap + * containers (e.g. the flush encoder) see the same representation as for closed blocks. + */ + public synchronized void materializeOpenBlockForRead() { + materializeOpenBlockTails(rowCount); + } + public BitMap getSegmentMovedMap() { return segmentMovedMap; } protected void updateSegmentMovedMap() { segmentMovedMap = null; + // Sorting may have materialized the lazily created indices container (and this method may + // materialize the moved-segment bitmap), both of which are charged by the container cost. + invalidateStructureRamCost(); if (indices == null) { return; } @@ -962,6 +1185,7 @@ public synchronized Pair delete( * or delete wrong rows. */ public synchronized void deleteColumn(int columnIndex) { + hasNull = true; List columnValues = values.get(columnIndex); if (columnValues == null) { return; @@ -971,6 +1195,7 @@ public synchronized void deleteColumn(int columnIndex) { getBitMap(columnIndex, arrayIndex).markAll(); } } + invalidateStructureRamCost(); } protected Object cloneValue(TSDataType type, Object value) { @@ -1019,8 +1244,16 @@ private void cloneColumnDataTo(AlignedTVList cloneList, Set columnsToCl continue; } - for (Object valueArray : columnValues) { - cloneList.values.get(i).add(cloneValue(dataTypes.get(i), valueArray)); + if (columnValues instanceof NullPrefixedValueList) { + // Preserve the lazy representation, otherwise the deep clone materializes one reference per + // block and reports a different container cost than the source. + cloneList.values.set( + i, ((NullPrefixedValueList) columnValues).cloneColumn(dataTypes.get(i))); + } else { + for (Object valueArray : columnValues) { + Object clonedValueArray = cloneValue(dataTypes.get(i), valueArray); + cloneList.values.get(i).add(clonedValueArray); + } } // Clone bitmap in columnIndex if (bitMaps != null && bitMaps.get(i) != null) { @@ -1048,6 +1281,19 @@ private void cloneColumnDataTo(AlignedTVList cloneList, Set columnsToCl cloneList.bitMaps.add(null); } } + cloneList.invalidateStructureRamCost(); + } + + @Override + protected void checkExpansion() { + if ((rowCount % ARRAY_SIZE) == 0) { + // Closing the previous block materializes its unwritten tails, so the closed block can be + // read + // with the plain bitmap representation. + materializeOpenBlockTails(ARRAY_SIZE); + resetOpenBlockState(); + } + super.checkExpansion(); } @Override @@ -1066,6 +1312,11 @@ protected void clearValue() { } Arrays.fill(materializedValueArrayCounts, 0); materializedValueArrayMemCost = 0; + Arrays.fill(blockLastRow, (short) 0); + Arrays.fill(touchedColumns, 0L); + touchedColumnCount = 0; + hasNull = false; + invalidateStructureRamCost(); } @Override @@ -1080,6 +1331,7 @@ protected void clearBitMap() { } materializedBitmapMemoryCost = 0; segmentMovedMap = null; + invalidateStructureRamCost(); } @Override @@ -1105,6 +1357,7 @@ protected void expandValues() { materializedBitmapMemoryCost += bitmapReferenceRamCost(); } } + invalidateStructureRamCost(); } /** @@ -1157,12 +1410,32 @@ protected TimeValuePair getTimeValuePair( @Override public synchronized void putAlignedValues( long[] time, Object[] value, BitMap[] bitMaps, int start, int end, TSStatus[] results) { + putAlignedValues(time, value, bitMaps, null, start, end, results); + } + + /** + * Write aligned rows whose value and bitmap arrays only contain the columns being written. + * + * @param destColumns {@code destColumns[i]} is the column index in this list that receives {@code + * value[i]} and {@code bitMaps[i]}. When it is null, the arrays are indexed directly by the + * device-wide column index. + */ + @SuppressWarnings("squid:S3776") // Suppress high Cognitive Complexity warning + public synchronized void putAlignedValues( + long[] time, + Object[] value, + BitMap[] bitMaps, + int[] destColumns, + int start, + int end, + TSStatus[] results) { checkExpansion(); int idx = start; updateMinMaxTimeAndSorted(time, start, end); if (!sorted) { segmentMovedMap = null; + invalidateStructureRamCost(); } while (idx < end) { @@ -1173,37 +1446,51 @@ public synchronized void putAlignedValues( if (internalRemaining >= inputRemaining) { // the remaining inputs can fit the last array, copy all remaining inputs into last array System.arraycopy(time, idx, timestamps.get(arrayIdx), elementIdx, inputRemaining); - arrayCopy(value, bitMaps, results, idx, arrayIdx, elementIdx, inputRemaining); + arrayCopy(value, bitMaps, results, destColumns, idx, arrayIdx, elementIdx, inputRemaining); for (int i = 0; i < inputRemaining; i++) { if (indices != null) { indices.get(arrayIdx)[elementIdx + i] = rowCount; } rowCount++; } - markNullBitmapRange(value, bitMaps, results, idx, elementIdx, inputRemaining, arrayIdx); + markNullBitmapRange( + value, bitMaps, results, destColumns, idx, elementIdx, inputRemaining, arrayIdx); + recordWrittenSegment(destColumns, arrayIdx, elementIdx, inputRemaining); break; } else { // the remaining inputs cannot fit the last array, fill the last array and create a new // one and enter the next loop System.arraycopy(time, idx, timestamps.get(arrayIdx), elementIdx, internalRemaining); - arrayCopy(value, bitMaps, results, idx, arrayIdx, elementIdx, internalRemaining); + arrayCopy( + value, bitMaps, results, destColumns, idx, arrayIdx, elementIdx, internalRemaining); for (int i = 0; i < internalRemaining; i++) { if (indices != null) { indices.get(arrayIdx)[elementIdx + i] = rowCount; } rowCount++; } - markNullBitmapRange(value, bitMaps, results, idx, elementIdx, internalRemaining, arrayIdx); + markNullBitmapRange( + value, bitMaps, results, destColumns, idx, elementIdx, internalRemaining, arrayIdx); + recordWrittenSegment(destColumns, arrayIdx, elementIdx, internalRemaining); idx += internalRemaining; checkExpansion(); } } } + private int getColumnCount(int[] destColumns) { + return destColumns == null ? values.size() : destColumns.length; + } + + private static int getColumnIndex(int[] destColumns, int position) { + return destColumns == null ? position : destColumns[position]; + } + private void markNullBitmapRange( Object[] inputValues, BitMap[] bitMaps, TSStatus[] results, + int[] destColumns, int idx, int elementIdx, int len, @@ -1215,27 +1502,32 @@ private void markNullBitmapRange( ? buildResultBitMap(results, idx, elementIdx, len) : null; - for (int j = 0; j < inputValues.length; j++) { + int columnCount = getColumnCount(destColumns); + for (int j = 0; j < columnCount; j++) { + int columnIndex = getColumnIndex(destColumns, j); // A null value array represents an entirely null block, so no bitmap is needed until the // block receives its first non-null value. - if (values.get(j).get(arrayIndex) == null) { + if (values.get(columnIndex).get(arrayIndex) == null) { continue; } /* Fast-path: column is entirely null */ if (inputValues[j] == null) { - getBitMap(j, arrayIndex).markRange(elementIdx, len); + getBitMap(columnIndex, arrayIndex).markRange(elementIdx, len); + hasNull = true; continue; } /* 2.mask the column bitmap */ if (bitMaps != null && bitMaps[j] != null && bitMaps[j].isRangeAnyMarked(idx, len)) { - getBitMap(j, arrayIndex).merge(bitMaps[j], idx, elementIdx, len); + getBitMap(columnIndex, arrayIndex).merge(bitMaps[j], idx, elementIdx, len); + hasNull = true; } /* 3. Overlay result bitmap (failure rows) */ if (resultBitMap != null) { - getBitMap(j, arrayIndex).merge(resultBitMap, elementIdx & 7, elementIdx, len); + getBitMap(columnIndex, arrayIndex).merge(resultBitMap, elementIdx & 7, elementIdx, len); + hasNull = true; } } } @@ -1278,22 +1570,25 @@ private void arrayCopy( Object[] value, BitMap[] bitMaps, TSStatus[] results, + int[] destColumns, int idx, int arrayIndex, int elementIndex, int remaining) { - for (int i = 0; i < values.size(); i++) { + int columnCount = getColumnCount(destColumns); + for (int i = 0; i < columnCount; i++) { if (value[i] == null || !containsNonNullValue(bitMaps, results, i, idx, remaining)) { continue; } - Object targetArray = getOrCreateValueArray(i, arrayIndex, elementIndex); + int columnIndex = getColumnIndex(destColumns, i); + Object targetArray = getOrCreateValueArray(columnIndex, arrayIndex, elementIndex); System.arraycopy(value[i], idx, targetArray, elementIndex, remaining); - if (dataTypes.get(i).isBinary()) { + if (dataTypes.get(columnIndex).isBinary()) { Binary[] binaryValues = (Binary[]) targetArray; // update raw size of Text chunk for (int valueIndex = 0; valueIndex < remaining; valueIndex++) { - memoryBinaryChunkSize[i] += + memoryBinaryChunkSize[columnIndex] += binaryValues[elementIndex + valueIndex] != null ? getBinarySize(binaryValues[elementIndex + valueIndex]) : 0; @@ -1329,10 +1624,13 @@ private Object getOrCreateValueArray(int columnIndex, int arrayIndex, int elemen if (valueArray == null) { valueArray = getPrimitiveArraysByType(dataTypes.get(columnIndex)); columnValues.set(arrayIndex, valueArray); + markTouched(columnIndex); materializedValueArrayCounts[columnIndex]++; materializedValueArrayMemCost += primitiveArrayMemCost(dataTypes.get(columnIndex)); if (elementIndex > 0) { + // The column skipped the rows before its first value in this block. getBitMap(columnIndex, arrayIndex).markRange(0, elementIndex); + hasNull = true; } } return valueArray; @@ -1355,6 +1653,7 @@ private BitMap getBitMap(int columnIndex, int arrayIndex) { localBitMaps.add(null); } bitMaps = localBitMaps; + invalidateStructureRamCost(); } // if the bitmap in columnIndex is null, init the bitmap of this column from the beginning @@ -1365,6 +1664,7 @@ private BitMap getBitMap(int columnIndex, int arrayIndex) { } bitMaps.set(columnIndex, columnBitMaps); materializedBitmapMemoryCost += (long) columnBitMaps.size() * bitmapReferenceRamCost(); + invalidateStructureRamCost(); } // if the bitmap in arrayIndex is null, init the bitmap @@ -1377,6 +1677,7 @@ private BitMap getBitMap(int columnIndex, int arrayIndex) { } private boolean markNullValue(int columnIndex, int arrayIndex, int elementIndex) { + hasNull = true; List columnValues = values.get(columnIndex); if (columnValues == null || columnValues.get(arrayIndex) == null) { return false; @@ -1427,7 +1728,16 @@ public synchronized long getArrayRamSize() { } public synchronized long getStructureRamSize() { - return calculateContainerRamCost(null); + if (structureRamCostDirty) { + structureRamCost = calculateContainerRamCost(null); + structureRamCostDirty = false; + } + return structureRamCost; + } + + /** Mark the cached container cost stale after a structural change. */ + private void invalidateStructureRamCost() { + structureRamCostDirty = true; } public synchronized long getRamSize(Set columnsToClone) { @@ -1457,9 +1767,11 @@ public synchronized long getRamSize(Set columnsToClone) { *

For a retained column, the inner value list is charged with the references of all its slots * (including null placeholders for blocks that were never materialized), so each slot reference * is counted exactly once; the materialized primitive array payload and header are charged once - * per materialized block by {@code materializedValueArrayMemCost}. The timestamps/indices inner - * lists only contribute their list object and backing-array header here, since their primitive - * arrays are already charged per block by {@code + * per materialized block by {@code materializedValueArrayMemCost}. Columns added by {@link + * #extendColumns(List)} do not allocate those null placeholders, so a {@link + * NullPrefixedValueList} is charged for its wrapper and for the prefix only once the prefix array + * is actually allocated. The timestamps/indices inner lists only contribute their list object and + * backing-array header here, since their primitive arrays are already charged per block by {@code * alignedTvListArrayMemCostWithoutPrimitiveArrays}. */ long calculateContainerRamCost(Set retainedColumns) { @@ -1468,6 +1780,8 @@ long calculateContainerRamCost(Set retainedColumns) { size += listRamCostWithReferences(dataTypes); size += RamUsageEstimator.sizeOfLongArray(memoryBinaryChunkSize.length); size += RamUsageEstimator.sizeOfIntArray(materializedValueArrayCounts.length); + size += RamUsageEstimator.sizeOfShortArray(blockLastRow.length); + size += RamUsageEstimator.sizeOfLongArray(touchedColumns.length); size += listRamCostWithoutReferences(timestamps); if (indices != null) { size += listRamCostWithoutReferences(indices); @@ -1479,7 +1793,9 @@ long calculateContainerRamCost(Set retainedColumns) { continue; } List columnValues = values.get(i); - if (columnValues != null) { + if (columnValues instanceof NullPrefixedValueList) { + size += ((NullPrefixedValueList) columnValues).ramCost(); + } else if (columnValues != null) { size += listRamCostWithReferences(columnValues); } } @@ -1876,6 +2192,7 @@ public int serializedSize() { @Override public void serializeToWAL(IWALByteBufferView buffer) { + materializeOpenBlockTails(rowCount); WALWriteUtils.write(TSDataType.VECTOR, buffer); buffer.putInt(dataTypes.size()); for (TSDataType dataType : dataTypes) { @@ -1981,6 +2298,7 @@ public BitMap getAllValueColDeletedMap(int rowCount) { if (rowCount <= 0) { return null; } + materializeOpenBlockTails(rowCount); int blockCount = (rowCount + ARRAY_SIZE - 1) / ARRAY_SIZE; // Preserve the dense fast path: one column without null blocks or bitmaps proves that every row @@ -2801,4 +3119,85 @@ public boolean isNullValue(int rowIndex, int columnIndex) { return outer.isNullValue(valueIndex, validColumnIndex); } } + + /** + * A value column that starts with a run of implicit nulls and only materializes them when a block + * in that prefix receives its first value. Columns added by {@link #extendColumns(List)} use this + * representation so extending a wide device costs O(columns) instead of O(columns * blocks). + */ + private final class NullPrefixedValueList extends AbstractList implements RandomAccess { + + // Number of blocks before the column was created. They are always null and are never + // materialized: the length is enough because rows are append-only. + private int implicitNullCount; + private final List tail = new ArrayList<>(); + + private NullPrefixedValueList(int implicitNullCount, int existingTailSize) { + this.implicitNullCount = implicitNullCount; + for (int i = 0; i < existingTailSize; i++) { + tail.add(null); + } + } + + @Override + public Object get(int index) { + Objects.checkIndex(index, size()); + return index < implicitNullCount ? null : tail.get(index - implicitNullCount); + } + + @Override + public int size() { + return implicitNullCount + tail.size(); + } + + @Override + public Object set(int index, Object element) { + Objects.checkIndex(index, size()); + if (index < implicitNullCount) { + // Writes are append-only, so the implicit null prefix must never be materialized. + throw new IllegalStateException(); + } + return tail.set(index - implicitNullCount, element); + } + + /** Container memory actually retained by this column: the wrapper and the tail list only. */ + private long ramCost() { + return MemoryEstimationHelper.ARRAY_LIST_INSTANCE_SIZE + + NUM_BYTES_OBJECT_REF + + listRamCostWithReferences(tail); + } + + /** Deep-copy this column for a clone, keeping the lazy prefix representation. */ + private NullPrefixedValueList cloneColumn(TSDataType type) { + NullPrefixedValueList clone = new NullPrefixedValueList(implicitNullCount, tail.size()); + for (int i = 0; i < tail.size(); i++) { + clone.tail.set(i, cloneValue(type, tail.get(i))); + } + return clone; + } + + @Override + public boolean add(Object element) { + modCount++; + boolean added = tail.add(element); + invalidateStructureRamCost(); + return added; + } + + @Override + public void add(int index, Object element) { + if (index != size()) { + throw new UnsupportedOperationException(); + } + add(element); + } + + @Override + public void clear() { + modCount++; + implicitNullCount = 0; + tail.clear(); + invalidateStructureRamCost(); + } + } } diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/memtable/AlignedWritableMemChunkTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/memtable/AlignedWritableMemChunkTest.java index 74fe091b32e67..f72244747c7be 100644 --- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/memtable/AlignedWritableMemChunkTest.java +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/memtable/AlignedWritableMemChunkTest.java @@ -147,6 +147,100 @@ public void testTabletWithoutExtensionReordersExistingColumnsAndBitmaps() { chunk.release(); } + @Test + public void testWriteScenariosKeepSparseAndConsecutiveNulls() { + // One test covering the write patterns users hit in practice: dense rows, a single sparse + // measurement, a long run of consecutive nulls for an earlier column, rotating sparse columns, + // explicit nulls and a block boundary; then the flush snapshot and both clone flavours must + // report exactly the same nulls and values. + int originalThreshold = IoTDBDescriptor.getInstance().getConfig().getTvListSortThreshold(); + IoTDBDescriptor.getInstance().getConfig().setTVListSortThreshold(0); + int columns = 9; + int writtenColumns = 8; + int rows = 1220; + List schemas = new ArrayList<>(columns); + for (int column = 0; column < columns; column++) { + schemas.add(new MeasurementSchema("s" + column, TSDataType.INT64)); + } + AlignedWritableMemChunk chunk = new AlignedWritableMemChunk(new ArrayList<>(schemas)); + Long[][] expected = new Long[rows][columns]; + try { + // Dense rows. + Object[] denseValues = new Object[writtenColumns]; + for (int row = 0; row < 200; row++) { + for (int column = 0; column < writtenColumns; column++) { + denseValues[column] = (long) row * 100 + column; + expected[row][column] = (Long) denseValues[column]; + } + chunk.writeAlignedPoints(row, denseValues, schemas.subList(0, writtenColumns)); + } + // One sparse measurement per row. + for (int row = 200; row < 400; row++) { + write(chunk, schemas, row, 0, (long) row, expected); + } + // Column 0 is null for 600 consecutive rows while only column 1 is written. + for (int row = 400; row < 1000; row++) { + write(chunk, schemas, row, 1, (long) row, expected); + } + // Rotating sparse columns across the block boundary. + for (int row = 1000; row < 1200; row++) { + write(chunk, schemas, row, (row - 1000) % writtenColumns, (long) row, expected); + } + // Explicit nulls must stay null. + for (int row = 1200; row < rows; row++) { + write(chunk, schemas, row, 2, null, expected); + } + + AlignedTVList list = chunk.getWorkingTVList(); + Assert.assertEquals(rows, list.rowCount()); + // Column 3 is never written: every block must stay a null block instead of a default-value + // array, exactly like before the sparse-write rework. + Assert.assertNull(list.getValues().get(writtenColumns).get(0)); + Assert.assertNull(list.getValues().get(writtenColumns).get(1)); + assertContent(list, expected); + // The flush snapshot must materialize the same representation the encoder reads. + list.materializeOpenBlockForRead(); + assertContent(list, expected); + assertContent(list.clone(), expected); + assertContent(list.cloneForFlushSort(), expected); + } finally { + chunk.release(); + IoTDBDescriptor.getInstance().getConfig().setTVListSortThreshold(originalThreshold); + } + } + + private static void write( + AlignedWritableMemChunk chunk, + List schemas, + int row, + int column, + Long value, + Long[][] expected) { + chunk.writeAlignedPoints( + row, new Object[] {value}, Collections.singletonList(schemas.get(column))); + expected[row][column] = value; + } + + private static void assertContent(AlignedTVList list, Long[][] expected) { + for (int row = 0; row < expected.length; row++) { + for (int column = 0; column < expected[row].length; column++) { + Long value = expected[row][column]; + if (value == null) { + Assert.assertTrue( + "row " + row + " column " + column + " must be null", list.isNullValue(row, column)); + } else { + Assert.assertFalse( + "row " + row + " column " + column + " must not be null", + list.isNullValue(row, column)); + Assert.assertEquals( + "row " + row + " column " + column, + value.longValue(), + list.getLongByValueIndex(row, column)); + } + } + } + } + @Test public void testPointsExtendColumnsAfterRemovalAndHandover() { IMeasurementSchema original = new MeasurementSchema("original", TSDataType.INT64); @@ -288,4 +382,34 @@ public void testEncodeFlushesPendingPageBeforeSealingAtChunkCap() { } } } + + @Test + public void testSingleMeasurementWriteToWideDevice() { + int columnCount = 10_000; + List schemas = new ArrayList<>(columnCount); + for (int column = 0; column < columnCount; column++) { + schemas.add(new MeasurementSchema("s" + column, TSDataType.INT64)); + } + AlignedWritableMemChunk chunk = new AlignedWritableMemChunk(schemas); + IMeasurementSchema lastSchema = schemas.get(columnCount - 1); + + chunk.writeAlignedPoints(0, new Object[] {10L}, Collections.singletonList(lastSchema)); + + AlignedTVList list = chunk.getWorkingTVList(); + Assert.assertEquals(1, list.rowCount()); + Assert.assertEquals(10L, list.getLongByValueIndex(0, columnCount - 1)); + for (int column = 0; column < columnCount - 1; column++) { + Assert.assertTrue(list.isNullValue(0, column)); + } + // A second write to a new measurement must not disturb the first row. + IMeasurementSchema newSchema = new MeasurementSchema("new", TSDataType.INT32); + chunk.writeAlignedPoints(1, new Object[] {7}, Collections.singletonList(newSchema)); + Assert.assertEquals(columnCount, chunk.getMeasurementIndex("new")); + Assert.assertEquals(7, list.getIntByValueIndex(1, columnCount)); + for (int column = 0; column < columnCount; column++) { + Assert.assertTrue(list.isNullValue(1, column)); + } + Assert.assertEquals(10L, list.getLongByValueIndex(0, columnCount - 1)); + chunk.release(); + } } diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/memtable/TsFileProcessorTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/memtable/TsFileProcessorTest.java index 116782cdff73a..218ae4f6ee54d 100644 --- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/memtable/TsFileProcessorTest.java +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/memtable/TsFileProcessorTest.java @@ -821,21 +821,21 @@ public void alignedTvListRamCostTest() true, new long[5]); IMemTable memTable = processor.getWorkMemTable(); - Assert.assertEquals(1788704, memTable.getTVListsRamCost()); + Assert.assertEquals(1795112, memTable.getTVListsRamCost()); processor.insertTablet( genInsertTableNode(100, true), Collections.singletonList(new int[] {0, 10}), new TSStatus[10], true, new long[5]); - Assert.assertEquals(1788704, memTable.getTVListsRamCost()); + Assert.assertEquals(1795112, memTable.getTVListsRamCost()); processor.insertTablet( genInsertTableNode(200, true), Collections.singletonList(new int[] {0, 10}), new TSStatus[10], true, new long[5]); - Assert.assertEquals(1788704, memTable.getTVListsRamCost()); + Assert.assertEquals(1795112, memTable.getTVListsRamCost()); Assert.assertEquals(90000, memTable.getTotalPointsNum()); Assert.assertEquals(720360, memTable.memSize()); // Test records @@ -844,7 +844,7 @@ public void alignedTvListRamCostTest() record.addTuple(DataPoint.getDataPoint(dataType, measurementId, String.valueOf(i))); processor.insert(buildInsertRowNodeByTSRecord(record), new long[5]); } - Assert.assertEquals(1790320, memTable.getTVListsRamCost()); + Assert.assertEquals(1796728, memTable.getTVListsRamCost()); Assert.assertEquals(90100, memTable.getTotalPointsNum()); Assert.assertEquals(721560, memTable.memSize()); } @@ -1084,56 +1084,56 @@ public void alignedTvListRamCostTest2() true, new long[5]); IMemTable memTable = processor.getWorkMemTable(); - Assert.assertEquals(1788704, memTable.getTVListsRamCost()); + Assert.assertEquals(1795112, memTable.getTVListsRamCost()); processor.insertTablet( genInsertTableNodeFors3000ToS6000(0, true), Collections.singletonList(new int[] {0, 10}), new TSStatus[10], true, new long[5]); - Assert.assertEquals(4152744, memTable.getTVListsRamCost()); + Assert.assertEquals(3973528, memTable.getTVListsRamCost()); processor.insertTablet( genInsertTableNode(100, true), Collections.singletonList(new int[] {0, 10}), new TSStatus[10], true, new long[5]); - Assert.assertEquals(4152744, memTable.getTVListsRamCost()); + Assert.assertEquals(4249528, memTable.getTVListsRamCost()); processor.insertTablet( genInsertTableNodeFors3000ToS6000(100, true), Collections.singletonList(new int[] {0, 10}), new TSStatus[10], true, new long[5]); - Assert.assertEquals(4152744, memTable.getTVListsRamCost()); + Assert.assertEquals(4249528, memTable.getTVListsRamCost()); processor.insertTablet( genInsertTableNode(200, true), Collections.singletonList(new int[] {0, 10}), new TSStatus[10], true, new long[5]); - Assert.assertEquals(4152744, memTable.getTVListsRamCost()); + Assert.assertEquals(4249528, memTable.getTVListsRamCost()); processor.insertTablet( genInsertTableNodeFors3000ToS6000(200, true), Collections.singletonList(new int[] {0, 10}), new TSStatus[10], true, new long[5]); - Assert.assertEquals(4152744, memTable.getTVListsRamCost()); + Assert.assertEquals(4249528, memTable.getTVListsRamCost()); processor.insertTablet( genInsertTableNode(300, true), Collections.singletonList(new int[] {0, 10}), new TSStatus[10], true, new long[5]); - Assert.assertEquals(5761296, memTable.getTVListsRamCost()); + Assert.assertEquals(5858080, memTable.getTVListsRamCost()); processor.insertTablet( genInsertTableNodeFors3000ToS6000(300, true), Collections.singletonList(new int[] {0, 10}), new TSStatus[10], true, new long[5]); - Assert.assertEquals(7633296, memTable.getTVListsRamCost()); + Assert.assertEquals(7586080, memTable.getTVListsRamCost()); Assert.assertEquals(240000, memTable.getTotalPointsNum()); Assert.assertEquals(1920960, memTable.memSize()); @@ -1143,14 +1143,14 @@ public void alignedTvListRamCostTest2() record.addTuple(DataPoint.getDataPoint(dataType, measurementId, String.valueOf(i))); processor.insert(buildInsertRowNodeByTSRecord(record), new long[5]); } - Assert.assertEquals(7634912, memTable.getTVListsRamCost()); + Assert.assertEquals(7587696, memTable.getTVListsRamCost()); // Test records for (int i = 1; i <= 100; i++) { TSRecord record = new TSRecord(deviceId, i); record.addTuple(DataPoint.getDataPoint(dataType, "s1", String.valueOf(i))); processor.insert(buildInsertRowNodeByTSRecord(record), new long[5]); } - Assert.assertEquals(7636528, memTable.getTVListsRamCost()); + Assert.assertEquals(7589312, memTable.getTVListsRamCost()); Assert.assertEquals(240200, memTable.getTotalPointsNum()); Assert.assertEquals(1923360, memTable.memSize()); } diff --git a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/datastructure/AlignedTVListTest.java b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/datastructure/AlignedTVListTest.java index 9e24080458d26..9dac9965edbc8 100644 --- a/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/datastructure/AlignedTVListTest.java +++ b/iotdb-core/datanode/src/test/java/org/apache/iotdb/db/utils/datastructure/AlignedTVListTest.java @@ -19,6 +19,7 @@ package org.apache.iotdb.db.utils.datastructure; import org.apache.iotdb.common.rpc.thrift.TSStatus; +import org.apache.iotdb.commons.queryengine.execution.MemoryEstimationHelper; import org.apache.iotdb.db.storageengine.dataregion.wal.utils.WALByteBufferForTest; import org.apache.iotdb.rpc.TSStatusCode; @@ -27,6 +28,7 @@ import org.apache.tsfile.external.commons.lang3.ArrayUtils; import org.apache.tsfile.utils.Binary; import org.apache.tsfile.utils.BitMap; +import org.apache.tsfile.utils.RamUsageEstimator; import org.junit.Assert; import org.junit.Test; @@ -415,7 +417,8 @@ public void testPrimitiveArraysAreAllocatedOnFirstWrite() { Assert.assertFalse(tvList.isNullValue(ARRAY_SIZE + 2, 2)); Assert.assertEquals(2, tvList.getIntByValueIndex(ARRAY_SIZE + 2, 2)); // Materializing the extended column charges its primitive array, the lazily created bitmap - // (slot references + bitmap) and the container of the new bitmap structure. + // (slot references + bitmap) and the container of the new bitmap structure. The implicit null + // prefix before the first value is represented by the bitmap only, never by an Object[] prefix. Assert.assertEquals( AlignedTVList.primitiveArrayMemCost(TSDataType.INT32) + 2L * AlignedTVList.bitmapReferenceRamCost() @@ -967,4 +970,270 @@ public void testReleaseNonQueryColumnsWithBitmaps() { Assert.assertEquals((long) (i * 100), tvList.getLongByValueIndex(i, 2)); } } + + @Test + public void testSparseAlignedValuesMapColumnsBitmapsAndResults() { + AlignedTVList tvList = + AlignedTVList.newAlignedList( + new ArrayList<>(Arrays.asList(TSDataType.INT64, TSDataType.BOOLEAN, TSDataType.TEXT))); + int rowCount = ARRAY_SIZE + 2; + long[] times = new long[rowCount]; + long[] first = new long[rowCount]; + Binary[] third = new Binary[rowCount]; + BitMap thirdBitmap = new BitMap(rowCount); + for (int i = 0; i < rowCount; i++) { + times[i] = i; + first[i] = i * 10L; + third[i] = new Binary("v" + i, TSFileConfig.STRING_CHARSET); + } + thirdBitmap.mark(1); + thirdBitmap.mark(ARRAY_SIZE + 1); + TSStatus[] results = new TSStatus[rowCount]; + results[ARRAY_SIZE] = new TSStatus(TSStatusCode.INTERNAL_SERVER_ERROR.getStatusCode()); + + tvList.putAlignedValues( + times, + new Object[] {first, third}, + new BitMap[] {null, thirdBitmap}, + new int[] {0, 2}, + 0, + rowCount, + results); + + Assert.assertEquals(rowCount, tvList.rowCount()); + for (int i = 0; i < rowCount; i++) { + if (i == ARRAY_SIZE) { + // A failed row is masked in every column written by this call. + Assert.assertTrue(tvList.isNullValue(i, 0)); + } else { + Assert.assertEquals(i * 10L, tvList.getLongByValueIndex(i, 0)); + } + Assert.assertTrue(tvList.isNullValue(i, 1)); + if (i == 1 || i == ARRAY_SIZE || i == ARRAY_SIZE + 1) { + Assert.assertTrue(tvList.isNullValue(i, 2)); + } else { + Assert.assertEquals("v" + i, tvList.getBinaryByValueIndex(i, 2).toString()); + } + } + tvList.clear(); + } + + @Test + public void testStructureRamCostCacheTracksStructuralChanges() { + AlignedTVList tvList = + AlignedTVList.newAlignedList( + new ArrayList<>(Arrays.asList(TSDataType.INT64, TSDataType.INT32))); + assertStructureRamCostMatches(tvList); + + tvList.putAlignedValue(0, new Object[] {1L, 2}); + assertStructureRamCostMatches(tvList); + + tvList.extendColumn(TSDataType.TEXT); + assertStructureRamCostMatches(tvList); + + // Materialize a bitmap container for both existing columns. + tvList.putAlignedValue(1, new Object[] {null, null, null}); + assertStructureRamCostMatches(tvList); + + // Cross a block boundary to change the value-column container sizes. + for (int i = 2; i <= ARRAY_SIZE; i++) { + tvList.putAlignedValue(i, new Object[] {(long) i, i, null}); + } + Assert.assertFalse(tvList.getValues().get(0).isEmpty()); + assertStructureRamCostMatches(tvList); + + tvList.releaseNonQueryColumns(Collections.singleton(0)); + assertStructureRamCostMatches(tvList); + + tvList.clear(); + assertStructureRamCostMatches(tvList); + } + + private static void assertStructureRamCostMatches(AlignedTVList tvList) { + long expected = tvList.calculateContainerRamCost(null); + Assert.assertEquals(expected, tvList.getStructureRamSize()); + // The second call exercises the cached path. + Assert.assertEquals(expected, tvList.getStructureRamSize()); + } + + @Test + public void testNullCellsAreNeverMaterializedAsDefaultValues() { + // A block that holds no value for a column must stay "no array at all" so that null is never + // confused with a default value such as 0. This is the same representation the list used before + // the sparse-write rework. + AlignedTVList tvList = + AlignedTVList.newAlignedList( + new ArrayList<>(Arrays.asList(TSDataType.INT64, TSDataType.INT64))); + for (int row = 0; row < ARRAY_SIZE; row++) { + tvList.putAlignedValue(row, new Object[] {(long) row, null}); + } + // Column 1 never got a value in block 0: the slot is null, not a zero-filled array. + Assert.assertNull(tvList.getValues().get(1).get(0)); + Assert.assertTrue(tvList.getBitMaps() == null || tvList.getBitMaps().get(1) == null); + for (int row = 0; row < ARRAY_SIZE; row++) { + Assert.assertTrue(tvList.isNullValue(row, 1)); + } + + // The first real value in block 1 materializes only that block; block 0 stays null. + tvList.putAlignedValue(ARRAY_SIZE, new Object[] {(long) ARRAY_SIZE, 7L}); + Assert.assertNull(tvList.getValues().get(1).get(0)); + Assert.assertNotNull(tvList.getValues().get(1).get(1)); + Assert.assertFalse(tvList.isNullValue(ARRAY_SIZE, 1)); + Assert.assertTrue(tvList.isNullValue(ARRAY_SIZE - 1, 1)); + + // An explicit null is recorded in the bitmap and must not be read back as a value. + tvList.putAlignedValue(ARRAY_SIZE + 1L, new Object[] {(long) ARRAY_SIZE + 1, null}); + Assert.assertTrue(tvList.isNullValue(ARRAY_SIZE + 1, 1)); + Assert.assertTrue(tvList.getBitMaps().get(1).get(1).isMarked(1)); + Assert.assertFalse(tvList.isNullValue(ARRAY_SIZE, 1)); + + // The flush snapshot must report exactly the same nulls. + tvList.materializeOpenBlockForRead(); + Assert.assertNull(tvList.getValues().get(1).get(0)); + for (int row = 0; row < ARRAY_SIZE; row++) { + Assert.assertTrue(tvList.isNullValue(row, 1)); + } + Assert.assertFalse(tvList.isNullValue(ARRAY_SIZE, 1)); + Assert.assertTrue(tvList.isNullValue(ARRAY_SIZE + 1, 1)); + tvList.clear(); + } + + @Test + public void testStructureRamCostTracksLazyIndicesAfterSort() { + AlignedTVList tvList = + AlignedTVList.newAlignedList(new ArrayList<>(Collections.singletonList(TSDataType.INT64))); + // Out-of-order rows force sort() to lazily materialize the indices container and the + // moved-segment bitmap, both of which are part of the container cost. + long[] times = {5, 3, 4, 1, 2}; + for (long time : times) { + tvList.putAlignedValue(time, new Object[] {time}); + } + assertStructureRamCostMatches(tvList); + long beforeSort = tvList.getStructureRamSize(); + + tvList.sort(); + assertStructureRamCostMatches(tvList); + Assert.assertTrue(tvList.getStructureRamSize() >= beforeSort); + Assert.assertNotNull(tvList.getSegmentMovedMap()); + + // Sorting an already sorted list must keep the cached cost valid. + tvList.sort(); + assertStructureRamCostMatches(tvList); + tvList.clear(); + } + + @Test + public void testExtendedColumnsOverManyBlocksPreserveNulls() { + AlignedTVList tvList = + AlignedTVList.newAlignedList(new ArrayList<>(Collections.singletonList(TSDataType.INT64))); + int rows = ARRAY_SIZE * 3 + 7; + for (int i = 0; i < rows; i++) { + tvList.putAlignedValue(i, new Object[] {(long) i}); + } + + int newColumns = 512; + tvList.extendColumns(Collections.nCopies(newColumns, TSDataType.INT32)); + + // Only the last new column is written; the other new columns stay null in every block. + tvList.putAlignedValue(rows, new Object[] {7}, new int[] {newColumns}); + for (int column = 1; column <= newColumns; column++) { + Assert.assertNull(tvList.getValues().get(column).get(0)); + } + Assert.assertEquals(7, tvList.getIntByValueIndex(rows, newColumns)); + for (int column = 1; column < newColumns; column++) { + Assert.assertTrue(tvList.isNullValue(rows, column)); + } + tvList.clear(); + } + + @Test + public void testStructureRamCostIncludesWriteMetadata() { + int columns = 1024; + AlignedTVList tvList = + AlignedTVList.newAlignedList( + new ArrayList<>(Collections.nCopies(columns, TSDataType.INT64))); + + // The per-column open-block state (last written row per column and the touched-column bitset) + // must be reported by the container cost, not only the pre-existing accounting arrays. + long expected = + AlignedTVList.listRamCostWithReferences(tvList.getTsDataTypes()) + + RamUsageEstimator.sizeOfLongArray(columns) + + RamUsageEstimator.sizeOfIntArray(columns) + + RamUsageEstimator.sizeOfShortArray(columns) + + RamUsageEstimator.sizeOfLongArray((columns + 63) >>> 6) + + AlignedTVList.listRamCostWithoutReferences(tvList.getTimestamps()) + + AlignedTVList.listRamCostWithReferences(tvList.getValues()) + + (long) columns * AlignedTVList.listRamCostWithReferences(Collections.emptyList()); + Assert.assertEquals(expected, tvList.getStructureRamSize()); + tvList.clear(); + } + + @Test + public void testLazyColumnPrefixIsAccountedOnlyWhenMaterialized() { + // With many blocks the lazy wrapper is cheaper than one reference per block, which is the + // scenario the representation is meant for. + int blocks = 64; + int rows = blocks * ARRAY_SIZE - 1; + AlignedTVList tvList = + AlignedTVList.newAlignedList(new ArrayList<>(Collections.singletonList(TSDataType.INT64))); + for (int i = 0; i < rows; i++) { + tvList.putAlignedValue(i, new Object[] {(long) i}); + } + Assert.assertNull(tvList.getBitMaps()); + + int extended = 64; + int columnsAfterExtension = 1 + extended; + long beforeExtension = tvList.getStructureRamSize(); + tvList.extendColumns(Collections.nCopies(extended, TSDataType.INT32)); + long afterExtension = tvList.getStructureRamSize(); + + long expectedExtension = + 2L + * (RamUsageEstimator.sizeOfObjectArray(columnsAfterExtension) + - RamUsageEstimator.sizeOfObjectArray(1)) + + (RamUsageEstimator.sizeOfLongArray(columnsAfterExtension) + - RamUsageEstimator.sizeOfLongArray(1)) + + (RamUsageEstimator.sizeOfIntArray(columnsAfterExtension) + - RamUsageEstimator.sizeOfIntArray(1)) + + (RamUsageEstimator.sizeOfShortArray(columnsAfterExtension) + - RamUsageEstimator.sizeOfShortArray(1)) + + (RamUsageEstimator.sizeOfLongArray((columnsAfterExtension + 63) >>> 6) + - RamUsageEstimator.sizeOfLongArray(1)) + + (long) extended * lazyColumnContainerRamCost(); + // Each lazy column also carries one null slot for the open block, so allow a small per-column + // slack instead of pinning the exact wrapper capacity. + long extensionGrowth = afterExtension - beforeExtension; + Assert.assertTrue(extensionGrowth >= expectedExtension); + Assert.assertTrue(extensionGrowth <= expectedExtension + 16L * extended); + // The lazy wrapper costs less than a dense array of block references at this block count. + Assert.assertTrue(lazyColumnContainerRamCost() < RamUsageEstimator.sizeOfObjectArray(blocks)); + assertStructureRamCostMatches(tvList); + + long beforeMaterialization = tvList.getStructureRamSize(); + tvList.putAlignedValue(rows, new Object[] {7}, new int[] {extended}); + long afterMaterialization = tvList.getStructureRamSize(); + + // The implicit null prefix is never materialized; only the bitmap for the written block is + // allocated, and the cached cost must be refreshed. + Assert.assertNull(tvList.getValues().get(extended).get(0)); + Assert.assertNotNull(tvList.getBitMaps()); + long expectedMaterialization = 0; + expectedMaterialization += AlignedTVList.listRamCostWithReferences(tvList.getBitMaps()); + for (List columnBitMaps : tvList.getBitMaps()) { + if (columnBitMaps != null) { + expectedMaterialization += AlignedTVList.listRamCostWithoutReferences(columnBitMaps); + } + } + long materializationGrowth = afterMaterialization - beforeMaterialization; + Assert.assertTrue(materializationGrowth >= expectedMaterialization); + Assert.assertTrue(materializationGrowth <= expectedMaterialization + 1024); + assertStructureRamCostMatches(tvList); + tvList.clear(); + } + + private static long lazyColumnContainerRamCost() { + return MemoryEstimationHelper.ARRAY_LIST_INSTANCE_SIZE + + NUM_BYTES_OBJECT_REF + + AlignedTVList.listRamCostWithReferences(Collections.emptyList()); + } }