Skip to content

Commit 6c3558f

Browse files
authored
HDDS-14629. Code cleanup after HDDS-13906 (#9773)
1 parent c2bb1b4 commit 6c3558f

File tree

3 files changed

+32
-35
lines changed

3 files changed

+32
-35
lines changed

hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OMDBArchiver.java

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,14 @@ public class OMDBArchiver {
4747

4848
private Path tmpDir;
4949
private Map<String, File> filesToWriteIntoTarball;
50-
private Map<String, String> hardLinkFileMap;
50+
private final Map<String, String> hardLinkFileMap;
5151
private static final Logger LOG = LoggerFactory.getLogger(OMDBArchiver.class);
5252
private boolean completed;
5353

5454
public OMDBArchiver() {
5555
this.tmpDir = null;
5656
this.filesToWriteIntoTarball = new HashMap<>();
57-
this.hardLinkFileMap = null;
57+
this.hardLinkFileMap = new HashMap<>();
5858
this.completed = false;
5959
}
6060

@@ -66,16 +66,12 @@ public Path getTmpDir() {
6666
return tmpDir;
6767
}
6868

69-
public Map<String, String> getHardLinkFileMap() {
70-
return hardLinkFileMap;
71-
}
72-
7369
public Map<String, File> getFilesToWriteIntoTarball() {
7470
return filesToWriteIntoTarball;
7571
}
7672

77-
public void setHardLinkFileMap(Map<String, String> hardLinkFileMap) {
78-
this.hardLinkFileMap = hardLinkFileMap;
73+
public void recordHardLinkMapping(String absolutePath, String fileId) {
74+
hardLinkFileMap.put(absolutePath, fileId);
7975
}
8076

8177
public boolean isCompleted() {

hadoop-ozone/ozone-manager/src/main/java/org/apache/hadoop/ozone/om/OMDBCheckpointServletInodeBasedXfer.java

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -184,6 +184,7 @@ public void processMetadataSnapshotRequest(HttpServletRequest request, HttpServl
184184
}
185185
} catch (IOException e) {
186186
LOG.error("unable to write to archive stream", e);
187+
response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
187188
} finally {
188189
try {
189190
if (tmpdir != null) {
@@ -269,8 +270,6 @@ public void collectDbDataToTransfer(HttpServletRequest request,
269270
if (shouldContinue) {
270271
// we finished transferring files from snapshot DB's by now and
271272
// this is the last step where we transfer the active om.db contents
272-
Map<String, String> hardLinkFileMap = new HashMap<>();
273-
omdbArchiver.setHardLinkFileMap(hardLinkFileMap);
274273
SnapshotCache snapshotCache = om.getOmSnapshotManager().getSnapshotCache();
275274
OmSnapshotLocalDataManager localDataManager = om.getOmSnapshotManager().getSnapshotLocalDataManager();
276275
/*
@@ -376,9 +375,7 @@ void collectSnapshotData(Set<String> sstFilesToExclude, Collection<Path> snapsho
376375
}
377376
for (Path snapshotLocalPropertyYaml : snapshotLocalPropertyFiles) {
378377
File yamlFile = snapshotLocalPropertyYaml.toFile();
379-
if (omdbArchiver.getHardLinkFileMap() != null) {
380-
omdbArchiver.getHardLinkFileMap().put(yamlFile.getAbsolutePath(), yamlFile.getName());
381-
}
378+
omdbArchiver.recordHardLinkMapping(yamlFile.getAbsolutePath(), yamlFile.getName());
382379
omdbArchiver.recordFileEntry(yamlFile, yamlFile.getName());
383380
}
384381
}
@@ -410,17 +407,26 @@ private static void cleanupCheckpoint(DBCheckpoint checkpoint) {
410407
static void writeHardlinkFile(OzoneConfiguration conf, Map<String, String> hardlinkFileMap,
411408
ArchiveOutputStream<TarArchiveEntry> archiveOutputStream) throws IOException {
412409
Path data = Files.createTempFile(DATA_PREFIX, DATA_SUFFIX);
413-
Path metaDirPath = OMStorage.getOmDbDir(conf).toPath();
414-
StringBuilder sb = new StringBuilder();
415-
416-
for (Map.Entry<String, String> entry : hardlinkFileMap.entrySet()) {
417-
Path p = Paths.get(entry.getKey());
418-
String fileId = entry.getValue();
419-
Path relativePath = metaDirPath.relativize(p);
420-
sb.append(relativePath).append('\t').append(fileId).append('\n');
410+
try {
411+
Path metaDirPath = OMStorage.getOmDbDir(conf).toPath()
412+
.toAbsolutePath().normalize();
413+
StringBuilder sb = new StringBuilder();
414+
415+
for (Map.Entry<String, String> entry : hardlinkFileMap.entrySet()) {
416+
Path p = new File(entry.getKey()).toPath();
417+
if (!p.isAbsolute()) {
418+
p = metaDirPath.resolve(p);
419+
}
420+
p = p.toAbsolutePath().normalize();
421+
String fileId = entry.getValue();
422+
Path relativePath = metaDirPath.relativize(p);
423+
sb.append(relativePath).append('\t').append(fileId).append('\n');
424+
}
425+
Files.write(data, sb.toString().getBytes(StandardCharsets.UTF_8), StandardOpenOption.TRUNCATE_EXISTING);
426+
includeFile(data.toFile(), OmSnapshotManager.OM_HARDLINK_FILE, archiveOutputStream);
427+
} finally {
428+
Files.deleteIfExists(data);
421429
}
422-
Files.write(data, sb.toString().getBytes(StandardCharsets.UTF_8), StandardOpenOption.TRUNCATE_EXISTING);
423-
includeFile(data.toFile(), OmSnapshotManager.OM_HARDLINK_FILE, archiveOutputStream);
424430
}
425431

426432
/**
@@ -500,14 +506,12 @@ private boolean collectFilesFromDir(Set<String> sstFilesToExclude, Stream<Path>
500506
continue;
501507
}
502508
String fileId = OmSnapshotUtils.getFileInodeAndLastModifiedTimeString(dbFile);
503-
if (omdbArchiver.getHardLinkFileMap() != null) {
504-
String path = dbFile.toFile().getAbsolutePath();
505-
// if the file is in the om checkpoint dir, then we need to change the path to point to the OM DB.
506-
if (path.contains(OM_CHECKPOINT_DIR)) {
507-
path = getDbStore().getDbLocation().toPath().resolve(dbFile.getFileName()).toAbsolutePath().toString();
508-
}
509-
omdbArchiver.getHardLinkFileMap().put(path, fileId);
509+
String path = dbFile.toFile().getAbsolutePath();
510+
// if the file is in the om checkpoint dir, then we need to change the path to point to the OM DB.
511+
if (path.contains(OM_CHECKPOINT_DIR)) {
512+
path = getDbStore().getDbLocation().toPath().resolve(dbFile.getFileName()).toAbsolutePath().toString();
510513
}
514+
omdbArchiver.recordHardLinkMapping(path, fileId);
511515
if (!sstFilesToExclude.contains(fileId)) {
512516
long fileSize = Files.size(dbFile);
513517
if (maxTotalSstSize.get() - fileSize <= 0) {

hadoop-ozone/ozone-manager/src/test/java/org/apache/hadoop/ozone/om/TestOMDBArchiver.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,7 @@
2727
import java.nio.charset.StandardCharsets;
2828
import java.nio.file.Files;
2929
import java.nio.file.Path;
30-
import java.util.HashMap;
3130
import java.util.List;
32-
import java.util.Map;
3331
import java.util.Optional;
3432
import java.util.stream.Stream;
3533
import org.apache.hadoop.fs.FileUtil;
@@ -86,15 +84,14 @@ public void testWriteToArchive(boolean completed) throws IOException {
8684
omdbArchiver.setTmpDir(tmpDir);
8785
assertThat(omdbArchiver.getFilesToWriteIntoTarball()).isNotNull();
8886
assertThat(omdbArchiver.getTmpDir()).isEqualTo(tmpDir);
89-
Map<String, String> hardLinkFileMap = new HashMap<String, String>();
9087
for (int i = 0; i < 10; i++) {
9188
String fileName = "hardlink-" + i;
9289
File dummyFile = new File(tmpDir.toFile(), fileName);
9390
Files.write(dummyFile.toPath(), "dummy".getBytes(StandardCharsets.UTF_8));
9491
omdbArchiver.getFilesToWriteIntoTarball().put(fileName, dummyFile);
95-
hardLinkFileMap.put(dummyFile.getAbsolutePath(), dummyFile.getName());
92+
omdbArchiver.recordHardLinkMapping(dummyFile.getAbsolutePath(),
93+
dummyFile.getName());
9694
}
97-
omdbArchiver.setHardLinkFileMap(hardLinkFileMap);
9895

9996
File tarFile = new File(folder.toFile(), "test-archive.tar");
10097
OzoneConfiguration conf = new OzoneConfiguration();

0 commit comments

Comments
 (0)