|
| 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.storageengine.load; |
| 21 | + |
| 22 | +import org.apache.iotdb.commons.pipe.datastructure.pattern.TablePattern; |
| 23 | +import org.apache.iotdb.commons.schema.table.TsFileTableSchemaUtil; |
| 24 | +import org.apache.iotdb.commons.schema.table.TsTable; |
| 25 | +import org.apache.iotdb.commons.schema.table.column.FieldColumnSchema; |
| 26 | +import org.apache.iotdb.commons.schema.table.column.TagColumnSchema; |
| 27 | +import org.apache.iotdb.db.conf.IoTDBDescriptor; |
| 28 | +import org.apache.iotdb.db.pipe.event.common.tablet.PipeRawTabletInsertionEvent; |
| 29 | +import org.apache.iotdb.db.pipe.event.common.tsfile.parser.table.TsFileInsertionEventTableParser; |
| 30 | +import org.apache.iotdb.db.queryengine.plan.scheduler.load.ChunkOffsetCalculator; |
| 31 | +import org.apache.iotdb.db.schemaengine.table.DataNodeTableCache; |
| 32 | +import org.apache.iotdb.db.storageengine.dataregion.DataRegion; |
| 33 | +import org.apache.iotdb.db.storageengine.load.splitter.ChunkData; |
| 34 | +import org.apache.iotdb.db.storageengine.load.splitter.TsFileData; |
| 35 | +import org.apache.iotdb.db.storageengine.load.splitter.TsFileSplitter; |
| 36 | +import org.apache.iotdb.pipe.api.event.dml.insertion.TabletInsertionEvent; |
| 37 | + |
| 38 | +import org.apache.tsfile.enums.ColumnCategory; |
| 39 | +import org.apache.tsfile.enums.TSDataType; |
| 40 | +import org.apache.tsfile.file.metadata.TableSchema; |
| 41 | +import org.apache.tsfile.file.metadata.enums.CompressionType; |
| 42 | +import org.apache.tsfile.file.metadata.enums.TSEncoding; |
| 43 | +import org.apache.tsfile.read.TsFileSequenceReader; |
| 44 | +import org.apache.tsfile.write.TsFileWriter; |
| 45 | +import org.apache.tsfile.write.record.Tablet; |
| 46 | +import org.junit.After; |
| 47 | +import org.junit.Before; |
| 48 | +import org.junit.Rule; |
| 49 | +import org.junit.Test; |
| 50 | +import org.junit.rules.TemporaryFolder; |
| 51 | +import org.mockito.Mockito; |
| 52 | + |
| 53 | +import java.io.File; |
| 54 | +import java.util.ArrayList; |
| 55 | +import java.util.Arrays; |
| 56 | +import java.util.Collections; |
| 57 | +import java.util.List; |
| 58 | +import java.util.Optional; |
| 59 | + |
| 60 | +import static org.junit.Assert.assertEquals; |
| 61 | + |
| 62 | +public class LoadTsFileTableSchemaTest { |
| 63 | + |
| 64 | + private static final String DATABASE = "load_table_schema_test"; |
| 65 | + private static final String TABLE = "measurements"; |
| 66 | + private static final int ROW_COUNT = 20; |
| 67 | + |
| 68 | + @Rule public final TemporaryFolder temporaryFolder = new TemporaryFolder(); |
| 69 | + |
| 70 | + private String[] originalLoadDirs; |
| 71 | + private DataRegion dataRegion; |
| 72 | + private TsTable table; |
| 73 | + |
| 74 | + @Before |
| 75 | + public void setUp() throws Exception { |
| 76 | + originalLoadDirs = IoTDBDescriptor.getInstance().getConfig().getLoadTsFileDirs(); |
| 77 | + IoTDBDescriptor.getInstance() |
| 78 | + .getConfig() |
| 79 | + .setLoadTsFileDirs(new String[] {temporaryFolder.getRoot().getAbsolutePath()}); |
| 80 | + dataRegion = Mockito.mock(DataRegion.class); |
| 81 | + Mockito.when(dataRegion.getDatabaseName()).thenReturn(DATABASE); |
| 82 | + Mockito.when(dataRegion.getDataRegionIdString()).thenReturn("0"); |
| 83 | + Mockito.when(dataRegion.getNonSystemDatabaseName()).thenReturn(Optional.of(DATABASE)); |
| 84 | + |
| 85 | + table = new TsTable(TABLE); |
| 86 | + table.addColumnSchema(new TagColumnSchema("device", TSDataType.STRING)); |
| 87 | + table.addColumnSchema( |
| 88 | + new FieldColumnSchema("value", TSDataType.INT32, TSEncoding.PLAIN, CompressionType.LZ4)); |
| 89 | + DataNodeTableCache.getInstance().preUpdateTable(DATABASE, table, null); |
| 90 | + DataNodeTableCache.getInstance().commitUpdateTable(DATABASE, TABLE, null); |
| 91 | + } |
| 92 | + |
| 93 | + @After |
| 94 | + public void tearDown() { |
| 95 | + DataNodeTableCache.getInstance().invalid(DATABASE); |
| 96 | + IoTDBDescriptor.getInstance().getConfig().setLoadTsFileDirs(originalLoadDirs); |
| 97 | + } |
| 98 | + |
| 99 | + @Test |
| 100 | + public void testLoadedTableRemainsReadableByPipe() throws Exception { |
| 101 | + assertLoadedTableReadable(false, false); |
| 102 | + } |
| 103 | + |
| 104 | + @Test |
| 105 | + public void testRecoveredTableRemainsReadableByPipe() throws Exception { |
| 106 | + assertLoadedTableReadable(true, false); |
| 107 | + } |
| 108 | + |
| 109 | + @Test |
| 110 | + public void testLoadedTableWithNullTagsRemainsReadableByPipe() throws Exception { |
| 111 | + assertLoadedTableReadable(false, true); |
| 112 | + } |
| 113 | + |
| 114 | + private void assertLoadedTableReadable(final boolean recover, final boolean nullTags) |
| 115 | + throws Exception { |
| 116 | + final File source = new File(temporaryFolder.getRoot(), "source.tsfile"); |
| 117 | + final TableSchema schema = TsFileTableSchemaUtil.toTsFileTableSchemaNoAttribute(table); |
| 118 | + try (final TsFileWriter writer = new TsFileWriter(source)) { |
| 119 | + writer.registerTableSchema(schema); |
| 120 | + final Tablet tablet = |
| 121 | + new Tablet( |
| 122 | + TABLE, |
| 123 | + Arrays.asList("device", "value"), |
| 124 | + Arrays.asList(TSDataType.STRING, TSDataType.INT32), |
| 125 | + Arrays.asList(ColumnCategory.TAG, ColumnCategory.FIELD), |
| 126 | + ROW_COUNT); |
| 127 | + for (int i = 0; i < ROW_COUNT; i++) { |
| 128 | + tablet.addTimestamp(i, i); |
| 129 | + tablet.addValue("device", i, nullTags ? null : "d" + i % 2); |
| 130 | + tablet.addValue("value", i, i); |
| 131 | + } |
| 132 | + writer.writeTable(tablet); |
| 133 | + } |
| 134 | + |
| 135 | + final File taskDir = temporaryFolder.newFolder("task"); |
| 136 | + TsFileWriterManager manager = new TsFileWriterManager(dataRegion, taskDir); |
| 137 | + try { |
| 138 | + final ChunkOffsetCalculator calculator = new ChunkOffsetCalculator(); |
| 139 | + final List<TsFileData> pieces = new ArrayList<>(); |
| 140 | + new TsFileSplitter( |
| 141 | + source, |
| 142 | + data -> { |
| 143 | + calculator.assign((ChunkData) data); |
| 144 | + pieces.add(data); |
| 145 | + return true; |
| 146 | + }) |
| 147 | + .splitTsFileByDataPartition(); |
| 148 | + manager.writePiece(pieces); |
| 149 | + if (recover) { |
| 150 | + manager.close(true); |
| 151 | + manager = new TsFileWriterManager(dataRegion, taskDir).recoverFromDisk(); |
| 152 | + } |
| 153 | + manager.prepare(false, Collections.emptyMap(), Collections.emptyMap()); |
| 154 | + |
| 155 | + final File staged = new File(taskDir, DATABASE + "-0-0.tsfile"); |
| 156 | + int rowCount = 0; |
| 157 | + try (final TsFileInsertionEventTableParser parser = |
| 158 | + new TsFileInsertionEventTableParser( |
| 159 | + staged, |
| 160 | + new TablePattern(true, null, null), |
| 161 | + Long.MIN_VALUE, |
| 162 | + Long.MAX_VALUE, |
| 163 | + null, |
| 164 | + null, |
| 165 | + null, |
| 166 | + false)) { |
| 167 | + for (final TabletInsertionEvent event : parser.toTabletInsertionEvents()) { |
| 168 | + final PipeRawTabletInsertionEvent tabletEvent = (PipeRawTabletInsertionEvent) event; |
| 169 | + rowCount += tabletEvent.convertToTablet().getRowSize(); |
| 170 | + tabletEvent.clearReferenceCount(getClass().getName()); |
| 171 | + } |
| 172 | + } |
| 173 | + assertEquals(ROW_COUNT, rowCount); |
| 174 | + try (final TsFileSequenceReader reader = new TsFileSequenceReader(staged.getPath())) { |
| 175 | + assertEquals(schema, reader.getTableSchemaMap().get(TABLE)); |
| 176 | + } |
| 177 | + } finally { |
| 178 | + manager.close(); |
| 179 | + } |
| 180 | + } |
| 181 | +} |
0 commit comments