spark-test commented on code in PR #58619:
URL: https://github.com/apache/spark/pull/58619#discussion_r4576863234


##########
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:
   **Nit (P3):** This comment lists only the Worker layout, but 
`validateContained` accepts a contained path below `appcache/<appId>` in either 
service, so with the flag on and `spark.local.dir=/data` this service accepts 
`/data/x/appcache/<appId>/y`. The class Javadoc, the config doc and 
`docs/spark-standalone.md` already say both layouts are accepted; a reader of 
this wiring site would instead conclude that YARN-shaped paths are rejected 
here.
   
   ```suggestion
       // Constrain registered localDirs to the configured local directories 
and, when
       // spark.shuffle.service.requireAppScopedLocalDirs is enabled, to the 
registering
       // application's own directory: spark-*/executor-*/<url-encoded-appId> 
with an optional
       // blockmgr-* child, or below appcache/<appId> (see LocalDirValidator 
for the exact rule).
   ```
   
   The same rule change leaves two unchanged comments in 
`YarnShuffleServiceSuite` stale. The one above `nmLocalDir` says an accepted 
local dir only has to contain the application ID as a path segment, and the one 
above `appLocalDir` gives that as the reason its paths pass, but 
`usercache/u/appcache/app1/blockmgr-x/app0` is now rejected for `app0`. Because 
the YARN service accepts the standalone shape too, the `nmLocalDir` comment 
should name both layouts (or point to `LocalDirValidator`), while the 
`appLocalDir` comment can say its paths pass because they lie below 
`appcache/<appId>`.
   
   This extends an [existing 
discussion](https://github.com/apache/spark/pull/58619#discussion_r4508227049).
   
   **Verification:**
   - **Inspection:** The comment where the standalone shuffle service wires the 
flag and the YARN shuffle service suite's comment on the NodeManager local dir 
each name both accepted layouts, the Worker's 
spark-*/executor-*/<url-encoded-appId> leaf with an optional blockmgr-* child 
and any path below appcache/<appId>, or refer to LocalDirValidator for the 
rule; the suite's comment on its app-local-dir helper gives being below 
appcache/<appId> as the reason its paths are accepted; and no comment in the 
YARN shuffle service suite says that containing the application ID as a path 
segment is enough for a local dir to be accepted.



##########
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:
   **Nit (P3):** This loop validates the Worker leaves 
`<root>/spark-*/executor-*/<appId>`, but the executors register the 
`blockmgr-*` directories below them (`BlockManager` registers 
`diskBlockManager.localDirsString`), so what they registered is never checked 
against another application ID. No other test covers that shape: every flag-on 
rejection case here and in `LocalDirValidatorSuite` is a three-name path with 
the wrong leaf, an out-of-root path, a YARN path, or 
`spark-random/executor-random/other/app%3A0%2F1`, which breaks both the leaf 
comparison and the `blockmgr-` test. Going by those fixtures and 
`isStandaloneAppDir`, each of these rewrites would leave both suites green:
   
   - skipping the application-ID comparison for four-name paths, which accepts 
`spark-a/executor-b/appB/blockmgr-c` for `appA`, the mis-registration the flag 
exists to reject;
   - allowing more than four names, which accepts `<leaf>/blockmgr-c/sub`;
   - dropping the `blockmgr-`, `spark-` or `executor-` test, which accepts 
`<leaf>/other`, `foo/executor-b/<appId>` or `spark-a/foo/<appId>`.
   
   The grid test's first comment (line 80) has a smaller version of the same 
gap: it says a localDir inside the application's own directory is accepted, 
while its assertion validates the leaf itself, and only a `blockmgr-*` child of 
the leaf would be accepted.
   
   **Verification:**
   - **Regression:** With app scoping on, another application's blockmgr-* 
directory below a Worker executor directory is rejected, and the case fails if 
the application-ID comparison is skipped for entries that end in a blockmgr-* 
directory.
   - **Regression:** A path below an application's blockmgr-* directory, a 
non-blockmgr child of the application leaf, and a leaf under a directory 
without the spark- or executor- prefix are each rejected, and each case fails 
if its own constraint stops being enforced while the rest of the path keeps the 
standalone shape.
   - **Behavior:** In the two-Worker end-to-end run with app scoping on, the 
shuffle succeeds, every Worker leaf holds at least one blockmgr-* directory, 
and those directories, the ones the executors registered, are accepted for the 
application and rejected for another application ID.



-- 
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