shrirangmhalgi commented on code in PR #56332:
URL: https://github.com/apache/spark/pull/56332#discussion_r3364783855


##########
sql/core/src/test/scala/org/apache/spark/sql/execution/datasources/v2/state/WriteProtectedLocalFileSystem.scala:
##########
@@ -0,0 +1,348 @@
+/*
+ * 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.sql.execution.datasources.v2.state
+
+import java.net.URI
+import java.nio.file.AccessDeniedException
+import java.util
+import java.util.concurrent.atomic.AtomicReference
+
+import scala.jdk.CollectionConverters._
+
+import org.apache.hadoop.conf.Configuration
+import org.apache.hadoop.fs.{CreateFlag, DelegateToFileSystem, 
FSDataOutputStream, FsServerDefaults, Options, Path}
+import org.apache.hadoop.fs.local.LocalConfigKeys
+import org.apache.hadoop.fs.permission.FsPermission
+import org.apache.hadoop.util.Progressable
+import org.scalatest.BeforeAndAfterEach
+
+import org.apache.spark.SparkConf
+import org.apache.spark.sql.streaming.StreamTest
+
+/**
+ * Write protection for the Hadoop FileSystem API (`fs.file.impl`).
+ *
+ * Extends [[org.apache.spark.DebugFilesystem]] so it replaces DebugFilesystem
+ * without losing open-connection tracking. Every write entry point is 
overridden to deny
+ * the operation when the target path is protected; reads are left untouched.
+ */
+class WriteProtectedLocalFileSystem extends org.apache.spark.DebugFilesystem {
+
+  override def create(
+      f: Path,
+      permission: FsPermission,
+      overwrite: Boolean,
+      bufferSize: Int,
+      replication: Short,
+      blockSize: Long,
+      progress: Progressable): FSDataOutputStream = {
+    if (WriteProtectedPaths.isProtected(f)) WriteProtectedPaths.deny("create", 
f)
+    super.create(f, permission, overwrite, bufferSize, replication, blockSize, 
progress)
+  }
+
+  override def create(
+      f: Path,
+      permission: FsPermission,
+      flags: util.EnumSet[CreateFlag],
+      bufferSize: Int,
+      replication: Short,
+      blockSize: Long,
+      progress: Progressable,
+      checksumOpt: Options.ChecksumOpt): FSDataOutputStream = {
+    if (WriteProtectedPaths.isProtected(f)) WriteProtectedPaths.deny("create", 
f)
+    super.create(
+      f, permission, flags, bufferSize, replication, blockSize, progress, 
checksumOpt)
+  }
+
+  override def createNonRecursive(
+      f: Path,
+      permission: FsPermission,
+      flags: util.EnumSet[CreateFlag],
+      bufferSize: Int,
+      replication: Short,
+      blockSize: Long,
+      progress: Progressable): FSDataOutputStream = {
+    if (WriteProtectedPaths.isProtected(f)) 
WriteProtectedPaths.deny("createNonRecursive", f)
+    super.createNonRecursive(f, permission, flags, bufferSize, replication, 
blockSize, progress)
+  }
+
+  override def append(f: Path, bufferSize: Int, progress: Progressable): 
FSDataOutputStream = {
+    if (WriteProtectedPaths.isProtected(f)) WriteProtectedPaths.deny("append", 
f)
+    super.append(f, bufferSize, progress)
+  }
+
+  override def rename(src: Path, dst: Path): Boolean = {
+    // A rename out of a protected path is also a write to that path -- check 
both ends.
+    if (WriteProtectedPaths.isProtected(src)) WriteProtectedPaths.deny("rename 
from", src)
+    if (WriteProtectedPaths.isProtected(dst)) WriteProtectedPaths.deny("rename 
to", dst)
+    super.rename(src, dst)
+  }
+
+  override def delete(f: Path, recursive: Boolean): Boolean = {
+    if (WriteProtectedPaths.isProtected(f)) WriteProtectedPaths.deny("delete", 
f)
+    super.delete(f, recursive)
+  }
+
+  override def mkdirs(f: Path, permission: FsPermission): Boolean = {
+    if (WriteProtectedPaths.isProtected(f)) WriteProtectedPaths.deny("mkdirs", 
f)
+    super.mkdirs(f, permission)
+  }
+
+  override def truncate(f: Path, newLength: Long): Boolean = {
+    if (WriteProtectedPaths.isProtected(f)) 
WriteProtectedPaths.deny("truncate", f)
+    super.truncate(f, newLength)
+  }
+
+  override def setTimes(f: Path, mtime: Long, atime: Long): Unit = {
+    if (WriteProtectedPaths.isProtected(f)) 
WriteProtectedPaths.deny("setTimes", f)
+    super.setTimes(f, mtime, atime)
+  }
+}
+
+/**
+ * Write protection for the Hadoop FileContext/AbstractFileSystem API
+ * (`fs.AbstractFileSystem.file.impl`).
+ *
+ * Needed because FileContextBasedCheckpointFileManager goes through 
FileContext, which
+ * bypasses the FileSystem-level write methods overridden in 
[[WriteProtectedLocalFileSystem]].
+ */
+class WriteProtectedAbstractFileSystem(uri: URI, conf: Configuration)
+    extends DelegateToFileSystem(
+      uri,
+      new WriteProtectedLocalFileSystem,
+      conf,
+      "file",
+      false) {
+
+  override def getUriDefaultPort(): Int = -1
+  override def getServerDefaults(): FsServerDefaults = 
LocalConfigKeys.getServerDefaults
+  override def isValidName(src: String): Boolean = true
+
+  override def createInternal(
+      f: Path,
+      flag: util.EnumSet[CreateFlag],
+      absolutePermission: FsPermission,
+      bufferSize: Int,
+      replication: Short,
+      blockSize: Long,
+      progress: Progressable,
+      checksumOpt: Options.ChecksumOpt,
+      createParent: Boolean): FSDataOutputStream = {
+    if (WriteProtectedPaths.isProtected(f)) WriteProtectedPaths.deny("create", 
f)
+    super.createInternal(
+      f, flag, absolutePermission, bufferSize, replication, blockSize,
+      progress, checksumOpt, createParent)
+  }
+
+  override def mkdir(
+      dir: Path, permission: FsPermission, createParent: Boolean): Unit = {
+    if (WriteProtectedPaths.isProtected(dir)) 
WriteProtectedPaths.deny("mkdir", dir)
+    super.mkdir(dir, permission, createParent)
+  }
+
+  override def delete(f: Path, recursive: Boolean): Boolean = {
+    if (WriteProtectedPaths.isProtected(f)) WriteProtectedPaths.deny("delete", 
f)
+    super.delete(f, recursive)
+  }
+
+  override def renameInternal(src: Path, dst: Path): Unit = {
+    // A rename out of a protected path is also a write to that path -- check 
both ends.
+    if (WriteProtectedPaths.isProtected(src)) WriteProtectedPaths.deny("rename 
from", src)
+    if (WriteProtectedPaths.isProtected(dst)) WriteProtectedPaths.deny("rename 
to", dst)
+    super.renameInternal(src, dst)
+  }
+
+  override def truncate(f: Path, newLength: Long): Boolean = {
+    if (WriteProtectedPaths.isProtected(f)) 
WriteProtectedPaths.deny("truncate", f)
+    super.truncate(f, newLength)
+  }
+
+  override def setTimes(f: Path, mtime: Long, atime: Long): Unit = {
+    if (WriteProtectedPaths.isProtected(f)) 
WriteProtectedPaths.deny("setTimes", f)
+    super.setTimes(f, mtime, atime)
+  }
+}
+
+/**
+ * Shared state for write-protected paths. Used by both 
[[WriteProtectedLocalFileSystem]]
+ * (FileSystem API) and [[WriteProtectedAbstractFileSystem]] (FileContext API).
+ *
+ * State is JVM-global: parallel suites in the same JVM would interfere. Tests 
in this
+ * package run serially via [[StateDataSourceTestBase]], which clears state in 
`afterEach`.
+ *
+ * Path normalization: prefixes are stored with a trailing "/", and matches use
+ * `pathStr.startsWith(prefix)`. Operations on the protected directory itself
+ * (without trailing "/") are NOT matched -- only operations on its 
descendants.
+ */
+object WriteProtectedPaths {

Review Comment:
   `WriteProtectedPaths` uses JVM-global mutable state 
(`AtomicReference[Set[String]]`). The doc comment notes "assumes a single suite 
runs at a time within a JVM" - but sbt's default is to fork one JVM per project 
and run suites in parallel within that JVM. If any other suite in `sql/core` 
touches state store paths concurrently, `isProtected` will false-positive. Is 
there a guarantee these suites run serially (e.g., via a shared `Sequential` 
tag), or should this use a thread-local / suite-scoped key to avoid flaky 
failures when new suites are added?
   
   



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