2727import org .apache .iotdb .db .engine .compaction .cross .rewrite .selector .RewriteCompactionFileSelector ;
2828import org .apache .iotdb .db .engine .modification .Modification ;
2929import org .apache .iotdb .db .engine .modification .ModificationFile ;
30- import org .apache .iotdb .db .engine .storagegroup .TsFileManager ;
3130import org .apache .iotdb .db .engine .storagegroup .TsFileResource ;
3231import org .apache .iotdb .db .engine .storagegroup .TsFileResourceStatus ;
33- import org .apache .iotdb .db .exception .metadata .MetadataException ;
34- import org .apache .iotdb .db .exception .metadata .PathNotExistException ;
35- import org .apache .iotdb .db .metadata .idtable .IDTableManager ;
36- import org .apache .iotdb .db .metadata .path .PartialPath ;
3732import org .apache .iotdb .db .query .control .FileReaderManager ;
38- import org .apache .iotdb .db .service .IoTDB ;
3933import org .apache .iotdb .tsfile .common .constant .TsFileConstant ;
40- import org .apache .iotdb .tsfile .file .metadata .AlignedChunkMetadata ;
41- import org .apache .iotdb .tsfile .file .metadata .ChunkMetadata ;
4234import org .apache .iotdb .tsfile .fileSystem .FSFactoryProducer ;
43- import org .apache .iotdb .tsfile .read .TsFileSequenceReader ;
44- import org .apache .iotdb .tsfile .utils .Pair ;
45- import org .apache .iotdb .tsfile .write .schema .IMeasurementSchema ;
46- import org .apache .iotdb .tsfile .write .writer .TsFileIOWriter ;
4735
48- import org .apache .commons .io .FileUtils ;
4936import org .slf4j .Logger ;
5037import org .slf4j .LoggerFactory ;
5138
5239import java .io .File ;
5340import java .io .IOException ;
5441import java .util .ArrayList ;
5542import java .util .Collection ;
56- import java .util .Comparator ;
57- import java .util .LinkedList ;
5843import java .util .List ;
5944
6045public class InnerSpaceCompactionUtils {
@@ -66,93 +51,6 @@ private InnerSpaceCompactionUtils() {
6651 throw new IllegalStateException ("Utility class" );
6752 }
6853
69- public static void compact (TsFileResource targetResource , List <TsFileResource > tsFileResources )
70- throws IOException , MetadataException , InterruptedException {
71-
72- try (MultiTsFileDeviceIterator deviceIterator = new MultiTsFileDeviceIterator (tsFileResources );
73- TsFileIOWriter writer = new TsFileIOWriter (targetResource .getTsFile ())) {
74- while (deviceIterator .hasNextDevice ()) {
75- Pair <String , Boolean > deviceInfo = deviceIterator .nextDevice ();
76- String device = deviceInfo .left ;
77- boolean aligned = deviceInfo .right ;
78-
79- writer .startChunkGroup (device );
80- if (aligned ) {
81- compactAlignedSeries (device , targetResource , writer , deviceIterator );
82- } else {
83- compactNotAlignedSeries (device , targetResource , writer , deviceIterator );
84- }
85- writer .endChunkGroup ();
86- }
87-
88- for (TsFileResource tsFileResource : tsFileResources ) {
89- targetResource .updatePlanIndexes (tsFileResource );
90- }
91- writer .endFile ();
92- targetResource .close ();
93- }
94- }
95-
96- private static void checkThreadInterrupted (TsFileResource tsFileResource )
97- throws InterruptedException {
98- if (Thread .currentThread ().isInterrupted ()) {
99- throw new InterruptedException (
100- String .format (
101- "[Compaction] compaction for target file %s abort" , tsFileResource .toString ()));
102- }
103- }
104-
105- private static void compactNotAlignedSeries (
106- String device ,
107- TsFileResource targetResource ,
108- TsFileIOWriter writer ,
109- MultiTsFileDeviceIterator deviceIterator )
110- throws IOException , MetadataException , InterruptedException {
111- MultiTsFileDeviceIterator .MeasurementIterator seriesIterator =
112- deviceIterator .iterateNotAlignedSeries (device , true );
113- while (seriesIterator .hasNextSeries ()) {
114- checkThreadInterrupted (targetResource );
115- // TODO: we can provide a configuration item to enable concurrent between each series
116- PartialPath p = new PartialPath (device , seriesIterator .nextSeries ());
117- IMeasurementSchema measurementSchema ;
118- // TODO: seriesIterator needs to be refactor.
119- // This statement must be called before next hasNextSeries() called, or it may be trapped in a
120- // dead-loop.
121- LinkedList <Pair <TsFileSequenceReader , List <ChunkMetadata >>> readerAndChunkMetadataList =
122- seriesIterator .getMetadataListForCurrentSeries ();
123- try {
124- if (IoTDBDescriptor .getInstance ().getConfig ().isEnableIDTable ()) {
125- measurementSchema =
126- IDTableManager .getInstance ().getSeriesSchema (device , p .getMeasurement ());
127- } else {
128- measurementSchema = IoTDB .schemaProcessor .getSeriesSchema (p );
129- }
130- } catch (PathNotExistException e ) {
131- logger .info ("A deleted path is skipped: {}" , e .getMessage ());
132- continue ;
133- }
134- SingleSeriesCompactionExecutor compactionExecutorOfCurrentTimeSeries =
135- new SingleSeriesCompactionExecutor (
136- p , measurementSchema , readerAndChunkMetadataList , writer , targetResource );
137- compactionExecutorOfCurrentTimeSeries .execute ();
138- }
139- }
140-
141- private static void compactAlignedSeries (
142- String device ,
143- TsFileResource targetResource ,
144- TsFileIOWriter writer ,
145- MultiTsFileDeviceIterator deviceIterator )
146- throws IOException , InterruptedException {
147- checkThreadInterrupted (targetResource );
148- LinkedList <Pair <TsFileSequenceReader , List <AlignedChunkMetadata >>> readerAndChunkMetadataList =
149- deviceIterator .getReaderAndChunkMetadataForCurrentAlignedSeries ();
150- AlignedSeriesCompactionExecutor compactionExecutor =
151- new AlignedSeriesCompactionExecutor (
152- device , targetResource , readerAndChunkMetadataList , writer );
153- compactionExecutor .execute ();
154- }
155-
15654 public static boolean deleteTsFilesInDisk (
15755 Collection <TsFileResource > mergeTsFiles , String storageGroupName ) {
15856 logger .info ("{} [Compaction] Compaction starts to delete real file " , storageGroupName );
@@ -185,34 +83,6 @@ public static void deleteModificationForSourceFile(
18583 }
18684 }
18785
188- /**
189- * This method is called to recover modifications while an exception occurs during compaction. It
190- * append new modifications of each selected tsfile to its corresponding old mods file and delete
191- * the compaction mods file.
192- *
193- * @param selectedTsFileResources
194- * @throws IOException
195- */
196- public static void appendNewModificationsToOldModsFile (
197- List <TsFileResource > selectedTsFileResources ) throws IOException {
198- for (TsFileResource sourceFile : selectedTsFileResources ) {
199- // if there are modifications to this seqFile during compaction
200- if (sourceFile .getCompactionModFile ().exists ()) {
201- ModificationFile compactionModificationFile =
202- ModificationFile .getCompactionMods (sourceFile );
203- Collection <Modification > newModification = compactionModificationFile .getModifications ();
204- compactionModificationFile .close ();
205- // write the new modifications to its old modification file
206- try (ModificationFile oldModificationFile = sourceFile .getModFile ()) {
207- for (Modification modification : newModification ) {
208- oldModificationFile .write (modification );
209- }
210- }
211- FileUtils .delete (new File (ModificationFile .getCompactionMods (sourceFile ).getFilePath ()));
212- }
213- }
214- }
215-
21686 /**
21787 * Collect all the compaction modification files of source files, and combines them as the
21888 * modification file of target file.
@@ -262,13 +132,6 @@ public static ICrossSpaceMergeFileSelector getCrossSpaceFileSelector(
262132 }
263133 }
264134
265- public static class TsFileNameComparator implements Comparator <TsFileSequenceReader > {
266-
267- @ Override
268- public int compare (TsFileSequenceReader o1 , TsFileSequenceReader o2 ) {
269- return TsFileManager .compareFileName (new File (o1 .getFileName ()), new File (o2 .getFileName ()));
270- }
271- }
272135 /**
273136 * Update the targetResource. Move xxx.target to xxx.tsfile and serialize xxx.tsfile.resource .
274137 *
0 commit comments