|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.iotdb.consensus.iot; |
| 21 | + |
| 22 | +import org.apache.iotdb.common.rpc.thrift.TEndPoint; |
| 23 | +import org.apache.iotdb.common.rpc.thrift.TSStatus; |
| 24 | +import org.apache.iotdb.commons.consensus.DataRegionId; |
| 25 | +import org.apache.iotdb.commons.utils.StatusUtils; |
| 26 | +import org.apache.iotdb.consensus.common.Peer; |
| 27 | +import org.apache.iotdb.consensus.common.request.DeserializedBatchIndexedConsensusRequest; |
| 28 | +import org.apache.iotdb.consensus.common.request.IndexedConsensusRequest; |
| 29 | +import org.apache.iotdb.consensus.config.IoTConsensusConfig; |
| 30 | +import org.apache.iotdb.consensus.iot.util.TestEntry; |
| 31 | +import org.apache.iotdb.consensus.iot.util.TestStateMachine; |
| 32 | +import org.apache.iotdb.rpc.TSStatusCode; |
| 33 | + |
| 34 | +import org.junit.Rule; |
| 35 | +import org.junit.Test; |
| 36 | +import org.junit.rules.TemporaryFolder; |
| 37 | + |
| 38 | +import java.util.Collections; |
| 39 | +import java.util.TreeSet; |
| 40 | +import java.util.concurrent.Executors; |
| 41 | +import java.util.concurrent.ScheduledExecutorService; |
| 42 | +import java.util.concurrent.TimeUnit; |
| 43 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 44 | +import java.util.concurrent.atomic.AtomicReference; |
| 45 | + |
| 46 | +import static org.junit.Assert.assertEquals; |
| 47 | +import static org.junit.Assert.assertFalse; |
| 48 | +import static org.junit.Assert.assertTrue; |
| 49 | + |
| 50 | +public class IoTConsensusServerImplTest { |
| 51 | + @Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder(); |
| 52 | + |
| 53 | + @Test |
| 54 | + public void testSyncLogBecomesReadOnlyWhileWaiting() throws Exception { |
| 55 | + final ScheduledExecutorService backgroundTaskService = |
| 56 | + Executors.newSingleThreadScheduledExecutor(); |
| 57 | + final AtomicBoolean readOnly = new AtomicBoolean(); |
| 58 | + final TestStateMachine stateMachine = |
| 59 | + new TestStateMachine() { |
| 60 | + @Override |
| 61 | + public boolean isReadOnly() { |
| 62 | + return readOnly.get(); |
| 63 | + } |
| 64 | + }; |
| 65 | + final Peer thisNode = new Peer(new DataRegionId(1), 0, new TEndPoint("127.0.0.1", 6667)); |
| 66 | + final IoTConsensusServerImpl server = |
| 67 | + new IoTConsensusServerImpl( |
| 68 | + temporaryFolder.getRoot().getAbsolutePath(), |
| 69 | + thisNode, |
| 70 | + new TreeSet<>(Collections.singletonList(thisNode)), |
| 71 | + stateMachine, |
| 72 | + backgroundTaskService, |
| 73 | + null, |
| 74 | + null, |
| 75 | + IoTConsensusConfig.newBuilder() |
| 76 | + .setReplication( |
| 77 | + IoTConsensusConfig.Replication.newBuilder() |
| 78 | + .setMaxWaitingTimeForWaitBatchInMs(100) |
| 79 | + .build()) |
| 80 | + .build()); |
| 81 | + final DeserializedBatchIndexedConsensusRequest request = |
| 82 | + new DeserializedBatchIndexedConsensusRequest(1, 2, 2); |
| 83 | + for (int index = 1; index <= 2; index++) { |
| 84 | + request.add( |
| 85 | + new IndexedConsensusRequest( |
| 86 | + index, index, Collections.singletonList(new TestEntry(index, thisNode)))); |
| 87 | + } |
| 88 | + final AtomicReference<TSStatus> status = new AtomicReference<>(); |
| 89 | + final Thread syncLogThread = new Thread(() -> status.set(server.syncLog(1, request))); |
| 90 | + try { |
| 91 | + assertFalse(server.isReadOnly()); |
| 92 | + syncLogThread.start(); |
| 93 | + final long deadline = System.nanoTime() + TimeUnit.SECONDS.toNanos(5); |
| 94 | + while (syncLogThread.getState() != Thread.State.TIMED_WAITING |
| 95 | + && System.nanoTime() < deadline) { |
| 96 | + Thread.sleep(10); |
| 97 | + } |
| 98 | + assertEquals(Thread.State.TIMED_WAITING, syncLogThread.getState()); |
| 99 | + readOnly.set(true); |
| 100 | + syncLogThread.join(TimeUnit.SECONDS.toMillis(15)); |
| 101 | + assertFalse(syncLogThread.isAlive()); |
| 102 | + assertEquals(2, status.get().getSubStatusSize()); |
| 103 | + assertEquals( |
| 104 | + TSStatusCode.SYSTEM_READ_ONLY.getStatusCode(), |
| 105 | + status.get().getSubStatus().get(0).getCode()); |
| 106 | + assertEquals( |
| 107 | + StatusUtils.getStatus(TSStatusCode.SYSTEM_READ_ONLY).getMessage(), |
| 108 | + status.get().getSubStatus().get(0).getMessage()); |
| 109 | + assertEquals( |
| 110 | + TSStatusCode.SYSTEM_READ_ONLY.getStatusCode(), |
| 111 | + status.get().getSubStatus().get(1).getCode()); |
| 112 | + assertTrue(stateMachine.getRequestSet().isEmpty()); |
| 113 | + |
| 114 | + // The sender must be able to retry the complete batch after the replica becomes writable. |
| 115 | + readOnly.set(false); |
| 116 | + server.reloadConsensusConfig( |
| 117 | + IoTConsensusConfig.newBuilder() |
| 118 | + .setReplication( |
| 119 | + IoTConsensusConfig.Replication.newBuilder() |
| 120 | + .setMaxPendingBatchesNum(1) |
| 121 | + .setMaxWaitingTimeForWaitBatchInMs(100) |
| 122 | + .build()) |
| 123 | + .build()); |
| 124 | + final TSStatus retryStatus = server.syncLog(1, request); |
| 125 | + assertTrue( |
| 126 | + retryStatus.getSubStatus().stream() |
| 127 | + .allMatch(s -> s.getCode() == TSStatusCode.SUCCESS_STATUS.getStatusCode())); |
| 128 | + assertEquals(2, stateMachine.getRequestSet().size()); |
| 129 | + } finally { |
| 130 | + syncLogThread.interrupt(); |
| 131 | + syncLogThread.join(TimeUnit.SECONDS.toMillis(5)); |
| 132 | + backgroundTaskService.shutdownNow(); |
| 133 | + } |
| 134 | + } |
| 135 | +} |
0 commit comments