holdenk commented on code in PR #58619:
URL: https://github.com/apache/spark/pull/58619#discussion_r4580861492


##########
core/src/main/scala/org/apache/spark/deploy/ExternalShuffleService.scala:
##########
@@ -83,18 +83,26 @@ class ExternalShuffleService(sparkConf: SparkConf, 
securityManager: SecurityMana
 
   /** Create a new shuffle block handler. Factored out for subclasses to 
override. */
   protected def newShuffleBlockHandler(conf: TransportConf): 
ExternalBlockHandler = {
-    // Constrain registered localDirs to the configured local directories.
+    // Constrain registered localDirs to the configured local directories and, 
when
+    // spark.shuffle.service.requireAppScopedLocalDirs is enabled, to the 
registering
+    // application's own per-app directory 
(spark-*/executor-*/<url-encoded-appId>, with an
+    // optional blockmgr-* child).

Review Comment:
   Updated the wiring comment in `ExternalShuffleService` to name both accepted 
layouts and point at `LocalDirValidator` for the exact rule, and updated the 
two `YarnShuffleServiceSuite` comments (`nmLocalDir` and `appLocalDir`) to 
match, in aff941b1dc2.
   
   ---
   <sub>🤖 Posted by an AI agent (Claude Code · skill `none`) on behalf of 
@holdenk.</sub>
   <!-- agent-attribution v1: agent=claude-code; skill=none; operator=holdenk 
-->
   



##########
core/src/test/scala/org/apache/spark/deploy/ExternalShuffleServiceLocalDirsSuite.scala:
##########
@@ -49,4 +54,105 @@ class ExternalShuffleServiceLocalDirsSuite extends 
SparkFunSuite {
       Utils.deleteRecursively(contained)
     }
   }
+
+  gridTest("requireAppScopedLocalDirs restricts localDirs to the registering 
app's directory")(
+      Seq(false, true)) { dbEnabled =>
+    withTempDir { root =>
+      val sparkConf = new SparkConf()
+        .set(SHUFFLE_SERVICE_ENABLED, true)
+        .set(SHUFFLE_SERVICE_DB_ENABLED, dbEnabled)
+        .set(SHUFFLE_SERVICE_REQUIRE_APP_SCOPED_LOCAL_DIRS, true)
+        .set("spark.local.dir", root.getAbsolutePath)
+      val service = new ExternalShuffleService(sparkConf, new 
SecurityManager(sparkConf))
+      val handler = service.getBlockHandler
+
+      val appId = s"app-${UUID.randomUUID()}"
+      val otherAppId = s"app-${UUID.randomUUID()}"
+      // The layout the Worker creates: <root>/spark-*/executor-*/<appId>.
+      val workerRoot = new File(root, s"spark-${UUID.randomUUID()}")
+      val executorRoot = new File(workerRoot, s"executor-${UUID.randomUUID()}")
+      val appScoped = new File(executorRoot, appId)
+      val otherAppScoped = new File(executorRoot, otherAppId)
+      // The pre-upgrade Worker layout has blockmgr in place of the app leaf.
+      val unscoped = new File(executorRoot, s"blockmgr-${UUID.randomUUID()}")
+      assert(appScoped.mkdirs() && otherAppScoped.mkdirs() && 
unscoped.mkdirs())
+      try {
+        // A localDir inside the registering application's own directory is 
accepted.
+        handler.validateLocalDirs(Array(appScoped.getAbsolutePath), appId)
+
+        // A directory under a different application id is rejected even 
though it is contained
+        // under a configured root.
+        intercept[IllegalArgumentException] {
+          handler.validateLocalDirs(Array(otherAppScoped.getAbsolutePath), 
appId)
+        }
+
+        // A contained directory without the appId as a path segment (what a 
Worker without
+        // the per-app layout would report) is rejected.
+        intercept[IllegalArgumentException] {
+          handler.validateLocalDirs(Array(unscoped.getAbsolutePath), appId)
+        }
+
+        // Root containment is still enforced with app scoping on.
+        intercept[IllegalArgumentException] {
+          val outside = new File(root.getParentFile, 
s"outside/appcache/$appId/blockmgr-x")
+          handler.validateLocalDirs(Array(outside.getAbsolutePath), appId)
+        }
+      } finally {
+        handler.close()
+        Utils.deleteRecursively(workerRoot)
+        Utils.deleteRecursively(unscoped)
+      }
+    }
+  }
+
+  test("local-cluster executors register app-scoped local dirs end to end") {
+    val root = Utils.createTempDir(namePrefix = "ess-e2e-root")
+    val serviceConf = new SparkConf()
+      .set(SHUFFLE_SERVICE_ENABLED, true)
+      .set(SHUFFLE_SERVICE_DB_ENABLED, false)
+      .set(SHUFFLE_SERVICE_REQUIRE_APP_SCOPED_LOCAL_DIRS, true)
+      .set(SHUFFLE_SERVICE_PORT, 0)
+      .set("spark.local.dir", root.getAbsolutePath)
+    val service = new ExternalShuffleService(serviceConf, new 
SecurityManager(serviceConf))
+    var sc: SparkContext = null
+    try {
+      service.start()
+      val conf = new SparkConf()
+        .setAppName("app-scoped-local-dirs-e2e")
+        .setMaster("local-cluster[2,1,1024]")
+        .set(SHUFFLE_SERVICE_ENABLED, true)
+        .set(SHUFFLE_SERVICE_PORT, service.boundPort)
+        .set("spark.local.dir", root.getAbsolutePath)
+      sc = new SparkContext(conf)
+      val appId = sc.applicationId
+      // One core per worker, so the application's two executors land on 
different Workers.
+      // A rejected shuffle registration makes an executor exit after driver 
registration,
+      // so the shuffle job below fails even if the driver briefly reports 
both executors.
+      TestUtils.waitUntilExecutorsUp(sc, 2, 60000)
+      assert(sc.parallelize(1 to 100, 4).map(i => (i % 10, 
i)).groupByKey().count() === 10)
+
+      val appDirs = root.listFiles().toSeq
+        .filter(d => d.isDirectory && d.getName.startsWith("spark-"))
+        .flatMap(_.listFiles())
+        .filter(d => d.isDirectory && d.getName.startsWith("executor-"))
+        .flatMap(_.listFiles())
+        .filter(d => d.isDirectory && d.getName == appId)
+      // Each executor has its own random directory above the application leaf.
+      assert(appDirs.length == 2)
+      assert(appDirs.map(_.getParentFile.getName).distinct.length == 2)
+      // The registered dirs satisfy the app-scoping check for this 
application only.

Review Comment:
   Added tests for the standalone shape in aff941b1dc2. 
`LocalDirValidatorSuite` now covers another application's `blockmgr-*` 
directory, a path below `blockmgr-*`, a non-`blockmgr-*` child of the 
application leaf, and leaves under a non-`spark-`/non-`executor-` prefix. The 
grid test validates the `blockmgr-*` child (and another application's) and its 
comment now matches the assertions. The end-to-end test checks that every 
Worker leaf holds a `blockmgr-*` directory and validates those directories for 
the application and for another application ID.
   
   ---
   <sub>🤖 Posted by an AI agent (Claude Code · skill `none`) on behalf of 
@holdenk.</sub>
   <!-- agent-attribution v1: agent=claude-code; skill=none; operator=holdenk 
-->
   



##########
common/network-shuffle/src/main/java/org/apache/spark/network/shuffle/LocalDirValidator.java:
##########
@@ -80,15 +84,29 @@ private void validateContained(String localDir, String 
appId) {
         "localDirs entry is not under any of the service's local directories: 
" + localDir);
     }
     if (requireAppScopedLocalDirs &&
-        (appId == null || appId.isEmpty() || !pathContainsSegment(canonical, 
appId))) {
+        (appId == null || appId.isEmpty() || 
allowedLocalDirRoots.stream().noneMatch(
+          root -> isStandaloneAppDir(root, canonical, appId) ||

Review Comment:
   The class Javadoc, the config doc and `docs/spark-standalone.md` now say 
that both layouts are accepted by either service (e679da45a2a). Making the rule 
depend on the service would need a layout argument threaded through 
`ExternalBlockHandler`, which is beyond this PR, so the documentation states 
the shared rule instead.
   
   ---
   <sub>🤖 Posted by an AI agent (Claude Code · skill `none`) on behalf of 
@holdenk.</sub>
   <!-- agent-attribution v1: agent=claude-code; skill=none; operator=holdenk 
-->
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to