perf(lifecycle): filter rules by reference and deduplicate tag and lock checks (#9735)

This commit is contained in:
Ramakrishna Chilaka
2026-11-29 08:11:36 +05:30
committed by GitHub
parent 5236d31055
commit 2576307d01
3 changed files with 78 additions and 75 deletions
+45 -36
View File
@@ -410,6 +410,41 @@ fn lifecycle_mod_time(obj: &ObjectOpts) -> Option<OffsetDateTime> {
None
}
fn filter_rules_refs<'a>(
config: &'a PersistedLifecycleConfiguration,
obj: &ObjectOpts,
) -> Option<Vec<&'a PersistedLifecycleRule>> {
if obj.name.is_empty() {
return None;
}
let mut rules = Vec::with_capacity(config.rules.len());
let mut decoded_tags: Option<HashMap<String, String>> = None;
let obj_size = i64::try_from(obj.size).unwrap_or(i64::MAX);
for rule in config.rules.iter() {
if rule.status.as_str() == "Disabled" {
continue;
}
if let Some(rule_prefix) = lifecycle_rule_prefix(rule)
&& !obj.name.starts_with(rule_prefix)
{
continue;
}
if let Some(filter) = rule.filter.as_ref() {
if crate::rule::requires_tag_matching(filter) {
let tags = decoded_tags.get_or_insert_with(|| crate::tagging::decode_tags_to_map(&obj.user_tags));
if !crate::rule::test_tags_with_map(filter, tags) {
continue;
}
}
if !obj.delete_marker && !<PersistedLifecycleFilter as Filter>::by_size(filter, obj_size) {
continue;
}
}
rules.push(rule);
}
Some(rules)
}
#[async_trait::async_trait]
pub trait Lifecycle {
async fn has_transition(&self) -> bool;
@@ -649,32 +684,7 @@ impl Lifecycle for PersistedLifecycleConfiguration {
}
async fn filter_rules(&self, obj: &ObjectOpts) -> Option<Vec<PersistedLifecycleRule>> {
if obj.name.is_empty() {
return None;
}
let mut rules = Vec::<PersistedLifecycleRule>::new();
for rule in self.rules.iter() {
if rule.status.as_str() == "Disabled" {
continue;
}
if let Some(rule_prefix) = lifecycle_rule_prefix(rule)
&& !obj.name.starts_with(rule_prefix)
{
continue;
}
if let Some(filter) = rule.filter.as_ref() {
if !<PersistedLifecycleFilter as Filter>::test_tags(filter, &obj.user_tags) {
continue;
}
if !obj.delete_marker
&& !<PersistedLifecycleFilter as Filter>::by_size(filter, i64::try_from(obj.size).unwrap_or(i64::MAX))
{
continue;
}
}
rules.push(rule.clone());
}
Some(rules)
filter_rules_refs(self, obj).map(|rules| rules.into_iter().cloned().collect())
}
async fn eval(&self, obj: &ObjectOpts) -> Event {
@@ -696,13 +706,13 @@ impl Lifecycle for PersistedLifecycleConfiguration {
return Event::default();
}
let Some(lc_rules) = self.filter_rules(obj).await else {
let Some(lc_rules) = filter_rules_refs(self, obj) else {
return Event::default();
};
let mut event: Option<Event> = None;
for rule in lc_rules {
let Some(expiration) = rule.expiration else {
let Some(expiration) = rule.expiration.as_ref() else {
continue;
};
if expiration.expired_object_delete_marker.is_some_and(|v| v) {
@@ -718,7 +728,7 @@ impl Lifecycle for PersistedLifecycleConfiguration {
let predicted = Event {
action: IlmAction::DeleteAction,
rule_id: rule.id.unwrap_or_default(),
rule_id: rule.id.clone().unwrap_or_default(),
due: Some(due),
noncurrent_days: 0,
newer_noncurrent_versions: 0,
@@ -752,8 +762,8 @@ impl Lifecycle for PersistedLifecycleConfiguration {
events.push(event);
}
if let Some(ref lc_rules) = self.filter_rules(obj).await {
for rule in lc_rules.iter() {
if let Some(lc_rules) = filter_rules_refs(self, obj) {
for rule in lc_rules {
// A retention count that PUT validation would have rejected can
// only come from older persistence or an import. Take no action
// for the rule instead of allowing another action on the same
@@ -764,7 +774,7 @@ impl Lifecycle for PersistedLifecycleConfiguration {
component = LOG_COMPONENT_ECSTORE,
subsystem = LOG_SUBSYSTEM_LIFECYCLE,
object = %obj.name,
rule_id = %rule.id.clone().unwrap_or_default(),
rule_id = %rule.id.as_deref().unwrap_or_default(),
reason = "corrupt_newer_noncurrent_versions",
"Skipped lifecycle evaluation for a rule with an invalid retention count"
);
@@ -997,14 +1007,13 @@ impl Lifecycle for PersistedLifecycleConfiguration {
// failed transition has an earlier deadline. Within each action class,
// prefer the earliest deadline using a deterministic total order.
let event = events
.iter()
.into_iter()
.min_by_key(|event| {
(
ilm_action_priority_rank(&event.action),
event.due.unwrap_or(OffsetDateTime::UNIX_EPOCH).unix_timestamp(),
)
})
.cloned()
.unwrap_or_default();
return event;
}
@@ -1013,8 +1022,8 @@ impl Lifecycle for PersistedLifecycleConfiguration {
}
async fn noncurrent_versions_expiration_limit(self: Arc<Self>, obj: &ObjectOpts) -> Event {
if let Some(filter_rules) = self.filter_rules(obj).await {
for rule in filter_rules.iter() {
if let Some(filter_rules) = filter_rules_refs(&self, obj) {
for rule in filter_rules {
if let Some(ref noncurrent_version_expiration) = rule.noncurrent_version_expiration {
return if let Some(newer_noncurrent_versions) = noncurrent_version_expiration.newer_noncurrent_versions {
// Zero means "no count constraint"; a negative count is
+21 -33
View File
@@ -19,7 +19,7 @@ use rustfs_storage_api::metadata_keys;
use time::{OffsetDateTime, format_description};
pub fn is_object_locked_by_metadata(user_defined: &HashMap<String, String>, is_delete_marker: bool) -> bool {
if is_delete_marker {
if is_delete_marker || user_defined.is_empty() {
return false;
}
@@ -57,11 +57,26 @@ pub fn is_object_locked(
if is_delete_marker {
return false;
}
if is_object_locked_by_metadata(user_defined, false) {
return true;
}
if has_explicit_lock_metadata(user_defined) {
return !explicit_lock_metadata_is_well_formed(user_defined);
if !user_defined.is_empty() {
let legal_hold = user_defined.get(metadata_keys::OBJECT_LOCK_LEGAL_HOLD);
if legal_hold.is_some_and(|value| value.eq_ignore_ascii_case("ON")) {
return true;
}
let mode = user_defined.get(metadata_keys::OBJECT_LOCK_MODE);
let retain_until = user_defined.get(metadata_keys::OBJECT_LOCK_RETAIN_UNTIL_DATE);
if let (Some(mode_val), Some(retain_val)) = (mode, retain_until)
&& is_retention_mode(mode_val)
&& let Ok(parsed_until) = OffsetDateTime::parse(retain_val, &format_description::well_known::Iso8601::DEFAULT)
{
if parsed_until.unix_timestamp() > OffsetDateTime::now_utc().unix_timestamp() {
return true;
}
return legal_hold.is_some_and(|value| !value.eq_ignore_ascii_case("OFF"));
} else if legal_hold.is_some() || mode.is_some() || retain_until.is_some() {
let legal_hold_ok = legal_hold.is_none_or(|value| value.eq_ignore_ascii_case("OFF"));
let retention_ok = mode.is_none() && retain_until.is_none();
return !(legal_hold_ok && retention_ok);
}
}
let Some(default_retention) = config.and_then(|config| config.rule.as_ref()?.default_retention.as_ref()) else {
@@ -84,33 +99,6 @@ pub fn is_object_locked(
retain_until.unix_timestamp() > OffsetDateTime::now_utc().unix_timestamp()
}
fn has_explicit_lock_metadata(user_defined: &HashMap<String, String>) -> bool {
user_defined.contains_key(metadata_keys::OBJECT_LOCK_LEGAL_HOLD)
|| user_defined.contains_key(metadata_keys::OBJECT_LOCK_MODE)
|| user_defined.contains_key(metadata_keys::OBJECT_LOCK_RETAIN_UNTIL_DATE)
}
fn explicit_lock_metadata_is_well_formed(user_defined: &HashMap<String, String>) -> bool {
if user_defined
.get(metadata_keys::OBJECT_LOCK_LEGAL_HOLD)
.is_some_and(|value| !value.eq_ignore_ascii_case("ON") && !value.eq_ignore_ascii_case("OFF"))
{
return false;
}
match (
user_defined.get(metadata_keys::OBJECT_LOCK_MODE),
user_defined.get(metadata_keys::OBJECT_LOCK_RETAIN_UNTIL_DATE),
) {
(None, None) => true,
(Some(mode), Some(retain_until)) => {
is_retention_mode(mode)
&& OffsetDateTime::parse(retain_until, &format_description::well_known::Iso8601::DEFAULT).is_ok()
}
_ => false,
}
}
fn default_retention_until(
mod_time: OffsetDateTime,
retention: &rustfs_gateway_types::persistence::PersistedDefaultRetention,
+12 -6
View File
@@ -38,9 +38,7 @@ impl Filter for PersistedLifecycleFilter {
}
let user_tags = tagging::decode_tags_to_map(user_tags);
self.tag.as_ref().is_none_or(|tag| tag_matches(tag, &user_tags))
&& self.and.as_ref().is_none_or(|and| and_tags_match(and, &user_tags))
test_tags_with_map(self, &user_tags)
}
fn by_size(&self, sz: i64) -> bool {
@@ -52,7 +50,7 @@ impl Filter for PersistedLifecycleFilter {
}
}
fn requires_tag_matching(filter: &PersistedLifecycleFilter) -> bool {
pub(crate) fn requires_tag_matching(filter: &PersistedLifecycleFilter) -> bool {
filter.tag.is_some()
|| filter
.and
@@ -61,6 +59,14 @@ fn requires_tag_matching(filter: &PersistedLifecycleFilter) -> bool {
.is_some_and(|tags| !tags.is_empty())
}
pub(crate) fn test_tags_with_map(
filter: &PersistedLifecycleFilter,
user_tags: &std::collections::HashMap<String, String>,
) -> bool {
filter.tag.as_ref().is_none_or(|tag| tag_matches(tag, user_tags))
&& filter.and.as_ref().is_none_or(|and| and_tags_match(and, user_tags))
}
fn tag_matches(tag: &PersistedLifecycleTag, user_tags: &std::collections::HashMap<String, String>) -> bool {
let Some(key) = tag.key.as_deref() else {
return false;
@@ -109,8 +115,8 @@ impl TransitionOps for PersistedTransition {
if self.days.is_some_and(|days| days < 0) {
return Err(std::io::Error::other(ERR_TRANSITION_INVALID));
}
if let Some(date) = self.date.clone() {
let date = crate::core::parse_lifecycle_date(&date).map_err(|_| std::io::Error::other(ERR_TRANSITION_INVALID))?;
if let Some(date) = self.date.as_deref() {
let date = crate::core::parse_lifecycle_date(date).map_err(|_| std::io::Error::other(ERR_TRANSITION_INVALID))?;
if date.unix_timestamp() <= 0 || !is_midnight(date) {
return Err(std::io::Error::other(ERR_TRANSITION_INVALID));
}