|
| 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.db.it; |
| 21 | + |
| 22 | +import org.apache.iotdb.it.env.EnvFactory; |
| 23 | +import org.apache.iotdb.it.env.cluster.node.DataNodeWrapper; |
| 24 | +import org.apache.iotdb.it.framework.IoTDBTestRunner; |
| 25 | +import org.apache.iotdb.it.utils.TsFileGenerator; |
| 26 | +import org.apache.iotdb.itbase.category.ClusterIT; |
| 27 | +import org.apache.iotdb.itbase.category.LocalStandaloneIT; |
| 28 | + |
| 29 | +import org.apache.tsfile.enums.TSDataType; |
| 30 | +import org.apache.tsfile.file.metadata.enums.TSEncoding; |
| 31 | +import org.apache.tsfile.write.schema.MeasurementSchema; |
| 32 | +import org.junit.After; |
| 33 | +import org.junit.Assert; |
| 34 | +import org.junit.Before; |
| 35 | +import org.junit.Test; |
| 36 | +import org.junit.experimental.categories.Category; |
| 37 | +import org.junit.runner.RunWith; |
| 38 | + |
| 39 | +import java.io.File; |
| 40 | +import java.nio.file.Files; |
| 41 | +import java.sql.Connection; |
| 42 | +import java.sql.Statement; |
| 43 | +import java.util.Collections; |
| 44 | +import java.util.concurrent.TimeUnit; |
| 45 | + |
| 46 | +@RunWith(IoTDBTestRunner.class) |
| 47 | +@Category({LocalStandaloneIT.class, ClusterIT.class}) |
| 48 | +public class IoTDBLoadTsFileActiveRetryIT { |
| 49 | + |
| 50 | + private static final String DATABASE = "root.sg.test_0"; |
| 51 | + private static final String DEVICE = DATABASE + ".d_0"; |
| 52 | + private static final String MEASUREMENT = "sensor_00"; |
| 53 | + private static final long UNALLOCATABLE_TABLET_CONVERSION_BATCH_MEMORY_SIZE_IN_BYTES = |
| 54 | + Long.MAX_VALUE / 4; |
| 55 | + private static final MeasurementSchema TSFILE_SCHEMA = |
| 56 | + new MeasurementSchema(MEASUREMENT, TSDataType.INT32, TSEncoding.RLE); |
| 57 | + |
| 58 | + private File tmpDir; |
| 59 | + |
| 60 | + @Before |
| 61 | + public void setUp() throws Exception { |
| 62 | + tmpDir = new File(Files.createTempDirectory("load-active-retry").toUri()); |
| 63 | + EnvFactory.getEnv().getConfig().getCommonConfig().setPipeMemoryManagementEnabled(false); |
| 64 | + EnvFactory.getEnv() |
| 65 | + .getConfig() |
| 66 | + .getDataNodeConfig() |
| 67 | + .setMaxAllocateMemoryRatioForLoad(1.0) |
| 68 | + .setLoadTsFileAnalyzeSchemaMemorySizeInBytes(1) |
| 69 | + .setLoadTsFileTabletConversionBatchMemorySizeInBytes( |
| 70 | + UNALLOCATABLE_TABLET_CONVERSION_BATCH_MEMORY_SIZE_IN_BYTES) |
| 71 | + .setLoadActiveListeningCheckIntervalSeconds(1); |
| 72 | + |
| 73 | + EnvFactory.getEnv().initClusterEnvironment(); |
| 74 | + } |
| 75 | + |
| 76 | + @After |
| 77 | + public void tearDown() throws Exception { |
| 78 | + try (final Connection connection = EnvFactory.getEnv().getConnection(); |
| 79 | + final Statement statement = connection.createStatement()) { |
| 80 | + statement.execute("delete database " + DATABASE); |
| 81 | + } catch (final Exception ignored) { |
| 82 | + // ignore cleanup failure |
| 83 | + } finally { |
| 84 | + EnvFactory.getEnv().cleanClusterEnvironment(); |
| 85 | + deleteRecursively(tmpDir); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testActiveLoadTemporaryUnavailableShouldKeepFileForRetry() throws Exception { |
| 91 | + final DataNodeWrapper dataNodeWrapper = EnvFactory.getEnv().getDataNodeWrapper(0); |
| 92 | + final File retryTsFile = new File(tmpDir, "1-0-0-0.tsfile"); |
| 93 | + final File permanentFailureTsFile = new File(tmpDir, "2-0-0-0.tsfile"); |
| 94 | + generateTsFile(retryTsFile); |
| 95 | + generateTsFile(permanentFailureTsFile); |
| 96 | + |
| 97 | + try (final Connection connection = |
| 98 | + EnvFactory.getEnv().getConnectionWithSpecifiedDataNode(dataNodeWrapper); |
| 99 | + final Statement statement = connection.createStatement()) { |
| 100 | + statement.execute("create database " + DATABASE); |
| 101 | + statement.execute( |
| 102 | + String.format( |
| 103 | + "create timeseries %s.%s %s", DEVICE, MEASUREMENT, TSDataType.INT64.name())); |
| 104 | + |
| 105 | + statement.execute( |
| 106 | + String.format( |
| 107 | + "load \"%s\" with ('database-level'='3', 'async'='true', 'on-success'='none', " |
| 108 | + + "'convert-on-type-mismatch'='true')", |
| 109 | + retryTsFile.getAbsolutePath())); |
| 110 | + statement.execute( |
| 111 | + String.format( |
| 112 | + "load \"%s\" with ('database-level'='3', 'async'='true', 'on-success'='none', " |
| 113 | + + "'convert-on-type-mismatch'='false')", |
| 114 | + permanentFailureTsFile.getAbsolutePath())); |
| 115 | + |
| 116 | + final File activeDir = getActiveLoadDir(dataNodeWrapper); |
| 117 | + final File failDir = getActiveLoadFailDir(dataNodeWrapper); |
| 118 | + final File activeTsFile = waitForFile(activeDir, retryTsFile.getName(), 30_000L); |
| 119 | + |
| 120 | + Assert.assertNotNull( |
| 121 | + "Async load should copy tsfile into active load directory", activeTsFile); |
| 122 | + |
| 123 | + Assert.assertNotNull( |
| 124 | + "Permanent active load failure should be moved to fail dir", |
| 125 | + waitForFile(failDir, permanentFailureTsFile.getName(), TimeUnit.SECONDS.toMillis(60))); |
| 126 | + |
| 127 | + assertFileKeptForRetry( |
| 128 | + activeDir, failDir, retryTsFile.getName(), TimeUnit.SECONDS.toMillis(12)); |
| 129 | + } |
| 130 | + } |
| 131 | + |
| 132 | + private void generateTsFile(final File tsFile) throws Exception { |
| 133 | + try (final TsFileGenerator generator = new TsFileGenerator(tsFile)) { |
| 134 | + generator.registerTimeseries(DEVICE, Collections.singletonList(TSFILE_SCHEMA)); |
| 135 | + generator.generateData(DEVICE, 10, 1, false); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private File getActiveLoadDir(final DataNodeWrapper dataNodeWrapper) { |
| 140 | + return new File( |
| 141 | + dataNodeWrapper.getNodePath() |
| 142 | + + File.separator |
| 143 | + + "ext" |
| 144 | + + File.separator |
| 145 | + + "load" |
| 146 | + + File.separator |
| 147 | + + "pending"); |
| 148 | + } |
| 149 | + |
| 150 | + private File getActiveLoadFailDir(final DataNodeWrapper dataNodeWrapper) { |
| 151 | + return new File( |
| 152 | + dataNodeWrapper.getNodePath() |
| 153 | + + File.separator |
| 154 | + + "ext" |
| 155 | + + File.separator |
| 156 | + + "load" |
| 157 | + + File.separator |
| 158 | + + "failed"); |
| 159 | + } |
| 160 | + |
| 161 | + private File waitForFile(final File root, final String fileName, final long timeoutMs) |
| 162 | + throws InterruptedException { |
| 163 | + final long deadline = System.currentTimeMillis() + timeoutMs; |
| 164 | + while (System.currentTimeMillis() < deadline) { |
| 165 | + final File file = findFile(root, fileName); |
| 166 | + if (file != null) { |
| 167 | + return file; |
| 168 | + } |
| 169 | + Thread.sleep(500L); |
| 170 | + } |
| 171 | + return null; |
| 172 | + } |
| 173 | + |
| 174 | + private boolean containsFile(final File root, final String fileName) { |
| 175 | + return findFile(root, fileName) != null; |
| 176 | + } |
| 177 | + |
| 178 | + private void assertFileKeptForRetry( |
| 179 | + final File activeDir, final File failDir, final String fileName, final long observationMs) |
| 180 | + throws InterruptedException { |
| 181 | + final long deadline = System.currentTimeMillis() + observationMs; |
| 182 | + while (System.currentTimeMillis() < deadline) { |
| 183 | + Assert.assertTrue( |
| 184 | + "Temporary unavailable active load should keep tsfile for retry", |
| 185 | + containsFile(activeDir, fileName)); |
| 186 | + Assert.assertFalse( |
| 187 | + "Temporary unavailable active load must not move tsfile to fail dir", |
| 188 | + containsFile(failDir, fileName)); |
| 189 | + Thread.sleep(500L); |
| 190 | + } |
| 191 | + } |
| 192 | + |
| 193 | + private File findFile(final File root, final String fileName) { |
| 194 | + if (root == null || !root.exists()) { |
| 195 | + return null; |
| 196 | + } |
| 197 | + if (root.isFile()) { |
| 198 | + return root.getName().equals(fileName) ? root : null; |
| 199 | + } |
| 200 | + |
| 201 | + final File[] children = root.listFiles(); |
| 202 | + if (children == null) { |
| 203 | + return null; |
| 204 | + } |
| 205 | + for (final File child : children) { |
| 206 | + final File file = findFile(child, fileName); |
| 207 | + if (file != null) { |
| 208 | + return file; |
| 209 | + } |
| 210 | + } |
| 211 | + return null; |
| 212 | + } |
| 213 | + |
| 214 | + private void deleteRecursively(final File file) { |
| 215 | + if (file == null || !file.exists()) { |
| 216 | + return; |
| 217 | + } |
| 218 | + final File[] children = file.listFiles(); |
| 219 | + if (children != null) { |
| 220 | + for (final File child : children) { |
| 221 | + deleteRecursively(child); |
| 222 | + } |
| 223 | + } |
| 224 | + Assert.assertTrue(file.delete()); |
| 225 | + } |
| 226 | +} |
0 commit comments