HeartSaVioR commented on a change in pull request #22952: [SPARK-20568][SS] 
Provide option to clean up completed files in streaming query
URL: https://github.com/apache/spark/pull/22952#discussion_r338334551
 
 

 ##########
 File path: 
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/FileStreamSource.scala
 ##########
 @@ -330,4 +353,137 @@ object FileStreamSource {
 
     def size: Int = map.size()
   }
+
+  private[sql] class FileStreamSourceCleaner(
+      fileSystem: FileSystem,
+      sourcePath: Path,
+      baseArchivePathString: Option[String]) extends Logging {
+
+    private val sourceGlobFilters: Seq[GlobFilter] = 
buildSourceGlobFilters(sourcePath)
+
+    private val baseArchivePath: Option[Path] = baseArchivePathString.map(new 
Path(_))
+
+    def archive(entry: FileEntry): Unit = {
+      require(baseArchivePath.isDefined)
+
+      val curPath = new Path(new URI(entry.path))
+      val curPathUri = curPath.toUri
+
+      val newPath = buildArchiveFilePath(curPathUri)
+
+      if (isArchiveFileMatchedAgainstSourcePattern(newPath)) {
+        logWarning(s"Fail to move $curPath to $newPath - destination matches " 
+
+          s"to source path/pattern. Skip moving file.")
+      } else {
+        doArchive(curPath, newPath)
+      }
+    }
+
+    def delete(entry: FileEntry): Unit = {
+      val curPath = new Path(new URI(entry.path))
+      try {
+        logDebug(s"Removing completed file $curPath")
+
+        if (!fileSystem.delete(curPath, false)) {
+          logWarning(s"Fail to remove $curPath / skip removing file.")
+        }
+      } catch {
+        case NonFatal(e) =>
+          // Log to error but swallow exception to avoid process being stopped
+          logWarning(s"Fail to remove $curPath / skip removing file.", e)
+      }
+    }
+
+    private def buildSourceGlobFilters(sourcePath: Path): Seq[GlobFilter] = {
+      val filters = new scala.collection.mutable.MutableList[GlobFilter]()
+
+      var currentPath = sourcePath
+      while (!currentPath.isRoot) {
+        filters += new GlobFilter(currentPath.getName)
+        currentPath = currentPath.getParent
+      }
+
+      filters.toList
+    }
+
+    private def buildArchiveFilePath(pathUri: URI): Path = {
+      require(baseArchivePath.isDefined)
+      new Path(baseArchivePath.get, pathUri.getPath.stripPrefix("/"))
+    }
+
+    /**
+     * This method checks whether the destination of archive file will be 
under the source path
+     * (which contains glob) to prevent the possibility of 
overwriting/re-reading as input.
+     *
+     * FileStreamSource reads the files which one of below conditions is met:
+     * 1) file itself is matched with source path
+     * 2) parent directory is matched with source path
+     *
+     * Checking with glob pattern is costly, so this method leverages above 
information to prune
+     * the cases where the file cannot be matched with source path. For 
example, when file is
+     * moved to archive directory, destination path will retain input file's 
path as suffix,
+     * so destination path can't be matched with source path if archive 
directory's depth is
+     * longer than 2, as neither file nor parent directory of destination path 
can be matched
+     * with source path.
+     */
+    private def isArchiveFileMatchedAgainstSourcePattern(archiveFile: Path): 
Boolean = {
+      if (baseArchivePath.get.depth() > 2) {
 
 Review comment:
   This optimization deals with simple fact: "glob path would only cover files 
in specific depth" (there's no notation like `**` which would match multiple 
depths of directories), so the depth of possibly matched paths is deterministic 
if you know about glob path.
   
   Suppose you have combined the path, all possible cases of archive path to be 
matched with glob path are 1) direct match: only possible if base archive path 
is just '/' which is depth 1 2) parent dir match: direct subdirectory under /  
(like `/a`) which the depth of base archive path would be 2. Does it make sense?
   
   > is there a reason why you need to check this for every archived path? 
Can't you, during config validation, perform this check once, with some path 
generated to match the source pattern, and then declare that there is no 
conflict between the archive path and the source path?
   
   No, that's what I've been missing. You're right it can be determined once we 
initialize. Nice finding.

----------------------------------------------------------------
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.
 
For queries about this service, please contact Infrastructure at:
[email protected]


With regards,
Apache Git Services

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

Reply via email to