Skip to content

Commit 8f54b80

Browse files
authored
Fixed the bug that schema region in lower version tree model may lose data when upgrades to table model
1 parent ad473d9 commit 8f54b80

File tree

2 files changed

+30
-25
lines changed

2 files changed

+30
-25
lines changed

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/attribute/DeviceAttributeStore.java

+22-17
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ public class DeviceAttributeStore implements IDeviceAttributeStore {
5353

5454
private final MemSchemaRegionStatistics regionStatistics;
5555

56-
public DeviceAttributeStore(MemSchemaRegionStatistics regionStatistics) {
56+
public DeviceAttributeStore(final MemSchemaRegionStatistics regionStatistics) {
5757
this.regionStatistics = regionStatistics;
5858
}
5959

@@ -63,15 +63,15 @@ public void clear() {
6363
}
6464

6565
@Override
66-
public synchronized boolean createSnapshot(File targetDir) {
67-
File snapshotTmp =
66+
public synchronized boolean createSnapshot(final File targetDir) {
67+
final File snapshotTmp =
6868
SystemFileFactory.INSTANCE.getFile(targetDir, SchemaConstant.DEVICE_ATTRIBUTE_SNAPSHOT_TMP);
69-
File snapshot =
69+
final File snapshot =
7070
SystemFileFactory.INSTANCE.getFile(targetDir, SchemaConstant.DEVICE_ATTRIBUTE_SNAPSHOT);
7171

7272
try {
73-
FileOutputStream fileOutputStream = new FileOutputStream(snapshotTmp);
74-
BufferedOutputStream outputStream = new BufferedOutputStream(fileOutputStream);
73+
final FileOutputStream fileOutputStream = new FileOutputStream(snapshotTmp);
74+
final BufferedOutputStream outputStream = new BufferedOutputStream(fileOutputStream);
7575
try {
7676
serialize(outputStream);
7777
} finally {
@@ -95,7 +95,7 @@ public synchronized boolean createSnapshot(File targetDir) {
9595
}
9696

9797
return true;
98-
} catch (IOException e) {
98+
} catch (final IOException e) {
9999
logger.error("Failed to create mtree snapshot due to {}", e.getMessage(), e);
100100
FileUtils.deleteFileIfExist(snapshot);
101101
return false;
@@ -105,25 +105,30 @@ public synchronized boolean createSnapshot(File targetDir) {
105105
}
106106

107107
@Override
108-
public void loadFromSnapshot(File snapshotDir, String sgSchemaDirPath) throws IOException {
109-
try (BufferedInputStream inputStream =
110-
new BufferedInputStream(
111-
Files.newInputStream(
112-
SystemFileFactory.INSTANCE
113-
.getFile(snapshotDir, SchemaConstant.DEVICE_ATTRIBUTE_SNAPSHOT)
114-
.toPath()))) {
108+
public void loadFromSnapshot(final File snapshotDir, final String sgSchemaDirPath)
109+
throws IOException {
110+
final File snapshot =
111+
SystemFileFactory.INSTANCE.getFile(snapshotDir, SchemaConstant.DEVICE_ATTRIBUTE_SNAPSHOT);
112+
if (!snapshot.exists()) {
113+
logger.info(
114+
"Device attribute snapshot {} not found, consider it as upgraded from the older version, use empty attributes",
115+
snapshot);
116+
return;
117+
}
118+
try (final BufferedInputStream inputStream =
119+
new BufferedInputStream(Files.newInputStream(snapshot.toPath()))) {
115120
deserialize(inputStream);
116-
} catch (IOException e) {
121+
} catch (final IOException e) {
117122
logger.warn("Load device attribute snapshot from {} failed", snapshotDir);
118123
throw e;
119124
}
120125
}
121126

122127
@Override
123-
public synchronized int createAttribute(List<String> nameList, Object[] valueList) {
128+
public synchronized int createAttribute(final List<String> nameList, final Object[] valueList) {
124129
// todo implement storage for device of diverse data types
125130
long memUsage = 0L;
126-
Map<String, String> attributeMap = new HashMap<>();
131+
final Map<String, String> attributeMap = new HashMap<>();
127132
String value;
128133
for (int i = 0; i < nameList.size(); i++) {
129134
value =

iotdb-core/datanode/src/main/java/org/apache/iotdb/db/schemaengine/schemaregion/impl/SchemaRegionMemoryImpl.java

+8-8
Original file line numberDiff line numberDiff line change
@@ -504,34 +504,34 @@ public synchronized boolean createSnapshot(File snapshotDir) {
504504

505505
// currently, this method is only used for cluster-ratis mode
506506
@Override
507-
public void loadSnapshot(File latestSnapshotRootDir) {
507+
public void loadSnapshot(final File latestSnapshotRootDir) {
508508
clear();
509509

510510
logger.info("Start loading snapshot of schemaRegion {}", schemaRegionId);
511-
long startTime = System.currentTimeMillis();
511+
final long startTime = System.currentTimeMillis();
512512

513513
try {
514514
usingMLog = false;
515515

516516
isRecovering = true;
517517

518-
long deviceAttributeSnapshotStartTime = System.currentTimeMillis();
518+
final long deviceAttributeSnapshotStartTime = System.currentTimeMillis();
519519
deviceAttributeStore = new DeviceAttributeStore(regionStatistics);
520520
deviceAttributeStore.loadFromSnapshot(latestSnapshotRootDir, schemaRegionDirPath);
521521
logger.info(
522522
"Device attribute snapshot loading of schemaRegion {} costs {}ms.",
523523
schemaRegionId,
524524
System.currentTimeMillis() - deviceAttributeSnapshotStartTime);
525525

526-
long tagSnapshotStartTime = System.currentTimeMillis();
526+
final long tagSnapshotStartTime = System.currentTimeMillis();
527527
tagManager =
528528
TagManager.loadFromSnapshot(latestSnapshotRootDir, schemaRegionDirPath, regionStatistics);
529529
logger.info(
530530
"Tag snapshot loading of schemaRegion {} costs {}ms.",
531531
schemaRegionId,
532532
System.currentTimeMillis() - tagSnapshotStartTime);
533533

534-
long mtreeSnapshotStartTime = System.currentTimeMillis();
534+
final long mTreeSnapshotStartTime = System.currentTimeMillis();
535535
mtree =
536536
MTreeBelowSGMemoryImpl.loadFromSnapshot(
537537
latestSnapshotRootDir,
@@ -567,7 +567,7 @@ public void loadSnapshot(File latestSnapshotRootDir) {
567567
logger.info(
568568
"MTree snapshot loading of schemaRegion {} costs {}ms.",
569569
schemaRegionId,
570-
System.currentTimeMillis() - mtreeSnapshotStartTime);
570+
System.currentTimeMillis() - mTreeSnapshotStartTime);
571571

572572
isRecovering = false;
573573
initialized = true;
@@ -577,7 +577,7 @@ public void loadSnapshot(File latestSnapshotRootDir) {
577577
schemaRegionId,
578578
System.currentTimeMillis() - startTime);
579579
logger.info("Successfully load snapshot of schemaRegion {}", schemaRegionId);
580-
} catch (IOException | IllegalPathException e) {
580+
} catch (final IOException | IllegalPathException e) {
581581
logger.error(
582582
"Failed to load snapshot for schemaRegion {} due to {}. Use empty schemaRegion",
583583
schemaRegionId,
@@ -587,7 +587,7 @@ public void loadSnapshot(File latestSnapshotRootDir) {
587587
initialized = false;
588588
isRecovering = true;
589589
init();
590-
} catch (MetadataException metadataException) {
590+
} catch (final MetadataException metadataException) {
591591
logger.error(
592592
"Error occurred during initializing schemaRegion {}",
593593
schemaRegionId,

0 commit comments

Comments
 (0)