|
| 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.confignode.it.partition; |
| 21 | + |
| 22 | +import org.apache.iotdb.common.rpc.thrift.TConsensusGroupId; |
| 23 | +import org.apache.iotdb.common.rpc.thrift.TSStatus; |
| 24 | +import org.apache.iotdb.common.rpc.thrift.TSeriesPartitionSlot; |
| 25 | +import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot; |
| 26 | +import org.apache.iotdb.commons.client.sync.SyncConfigNodeIServiceClient; |
| 27 | +import org.apache.iotdb.confignode.it.utils.ConfigNodeTestUtils; |
| 28 | +import org.apache.iotdb.confignode.rpc.thrift.TDataPartitionReq; |
| 29 | +import org.apache.iotdb.confignode.rpc.thrift.TDataPartitionTableResp; |
| 30 | +import org.apache.iotdb.confignode.rpc.thrift.TDatabaseSchema; |
| 31 | +import org.apache.iotdb.consensus.ConsensusFactory; |
| 32 | +import org.apache.iotdb.it.env.EnvFactory; |
| 33 | +import org.apache.iotdb.it.framework.IoTDBTestRunner; |
| 34 | +import org.apache.iotdb.itbase.category.ClusterIT; |
| 35 | +import org.apache.iotdb.rpc.TSStatusCode; |
| 36 | + |
| 37 | +import org.junit.AfterClass; |
| 38 | +import org.junit.Assert; |
| 39 | +import org.junit.BeforeClass; |
| 40 | +import org.junit.Test; |
| 41 | +import org.junit.experimental.categories.Category; |
| 42 | +import org.junit.runner.RunWith; |
| 43 | + |
| 44 | +import java.util.ArrayList; |
| 45 | +import java.util.Collections; |
| 46 | +import java.util.List; |
| 47 | +import java.util.Map; |
| 48 | + |
| 49 | +@RunWith(IoTDBTestRunner.class) |
| 50 | +@Category({ClusterIT.class}) |
| 51 | +public class IoTDBPartitionShuffleStrategyIT { |
| 52 | + |
| 53 | + private static final String testDataRegionConsensusProtocolClass = |
| 54 | + ConsensusFactory.RATIS_CONSENSUS; |
| 55 | + private static final int testReplicationFactor = 1; |
| 56 | + private static final String testDataPartitionAllocationStrategy = "SHUFFLE"; |
| 57 | + private static final int testSeriesSlotNum = 1000; |
| 58 | + private static final long testTimePartitionInterval = 604800000; |
| 59 | + private static final double testDataRegionPerDataNode = 5.0; |
| 60 | + |
| 61 | + private static final String database = "root.database"; |
| 62 | + private static final int testTimePartitionSlotsNum = 100; |
| 63 | + |
| 64 | + @BeforeClass |
| 65 | + public static void setUp() throws Exception { |
| 66 | + EnvFactory.getEnv() |
| 67 | + .getConfig() |
| 68 | + .getCommonConfig() |
| 69 | + .setDataRegionConsensusProtocolClass(testDataRegionConsensusProtocolClass) |
| 70 | + .setDataReplicationFactor(testReplicationFactor) |
| 71 | + .setTimePartitionInterval(testTimePartitionInterval) |
| 72 | + .setSeriesSlotNum(testSeriesSlotNum) |
| 73 | + .setDataPartitionAllocationStrategy(testDataPartitionAllocationStrategy) |
| 74 | + .setDataRegionPerDataNode(testDataRegionPerDataNode); |
| 75 | + |
| 76 | + // Init 1C1D environment |
| 77 | + EnvFactory.getEnv().initClusterEnvironment(1, 1); |
| 78 | + |
| 79 | + // Set Database |
| 80 | + try (SyncConfigNodeIServiceClient client = |
| 81 | + (SyncConfigNodeIServiceClient) EnvFactory.getEnv().getLeaderConfigNodeConnection()) { |
| 82 | + TSStatus status = client.setDatabase(new TDatabaseSchema(database)); |
| 83 | + Assert.assertEquals(TSStatusCode.SUCCESS_STATUS.getStatusCode(), status.getCode()); |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + @AfterClass |
| 88 | + public static void tearDown() { |
| 89 | + EnvFactory.getEnv().cleanClusterEnvironment(); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + public void testDataPartitionShuffleStrategy() throws Exception { |
| 94 | + List<Integer> randomTimeSlotList = new ArrayList<>(); |
| 95 | + for (int i = 0; i < testTimePartitionSlotsNum; i++) { |
| 96 | + randomTimeSlotList.add(i); |
| 97 | + } |
| 98 | + Collections.shuffle(randomTimeSlotList); |
| 99 | + for (int timeSlotId : randomTimeSlotList) { |
| 100 | + // To test the shuffle strategy, we merely need to use a random time slot order |
| 101 | + ConfigNodeTestUtils.getOrCreateDataPartitionWithRetry( |
| 102 | + database, 0, testSeriesSlotNum, timeSlotId, timeSlotId + 1, testTimePartitionInterval); |
| 103 | + } |
| 104 | + TDataPartitionTableResp dataPartitionTableResp; |
| 105 | + try (SyncConfigNodeIServiceClient client = |
| 106 | + (SyncConfigNodeIServiceClient) EnvFactory.getEnv().getLeaderConfigNodeConnection()) { |
| 107 | + dataPartitionTableResp = |
| 108 | + client.getDataPartitionTable( |
| 109 | + new TDataPartitionReq( |
| 110 | + ConfigNodeTestUtils.constructPartitionSlotsMap( |
| 111 | + database, |
| 112 | + 0, |
| 113 | + testSeriesSlotNum, |
| 114 | + 0, |
| 115 | + testTimePartitionSlotsNum, |
| 116 | + testTimePartitionInterval))); |
| 117 | + } |
| 118 | + Map<String, Map<TSeriesPartitionSlot, Map<TTimePartitionSlot, List<TConsensusGroupId>>>> |
| 119 | + partitionTable = dataPartitionTableResp.getDataPartitionTable(); |
| 120 | + for (long currentStartTime = testTimePartitionInterval; |
| 121 | + currentStartTime < testTimePartitionInterval * testTimePartitionSlotsNum; |
| 122 | + currentStartTime += testTimePartitionInterval) { |
| 123 | + TTimePartitionSlot precedingTimeSlot = |
| 124 | + new TTimePartitionSlot(currentStartTime - testTimePartitionInterval); |
| 125 | + TTimePartitionSlot currentTimeSlot = new TTimePartitionSlot(currentStartTime); |
| 126 | + for (int seriesSlotId = 0; seriesSlotId < testSeriesSlotNum; seriesSlotId++) { |
| 127 | + TSeriesPartitionSlot seriesPartitionSlot = new TSeriesPartitionSlot(seriesSlotId); |
| 128 | + List<TConsensusGroupId> precedingRegionGroupIds = |
| 129 | + partitionTable.get(database).get(seriesPartitionSlot).get(precedingTimeSlot); |
| 130 | + List<TConsensusGroupId> currentRegionGroupIds = |
| 131 | + partitionTable.get(database).get(seriesPartitionSlot).get(currentTimeSlot); |
| 132 | + Assert.assertEquals(precedingRegionGroupIds.size(), currentRegionGroupIds.size()); |
| 133 | + for (int i = 0; i < precedingRegionGroupIds.size(); i++) { |
| 134 | + // Ensure that the RegionGroupId is different in two adjacent TimePartitionSlots |
| 135 | + Assert.assertNotEquals(precedingRegionGroupIds.get(i), currentRegionGroupIds.get(i)); |
| 136 | + } |
| 137 | + } |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments