sarutak commented on code in PR #59061:
URL: https://github.com/apache/spark/pull/59061#discussion_r4439038787
##########
core/src/main/scala/org/apache/spark/util/Utils.scala:
##########
@@ -223,19 +224,52 @@ private[spark] object Utils
writeByteBufferImpl(bb, out.write)
}
+ // fromString returns a mutable HashSet; wrap it so no future caller can
mutate the shared set.
+ private val chmod700Perms =
+ Collections.unmodifiableSet(PosixFilePermissions.fromString("rwx------"))
+
/**
* JDK equivalent of `chmod 700 file`.
*
+ * On POSIX filesystems this is a single `chmod` call, which is atomic:
concurrent calls on
+ * the same path (e.g. Workers sharing a local root dir in local-cluster
mode) cannot
+ * interleave into a broken permission set, unlike the `File.set*` fallback
below, each call
+ * of which is a non-atomic stat-then-chmod.
+ *
* @param file the file whose permissions will be modified
* @return true if the permissions were successfully changed, false
otherwise.
*/
def chmod700(file: File): Boolean = {
- file.setReadable(false, false) &&
- file.setReadable(true, true) &&
- file.setWritable(false, false) &&
- file.setWritable(true, true) &&
- file.setExecutable(false, false) &&
- file.setExecutable(true, true)
+ if (file.getPath.isEmpty) {
+ // The NIO empty path resolves to the process working directory; the
File.set* sequence
+ // returned false for it, so keep that behavior rather than chmod-ing
the cwd.
+ return false
+ }
+ try {
+ Files.setPosixFilePermissions(file.toPath, chmod700Perms)
+ true
+ } catch {
+ case _: UnsupportedOperationException =>
+ // Non-POSIX filesystem (e.g. Windows): best-effort, non-atomic
fallback. Remove all
Review Comment:
I think the non-short-circuit `&` you pushed is the right choice, and `||`
would not work here. `||` stops at the first call that returns true. On
Windows, the native `WinNTFileSystem.setPermission` returns `enable` as is for
READ and EXECUTE ([WinNTFileSystem_md.c
L473-L476](https://github.com/openjdk/jdk17u/blob/0b592b7f04aae6cec666345be37c1456845e6e0d/src/java.base/windows/native/libjava/WinNTFileSystem_md.c#L473-L476)
in JDK 17), so `setReadable(true, true)` always returns true. For WRITE it
skips directories and returns false
([L495-L496](https://github.com/openjdk/jdk17u/blob/0b592b7f04aae6cec666345be37c1456845e6e0d/src/java.base/windows/native/libjava/WinNTFileSystem_md.c#L495-L496)),
and on a regular file it sets or clears `FILE_ATTRIBUTE_READONLY`
([L498-L503](https://github.com/openjdk/jdk17u/blob/0b592b7f04aae6cec666345be37c1456845e6e0d/src/java.base/windows/native/libjava/WinNTFileSystem_md.c#L498-L503)).
So for a directory `||` would return true without changing anyth
ing, and for a regular file it would stop right after `setWritable(false,
false)` and leave the file read-only. With `&`, every step runs and the result
is true only if all of them succeed, which matches the `@return`. For
directories, which all current callers pass, `&` behaves exactly like the old
`&&`: no writes, and the result is false.
##########
core/src/main/scala/org/apache/spark/util/Utils.scala:
##########
@@ -223,19 +224,53 @@ private[spark] object Utils
writeByteBufferImpl(bb, out.write)
}
+ // fromString returns a mutable HashSet; wrap it so no future caller can
mutate the shared set.
+ private val chmod700Perms =
+ Collections.unmodifiableSet(PosixFilePermissions.fromString("rwx------"))
+
/**
* JDK equivalent of `chmod 700 file`.
*
+ * On POSIX filesystems this is a single `chmod` call, which is atomic:
concurrent calls on
+ * the same path (e.g. Workers sharing a local root dir in local-cluster
mode) cannot
Review Comment:
Is there a path that actually runs `chmod700` on the same path concurrently?
As far as I can tell, every caller chmods a directory it has just created under
a unique name: `executor-<UUID>` in `Worker`, `spark-<UUID>` in
`getOrCreateLocalRootDirsImpl` (and `getOrCreateLocalRootDirs` caches the
result under `this.synchronized`, so Workers in the same JVM share one root but
chmod it only once), `appstore-<UUID>` in `HistoryServerDiskManager`, and in
`KVUtils` either a `listing.*db` under a fresh `spark-ui-<UUID>` (live UI) or
under the History Server's own store path (one per History Server). If that is
right, the "e.g." here, the test comment at UtilsSuite.scala line 578, and the
"Why" section describe a race that current callers cannot hit. The change is
still worth it as described in the JIRA (one atomic call instead of six), so
maybe phrase it as "concurrent calls on the same path cannot interleave"
without the local-cluster example, unless there is a concrete case I am missing.
##########
core/src/main/scala/org/apache/spark/util/Utils.scala:
##########
@@ -223,19 +224,53 @@ private[spark] object Utils
writeByteBufferImpl(bb, out.write)
}
+ // fromString returns a mutable HashSet; wrap it so no future caller can
mutate the shared set.
+ private val chmod700Perms =
+ Collections.unmodifiableSet(PosixFilePermissions.fromString("rwx------"))
+
/**
* JDK equivalent of `chmod 700 file`.
*
+ * On POSIX filesystems this is a single `chmod` call, which is atomic:
concurrent calls on
+ * the same path (e.g. Workers sharing a local root dir in local-cluster
mode) cannot
+ * interleave into a broken permission set, unlike the `File.set*` fallback
below, each call
+ * of which is a non-atomic stat-then-chmod. The POSIX call writes exactly
the nine rwx bits,
+ * so it also clears setuid, setgid, and sticky bits; the fallback preserves
them.
Review Comment:
Thanks for adding this. Nit: the fallback only runs where there is no POSIX
view (in practice Windows), so there are no such bits there to preserve. What
the reader needs is the contrast with the previous implementation, e.g. "...,
which the previous `File.set*` implementation kept." Similarly for the fallback
comment at line 254: on Windows, `setReadable`/`setExecutable` do nothing
([WinNTFileSystem_md.c
L473-L476](https://github.com/openjdk/jdk17u/blob/0b592b7f04aae6cec666345be37c1456845e6e0d/src/java.base/windows/native/libjava/WinNTFileSystem_md.c#L473-L476)
in JDK 17) and `setWritable` ignores directories
([L495-L496](https://github.com/openjdk/jdk17u/blob/0b592b7f04aae6cec666345be37c1456845e6e0d/src/java.base/windows/native/libjava/WinNTFileSystem_md.c#L495-L496)),
so for a directory (every current caller) the fallback changes nothing and
returns false. Saying that directly would be clearer than "revoke ... where
supported".
##########
core/src/test/scala/org/apache/spark/util/UtilsSuite.scala:
##########
@@ -573,6 +574,71 @@ class UtilsSuite extends SparkFunSuite with
ResetSystemProperties {
assert(!Utils.createDirectory(scenario8))
}
+ test("chmod700 leaves rwx------ under concurrent calls on the same path") {
+ // Workers in local-cluster mode share local root dirs and can chmod the
same
+ // per-application directory concurrently; the stat-then-chmod File.set*
sequence
+ // interleaves into broken permission sets, so chmod700 must be atomic.
Check both
+ // the final mode and samples taken during the race: with an atomic chmod
every sample
+ // is the initial or the final mode, and anything else is a non-atomic
interleaving.
+
assume(FileSystems.getDefault.supportedFileAttributeViews().contains("posix"))
+ withTempDir { dir =>
+ val initialPerms = PosixFilePermissions.fromString("rwxr-xr-x")
+ val expectedPerms = PosixFilePermissions.fromString("rwx------")
+ // Start from a permissive mode so the assertions cannot pass vacuously
under umask 077.
+ Files.setPosixFilePermissions(dir.toPath, initialPerms)
+ @volatile var observing = true
+ // Written only by the observer and read after observer.join(), which
synchronizes.
+ var brokenStates = Set.empty[java.util.Set[PosixFilePermission]]
+ var observerError: Throwable = null
+ val observer = new Thread(() => {
+ try {
+ while (observing) {
+ val perms = Files.getPosixFilePermissions(dir.toPath)
+ if (perms != initialPerms && perms != expectedPerms) {
+ brokenStates += perms
+ }
+ }
+ } catch {
+ case t: Throwable => observerError = t
+ }
+ })
+ observer.start()
+ try {
+ val numThreads = 4
+ (1 to 10).foreach { _ =>
+ // Align the thread starts to widen the race window.
+ val barrier = new CyclicBarrier(numThreads)
+ val threads = (1 to numThreads).map { _ =>
+ new Thread(() => {
+ barrier.await()
+ (1 to 20).foreach(_ => Utils.chmod700(dir))
Review Comment:
Nit: no test checks that `chmod700` returns `true` on POSIX; the other two
tests only check `false`. A single `assert(Utils.chmod700(dir))` on the main
thread (e.g. before the race) would cover it. An assert inside these worker
threads would not fail the test.
##########
core/src/main/scala/org/apache/spark/util/Utils.scala:
##########
@@ -223,19 +224,53 @@ private[spark] object Utils
writeByteBufferImpl(bb, out.write)
}
+ // fromString returns a mutable HashSet; wrap it so no future caller can
mutate the shared set.
Review Comment:
Nit: `fromString` actually builds an `EnumSet` ([PosixFilePermissions.java
L129](https://github.com/openjdk/jdk17u/blob/0b592b7f04aae6cec666345be37c1456845e6e0d/src/java.base/share/classes/java/nio/file/attribute/PosixFilePermissions.java#L129)
in JDK 17). My earlier comment said `HashSet`, sorry about that. "returns a
mutable set" would be accurate.
--
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]