-
Notifications
You must be signed in to change notification settings - Fork 968
/
TTLTest.java
308 lines (269 loc) · 12 KB
/
TTLTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
*/
package org.apache.iotdb.db.engine.storagegroup;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import org.apache.iotdb.db.conf.IoTDBConstant;
import org.apache.iotdb.db.conf.IoTDBDescriptor;
import org.apache.iotdb.db.conf.directories.DirectoryManager;
import org.apache.iotdb.db.engine.flush.TsFileFlushPolicy.DirectFlushPolicy;
import org.apache.iotdb.db.engine.querycontext.QueryDataSource;
import org.apache.iotdb.db.exception.metadata.MetadataException;
import org.apache.iotdb.db.exception.StartupException;
import org.apache.iotdb.db.exception.StorageEngineException;
import org.apache.iotdb.db.exception.path.PathException;
import org.apache.iotdb.db.exception.query.OutOfTTLException;
import org.apache.iotdb.db.exception.query.QueryProcessException;
import org.apache.iotdb.db.exception.storageGroup.StorageGroupException;
import org.apache.iotdb.db.exception.storageGroup.StorageGroupProcessorException;
import org.apache.iotdb.db.metadata.MManager;
import org.apache.iotdb.db.metadata.MNode;
import org.apache.iotdb.db.qp.QueryProcessor;
import org.apache.iotdb.db.qp.executor.QueryProcessExecutor;
import org.apache.iotdb.db.qp.physical.crud.InsertPlan;
import org.apache.iotdb.db.qp.physical.sys.SetTTLPlan;
import org.apache.iotdb.db.qp.physical.sys.ShowTTLPlan;
import org.apache.iotdb.db.query.control.QueryResourceManager;
import org.apache.iotdb.db.query.reader.resourceRelated.SeqResourceIterateReader;
import org.apache.iotdb.db.utils.EnvironmentUtils;
import org.apache.iotdb.tsfile.common.constant.TsFileConstant;
import org.apache.iotdb.tsfile.exception.filter.QueryFilterOptimizationException;
import org.apache.iotdb.tsfile.file.metadata.enums.CompressionType;
import org.apache.iotdb.tsfile.file.metadata.enums.TSDataType;
import org.apache.iotdb.tsfile.file.metadata.enums.TSEncoding;
import org.apache.iotdb.tsfile.read.common.BatchData;
import org.apache.iotdb.tsfile.read.common.Path;
import org.apache.iotdb.tsfile.read.common.RowRecord;
import org.apache.iotdb.tsfile.read.query.dataset.QueryDataSet;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
public class TTLTest {
private String sg1 = "root.TTL_SG1";
private String sg2 = "root.TTL_SG2";
private long ttl = 12345;
private StorageGroupProcessor storageGroupProcessor;
private String s1 = "s1";
private String g1s1 = sg1 + IoTDBConstant.PATH_SEPARATOR + s1;
@Before
public void setUp()
throws MetadataException, IOException, StartupException, PathException, StorageGroupProcessorException {
EnvironmentUtils.envSetUp();
createSchemas();
}
@After
public void tearDown() throws IOException, StorageEngineException {
storageGroupProcessor.waitForAllCurrentTsFileProcessorsClosed();
EnvironmentUtils.cleanEnv();
}
private void createSchemas()
throws MetadataException, PathException, StorageGroupProcessorException {
MManager.getInstance().setStorageGroupToMTree(sg1);
MManager.getInstance().setStorageGroupToMTree(sg2);
storageGroupProcessor = new StorageGroupProcessor(IoTDBDescriptor.getInstance().getConfig()
.getSystemDir(), sg1, new DirectFlushPolicy());
MManager.getInstance().addPathToMTree(g1s1, TSDataType.INT64, TSEncoding.PLAIN,
CompressionType.UNCOMPRESSED, Collections.emptyMap());
storageGroupProcessor.addMeasurement("s1", TSDataType.INT64, TSEncoding.PLAIN,
CompressionType.UNCOMPRESSED, Collections.emptyMap());
}
@Test
public void testSetMetaTTL() throws IOException, PathException, StorageGroupException {
// exception is expected when setting ttl to a non-exist storage group
boolean caught = false;
try {
MManager.getInstance().setTTL(sg1 + ".notExist", ttl);
} catch (PathException e) {
caught = true;
}
assertTrue(caught);
// normally set ttl
MManager.getInstance().setTTL(sg1, ttl);
MNode mNode = MManager.getInstance().getNodeByPathWithCheck(sg1);
assertEquals(ttl, mNode.getDataTTL());
// default ttl
mNode = MManager.getInstance().getNodeByPathWithCheck(sg2);
assertEquals(Long.MAX_VALUE, mNode.getDataTTL());
}
@Test
public void testTTLWrite() throws QueryProcessException {
InsertPlan insertPlan = new InsertPlan();
insertPlan.setDeviceId(sg1);
insertPlan.setTime(System.currentTimeMillis());
insertPlan.setMeasurements(new String[]{"s1"});
insertPlan.setValues(new String[]{"1"});
insertPlan.setDataTypes(new TSDataType[]{TSDataType.INT64});
// ok without ttl
storageGroupProcessor.insert(insertPlan);
storageGroupProcessor.setDataTTL(1000);
// with ttl
insertPlan.setTime(System.currentTimeMillis() - 1001);
boolean caught = false;
try {
storageGroupProcessor.insert(insertPlan);
} catch (OutOfTTLException e) {
caught = true;
}
assertTrue(caught);
insertPlan.setTime(System.currentTimeMillis() - 900);
storageGroupProcessor.insert(insertPlan);
}
private void prepareData() throws QueryProcessException {
InsertPlan insertPlan = new InsertPlan();
insertPlan.setDeviceId(sg1);
insertPlan.setTime(System.currentTimeMillis());
insertPlan.setMeasurements(new String[]{"s1"});
insertPlan.setValues(new String[]{"1"});
insertPlan.setDataTypes(new TSDataType[]{TSDataType.INT64});
long initTime = System.currentTimeMillis();
// sequence data
for (int i = 1000; i < 2000; i++) {
insertPlan.setTime(initTime - 2000 + i);
storageGroupProcessor.insert(insertPlan);
if ((i + 1) % 300 == 0) {
storageGroupProcessor.putAllWorkingTsFileProcessorIntoClosingList();
}
}
// unsequence data
for (int i = 0; i < 1000; i++) {
insertPlan.setTime(initTime - 2000 + i);
storageGroupProcessor.insert(insertPlan);
if ((i + 1) % 300 == 0) {
storageGroupProcessor.putAllWorkingTsFileProcessorIntoClosingList();
}
}
}
@Test
public void testTTLRead() throws IOException, QueryProcessException, StorageEngineException {
prepareData();
// files before ttl
QueryDataSource dataSource = storageGroupProcessor
.query(sg1, s1, EnvironmentUtils.TEST_QUERY_CONTEXT
, null);
List<TsFileResource> seqResource = dataSource.getSeqResources();
List<TsFileResource> unseqResource = dataSource.getUnseqResources();
assertEquals(4, seqResource.size());
assertEquals(4, unseqResource.size());
storageGroupProcessor.setDataTTL(500);
// files after ttl
dataSource = storageGroupProcessor.query(sg1, s1, EnvironmentUtils.TEST_QUERY_CONTEXT
, null);
seqResource = dataSource.getSeqResources();
unseqResource = dataSource.getUnseqResources();
assertTrue(seqResource.size() < 4);
assertEquals(0, unseqResource.size());
Path path = new Path(sg1, s1);
SeqResourceIterateReader reader = new SeqResourceIterateReader(path,
seqResource, null, EnvironmentUtils.TEST_QUERY_CONTEXT);
int cnt = 0;
while (reader.hasNextBatch()) {
BatchData batchData = reader.nextBatch();
while (batchData.hasCurrent()) {
batchData.next();
cnt++;
}
}
reader.close();
// we cannot offer the exact number since when exactly ttl will be checked is unknown
assertTrue(cnt <= 1000);
storageGroupProcessor.setDataTTL(0);
dataSource = storageGroupProcessor.query(sg1, s1, EnvironmentUtils.TEST_QUERY_CONTEXT
, null);
seqResource = dataSource.getSeqResources();
unseqResource = dataSource.getUnseqResources();
assertEquals(0, seqResource.size());
assertEquals(0, unseqResource.size());
QueryResourceManager.getInstance().endQuery(EnvironmentUtils.TEST_QUERY_JOB_ID);
}
@Test
public void testTTLRemoval() throws StorageEngineException, QueryProcessException {
prepareData();
storageGroupProcessor.waitForAllCurrentTsFileProcessorsClosed();
// files before ttl
File seqDir = new File(DirectoryManager.getInstance().getNextFolderForSequenceFile(), sg1);
File unseqDir = new File(DirectoryManager.getInstance().getNextFolderForUnSequenceFile(), sg1);
File[] seqFiles = seqDir.listFiles(f -> f.getName().endsWith(TsFileConstant.TSFILE_SUFFIX));
File[] unseqFiles = unseqDir.listFiles(f -> f.getName().endsWith(TsFileConstant.TSFILE_SUFFIX));
assertEquals(4, seqFiles.length);
assertEquals(4, unseqFiles.length);
storageGroupProcessor.setDataTTL(500);
storageGroupProcessor.checkFilesTTL();
// files after ttl
seqFiles = seqDir.listFiles(f -> f.getName().endsWith(TsFileConstant.TSFILE_SUFFIX));
unseqFiles = unseqDir.listFiles(f -> f.getName().endsWith(TsFileConstant.TSFILE_SUFFIX));
assertTrue(seqFiles.length <= 2);
assertEquals(0, unseqFiles.length);
}
@Test
public void testParseSetTTL()
throws MetadataException, QueryProcessException {
QueryProcessor queryProcessor = new QueryProcessor(new QueryProcessExecutor());
SetTTLPlan plan = (SetTTLPlan) queryProcessor
.parseSQLToPhysicalPlan("SET TTL TO " + sg1 + " 10000");
assertEquals(sg1, plan.getStorageGroup());
assertEquals(10000, plan.getDataTTL());
plan = (SetTTLPlan) queryProcessor.parseSQLToPhysicalPlan("UNSET TTL TO " + sg2);
assertEquals(sg2, plan.getStorageGroup());
assertEquals(Long.MAX_VALUE, plan.getDataTTL());
}
@Test
public void testParseShowTTL()
throws MetadataException, QueryProcessException {
QueryProcessor queryProcessor = new QueryProcessor(new QueryProcessExecutor());
ShowTTLPlan plan = (ShowTTLPlan) queryProcessor.parseSQLToPhysicalPlan("SHOW ALL TTL");
assertTrue(plan.getStorageGroups().isEmpty());
List<String> sgs = new ArrayList<>();
sgs.add("root.sg1");
sgs.add("root.sg2");
sgs.add("root.sg3");
plan = (ShowTTLPlan) queryProcessor
.parseSQLToPhysicalPlan("SHOW TTL ON root.sg1,root.sg2,root.sg3");
assertEquals(sgs, plan.getStorageGroups());
}
@Test
public void testShowTTL()
throws IOException, QueryProcessException, QueryFilterOptimizationException, StorageEngineException {
MManager.getInstance().setTTL(sg1, ttl);
ShowTTLPlan plan = new ShowTTLPlan(Collections.emptyList());
QueryProcessExecutor executor = new QueryProcessExecutor();
QueryDataSet queryDataSet = executor.processQuery(plan, EnvironmentUtils.TEST_QUERY_CONTEXT);
RowRecord rowRecord = queryDataSet.next();
assertEquals(sg2, rowRecord.getFields().get(0).getStringValue());
assertEquals("null", rowRecord.getFields().get(1).getStringValue());
rowRecord = queryDataSet.next();
assertEquals(sg1, rowRecord.getFields().get(0).getStringValue());
assertEquals(ttl, rowRecord.getFields().get(1).getLongV());
}
@Test
public void testTTLCleanFile() throws QueryProcessException {
prepareData();
storageGroupProcessor.waitForAllCurrentTsFileProcessorsClosed();
assertEquals(4, storageGroupProcessor.getSequenceFileList().size());
assertEquals(4, storageGroupProcessor.getUnSequenceFileList().size());
storageGroupProcessor.setDataTTL(0);
assertEquals(0, storageGroupProcessor.getSequenceFileList().size());
assertEquals(0, storageGroupProcessor.getUnSequenceFileList().size());
}
}