Skip to content
This repository was archived by the owner on Jan 3, 2023. It is now read-only.

Commit 24d00f7

Browse files
authored
Solve #1721, fix namespace mismatch caused by unlink (#1717)
Fix an issue in InotifyEventApplier.getUnlinkSql, which may cause namespace not mismatch in file table.
1 parent 23de3bc commit 24d00f7

File tree

2 files changed

+51
-13
lines changed

2 files changed

+51
-13
lines changed

smart-hadoop-support/smart-hadoop/src/main/java/org/smartdata/hdfs/metric/fetcher/InotifyEventApplier.java

Lines changed: 47 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -301,17 +301,54 @@ private List<String> getAppendSql(Event.AppendEvent appendEvent) {
301301
}
302302

303303
private List<String> getUnlinkSql(Event.UnlinkEvent unlinkEvent) throws MetaStoreException {
304-
List<FileInfo> fileInfos = metaStore.getFilesByPrefix(unlinkEvent.getPath());
305-
for (FileInfo fileInfo:fileInfos) {
306-
if (fileInfo.isdir()) {
307-
continue;
308-
}
309-
if (inBackup(unlinkEvent.getPath())) {
310-
FileDiff fileDiff = new FileDiff(FileDiffType.DELETE);
311-
fileDiff.setSrc(unlinkEvent.getPath());
312-
metaStore.insertFileDiff(fileDiff);
304+
// delete root, i.e., /
305+
String root = "/";
306+
if (root.equals(unlinkEvent.getPath())) {
307+
LOG.warn("Deleting root directory!!!");
308+
insertDeleteDiff(root, true);
309+
return Arrays.asList(
310+
String.format("DELETE FROM file WHERE path like '%s%%'", root));
311+
}
312+
FileInfo fileInfo = metaStore.getFile(unlinkEvent.getPath());
313+
if (fileInfo == null) return Arrays.asList();
314+
if (fileInfo.isdir()) {
315+
insertDeleteDiff(unlinkEvent.getPath(), true);
316+
// delete all files in this dir from file table
317+
return Arrays.asList(
318+
String.format("DELETE FROM file WHERE path LIKE '%s/%%';", unlinkEvent.getPath()),
319+
String.format("DELETE FROM file WHERE path = '%s';", unlinkEvent.getPath()));
320+
} else {
321+
insertDeleteDiff(unlinkEvent.getPath(), false);
322+
// delete file in file table
323+
return Arrays.asList(
324+
String.format("DELETE FROM file WHERE path = '%s';", unlinkEvent.getPath()));
325+
}
326+
}
327+
328+
private void insertDeleteDiff(String path, boolean isDir) throws MetaStoreException {
329+
if (isDir) {
330+
List<FileInfo> fileInfos = metaStore.getFilesByPrefix(path);
331+
for (FileInfo fileInfo : fileInfos) {
332+
// recursively on dir
333+
if (fileInfo.isdir()) {
334+
if (path.equals(fileInfo.getPath())) {
335+
continue;
336+
}
337+
insertDeleteDiff(fileInfo.getPath(), true);
338+
continue;
339+
}
340+
insertDeleteDiff(fileInfo.getPath());
313341
}
342+
} else {
343+
insertDeleteDiff(path);
344+
}
345+
}
346+
347+
private void insertDeleteDiff(String path) throws MetaStoreException {
348+
if (inBackup(path)) {
349+
FileDiff fileDiff = new FileDiff(FileDiffType.DELETE);
350+
fileDiff.setSrc(path);
351+
metaStore.insertFileDiff(fileDiff);
314352
}
315-
return Arrays.asList(String.format("DELETE FROM file WHERE path LIKE '%s%%';", unlinkEvent.getPath()));
316353
}
317354
}

smart-hadoop-support/smart-hadoop/src/test/java/org/smartdata/hdfs/metric/fetcher/TestInotifyEventApplier.java

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -165,10 +165,11 @@ public void testApplier() throws Exception {
165165

166166
Event unlink = new Event.UnlinkEvent.Builder().path("/").timestamp(6).build();
167167
applier.apply(new Event[]{unlink});
168-
Assert.assertFalse(metaStore.getFile().size() > 0);
169-
168+
Thread.sleep(1200);
169+
Assert.assertEquals(metaStore.getFile().size(), 0);
170+
System.out.println("Files in table " + metaStore.getFile().size());
170171
List<FileDiff> fileDiffList = metaStore.getPendingDiff();
171-
Assert.assertTrue(fileDiffList.size() == 3);
172+
Assert.assertTrue(fileDiffList.size() == 4);
172173
}
173174

174175
@Test

0 commit comments

Comments
 (0)