Skip to content

Commit 59702c7

Browse files
authored
fix: 17841: Backport the fix for 17803 to release 0.59 (#17845)
Signed-off-by: Artem Ananev <[email protected]>
1 parent 5c54dbd commit 59702c7

File tree

6 files changed

+49
-5
lines changed

6 files changed

+49
-5
lines changed

platform-sdk/swirlds-merkledb/src/main/java/com/swirlds/merkledb/MerkleDbDataSource.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2022-2024 Hedera Hashgraph, LLC
2+
* Copyright (C) 2022-2025 Hedera Hashgraph, LLC
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -1081,6 +1081,10 @@ private void runWithSnapshotExecutor(
10811081
(System.currentTimeMillis() - START) * UnitConstants.MILLISECONDS_TO_SECONDS);
10821082
return true; // turns this into a callable, so it can throw checked
10831083
// exceptions
1084+
} catch (final Throwable t) {
1085+
// log and rethrow
1086+
logger.error(EXCEPTION.getMarker(), "[{}] Snapshot {} failed", tableName, taskName, t);
1087+
throw t;
10841088
} finally {
10851089
countDownLatch.countDown();
10861090
}

platform-sdk/swirlds-merkledb/src/main/java/com/swirlds/merkledb/collections/AbstractLongList.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,8 @@ protected void setChunk(int chunkIndex, C chunk) {
306306
}
307307

308308
/**
309-
* Reads a specified range of elements from a file channel into the given buffer.
309+
* Reads a specified range of elements from a file channel into the given buffer, starting from
310+
* the given start index (inc) and up to the given end index (exc).
310311
* <p>
311312
* This method computes the appropriate byte offsets within the buffer and the number of bytes
312313
* to read based on the provided {@code startIndex} and {@code endIndex}. It then performs a

platform-sdk/swirlds-merkledb/src/main/java/com/swirlds/merkledb/collections/LongListDisk.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -300,7 +300,7 @@ protected void writeLongsData(final FileChannel fc) throws IOException {
300300
}
301301
if (i == (totalNumOfChunks - 1)) {
302302
// the last array, so set limit to only the data needed
303-
final long bytesWrittenSoFar = (long) memoryChunkSize * (long) i;
303+
final long bytesWrittenSoFar = (long) memoryChunkSize * i;
304304
final long remainingBytes = (size() * Long.BYTES) - bytesWrittenSoFar;
305305
transferBuffer.limit(toIntExact(remainingBytes));
306306
} else {

platform-sdk/swirlds-merkledb/src/main/java/com/swirlds/merkledb/collections/LongListHeap.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,8 +108,11 @@ protected AtomicLongArray readChunkData(FileChannel fileChannel, int chunkIndex,
108108
AtomicLongArray chunk = createChunk();
109109

110110
readDataIntoBuffer(fileChannel, chunkIndex, startIndex, endIndex, initReadBuffer);
111+
111112
final int startOffset = startIndex * Long.BYTES;
113+
final int endOffset = endIndex * Long.BYTES;
112114
initReadBuffer.position(startOffset);
115+
initReadBuffer.limit(endOffset);
113116

114117
while (initReadBuffer.hasRemaining()) {
115118
int index = initReadBuffer.position() / Long.BYTES;

platform-sdk/swirlds-merkledb/src/main/java/com/swirlds/merkledb/collections/LongListOffHeap.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,10 @@ protected ByteBuffer readChunkData(FileChannel fileChannel, int chunkIndex, int
9191
throws IOException {
9292
ByteBuffer chunk = createChunk();
9393
readDataIntoBuffer(fileChannel, chunkIndex, startIndex, endIndex, chunk);
94+
// All chunks (byte buffers) in LongListOffHeap are stored with position == 0 and
95+
// limit == capacity. When this list is written to a file, the first and the last
96+
// chunk positions and limits are taken care of
97+
chunk.clear();
9498
return chunk;
9599
}
96100

@@ -152,8 +156,8 @@ protected void writeLongsData(final FileChannel fc) throws IOException {
152156
}
153157
if (i == (totalNumOfChunks - 1)) {
154158
// last array, so set limit to only the data needed
155-
final long bytesWrittenSoFar = (long) memoryChunkSize * (long) i;
156-
final long remainingBytes = (size() * Long.BYTES) - bytesWrittenSoFar;
159+
final long bytesWrittenSoFar = (long) memoryChunkSize * i;
160+
final long remainingBytes = size() * Long.BYTES - bytesWrittenSoFar;
157161
buf.limit(toIntExact(remainingBytes));
158162
} else {
159163
buf.limit(buf.capacity());

platform-sdk/swirlds-merkledb/src/test/java/com/swirlds/merkledb/collections/AbstractLongListTest.java

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,38 @@ void testSpliteratorEdgeCases() {
495495
}
496496
}
497497

498+
@Test
499+
void testRestoreUpdateSnapshot() throws IOException {
500+
try (final LongList longList = createFullyParameterizedLongListWith(100, MAX_LONGS)) {
501+
longList.updateValidRange(0, 50);
502+
longList.put(2, 1002);
503+
final Path tmpDir = Files.createTempDirectory("testRestoreUpdateSnapshot");
504+
final Path tmpFile = tmpDir.resolve("snapshot");
505+
longList.writeToFile(tmpFile);
506+
try (final LongList restored = createLongListFromFile(tmpFile)) {
507+
restored.updateValidRange(0, 50);
508+
assertEquals(3, restored.size());
509+
assertEquals(1002, restored.get(2));
510+
restored.put(3, 1003);
511+
final Path tmpDir2 = Files.createTempDirectory("testRestoreUpdateSnapshot");
512+
final Path tmpFile2 = tmpDir2.resolve("snapshot");
513+
restored.writeToFile(tmpFile2);
514+
try (final LongList restored2 = createLongListFromFile(tmpFile2)) {
515+
restored2.updateValidRange(0, 50);
516+
assertEquals(4, restored2.size());
517+
assertEquals(1002, restored2.get(2));
518+
assertEquals(1003, restored2.get(3));
519+
} finally {
520+
Files.delete(tmpFile2);
521+
Files.delete(tmpDir2);
522+
}
523+
} finally {
524+
Files.delete(tmpFile);
525+
Files.delete(tmpDir);
526+
}
527+
}
528+
}
529+
498530
// Parametrized tests to test cross compatibility between the Long List implementations
499531

500532
/**

0 commit comments

Comments
 (0)