Skip to content

Commit f76571d

Browse files
authored
Fix snapshot load region replacement (#17684)
1 parent e9584ec commit f76571d

3 files changed

Lines changed: 48 additions & 17 deletions

File tree

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/consensus/statemachine/dataregion/DataRegionStateMachine.java

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -123,21 +123,25 @@ public boolean clearSnapshot() {
123123

124124
@Override
125125
public void loadSnapshot(File latestSnapshotRootDir) {
126-
DataRegion newRegion =
127-
new SnapshotLoader(
128-
latestSnapshotRootDir.getAbsolutePath(),
129-
region.getDatabaseName(),
130-
region.getDataRegionIdString())
131-
.loadSnapshotForStateMachine();
132-
if (newRegion == null) {
133-
logger.error("Fail to load snapshot from {}", latestSnapshotRootDir);
134-
return;
135-
}
136-
this.region = newRegion;
126+
String databaseName = region.getDatabaseName();
127+
String dataRegionIdString = region.getDataRegionIdString();
128+
DataRegionId regionId = new DataRegionId(Integer.parseInt(dataRegionIdString));
137129
try {
138-
StorageEngine.getInstance()
139-
.setDataRegion(
140-
new DataRegionId(Integer.parseInt(region.getDataRegionIdString())), region);
130+
DataRegion newRegion =
131+
StorageEngine.getInstance()
132+
.setDataRegionForSnapshotLoad(
133+
regionId,
134+
() ->
135+
new SnapshotLoader(
136+
latestSnapshotRootDir.getAbsolutePath(),
137+
databaseName,
138+
dataRegionIdString)
139+
.loadSnapshotForStateMachine());
140+
if (newRegion == null) {
141+
logger.error("Fail to load snapshot from {}", latestSnapshotRootDir);
142+
return;
143+
}
144+
this.region = newRegion;
141145
ChunkCache.getInstance().clear();
142146
TimeSeriesMetadataCache.getInstance().clear();
143147
BloomFilterCache.getInstance().clear();

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/StorageEngine.java

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@
115115
import java.util.concurrent.atomic.AtomicLong;
116116
import java.util.concurrent.atomic.AtomicReference;
117117
import java.util.function.Consumer;
118+
import java.util.function.Supplier;
118119
import java.util.stream.Collectors;
119120
import java.util.stream.Stream;
120121

@@ -922,6 +923,33 @@ public int getDataRegionNumber() {
922923
}
923924

924925
/** This method is not thread-safe */
926+
public DataRegion setDataRegionForSnapshotLoad(
927+
DataRegionId regionId, Supplier<DataRegion> newRegionSupplier) {
928+
if (dataRegionMap.containsKey(regionId)) {
929+
DataRegion oldRegion = dataRegionMap.get(regionId);
930+
oldRegion.markDeleted();
931+
oldRegion.abortCompaction();
932+
oldRegion.syncCloseAllWorkingTsFileProcessors();
933+
oldRegion.deleteFolder(systemDir);
934+
WRITING_METRICS.removeDataRegionMemoryCostMetrics(regionId);
935+
WRITING_METRICS.removeFlushingMemTableStatusMetrics(regionId);
936+
WRITING_METRICS.removeActiveMemtableCounterMetrics(regionId);
937+
FileMetrics.getInstance()
938+
.deleteRegion(oldRegion.getDatabaseName(), oldRegion.getDataRegionIdString());
939+
}
940+
941+
DataRegion newRegion = newRegionSupplier.get();
942+
if (newRegion != null) {
943+
WRITING_METRICS.createFlushingMemTableStatusMetrics(regionId);
944+
WRITING_METRICS.createDataRegionMemoryCostMetrics(newRegion);
945+
WRITING_METRICS.createActiveMemtableCounterMetrics(regionId);
946+
dataRegionMap.put(regionId, newRegion);
947+
}
948+
return newRegion;
949+
}
950+
951+
/** This method is not thread-safe */
952+
@TestOnly
925953
public void setDataRegion(DataRegionId regionId, DataRegion newRegion) {
926954
if (dataRegionMap.containsKey(regionId)) {
927955
DataRegion oldRegion = dataRegionMap.get(regionId);

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/storageengine/dataregion/snapshot/SnapshotTaker.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ private void createHardLink(File target, File source) throws IOException {
271271
if (!target.getParentFile().exists()) {
272272
LOGGER.error("Hard link target dir {} doesn't exist", target.getParentFile());
273273
}
274-
if (!checkHardLinkSourceFile(source)) {
274+
if (!checkHardLinkSourceFile(source, 10)) {
275275
return;
276276
}
277277
Files.deleteIfExists(target.toPath());
@@ -280,8 +280,7 @@ private void createHardLink(File target, File source) throws IOException {
280280
}
281281

282282
/** For "source file not exists" problem (jira787) debugging */
283-
private boolean checkHardLinkSourceFile(File source) {
284-
int retry = 10;
283+
private boolean checkHardLinkSourceFile(File source, int retry) {
285284
while (!source.exists() && retry > 0) {
286285
LOGGER.warn(
287286
"Hard link source file {} doesn't exist, will retry for {} times...", source, retry);

0 commit comments

Comments
 (0)