jt2594838 commented on code in PR #18557: URL: https://github.com/apache/iotdb/pull/18557#discussion_r3900495011
########## iotdb-core/consensus/src/test/java/org/apache/iotdb/consensus/iot/ConcurrentLargeWriteTest.java: ########## @@ -0,0 +1,248 @@ +/* + * 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.ConsensusGroupId; +import org.apache.iotdb.commons.consensus.DataRegionId; +import org.apache.iotdb.commons.exception.StartupException; +import org.apache.iotdb.consensus.ConsensusFactory; +import org.apache.iotdb.consensus.common.ConsensusGroup; +import org.apache.iotdb.consensus.common.Peer; +import org.apache.iotdb.consensus.common.request.IConsensusRequest; +import org.apache.iotdb.consensus.config.ConsensusConfig; +import org.apache.iotdb.consensus.config.IoTConsensusConfig; +import org.apache.iotdb.consensus.exception.ConsensusException; +import org.apache.iotdb.consensus.iot.logdispatcher.IoTConsensusMemoryManager; +import org.apache.iotdb.consensus.iot.util.TestStateMachine; +import org.apache.iotdb.rpc.TSStatusCode; + +import org.apache.ratis.util.FileUtils; +import org.junit.After; +import org.junit.Assert; +import org.junit.Assume; +import org.junit.Before; +import org.junit.Test; + +import java.io.ByteArrayOutputStream; +import java.io.DataOutputStream; +import java.io.File; +import java.io.IOException; +import java.net.ServerSocket; +import java.nio.ByteBuffer; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.Collections; +import java.util.List; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.ExecutorService; +import java.util.concurrent.Executors; +import java.util.concurrent.Future; +import java.util.concurrent.TimeUnit; + +public class ConcurrentLargeWriteTest { + + private static final ConsensusGroupId GROUP_ID = new DataRegionId(1); + private static final int FIRST_PORT = 10000; + private static final int LAST_PORT = 20000; + private static final int REQUEST_COUNT = 4; + private static final int REQUEST_PAYLOAD_SIZE = 8 * 1024 * 1024; + private static final long REPLICATION_TIMEOUT_SECONDS = 60; + + private List<Peer> peers; + private final List<File> storageDirs = + Arrays.asList( + new File("target" + File.separator + "concurrent-large-write-1"), + new File("target" + File.separator + "concurrent-large-write-2")); + private final List<IoTConsensus> servers = new ArrayList<>(); + private final List<TestStateMachine> stateMachines = new ArrayList<>(); + private ConsensusGroup group; + private final IoTConsensusMemoryManager memoryManager = IoTConsensusMemoryManager.getInstance(); + + private long previousMaxMemory; + private long previousMaxQueueMemory; + + @Before + public void setUp() throws Exception { + int basePort = findAvailablePortPair(); + Assume.assumeTrue(basePort > 0); + peers = + Arrays.asList( + new Peer(GROUP_ID, 1, new TEndPoint("127.0.0.1", basePort)), + new Peer(GROUP_ID, 2, new TEndPoint("127.0.0.1", basePort + 1))); + group = new ConsensusGroup(GROUP_ID, peers); + previousMaxMemory = memoryManager.getMaxMemorySizeInByte(); + previousMaxQueueMemory = memoryManager.getMaxMemorySizeForQueueInByte(); + for (File storageDir : storageDirs) { + FileUtils.deleteFully(storageDir); + FileUtils.createDirectories(storageDir); + } + + IoTConsensusConfig consensusConfig = + IoTConsensusConfig.newBuilder() + .setReplication( + IoTConsensusConfig.Replication.newBuilder() + .setMaxLogEntriesNumPerBatch(2) + .setMaxSizePerBatch(REQUEST_PAYLOAD_SIZE * 2) + .setMaxPendingBatchesNum(5) + .setBasicRetryWaitTimeMs(10) + // Only one large batch fits, forcing competing dispatchers to retry + // reservation. + .setAllocateMemoryForConsensus(REQUEST_PAYLOAD_SIZE * 2L) Review Comment: This integration scenario starts two real IoTConsensus replicas and launches eight concurrent 8 MiB writes (four per replica) while the consensus memory cap admits only one two-entry batch. That deliberate contention exercises dispatcher reservation retries; the test then waits for both minSyncIndex values and all 16 state-machine deliveries, proving large concurrent requests do not deadlock synchronization. Dynamic port selection and duplicate writable buffers keep the test isolated and Thrift-compatible; the test passed in the consensus Maven module. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
