-
Notifications
You must be signed in to change notification settings - Fork 29.3k
[SPARK-30312][SQL] Preserve path permission and acl when truncate table #26956
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 5 commits
73e1bb0
60134c1
1df437b
2c3f1fb
39fe234
7b29f2f
ab69638
00a2bc8
8e429a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -20,7 +20,7 @@ package org.apache.spark.sql.execution.command | |
| import java.net.{URI, URISyntaxException} | ||
|
|
||
| import scala.collection.mutable.ArrayBuffer | ||
| import scala.util.Try | ||
| import scala.util.{Failure, Try} | ||
| import scala.util.control.NonFatal | ||
|
|
||
| import org.apache.hadoop.fs.{FileContext, FsConstants, Path} | ||
|
|
@@ -499,8 +499,35 @@ case class TruncateTableCommand( | |
| val path = new Path(location.get) | ||
| try { | ||
| val fs = path.getFileSystem(hadoopConf) | ||
| val fileStatus = fs.getFileStatus(path) | ||
| // Not all fs impl. support these APIs. | ||
| val optPermission = Try(fileStatus.getPermission()) | ||
| val optAcls = Try(fs.getAclStatus(path).getEntries) | ||
|
|
||
| fs.delete(path, true) | ||
|
|
||
| // We should keep original permission/acl of the path. | ||
| // For owner/group, only super-user can set it, for example on HDFS. Because | ||
| // current user can delete the path, we assume the user/group is correct or not an issue. | ||
| fs.mkdirs(path) | ||
| optPermission.foreach { permission => | ||
| Try(fs.setPermission(path, permission)) match { | ||
|
viirya marked this conversation as resolved.
Outdated
|
||
| case Failure(e) => | ||
| throw new AnalysisException( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ur,
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Also,
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Ah, this follows throwing AnalysisException at the outside try..catch block. We can change this.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
For security reason, cannot set back original Acl/permission seems serious issue so I'm ok with a
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Maybe
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Based on previous discussion around #26956 (comment), we may not need a config.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. SecurityException sounds good |
||
| s"Failed to set original permission $permission back to " + | ||
| s"the created path: $path. Exception: ${e.getMessage}") | ||
| case _ => | ||
| } | ||
| } | ||
| optAcls.foreach { acls => | ||
| Try(fs.setAcl(path, acls)) match { | ||
| case Failure(e) => | ||
| throw new AnalysisException( | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto. |
||
| s"Failed to set original ACL $acls back to " + | ||
| s"the created path: $path. Exception: ${e.getMessage}") | ||
| case _ => | ||
| } | ||
| } | ||
| } catch { | ||
| case NonFatal(e) => | ||
| throw new AnalysisException( | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not familiar with Hadoop FS APIs, but do we have something like
rm -rf tablePath/*?. My point is, if we don't delete the parent folder, then we don't have this problem at all.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In the FileSystem API, I didn't find one like that.
But I just search again, there is FileUtil API that seems working like that.
I will test tomorrow to see if it work well to keep permission/acl in a Spark cluster like current approach.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, I think
FileUtil.fullyDeleteContentsonly works on local file (java.io.File), not for files on distributed file system like HDFS.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So I think there is no single API to do
rm -rf tablePath/*in Hadoop FS, if I haven't missed anything.We can do
listStatusand delete all contents in the given directory. But it is inefficient for DFS and is easier to have error.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you know how Hive/Presto implement TRUNCATE TABLE? Are there other file attributes we need to retain?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For Hive, I try to trace the code path for TRUNCATE TABLE:
Get the locations of table/partitions and call Warehouse.deleteDir for each.
https://github.com/apache/hive/blob/master/standalone-metastore/metastore-server/src/main/java/org/apache/hadoop/hive/metastore/HiveMetaStore.java#L3109
FileSystem.delete(f, true).In
HiveMetaStore.truncateTableInternal, after the location is deleted, new directory is created and previous file status including permissions, group, and ACLs are set to new directory:https://github.com/apache/hive/blob/35f86c749cefc2a9972a991deed78a1c3719093d/standalone-metastore/metastore-common/src/main/java/org/apache/hadoop/hive/metastore/utils/HdfsUtils.java#L288
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now I retain permission and ACLs. I was doing
fs.setOwnerin first commit for retaining path owner and group. Butfs.setOwnerthrows exception because it is only doable by super user.In Hive code, since it is code running the metastore server, I think it is running with enough permission to set owner/group. For Spark, we may not running it with super user.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In Presto, looks like it takes the approach by listing all files/directories under the path to delete (i.e.,
rm -rf tablePath/*), and recursively deleting them:https://github.com/prestodb/presto/blob/9c21aaa659f9d4927fe12bec4b8015c8cbf4722e/presto-hive-metastore/src/main/java/com/facebook/presto/hive/metastore/SemiTransactionalHiveMetastore.java#L485
https://github.com/prestodb/presto/blob/9c21aaa659f9d4927fe12bec4b8015c8cbf4722e/presto-hive-metastore/src/main/java/com/facebook/presto/hive/metastore/SemiTransactionalHiveMetastore.java#L1796
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the presto way seems safer to me.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was concerned with the presto way on performance regression. For a table which has many files/directories, deleting them one by one could be a bottleneck.
If we add a config for retaining permission/ACLs when truncating table? Users can choose to disable it and directly delete top directory (current behavior) without retaining permission/ACLs.