Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -592,4 +592,47 @@ public void shouldIdentifyCMURIs() {
assertFalse(ReplChangeManager
.isCMFileUri(new Path("/somepath/adir/", "ab.jar")));
}

/**
* Regression test for stale CM-root cache bug: when the CM root directory is deleted from
* HDFS (e.g. by DROP DATABASE CASCADE) while the in-memory encryptionZoneToCmrootMapping
* still holds the cached path, getCmRoot() must detect the missing directory and recreate it.
*/
@Test
public void testRecycleAfterCmRootDeletedFromHdfs() throws Exception {
Path dirDb = new Path(warehouse.getWhRoot(), "dbStaleCmRoot");
fs.delete(dirDb, true);
fs.mkdirs(dirDb);

Path sourceFile1 = new Path(dirDb, "file1");
createFile(sourceFile1, "content1");

ReplChangeManager cm = ReplChangeManager.getInstance(hiveConf);

// First recycle: CM root exists, cache is populated, file is moved successfully.
int ret = cm.recycle(sourceFile1, RecycleType.MOVE, false);
Assert.assertEquals(1, ret);
Assert.assertFalse(fs.exists(sourceFile1));

Path cmRootPath = new Path(cmroot);
Assert.assertTrue("CM root should exist after first recycle", fs.exists(cmRootPath));

// Simulate DROP DATABASE CASCADE removing .cmroot from HDFS.
fs.delete(cmRootPath, true);
Assert.assertFalse("CM root should be deleted from HDFS", fs.exists(cmRootPath));

// Do NOT reset ReplChangeManager — preserve stale in-memory cache.
Path sourceFile2 = new Path(dirDb, "file2");
createFile(sourceFile2, "content2");
String file2Checksum = ReplChangeManager.checksumFor(sourceFile2, fs);

// Second MOVE recycle: cache hit must detect missing CM root, recreate it, and succeed.
ret = cm.recycle(sourceFile2, RecycleType.MOVE, false);
Assert.assertEquals(1, ret);
Assert.assertFalse("Source file should be removed after successful recycle", fs.exists(sourceFile2));
Assert.assertTrue("CM root should be recreated on cache hit", fs.exists(cmRootPath));

Path cmFile2Path = ReplChangeManager.getCMPath(hiveConf, sourceFile2.getName(), file2Checksum, cmroot);
Assert.assertTrue("Recycled file should exist under recreated CM root", fs.exists(cmFile2Path));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -575,7 +575,7 @@
}

@VisibleForTesting
Path getCmRoot(Path path) throws IOException {

Check failure on line 578 in standalone-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/metastore/ReplChangeManager.java

View check run for this annotation

SonarQubeCloud / SonarCloud Code Analysis

Refactor this method to reduce its Cognitive Complexity from 16 to the 15 allowed.

See more on https://sonarcloud.io/project/issues?id=apache_hive&issues=AaAeu5ReB1rsUSwAewBM&open=AaAeu5ReB1rsUSwAewBM&pullRequest=6710
Path cmroot = null;
//Default path if hive.repl.cm dir is encrypted
String cmrootDir = fallbackNonEncryptedCmRootDir;
Expand All @@ -590,6 +590,14 @@
}
if (encryptionZoneToCmrootMapping.containsKey(encryptionZonePath)) {
cmroot = new Path(encryptionZoneToCmrootMapping.get(encryptionZonePath));
FileSystem cmFs = cmroot.getFileSystem(conf);
if (!cmFs.exists(cmroot)) {
synchronized (instance) {
if (!cmFs.exists(cmroot)) {
createCmRoot(cmroot);
}
}
}
} else {
cmroot = new Path(cmrootDir);
synchronized (instance) {
Expand Down
Loading