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_r338347720
##########
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) {
+ // there's no chance for archive file to be matched against source
pattern
+ return false
+ }
+
+ var matched = true
+
+ // new path will never match against source path when the depth is not a
range of
+ // the depth of source path ~ (the depth of source path + 1)
+ // because the source files are picked when they match against source
pattern or
+ // their parent directories match against source pattern
+ val depthSourcePattern = sourceGlobFilters.length
+ val depthArchiveFile = archiveFile.depth()
+
+ // we already checked against the depth of archive path, but rechecking
wouldn't hurt
+ if (depthArchiveFile < depthSourcePattern || depthArchiveFile >
depthSourcePattern + 1) {
+ // never matched
+ matched = false
+ } else {
+ var pathToCompare = if (depthArchiveFile == depthSourcePattern + 1) {
+ archiveFile.getParent
+ } else {
+ archiveFile
+ }
+
+ // Now pathToCompare should have same depth as sourceGlobFilters.length
+ var index = 0
+ do {
+ // GlobFilter only matches against its name, not full path so it's
safe to compare
+ if (!sourceGlobFilters(index).accept(pathToCompare)) {
+ matched = false
+ } else {
+ pathToCompare = pathToCompare.getParent
+ index += 1
+ }
+ } while (matched && !pathToCompare.isRoot)
+ }
+
+ matched
+ }
+
+ private def doArchive(sourcePath: Path, archivePath: Path): Unit = {
+ try {
+ logDebug(s"Creating directory if it doesn't exist
${archivePath.getParent}")
+ if (!fileSystem.exists(archivePath.getParent)) {
Review comment:
Maybe better to do latter. Even we move the archiving as asynchronous one,
it might lead archive requests being stacked which might not be able to catch
up. We can reconsider if we hear actual use case on it.
----------------------------------------------------------------
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]