Skip to content

Commit e593fcb

Browse files
authored
Implement table & tree disk usage statistics (#17169)
1 parent 67ddff9 commit e593fcb

97 files changed

Lines changed: 6353 additions & 181 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,163 @@
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.isession.ISession;
23+
import org.apache.iotdb.isession.SessionDataSet;
24+
import org.apache.iotdb.it.env.EnvFactory;
25+
import org.apache.iotdb.it.framework.IoTDBTestRunner;
26+
import org.apache.iotdb.itbase.category.ClusterIT;
27+
import org.apache.iotdb.itbase.category.LocalStandaloneIT;
28+
29+
import org.junit.AfterClass;
30+
import org.junit.Assert;
31+
import org.junit.BeforeClass;
32+
import org.junit.Test;
33+
import org.junit.experimental.categories.Category;
34+
import org.junit.runner.RunWith;
35+
36+
import java.util.HashMap;
37+
import java.util.Map;
38+
39+
import static org.junit.Assert.fail;
40+
41+
@RunWith(IoTDBTestRunner.class)
42+
@Category({LocalStandaloneIT.class, ClusterIT.class})
43+
public class IoTDBShowDiskUsageIT {
44+
45+
@BeforeClass
46+
public static void setUp() throws Exception {
47+
EnvFactory.getEnv().initClusterEnvironment();
48+
49+
try (ISession session = EnvFactory.getEnv().getSessionConnection()) {
50+
for (int i = 0; i < 20; i++) {
51+
session.executeNonQueryStatement(
52+
"insert into root.test.d" + i + "(time, s0, s1, s2) values (" + i + ",1, 1, 1)");
53+
}
54+
session.executeNonQueryStatement(
55+
"insert into root.test.d0(time,s0,s1) aligned values (-1,1,1)");
56+
session.executeNonQueryStatement("flush");
57+
}
58+
}
59+
60+
@AfterClass
61+
public static void tearDown() throws Exception {
62+
EnvFactory.getEnv().cleanClusterEnvironment();
63+
}
64+
65+
@Test
66+
public void test1() {
67+
68+
try (ISession session = EnvFactory.getEnv().getSessionConnection()) {
69+
SessionDataSet sessionDataSet =
70+
session.executeQueryStatement("show disk_usage from root.test.**");
71+
SessionDataSet.DataIterator iterator = sessionDataSet.iterator();
72+
long sum = 0;
73+
Map<Long, Long> timePartitionSizes = new HashMap<>();
74+
while (iterator.next()) {
75+
long timePartition = iterator.getLong("TimePartition");
76+
long size = iterator.getLong("SizeInBytes");
77+
timePartitionSizes.compute(timePartition, (k, v) -> v == null ? size : v + size);
78+
sum += size;
79+
}
80+
Assert.assertTrue(sum > 0);
81+
Assert.assertTrue(timePartitionSizes.containsKey(0L));
82+
Assert.assertTrue(timePartitionSizes.containsKey(-1L));
83+
} catch (Exception e) {
84+
e.printStackTrace();
85+
fail(e.getMessage());
86+
}
87+
}
88+
89+
@Test
90+
public void test2() {
91+
try (ISession session = EnvFactory.getEnv().getSessionConnection()) {
92+
SessionDataSet sessionDataSet =
93+
session.executeQueryStatement("show disk_usage from root.test.** limit 2");
94+
SessionDataSet.DataIterator iterator = sessionDataSet.iterator();
95+
int count = 0;
96+
while (iterator.next()) {
97+
count++;
98+
}
99+
Assert.assertEquals(2, count);
100+
} catch (Exception e) {
101+
e.printStackTrace();
102+
fail(e.getMessage());
103+
}
104+
}
105+
106+
@Test
107+
public void test3() {
108+
try (ISession session = EnvFactory.getEnv().getSessionConnection()) {
109+
SessionDataSet sessionDataSet =
110+
session.executeQueryStatement(
111+
"show disk_usage from root.test.** order by TimePartition desc");
112+
SessionDataSet.DataIterator iterator = sessionDataSet.iterator();
113+
long previousTimePartition = Long.MAX_VALUE;
114+
while (iterator.next()) {
115+
long currentTimePartition = iterator.getLong("TimePartition");
116+
Assert.assertTrue(currentTimePartition <= previousTimePartition);
117+
previousTimePartition = currentTimePartition;
118+
}
119+
} catch (Exception e) {
120+
e.printStackTrace();
121+
fail(e.getMessage());
122+
}
123+
}
124+
125+
@Test
126+
public void test4() {
127+
try (ISession session = EnvFactory.getEnv().getSessionConnection()) {
128+
SessionDataSet sessionDataSet =
129+
session.executeQueryStatement(
130+
"show disk_usage from root.test.** where TimePartition < 0 or DataNodeId >= 2");
131+
SessionDataSet.DataIterator iterator = sessionDataSet.iterator();
132+
while (iterator.next()) {
133+
Assert.assertTrue(
134+
iterator.getLong("TimePartition") < 0 || iterator.getLong("DataNodeId") >= 2);
135+
}
136+
} catch (Exception e) {
137+
e.printStackTrace();
138+
fail(e.getMessage());
139+
}
140+
}
141+
142+
@Test
143+
public void test5() {
144+
try (ISession session = EnvFactory.getEnv().getSessionConnection()) {
145+
SessionDataSet sessionDataSet =
146+
session.executeQueryStatement(
147+
"show disk_usage from root.test.** order by TimePartition desc limit 2");
148+
SessionDataSet.DataIterator iterator = sessionDataSet.iterator();
149+
long previousTimePartition = Long.MAX_VALUE;
150+
int count = 0;
151+
while (iterator.next()) {
152+
count++;
153+
long currentTimePartition = iterator.getLong("TimePartition");
154+
Assert.assertTrue(currentTimePartition <= previousTimePartition);
155+
previousTimePartition = currentTimePartition;
156+
}
157+
Assert.assertEquals(2, count);
158+
} catch (Exception e) {
159+
e.printStackTrace();
160+
fail(e.getMessage());
161+
}
162+
}
163+
}
Lines changed: 176 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,176 @@
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.relational.it;
21+
22+
import org.apache.iotdb.isession.ITableSession;
23+
import org.apache.iotdb.isession.SessionDataSet;
24+
import org.apache.iotdb.it.env.EnvFactory;
25+
import org.apache.iotdb.it.framework.IoTDBTestRunner;
26+
import org.apache.iotdb.itbase.category.TableClusterIT;
27+
import org.apache.iotdb.itbase.category.TableLocalStandaloneIT;
28+
29+
import org.junit.AfterClass;
30+
import org.junit.Assert;
31+
import org.junit.BeforeClass;
32+
import org.junit.Test;
33+
import org.junit.experimental.categories.Category;
34+
import org.junit.runner.RunWith;
35+
36+
import java.util.HashMap;
37+
import java.util.Map;
38+
39+
import static org.junit.Assert.fail;
40+
41+
@RunWith(IoTDBTestRunner.class)
42+
@Category({TableLocalStandaloneIT.class, TableClusterIT.class})
43+
public class IoTDBShowDiskUsageTableIT {
44+
45+
@BeforeClass
46+
public static void setUp() throws Exception {
47+
EnvFactory.getEnv().initClusterEnvironment();
48+
try (ITableSession session = EnvFactory.getEnv().getTableSessionConnection()) {
49+
session.executeNonQueryStatement("create database test");
50+
session.executeNonQueryStatement("use test");
51+
session.executeNonQueryStatement(
52+
"create table t1(tag1 string tag, s0 int32 field, s1 int32 field)");
53+
session.executeNonQueryStatement(
54+
"create table t2(tag1 string tag, s0 int32 field, s1 int32 field)");
55+
session.executeNonQueryStatement(
56+
"create view v1(tag1 string tag, s0 int32 field, s1 int32 field) as root.test.**");
57+
for (int i = 0; i < 20; i++) {
58+
session.executeNonQueryStatement(
59+
"insert into t1(time,tag1,s0,s1) values (" + i + ", 'd" + i + "', 1, 1)");
60+
}
61+
session.executeNonQueryStatement("insert into t1(time,tag1,s0,s1) values (-1,'d1',1,1)");
62+
session.executeNonQueryStatement("flush");
63+
}
64+
}
65+
66+
@AfterClass
67+
public static void tearDown() throws Exception {
68+
EnvFactory.getEnv().cleanClusterEnvironment();
69+
}
70+
71+
@Test
72+
public void test1() {
73+
try (ITableSession session = EnvFactory.getEnv().getTableSessionConnection()) {
74+
SessionDataSet sessionDataSet =
75+
session.executeQueryStatement(
76+
"select * from information_schema.table_disk_usage where database = 'test'");
77+
SessionDataSet.DataIterator iterator = sessionDataSet.iterator();
78+
long sum = 0;
79+
Map<Long, Long> timePartitionSizes = new HashMap<>();
80+
Map<String, Long> tableSizes = new HashMap<>();
81+
while (iterator.next()) {
82+
String table = iterator.getString("table_name");
83+
long timePartition = iterator.getLong("time_partition");
84+
long size = iterator.getLong("size_in_bytes");
85+
timePartitionSizes.compute(timePartition, (k, v) -> v == null ? size : v + size);
86+
tableSizes.compute(table, (k, v) -> v == null ? size : v + size);
87+
sum += size;
88+
}
89+
Assert.assertTrue(sum > 0);
90+
Assert.assertEquals(2, tableSizes.size());
91+
Assert.assertEquals(0L, (long) tableSizes.get("t2"));
92+
Assert.assertFalse(tableSizes.containsKey("v1"));
93+
Assert.assertTrue(timePartitionSizes.containsKey(0L));
94+
Assert.assertTrue(timePartitionSizes.containsKey(-1L));
95+
} catch (Exception e) {
96+
e.printStackTrace();
97+
fail(e.getMessage());
98+
}
99+
}
100+
101+
@Test
102+
public void test2() {
103+
try (ITableSession session = EnvFactory.getEnv().getTableSessionConnection()) {
104+
SessionDataSet sessionDataSet =
105+
session.executeQueryStatement(
106+
"select * from information_schema.table_disk_usage where database = 'test' limit 2");
107+
SessionDataSet.DataIterator iterator = sessionDataSet.iterator();
108+
int count = 0;
109+
while (iterator.next()) {
110+
count++;
111+
}
112+
Assert.assertEquals(2, count);
113+
} catch (Exception e) {
114+
e.printStackTrace();
115+
fail(e.getMessage());
116+
}
117+
}
118+
119+
@Test
120+
public void test3() {
121+
try (ITableSession session = EnvFactory.getEnv().getTableSessionConnection()) {
122+
SessionDataSet sessionDataSet =
123+
session.executeQueryStatement(
124+
"select * from information_schema.table_disk_usage where database = 'test' order by time_partition desc");
125+
SessionDataSet.DataIterator iterator = sessionDataSet.iterator();
126+
long previousTimePartition = Long.MAX_VALUE;
127+
while (iterator.next()) {
128+
long currentTimePartition = iterator.getLong("time_partition");
129+
Assert.assertTrue(currentTimePartition <= previousTimePartition);
130+
previousTimePartition = currentTimePartition;
131+
}
132+
} catch (Exception e) {
133+
e.printStackTrace();
134+
fail(e.getMessage());
135+
}
136+
}
137+
138+
@Test
139+
public void test4() {
140+
try (ITableSession session = EnvFactory.getEnv().getTableSessionConnection()) {
141+
SessionDataSet sessionDataSet =
142+
session.executeQueryStatement(
143+
"select * from information_schema.table_disk_usage where database = 'test' and (time_partition < 0 or datanode_id >= 2)");
144+
SessionDataSet.DataIterator iterator = sessionDataSet.iterator();
145+
while (iterator.next()) {
146+
Assert.assertTrue(
147+
iterator.getLong("time_partition") < 0 || iterator.getLong("datanode_id") >= 2);
148+
}
149+
} catch (Exception e) {
150+
e.printStackTrace();
151+
fail(e.getMessage());
152+
}
153+
}
154+
155+
@Test
156+
public void test5() {
157+
try (ITableSession session = EnvFactory.getEnv().getTableSessionConnection()) {
158+
SessionDataSet sessionDataSet =
159+
session.executeQueryStatement(
160+
"select * from information_schema.table_disk_usage where database = 'test' order by time_partition desc limit 2");
161+
SessionDataSet.DataIterator iterator = sessionDataSet.iterator();
162+
long previousTimePartition = Long.MAX_VALUE;
163+
int count = 0;
164+
while (iterator.next()) {
165+
count++;
166+
long currentTimePartition = iterator.getLong("time_partition");
167+
Assert.assertTrue(currentTimePartition <= previousTimePartition);
168+
previousTimePartition = currentTimePartition;
169+
}
170+
Assert.assertEquals(2, count);
171+
} catch (Exception e) {
172+
e.printStackTrace();
173+
fail(e.getMessage());
174+
}
175+
}
176+
}

‎integration-test/src/test/java/org/apache/iotdb/relational/it/schema/IoTDBDatabaseIT.java‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -414,6 +414,7 @@ public void testInformationSchema() throws SQLException {
414414
"regions,INF,",
415415
"services,INF,",
416416
"subscriptions,INF,",
417+
"table_disk_usage,INF,",
417418
"tables,INF,",
418419
"topics,INF,",
419420
"views,INF,"));
@@ -566,6 +567,17 @@ public void testInformationSchema() throws SQLException {
566567
"mpp_port,INT32,ATTRIBUTE,",
567568
"data_consensus_port,INT32,ATTRIBUTE,",
568569
"schema_consensus_port,INT32,ATTRIBUTE,")));
570+
TestUtils.assertResultSetEqual(
571+
statement.executeQuery("desc table_disk_usage"),
572+
"ColumnName,DataType,Category,",
573+
new HashSet<>(
574+
Arrays.asList(
575+
"database,STRING,FIELD,",
576+
"table_name,STRING,FIELD,",
577+
"datanode_id,INT32,FIELD,",
578+
"region_id,INT32,FIELD,",
579+
"time_partition,INT64,FIELD,",
580+
"size_in_bytes,INT64,FIELD,")));
569581

570582
// Only root user is allowed
571583
Assert.assertThrows(SQLException.class, () -> statement.execute("select * from regions"));
@@ -580,6 +592,8 @@ public void testInformationSchema() throws SQLException {
580592
Assert.assertThrows(SQLException.class, () -> statement.execute("select * from data_nodes"));
581593
Assert.assertThrows(
582594
SQLException.class, () -> statement.executeQuery("select * from pipe_plugins"));
595+
Assert.assertThrows(
596+
SQLException.class, () -> statement.executeQuery("select * from table_disk_usage"));
583597

584598
// Filter out not self-created pipes
585599
TestUtils.assertResultSetEqual(
@@ -645,6 +659,7 @@ public void testInformationSchema() throws SQLException {
645659
"information_schema,configurations,INF,USING,null,SYSTEM VIEW,",
646660
"information_schema,keywords,INF,USING,null,SYSTEM VIEW,",
647661
"information_schema,nodes,INF,USING,null,SYSTEM VIEW,",
662+
"information_schema,table_disk_usage,INF,USING,null,SYSTEM VIEW,",
648663
"information_schema,config_nodes,INF,USING,null,SYSTEM VIEW,",
649664
"information_schema,data_nodes,INF,USING,null,SYSTEM VIEW,",
650665
"information_schema,connections,INF,USING,null,SYSTEM VIEW,",
@@ -655,7 +670,7 @@ public void testInformationSchema() throws SQLException {
655670
TestUtils.assertResultSetEqual(
656671
statement.executeQuery("count devices from tables where status = 'USING'"),
657672
"count(devices),",
658-
Collections.singleton("22,"));
673+
Collections.singleton("23,"));
659674
TestUtils.assertResultSetEqual(
660675
statement.executeQuery(
661676
"select * from columns where table_name = 'queries' or database = 'test'"),

0 commit comments

Comments
 (0)