vanzin 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_r335145149
##########
File path:
sql/core/src/main/scala/org/apache/spark/sql/execution/streaming/FileStreamSource.scala
##########
@@ -330,4 +353,129 @@ 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(baseArchivePathString.isDefined)
+ val baseArchivePathStr = baseArchivePathString.get
+ val normalizedBaseArchiveDirPath = if (baseArchivePathStr.endsWith("/"))
{
+ baseArchivePathStr.substring(0, baseArchivePathStr.length - 1)
+ } else {
+ baseArchivePathStr
+ }
+
+ new Path(normalizedBaseArchiveDirPath + pathUri.getPath)
Review comment:
> .getPath is used because URI could have schema part
Then use `getPath`. That's not my point. My point is that constantly
switching from string to URI to Path is confusing, and you should avoid that.
You don't need mess with strings to create paths. `Path` has an explicit
constructor where you can define the parent and the child. Use that and your
code will be simpler.
----------------------------------------------------------------
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]