|
| 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.regionmigration.pass.daily.iotv1; |
| 21 | + |
| 22 | +import org.apache.iotdb.it.env.EnvFactory; |
| 23 | +import org.apache.iotdb.it.env.cluster.node.DataNodeWrapper; |
| 24 | + |
| 25 | +import org.awaitility.Awaitility; |
| 26 | +import org.junit.Assert; |
| 27 | + |
| 28 | +import java.io.File; |
| 29 | +import java.sql.ResultSet; |
| 30 | +import java.sql.Statement; |
| 31 | +import java.util.HashSet; |
| 32 | +import java.util.Set; |
| 33 | +import java.util.concurrent.TimeUnit; |
| 34 | + |
| 35 | +final class RegionMigrateFileAssertions { |
| 36 | + |
| 37 | + static final String MULTI_DATA_DIRS = |
| 38 | + "data/datanode/data/disk0,data/datanode/data/disk1,data/datanode/data/disk2"; |
| 39 | + |
| 40 | + private static final String SEQUENCE_FOLDER = "sequence"; |
| 41 | + private static final String TSFILE_SUFFIX = ".tsfile"; |
| 42 | + private static final String TSFILE_RESOURCE_SUFFIX = ".tsfile.resource"; |
| 43 | + private static final String MODS_SUFFIX = ".mods2"; |
| 44 | + |
| 45 | + private RegionMigrateFileAssertions() {} |
| 46 | + |
| 47 | + static void awaitTsFileVisibleOnReplicas( |
| 48 | + String database, int dataRegionId, Set<Integer> dataNodeIds) { |
| 49 | + awaitFileVisibleOnReplicas(database, dataRegionId, dataNodeIds, TSFILE_SUFFIX); |
| 50 | + } |
| 51 | + |
| 52 | + static void awaitTsFileResourceVisibleOnReplicas( |
| 53 | + String database, int dataRegionId, Set<Integer> dataNodeIds) { |
| 54 | + awaitFileVisibleOnReplicas(database, dataRegionId, dataNodeIds, TSFILE_RESOURCE_SUFFIX); |
| 55 | + } |
| 56 | + |
| 57 | + static void awaitTsFileResourceVisibleOnReplicas( |
| 58 | + Statement statement, String database, int dataRegionId, Set<Integer> dataNodeIds) { |
| 59 | + awaitFileVisibleOnReplicas( |
| 60 | + statement, database, dataRegionId, dataNodeIds, TSFILE_RESOURCE_SUFFIX); |
| 61 | + } |
| 62 | + |
| 63 | + static void awaitModsVisibleOnReplicas( |
| 64 | + String database, int dataRegionId, Set<Integer> dataNodeIds) { |
| 65 | + awaitFileVisibleOnReplicas(database, dataRegionId, dataNodeIds, MODS_SUFFIX); |
| 66 | + } |
| 67 | + |
| 68 | + static void awaitRegionReplicas( |
| 69 | + Statement statement, int dataRegionId, Set<Integer> expectedReplicaDataNodeIds) { |
| 70 | + Awaitility.await() |
| 71 | + .atMost(10, TimeUnit.MINUTES) |
| 72 | + .pollDelay(1, TimeUnit.SECONDS) |
| 73 | + .pollInterval(2, TimeUnit.SECONDS) |
| 74 | + .untilAsserted( |
| 75 | + () -> |
| 76 | + Assert.assertEquals( |
| 77 | + expectedReplicaDataNodeIds, getReplicaDataNodeIds(statement, dataRegionId))); |
| 78 | + } |
| 79 | + |
| 80 | + static Set<Integer> getReplicaDataNodeIds(Statement statement, int dataRegionId) |
| 81 | + throws Exception { |
| 82 | + Set<Integer> replicaDataNodeIds = new HashSet<>(); |
| 83 | + try (ResultSet showRegions = statement.executeQuery("SHOW REGIONS")) { |
| 84 | + while (showRegions.next()) { |
| 85 | + if ("DataRegion".equals(showRegions.getString("Type")) |
| 86 | + && showRegions.getInt("RegionId") == dataRegionId) { |
| 87 | + replicaDataNodeIds.add(showRegions.getInt("DataNodeId")); |
| 88 | + } |
| 89 | + } |
| 90 | + } |
| 91 | + Assert.assertFalse(replicaDataNodeIds.isEmpty()); |
| 92 | + return replicaDataNodeIds; |
| 93 | + } |
| 94 | + |
| 95 | + private static void awaitFileVisibleOnReplicas( |
| 96 | + String database, int dataRegionId, Set<Integer> dataNodeIds, String suffix) { |
| 97 | + awaitFileVisibleOnReplicas(null, database, dataRegionId, dataNodeIds, suffix); |
| 98 | + } |
| 99 | + |
| 100 | + private static void awaitFileVisibleOnReplicas( |
| 101 | + Statement flushStatement, |
| 102 | + String database, |
| 103 | + int dataRegionId, |
| 104 | + Set<Integer> dataNodeIds, |
| 105 | + String suffix) { |
| 106 | + for (int dataNodeId : dataNodeIds) { |
| 107 | + DataNodeWrapper dataNodeWrapper = |
| 108 | + EnvFactory.getEnv().dataNodeIdToWrapper(dataNodeId).orElseThrow(); |
| 109 | + Awaitility.await() |
| 110 | + .atMost(2, TimeUnit.MINUTES) |
| 111 | + .pollDelay(500, TimeUnit.MILLISECONDS) |
| 112 | + .pollInterval(1, TimeUnit.SECONDS) |
| 113 | + .untilAsserted( |
| 114 | + () -> { |
| 115 | + if (flushStatement != null) { |
| 116 | + flushStatement.execute("FLUSH"); |
| 117 | + } |
| 118 | + Assert.assertTrue( |
| 119 | + String.format( |
| 120 | + "Expected file with suffix %s for database %s region %d on DataNode %d", |
| 121 | + suffix, database, dataRegionId, dataNodeId), |
| 122 | + containsSequenceFileWithSuffix( |
| 123 | + dataNodeWrapper, database, dataRegionId, suffix)); |
| 124 | + }); |
| 125 | + } |
| 126 | + } |
| 127 | + |
| 128 | + private static boolean containsSequenceFileWithSuffix( |
| 129 | + DataNodeWrapper dataNodeWrapper, String database, int dataRegionId, String suffix) { |
| 130 | + for (String dataDir : MULTI_DATA_DIRS.split(",")) { |
| 131 | + File regionDir = |
| 132 | + new File( |
| 133 | + dataNodeWrapper.getNodePath(), |
| 134 | + dataDir |
| 135 | + + File.separator |
| 136 | + + SEQUENCE_FOLDER |
| 137 | + + File.separator |
| 138 | + + database |
| 139 | + + File.separator |
| 140 | + + dataRegionId); |
| 141 | + if (containsFileWithSuffix(regionDir, suffix)) { |
| 142 | + return true; |
| 143 | + } |
| 144 | + } |
| 145 | + return false; |
| 146 | + } |
| 147 | + |
| 148 | + private static boolean containsFileWithSuffix(File file, String suffix) { |
| 149 | + if (!file.exists()) { |
| 150 | + return false; |
| 151 | + } |
| 152 | + if (file.isFile()) { |
| 153 | + // IoTConsensus followers can create an open zero-byte TsFile before a later FLUSH closes it. |
| 154 | + return file.getName().endsWith(suffix) && (TSFILE_SUFFIX.equals(suffix) || file.length() > 0); |
| 155 | + } |
| 156 | + File[] children = file.listFiles(); |
| 157 | + if (children == null) { |
| 158 | + return false; |
| 159 | + } |
| 160 | + for (File child : children) { |
| 161 | + if (containsFileWithSuffix(child, suffix)) { |
| 162 | + return true; |
| 163 | + } |
| 164 | + } |
| 165 | + return false; |
| 166 | + } |
| 167 | +} |
0 commit comments