Skip to content

Commit 4e62af2

Browse files
CRZbulabulaHTHou
authored andcommitted
[To dev/1.3] Use reference time position for PartitionTableAutoCleaner
1 parent 8f33259 commit 4e62af2

File tree

5 files changed

+176
-20
lines changed

5 files changed

+176
-20
lines changed

integration-test/src/test/java/org/apache/iotdb/confignode/it/partition/IoTDBPartitionTableAutoCleanIT.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
2323
import org.apache.iotdb.commons.client.sync.SyncConfigNodeIServiceClient;
24-
import org.apache.iotdb.commons.utils.TimePartitionUtils;
2524
import org.apache.iotdb.confignode.rpc.thrift.TDataPartitionReq;
2625
import org.apache.iotdb.confignode.rpc.thrift.TDataPartitionTableResp;
2726
import org.apache.iotdb.it.env.EnvFactory;
@@ -52,7 +51,11 @@ public class IoTDBPartitionTableAutoCleanIT {
5251
private static final long TEST_TTL_CHECK_INTERVAL = 5_000;
5352

5453
private static final TTimePartitionSlot TEST_CURRENT_TIME_SLOT =
55-
TimePartitionUtils.getCurrentTimePartitionSlot();
54+
new TTimePartitionSlot()
55+
.setStartTime(
56+
System.currentTimeMillis()
57+
/ TEST_TIME_PARTITION_INTERVAL
58+
* TEST_TIME_PARTITION_INTERVAL);
5659
private static final long TEST_TTL = 7 * TEST_TIME_PARTITION_INTERVAL;
5760

5861
@Before
Lines changed: 138 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
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+
}

iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/manager/TTLManager.java

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
import org.apache.iotdb.commons.conf.CommonDescriptor;
2323
import org.apache.iotdb.commons.exception.IllegalPathException;
2424
import org.apache.iotdb.commons.path.PartialPath;
25+
import org.apache.iotdb.commons.utils.CommonDateTimeUtils;
2526
import org.apache.iotdb.commons.utils.PathUtils;
2627
import org.apache.iotdb.confignode.consensus.request.read.ttl.ShowTTLPlan;
2728
import org.apache.iotdb.confignode.consensus.request.write.database.DatabaseSchemaPlan;
@@ -135,8 +136,12 @@ public int getTTLCount() {
135136
* @return the maximum ttl of the subtree of the corresponding database. return NULL_TTL if the
136137
* TTL is not set or the database does not exist.
137138
*/
138-
public long getDatabaseMaxTTL(String database) {
139-
return ttlInfo.getDatabaseMaxTTL(database);
139+
public long getDatabaseMaxTTL(final String database) {
140+
final long ttl = ttlInfo.getDatabaseMaxTTL(database);
141+
return ttl == Long.MAX_VALUE || ttl < 0
142+
? ttl
143+
: CommonDateTimeUtils.convertMilliTimeWithPrecision(
144+
ttl, CommonDescriptor.getInstance().getConfig().getTimestampPrecision());
140145
}
141146

142147
/** Only used for upgrading from old database-level ttl to device-level ttl. */

iotdb-core/confignode/src/main/java/org/apache/iotdb/confignode/procedure/PartitionTableAutoCleaner.java

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
import org.apache.iotdb.common.rpc.thrift.TTimePartitionSlot;
2323
import org.apache.iotdb.commons.conf.CommonConfig;
2424
import org.apache.iotdb.commons.conf.CommonDescriptor;
25-
import org.apache.iotdb.commons.utils.TimePartitionUtils;
2625
import org.apache.iotdb.confignode.consensus.request.write.partition.AutoCleanPartitionTablePlan;
2726
import org.apache.iotdb.confignode.manager.ConfigManager;
2827
import org.apache.iotdb.consensus.exception.ConsensusException;
@@ -42,6 +41,10 @@ public class PartitionTableAutoCleaner<Env> extends InternalProcedure<Env> {
4241
private static final Logger LOGGER = LoggerFactory.getLogger(PartitionTableAutoCleaner.class);
4342

4443
private static final CommonConfig COMMON_CONFIG = CommonDescriptor.getInstance().getConfig();
44+
45+
private static final String timestampPrecision =
46+
CommonDescriptor.getInstance().getConfig().getTimestampPrecision();
47+
4548
private final ConfigManager configManager;
4649

4750
public PartitionTableAutoCleaner(ConfigManager configManager) {
@@ -59,6 +62,12 @@ protected void periodicExecute(Env env) {
5962
for (String database : databases) {
6063
long databaseTTL = configManager.getTTLManager().getDatabaseMaxTTL(database);
6164
databaseTTLMap.put(database, databaseTTL);
65+
}
66+
LOGGER.info(
67+
"[PartitionTableCleaner] Periodically activate PartitionTableAutoCleaner, databaseTTL: {}",
68+
databaseTTLMap);
69+
for (String database : databases) {
70+
long databaseTTL = databaseTTLMap.get(database);
6271
if (!configManager.getPartitionManager().isDatabaseExist(database)
6372
|| databaseTTL < 0
6473
|| databaseTTL == Long.MAX_VALUE) {
@@ -71,8 +80,7 @@ protected void periodicExecute(Env env) {
7180
"[PartitionTableCleaner] Periodically activate PartitionTableAutoCleaner for: {}",
7281
databaseTTLMap);
7382
// Only clean the partition table when necessary
74-
TTimePartitionSlot currentTimePartitionSlot =
75-
TimePartitionUtils.getCurrentTimePartitionSlot();
83+
TTimePartitionSlot currentTimePartitionSlot = getCurrentTimePartitionSlot();
7684
try {
7785
configManager
7886
.getConsensusManager()
@@ -82,4 +90,19 @@ protected void periodicExecute(Env env) {
8290
}
8391
}
8492
}
93+
94+
/**
95+
* @return The time partition slot corresponding to current timestamp. Note that we do not shift
96+
* the start time to the correct starting point, since this interface only constructs a time
97+
* reference position for the partition table cleaner.
98+
*/
99+
private static TTimePartitionSlot getCurrentTimePartitionSlot() {
100+
if ("ms".equals(timestampPrecision)) {
101+
return new TTimePartitionSlot(System.currentTimeMillis());
102+
} else if ("us".equals(timestampPrecision)) {
103+
return new TTimePartitionSlot(System.currentTimeMillis() * 1000);
104+
} else {
105+
return new TTimePartitionSlot(System.currentTimeMillis() * 1000_000);
106+
}
107+
}
85108
}

iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/utils/TimePartitionUtils.java

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,6 @@ public class TimePartitionUtils {
3434
private static long timePartitionOrigin =
3535
CommonDescriptor.getInstance().getConfig().getTimePartitionOrigin();
3636

37-
private static String timestampPrecision =
38-
CommonDescriptor.getInstance().getConfig().getTimestampPrecision();
39-
4037
/** Time range for dividing database, the time unit is the same with IoTDB's TimestampPrecision */
4138
private static long timePartitionInterval =
4239
CommonDescriptor.getInstance().getConfig().getTimePartitionInterval();
@@ -74,16 +71,6 @@ public class TimePartitionUtils {
7471
}
7572
}
7673

77-
public static TTimePartitionSlot getCurrentTimePartitionSlot() {
78-
if ("ms".equals(timestampPrecision)) {
79-
return getTimePartitionSlot(System.currentTimeMillis());
80-
} else if ("us".equals(timestampPrecision)) {
81-
return getTimePartitionSlot(System.currentTimeMillis() * 1000);
82-
} else {
83-
return getTimePartitionSlot(System.currentTimeMillis() * 1000_000);
84-
}
85-
}
86-
8774
public static TTimePartitionSlot getTimePartitionSlot(long time) {
8875
TTimePartitionSlot timePartitionSlot = new TTimePartitionSlot();
8976
timePartitionSlot.setStartTime(getTimePartitionLowerBound(time));

0 commit comments

Comments
 (0)