Skip to content

Commit a1be25e

Browse files
committed
Fix flush error due to compression ratio (#12953)
1 parent 4500875 commit a1be25e

File tree

4 files changed

+60
-35
lines changed

4 files changed

+60
-35
lines changed

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/flush/CompressionRatio.java

+24-14
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,18 @@ void restore() throws IOException {
144144
int maxRatioIndex = 0;
145145
for (int i = 0; i < ratioFiles.length; i++) {
146146
String[] fileNameArray = ratioFiles[i].getName().split("-");
147-
long diskSize = Long.parseLong(fileNameArray[2]);
148-
if (diskSize > totalDiskSize) {
149-
totalMemorySize = new AtomicLong(Long.parseLong(fileNameArray[1]));
150-
totalDiskSize = diskSize;
151-
maxRatioIndex = i;
147+
// fileNameArray.length != 3 means the compression ratio may be negative, ignore it
148+
if (fileNameArray.length == 3) {
149+
try {
150+
long diskSize = Long.parseLong(fileNameArray[2]);
151+
if (diskSize > totalDiskSize) {
152+
totalMemorySize = new AtomicLong(Long.parseLong(fileNameArray[1]));
153+
totalDiskSize = diskSize;
154+
maxRatioIndex = i;
155+
}
156+
} catch (NumberFormatException ignore) {
157+
// ignore illegal compression file name
158+
}
152159
}
153160
}
154161
LOGGER.debug(
@@ -165,11 +172,18 @@ void restore() throws IOException {
165172
totalDiskSize = 1;
166173
for (int i = 0; i < ratioFilesBeforeV121.length; i++) {
167174
String[] fileNameArray = ratioFilesBeforeV121[i].getName().split("-");
168-
double currentCompressRatio =
169-
Double.parseDouble(fileNameArray[1]) / Double.parseDouble(fileNameArray[2]);
170-
if (getRatio() < currentCompressRatio) {
171-
totalMemorySize = new AtomicLong((long) currentCompressRatio);
172-
maxRatioIndex = i;
175+
// fileNameArray.length != 3 means the compression ratio may be negative, ignore it
176+
if (fileNameArray.length == 3) {
177+
try {
178+
double currentCompressRatio =
179+
Double.parseDouble(fileNameArray[1]) / Double.parseDouble(fileNameArray[2]);
180+
if (getRatio() < currentCompressRatio) {
181+
totalMemorySize = new AtomicLong((long) currentCompressRatio);
182+
maxRatioIndex = i;
183+
}
184+
} catch (NumberFormatException ignore) {
185+
// ignore illegal compression file name
186+
}
173187
}
174188
}
175189
deleteRedundantFilesByIndex(ratioFilesBeforeV121, maxRatioIndex);
@@ -201,10 +215,6 @@ void reset() throws IOException {
201215
totalDiskSize = 0L;
202216
}
203217

204-
public static void decreaseDuplicatedMemorySize(long size) {
205-
totalMemorySize.addAndGet(-size);
206-
}
207-
208218
public static CompressionRatio getInstance() {
209219
return CompressionRatioHolder.INSTANCE;
210220
}

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/memtable/AlignedWritableMemChunk.java

-13
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,8 @@
1919

2020
package org.apache.iotdb.db.storageengine.dataregion.memtable;
2121

22-
import org.apache.iotdb.db.storageengine.dataregion.flush.CompressionRatio;
2322
import org.apache.iotdb.db.storageengine.dataregion.wal.buffer.IWALByteBufferView;
2423
import org.apache.iotdb.db.storageengine.dataregion.wal.utils.WALWriteUtils;
25-
import org.apache.iotdb.db.utils.MemUtils;
2624
import org.apache.iotdb.db.utils.datastructure.AlignedTVList;
2725
import org.apache.iotdb.db.utils.datastructure.TVList;
2826
import org.apache.iotdb.tsfile.common.conf.TSFileDescriptor;
@@ -389,17 +387,6 @@ public void encode(IChunkWriter chunkWriter) {
389387
list.getValueIndex(sortedRowIndex);
390388
}
391389
if (timeDuplicateInfo[sortedRowIndex]) {
392-
if (!list.isNullValue(list.getValueIndex(sortedRowIndex), columnIndex)) {
393-
long recordSize =
394-
MemUtils.getRecordSize(
395-
tsDataType,
396-
tsDataType == TSDataType.TEXT
397-
? list.getBinaryByValueIndex(
398-
list.getValueIndex(sortedRowIndex), columnIndex)
399-
: null,
400-
true);
401-
CompressionRatio.decreaseDuplicatedMemorySize(recordSize);
402-
}
403390
continue;
404391
}
405392
}

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/memtable/WritableMemChunk.java

-8
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,7 @@
2020

2121
import org.apache.iotdb.db.conf.IoTDBConfig;
2222
import org.apache.iotdb.db.conf.IoTDBDescriptor;
23-
import org.apache.iotdb.db.storageengine.dataregion.flush.CompressionRatio;
2423
import org.apache.iotdb.db.storageengine.dataregion.wal.buffer.IWALByteBufferView;
25-
import org.apache.iotdb.db.utils.MemUtils;
2624
import org.apache.iotdb.db.utils.datastructure.TVList;
2725
import org.apache.iotdb.tsfile.exception.write.UnSupportedDataTypeException;
2826
import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
@@ -332,12 +330,6 @@ public void encode(IChunkWriter chunkWriter) {
332330

333331
// skip duplicated data
334332
if ((sortedRowIndex + 1 < list.rowCount() && (time == list.getTime(sortedRowIndex + 1)))) {
335-
long recordSize =
336-
MemUtils.getRecordSize(
337-
tsDataType,
338-
tsDataType == TSDataType.TEXT ? list.getBinary(sortedRowIndex) : null,
339-
true);
340-
CompressionRatio.decreaseDuplicatedMemorySize(recordSize);
341333
continue;
342334
}
343335

iotdb-core/datanode/src/test/java/org/apache/iotdb/db/storageengine/dataregion/flush/CompressionRatioTest.java

+36
Original file line numberDiff line numberDiff line change
@@ -104,4 +104,40 @@ public void testRestore() throws IOException {
104104
// largest diskSize to the memory
105105
assertEquals(2, compressionRatio.getRatio(), 0.1);
106106
}
107+
108+
@Test
109+
public void testRestoreIllegal1() throws IOException {
110+
Files.createFile(
111+
new File(
112+
directory,
113+
String.format(Locale.ENGLISH, CompressionRatio.RATIO_FILE_PATH_FORMAT, 10, 50))
114+
.toPath());
115+
116+
Files.createFile(
117+
new File(
118+
directory,
119+
String.format(Locale.ENGLISH, CompressionRatio.RATIO_FILE_PATH_FORMAT, -1000, 100))
120+
.toPath());
121+
122+
compressionRatio.restore();
123+
124+
// if multiple files exist in the system due to some exceptions, restore the file with the
125+
// largest diskSize to the memory
126+
assertEquals(0.2, compressionRatio.getRatio(), 0.1);
127+
}
128+
129+
@Test
130+
public void testRestoreIllegal2() throws IOException {
131+
132+
Files.createFile(
133+
new File(
134+
directory,
135+
String.format(Locale.ENGLISH, CompressionRatio.RATIO_FILE_PATH_FORMAT, -1000, 100))
136+
.toPath());
137+
138+
compressionRatio.restore();
139+
140+
// if compression ratio from file is negative, assume the compression ratio is 0 / 0 = NaN
141+
assertEquals(Double.NaN, compressionRatio.getRatio(), 0.1);
142+
}
107143
}

0 commit comments

Comments
 (0)