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

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


The following commit(s) were added to refs/heads/master by this push:
     new 5fc2a91a212 Support log payload compression in IoTConsensus V1 (#19244)
5fc2a91a212 is described below

commit 5fc2a91a212fd4b4873ec80f36938314044ad1c8
Author: Hongzhi Gao <[email protected]>
AuthorDate: Thu Dec 3 09:56:08 2026 +0800

    Support log payload compression in IoTConsensus V1 (#19244)
    
    * Support log payload compression in IoTConsensus V1
    
    * Use a fixed minimum size for IoTConsensus log compression
    
    * Remove CRC checks from IoTConsensus log compression
    
    * Address IoTConsensus compression naming and code style review
    
    * Reuse TsFile compression types and default codecs for IoTConsensus logs
    
    * Reuse TsFile compression identifiers in IoTConsensus log metadata
    
    * Use a shared decoded payload limit for IoTConsensus log compression
    
    * Release IoTConsensus log compression memory on encoding failure
---
 .../iotdb/consensus/i18n/IoTConsensusMessages.java |   5 +
 .../iotdb/consensus/i18n/IoTConsensusMessages.java |   5 +
 .../iotdb/consensus/config/IoTConsensusConfig.java |  30 ++
 .../consensus/iot/IoTConsensusServerMetrics.java   |  20 +
 .../consensus/iot/client/DispatchLogHandler.java   |  13 +-
 .../logdispatcher/IoTConsensusMemoryManager.java   |   9 +
 .../iot/logdispatcher/LogCompression.java          | 359 ++++++++++++++++
 .../consensus/iot/logdispatcher/LogDispatcher.java |  44 +-
 .../logdispatcher/LogDispatcherThreadMetrics.java  |  75 ++++
 .../service/IoTConsensusRPCServiceProcessor.java   |  14 +
 .../consensus/config/IoTConsensusConfigTest.java   |  37 ++
 .../consensus/iot/CompressedReplicationTest.java   | 176 ++++++++
 .../iot/client/DispatchLogHandlerTest.java         |  89 ++++
 .../iot/logdispatcher/LogCompressionTest.java      | 450 +++++++++++++++++++++
 .../LogDispatcherCompressionTest.java              | 178 ++++++++
 .../LogDispatcherThreadMetricsTest.java            |  41 ++
 .../apache/iotdb/db/i18n/DataNodeMiscMessages.java |   1 +
 .../apache/iotdb/db/i18n/DataNodeMiscMessages.java |   1 +
 .../java/org/apache/iotdb/db/conf/IoTDBConfig.java |   9 +
 .../org/apache/iotdb/db/conf/IoTDBDescriptor.java  |  25 ++
 .../db/consensus/DataRegionConsensusImpl.java      |   1 +
 .../conf/iotdb-system.properties.template          |  12 +
 .../iotdb/commons/service/metric/enums/Metric.java |   2 +
 .../src/main/thrift/iotconsensus.thrift            |  10 +
 24 files changed, 1603 insertions(+), 3 deletions(-)

diff --git 
a/iotdb-core/consensus/src/main/i18n/en/org/apache/iotdb/consensus/i18n/IoTConsensusMessages.java
 
b/iotdb-core/consensus/src/main/i18n/en/org/apache/iotdb/consensus/i18n/IoTConsensusMessages.java
index 8aac42ee593..67b1a532b80 100644
--- 
a/iotdb-core/consensus/src/main/i18n/en/org/apache/iotdb/consensus/i18n/IoTConsensusMessages.java
+++ 
b/iotdb-core/consensus/src/main/i18n/en/org/apache/iotdb/consensus/i18n/IoTConsensusMessages.java
@@ -25,6 +25,11 @@ package org.apache.iotdb.consensus.i18n;
  */
 public final class IoTConsensusMessages {
 
+
+  public static final String 
EXCEPTION_INVALID_IOTCONSENSUS_COMPRESSED_LOG_ARG_EXPECTED_ARG_ACTUAL_ARG_4986175E
 = "Invalid IoTConsensus compressed log (%s, expected %d, actual %d)";
+
+  public static final String 
EXCEPTION_UNSUPPORTED_IOTCONSENSUS_LOG_COMPRESSION_TYPE_ARG_4008DA9A = 
"Unsupported IoTConsensus log compression type: %s";
+
   private IoTConsensusMessages() {}
 
   // ===================== IoTConsensus lifecycle =====================
diff --git 
a/iotdb-core/consensus/src/main/i18n/zh/org/apache/iotdb/consensus/i18n/IoTConsensusMessages.java
 
b/iotdb-core/consensus/src/main/i18n/zh/org/apache/iotdb/consensus/i18n/IoTConsensusMessages.java
index ebd96ada7f8..1ec796c86c9 100644
--- 
a/iotdb-core/consensus/src/main/i18n/zh/org/apache/iotdb/consensus/i18n/IoTConsensusMessages.java
+++ 
b/iotdb-core/consensus/src/main/i18n/zh/org/apache/iotdb/consensus/i18n/IoTConsensusMessages.java
@@ -24,6 +24,11 @@ package org.apache.iotdb.consensus.i18n;
  */
 public final class IoTConsensusMessages {
 
+
+  public static final String 
EXCEPTION_INVALID_IOTCONSENSUS_COMPRESSED_LOG_ARG_EXPECTED_ARG_ACTUAL_ARG_4986175E
 = "无效的 IoTConsensus 压缩日志(%s,预期 %d,实际 %d)";
+
+  public static final String 
EXCEPTION_UNSUPPORTED_IOTCONSENSUS_LOG_COMPRESSION_TYPE_ARG_4008DA9A = "不支持的 
IoTConsensus 日志压缩类型:%s";
+
   private IoTConsensusMessages() {}
 
   // ===================== IoTConsensus 生命周期 =====================
diff --git 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/config/IoTConsensusConfig.java
 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/config/IoTConsensusConfig.java
index c6ca9a7e0dd..1cd7cb074c2 100644
--- 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/config/IoTConsensusConfig.java
+++ 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/config/IoTConsensusConfig.java
@@ -22,6 +22,9 @@ package org.apache.iotdb.consensus.config;
 import 
org.apache.iotdb.commons.client.property.ClientPoolProperty.DefaultProperty;
 import org.apache.iotdb.commons.memory.AtomicLongMemoryBlock;
 import org.apache.iotdb.commons.memory.IMemoryBlock;
+import org.apache.iotdb.consensus.i18n.IoTConsensusMessages;
+
+import org.apache.tsfile.file.metadata.enums.CompressionType;
 
 import java.util.Optional;
 import java.util.concurrent.TimeUnit;
@@ -310,6 +313,7 @@ public class IoTConsensusConfig {
 
     private final int maxLogEntriesNumPerBatch;
     private final int maxSizePerBatch;
+    private final CompressionType logCompressionType;
     private final int maxPendingBatchesNum;
 
     private final int maxQueueLength;
@@ -330,6 +334,7 @@ public class IoTConsensusConfig {
     private Replication(
         int maxLogEntriesNumPerBatch,
         int maxSizePerBatch,
+        CompressionType logCompressionType,
         int maxPendingBatchesNum,
         int maxQueueLength,
         long maxWaitingTimeForWaitBatchInMs,
@@ -347,6 +352,7 @@ public class IoTConsensusConfig {
         long snapshotTransmissionProgressLogIntervalMs) {
       this.maxLogEntriesNumPerBatch = maxLogEntriesNumPerBatch;
       this.maxSizePerBatch = maxSizePerBatch;
+      this.logCompressionType = logCompressionType;
       this.maxPendingBatchesNum = maxPendingBatchesNum;
       this.maxQueueLength = maxQueueLength;
       this.maxWaitingTimeForWaitBatchInMs = maxWaitingTimeForWaitBatchInMs;
@@ -372,6 +378,10 @@ public class IoTConsensusConfig {
       return maxSizePerBatch;
     }
 
+    public CompressionType getLogCompressionType() {
+      return logCompressionType;
+    }
+
     public int getMaxPendingBatchesNum() {
       return maxPendingBatchesNum;
     }
@@ -440,6 +450,7 @@ public class IoTConsensusConfig {
 
       private int maxLogEntriesNumPerBatch = 1024;
       private int maxSizePerBatch = 16 * 1024 * 1024;
+      private CompressionType logCompressionType = 
CompressionType.UNCOMPRESSED;
       private int maxPendingBatchesNum = 5;
       private int maxQueueLength = 4096;
       private long maxWaitingTimeForWaitBatchInMs = 10 * 1000L;
@@ -473,6 +484,24 @@ public class IoTConsensusConfig {
         return this;
       }
 
+      public Builder setLogCompressionType(CompressionType type) {
+        switch (type) {
+          case UNCOMPRESSED:
+          case LZ4:
+          case SNAPPY:
+          case ZSTD:
+            break;
+          default:
+            throw new IllegalArgumentException(
+                String.format(
+                    IoTConsensusMessages
+                        
.EXCEPTION_UNSUPPORTED_IOTCONSENSUS_LOG_COMPRESSION_TYPE_ARG_4008DA9A,
+                    type));
+        }
+        this.logCompressionType = type;
+        return this;
+      }
+
       public Replication.Builder setMaxPendingBatchesNum(int 
maxPendingBatchesNum) {
         this.maxPendingBatchesNum = maxPendingBatchesNum;
         return this;
@@ -557,6 +586,7 @@ public class IoTConsensusConfig {
         return new Replication(
             maxLogEntriesNumPerBatch,
             maxSizePerBatch,
+            logCompressionType,
             maxPendingBatchesNum,
             maxQueueLength,
             maxWaitingTimeForWaitBatchInMs,
diff --git 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/IoTConsensusServerMetrics.java
 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/IoTConsensusServerMetrics.java
index 117f9712635..08714ff2ee4 100644
--- 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/IoTConsensusServerMetrics.java
+++ 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/IoTConsensusServerMetrics.java
@@ -37,6 +37,7 @@ public class IoTConsensusServerMetrics implements IMetricSet {
   private Timer offerRequestToQueueTimer = 
DoNothingMetricManager.DO_NOTHING_TIMER;
   private Timer consensusWriteTimer = DoNothingMetricManager.DO_NOTHING_TIMER;
   private static final String IOT_RECEIVE_LOG = 
Metric.IOT_RECEIVE_LOG.toString();
+  private Timer decompressTimer = DoNothingMetricManager.DO_NOTHING_TIMER;
   private Timer deserializeTimer = DoNothingMetricManager.DO_NOTHING_TIMER;
   private Timer sortTimer = DoNothingMetricManager.DO_NOTHING_TIMER;
   private Timer applyTimer = DoNothingMetricManager.DO_NOTHING_TIMER;
@@ -50,6 +51,10 @@ public class IoTConsensusServerMetrics implements IMetricSet 
{
 
   private static final String IMPL = "ioTConsensusServerImpl";
 
+  public void recordDecompressCost(long nanos) {
+    decompressTimer.updateNanos(nanos);
+  }
+
   public void recordDeserializeCost(long costTimeInNanos) {
     deserializeTimer.updateNanos(costTimeInNanos);
   }
@@ -190,6 +195,14 @@ public class IoTConsensusServerMetrics implements 
IMetricSet {
 
   private void bindSyncLogTimer(AbstractMetricService metricService) {
     // bind sync log timers
+    decompressTimer =
+        metricService.getOrCreateTimer(
+            IOT_RECEIVE_LOG,
+            MetricLevel.IMPORTANT,
+            Tag.STAGE.toString(),
+            "decompress",
+            Tag.REGION.toString(),
+            impl.getConsensusGroupId());
     deserializeTimer =
         metricService.getOrCreateTimer(
             IOT_RECEIVE_LOG,
@@ -319,6 +332,13 @@ public class IoTConsensusServerMetrics implements 
IMetricSet {
 
   private void unbindSyncLogTimer(AbstractMetricService metricService) {
     // unbind sync log timers
+    metricService.remove(
+        MetricType.TIMER,
+        IOT_RECEIVE_LOG,
+        Tag.STAGE.toString(),
+        "decompress",
+        Tag.REGION.toString(),
+        impl.getConsensusGroupId());
     metricService.remove(
         MetricType.TIMER,
         IOT_RECEIVE_LOG,
diff --git 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/client/DispatchLogHandler.java
 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/client/DispatchLogHandler.java
index 615ecd6b07c..fe264ef681f 100644
--- 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/client/DispatchLogHandler.java
+++ 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/client/DispatchLogHandler.java
@@ -39,6 +39,7 @@ import 
org.apache.tsfile.external.commons.lang3.exception.ExceptionUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.io.IOException;
 import java.util.List;
 import java.util.concurrent.TimeUnit;
 import java.util.stream.Collectors;
@@ -53,6 +54,7 @@ public class DispatchLogHandler implements 
AsyncMethodCallback<TSyncLogEntriesRe
   private final LogDispatcherThreadMetrics logDispatcherThreadMetrics;
   private int retryCount;
   private long retryInterval;
+  private volatile boolean hasCompressedAttempt;
 
   public DispatchLogHandler(
       LogDispatcherThread thread,
@@ -65,8 +67,17 @@ public class DispatchLogHandler implements 
AsyncMethodCallback<TSyncLogEntriesRe
     this.retryInterval = 
thread.getConfig().getReplication().getBasicRetryWaitTimeMs();
   }
 
+  public void markCompressedAttempt() {
+    hasCompressedAttempt = true;
+  }
+
   @Override
   public void onComplete(TSyncLogEntriesRes response) {
+    if (response.isSetLogCompressionError()) {
+      // Decoding failed before application, regardless of the ordinary status 
retry classification.
+      onError(new IOException(response.getLogCompressionError().getMessage()));
+      return;
+    }
     recordTransferAttempt(response);
     if (response.getStatuses().stream()
         .anyMatch(status -> RetryUtils.needRetryForWrite(status.getCode()))) {
@@ -138,7 +149,7 @@ public class DispatchLogHandler implements 
AsyncMethodCallback<TSyncLogEntriesRe
           actualCause.toString());
     }
     // skip TApplicationException caused by follower
-    if (actualCause instanceof TApplicationException) {
+    if (actualCause instanceof TApplicationException && !hasCompressedAttempt) 
{
       completeBatch(batch);
       LOGGER.warn(IoTConsensusMessages.SKIP_RETRY_TAPPLICATION_EXCEPTION, 
batch);
       logDispatcherThreadMetrics.recordSyncLogTimePerRequest(System.nanoTime() 
- createTime);
diff --git 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/logdispatcher/IoTConsensusMemoryManager.java
 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/logdispatcher/IoTConsensusMemoryManager.java
index 4d57b32e506..c1a1d4277d3 100644
--- 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/logdispatcher/IoTConsensusMemoryManager.java
+++ 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/logdispatcher/IoTConsensusMemoryManager.java
@@ -129,6 +129,15 @@ public class IoTConsensusMemoryManager {
     }
   }
 
+  /** Non-blocking reservation for the additional arrays held by an encoded 
RPC attempt. */
+  public boolean reserveLogCompression(long size) {
+    return reserve(size, false);
+  }
+
+  public void freeLogCompression(long size) {
+    free(size, false);
+  }
+
   public void free(Batch batch) {
     free(batch.getMemorySize(), false);
     if (logger.isDebugEnabled()) {
diff --git 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/logdispatcher/LogCompression.java
 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/logdispatcher/LogCompression.java
new file mode 100644
index 00000000000..b1c6472420c
--- /dev/null
+++ 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/logdispatcher/LogCompression.java
@@ -0,0 +1,359 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.consensus.iot.logdispatcher;
+
+import org.apache.iotdb.consensus.i18n.IoTConsensusMessages;
+import org.apache.iotdb.consensus.iot.thrift.TLogBufferCompression;
+import org.apache.iotdb.consensus.iot.thrift.TLogEntry;
+import org.apache.iotdb.consensus.iot.thrift.TSyncLogEntriesReq;
+
+import org.apache.tsfile.compress.ICompressor;
+import org.apache.tsfile.compress.IUnCompressor;
+import org.apache.tsfile.file.metadata.enums.CompressionType;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.atomic.AtomicLong;
+
+/** Wire-only encoding: the original batch and its buffer positions remain 
available for retries. */
+public final class LogCompression {
+  // Both ends must use the same decoded payload limit, independent of the 
transport frame limit.
+  public static final int MAX_DECODED_PAYLOAD_BYTES = 64 * 1024 * 1024;
+
+  // Skip tiny blocks to avoid codec and descriptor overhead.
+  private static final int MIN_COMPRESSION_SIZE = 4096;
+  // Conservative allowance for the additional Thrift descriptor and list 
field on the wire.
+  private static final int DESCRIPTOR_BYTES = 32;
+  private static final ICompressor LZ4 = 
ICompressor.getCompressor(CompressionType.LZ4);
+  private static final ICompressor SNAPPY = 
ICompressor.getCompressor(CompressionType.SNAPPY);
+  private static final ICompressor ZSTD = 
ICompressor.getCompressor(CompressionType.ZSTD);
+  private static final IUnCompressor LZ4_DECODER =
+      IUnCompressor.getUnCompressor(CompressionType.LZ4);
+  private static final IUnCompressor SNAPPY_DECODER =
+      IUnCompressor.getUnCompressor(CompressionType.SNAPPY);
+  private static final IUnCompressor ZSTD_DECODER =
+      IUnCompressor.getUnCompressor(CompressionType.ZSTD);
+
+  private LogCompression() {}
+
+  /** Accounts for arrays throughout the async attempt, including synchronous 
send failure. */
+  public static final class EncodedRequest implements AutoCloseable {
+    private TSyncLogEntriesReq request;
+    private final AtomicLong reserved;
+    private final long rawBytes;
+    private long encodedBytes;
+    private boolean memoryLimited;
+
+    private EncodedRequest(
+        TSyncLogEntriesReq request, long reserved, long rawBytes, boolean 
memoryLimited) {
+      this.request = request;
+      this.reserved = new AtomicLong(reserved);
+      this.rawBytes = rawBytes;
+      this.encodedBytes = rawBytes;
+      this.memoryLimited = memoryLimited;
+    }
+
+    public long getRawBytes() {
+      return rawBytes;
+    }
+
+    public long getEncodedBytes() {
+      return encodedBytes;
+    }
+
+    public boolean isMemoryLimited() {
+      return memoryLimited;
+    }
+
+    public TSyncLogEntriesReq getRequest() {
+      return request;
+    }
+
+    private void retain(long bytes) {
+      long released = reserved.getAndSet(bytes) - bytes;
+      if (released > 0) {
+        IoTConsensusMemoryManager.getInstance().freeLogCompression(released);
+      }
+    }
+
+    @Override
+    public void close() {
+      long bytes = reserved.getAndSet(0);
+      if (bytes > 0) {
+        IoTConsensusMemoryManager.getInstance().freeLogCompression(bytes);
+      }
+    }
+  }
+
+  public static EncodedRequest compress(
+      TSyncLogEntriesReq original, CompressionType codec, int maxDecodedBytes) 
throws IOException {
+    long buffers = 0;
+    long rawSize = 0;
+    boolean hasEligibleBuffer = false;
+    for (TLogEntry entry : original.getLogEntries()) {
+      for (ByteBuffer buffer : entry.getData()) {
+        int uncompressedLength = buffer.remaining();
+        rawSize += uncompressedLength;
+        if (uncompressedLength >= MIN_COMPRESSION_SIZE) {
+          hasEligibleBuffer = true;
+        }
+        buffers++;
+      }
+    }
+    // Preserve the original path for oversized legal entries and when scratch 
memory is scarce.
+    if (codec == CompressionType.UNCOMPRESSED || !hasEligibleBuffer || rawSize 
> maxDecodedBytes) {
+      return new EncodedRequest(original, 0, rawSize, false);
+    }
+    // Charge descriptor/list/entry overhead first, then one block's working 
arrays at a time.
+    // Retain only actual compact outputs between blocks, not a whole-batch 
worst-case estimate.
+    long budget = buffers * 256 + original.getLogEntriesSize() * 128L;
+    EncodedRequest result = new EncodedRequest(original, 0, rawSize, false);
+    if 
(!IoTConsensusMemoryManager.getInstance().reserveLogCompression(budget)) {
+      result.memoryLimited = true;
+      return result;
+    }
+    result.reserved.set(budget);
+    boolean completed = false;
+    try {
+      List<TLogEntry> entries = new ArrayList<>(original.getLogEntriesSize());
+      long retained = 0;
+      boolean changed = false;
+      for (TLogEntry entry : original.getLogEntries()) {
+        List<ByteBuffer> data = new ArrayList<>(entry.getDataSize());
+        List<TLogBufferCompression> info = new 
ArrayList<>(entry.getDataSize());
+        long raw = 0;
+        long wire = 0;
+        long arrays = 0;
+        boolean compressed = false;
+        for (ByteBuffer buffer : entry.getData()) {
+          int uncompressedLength = buffer.remaining();
+          raw += uncompressedLength;
+          ByteBuffer encoded = buffer;
+          CompressionType actualCodec = CompressionType.UNCOMPRESSED;
+          if (uncompressedLength >= MIN_COMPRESSION_SIZE) {
+            // Includes an optional input copy, codec-bound output and compact 
output, with
+            // allowance for array headers and codec bounds on very small 
buffers.
+            long workspace = 4L * uncompressedLength + 256;
+            long before = result.reserved.get();
+            if 
(IoTConsensusMemoryManager.getInstance().reserveLogCompression(workspace)) {
+              result.reserved.addAndGet(workspace);
+              byte[] input = buffer.hasArray() ? buffer.array() : 
bytes(buffer);
+              int offset = buffer.hasArray() ? buffer.arrayOffset() + 
buffer.position() : 0;
+              int maxCompressedLength =
+                  
compressor(codec).getMaxBytesForCompression(uncompressedLength);
+              byte[] output = new byte[maxCompressedLength];
+              int compressedLength =
+                  compressor(codec).compress(input, offset, 
uncompressedLength, output);
+              if (compressedLength < 0) {
+                throw invalid(Failure.CODEC, uncompressedLength, 
compressedLength);
+              }
+              if ((long) compressedLength + DESCRIPTOR_BYTES < 
uncompressedLength) {
+                encoded = ByteBuffer.wrap(Arrays.copyOf(output, 
compressedLength));
+                actualCodec = codec;
+                arrays += compressedLength;
+                compressed = true;
+              }
+              result.retain(
+                  before + (actualCodec == CompressionType.UNCOMPRESSED ? 0 : 
encoded.remaining()));
+            } else {
+              // Keep this block raw; other smaller blocks in the entry may 
still fit.
+              result.memoryLimited = true;
+            }
+          }
+          data.add(encoded);
+          info.add(new TLogBufferCompression(actualCodec.serialize(), 
uncompressedLength));
+          wire += encoded.remaining();
+        }
+        // Raw descriptors in a mixed WAL entry cost space too.
+        if (compressed && wire + DESCRIPTOR_BYTES * (long) data.size() + 8 < 
raw) {
+          entries.add(copy(entry, data).setCompressionInfo(info));
+          retained += arrays + data.size() * 256L + 128;
+          result.encodedBytes += wire - raw;
+          changed = true;
+        } else {
+          entries.add(entry);
+          result.retain(result.reserved.get() - arrays);
+        }
+      }
+      if (changed) {
+        result.request =
+            new TSyncLogEntriesReq(original.getPeerId(), 
original.getConsensusGroupId(), entries);
+      }
+      result.retain(retained);
+      completed = true;
+      return result;
+    } finally {
+      if (!completed) {
+        result.close();
+      }
+    }
+  }
+
+  /** Validate declared sizes before allocating; publish nothing until every 
buffer is decoded. */
+  public static void decompress(TSyncLogEntriesReq request, int 
maxDecodedBytes)
+      throws IOException {
+    boolean encoded = false;
+    long total = 0;
+    for (TLogEntry entry : request.getLogEntries()) {
+      if (entry.isSetCompressionInfo()) {
+        encoded = true;
+        if (entry.getCompressionInfoSize() != entry.getDataSize()) {
+          throw invalid(Failure.DESCRIPTORS, entry.getDataSize(), 
entry.getCompressionInfoSize());
+        }
+        for (int i = 0; i < entry.getDataSize(); i++) {
+          TLogBufferCompression info = entry.getCompressionInfo().get(i);
+          int size = info.getUncompressedSize();
+          if (size < 0) {
+            throw invalid(Failure.LENGTH, 0, size);
+          }
+          if (info.getCodec() == CompressionType.UNCOMPRESSED.serialize()) {
+            if (size != entry.getData().get(i).remaining()) {
+              throw invalid(Failure.LENGTH, size, 
entry.getData().get(i).remaining());
+            }
+          } else {
+            // Reject unsupported codecs before allocating or decoding any 
output buffers.
+            decoder(info.getCodec());
+          }
+          total += size;
+          if (total > maxDecodedBytes) {
+            throw invalid(Failure.LIMIT, maxDecodedBytes, total);
+          }
+        }
+      } else {
+        for (ByteBuffer buffer : entry.getData()) {
+          total += buffer.remaining();
+        }
+      }
+    }
+    if (!encoded) {
+      return;
+    }
+    if (total > maxDecodedBytes) {
+      throw invalid(Failure.LIMIT, maxDecodedBytes, total);
+    }
+    List<TLogEntry> decoded = new ArrayList<>(request.getLogEntriesSize());
+    for (TLogEntry entry : request.getLogEntries()) {
+      if (!entry.isSetCompressionInfo()) {
+        decoded.add(entry);
+        continue;
+      }
+      List<ByteBuffer> data = new ArrayList<>(entry.getDataSize());
+      for (int i = 0; i < entry.getDataSize(); i++) {
+        TLogBufferCompression info = entry.getCompressionInfo().get(i);
+        ByteBuffer buffer = entry.getData().get(i);
+        if (info.getCodec() != CompressionType.UNCOMPRESSED.serialize()) {
+          byte[] input = buffer.hasArray() ? buffer.array() : bytes(buffer);
+          int offset = buffer.hasArray() ? buffer.arrayOffset() + 
buffer.position() : 0;
+          byte[] output = new byte[info.getUncompressedSize()];
+          int actual;
+          try {
+            actual =
+                decoder(info.getCodec()).uncompress(input, offset, 
buffer.remaining(), output, 0);
+          } catch (RuntimeException e) {
+            // TsFile codecs may report malformed input through unchecked 
exceptions.
+            IOException failure = invalid(Failure.CODEC, output.length, 
buffer.remaining());
+            failure.initCause(e);
+            throw failure;
+          }
+          if (actual != output.length) {
+            throw invalid(Failure.LENGTH, output.length, actual);
+          }
+          buffer = ByteBuffer.wrap(output);
+        }
+        data.add(buffer);
+      }
+      decoded.add(copy(entry, data));
+    }
+    // The RPC exclusively owns this request. Dropping the encoded entries 
also drops their arrays.
+    // No partially decoded request is exposed on failure.
+    request.setLogEntries(decoded);
+  }
+
+  private static TLogEntry copy(TLogEntry source, List<ByteBuffer> data) {
+    TLogEntry entry =
+        new TLogEntry(data, source.getSearchIndex(), source.isFromWAL(), 
source.getMemorySize());
+    if (source.isSetRoutingEpoch()) {
+      entry.setRoutingEpoch(source.getRoutingEpoch());
+    }
+    if (source.isSetPhysicalTime()) {
+      entry.setPhysicalTime(source.getPhysicalTime());
+    }
+    return entry;
+  }
+
+  private static byte[] bytes(ByteBuffer buffer) {
+    ByteBuffer copy = buffer.duplicate();
+    byte[] bytes = new byte[copy.remaining()];
+    copy.get(bytes);
+    return bytes;
+  }
+
+  private static ICompressor compressor(CompressionType codec) throws 
IOException {
+    switch (codec) {
+      case LZ4:
+        return LZ4;
+      case SNAPPY:
+        return SNAPPY;
+      case ZSTD:
+        return ZSTD;
+      default:
+        throw invalid(Failure.CODEC, 0, codec.serialize());
+    }
+  }
+
+  private static IUnCompressor decoder(byte codec) throws IOException {
+    CompressionType type;
+    try {
+      type = CompressionType.deserialize(codec);
+    } catch (IllegalArgumentException e) {
+      throw invalid(Failure.CODEC, 0, codec);
+    }
+    switch (type) {
+      case LZ4:
+        return LZ4_DECODER;
+      case SNAPPY:
+        return SNAPPY_DECODER;
+      case ZSTD:
+        return ZSTD_DECODER;
+      default:
+        throw invalid(Failure.CODEC, 0, codec);
+    }
+  }
+
+  private enum Failure {
+    CODEC,
+    LENGTH,
+    LIMIT,
+    DESCRIPTORS
+  }
+
+  private static IOException invalid(Failure failure, long expected, long 
actual) {
+    return new IOException(
+        String.format(
+            IoTConsensusMessages
+                
.EXCEPTION_INVALID_IOTCONSENSUS_COMPRESSED_LOG_ARG_EXPECTED_ARG_ACTUAL_ARG_4986175E,
+            failure.name(),
+            expected,
+            actual));
+  }
+}
diff --git 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/logdispatcher/LogDispatcher.java
 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/logdispatcher/LogDispatcher.java
index 593d77322f9..2b610bd63ce 100644
--- 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/logdispatcher/LogDispatcher.java
+++ 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/logdispatcher/LogDispatcher.java
@@ -38,7 +38,10 @@ import org.apache.iotdb.consensus.iot.log.ConsensusReqReader;
 import org.apache.iotdb.consensus.iot.log.GetConsensusReqReaderPlan;
 import org.apache.iotdb.consensus.iot.thrift.TLogEntry;
 import org.apache.iotdb.consensus.iot.thrift.TSyncLogEntriesReq;
+import org.apache.iotdb.consensus.iot.thrift.TSyncLogEntriesRes;
 
+import org.apache.thrift.async.AsyncMethodCallback;
+import org.apache.tsfile.file.metadata.enums.CompressionType;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -651,8 +654,8 @@ public class LogDispatcher {
     }
 
     public void sendBatchAsync(Batch batch, DispatchLogHandler handler) {
+      LogCompression.EncodedRequest encoded = null;
       try {
-        AsyncIoTConsensusServiceClient client = 
clientManager.borrowClient(peer.getEndpoint());
         TSyncLogEntriesReq req =
             new TSyncLogEntriesReq(
                 selfPeerId, peer.getGroupId().convertToTConsensusGroupId(), 
batch.getLogEntries());
@@ -661,8 +664,45 @@ public class LogDispatcher {
             batch.getStartIndex(),
             batch.getEndIndex(),
             peer.getGroupId().convertToTConsensusGroupId());
-        client.syncLogEntries(req, handler);
+        if (config.getReplication().getLogCompressionType() == 
CompressionType.UNCOMPRESSED) {
+          clientManager.borrowClient(peer.getEndpoint()).syncLogEntries(req, 
handler);
+          return;
+        }
+        long compressionStart = System.nanoTime();
+        encoded =
+            LogCompression.compress(
+                req,
+                config.getReplication().getLogCompressionType(),
+                LogCompression.MAX_DECODED_PAYLOAD_BYTES);
+        logDispatcherThreadMetrics.recordCompressTime(System.nanoTime() - 
compressionStart);
+        logDispatcherThreadMetrics.recordCompressionBytes(
+            encoded.getRawBytes(), encoded.getEncodedBytes(), 
encoded.isMemoryLimited());
+        if (encoded.getRequest() == req) {
+          encoded.close();
+          clientManager.borrowClient(peer.getEndpoint()).syncLogEntries(req, 
handler);
+          return;
+        }
+        handler.markCompressedAttempt();
+        final LogCompression.EncodedRequest attempt = encoded;
+        clientManager
+            .borrowClient(peer.getEndpoint())
+            .syncLogEntries(
+                attempt.getRequest(),
+                new AsyncMethodCallback<TSyncLogEntriesRes>() {
+                  @Override
+                  public void onComplete(TSyncLogEntriesRes response) {
+                    attempt.close();
+                    handler.onComplete(response);
+                  }
+
+                  @Override
+                  public void onError(Exception exception) {
+                    attempt.close();
+                    handler.onError(exception);
+                  }
+                });
       } catch (Exception e) {
+        if (encoded != null) encoded.close();
         logger.error(IoTConsensusMessages.CANNOT_SYNC_LOGS_TO_PEER, peer, e);
         handler.onError(e);
       }
diff --git 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/logdispatcher/LogDispatcherThreadMetrics.java
 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/logdispatcher/LogDispatcherThreadMetrics.java
index 9a5dd6c4b28..501979fbeb7 100644
--- 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/logdispatcher/LogDispatcherThreadMetrics.java
+++ 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/logdispatcher/LogDispatcherThreadMetrics.java
@@ -24,6 +24,7 @@ import org.apache.iotdb.commons.service.metric.enums.Tag;
 import org.apache.iotdb.metrics.AbstractMetricService;
 import org.apache.iotdb.metrics.impl.DoNothingMetricManager;
 import org.apache.iotdb.metrics.metricsets.IMetricSet;
+import org.apache.iotdb.metrics.type.Counter;
 import org.apache.iotdb.metrics.type.Timer;
 import org.apache.iotdb.metrics.utils.MetricLevel;
 import org.apache.iotdb.metrics.utils.MetricType;
@@ -45,6 +46,10 @@ public class LogDispatcherThreadMetrics implements 
IMetricSet {
   private final LogDispatcher.LogDispatcherThread logDispatcherThread;
   private final String peerGroupId;
 
+  private Counter rawBytes = DoNothingMetricManager.DO_NOTHING_COUNTER;
+  private Counter encodedBytes = DoNothingMetricManager.DO_NOTHING_COUNTER;
+  private Counter memoryFallbacks = DoNothingMetricManager.DO_NOTHING_COUNTER;
+  private Timer compressTimer = DoNothingMetricManager.DO_NOTHING_TIMER;
   private Timer constructBatchTimer = DoNothingMetricManager.DO_NOTHING_TIMER;
   private Timer syncLogTimePerRequestTimer = 
DoNothingMetricManager.DO_NOTHING_TIMER;
 
@@ -53,6 +58,16 @@ public class LogDispatcherThreadMetrics implements 
IMetricSet {
     this.peerGroupId = logDispatcherThread.getPeer().getGroupId().toString();
   }
 
+  public void recordCompressionBytes(long raw, long encoded, boolean 
memoryLimited) {
+    rawBytes.inc(raw);
+    encodedBytes.inc(encoded);
+    if (memoryLimited) memoryFallbacks.inc();
+  }
+
+  public void recordCompressTime(long nanos) {
+    compressTimer.updateNanos(nanos);
+  }
+
   public void recordConstructBatchTime(long costTimeInNanos) {
     constructBatchTimer.updateNanos(costTimeInNanos);
   }
@@ -108,6 +123,38 @@ public class LogDispatcherThreadMetrics implements 
IMetricSet {
       REGION_TO_STAGE_TIMER_OWNERS
           .computeIfAbsent(peerGroupId, region -> new HashSet<>())
           .add(this);
+      rawBytes =
+          metricService.getOrCreateCounter(
+              Metric.IOT_LOG_COMPRESSION_BYTES.toString(),
+              MetricLevel.IMPORTANT,
+              Tag.REGION.toString(),
+              peerGroupId,
+              Tag.TYPE.toString(),
+              "raw");
+      encodedBytes =
+          metricService.getOrCreateCounter(
+              Metric.IOT_LOG_COMPRESSION_BYTES.toString(),
+              MetricLevel.IMPORTANT,
+              Tag.REGION.toString(),
+              peerGroupId,
+              Tag.TYPE.toString(),
+              "encoded");
+      memoryFallbacks =
+          metricService.getOrCreateCounter(
+              Metric.IOT_LOG_COMPRESSION_MEMORY_FALLBACKS.toString(),
+              MetricLevel.IMPORTANT,
+              Tag.REGION.toString(),
+              peerGroupId);
+      compressTimer =
+          metricService.getOrCreateTimer(
+              Metric.IOT_SEND_LOG.toString(),
+              MetricLevel.IMPORTANT,
+              Tag.NAME.toString(),
+              Metric.IOT_CONSENSUS.toString(),
+              Tag.STAGE.toString(),
+              "compress",
+              Tag.REGION.toString(),
+              peerGroupId);
       constructBatchTimer =
           metricService.getOrCreateTimer(
               Metric.IOT_SEND_LOG.toString(),
@@ -140,6 +187,34 @@ public class LogDispatcherThreadMetrics implements 
IMetricSet {
           return;
         }
         REGION_TO_STAGE_TIMER_OWNERS.remove(peerGroupId);
+        metricService.remove(
+            MetricType.COUNTER,
+            Metric.IOT_LOG_COMPRESSION_BYTES.toString(),
+            Tag.REGION.toString(),
+            peerGroupId,
+            Tag.TYPE.toString(),
+            "raw");
+        metricService.remove(
+            MetricType.COUNTER,
+            Metric.IOT_LOG_COMPRESSION_BYTES.toString(),
+            Tag.REGION.toString(),
+            peerGroupId,
+            Tag.TYPE.toString(),
+            "encoded");
+        metricService.remove(
+            MetricType.COUNTER,
+            Metric.IOT_LOG_COMPRESSION_MEMORY_FALLBACKS.toString(),
+            Tag.REGION.toString(),
+            peerGroupId);
+        metricService.remove(
+            MetricType.TIMER,
+            Metric.IOT_SEND_LOG.toString(),
+            Tag.NAME.toString(),
+            Metric.IOT_CONSENSUS.toString(),
+            Tag.STAGE.toString(),
+            "compress",
+            Tag.REGION.toString(),
+            peerGroupId);
       }
       metricService.remove(
           MetricType.TIMER,
diff --git 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/service/IoTConsensusRPCServiceProcessor.java
 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/service/IoTConsensusRPCServiceProcessor.java
index 6db087f7fe2..ea0811986d5 100644
--- 
a/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/service/IoTConsensusRPCServiceProcessor.java
+++ 
b/iotdb-core/consensus/src/main/java/org/apache/iotdb/consensus/iot/service/IoTConsensusRPCServiceProcessor.java
@@ -35,6 +35,7 @@ import org.apache.iotdb.consensus.i18n.IoTConsensusMessages;
 import org.apache.iotdb.consensus.i18n.IoTConsensusV2Messages;
 import org.apache.iotdb.consensus.iot.IoTConsensus;
 import org.apache.iotdb.consensus.iot.IoTConsensusServerImpl;
+import org.apache.iotdb.consensus.iot.logdispatcher.LogCompression;
 import org.apache.iotdb.consensus.iot.thrift.IoTConsensusIService;
 import org.apache.iotdb.consensus.iot.thrift.TActivatePeerReq;
 import org.apache.iotdb.consensus.iot.thrift.TActivatePeerRes;
@@ -65,6 +66,7 @@ import org.apache.thrift.TException;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
+import java.io.IOException;
 import java.util.Collections;
 import java.util.stream.Collectors;
 
@@ -110,6 +112,18 @@ public class IoTConsensusRPCServiceProcessor implements 
IoTConsensusIService.Ifa
       status.setMessage(message);
       return new TSyncLogEntriesRes(Collections.singletonList(status));
     }
+    long decompressStart = System.nanoTime();
+    try {
+      // Every block is checked before constructing requests or entering the 
sync/apply path.
+      LogCompression.decompress(req, LogCompression.MAX_DECODED_PAYLOAD_BYTES);
+    } catch (IOException | IllegalArgumentException e) {
+      TSStatus status = new 
TSStatus(TSStatusCode.INTERNAL_SERVER_ERROR.getStatusCode());
+      status.setMessage(e.getMessage());
+      return new TSyncLogEntriesRes(Collections.singletonList(status))
+          .setLogCompressionError(status);
+    } finally {
+      
impl.getIoTConsensusServerMetrics().recordDecompressCost(System.nanoTime() - 
decompressStart);
+    }
     BatchIndexedConsensusRequest logEntriesInThisBatch =
         new BatchIndexedConsensusRequest(req.peerId);
     final int sourceNodeId = req.peerId;
diff --git 
a/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/config/IoTConsensusConfigTest.java
 
b/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/config/IoTConsensusConfigTest.java
new file mode 100644
index 00000000000..2c05a9f92bb
--- /dev/null
+++ 
b/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/config/IoTConsensusConfigTest.java
@@ -0,0 +1,37 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.consensus.config;
+
+import org.apache.tsfile.file.metadata.enums.CompressionType;
+import org.junit.Test;
+
+import static org.junit.Assert.assertThrows;
+
+public class IoTConsensusConfigTest {
+  @Test
+  public void testRejectsCompressionTypesUnsupportedByWireProtocol() {
+    for (CompressionType type :
+        new CompressionType[] {CompressionType.GZIP, CompressionType.LZMA2}) {
+      assertThrows(
+          IllegalArgumentException.class,
+          () -> 
IoTConsensusConfig.Replication.newBuilder().setLogCompressionType(type));
+    }
+  }
+}
diff --git 
a/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/iot/CompressedReplicationTest.java
 
b/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/iot/CompressedReplicationTest.java
new file mode 100644
index 00000000000..42190e85f39
--- /dev/null
+++ 
b/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/iot/CompressedReplicationTest.java
@@ -0,0 +1,176 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.consensus.iot;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupType;
+import org.apache.iotdb.common.rpc.thrift.TEndPoint;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.commons.consensus.DataRegionId;
+import org.apache.iotdb.commons.request.IConsensusRequest;
+import org.apache.iotdb.consensus.common.Peer;
+import org.apache.iotdb.consensus.common.request.BatchIndexedConsensusRequest;
+import org.apache.iotdb.consensus.common.request.IndexedConsensusRequest;
+import org.apache.iotdb.consensus.config.ConsensusConfig;
+import org.apache.iotdb.consensus.config.IoTConsensusConfig;
+import org.apache.iotdb.consensus.iot.util.TestEntry;
+import org.apache.iotdb.consensus.iot.util.TestStateMachine;
+
+import org.apache.tsfile.file.metadata.enums.CompressionType;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import java.net.ServerSocket;
+import java.nio.ByteBuffer;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
+
+/** Real async Thrift in both directions, using the existing state 
machine/reader fixture. */
+@RunWith(Parameterized.class)
+public class CompressedReplicationTest {
+  @Rule public TemporaryFolder directory = new TemporaryFolder();
+
+  @Parameterized.Parameters(name = "{0}")
+  public static Object[] codecs() {
+    return new Object[] {CompressionType.LZ4, CompressionType.SNAPPY, 
CompressionType.ZSTD};
+  }
+
+  @Parameterized.Parameter public CompressionType codec;
+  private static final int COUNT = 32;
+  private static final int BYTES = 65536;
+
+  @Test
+  public void testBidirectionalReplicationPreservesFullPayloadAndMetadata() 
throws Exception {
+    DataRegionId group = new DataRegionId(1);
+    int firstPort, secondPort;
+    try (ServerSocket first = new ServerSocket(0);
+        ServerSocket second = new ServerSocket(0)) {
+      firstPort = first.getLocalPort();
+      secondPort = second.getLocalPort();
+    }
+    List<Peer> peers =
+        Arrays.asList(
+            new Peer(group, 1, new TEndPoint("127.0.0.1", firstPort)),
+            new Peer(group, 2, new TEndPoint("127.0.0.1", secondPort)));
+    IoTConsensus[] servers = new IoTConsensus[2];
+    ObservedStateMachine[] machines = {new ObservedStateMachine(), new 
ObservedStateMachine()};
+    try {
+      for (int i = 0; i < 2; i++) {
+        final int node = i;
+        ConsensusConfig config =
+            ConsensusConfig.newBuilder()
+                .setThisNodeId(i + 1)
+                .setThisNode(peers.get(i).getEndpoint())
+                .setConsensusGroupType(TConsensusGroupType.DataRegion)
+                .setStorageDir(directory.newFolder().getAbsolutePath())
+                .setRecvSnapshotDirs(
+                    
Collections.singletonList(directory.newFolder().getAbsolutePath()))
+                .setIoTConsensusConfig(
+                    IoTConsensusConfig.newBuilder()
+                        .setReplication(
+                            IoTConsensusConfig.Replication.newBuilder()
+                                .setLogCompressionType(codec)
+                                .setMaxWaitingTimeForWaitBatchInMs(100)
+                                .build())
+                        .build())
+                .build();
+        servers[i] = new IoTConsensus(config, ignored -> machines[node]);
+        
servers[i].recordCorrectPeerListBeforeStarting(Collections.singletonMap(group, 
peers));
+        servers[i].start();
+      }
+      for (IoTConsensus server : servers) server.createLocalPeer(group, peers);
+      for (int j = 0; j < COUNT; j++) {
+        for (int i = 0; i < 2; i++) {
+          final TestEntry entry =
+              new TestEntry(j, peers.get(i)) {
+                @Override
+                public ByteBuffer serializeToByteBuffer() {
+                  ByteBuffer result = ByteBuffer.allocate(BYTES);
+                  result.put(super.serializeToByteBuffer());
+                  // A repeated nonzero tail makes corruption visible beyond 
the parsed TestEntry.
+                  while (result.hasRemaining()) result.put((byte) 42);
+                  result.flip();
+                  return result;
+                }
+              };
+          assertEquals(200, servers[i].write(group, entry).getCode());
+        }
+      }
+      for (int i = 0; i < 2; i++) {
+        assertTrue(
+            "remote requests must all be applied", 
machines[i].applied.await(30, TimeUnit.SECONDS));
+        assertNull(machines[i].failure.get());
+        assertEquals(COUNT * 2, machines[i].getData().size());
+        for (Peer peer : peers)
+          for (int j = 0; j < COUNT; j++)
+            assertTrue(machines[i].getData().contains(new TestEntry(j, peer)));
+      }
+    } finally {
+      for (IoTConsensus server : servers) if (server != null) server.stop();
+    }
+  }
+
+  private static class ObservedStateMachine extends TestStateMachine {
+    private final CountDownLatch applied = new CountDownLatch(COUNT);
+    private final AtomicReference<AssertionError> failure = new 
AtomicReference<>();
+
+    @Override
+    public IConsensusRequest deserializeRequest(IConsensusRequest request) {
+      if (request instanceof BatchIndexedConsensusRequest) {
+        try {
+          for (IndexedConsensusRequest indexed :
+              ((BatchIndexedConsensusRequest) request).getRequests()) {
+            assertTrue(indexed.getPhysicalTime() > 0);
+            for (IConsensusRequest inner : indexed.getRequests()) {
+              ByteBuffer buffer = inner.serializeToByteBuffer().duplicate();
+              assertEquals(BYTES, buffer.remaining());
+              buffer.getInt();
+              Peer.deserialize(buffer);
+              while (buffer.hasRemaining()) assertEquals(42, buffer.get());
+            }
+          }
+        } catch (AssertionError e) {
+          failure.compareAndSet(null, e);
+        }
+      }
+      return super.deserializeRequest(request);
+    }
+
+    @Override
+    public TSStatus write(IConsensusRequest request) {
+      TSStatus result = super.write(request);
+      if (request instanceof IndexedConsensusRequest
+          && ((IndexedConsensusRequest) request).getSearchIndex() == -1) {
+        applied.countDown();
+      }
+      return result;
+    }
+  }
+}
diff --git 
a/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/iot/client/DispatchLogHandlerTest.java
 
b/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/iot/client/DispatchLogHandlerTest.java
index 13736903902..224d5dfd92f 100644
--- 
a/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/iot/client/DispatchLogHandlerTest.java
+++ 
b/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/iot/client/DispatchLogHandlerTest.java
@@ -24,21 +24,42 @@ import org.apache.iotdb.common.rpc.thrift.TSStatus;
 import org.apache.iotdb.commons.audit.UserDataTransferAuditEvent;
 import org.apache.iotdb.commons.audit.UserDataTransferAuditHandler;
 import org.apache.iotdb.commons.audit.UserDataTransferProtectionMethod;
+import org.apache.iotdb.commons.consensus.DataRegionId;
+import org.apache.iotdb.consensus.common.Peer;
 import org.apache.iotdb.consensus.config.IoTConsensusConfig;
+import org.apache.iotdb.consensus.iot.IoTConsensusServerImpl;
 import org.apache.iotdb.consensus.iot.logdispatcher.Batch;
+import org.apache.iotdb.consensus.iot.logdispatcher.IndexController;
+import 
org.apache.iotdb.consensus.iot.logdispatcher.LogDispatcher.LogDispatcherThread;
+import org.apache.iotdb.consensus.iot.logdispatcher.LogDispatcherThreadMetrics;
+import org.apache.iotdb.consensus.iot.logdispatcher.SyncStatus;
 import org.apache.iotdb.consensus.iot.thrift.TLogEntry;
 import org.apache.iotdb.consensus.iot.thrift.TSyncLogEntriesRes;
 
+import org.apache.thrift.TApplicationException;
+import org.junit.Rule;
 import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.mockito.ArgumentCaptor;
 
 import java.io.IOException;
 import java.util.ArrayList;
+import java.util.Collections;
 import java.util.List;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
 import java.util.concurrent.atomic.AtomicInteger;
 
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.ArgumentMatchers.same;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.times;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
 
 public class DispatchLogHandlerTest {
 
@@ -164,6 +185,74 @@ public class DispatchLogHandlerTest {
         null);
   }
 
+  @Rule public TemporaryFolder directory = new TemporaryFolder();
+
+  @Test
+  public void testCompressedProtocolFailureRetriesWithoutAdvancingIndex() 
throws Exception {
+    verifyCompressionFailure(true);
+  }
+
+  @Test
+  public void testExplicitDecodeErrorOverridesNonRetryStatus() throws 
Exception {
+    verifyCompressionFailure(false);
+  }
+
+  private void verifyCompressionFailure(boolean protocolError) throws 
Exception {
+    IoTConsensusConfig config = IoTConsensusConfig.newBuilder().build();
+    Peer peer = new Peer(new DataRegionId(1), 2, TARGET);
+    IndexController controller =
+        new IndexController(directory.newFolder().getAbsolutePath(), peer, 0, 
500);
+    SyncStatus status = new SyncStatus(controller, config);
+    Batch batch = createBatch(false);
+    LogDispatcherThread thread = mock(LogDispatcherThread.class);
+    IoTConsensusServerImpl impl = mock(IoTConsensusServerImpl.class);
+    ScheduledExecutorService scheduler = mock(ScheduledExecutorService.class);
+    when(thread.getConfig()).thenReturn(config);
+    when(thread.getPeer()).thenReturn(peer);
+    when(thread.getSyncStatus()).thenReturn(status);
+    when(thread.getImpl()).thenReturn(impl);
+    when(impl.getBackgroundTaskService()).thenReturn(scheduler);
+    
when(impl.getUserDataTransferAuditHandler()).thenReturn(UserDataTransferAuditHandler.NO_OP);
+    status.addNextBatch(batch);
+    try {
+      DispatchLogHandler handler =
+          new DispatchLogHandler(thread, 
mock(LogDispatcherThreadMetrics.class), batch);
+      if (protocolError) {
+        handler.markCompressedAttempt();
+        handler.onError(new 
TApplicationException(TApplicationException.PROTOCOL_ERROR));
+      } else {
+        TSStatus error = new TSStatus(200).setMessage("decode failed");
+        handler.onComplete(
+            new 
TSyncLogEntriesRes(Collections.singletonList(error)).setLogCompressionError(error));
+      }
+      assertEquals(0, controller.getCurrentIndex());
+      assertFalse(batch.isSynced());
+      assertTrue(status.hasPendingBatches());
+      // A later in-flight batch may ACK first. Its success must not advance 
past the rejected
+      // batch or release either raw batch before the missing prefix has 
succeeded.
+      Batch later = new Batch(config);
+      later.addTLogEntry(new TLogEntry().setSearchIndex(2).setMemorySize(1));
+      later.buildIndex();
+      status.addNextBatch(later);
+      DispatchLogHandler laterHandler =
+          new DispatchLogHandler(thread, 
mock(LogDispatcherThreadMetrics.class), later);
+      laterHandler.onComplete(new 
TSyncLogEntriesRes(Collections.singletonList(new TSStatus(200))));
+      assertTrue(later.isSynced());
+      assertEquals(0, controller.getCurrentIndex());
+      assertEquals(2, status.getPendingBatches().size());
+      ArgumentCaptor<Runnable> retry = ArgumentCaptor.forClass(Runnable.class);
+      verify(scheduler).schedule(retry.capture(), anyLong(), 
eq(TimeUnit.MILLISECONDS));
+      retry.getValue().run();
+      verify(thread).sendBatchAsync(same(batch), same(handler));
+      handler.onComplete(new TSyncLogEntriesRes(Collections.singletonList(new 
TSStatus(200))));
+      assertEquals(2, controller.getCurrentIndex());
+      assertFalse(status.hasPendingBatches());
+      verify(thread, times(2)).updateSafelyDeletedSearchIndex();
+    } finally {
+      status.free();
+    }
+  }
+
   private static Batch createBatch(boolean containsUserData) {
     final Batch batch = new Batch(IoTConsensusConfig.newBuilder().build());
     batch.addTLogEntry(new TLogEntry().setSearchIndex(1).setMemorySize(1), 
containsUserData);
diff --git 
a/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/iot/logdispatcher/LogCompressionTest.java
 
b/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/iot/logdispatcher/LogCompressionTest.java
new file mode 100644
index 00000000000..20b01208625
--- /dev/null
+++ 
b/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/iot/logdispatcher/LogCompressionTest.java
@@ -0,0 +1,450 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.consensus.iot.logdispatcher;
+
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId;
+import org.apache.iotdb.common.rpc.thrift.TConsensusGroupType;
+import org.apache.iotdb.common.rpc.thrift.TSStatus;
+import org.apache.iotdb.commons.consensus.DataRegionId;
+import org.apache.iotdb.commons.memory.AtomicLongMemoryBlock;
+import org.apache.iotdb.commons.memory.IMemoryBlock;
+import org.apache.iotdb.consensus.IStateMachine;
+import org.apache.iotdb.consensus.common.request.ByteBufferConsensusRequest;
+import org.apache.iotdb.consensus.common.request.IndexedConsensusRequest;
+import org.apache.iotdb.consensus.config.IoTConsensusConfig;
+import org.apache.iotdb.consensus.i18n.IoTConsensusMessages;
+import org.apache.iotdb.consensus.iot.IoTConsensus;
+import org.apache.iotdb.consensus.iot.IoTConsensusServerImpl;
+import org.apache.iotdb.consensus.iot.IoTConsensusServerMetrics;
+import org.apache.iotdb.consensus.iot.service.IoTConsensusRPCServiceProcessor;
+import org.apache.iotdb.consensus.iot.thrift.TLogEntry;
+import org.apache.iotdb.consensus.iot.thrift.TSyncLogEntriesReq;
+import org.apache.iotdb.consensus.iot.thrift.TSyncLogEntriesRes;
+
+import org.apache.thrift.TDeserializer;
+import org.apache.thrift.TSerializer;
+import org.apache.tsfile.file.metadata.enums.CompressionType;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collections;
+import java.util.List;
+import java.util.Random;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotSame;
+import static org.junit.Assert.assertSame;
+import static org.junit.Assert.assertThrows;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.ArgumentMatchers.anyInt;
+import static org.mockito.ArgumentMatchers.anyList;
+import static org.mockito.ArgumentMatchers.anyLong;
+import static org.mockito.ArgumentMatchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.never;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+@RunWith(Parameterized.class)
+public class LogCompressionTest {
+  @Parameterized.Parameters(name = "{0}")
+  public static Object[][] codecs() {
+    return new Object[][] {
+      {CompressionType.LZ4, (byte) 7},
+      {CompressionType.SNAPPY, (byte) 1},
+      {CompressionType.ZSTD, (byte) 8}
+    };
+  }
+
+  @Parameterized.Parameter public CompressionType codec;
+
+  @Parameterized.Parameter(1)
+  public byte expectedWireCodec;
+
+  private IMemoryBlock previous;
+  private final IoTConsensusMemoryManager memory = 
IoTConsensusMemoryManager.getInstance();
+  private static final int LIMIT = 8 * 1024 * 1024;
+
+  @Before
+  public void setUp() {
+    previous = memory.getMemoryBlock();
+    memory.setMemoryBlock(new AtomicLongMemoryBlock("compression-test", null, 
64L * 1024 * 1024));
+    memory.reset();
+  }
+
+  @After
+  public void tearDown() {
+    assertEquals(0, memory.getMemoryBlock().getUsedMemoryInBytes());
+    memory.reset();
+    memory.setMemoryBlock(previous);
+  }
+
+  private TSyncLogEntriesReq request() {
+    byte[] compressible = new byte[16384];
+    Arrays.fill(compressible, (byte) 42);
+    for (int i = 0; i < 17; i++) compressible[i] = (byte) i;
+    ByteBuffer sliced = ByteBuffer.wrap(compressible);
+    sliced.position(17);
+    sliced.limit(16000);
+    ByteBuffer direct = ByteBuffer.allocateDirect(8192);
+    while (direct.hasRemaining()) direct.put((byte) 7);
+    direct.flip();
+    byte[] random = new byte[8192];
+    new Random(123).nextBytes(random);
+    TLogEntry wal =
+        new TLogEntry(
+                Arrays.asList(
+                    sliced,
+                    direct.asReadOnlyBuffer(),
+                    ByteBuffer.wrap(new byte[] {1, 2, 3}),
+                    ByteBuffer.wrap(random)),
+                1,
+                true,
+                50000)
+            .setRoutingEpoch(9)
+            .setPhysicalTime(12345);
+    TLogEntry queue =
+        new TLogEntry(
+            
Collections.singletonList(ByteBuffer.wrap(compressible).asReadOnlyBuffer()),
+            2,
+            false,
+            16384);
+    return new TSyncLogEntriesReq(
+        3, new TConsensusGroupId(TConsensusGroupType.DataRegion, 5), 
Arrays.asList(wal, queue));
+  }
+
+  @Test
+  public void testRoundTripThriftAndRetryOwnership() throws Exception {
+    TSyncLogEntriesReq raw = request();
+    TSyncLogEntriesReq snapshot = raw.deepCopy();
+    for (int attempt = 0; attempt < 2; attempt++) {
+      LogCompression.EncodedRequest encoded = LogCompression.compress(raw, 
codec, LIMIT);
+      try {
+        assertNotSame(raw, encoded.getRequest());
+        assertEquals(
+            encoded.getRequest().getLogEntries().stream()
+                .flatMap(e -> e.getData().stream())
+                .mapToLong(ByteBuffer::remaining)
+                .sum(),
+            encoded.getEncodedBytes());
+        assertTrue(encoded.getEncodedBytes() < encoded.getRawBytes());
+        assertTrue(memory.getMemoryBlock().getUsedMemoryInBytes() > 0);
+        assertEquals(
+            expectedWireCodec,
+            
encoded.getRequest().getLogEntries().get(0).getCompressionInfo().get(0).getCodec());
+        assertEquals(
+            (byte) 0,
+            
encoded.getRequest().getLogEntries().get(0).getCompressionInfo().get(2).getCodec());
+        TSyncLogEntriesReq received = new TSyncLogEntriesReq();
+        new TDeserializer()
+            .deserialize(received, new 
TSerializer().serialize(encoded.getRequest()));
+        LogCompression.decompress(received, LIMIT);
+        assertEquals(snapshot, received);
+        assertEquals(snapshot, raw);
+        assertEquals(17, 
raw.getLogEntries().get(0).getData().get(0).position());
+        assertEquals(16000, 
raw.getLogEntries().get(0).getData().get(0).limit());
+      } finally {
+        encoded.close();
+        encoded.close();
+      }
+    }
+  }
+
+  @Test
+  public void testFixedMinimumSizeBoundary() throws Exception {
+    for (int size : new int[] {4095, 4096, 4097}) {
+      TSyncLogEntriesReq raw =
+          new TSyncLogEntriesReq(
+              1,
+              new TConsensusGroupId(TConsensusGroupType.DataRegion, 1),
+              Collections.singletonList(
+                  new TLogEntry(
+                      Collections.singletonList(ByteBuffer.allocate(size)), 1, 
false, size)));
+      try (LogCompression.EncodedRequest encoded = 
LogCompression.compress(raw, codec, LIMIT)) {
+        if (size < 4096) {
+          assertSame(raw, encoded.getRequest());
+          
assertFalse(encoded.getRequest().getLogEntries().get(0).isSetCompressionInfo());
+        } else {
+          assertNotSame(raw, encoded.getRequest());
+          assertEquals(
+              expectedWireCodec,
+              
encoded.getRequest().getLogEntries().get(0).getCompressionInfo().get(0).getCodec());
+        }
+        TSyncLogEntriesReq received = encoded.getRequest().deepCopy();
+        LogCompression.decompress(received, LIMIT);
+        assertEquals(
+            raw.getLogEntries().get(0).getData(), 
received.getLogEntries().get(0).getData());
+        assertEquals(0, 
raw.getLogEntries().get(0).getData().get(0).position());
+      }
+    }
+  }
+
+  @Test
+  public void testAdaptiveAndMemoryFallback() throws Exception {
+    TSyncLogEntriesReq raw = request();
+    try (LogCompression.EncodedRequest result =
+        LogCompression.compress(raw, CompressionType.UNCOMPRESSED, LIMIT)) {
+      assertSame(raw, result.getRequest());
+    }
+    byte[] random = new byte[16384];
+    new Random(1).nextBytes(random);
+    TSyncLogEntriesReq noise = raw.deepCopy();
+    noise.setLogEntries(
+        Collections.singletonList(
+            new TLogEntry(
+                Collections.singletonList(ByteBuffer.wrap(random)), 1, false, 
random.length)));
+    try (LogCompression.EncodedRequest result = LogCompression.compress(noise, 
codec, LIMIT)) {
+      assertSame(noise, result.getRequest());
+    }
+    long capacity = memory.getMemoryBlock().getTotalMemorySizeInBytes();
+    assertTrue(memory.reserveLogCompression(capacity));
+    try (LogCompression.EncodedRequest result = LogCompression.compress(raw, 
codec, LIMIT)) {
+      assertSame(raw, result.getRequest());
+      assertTrue(result.isMemoryLimited());
+      assertEquals(result.getRawBytes(), result.getEncodedBytes());
+      assertEquals(capacity, memory.getMemoryBlock().getUsedMemoryInBytes());
+    } finally {
+      memory.freeLogCompression(capacity);
+    }
+  }
+
+  @Test
+  public void testRejectsEntireRequestBeforePublishingAnyDecodedEntry() throws 
Exception {
+    try (LogCompression.EncodedRequest encoded = 
LogCompression.compress(request(), codec, LIMIT)) {
+      TSyncLogEntriesReq bad = encoded.getRequest().deepCopy();
+      // The first entry decodes successfully before the later entry fails in 
the codec.
+      bad.getLogEntries().get(1).getData().set(0, ByteBuffer.wrap(new byte[] 
{0, 1, 2}));
+      TSyncLogEntriesReq snapshot = bad.deepCopy();
+      assertThrows(IOException.class, () -> LogCompression.decompress(bad, 
LIMIT));
+      assertEquals(snapshot, bad);
+      // Raw blocks in mixed entries still have to match their declared 
lengths.
+      TSyncLogEntriesReq secondBad = encoded.getRequest().deepCopy();
+      
secondBad.getLogEntries().get(0).getCompressionInfo().get(2).setUncompressedSize(4);
+      final TSyncLogEntriesReq mixed = secondBad;
+      assertThrows(IOException.class, () -> LogCompression.decompress(mixed, 
LIMIT));
+    }
+  }
+
+  @Test
+  public void testMixedRawBufferContentIsForwardedWithoutChecksum() throws 
Exception {
+    try (LogCompression.EncodedRequest encoded = 
LogCompression.compress(request(), codec, LIMIT)) {
+      TSyncLogEntriesReq received = encoded.getRequest().deepCopy();
+      byte[] changed = {4, 5, 6};
+      received.getLogEntries().get(0).getData().set(2, 
ByteBuffer.wrap(changed));
+      TSyncLogEntriesReq expected = request();
+      expected.getLogEntries().get(0).getData().set(2, 
ByteBuffer.wrap(changed));
+      LogCompression.decompress(received, LIMIT);
+      // Same-length raw content is preserved verbatim; there is no payload 
integrity checksum.
+      assertEquals(expected, received);
+    }
+  }
+
+  @Test
+  public void testMalformedMetadataAndCorruptedCodecInput() throws Exception {
+    try (LogCompression.EncodedRequest encoded = 
LogCompression.compress(request(), codec, LIMIT)) {
+      TSyncLogEntriesReq count = encoded.getRequest().deepCopy();
+      count.getLogEntries().get(0).getCompressionInfo().remove(0);
+      assertThrows(IOException.class, () -> LogCompression.decompress(count, 
LIMIT));
+      TSyncLogEntriesReq huge = encoded.getRequest().deepCopy();
+      huge.getLogEntries()
+          .get(0)
+          .getCompressionInfo()
+          .get(0)
+          .setUncompressedSize(Integer.MAX_VALUE);
+      assertThrows(IOException.class, () -> LogCompression.decompress(huge, 
LIMIT));
+      TSyncLogEntriesReq longOutput = encoded.getRequest().deepCopy();
+      
longOutput.getLogEntries().get(0).getCompressionInfo().get(0).setUncompressedSize(16000);
+      assertThrows(IOException.class, () -> 
LogCompression.decompress(longOutput, LIMIT));
+      TSyncLogEntriesReq rawLength = encoded.getRequest().deepCopy();
+      
rawLength.getLogEntries().get(0).getCompressionInfo().get(2).setUncompressedSize(4);
+      assertThrows(IOException.class, () -> 
LogCompression.decompress(rawLength, LIMIT));
+      TSyncLogEntriesReq negative = encoded.getRequest().deepCopy();
+      
negative.getLogEntries().get(0).getCompressionInfo().get(0).setUncompressedSize(-1);
+      assertThrows(IOException.class, () -> 
LogCompression.decompress(negative, LIMIT));
+      TSyncLogEntriesReq unknown = encoded.getRequest().deepCopy();
+      
unknown.getLogEntries().get(0).getCompressionInfo().get(0).setCodec((byte) 127);
+      assertThrows(IOException.class, () -> LogCompression.decompress(unknown, 
LIMIT));
+      TSyncLogEntriesReq corrupt = encoded.getRequest().deepCopy();
+      corrupt.getLogEntries().get(0).getData().set(0, ByteBuffer.wrap(new 
byte[] {0, 1, 2}));
+      assertThrows(IOException.class, () -> LogCompression.decompress(corrupt, 
LIMIT));
+    }
+  }
+
+  @Test
+  public void testRejectsUnsupportedCodecBeforeDecodingAnyEntry() throws 
Exception {
+    try (LogCompression.EncodedRequest encoded = 
LogCompression.compress(request(), codec, LIMIT)) {
+      for (byte unsupported :
+          new byte[] {
+            127, -1, CompressionType.GZIP.serialize(), 
CompressionType.LZMA2.serialize()
+          }) {
+        TSyncLogEntriesReq bad = encoded.getRequest().deepCopy();
+        // The later unsupported codec must be rejected before the earlier 
corrupt body is decoded.
+        bad.getLogEntries().get(0).getData().set(0, ByteBuffer.wrap(new byte[] 
{0, 1, 2}));
+        
bad.getLogEntries().get(1).getCompressionInfo().get(0).setCodec(unsupported);
+        TSyncLogEntriesReq received = new TSyncLogEntriesReq();
+        new TDeserializer().deserialize(received, new 
TSerializer().serialize(bad));
+        TSyncLogEntriesReq snapshot = received.deepCopy();
+        IOException failure =
+            assertThrows(IOException.class, () -> 
LogCompression.decompress(received, LIMIT));
+        assertEquals(
+            String.format(
+                IoTConsensusMessages
+                    
.EXCEPTION_INVALID_IOTCONSENSUS_COMPRESSED_LOG_ARG_EXPECTED_ARG_ACTUAL_ARG_4986175E,
+                "CODEC",
+                0,
+                (long) unsupported),
+            failure.getMessage());
+        assertEquals(snapshot, received);
+      }
+    }
+  }
+
+  @Test
+  public void testSingleLogAboveSoftBatchLimit() throws Exception {
+    TSyncLogEntriesReq raw = request();
+    raw.setLogEntries(
+        Collections.singletonList(
+            new TLogEntry(
+                Collections.singletonList(ByteBuffer.allocate(2 * 1024 * 
1024)),
+                1,
+                false,
+                2 * 1024 * 1024)));
+    try (LogCompression.EncodedRequest encoded = LogCompression.compress(raw, 
codec, LIMIT)) {
+      TSyncLogEntriesReq received = encoded.getRequest().deepCopy();
+      LogCompression.decompress(received, LIMIT);
+      assertEquals(raw, received);
+    }
+  }
+
+  @Test
+  public void 
testProcessorRejectsBeforeDeserializeAndDecodesWithSendingDisabled()
+      throws Exception {
+    IoTConsensus consensus = mock(IoTConsensus.class);
+    IoTConsensusServerImpl impl = mock(IoTConsensusServerImpl.class);
+    IStateMachine stateMachine = mock(IStateMachine.class);
+    when(consensus.getImpl(new DataRegionId(5))).thenReturn(impl);
+    when(impl.isActive()).thenReturn(true);
+    // Decoder support must not depend on the sending codec (default NONE).
+    when(impl.getConfig()).thenReturn(IoTConsensusConfig.newBuilder().build());
+    
when(impl.getIoTConsensusServerMetrics()).thenReturn(mock(IoTConsensusServerMetrics.class));
+    when(impl.getStateMachine()).thenReturn(stateMachine);
+    IoTConsensusRPCServiceProcessor processor = new 
IoTConsensusRPCServiceProcessor(consensus);
+    try (LogCompression.EncodedRequest encoded = 
LogCompression.compress(request(), codec, LIMIT)) {
+      TSyncLogEntriesReq corrupt = encoded.getRequest().deepCopy();
+      corrupt.getLogEntries().get(1).getData().set(0, ByteBuffer.wrap(new 
byte[] {0, 1, 2}));
+      assertTrue(processor.syncLogEntries(corrupt).isSetLogCompressionError());
+      // Snappy reports a short output buffer as IllegalArgumentException; the 
RPC boundary must
+      // reject it in the same explicit, pre-application error path as codec 
I/O failures.
+      TSyncLogEntriesReq shortOutput = encoded.getRequest().deepCopy();
+      
shortOutput.getLogEntries().get(0).getCompressionInfo().get(0).setUncompressedSize(1);
+      
assertTrue(processor.syncLogEntries(shortOutput).isSetLogCompressionError());
+      for (byte unsupported :
+          new byte[] {127, CompressionType.GZIP.serialize(), 
CompressionType.LZMA2.serialize()}) {
+        TSyncLogEntriesReq badCodec = encoded.getRequest().deepCopy();
+        
badCodec.getLogEntries().get(0).getCompressionInfo().get(0).setCodec(unsupported);
+        TSyncLogEntriesReq received = new TSyncLogEntriesReq();
+        new TDeserializer().deserialize(received, new 
TSerializer().serialize(badCodec));
+        
assertTrue(processor.syncLogEntries(received).isSetLogCompressionError());
+      }
+      verify(impl, never()).getStateMachine();
+      verify(impl, never()).syncLog(anyInt(), any());
+      verify(impl, never())
+          .buildIndexedConsensusRequestForRemoteRequest(
+              anyLong(), anyLong(), anyLong(), anyInt(), anyList());
+      when(impl.buildIndexedConsensusRequestForRemoteRequest(
+              anyLong(), anyLong(), anyLong(), anyInt(), anyList()))
+          .thenAnswer(
+              call -> new IndexedConsensusRequest(-1, call.getArgument(0), 
call.getArgument(4)));
+      ByteBufferConsensusRequest applied = new 
ByteBufferConsensusRequest(ByteBuffer.allocate(1));
+      when(stateMachine.deserializeRequest(any())).thenReturn(applied);
+      when(impl.syncLog(eq(3), eq(applied)))
+          .thenReturn(new 
TSStatus(200).setSubStatus(Collections.singletonList(new TSStatus(200))));
+      TSyncLogEntriesReq valid = encoded.getRequest().deepCopy();
+      TSyncLogEntriesRes result = processor.syncLogEntries(valid);
+      assertFalse(result.isSetLogCompressionError());
+      assertEquals(200, result.getStatuses().get(0).getCode());
+      assertEquals(request(), valid);
+      verify(impl).syncLog(3, applied);
+    }
+  }
+
+  @Test
+  public void testMultiBlockScratchBudgetMatchesSequentialPeak() throws 
Exception {
+    memory.setMemoryBlock(new AtomicLongMemoryBlock("compression-tight-pool", 
null, 512 * 1024));
+    TSyncLogEntriesReq raw = request();
+    List<TLogEntry> entries = new ArrayList<>();
+    for (int i = 0; i < 16; i++)
+      entries.add(
+          new TLogEntry(
+              Collections.singletonList(ByteBuffer.allocate(16384)), i + 1, 
false, 16384));
+    raw.setLogEntries(entries);
+    try (LogCompression.EncodedRequest encoded = LogCompression.compress(raw, 
codec, LIMIT)) {
+      assertNotSame(raw, encoded.getRequest());
+      assertEquals(16 * 16384, encoded.getRawBytes());
+      assertFalse(encoded.isMemoryLimited());
+      TSyncLogEntriesReq received = encoded.getRequest().deepCopy();
+      LogCompression.decompress(received, LIMIT);
+      assertEquals(raw, received);
+      assertTrue(memory.getMemoryBlock().getUsedMemoryInBytes() < 512 * 1024);
+    }
+  }
+
+  @Test
+  public void 
testMemoryPressureFallsBackPerBlockAndKeepsOtherBlocksCompressed() throws 
Exception {
+    memory.setMemoryBlock(new 
AtomicLongMemoryBlock("compression-partial-pool", null, 48 * 1024));
+    TSyncLogEntriesReq raw = request();
+    raw.setLogEntries(
+        Collections.singletonList(
+            new TLogEntry(
+                Arrays.asList(
+                    ByteBuffer.allocate(4096),
+                    ByteBuffer.allocate(32768),
+                    ByteBuffer.allocate(4096)),
+                1,
+                true,
+                40960)));
+    try (LogCompression.EncodedRequest encoded = LogCompression.compress(raw, 
codec, LIMIT)) {
+      assertNotSame(raw, encoded.getRequest());
+      assertTrue(encoded.isMemoryLimited());
+      assertEquals(
+          expectedWireCodec,
+          
encoded.getRequest().getLogEntries().get(0).getCompressionInfo().get(0).getCodec());
+      assertEquals(
+          (byte) 0,
+          
encoded.getRequest().getLogEntries().get(0).getCompressionInfo().get(1).getCodec());
+      assertEquals(
+          expectedWireCodec,
+          
encoded.getRequest().getLogEntries().get(0).getCompressionInfo().get(2).getCodec());
+      TSyncLogEntriesReq received = encoded.getRequest().deepCopy();
+      LogCompression.decompress(received, LIMIT);
+      assertEquals(raw, received);
+      assertTrue(encoded.getEncodedBytes() < encoded.getRawBytes());
+    }
+  }
+}
diff --git 
a/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/iot/logdispatcher/LogDispatcherCompressionTest.java
 
b/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/iot/logdispatcher/LogDispatcherCompressionTest.java
new file mode 100644
index 00000000000..24d9607167f
--- /dev/null
+++ 
b/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/iot/logdispatcher/LogDispatcherCompressionTest.java
@@ -0,0 +1,178 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.iotdb.consensus.iot.logdispatcher;
+
+import org.apache.iotdb.common.rpc.thrift.TEndPoint;
+import org.apache.iotdb.commons.client.IClientManager;
+import org.apache.iotdb.commons.client.exception.ClientManagerException;
+import org.apache.iotdb.commons.consensus.DataRegionId;
+import org.apache.iotdb.commons.memory.AtomicLongMemoryBlock;
+import org.apache.iotdb.commons.memory.IMemoryBlock;
+import org.apache.iotdb.consensus.IStateMachine;
+import org.apache.iotdb.consensus.common.Peer;
+import org.apache.iotdb.consensus.config.IoTConsensusConfig;
+import org.apache.iotdb.consensus.iot.IoTConsensusServerImpl;
+import org.apache.iotdb.consensus.iot.client.AsyncIoTConsensusServiceClient;
+import org.apache.iotdb.consensus.iot.client.DispatchLogHandler;
+import org.apache.iotdb.consensus.iot.thrift.TLogEntry;
+import org.apache.iotdb.consensus.iot.thrift.TSyncLogEntriesReq;
+import org.apache.iotdb.consensus.iot.thrift.TSyncLogEntriesRes;
+import org.apache.iotdb.consensus.iot.util.FakeConsensusReqReader;
+
+import org.apache.thrift.TException;
+import org.apache.thrift.async.AsyncMethodCallback;
+import org.apache.tsfile.file.metadata.enums.CompressionType;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.TemporaryFolder;
+import org.mockito.ArgumentCaptor;
+import org.mockito.Captor;
+import org.mockito.Mock;
+import org.mockito.junit.MockitoJUnit;
+import org.mockito.junit.MockitoRule;
+
+import java.io.IOException;
+import java.nio.ByteBuffer;
+import java.util.Collections;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.ArgumentMatchers.any;
+import static org.mockito.Mockito.doThrow;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.when;
+
+public class LogDispatcherCompressionTest {
+  @Rule public TemporaryFolder directory = new TemporaryFolder();
+  private final IoTConsensusMemoryManager memory = 
IoTConsensusMemoryManager.getInstance();
+  private IMemoryBlock previous;
+  private LogDispatcher dispatcher;
+  private LogDispatcher.LogDispatcherThread thread;
+  @Rule public MockitoRule mocks = MockitoJUnit.rule();
+  @Mock private IClientManager<TEndPoint, AsyncIoTConsensusServiceClient> 
clients;
+  @Captor private ArgumentCaptor<AsyncMethodCallback<TSyncLogEntriesRes>> 
callback;
+  private AsyncIoTConsensusServiceClient client;
+  private DispatchLogHandler handler;
+  private Batch batch;
+  private Peer target;
+
+  @Before
+  public void setUp() throws Exception {
+    previous = memory.getMemoryBlock();
+    memory.setMemoryBlock(
+        new AtomicLongMemoryBlock("sender-compression-test", null, 64L * 1024 
* 1024));
+    memory.reset();
+    IoTConsensusConfig config =
+        IoTConsensusConfig.newBuilder()
+            .setReplication(
+                IoTConsensusConfig.Replication.newBuilder()
+                    .setLogCompressionType(CompressionType.LZ4)
+                    .build())
+            .build();
+    IoTConsensusServerImpl impl = mock(IoTConsensusServerImpl.class);
+    IStateMachine stateMachine = mock(IStateMachine.class);
+    when(impl.getStateMachine()).thenReturn(stateMachine);
+    
when(stateMachine.read(any())).thenReturn(mock(FakeConsensusReqReader.class));
+    
when(impl.getStorageDir()).thenReturn(directory.newFolder().getAbsolutePath());
+    Peer self = new Peer(new DataRegionId(1), 1, new TEndPoint("127.0.0.1", 
9000));
+    target = new Peer(new DataRegionId(1), 2, new TEndPoint("127.0.0.1", 
9001));
+    when(impl.getThisNode()).thenReturn(self);
+    when(impl.getConfiguration()).thenReturn(Collections.singletonList(self));
+    client = mock(AsyncIoTConsensusServiceClient.class);
+    when(clients.borrowClient(target.getEndpoint()))
+        .thenAnswer(
+            call -> {
+              // Encoding is complete before obtaining a client, including its 
temporary
+              // reservation.
+              assertTrue(memory.getMemoryBlock().getUsedMemoryInBytes() > 0);
+              return client;
+            });
+    dispatcher = new LogDispatcher(impl, clients);
+    thread = dispatcher.new LogDispatcherThread(target, config, 0);
+    handler = mock(DispatchLogHandler.class);
+    batch = new Batch(config);
+    batch.addTLogEntry(
+        new TLogEntry(Collections.singletonList(ByteBuffer.allocate(16384)), 
1, false, 16384));
+    batch.buildIndex();
+  }
+
+  @After
+  public void tearDown() {
+    Thread.currentThread().interrupt();
+    thread.run();
+    thread.stop();
+    dispatcher.stop();
+    assertEquals(0, memory.getMemoryBlock().getUsedMemoryInBytes());
+    memory.reset();
+    memory.setMemoryBlock(previous);
+  }
+
+  @Test
+  public void testSuccessReleasesWireMemoryAndPreservesRawBatch() throws 
Exception {
+    verifyAsyncCompletion(false);
+  }
+
+  @Test
+  public void testAsyncFailureReleasesWireMemoryAndPreservesRawBatch() throws 
Exception {
+    verifyAsyncCompletion(true);
+  }
+
+  private void verifyAsyncCompletion(boolean failure) throws Exception {
+    thread.sendBatchAsync(batch, handler);
+    ArgumentCaptor<TSyncLogEntriesReq> request = 
ArgumentCaptor.forClass(TSyncLogEntriesReq.class);
+    verify(client).syncLogEntries(request.capture(), callback.capture());
+    verify(handler).markCompressedAttempt();
+    
assertTrue(request.getValue().getLogEntries().get(0).isSetCompressionInfo());
+    assertTrue(memory.getMemoryBlock().getUsedMemoryInBytes() > 0);
+    if (failure) {
+      IOException error = new IOException();
+      callback.getValue().onError(error);
+      verify(handler).onError(error);
+    } else {
+      TSyncLogEntriesRes response = new TSyncLogEntriesRes();
+      callback.getValue().onComplete(response);
+      verify(handler).onComplete(response);
+    }
+    assertEquals(0, memory.getMemoryBlock().getUsedMemoryInBytes());
+    assertEquals(16384, 
batch.getLogEntries().get(0).getData().get(0).remaining());
+    assertTrue(!batch.getLogEntries().get(0).isSetCompressionInfo());
+  }
+
+  @Test
+  public void testBorrowFailureReleasesWireMemory() throws Exception {
+    ClientManagerException error = new ClientManagerException(new 
IOException());
+    doThrow(error).when(clients).borrowClient(target.getEndpoint());
+    thread.sendBatchAsync(batch, handler);
+    verify(handler).onError(error);
+    assertEquals(0, memory.getMemoryBlock().getUsedMemoryInBytes());
+  }
+
+  @Test
+  public void testSynchronousSendFailureReleasesWireMemory() throws Exception {
+    TException error = new TException();
+    doThrow(error).when(client).syncLogEntries(any(), any());
+    thread.sendBatchAsync(batch, handler);
+    verify(handler).onError(error);
+    assertEquals(0, memory.getMemoryBlock().getUsedMemoryInBytes());
+  }
+}
diff --git 
a/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/iot/logdispatcher/LogDispatcherThreadMetricsTest.java
 
b/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/iot/logdispatcher/LogDispatcherThreadMetricsTest.java
index 4da003936a9..31980378f43 100644
--- 
a/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/iot/logdispatcher/LogDispatcherThreadMetricsTest.java
+++ 
b/iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/iot/logdispatcher/LogDispatcherThreadMetricsTest.java
@@ -27,6 +27,7 @@ import org.apache.iotdb.commons.service.metric.enums.Tag;
 import org.apache.iotdb.consensus.common.Peer;
 import org.apache.iotdb.metrics.config.MetricConfig;
 import org.apache.iotdb.metrics.config.MetricConfigDescriptor;
+import org.apache.iotdb.metrics.type.Counter;
 import org.apache.iotdb.metrics.type.IMetric;
 import org.apache.iotdb.metrics.type.Timer;
 import org.apache.iotdb.metrics.utils.MetricInfo;
@@ -38,6 +39,7 @@ import org.junit.Test;
 import org.mockito.Mockito;
 
 import java.util.Map;
+import java.util.Objects;
 import java.util.stream.Collectors;
 
 import static org.junit.Assert.assertEquals;
@@ -116,6 +118,45 @@ public class LogDispatcherThreadMetricsTest {
     assertNull(findConstructBatchTimer());
   }
 
+  @Test
+  public void testCompressionCountersShareRegionOwnership() {
+    LogDispatcherThreadMetrics first = new 
LogDispatcherThreadMetrics(mockThread(2));
+    LogDispatcherThreadMetrics second = new 
LogDispatcherThreadMetrics(mockThread(3));
+    service.addMetricSet(first);
+    service.addMetricSet(second);
+    try {
+      service.restartService();
+      first.recordCompressionBytes(10, 4, true);
+      second.recordCompressionBytes(20, 6, false);
+      assertEquals(30, 
findCompressionCounter(Metric.IOT_LOG_COMPRESSION_BYTES, "raw").getCount());
+      assertEquals(
+          10, findCompressionCounter(Metric.IOT_LOG_COMPRESSION_BYTES, 
"encoded").getCount());
+      assertEquals(
+          1, 
findCompressionCounter(Metric.IOT_LOG_COMPRESSION_MEMORY_FALLBACKS, 
null).getCount());
+      service.removeMetricSet(second);
+      first.recordCompressionBytes(5, 2, true);
+      assertEquals(35, 
findCompressionCounter(Metric.IOT_LOG_COMPRESSION_BYTES, "raw").getCount());
+      assertEquals(
+          2, 
findCompressionCounter(Metric.IOT_LOG_COMPRESSION_MEMORY_FALLBACKS, 
null).getCount());
+    } finally {
+      service.removeMetricSet(first);
+      service.removeMetricSet(second);
+    }
+    assertNull(findCompressionCounter(Metric.IOT_LOG_COMPRESSION_BYTES, 
"raw"));
+    assertNull(findCompressionCounter(Metric.IOT_LOG_COMPRESSION_BYTES, 
"encoded"));
+    
assertNull(findCompressionCounter(Metric.IOT_LOG_COMPRESSION_MEMORY_FALLBACKS, 
null));
+  }
+
+  private Counter findCompressionCounter(Metric metric, String type) {
+    for (Map.Entry<MetricInfo, IMetric> entry : 
service.getAllMetrics().entrySet()) {
+      Map<String, String> tags = entry.getKey().getTags();
+      if (metric.toString().equals(entry.getKey().getName())
+          && REGION.toString().equals(tags.get(Tag.REGION.toString()))
+          && Objects.equals(type, tags.get(Tag.TYPE.toString()))) return 
(Counter) entry.getValue();
+    }
+    return null;
+  }
+
   private static LogDispatcher.LogDispatcherThread mockThread(final int 
peerNodeId) {
     final LogDispatcher.LogDispatcherThread thread =
         Mockito.mock(LogDispatcher.LogDispatcherThread.class);
diff --git 
a/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeMiscMessages.java
 
b/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeMiscMessages.java
index a338d3d9640..5a75dd7bb38 100644
--- 
a/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeMiscMessages.java
+++ 
b/iotdb-core/datanode/src/main/i18n/en/org/apache/iotdb/db/i18n/DataNodeMiscMessages.java
@@ -21,6 +21,7 @@ package org.apache.iotdb.db.i18n;
 
 /** Compile-time i18n constants for DataNode misc subsystems (English). */
 public final class DataNodeMiscMessages {
+  public static final String 
EXCEPTION_INVALID_IOTCONSENSUS_LOG_COMPRESSION_CODEC_ARG_2B9C27F7 = "Invalid 
IoTConsensus log compression codec: %s";
   public static final String 
LOG_FAILED_TO_AUTHORIZE_CQ_ARG_FOR_USER_ARG_ARG_B436A81F =
       "Failed to authorize CQ {} for user {}: {}";
 
diff --git 
a/iotdb-core/datanode/src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeMiscMessages.java
 
b/iotdb-core/datanode/src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeMiscMessages.java
index 71f252edd6f..485a32e7448 100644
--- 
a/iotdb-core/datanode/src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeMiscMessages.java
+++ 
b/iotdb-core/datanode/src/main/i18n/zh/org/apache/iotdb/db/i18n/DataNodeMiscMessages.java
@@ -21,6 +21,7 @@ package org.apache.iotdb.db.i18n;
 
 /** 编译时国际化常量 - DataNode 杂项子系统(中文)。 */
 public final class DataNodeMiscMessages {
+  public static final String 
EXCEPTION_INVALID_IOTCONSENSUS_LOG_COMPRESSION_CODEC_ARG_2B9C27F7 = "无效的 
IoTConsensus 日志压缩算法:%s";
   public static final String 
LOG_FAILED_TO_AUTHORIZE_CQ_ARG_FOR_USER_ARG_ARG_B436A81F =
       "CQ {} 的用户 {} 鉴权失败:{}";
 
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/conf/IoTDBConfig.java 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/conf/IoTDBConfig.java
index c6a0d487896..bd2895f8876 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/conf/IoTDBConfig.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/conf/IoTDBConfig.java
@@ -1130,6 +1130,7 @@ public class IoTDBConfig implements 
DefaultEncodingProvider {
   // IoTConsensus Config
   private int maxLogEntriesNumPerBatch = 1024;
   private int maxSizePerBatch = 16 * 1024 * 1024;
+  private CompressionType iotLogCompressionType = CompressionType.UNCOMPRESSED;
   private int maxPendingBatchesNum = 5;
   private double maxMemoryRatioForQueue = 0.6;
   private long regionMigrationSpeedLimitBytesPerSecond = 48 * 1024 * 1024L;
@@ -1309,6 +1310,14 @@ public class IoTDBConfig implements 
DefaultEncodingProvider {
     return maxLogEntriesNumPerBatch;
   }
 
+  public CompressionType getIotLogCompressionType() {
+    return iotLogCompressionType;
+  }
+
+  public void setIotLogCompressionType(CompressionType type) {
+    iotLogCompressionType = type;
+  }
+
   public int getMaxSizePerBatch() {
     return maxSizePerBatch;
   }
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/conf/IoTDBDescriptor.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/conf/IoTDBDescriptor.java
index 347636a6728..4eec35ca33d 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/conf/IoTDBDescriptor.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/conf/IoTDBDescriptor.java
@@ -95,6 +95,7 @@ import java.nio.file.FileStore;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.List;
+import java.util.Locale;
 import java.util.Optional;
 import java.util.Properties;
 import java.util.ServiceLoader;
@@ -1192,6 +1193,30 @@ public class IoTDBDescriptor {
             properties.getProperty(
                 "schema_thread_count", 
Integer.toString(conf.getSchemaThreadCount()))));
 
+    // Sending policy is a startup setting. Decoding is always available for 
in-flight requests.
+    String compression = 
properties.getProperty("data_region_iot_log_compression", "NONE").trim();
+    try {
+      String normalizedCompression = compression.toUpperCase(Locale.ROOT);
+      switch (normalizedCompression) {
+        case "NONE":
+          conf.setIotLogCompressionType(CompressionType.UNCOMPRESSED);
+          break;
+        case "LZ4":
+        case "SNAPPY":
+        case "ZSTD":
+          
conf.setIotLogCompressionType(CompressionType.valueOf(normalizedCompression));
+          break;
+        default:
+          throw new IllegalArgumentException();
+      }
+    } catch (IllegalArgumentException e) {
+      throw new IOException(
+          String.format(
+              DataNodeMiscMessages
+                  
.EXCEPTION_INVALID_IOTCONSENSUS_LOG_COMPRESSION_CODEC_ARG_2B9C27F7,
+              compression),
+          e);
+    }
     loadIoTConsensusProps(properties);
     loadIoTConsensusV2Props(properties);
 
diff --git 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/consensus/DataRegionConsensusImpl.java
 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/consensus/DataRegionConsensusImpl.java
index ba08b7379bc..6efaee537e1 100644
--- 
a/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/consensus/DataRegionConsensusImpl.java
+++ 
b/iotdb-core/datanode/src/main/java/org/apache/iotdb/db/consensus/DataRegionConsensusImpl.java
@@ -197,6 +197,7 @@ public class DataRegionConsensusImpl {
                           .setConsensusMemoryBlock(memoryBlock)
                           
.setMaxLogEntriesNumPerBatch(CONF.getMaxLogEntriesNumPerBatch())
                           .setMaxSizePerBatch(CONF.getMaxSizePerBatch())
+                          
.setLogCompressionType(CONF.getIotLogCompressionType())
                           
.setMaxPendingBatchesNum(CONF.getMaxPendingBatchesNum())
                           
.setMaxMemoryRatioForQueue(CONF.getMaxMemoryRatioForQueue())
                           .setRegionMigrationSpeedLimitBytesPerSecond(
diff --git 
a/iotdb-core/node-commons/src/assembly/resources/conf/iotdb-system.properties.template
 
b/iotdb-core/node-commons/src/assembly/resources/conf/iotdb-system.properties.template
index ae50a48d7af..1db3f3271aa 100644
--- 
a/iotdb-core/node-commons/src/assembly/resources/conf/iotdb-system.properties.template
+++ 
b/iotdb-core/node-commons/src/assembly/resources/conf/iotdb-system.properties.template
@@ -1873,6 +1873,18 @@ enable_wal_compression=true
 ### IoTConsensus Configuration
 ####################
 
+# IoTConsensus V1 log payload codec: NONE, LZ4, SNAPPY, ZSTD.
+# Compression uses the TsFile implementation and its default codec settings.
+# Keep NONE during rolling upgrades. Enable only after all peers support log 
compression.
+# This controls sending; receivers can decode every supported codec even when 
set to NONE.
+# Independent of WAL/TsFile compression and dn_rpc_thrift_compression_enable.
+# Buffers smaller than 4 KiB or without a net size benefit are sent 
uncompressed.
+# Requests with more than 64 MiB of uncompressed log payload are sent 
uncompressed.
+# The actual RPC frame must still fit the receiving node's Thrift frame limit.
+# effectiveMode: restart
+# Datatype: String
+data_region_iot_log_compression = NONE
+
 # The maximum log entries num in IoTConsensus Batch
 # effectiveMode: hot_reload
 # Datatype: int
diff --git 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/service/metric/enums/Metric.java
 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/service/metric/enums/Metric.java
index 9284a391427..cd29e0b6b61 100644
--- 
a/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/service/metric/enums/Metric.java
+++ 
b/iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/service/metric/enums/Metric.java
@@ -61,6 +61,8 @@ public enum Metric {
   STAGE("stage"),
   IOT_CONSENSUS("iot_consensus"),
   IOT_SEND_LOG("iot_send_log"),
+  IOT_LOG_COMPRESSION_BYTES("iot_log_compression_bytes"),
+  IOT_LOG_COMPRESSION_MEMORY_FALLBACKS("iot_log_compression_memory_fallbacks"),
   IOT_RECEIVE_LOG("iot_receive_log"),
   IOT_CONSENSUS_V2("iot_consensus_v2"),
   IOT_CONSENSUS_V2_MODE("iot_consensus_v2_mode"),
diff --git 
a/iotdb-protocol/thrift-consensus/src/main/thrift/iotconsensus.thrift 
b/iotdb-protocol/thrift-consensus/src/main/thrift/iotconsensus.thrift
index 169a7961e44..6726213a1c5 100644
--- a/iotdb-protocol/thrift-consensus/src/main/thrift/iotconsensus.thrift
+++ b/iotdb-protocol/thrift-consensus/src/main/thrift/iotconsensus.thrift
@@ -20,6 +20,12 @@
 include "common.thrift"
 namespace java org.apache.iotdb.consensus.iot.thrift
 
+struct TLogBufferCompression {
+  // Value returned by TsFile CompressionType.serialize().
+  1: required byte codec
+  2: required i32 uncompressedSize
+}
+
 // If this TLogEntry comes from the queue in memory, the list size of the data 
will only be 1.
 // If this TLogEntry comes from wal, the list size of the data may be greater 
than 1.
 struct TLogEntry {
@@ -29,6 +35,8 @@ struct TLogEntry {
   4: required i64 memorySize
   5: optional i64 routingEpoch
   6: optional i64 physicalTime
+  // One descriptor per data buffer; absent means the original, uncompressed 
entry.
+  7: optional list<TLogBufferCompression> compressionInfo
 }
 
 struct TSyncLogEntriesReq {
@@ -41,6 +49,8 @@ struct TSyncLogEntriesReq {
 struct TSyncLogEntriesRes {
   1: required list<common.TSStatus> statuses
   2: optional i64 receiverMemSize
+  // Rejected before application. This must never be treated as a completed 
batch.
+  3: optional common.TSStatus logCompressionError
 }
 
 struct TSyncWriterSafeTimeBarrierReq {

Reply via email to