Skip to content

Commit 5ae3728

Browse files
CRZbulabulaOneSizeFitsQuorumchenrongzhao.yzd
authored
[To/dev 1.3]Get correct TTL in tree model (#15256) (#15298)
* Get correct TTL in tree model (#15256) * Finish * remove debug log * Update IoTDBPartitionTableAutoCleanIT.java * Update IoTDBPartitionTableAutoCleanIT.java * Update IoTDBPartitionTableAutoCleanIT.java --------- Co-authored-by: Potato <[email protected]> * Update IoTDBPartitionTableAutoCleanIT.java * spotless --------- Co-authored-by: Potato <[email protected]> Co-authored-by: chenrongzhao.yzd <[email protected]>
1 parent b4161dd commit 5ae3728

File tree

2 files changed

+54
-36
lines changed

2 files changed

+54
-36
lines changed

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

Lines changed: 32 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@
4545
@Category({ClusterIT.class})
4646
public class IoTDBPartitionTableAutoCleanIT {
4747

48+
private static final String TREE_DATABASE_PREFIX = "root.db.g_";
49+
4850
private static final int TEST_REPLICATION_FACTOR = 1;
4951
private static final long TEST_TIME_PARTITION_INTERVAL = 604800000;
5052
private static final long TEST_TTL_CHECK_INTERVAL = 5_000;
@@ -73,46 +75,41 @@ public void tearDown() {
7375
}
7476

7577
@Test
76-
public void testAutoCleanPartitionTable() throws Exception {
78+
public void testAutoCleanPartitionTableForTreeModel() throws Exception {
7779
try (Connection connection = EnvFactory.getEnv().getConnection();
7880
Statement statement = connection.createStatement()) {
79-
// Create db1
80-
statement.execute("CREATE DATABASE root.db1");
81-
statement.execute("CREATE TIMESERIES root.db1.s WITH DATATYPE=INT64,ENCODING=PLAIN");
82-
// Insert expired data
83-
statement.execute(
84-
String.format(
85-
"INSERT INTO root.db1(timestamp, s) VALUES (%d, %d)",
86-
TEST_CURRENT_TIME_SLOT.getStartTime() - TEST_TTL * 2, -1));
87-
// Insert existed data
88-
statement.execute(
89-
String.format(
90-
"INSERT INTO root.db1(timestamp, s) VALUES (%d, %d)",
91-
TEST_CURRENT_TIME_SLOT.getStartTime(), 1));
92-
// Let db.TTL > device.TTL, the valid TTL should be the bigger one
93-
statement.execute("SET TTL TO root.db1 " + TEST_TTL);
94-
statement.execute("SET TTL TO root.db1.s " + 10);
95-
// Create db2
96-
statement.execute("CREATE DATABASE root.db2");
97-
statement.execute("CREATE TIMESERIES root.db2.s WITH DATATYPE=INT64,ENCODING=PLAIN");
98-
// Insert expired data
99-
statement.execute(
100-
String.format(
101-
"INSERT INTO root.db2(timestamp, s) VALUES (%d, %d)",
102-
TEST_CURRENT_TIME_SLOT.getStartTime() - TEST_TTL * 2, -1));
103-
// Insert existed data
104-
statement.execute(
105-
String.format(
106-
"INSERT INTO root.db2(timestamp, s) VALUES (%d, %d)",
107-
TEST_CURRENT_TIME_SLOT.getStartTime(), 1));
108-
// Let db.TTL < device.TTL, the valid TTL should be the bigger one
109-
statement.execute("SET TTL TO root.db2 " + 10);
110-
statement.execute("SET TTL TO root.db2.s " + TEST_TTL);
81+
// Create databases and insert test data
82+
for (int i = 0; i < 3; i++) {
83+
String databaseName = String.format("%s%d", TREE_DATABASE_PREFIX, i);
84+
statement.execute(String.format("CREATE DATABASE %s", databaseName));
85+
statement.execute(
86+
String.format(
87+
"CREATE TIMESERIES %s.s WITH DATATYPE=INT64,ENCODING=PLAIN", databaseName));
88+
// Insert expired data
89+
statement.execute(
90+
String.format(
91+
"INSERT INTO %s(timestamp, s) VALUES (%d, %d)",
92+
databaseName, TEST_CURRENT_TIME_SLOT.getStartTime() - TEST_TTL * 2, -1));
93+
// Insert existed data
94+
statement.execute(
95+
String.format(
96+
"INSERT INTO %s(timestamp, s) VALUES (%d, %d)",
97+
databaseName, TEST_CURRENT_TIME_SLOT.getStartTime(), 1));
98+
}
99+
// Let db0.TTL > device.TTL, the valid TTL should be the bigger one
100+
statement.execute(String.format("SET TTL TO %s0 %d", TREE_DATABASE_PREFIX, TEST_TTL));
101+
statement.execute(String.format("SET TTL TO %s0.s %d", TREE_DATABASE_PREFIX, 10));
102+
// Let db1.TTL < device.TTL, the valid TTL should be the bigger one
103+
statement.execute(String.format("SET TTL TO %s1 %d", TREE_DATABASE_PREFIX, 10));
104+
statement.execute(String.format("SET TTL TO %s1.s %d", TREE_DATABASE_PREFIX, TEST_TTL));
105+
// Set TTL to path db2.**
106+
statement.execute(String.format("SET TTL TO %s2.** %d", TREE_DATABASE_PREFIX, TEST_TTL));
111107
}
112108

113109
TDataPartitionReq req = new TDataPartitionReq();
114-
req.putToPartitionSlotsMap("root.db1", new TreeMap<>());
115-
req.putToPartitionSlotsMap("root.db2", new TreeMap<>());
110+
for (int i = 0; i < 3; i++) {
111+
req.putToPartitionSlotsMap(String.format("%s%d", TREE_DATABASE_PREFIX, i), new TreeMap<>());
112+
}
116113
try (SyncConfigNodeIServiceClient client =
117114
(SyncConfigNodeIServiceClient) EnvFactory.getEnv().getLeaderConfigNodeConnection()) {
118115
for (int retry = 0; retry < 120; retry++) {

iotdb-core/node-commons/src/main/java/org/apache/iotdb/commons/schema/ttl/TTLCache.java

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -178,7 +178,7 @@ public long getLastNodeTTL(String[] nodes) {
178178
* TTL is not set or the database does not exist.
179179
*/
180180
public long getDatabaseMaxTTL(String database) {
181-
CacheNode node = ttlCacheTree.getChild(database);
181+
CacheNode node = ttlCacheTree.searchChild(database);
182182
if (node == null) {
183183
return NULL_TTL;
184184
}
@@ -296,6 +296,27 @@ public CacheNode getChild(String name) {
296296
return children.get(name);
297297
}
298298

299+
/**
300+
* Search the child node by name.
301+
*
302+
* @param name the name corresponding to the child node, use '.' to separate each node
303+
* @return the child node if it exists, otherwise return null
304+
*/
305+
public CacheNode searchChild(String name) {
306+
String[] nodeNames = name.split("\\.");
307+
CacheNode current = this;
308+
for (String nodeName : nodeNames) {
309+
if (nodeName.equals("root")) {
310+
continue;
311+
}
312+
current = current.getChild(nodeName);
313+
if (current == null) {
314+
return null;
315+
}
316+
}
317+
return current;
318+
}
319+
299320
public Map<String, CacheNode> getChildren() {
300321
return children;
301322
}

0 commit comments

Comments
 (0)