Skip to content

Commit 1d139bd

Browse files
Get correct TTL in tree model (apache#15256)
* Finish * remove debug log * Update IoTDBPartitionTableAutoCleanIT.java * Update IoTDBPartitionTableAutoCleanIT.java * Update IoTDBPartitionTableAutoCleanIT.java --------- Co-authored-by: Potato <[email protected]>
1 parent ef8dc52 commit 1d139bd

File tree

2 files changed

+95
-30
lines changed

2 files changed

+95
-30
lines changed

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

Lines changed: 73 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import org.apache.iotdb.it.env.EnvFactory;
2828
import org.apache.iotdb.it.framework.IoTDBTestRunner;
2929
import org.apache.iotdb.itbase.category.ClusterIT;
30+
import org.apache.iotdb.itbase.env.BaseEnv;
3031
import org.apache.iotdb.rpc.TSStatusCode;
3132

3233
import org.junit.After;
@@ -45,6 +46,9 @@
4546
@Category({ClusterIT.class})
4647
public class IoTDBPartitionTableAutoCleanIT {
4748

49+
private static final String TREE_DATABASE_PREFIX = "root.db.g_";
50+
private static final String TABLE_DATABASE_PREFIX = "database_";
51+
4852
private static final int TEST_REPLICATION_FACTOR = 1;
4953
private static final long TEST_TIME_PARTITION_INTERVAL = 604800000;
5054
private static final long TEST_TTL_CHECK_INTERVAL = 5_000;
@@ -73,46 +77,86 @@ public void tearDown() {
7377
}
7478

7579
@Test
76-
public void testAutoCleanPartitionTable() throws Exception {
77-
try (Connection connection = EnvFactory.getEnv().getConnection();
80+
public void testAutoCleanPartitionTableForTreeModel() throws Exception {
81+
try (Connection connection = EnvFactory.getEnv().getConnection(BaseEnv.TREE_SQL_DIALECT);
7882
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");
83+
// Create databases and insert test data
84+
for (int i = 0; i < 3; i++) {
85+
String databaseName = String.format("%s%d", TREE_DATABASE_PREFIX, i);
86+
statement.execute(String.format("CREATE DATABASE %s", databaseName));
87+
statement.execute(
88+
String.format(
89+
"CREATE TIMESERIES %s.s WITH DATATYPE=INT64,ENCODING=PLAIN", databaseName));
90+
// Insert expired data
91+
statement.execute(
92+
String.format(
93+
"INSERT INTO %s(timestamp, s) VALUES (%d, %d)",
94+
databaseName, TEST_CURRENT_TIME_SLOT.getStartTime() - TEST_TTL * 2, -1));
95+
// Insert existed data
96+
statement.execute(
97+
String.format(
98+
"INSERT INTO %s(timestamp, s) VALUES (%d, %d)",
99+
databaseName, TEST_CURRENT_TIME_SLOT.getStartTime(), 1));
100+
}
101+
// Let db0.TTL > device.TTL, the valid TTL should be the bigger one
102+
statement.execute(String.format("SET TTL TO %s0 %d", TREE_DATABASE_PREFIX, TEST_TTL));
103+
statement.execute(String.format("SET TTL TO %s0.s %d", TREE_DATABASE_PREFIX, 10));
104+
// Let db1.TTL < device.TTL, the valid TTL should be the bigger one
105+
statement.execute(String.format("SET TTL TO %s1 %d", TREE_DATABASE_PREFIX, 10));
106+
statement.execute(String.format("SET TTL TO %s1.s %d", TREE_DATABASE_PREFIX, TEST_TTL));
107+
// Set TTL to path db2.**
108+
statement.execute(String.format("SET TTL TO %s2.** %d", TREE_DATABASE_PREFIX, TEST_TTL));
109+
}
110+
111+
TDataPartitionReq req = new TDataPartitionReq();
112+
for (int i = 0; i < 3; i++) {
113+
req.putToPartitionSlotsMap(String.format("%s%d", TREE_DATABASE_PREFIX, i), new TreeMap<>());
114+
}
115+
try (SyncConfigNodeIServiceClient client =
116+
(SyncConfigNodeIServiceClient) EnvFactory.getEnv().getLeaderConfigNodeConnection()) {
117+
for (int retry = 0; retry < 120; retry++) {
118+
boolean partitionTableAutoCleaned = true;
119+
TDataPartitionTableResp resp = client.getDataPartitionTable(req);
120+
if (TSStatusCode.SUCCESS_STATUS.getStatusCode() == resp.getStatus().getCode()) {
121+
partitionTableAutoCleaned =
122+
resp.getDataPartitionTable().entrySet().stream()
123+
.flatMap(e1 -> e1.getValue().entrySet().stream())
124+
.allMatch(e2 -> e2.getValue().size() == 1);
125+
}
126+
if (partitionTableAutoCleaned) {
127+
return;
128+
}
129+
TimeUnit.SECONDS.sleep(1);
130+
}
131+
}
132+
Assert.fail("The PartitionTable in the ConfigNode is not auto cleaned!");
133+
}
134+
135+
@Test
136+
public void testAutoCleanPartitionTableForTableModel() throws Exception {
137+
try (final Connection connection =
138+
EnvFactory.getEnv().getConnection(BaseEnv.TABLE_SQL_DIALECT);
139+
final Statement statement = connection.createStatement()) {
140+
// Create databases and insert test data
141+
String databaseName = TABLE_DATABASE_PREFIX;
142+
statement.execute(String.format("CREATE DATABASE IF NOT EXISTS %s", databaseName));
143+
statement.execute(String.format("USE %s", databaseName));
144+
statement.execute("CREATE TABLE tb (time TIMESTAMP TIME, s int64 FIELD)");
98145
// Insert expired data
99146
statement.execute(
100147
String.format(
101-
"INSERT INTO root.db2(timestamp, s) VALUES (%d, %d)",
148+
"INSERT INTO tb(time, s) VALUES (%d, %d)",
102149
TEST_CURRENT_TIME_SLOT.getStartTime() - TEST_TTL * 2, -1));
103150
// Insert existed data
104151
statement.execute(
105152
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);
153+
"INSERT INTO tb(time, s) VALUES (%d, %d)", TEST_CURRENT_TIME_SLOT.getStartTime(), 1));
154+
statement.execute(String.format("USE %s", TABLE_DATABASE_PREFIX));
155+
statement.execute(String.format("ALTER TABLE tb SET PROPERTIES TTL=%d", TEST_TTL));
111156
}
112157

113158
TDataPartitionReq req = new TDataPartitionReq();
114-
req.putToPartitionSlotsMap("root.db1", new TreeMap<>());
115-
req.putToPartitionSlotsMap("root.db2", new TreeMap<>());
159+
req.putToPartitionSlotsMap(TABLE_DATABASE_PREFIX, new TreeMap<>());
116160
try (SyncConfigNodeIServiceClient client =
117161
(SyncConfigNodeIServiceClient) EnvFactory.getEnv().getLeaderConfigNodeConnection()) {
118162
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
@@ -182,7 +182,7 @@ public long getLastNodeTTL(String[] nodes) {
182182
* TTL is not set or the database does not exist.
183183
*/
184184
public long getDatabaseMaxTTL(String database) {
185-
CacheNode node = ttlCacheTree.getChild(database);
185+
CacheNode node = ttlCacheTree.searchChild(database);
186186
if (node == null) {
187187
return NULL_TTL;
188188
}
@@ -300,6 +300,27 @@ public CacheNode getChild(String name) {
300300
return children.get(name);
301301
}
302302

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

0 commit comments

Comments
 (0)