Skip to content

Commit aa2acd6

Browse files
Caideyipijt2594838
andauthored
Fix short reads in fixed-length deserialization (#17870)
* Fix short reads in fixed-length deserialization * Fix tag log append EOF handling * spotless --------- Co-authored-by: Jiang Tian <jt2594838@163.com>
1 parent cb97fe4 commit aa2acd6

30 files changed

Lines changed: 444 additions & 73 deletions

File tree

iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/transformation/datastructure/SerializableList.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.iotdb.calc.service.AbstractTemporaryQueryDataFileService;
2323
import org.apache.iotdb.commons.conf.CommonDescriptor;
2424
import org.apache.iotdb.commons.file.SystemFileFactory;
25+
import org.apache.iotdb.commons.utils.IOUtils;
2526

2627
import org.apache.tsfile.utils.PublicBAOS;
2728

@@ -60,7 +61,7 @@ default void deserialize() throws IOException {
6061
}
6162
init();
6263
ByteBuffer byteBuffer = ByteBuffer.allocate(recorder.getSerializedByteLength());
63-
recorder.getFileChannel().read(byteBuffer);
64+
IOUtils.readFully(recorder.getFileChannel(), byteBuffer);
6465
byteBuffer.flip();
6566
deserialize(byteBuffer);
6667
recorder.closeFile();

iotdb-core/calc-commons/src/main/java/org/apache/iotdb/calc/utils/sort/FileSpillerReader.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121

2222
import org.apache.iotdb.calc.utils.datastructure.MergeSortKey;
2323
import org.apache.iotdb.commons.exception.IoTDBException;
24+
import org.apache.iotdb.commons.utils.IOUtils;
2425
import org.apache.iotdb.rpc.TSStatusCode;
2526

2627
import org.apache.tsfile.common.conf.TSFileDescriptor;
@@ -93,10 +94,11 @@ private long read() throws IoTDBException {
9394
if (readLen == -1) {
9495
return -1;
9596
}
97+
IOUtils.readFully(fileChannel, bytes);
9698
bytes.flip();
9799
int capacity = bytes.getInt();
98100
ByteBuffer tsBlockBytes = ByteBuffer.allocate(capacity);
99-
fileChannel.read(tsBlockBytes);
101+
IOUtils.readFully(fileChannel, tsBlockBytes);
100102
tsBlockBytes.flip();
101103
TsBlock cachedTsBlock = serde.deserialize(tsBlockBytes);
102104
cacheBlocks.add(cachedTsBlock);

iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/externalservice/ExternalServiceInfo.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.slf4j.LoggerFactory;
3939

4040
import java.io.ByteArrayOutputStream;
41+
import java.io.DataInputStream;
4142
import java.io.DataOutputStream;
4243
import java.io.File;
4344
import java.io.FileInputStream;
@@ -239,7 +240,7 @@ private ServiceInfo deserializeServiceInfoConsiderCRC(InputStream inputStream)
239240
throws IOException {
240241
int length = ReadWriteIOUtils.readInt(inputStream);
241242
byte[] bytes = new byte[length];
242-
inputStream.read(bytes);
243+
new DataInputStream(inputStream).readFully(bytes);
243244

244245
crc32.reset();
245246
crc32.update(bytes, 0, length);

iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/persistence/ProcedureInfo.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.iotdb.commons.conf.CommonDescriptor;
2424
import org.apache.iotdb.commons.snapshot.SnapshotProcessor;
2525
import org.apache.iotdb.commons.utils.FileUtils;
26+
import org.apache.iotdb.commons.utils.IOUtils;
2627
import org.apache.iotdb.commons.utils.TestOnly;
2728
import org.apache.iotdb.confignode.consensus.request.write.procedure.DeleteProcedurePlan;
2829
import org.apache.iotdb.confignode.consensus.request.write.procedure.UpdateProcedurePlan;
@@ -165,8 +166,16 @@ private static Optional<Procedure> loadProcedure(Path procedureFilePath) {
165166
try (FileInputStream fis = new FileInputStream(procedureFilePath.toFile())) {
166167
Procedure procedure = null;
167168
try (FileChannel channel = fis.getChannel()) {
168-
ByteBuffer byteBuffer = ByteBuffer.allocate(PROCEDURE_LOAD_BUFFER_SIZE);
169-
if (channel.read(byteBuffer) > 0) {
169+
final long fileSize = channel.size();
170+
if (fileSize > PROCEDURE_LOAD_BUFFER_SIZE) {
171+
throw new IOException(
172+
String.format(
173+
"Procedure file %s exceeds the load buffer limit %s, actual size %s",
174+
procedureFilePath, PROCEDURE_LOAD_BUFFER_SIZE, fileSize));
175+
}
176+
ByteBuffer byteBuffer = ByteBuffer.allocate((int) fileSize);
177+
if (fileSize > 0) {
178+
IOUtils.readFully(channel, byteBuffer);
170179
byteBuffer.flip();
171180
procedure = ProcedureFactory.getInstance().create(byteBuffer);
172181
byteBuffer.clear();

iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/writelog/io/SingleFileLogReader.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,7 @@ public boolean hasNext() {
7676
}
7777
buffer = new byte[logSize];
7878

79-
int readLen = logStream.read(buffer, 0, logSize);
80-
if (readLen < logSize) {
81-
throw new IOException(ConfigNodeMessages.REACH_EOF);
82-
}
79+
logStream.readFully(buffer, 0, logSize);
8380

8481
final long checkSum = logStream.readLong();
8582
checkSummer.reset();

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/pipe/consensus/deletion/recover/DeletionReader.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
package org.apache.iotdb.db.pipe.consensus.deletion.recover;
2121

22+
import org.apache.iotdb.commons.utils.IOUtils;
2223
import org.apache.iotdb.db.i18n.DataNodePipeMessages;
2324
import org.apache.iotdb.db.pipe.consensus.deletion.DeletionResource;
2425
import org.apache.iotdb.db.pipe.consensus.deletion.DeletionResourceManager;
@@ -60,7 +61,7 @@ public List<DeletionResource> readAllDeletions() throws IOException {
6061
try {
6162
// Read magic string
6263
ByteBuffer magicStringBuffer = ByteBuffer.allocate(MAGIC_STRING_BYTES_SIZE);
63-
fileChannel.read(magicStringBuffer);
64+
IOUtils.readFully(fileChannel, magicStringBuffer);
6465
magicStringBuffer.flip();
6566
String magicVersion = new String(magicStringBuffer.array(), StandardCharsets.UTF_8);
6667
if (LOGGER.isDebugEnabled()) {
@@ -70,7 +71,7 @@ public List<DeletionResource> readAllDeletions() throws IOException {
7071
// Read deletions
7172
long remainingBytes = fileChannel.size() - fileChannel.position();
7273
ByteBuffer byteBuffer = ByteBuffer.allocate((int) remainingBytes);
73-
fileChannel.read(byteBuffer);
74+
IOUtils.readFully(fileChannel, byteBuffer);
7475
byteBuffer.flip();
7576

7677
List<DeletionResource> deletions = new ArrayList<>();

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/planner/plan/node/write/ObjectNode.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ public static ObjectNode deserializeFromWAL(ByteBuffer buffer) {
158158
if (objectFile.isPresent()) {
159159
try (RandomAccessFile raf = new RandomAccessFile(objectFile.get(), "r")) {
160160
raf.seek(offset);
161-
raf.read(contents);
161+
raf.readFully(contents);
162162
} catch (IOException e) {
163163
throw new RuntimeException(e);
164164
}
@@ -308,7 +308,7 @@ public ByteBuffer serialize() {
308308
private void readContentFromFile(File file, byte[] contents) throws IOException {
309309
try (RandomAccessFile raf = new RandomAccessFile(file, "r")) {
310310
raf.seek(offset);
311-
raf.read(contents);
311+
raf.readFully(contents);
312312
}
313313
}
314314

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/logfile/FakeCRC32Deserializer.java

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030

3131
import java.io.ByteArrayInputStream;
3232
import java.io.DataInputStream;
33-
import java.io.EOFException;
3433
import java.io.IOException;
3534
import java.io.InputStream;
3635
import java.nio.ByteBuffer;
@@ -70,9 +69,7 @@ public T deserialize(InputStream inputStream) throws IOException {
7069
}
7170

7271
byte[] logBuffer = new byte[logLength];
73-
if (logLength < inputStream.read(logBuffer, 0, logLength)) {
74-
throw new EOFException();
75-
}
72+
dataInputStream.readFully(logBuffer, 0, logLength);
7673

7774
T result = deserializer.deserialize(ByteBuffer.wrap(logBuffer));
7875

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/pbtree/schemafile/SchemaFile.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import org.apache.iotdb.commons.schema.SchemaConstant;
2424
import org.apache.iotdb.commons.schema.node.role.IDatabaseMNode;
2525
import org.apache.iotdb.commons.schema.node.utils.IMNodeFactory;
26+
import org.apache.iotdb.commons.utils.IOUtils;
2627
import org.apache.iotdb.commons.utils.PathUtils;
2728
import org.apache.iotdb.commons.utils.TestOnly;
2829
import org.apache.iotdb.consensus.ConsensusFactory;
@@ -360,7 +361,7 @@ private void initFileHeader() throws IOException, MetadataException {
360361
lastSGAddr = 0L;
361362
pageManager = new BTreePageManager(channel, pmtFile, -1, logPath);
362363
} else {
363-
channel.read(headerContent);
364+
IOUtils.readFully(channel, headerContent);
364365
headerContent.clear();
365366
lastPageIndex = ReadWriteIOUtils.readInt(headerContent);
366367
dataTTL = ReadWriteIOUtils.readLong(headerContent);

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/mtree/impl/pbtree/schemafile/log/SchemaFileLogReader.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@
2727
import org.slf4j.Logger;
2828
import org.slf4j.LoggerFactory;
2929

30+
import java.io.DataInputStream;
31+
import java.io.EOFException;
3032
import java.io.File;
3133
import java.io.FileInputStream;
3234
import java.io.IOException;
@@ -91,8 +93,9 @@ public List<byte[]> collectUpdatedEntries() throws IOException, SchemaFileLogCor
9193
}
9294
}
9395

94-
// corrupted within one entry
95-
if (inputStream.read(tempBytes, 1, tempBytes.length - 1) < tempBytes.length - 2) {
96+
try {
97+
new DataInputStream(inputStream).readFully(tempBytes, 1, tempBytes.length - 1);
98+
} catch (EOFException e) {
9699
throw new SchemaFileLogCorruptedException(
97100
logFile.getAbsolutePath(), DataNodeSchemaMessages.SCHEMA_FILE_LOG_INCOMPLETE_ENTRY);
98101
}

0 commit comments

Comments
 (0)