Skip to content
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

Update bucket lifecycle check #357

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion project/SparkRedshiftBuild.scala
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ object SparkRedshiftBuild extends Build {
testSparkVersion := sys.props.get("spark.testVersion").getOrElse(sparkVersion.value),
testSparkAvroVersion := sys.props.get("sparkAvro.testVersion").getOrElse("3.0.0"),
testHadoopVersion := sys.props.get("hadoop.testVersion").getOrElse("2.2.0"),
testAWSJavaSDKVersion := sys.props.get("aws.testVersion").getOrElse("1.10.22"),
testAWSJavaSDKVersion := sys.props.get("aws.testVersion").getOrElse("1.11.166"),
spName := "databricks/spark-redshift",
sparkComponents ++= Seq("sql", "hive"),
spIgnoreProvided := true,
Expand Down
23 changes: 22 additions & 1 deletion src/main/scala/com/databricks/spark/redshift/Utils.scala
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import scala.util.control.NonFatal

import com.amazonaws.services.s3.{AmazonS3URI, AmazonS3Client}
import com.amazonaws.services.s3.model.BucketLifecycleConfiguration
import com.amazonaws.services.s3.model.lifecycle.{LifecycleAndOperator, LifecyclePredicateVisitor, LifecyclePrefixPredicate, LifecycleTagPredicate}
import org.apache.hadoop.conf.Configuration
import org.apache.hadoop.fs.FileSystem
import org.slf4j.LoggerFactory
Expand Down Expand Up @@ -133,11 +134,17 @@ private[redshift] object Utils {
val rules = Option(s3Client.getBucketLifecycleConfiguration(bucket))
.map(_.getRules.asScala)
.getOrElse(Seq.empty)
val keyPrefixMatchingVisitor = new KeyPrefixMatchingVisitor(key)
rules.exists { rule =>
// Note: this only checks that there is an active rule which matches the temp directory;
// it does not actually check that the rule will delete the files. This check is still
// better than nothing, though, and we can always improve it later.
rule.getStatus == BucketLifecycleConfiguration.ENABLED && key.startsWith(rule.getPrefix)
rule.getFilter.getPredicate.accept(keyPrefixMatchingVisitor)
if (rule.getStatus == BucketLifecycleConfiguration.ENABLED) {
keyPrefixMatchingVisitor.matchFound
} else {
false
}
}
}
if (!hasMatchingBucketLifecycleRule) {
Expand Down Expand Up @@ -205,3 +212,17 @@ private[redshift] object Utils {
}
}
}

private class KeyPrefixMatchingVisitor(key: String) extends LifecyclePredicateVisitor {
var matchFound = false

override def visit(lifecyclePrefixPredicate: LifecyclePrefixPredicate): Unit = {
if (!matchFound && key.startsWith(lifecyclePrefixPredicate.getPrefix)) {
matchFound = true
}
}

override def visit(lifecycleTagPredicate: LifecycleTagPredicate): Unit = {}

override def visit(lifecycleAndOperator: LifecycleAndOperator): Unit = {}
}