Skip to content

Commit 66e53e1

Browse files
authored
[IOTDB-3771] Fix cannot take snapshot when the data dir and snapshot dir is on different disk (apache#6782)
1 parent cf4ca21 commit 66e53e1

File tree

20 files changed

+847
-260
lines changed

20 files changed

+847
-260
lines changed

node-commons/src/main/java/org/apache/iotdb/commons/utils/FileUtils.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,4 +136,21 @@ public static long getDirSize(String path) {
136136
}
137137
return sum;
138138
}
139+
140+
public static void recursiveDeleteFolder(String path) throws IOException {
141+
File file = new File(path);
142+
if (file.isDirectory()) {
143+
File[] files = file.listFiles();
144+
if (files == null || files.length == 0) {
145+
org.apache.commons.io.FileUtils.deleteDirectory(file);
146+
} else {
147+
for (File f : files) {
148+
recursiveDeleteFolder(f.getAbsolutePath());
149+
}
150+
org.apache.commons.io.FileUtils.deleteDirectory(file);
151+
}
152+
} else {
153+
org.apache.commons.io.FileUtils.delete(file);
154+
}
155+
}
139156
}

server/pom.xml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,12 @@
223223
<groupId>com.google.guava</groupId>
224224
<artifactId>guava</artifactId>
225225
</dependency>
226+
<dependency>
227+
<groupId>org.apache.iotdb</groupId>
228+
<artifactId>node-commons</artifactId>
229+
<version>${project.version}</version>
230+
<scope>compile</scope>
231+
</dependency>
226232
</dependencies>
227233
<build>
228234
<plugins>

server/src/main/java/org/apache/iotdb/db/conf/IoTDBConfig.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1156,7 +1156,7 @@ public String[] getDataDirs() {
11561156
return dataDirs;
11571157
}
11581158

1159-
void setDataDirs(String[] dataDirs) {
1159+
public void setDataDirs(String[] dataDirs) {
11601160
this.dataDirs = dataDirs;
11611161
}
11621162

server/src/main/java/org/apache/iotdb/db/conf/directories/DirectoryManager.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.apache.iotdb.db.conf.directories;
2020

2121
import org.apache.iotdb.commons.conf.IoTDBConstant;
22+
import org.apache.iotdb.commons.utils.TestOnly;
2223
import org.apache.iotdb.db.conf.IoTDBDescriptor;
2324
import org.apache.iotdb.db.conf.SystemStatus;
2425
import org.apache.iotdb.db.conf.directories.strategy.DirectoryStrategy;
@@ -175,6 +176,22 @@ public List<String> getAllFilesFolders() {
175176
return folders;
176177
}
177178

179+
@TestOnly
180+
public void resetFolders() {
181+
sequenceFileFolders =
182+
new ArrayList<>(Arrays.asList(IoTDBDescriptor.getInstance().getConfig().getDataDirs()));
183+
for (int i = 0; i < sequenceFileFolders.size(); i++) {
184+
sequenceFileFolders.set(
185+
i, sequenceFileFolders.get(i) + File.separator + IoTDBConstant.SEQUENCE_FLODER_NAME);
186+
}
187+
unsequenceFileFolders =
188+
new ArrayList<>(Arrays.asList(IoTDBDescriptor.getInstance().getConfig().getDataDirs()));
189+
for (int i = 0; i < unsequenceFileFolders.size(); i++) {
190+
unsequenceFileFolders.set(
191+
i, unsequenceFileFolders.get(i) + File.separator + IoTDBConstant.UNSEQUENCE_FLODER_NAME);
192+
}
193+
}
194+
178195
private static class DirectoriesHolder {
179196
private static final DirectoryManager INSTANCE = new DirectoryManager();
180197
}

server/src/main/java/org/apache/iotdb/db/consensus/statemachine/DataRegionStateMachine.java

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.slf4j.LoggerFactory;
4949

5050
import java.io.File;
51+
import java.io.IOException;
5152
import java.util.ArrayList;
5253
import java.util.LinkedList;
5354
import java.util.List;
@@ -97,7 +98,7 @@ public boolean takeSnapshot(File snapshotDir) {
9798
} catch (Exception e) {
9899
logger.error(
99100
"Exception occurs when taking snapshot for {}-{} in {}",
100-
region.getLogicalStorageGroupName(),
101+
region.getStorageGroupName(),
101102
region.getDataRegionId(),
102103
snapshotDir,
103104
e);
@@ -110,7 +111,7 @@ public void loadSnapshot(File latestSnapshotRootDir) {
110111
DataRegion newRegion =
111112
new SnapshotLoader(
112113
latestSnapshotRootDir.getAbsolutePath(),
113-
region.getLogicalStorageGroupName(),
114+
region.getStorageGroupName(),
114115
region.getDataRegionId())
115116
.loadSnapshotForStateMachine();
116117
if (newRegion == null) {
@@ -258,8 +259,20 @@ private InsertNode grabInsertNode(IndexedConsensusRequest indexedRequest) {
258259

259260
@Override
260261
public List<File> getSnapshotFiles(File latestSnapshotRootDir) {
261-
// TODO: implement this method
262-
return super.getSnapshotFiles(latestSnapshotRootDir);
262+
try {
263+
return new SnapshotLoader(
264+
latestSnapshotRootDir.getAbsolutePath(),
265+
region.getStorageGroupName(),
266+
region.getDataRegionId())
267+
.getSnapshotFileInfo();
268+
} catch (IOException e) {
269+
logger.error(
270+
"Meets error when getting snapshot files for {}-{}",
271+
region.getStorageGroupName(),
272+
region.getDataRegionId(),
273+
e);
274+
return null;
275+
}
263276
}
264277

265278
@Override

server/src/main/java/org/apache/iotdb/db/engine/StorageEngine.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1113,7 +1113,7 @@ public void mergeUnLock(List<DataRegion> list) {
11131113
public String getStorageGroupPath(PartialPath path) throws StorageEngineException {
11141114
PartialPath deviceId = path.getDevicePath();
11151115
DataRegion storageGroupProcessor = getProcessor(deviceId);
1116-
return storageGroupProcessor.getLogicalStorageGroupName()
1116+
return storageGroupProcessor.getStorageGroupName()
11171117
+ File.separator
11181118
+ storageGroupProcessor.getDataRegionId();
11191119
}

server/src/main/java/org/apache/iotdb/db/engine/StorageEngineV2.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ public void forceCloseAllProcessor() throws TsFileProcessorException {
540540

541541
public void closeStorageGroupProcessor(String storageGroupPath, boolean isSeq) {
542542
for (DataRegion dataRegion : dataRegionMap.values()) {
543-
if (dataRegion.getLogicalStorageGroupName().equals(storageGroupPath)) {
543+
if (dataRegion.getStorageGroupName().equals(storageGroupPath)) {
544544
if (isSeq) {
545545
for (TsFileProcessor tsFileProcessor : dataRegion.getWorkSequenceTsFileProcessors()) {
546546
dataRegion.syncCloseOneTsFileProcessor(isSeq, tsFileProcessor);
@@ -662,14 +662,12 @@ public void deleteDataRegion(DataRegionId regionId) {
662662
.equals(ConsensusFactory.MultiLeaderConsensus)) {
663663
WALManager.getInstance()
664664
.deleteWALNode(
665-
region.getLogicalStorageGroupName()
666-
+ FILE_NAME_SEPARATOR
667-
+ region.getDataRegionId());
665+
region.getStorageGroupName() + FILE_NAME_SEPARATOR + region.getDataRegionId());
668666
}
669667
} catch (Exception e) {
670668
logger.error(
671669
"Error occurs when deleting data region {}-{}",
672-
region.getLogicalStorageGroupName(),
670+
region.getStorageGroupName(),
673671
region.getDataRegionId(),
674672
e);
675673
} finally {
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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.engine.snapshot;
21+
22+
import org.apache.iotdb.db.engine.modification.ModificationFile;
23+
import org.apache.iotdb.db.engine.storagegroup.TsFileResource;
24+
import org.apache.iotdb.tsfile.common.constant.TsFileConstant;
25+
26+
import java.io.File;
27+
import java.util.Arrays;
28+
import java.util.HashSet;
29+
import java.util.Set;
30+
31+
public class SnapshotFileSet {
32+
public static final String[] DATA_FILE_SUFFIX =
33+
new String[] {
34+
TsFileConstant.TSFILE_SUFFIX, TsFileResource.RESOURCE_SUFFIX, ModificationFile.FILE_SUFFIX,
35+
};
36+
37+
private static final Set<String> DATA_FILE_SUFFIX_SET =
38+
new HashSet<>(Arrays.asList(DATA_FILE_SUFFIX));
39+
40+
public static boolean isDataFile(File file) {
41+
String[] fileName = file.getName().split("\\.");
42+
return DATA_FILE_SUFFIX_SET.contains(fileName[fileName.length - 1]);
43+
}
44+
}

0 commit comments

Comments
 (0)