Copilot commented on code in PR #13098:
URL: https://github.com/apache/gluten/pull/13098#discussion_r4082821348


##########
gluten-core/src/test/scala/org/apache/spark/util/SparkDirectoryUtilSuite.scala:
##########
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.spark.util
+
+import org.apache.spark.SparkConf
+
+import org.scalatest.funsuite.AnyFunSuite
+
+import java.io.File
+import java.nio.file.Files
+
+class SparkDirectoryUtilSuite extends AnyFunSuite {
+
+  test("namespace fails fast naming the configured local dirs when none can be 
created") {
+    // A regular file cannot host a local directory, so directory creation 
fails
+    // for every configured root.
+    val notADir = Files.createTempFile("gluten-not-a-dir", ".tmp")
+    notADir.toFile.deleteOnExit()
+    val conf = new SparkConf()
+      .set("spark.master", "local[1]")
+      .set("spark.local.dirs", notADir.toAbsolutePath.toString)
+      .set("spark.local.dir", notADir.toAbsolutePath.toString)
+    SparkDirectoryUtil.init(conf)
+    val exception = intercept[IllegalStateException] {
+      SparkDirectoryUtil.get().namespace("test-namespace")
+    }

Review Comment:
   `SparkDirectoryUtil.init(conf)` mutates a process-wide singleton that (per 
the provided `init(roots: Array[String])` logic) cannot be reinitialized with 
different roots. This test can become order-dependent/flaky or break other 
suites that also initialize `SparkDirectoryUtil` with different local dirs. 
Consider isolating the test by (mandatory) adding a test-only reset/harness 
(e.g., `private[spark] def resetForTests()` or a scoped initializer that 
restores the prior state in `finally`), or (alternative) introducing a 
package-visible factory for constructing a fresh `SparkDirectoryUtil` instance 
for tests without touching global state.



##########
gluten-core/src/main/scala/org/apache/spark/util/SparkDirectoryUtil.scala:
##########
@@ -98,16 +106,22 @@ class Namespace(private val parents: Array[File], private 
val name: String) {
       path.toFile
   }
 
+  if (all.isEmpty) {
+    // all.isEmpty iff parents.isEmpty (map preserves length), so the 
actionable
+    // detail here is that no parent directories were provided.
+    throw new IllegalStateException(
+      s"No available Gluten local directory in namespace '$name': " +
+        s"no parent directories were provided")
+  }
+
   private val cycleLooper = Stream.continually(all).flatten.toIterator

Review Comment:
   `Stream` is deprecated in Scala 2.13+ and the infinite 
`Stream.continually(...).flatten` round-robin pattern is harder to reason about 
(and is the root cause of the original hang when `all` is empty). Even with the 
new guard, consider switching to a simpler index-based round-robin (e.g., 
`private var nextIdx = 0; val subDir = all(nextIdx); nextIdx = (nextIdx + 1) % 
all.length`) to avoid deprecated APIs and make iteration behavior more explicit.



##########
gluten-core/src/test/scala/org/apache/spark/util/SparkDirectoryUtilSuite.scala:
##########
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.spark.util
+
+import org.apache.spark.SparkConf
+
+import org.scalatest.funsuite.AnyFunSuite
+
+import java.io.File
+import java.nio.file.Files
+
+class SparkDirectoryUtilSuite extends AnyFunSuite {
+
+  test("namespace fails fast naming the configured local dirs when none can be 
created") {
+    // A regular file cannot host a local directory, so directory creation 
fails
+    // for every configured root.
+    val notADir = Files.createTempFile("gluten-not-a-dir", ".tmp")
+    notADir.toFile.deleteOnExit()
+    val conf = new SparkConf()
+      .set("spark.master", "local[1]")
+      .set("spark.local.dirs", notADir.toAbsolutePath.toString)
+      .set("spark.local.dir", notADir.toAbsolutePath.toString)
+    SparkDirectoryUtil.init(conf)
+    val exception = intercept[IllegalStateException] {
+      SparkDirectoryUtil.get().namespace("test-namespace")
+    }
+    assert(exception.getMessage.contains("test-namespace"))
+    assert(exception.getMessage.contains(notADir.toAbsolutePath.toString))
+  }
+
+  test("Namespace rejects an empty parent list") {
+    val exception = intercept[IllegalStateException] {
+      new Namespace(Array.empty[File], "test-namespace")
+    }
+    assert(exception.getMessage.contains("test-namespace"))
+  }
+
+  test("Namespace creates child directories under an available root") {
+    val root = Files.createTempDirectory("gluten-usable-root").toFile
+    try {
+      val namespace = new Namespace(Array(root), "test-namespace")
+      val child = namespace.mkChildDirRoundRobin("child-dir")
+      assert(child.exists)

Review Comment:
   This assertion only checks existence; it would still pass if a regular file 
exists at that path. To better validate behavior, assert that 
`child.isDirectory` (and optionally that `child.getParentFile == root` or that 
the resolved path starts with `root.getCanonicalPath`) to ensure a directory 
was created under the expected root.



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