Repository: rucio/rucio

Issue:
Improve error message for PFN mismatch
Motivation
----------
Sometimes we get the error in panda for our jobs:

```
ddm, 200: Could not add files to DDM: Details: [u"One of the PFNs provided does not match the Rucio expected PFN : [u'gsiftp://dtn01.nersc.gov/project/projectdirs/m2015/atlasdatadisk/rucio/mc16_13TeV/3b/cd/log.11350637._033145.job.log.tgz.1'] vs [u'gsiftp://dtn01.nersc.gov:2811/project/projectdirs/m2015/atlasdatadisk/rucio/mc16_13TeV/3b/cd/log.11350637._033145.job.log.tgz.1'] ([

{'scope': u'mc16_13TeV', 'name': u'log.11350637._033145.job.log.tgz.1'}
])"]
```

In this message it does not make it clear, which path we should be editing vs which one Rucio is expected. I found the message sitting in 
rucio/src/lib/rucio/core/replica.py
around line 1023:
raise exception.InvalidPath('One of the PFNs provided does not match the Rucio expected PFN : %s vs %s (%s)' % (str(pfns), str(expected_pfns), str(lfns)))

So I see the first is probably what we set in AGIS and the second is what you are expecting. Can you make this clear in the error message?

Thanks,
Taylor

_Migrated from [JIRA](https://its.cern.ch/jira/browse/RUCIO-2692)_


Discussion:

Resolving patch:
diff --git a/lib/rucio/core/replica.py b/lib/rucio/core/replica.py
--- a/lib/rucio/core/replica.py
+++ b/lib/rucio/core/replica.py
@@ -1254,8 +1254,8 @@ def add_replicas(rse, files, account, rse_id=None, ignore_availability=True,
             expected_pfns = clean_surls(expected_pfns.values())
             pfns = clean_surls(pfns)
             if pfns != expected_pfns:
-                print('ALERT: One of the PFNs provided does not match the Rucio expected PFN : %s vs %s (%s)' % (str(pfns), str(expected_pfns), str(lfns)))
-                raise exception.InvalidPath('One of the PFNs provided does not match the Rucio expected PFN : %s vs %s (%s)' % (str(pfns), str(expected_pfns), str(lfns)))
+                print('ALERT: One of the PFNs provided does not match the Rucio expected PFN : got %s, expected %s (%s)' % (str(pfns), str(expected_pfns), str(lfns)))
+                raise exception.InvalidPath('One of the PFNs provided does not match the Rucio expected PFN : got %s, expected %s (%s)' % (str(pfns), str(expected_pfns), str(lfns)))
 
     nbfiles, bytes = __bulk_add_replicas(rse_id=replica_rse.id, files=files, account=account, session=session)
     increase(rse_id=replica_rse.id, files=nbfiles, bytes=bytes, session=session)
diff --git a/lib/rucio/daemons/bb8/common.py b/lib/rucio/daemons/bb8/common.py
--- a/lib/rucio/daemons/bb8/common.py
+++ b/lib/rucio/daemons/bb8/common.py
@@ -221,7 +221,7 @@ def list_rebalance_rule_candidates(rse, mode=None, session=None):
     """
 
     if mode is None:
-        sql = """SELECT dsl.scope as scope, dsl.name as name, rawtohex(r.id) as rule_id, r.rse_expression as rse_expression, r.subscription_id as subscription_id, d.bytes as bytes, d.length as length FROM atlas_rucio.dataset_locks dsl JOIN atlas_rucio.rules r ON (dsl.rule_id = r.id) JOIN atlas_rucio.dids d ON (dsl.scope = d.scope and dsl.name = d.name)
+        sql = """SELECT dsl.scope as scope, dsl.name as name, rawtohex(r.id) as rule_id, r.rse_expression as rse_expression, r.subscription_id as subscription_id, d.bytes as bytes, d.length as length, (case when d.length < 1 then 0 else d.bytes/d.length end) as fsize FROM atlas_rucio.dataset_locks dsl JOIN atlas_rucio.rules r ON (dsl.rule_id = r.id) JOIN atlas_rucio.dids d ON (dsl.scope = d.scope and dsl.name = d.name)
 WHERE
 dsl.rse_id = atlas_rucio.rse2id(:rse) and
 (r.expires_at > sysdate+60 or r.expires_at is NULL) and
@@ -235,8 +235,9 @@ def list_rebalance_rule_candidates(rse, mode=None, session=None):
 d.is_open = 0 and
 d.did_type = 'D' and
 r.grouping IN ('D', 'A') and
+(case when d.length < 1 then 0 else d.bytes/d.length end) > 1000*1000*1000 and
 1 = (SELECT count(*) FROM atlas_rucio.dataset_locks WHERE scope=dsl.scope and name=dsl.name and rse_id = dsl.rse_id)
-ORDER BY dsl.accessed_at ASC NULLS FIRST, d.bytes DESC"""  # NOQA
+ORDER BY fsize DESC, dsl.accessed_at ASC NULLS FIRST, d.bytes DESC"""  # NOQA
     elif mode == 'decommission':  # OBSOLETE
         sql = """SELECT r.scope, r.name, rawtohex(r.id) as rule_id, r.rse_expression as rse_expression, r.subscription_id as subscription_id, 0 as bytes, 0 as length FROM atlas_rucio.rules r
 WHERE
@@ -335,7 +336,7 @@ def rebalance_rse(rse, max_bytes=1E9, max_files=None, dry_run=False, exclude_exp
 
     print 'scope:name rule_id bytes(Gb) target_rse child_rule_id'
 
-    for scope, name, rule_id, rse_expression, subscription_id, bytes, length in list_rebalance_rule_candidates(rse=rse, mode=mode):
+    for scope, name, rule_id, rse_expression, subscription_id, bytes, length, fsize in list_rebalance_rule_candidates(rse=rse, mode=mode):
         if force_expression is not None and subscription_id is not None:
             continue
 


Test patch: