Repository: rucio/rucio

Issue:
Injector should not inject a rule which is about to expire soon
Motivation
----------
This potentially creates all kind of contention problems. Thus the injector should not inject a rule which is about to expire soon (minutes).



Discussion:

Resolving patch:
diff --git a/lib/rucio/core/rule.py b/lib/rucio/core/rule.py
--- a/lib/rucio/core/rule.py
+++ b/lib/rucio/core/rule.py
@@ -610,6 +610,11 @@ def inject_rule(rule_id, session=None):
     except NoResultFound:
         raise RuleNotFound('No rule with the id %s found' % (rule_id))
 
+    # Check if rule will expire in the next 5 minutes:
+    if rule.child_rule_id is None and rule.expires_at is not None and rule.expires_at < datetime.utcnow() + timedelta(seconds=300):
+        logging.info('Rule %s expiring soon, skipping', str(rule.id))
+        return
+
     # Special R2D2 container handling
     if (rule.did_type == DIDType.CONTAINER and '.r2d2_request.' in rule.name) or (rule.split_container and rule.did_type == DIDType.CONTAINER):
         logging.debug("Creating dataset rules for Split Container rule %s", str(rule.id))


Test patch:
diff --git a/lib/rucio/tests/test_judge_injector.py b/lib/rucio/tests/test_judge_injector.py
--- a/lib/rucio/tests/test_judge_injector.py
+++ b/lib/rucio/tests/test_judge_injector.py
@@ -6,7 +6,7 @@
 # http://www.apache.org/licenses/LICENSE-2.0
 #
 # Authors:
-# - Martin Barisits, <martin.barisits@cern.ch>, 2015-2016
+# - Martin Barisits, <martin.barisits@cern.ch>, 2015-2019
 
 from nose.tools import assert_raises
 
@@ -136,7 +136,7 @@ def test_add_rule_with_r2d2_container_treating(self):
             add_did(scope, dataset, DIDType.from_sym('DATASET'), 'jdoe')
             attach_dids(scope, dataset, files, 'jdoe')
             attach_dids(scope, container, [{'scope': scope, 'name': dataset}], 'jdoe')
-        rule_id = add_rule(dids=[{'scope': scope, 'name': container}], account='jdoe', copies=1, rse_expression=self.rse1, grouping='DATASET', weight=None, lifetime=100, locked=False, subscription_id=None, ask_approval=True)[0]
+        rule_id = add_rule(dids=[{'scope': scope, 'name': container}], account='jdoe', copies=1, rse_expression=self.rse1, grouping='DATASET', weight=None, lifetime=900, locked=False, subscription_id=None, ask_approval=True)[0]
         approve_rule(rule_id, approver='root')
         assert(get_rule(rule_id)['state'] == RuleState.INJECT)
         rule_injector(once=True)
@@ -159,8 +159,8 @@ def test_add_rule_with_r2d2_container_treating_and_duplicate_rule(self):
             add_did(scope, dataset, DIDType.from_sym('DATASET'), 'jdoe')
             attach_dids(scope, dataset, files, 'jdoe')
             attach_dids(scope, container, [{'scope': scope, 'name': dataset}], 'jdoe')
-        add_rule(dids=[{'scope': scope, 'name': dataset}], account='jdoe', copies=1, rse_expression=self.rse1, grouping='DATASET', weight=None, lifetime=100, locked=False, subscription_id=None, ask_approval=False)
-        rule_id = add_rule(dids=[{'scope': scope, 'name': container}], account='jdoe', copies=1, rse_expression=self.rse1, grouping='DATASET', weight=None, lifetime=100, locked=False, subscription_id=None, ask_approval=True)[0]
+        add_rule(dids=[{'scope': scope, 'name': dataset}], account='jdoe', copies=1, rse_expression=self.rse1, grouping='DATASET', weight=None, lifetime=900, locked=False, subscription_id=None, ask_approval=False)
+        rule_id = add_rule(dids=[{'scope': scope, 'name': container}], account='jdoe', copies=1, rse_expression=self.rse1, grouping='DATASET', weight=None, lifetime=900, locked=False, subscription_id=None, ask_approval=True)[0]
         approve_rule(rule_id, approver='root')
         assert(get_rule(rule_id)['state'] == RuleState.INJECT)
         rule_injector(once=True)