Repository: rucio/rucio
Issue: Daemon crashes in 1.20.3rc1

Motivation
----------
Conveyor submitter raised an error -> core.transfer.get_transfer_requests_and_source_replicas
Light reaper raised an error -> Invalid UUID

Modification
------------
Reorder checks in get_transfer_requests_and_source_replicas
Convert rse name to id in light reaper (and dark reaper)


Changed files:
lib/rucio/daemons/reaper/light_reaper.py, lib/rucio/api/temporary_did.py, lib/rucio/core/transfer.py, lib/rucio/daemons/reaper/dark_reaper.py, lib/rucio/api/did.py

diff --git a/lib/rucio/api/did.py b/lib/rucio/api/did.py
index 4051ad8a0..0d7578733 100644
--- a/lib/rucio/api/did.py
+++ b/lib/rucio/api/did.py
@@ -103,8 +103,11 @@ def add_dids(dids, issuer):
     :param issuer: The issuer account.
     """
     for d in dids:
-        if 'rse' in d.keys():
-            d.update({'rse_id': get_rse_id(rse=d.get('rse'))})
+        if 'rse' in d:
+            rse_id = None
+            if d['rse'] is not None:
+                rse_id = get_rse_id(rse=d['rse'])
+            d['rse_id'] = rse_id
 
     kwargs = {'issuer': issuer, 'dids': dids}
     if not rucio.api.permission.has_permission(issuer=issuer, action='add_dids', kwargs=kwargs):
@@ -124,7 +127,8 @@ def attach_dids(scope, name, attachment, issuer):
 
     rse_id = None
     if 'rse' in attachment:
-        rse_id = get_rse_id(rse=attachment.get('rse'))
+        if attachment['rse'] is not None:
+            rse_id = get_rse_id(rse=attachment['rse'])
         attachment['rse_id'] = rse_id
 
     kwargs = {'scope': scope, 'name': name, 'attachment': attachment}
@@ -152,7 +156,11 @@ def attach_dids_to_dids(attachments, issuer, ignore_duplicate=False):
     validate_schema(name='attachments', obj=attachments)
 
     for a in attachments:
-        a.update({'rse_id': get_rse_id(rse=a.get('rse'))})
+        if 'rse' in a:
+            rse_id = None
+            if a['rse'] is not None:
+                rse_id = get_rse_id(rse=a['rse'])
+            a['rse_id'] = rse_id
 
     if not rucio.api.permission.has_permission(issuer=issuer, action='attach_dids_to_dids', kwargs={'attachments': attachments}):
         raise rucio.common.exception.AccessDenied('Account %s can not add data identifiers' % (issuer))
diff --git a/lib/rucio/api/temporary_did.py b/lib/rucio/api/temporary_did.py
index 9aef2794c..2c5a97eb2 100644
--- a/lib/rucio/api/temporary_did.py
+++ b/lib/rucio/api/temporary_did.py
@@ -26,6 +26,9 @@ def add_temporary_dids(dids, issuer):
     """
     for did in dids:
         if 'rse' in did and 'rse_id' not in did:
-            did['rse_id'] = get_rse_id(rse=did['rse'])
+            rse_id = None
+            if did['rse'] is not None:
+                rse_id = get_rse_id(rse=did['rse'])
+            did['rse_id'] = rse_id
 
     return temporary_did.add_temporary_dids(dids=dids, account=issuer)
diff --git a/lib/rucio/core/transfer.py b/lib/rucio/core/transfer.py
index 2129e2ea7..b507d3ccd 100644
--- a/lib/rucio/core/transfer.py
+++ b/lib/rucio/core/transfer.py
@@ -422,11 +422,26 @@ def get_transfer_requests_and_source_replicas(total_workers=0, worker_number=0,
     transfers, rses_info, protocols, rse_attrs, reqs_no_source, reqs_only_tape_source, reqs_scheme_mismatch = {}, {}, {}, {}, [], [], []
 
     for req_id, rule_id, scope, name, md5, adler32, bytes, activity, attributes, previous_attempt_id, dest_rse_id, source_rse_id, rse, deterministic, rse_type, path, retry_count, src_url, ranking, link_ranking in req_sources:
+
+        # source_rse_id will be None if no source replicas
+        # rse will be None if rse is staging area
+        if source_rse_id is None or rse is None:
+            continue
+
         dest_rse_name = get_rse_name(rse_id=dest_rse_id, session=session)
         source_rse_name = get_rse_name(rse_id=source_rse_id, session=session)
+
+        if link_ranking is None:
+            logging.debug("Request %s: no link from %s to %s" % (req_id, source_rse_name, dest_rse_name))
+            continue
+
+        if source_rse_id in unavailable_read_rse_ids:
+            continue
+
         if dest_rse_id in unavailable_write_rse_ids:
             logging.warning('RSE %s is blacklisted for write. Will skip the submission of new jobs' % (dest_rse_name))
             continue
+
         transfer_src_type = "DISK"
         transfer_dst_type = "DISK"
         allow_tape_source = True
@@ -444,18 +459,6 @@ def get_transfer_requests_and_source_replicas(total_workers=0, worker_number=0,
                 if req_id not in reqs_no_source:
                     reqs_no_source.append(req_id)
 
-                # source_rse_id will be None if no source replicas
-                # rse will be None if rse is staging area
-                if source_rse_id is None or rse is None:
-                    continue
-
-                if link_ranking is None:
-                    logging.debug("Request %s: no link from %s to %s" % (req_id, dest_rse_name, source_rse_name))
-                    continue
-
-                if source_rse_id in unavailable_read_rse_ids:
-                    continue
-
                 # Get destination rse information
                 if dest_rse_id not in rses_info:
                     rses_info[dest_rse_id] = rsemgr.get_rse_info(rse=dest_rse_name, session=session)
@@ -646,18 +649,6 @@ def get_transfer_requests_and_source_replicas(total_workers=0, worker_number=0,
             else:
                 current_schemes = transfers[req_id]['schemes']
 
-                # source_rse_id will be None if no source replicas
-                # rse will be None if rse is staging area
-                if source_rse_id is None or rse is None:
-                    continue
-
-                if link_ranking is None:
-                    logging.debug("Request %s: no link from %s to %s" % (req_id, source_rse_name, dest_rse_name))
-                    continue
-
-                if source_rse_id in unavailable_read_rse_ids:
-                    continue
-
                 attr = None
                 if attributes:
                     if isinstance(attributes, dict):
diff --git a/lib/rucio/daemons/reaper/dark_reaper.py b/lib/rucio/daemons/reaper/dark_reaper.py
index b60cb79c9..ba12f29d8 100644
--- a/lib/rucio/daemons/reaper/dark_reaper.py
+++ b/lib/rucio/daemons/reaper/dark_reaper.py
@@ -193,6 +193,8 @@ def run(total_workers=1, chunk_size=100, once=False, rses=[], scheme=None,
         rses = list_rses()
     elif not rses:
         rses = [rse['id'] for rse in rse_core.list_rses()]
+    else:
+        rses = [rse_core.get_rse_id(rse=rse) for rse in rses]
 
     threads = []
     for worker in range(total_workers):
diff --git a/lib/rucio/daemons/reaper/light_reaper.py b/lib/rucio/daemons/reaper/light_reaper.py
index a9fc4d4b2..1dad6de0c 100644
--- a/lib/rucio/daemons/reaper/light_reaper.py
+++ b/lib/rucio/daemons/reaper/light_reaper.py
@@ -189,6 +189,8 @@ def run(total_workers=1, chunk_size=100, once=False, rses=[], scheme=None,
 
     if all_rses:
         rses = [rse['id'] for rse in rse_core.list_rses()]
+    else:
+        rses = [rse_core.get_rse_id(rse=rse) for rse in rses]
 
     threads = []
     for worker in range(total_workers):