-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Pipe: use mem table to batch write tree data into tsfile #15276
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
2444f33
setup
VGalaxies a95afba
minor improve
VGalaxies 1152762
fix IT
VGalaxies 8152dd8
fix IT
VGalaxies 75a84b1
fix review
VGalaxies 48bc886
minor improve
VGalaxies a91be16
refact to V2
VGalaxies efb8267
try fix
VGalaxies 6a2af7a
release memTable
VGalaxies c5f5ce2
fix review
VGalaxies 03d02c3
fixup
VGalaxies 58ad914
minor improve
VGalaxies d6c3bfe
fix totalBufferSize
VGalaxies a1f3eb0
Merge branch 'master' into tree-tsfile-builder
VGalaxies File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
196 changes: 196 additions & 0 deletions
196
...in/java/org/apache/iotdb/db/pipe/connector/util/builder/PipeTreeModelTsFileBuilderV2.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,196 @@ | ||
| /* | ||
| * 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.pipe.connector.util.builder; | ||
|
|
||
| import org.apache.iotdb.commons.path.PartialPath; | ||
| import org.apache.iotdb.db.queryengine.plan.planner.plan.node.PlanNodeId; | ||
| import org.apache.iotdb.db.queryengine.plan.planner.plan.node.write.InsertTabletNode; | ||
| import org.apache.iotdb.db.storageengine.dataregion.flush.MemTableFlushTask; | ||
| import org.apache.iotdb.db.storageengine.dataregion.memtable.IMemTable; | ||
| import org.apache.iotdb.db.storageengine.dataregion.memtable.PrimitiveMemTable; | ||
|
|
||
| import org.apache.tsfile.enums.TSDataType; | ||
| import org.apache.tsfile.exception.write.WriteProcessException; | ||
| import org.apache.tsfile.utils.DateUtils; | ||
| import org.apache.tsfile.utils.Pair; | ||
| import org.apache.tsfile.write.record.Tablet; | ||
| import org.apache.tsfile.write.schema.IMeasurementSchema; | ||
| import org.apache.tsfile.write.schema.MeasurementSchema; | ||
| import org.apache.tsfile.write.writer.RestorableTsFileIOWriter; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.time.LocalDate; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
| import java.util.concurrent.atomic.AtomicLong; | ||
|
|
||
| public class PipeTreeModelTsFileBuilderV2 extends PipeTsFileBuilder { | ||
|
|
||
| private static final Logger LOGGER = LoggerFactory.getLogger(PipeTreeModelTsFileBuilderV2.class); | ||
|
|
||
| private static final PlanNodeId PLACEHOLDER_PLAN_NODE_ID = | ||
| new PlanNodeId("PipeTreeModelTsFileBuilderV2"); | ||
|
|
||
| private final List<Tablet> tabletList = new ArrayList<>(); | ||
| private final List<Boolean> isTabletAlignedList = new ArrayList<>(); | ||
|
|
||
| // TODO: remove me later if stable | ||
| private final PipeTreeModelTsFileBuilder fallbackBuilder; | ||
|
|
||
| public PipeTreeModelTsFileBuilderV2( | ||
| final AtomicLong currentBatchId, final AtomicLong tsFileIdGenerator) { | ||
| super(currentBatchId, tsFileIdGenerator); | ||
| fallbackBuilder = new PipeTreeModelTsFileBuilder(currentBatchId, tsFileIdGenerator); | ||
| } | ||
|
|
||
| @Override | ||
| public void bufferTableModelTablet(final String dataBase, final Tablet tablet) { | ||
| throw new UnsupportedOperationException( | ||
| "PipeTreeModelTsFileBuilderV2 does not support table model tablet to build TSFile"); | ||
| } | ||
|
|
||
| @Override | ||
| public void bufferTreeModelTablet(final Tablet tablet, final Boolean isAligned) { | ||
| tabletList.add(tablet); | ||
| isTabletAlignedList.add(isAligned); | ||
| fallbackBuilder.bufferTreeModelTablet(tablet, isAligned); | ||
| } | ||
|
|
||
| @Override | ||
| public List<Pair<String, File>> convertTabletToTsFileWithDBInfo() | ||
| throws IOException, WriteProcessException { | ||
| try { | ||
| return writeTabletsToTsFiles(); | ||
| } catch (final Exception e) { | ||
| LOGGER.warn( | ||
| "Exception occurred when PipeTreeModelTsFileBuilderV2 writing tablets to tsfile, use fallback tsfile builder: {}", | ||
| e.getMessage(), | ||
| e); | ||
| return fallbackBuilder.convertTabletToTsFileWithDBInfo(); | ||
| } | ||
| } | ||
|
|
||
| @Override | ||
| public boolean isEmpty() { | ||
| return tabletList.isEmpty(); | ||
| } | ||
|
|
||
| @Override | ||
| public void onSuccess() { | ||
| super.onSuccess(); | ||
| tabletList.clear(); | ||
| isTabletAlignedList.clear(); | ||
| fallbackBuilder.onSuccess(); | ||
| } | ||
|
|
||
| @Override | ||
| public synchronized void close() { | ||
| super.close(); | ||
| tabletList.clear(); | ||
| isTabletAlignedList.clear(); | ||
| fallbackBuilder.close(); | ||
| } | ||
|
|
||
| private List<Pair<String, File>> writeTabletsToTsFiles() throws WriteProcessException { | ||
| final IMemTable memTable = new PrimitiveMemTable(null, null); | ||
| final List<Pair<String, File>> sealedFiles = new ArrayList<>(); | ||
| try (final RestorableTsFileIOWriter writer = new RestorableTsFileIOWriter(createFile())) { | ||
| writeTabletsIntoOneFile(memTable, writer); | ||
| sealedFiles.add(new Pair<>(null, writer.getFile())); | ||
| } catch (final Exception e) { | ||
| LOGGER.warn( | ||
| "Batch id = {}: Failed to write tablets into tsfile, because {}", | ||
| currentBatchId.get(), | ||
| e.getMessage(), | ||
| e); | ||
| // TODO: handle ex | ||
| throw new WriteProcessException(e); | ||
| } finally { | ||
| memTable.release(); | ||
| } | ||
|
|
||
| return sealedFiles; | ||
| } | ||
|
|
||
| private void writeTabletsIntoOneFile( | ||
| final IMemTable memTable, final RestorableTsFileIOWriter writer) throws Exception { | ||
| for (int i = 0, size = tabletList.size(); i < size; ++i) { | ||
| final Tablet tablet = tabletList.get(i); | ||
|
|
||
| // convert date value to int | ||
| // refer to | ||
| // org.apache.iotdb.db.storageengine.dataregion.memtable.WritableMemChunk.writeNonAlignedTablet | ||
| final Object[] values = tablet.getValues(); | ||
| for (int j = 0; j < tablet.getSchemas().size(); ++j) { | ||
| final IMeasurementSchema schema = tablet.getSchemas().get(j); | ||
| if (Objects.nonNull(schema) && Objects.equals(TSDataType.DATE, schema.getType())) { | ||
| final LocalDate[] dates = ((LocalDate[]) values[j]); | ||
| final int[] dateValues = new int[dates.length]; | ||
| for (int k = 0; k < Math.min(dates.length, tablet.getRowSize()); k++) { | ||
| dateValues[k] = DateUtils.parseDateExpressionToInt(dates[k]); | ||
| } | ||
| values[j] = dateValues; | ||
| } | ||
| } | ||
|
|
||
| final InsertTabletNode insertTabletNode = | ||
| new InsertTabletNode( | ||
| PLACEHOLDER_PLAN_NODE_ID, | ||
| new PartialPath(tablet.getDeviceId()), | ||
| isTabletAlignedList.get(i), | ||
| tablet.getSchemas().stream() | ||
| .map(IMeasurementSchema::getMeasurementName) | ||
| .toArray(String[]::new), | ||
| tablet.getSchemas().stream() | ||
| .map(IMeasurementSchema::getType) | ||
| .toArray(TSDataType[]::new), | ||
| // TODO: cast | ||
| tablet.getSchemas().stream() | ||
| .map(schema -> (MeasurementSchema) schema) | ||
| .toArray(MeasurementSchema[]::new), | ||
| tablet.getTimestamps(), | ||
| tablet.getBitMaps(), | ||
| tablet.getValues(), | ||
| tablet.getRowSize()); | ||
|
|
||
| final int start = 0; | ||
| final int end = insertTabletNode.getRowCount(); | ||
|
|
||
| try { | ||
| if (insertTabletNode.isAligned()) { | ||
| memTable.insertAlignedTablet(insertTabletNode, start, end, null); | ||
| } else { | ||
| memTable.insertTablet(insertTabletNode, start, end); | ||
| } | ||
| } catch (final org.apache.iotdb.db.exception.WriteProcessException e) { | ||
| throw new WriteProcessException(e); | ||
| } | ||
| } | ||
|
|
||
| final MemTableFlushTask memTableFlushTask = new MemTableFlushTask(memTable, writer, null, null); | ||
| memTableFlushTask.syncFlushMemTable(); | ||
|
|
||
| writer.endFile(); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.