This is an automated email from the ASF dual-hosted git repository.

NSAmelchev pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/ignite.git


The following commit(s) were added to refs/heads/master by this push:
     new bd3c3bab661 IGNITE-29175 Copy compressed snapshot partitions sparsely 
on restore instead of punching holes (#13710)
bd3c3bab661 is described below

commit bd3c3bab66175d43fc1d5d5fec59ad6f2ff0fa7b
Author: Nikita Amelchev <[email protected]>
AuthorDate: Tue Nov 3 18:31:19 2026 +0300

    IGNITE-29175 Copy compressed snapshot partitions sparsely on restore 
instead of punching holes (#13710)
---
 .../snapshot/SnapshotCompressionBasicTest.java     | 40 +++++++++++
 .../snapshot/SnapshotRestoreProcess.java           | 84 ++++++++++++++++------
 2 files changed, 104 insertions(+), 20 deletions(-)

diff --git 
a/modules/compress/src/test/java/org/apache/ignite/internal/processors/cache/persistence/snapshot/SnapshotCompressionBasicTest.java
 
b/modules/compress/src/test/java/org/apache/ignite/internal/processors/cache/persistence/snapshot/SnapshotCompressionBasicTest.java
index ae0c2da94d2..62a3e205864 100644
--- 
a/modules/compress/src/test/java/org/apache/ignite/internal/processors/cache/persistence/snapshot/SnapshotCompressionBasicTest.java
+++ 
b/modules/compress/src/test/java/org/apache/ignite/internal/processors/cache/persistence/snapshot/SnapshotCompressionBasicTest.java
@@ -43,6 +43,7 @@ import org.apache.ignite.IgniteCheckedException;
 import org.apache.ignite.IgniteException;
 import org.apache.ignite.cache.QueryEntity;
 import org.apache.ignite.cache.QueryIndex;
+import org.apache.ignite.cache.affinity.rendezvous.RendezvousAffinityFunction;
 import org.apache.ignite.cluster.ClusterState;
 import org.apache.ignite.configuration.CacheConfiguration;
 import org.apache.ignite.configuration.DiskPageCompression;
@@ -57,6 +58,8 @@ import 
org.apache.ignite.internal.processors.compress.CompressionProcessor;
 import org.apache.ignite.internal.util.typedef.F;
 import org.apache.ignite.internal.util.typedef.G;
 import org.apache.ignite.internal.util.typedef.internal.U;
+import org.apache.ignite.metric.MetricRegistry;
+import org.apache.ignite.spi.metric.LongMetric;
 import org.apache.ignite.testframework.GridTestUtils;
 import org.junit.After;
 import org.junit.Before;
@@ -67,6 +70,9 @@ import org.mockito.Mockito;
 import static org.apache.ignite.events.EventType.EVTS_CLUSTER_SNAPSHOT;
 import static 
org.apache.ignite.events.EventType.EVT_CLUSTER_SNAPSHOT_RESTORE_FINISHED;
 import static 
org.apache.ignite.events.EventType.EVT_CLUSTER_SNAPSHOT_RESTORE_STARTED;
+import static 
org.apache.ignite.internal.processors.cache.CacheGroupMetricsImpl.CACHE_GROUP_METRICS_PREFIX;
+import static 
org.apache.ignite.internal.processors.metric.impl.MetricUtils.metricName;
+import static org.junit.Assume.assumeFalse;
 
 /** */
 public class SnapshotCompressionBasicTest extends AbstractSnapshotSelfTest {
@@ -265,6 +271,40 @@ public class SnapshotCompressionBasicTest extends 
AbstractSnapshotSelfTest {
         }
     }
 
+    /** */
+    @Test
+    public void testRestoredPartitionsAreSparse() throws Exception {
+        // Doesn't matter for a single node.
+        assumeFalse(onlyPrimary);
+
+        IgniteEx ignite = startGrid(0);
+
+        ignite.cluster().state(ClusterState.ACTIVE);
+
+        IgniteCache<Integer, byte[]> cache = ignite.createCache(new 
CacheConfiguration<Integer, byte[]>(DEFAULT_CACHE_NAME)
+            .setAffinity(new RendezvousAffinityFunction(false, 4))
+            .setDiskPageCompression(DISK_PAGE_COMPRESSION));
+
+        // Partition files must be well above 64 KiB: the end-of-file 
preallocation that can make the copy dense (XFS does it)
+        // starts at that size.
+        for (int i = 0; i < 4_000; i++)
+            cache.put(i, new byte[1024]);
+
+        ignite.snapshot().createSnapshot(SNAPSHOT_NAME).get(TIMEOUT);
+
+        cache.destroy();
+
+        ignite.snapshot().restoreSnapshot(SNAPSHOT_NAME, null).get(TIMEOUT);
+
+        MetricRegistry mreg = 
ignite.context().metric().registry(metricName(CACHE_GROUP_METRICS_PREFIX, 
DEFAULT_CACHE_NAME));
+
+        long size = mreg.<LongMetric>findMetric("StorageSize").value();
+        long sparseSize = 
mreg.<LongMetric>findMetric("SparseStorageSize").value();
+
+        // Zero-filled values compress every 8 KiB page into one 4 KiB block, 
so a sparse group takes about half of its size.
+        assertTrue("sparseSize=" + sparseSize + ", size=" + size, sparseSize < 
0.75 * size);
+    }
+
     /** {@inheritDoc} */
     @Override protected Function<Integer, Object> valueBuilder() {
         return i -> new Value("name_" + i);
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/snapshot/SnapshotRestoreProcess.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/snapshot/SnapshotRestoreProcess.java
index 2643a5575f6..f36bd768665 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/snapshot/SnapshotRestoreProcess.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/persistence/snapshot/SnapshotRestoreProcess.java
@@ -19,6 +19,7 @@ package 
org.apache.ignite.internal.processors.cache.persistence.snapshot;
 
 import java.io.File;
 import java.io.IOException;
+import java.io.RandomAccessFile;
 import java.nio.ByteBuffer;
 import java.nio.file.Files;
 import java.nio.file.Path;
@@ -74,6 +75,8 @@ import 
org.apache.ignite.internal.processors.cache.distributed.dht.topology.Grid
 import 
org.apache.ignite.internal.processors.cache.persistence.CacheStripedExecutor;
 import 
org.apache.ignite.internal.processors.cache.persistence.GridCacheDatabaseSharedManager;
 import 
org.apache.ignite.internal.processors.cache.persistence.checkpoint.CheckpointProgress;
+import org.apache.ignite.internal.processors.cache.persistence.file.FileIO;
+import 
org.apache.ignite.internal.processors.cache.persistence.file.FileIOFactory;
 import 
org.apache.ignite.internal.processors.cache.persistence.file.FilePageStore;
 import 
org.apache.ignite.internal.processors.cache.persistence.file.FilePageStoreManager;
 import 
org.apache.ignite.internal.processors.cache.persistence.file.FileVersionCheckingFactory;
@@ -99,6 +102,9 @@ import org.apache.ignite.metric.MetricRegistry;
 import org.apache.ignite.plugin.extensions.communication.Message;
 import org.jetbrains.annotations.Nullable;
 
+import static java.nio.file.StandardOpenOption.CREATE;
+import static java.nio.file.StandardOpenOption.READ;
+import static java.nio.file.StandardOpenOption.WRITE;
 import static java.util.Optional.ofNullable;
 import static 
org.apache.ignite.internal.pagemem.PageIdAllocator.INDEX_PARTITION;
 import static 
org.apache.ignite.internal.processors.cache.persistence.filename.NodeFileTree.partId;
@@ -1764,30 +1770,14 @@ public class SnapshotRestoreProcess {
                     ", snpDir=" + snpFile.getAbsolutePath() + ", name=" + 
snpFile.getName() + ']');
             }
 
-            IgniteSnapshotManager.copy(snapMgr.ioFactory(), snpFile, 
tmpPartFile, snpFile.length());
+            if (opCtx.isGroupCompressed(grpId))
+                copyCompressed(opCtx, grpId, snpFile, tmpPartFile);
+            else
+                IgniteSnapshotManager.copy(snapMgr.ioFactory(), snpFile, 
tmpPartFile, snpFile.length());
 
             return tmpPartFile.toPath();
         }, snapMgr.snapshotExecutorService());
 
-        if (opCtx.isGroupCompressed(grpId)) {
-            copyPartFut = copyPartFut.thenComposeAsync(
-                p -> {
-                    IgniteCompletableFuture<Path> result = new 
IgniteCompletableFuture<>();
-
-                    try {
-                        punchHole(grpId, partFut.partId, tmpPartFile);
-
-                        result.complete(tmpPartFile.toPath());
-                    }
-                    catch (Throwable t) {
-                        result.completeExceptionally(t);
-                    }
-                    return result;
-                },
-                snapMgr.snapshotExecutorService()
-            );
-        }
-
         copyPartFut.whenComplete((r, t) -> opCtx.errHnd.accept(t))
             .whenComplete((r, t) -> {
                 if (t == null)
@@ -1797,6 +1787,60 @@ public class SnapshotRestoreProcess {
             });
     }
 
+    /**
+     * Copies a partition of a compressed cache group page by page writing 
only the compressed bytes of each page, so the
+     * rest of the page is left unallocated by the file system and the copy is 
sparse without punching holes in it.
+     */
+    private void copyCompressed(SnapshotRestoreContext opCtx, int grpId, File 
from, File to) {
+        FileIOFactory ioFactory = 
ctx.cache().context().snapshotMgr().ioFactory();
+        FilePageStoreManager storeMgr = 
(FilePageStoreManager)ctx.cache().context().pageStore();
+        int pageSize = ctx.cache().context().database().pageSize();
+
+        try (FileIO src = ioFactory.create(from, READ); FileIO dest = 
ioFactory.create(to, CREATE, WRITE)) {
+            long len = src.size();
+
+            // The file gets its final size first: XFS preallocates blocks on 
extending writes, and those between the pages
+            // would stay allocated, see "speculative preallocation" in 
https://xfs.org/index.php/XFS_FAQ.
+            try (RandomAccessFile file = new RandomAccessFile(to, "rw")) {
+                file.setLength(len);
+            }
+
+            ByteBuffer buf = locBuff.get();
+
+            buf.clear();
+            src.readFully(buf, 0);
+            buf.flip();
+
+            buf.getLong(); // Signature.
+
+            int hdrSize = storeMgr.getPageStoreFactory(grpId, 
null).headerSize(buf.getInt());
+
+            buf.rewind();
+            buf.limit(hdrSize);
+            dest.writeFully(buf, 0);
+
+            for (long off = hdrSize; off < len; off += pageSize) {
+                if (opCtx.stopChecker.getAsBoolean())
+                    throw new IgniteInterruptedException("The operation has 
been stopped on copy file: " + from.getAbsolutePath());
+
+                if (Thread.interrupted())
+                    throw new IgniteInterruptedException("Thread has been 
interrupted: " + Thread.currentThread().getName());
+
+                buf.clear();
+                src.readFully(buf, off);
+                buf.flip();
+
+                if (PageIO.getCompressionType(buf) != 
CompressionProcessor.UNCOMPRESSED_PAGE)
+                    buf.limit(PageIO.getCompressedSize(buf));
+
+                dest.writeFully(buf, off);
+            }
+        }
+        catch (IOException e) {
+            throw new IgniteException(e);
+        }
+    }
+
     /** */
     private void punchHole(int grpId, int partId, File partFile) throws 
Exception {
         FilePageStoreManager storeMgr = 
(FilePageStoreManager)ctx.cache().context().pageStore();

Reply via email to