|
| 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.TTimePartitionSlot; |
| 23 | +import org.apache.iotdb.commons.client.sync.SyncConfigNodeIServiceClient; |
| 24 | +import org.apache.iotdb.confignode.rpc.thrift.TDataPartitionReq; |
| 25 | +import org.apache.iotdb.confignode.rpc.thrift.TDataPartitionTableResp; |
| 26 | +import org.apache.iotdb.it.env.EnvFactory; |
| 27 | +import org.apache.iotdb.it.framework.IoTDBTestRunner; |
| 28 | +import org.apache.iotdb.itbase.category.ClusterIT; |
| 29 | +import org.apache.iotdb.rpc.TSStatusCode; |
| 30 | + |
| 31 | +import org.junit.After; |
| 32 | +import org.junit.Assert; |
| 33 | +import org.junit.Before; |
| 34 | +import org.junit.Test; |
| 35 | +import org.junit.experimental.categories.Category; |
| 36 | +import org.junit.runner.RunWith; |
| 37 | + |
| 38 | +import java.sql.Connection; |
| 39 | +import java.sql.Statement; |
| 40 | +import java.util.TreeMap; |
| 41 | +import java.util.concurrent.TimeUnit; |
| 42 | + |
| 43 | +@RunWith(IoTDBTestRunner.class) |
| 44 | +@Category({ClusterIT.class}) |
| 45 | +public class IoTDBPartitionTableAutoCleanUSIT { |
| 46 | + |
| 47 | + private static final String TREE_DATABASE_PREFIX = "root.db.g_"; |
| 48 | + |
| 49 | + private static final int TEST_REPLICATION_FACTOR = 1; |
| 50 | + private static final long TEST_TIME_PARTITION_INTERVAL_IN_MS = 604800_000; |
| 51 | + private static final long TEST_TTL_CHECK_INTERVAL = 5_000; |
| 52 | + |
| 53 | + private static final TTimePartitionSlot TEST_CURRENT_TIME_SLOT = |
| 54 | + new TTimePartitionSlot() |
| 55 | + .setStartTime( |
| 56 | + System.currentTimeMillis() |
| 57 | + * 1000L |
| 58 | + / TEST_TIME_PARTITION_INTERVAL_IN_MS |
| 59 | + * TEST_TIME_PARTITION_INTERVAL_IN_MS); |
| 60 | + private static final long TEST_TTL_IN_MS = 7 * TEST_TIME_PARTITION_INTERVAL_IN_MS; |
| 61 | + |
| 62 | + @Before |
| 63 | + public void setUp() throws Exception { |
| 64 | + EnvFactory.getEnv() |
| 65 | + .getConfig() |
| 66 | + .getCommonConfig() |
| 67 | + .setSchemaReplicationFactor(TEST_REPLICATION_FACTOR) |
| 68 | + .setDataReplicationFactor(TEST_REPLICATION_FACTOR) |
| 69 | + .setTimePartitionInterval(TEST_TIME_PARTITION_INTERVAL_IN_MS) |
| 70 | + .setTTLCheckInterval(TEST_TTL_CHECK_INTERVAL) |
| 71 | + // Note that the time precision of IoTDB is us in this IT |
| 72 | + .setTimestampPrecision("us"); |
| 73 | + |
| 74 | + // Init 1C1D environment |
| 75 | + EnvFactory.getEnv().initClusterEnvironment(1, 1); |
| 76 | + } |
| 77 | + |
| 78 | + @After |
| 79 | + public void tearDown() { |
| 80 | + EnvFactory.getEnv().cleanClusterEnvironment(); |
| 81 | + } |
| 82 | + |
| 83 | + @Test |
| 84 | + public void testAutoCleanPartitionTableForTreeModel() throws Exception { |
| 85 | + try (Connection connection = EnvFactory.getEnv().getConnection(); |
| 86 | + Statement statement = connection.createStatement()) { |
| 87 | + // Create databases and insert test data |
| 88 | + for (int i = 0; i < 3; i++) { |
| 89 | + String databaseName = String.format("%s%d", TREE_DATABASE_PREFIX, i); |
| 90 | + statement.execute(String.format("CREATE DATABASE %s", databaseName)); |
| 91 | + statement.execute( |
| 92 | + String.format( |
| 93 | + "CREATE TIMESERIES %s.s WITH DATATYPE=INT64,ENCODING=PLAIN", databaseName)); |
| 94 | + // Insert expired data |
| 95 | + statement.execute( |
| 96 | + String.format( |
| 97 | + "INSERT INTO %s(timestamp, s) VALUES (%d, %d)", |
| 98 | + databaseName, TEST_CURRENT_TIME_SLOT.getStartTime() - TEST_TTL_IN_MS * 2000, -1)); |
| 99 | + // Insert existed data |
| 100 | + statement.execute( |
| 101 | + String.format( |
| 102 | + "INSERT INTO %s(timestamp, s) VALUES (%d, %d)", |
| 103 | + databaseName, TEST_CURRENT_TIME_SLOT.getStartTime(), 1)); |
| 104 | + } |
| 105 | + // Let db0.TTL > device.TTL, the valid TTL should be the bigger one |
| 106 | + statement.execute(String.format("SET TTL TO %s0 %d", TREE_DATABASE_PREFIX, TEST_TTL_IN_MS)); |
| 107 | + statement.execute(String.format("SET TTL TO %s0.s %d", TREE_DATABASE_PREFIX, 10)); |
| 108 | + // Let db1.TTL < device.TTL, the valid TTL should be the bigger one |
| 109 | + statement.execute(String.format("SET TTL TO %s1 %d", TREE_DATABASE_PREFIX, 10)); |
| 110 | + statement.execute(String.format("SET TTL TO %s1.s %d", TREE_DATABASE_PREFIX, TEST_TTL_IN_MS)); |
| 111 | + // Set TTL to path db2.** |
| 112 | + statement.execute( |
| 113 | + String.format("SET TTL TO %s2.** %d", TREE_DATABASE_PREFIX, TEST_TTL_IN_MS)); |
| 114 | + } |
| 115 | + TDataPartitionReq req = new TDataPartitionReq(); |
| 116 | + for (int i = 0; i < 3; i++) { |
| 117 | + req.putToPartitionSlotsMap(String.format("%s%d", TREE_DATABASE_PREFIX, i), new TreeMap<>()); |
| 118 | + } |
| 119 | + try (SyncConfigNodeIServiceClient client = |
| 120 | + (SyncConfigNodeIServiceClient) EnvFactory.getEnv().getLeaderConfigNodeConnection()) { |
| 121 | + for (int retry = 0; retry < 120; retry++) { |
| 122 | + boolean partitionTableAutoCleaned = true; |
| 123 | + TDataPartitionTableResp resp = client.getDataPartitionTable(req); |
| 124 | + if (TSStatusCode.SUCCESS_STATUS.getStatusCode() == resp.getStatus().getCode()) { |
| 125 | + partitionTableAutoCleaned = |
| 126 | + resp.getDataPartitionTable().entrySet().stream() |
| 127 | + .flatMap(e1 -> e1.getValue().entrySet().stream()) |
| 128 | + .allMatch(e2 -> e2.getValue().size() == 1); |
| 129 | + } |
| 130 | + if (partitionTableAutoCleaned) { |
| 131 | + return; |
| 132 | + } |
| 133 | + TimeUnit.SECONDS.sleep(1); |
| 134 | + } |
| 135 | + } |
| 136 | + Assert.fail("The PartitionTable in the ConfigNode is not auto cleaned!"); |
| 137 | + } |
| 138 | +} |
0 commit comments