|
| 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 | +} |
0 commit comments