Repository: dmwm/wmcore
Issue: MSTransferor: not full intersection of pileup and input primary data

**Impact of the bug**
ReqMgr2MS - MSTransferor

**Describe the bug**
As a follow up of: https://github.com/dmwm/WMCore/issues/9745

I have made changes to optimize amount of data to be transferred, however, that makes MSTransferor assume multiple locations for the pileup - while only one of them get the pileup transferred to - and multiple locations for the input blocks.

**How to reproduce it**
have a workflow requesting pileup and primary input (with multiple blocks).

**Expected behavior**
Given that we make a single copy request for the pileup dataset, we need to make sure that the same site will get all the primary blocks. There could be further optimizations to make sure we transfer the least possible amount of data, but we need to evaluate the extra complexity for this..

**Additional context and error message**
From MSTransfer logs:
```
2020-07-06 10:16:43,525:INFO:MSTransferor: Checking secondary data location for request: amaltaro_TC_6Tasks_Scratch_HG2007_Val_200706_081024_5434, TrustPUSitelists: False, request white/black list PNNs: set([u'T2_US_Wisconsin', u'T2_CH_CE
RN', u'T2_US_Nebraska'])
2020-07-06 10:16:43,525:INFO:MSTransferor: it has secondary: /MinBias_TuneCP5_13TeV-pythia8/RunIISummer19UL16SIM-106X_mcRun2_asymptotic_v3-v1/GEN-SIM, total size: 88518.12 GB, current disk locations: set([u'T1_UK_RAL_Disk', u'T1_US_FNAL_D
isk'])
2020-07-06 10:16:43,525:INFO:MSTransferor: secondary: /MinBias_TuneCP5_13TeV-pythia8/RunIISummer19UL16SIM-106X_mcRun2_asymptotic_v3-v1/GEN-SIM will need data placement!!!
2020-07-06 10:16:43,525:INFO:MSTransferor: Finding final pileup destination for request: amaltaro_TC_6Tasks_Scratch_HG2007_Val_200706_081024_5434
2020-07-06 10:16:43,525:INFO:MSTransferor:   found a PSN list: set([u'T2_US_Wisconsin', u'T2_US_Nebraska']), which maps to a list of PNNs: set([u'T2_US_Wisconsin', u'T2_US_Nebraska'])
2020-07-06 10:16:43,526:INFO:MSTransferor: Checking data location for request: amaltaro_TC_6Tasks_Scratch_HG2007_Val_200706_081024_5434, TrustSitelists: False, request white/black list PNNs: set([u'T2_US_Wisconsin', u'T2_CH_CERN', u'T2_US
_Nebraska'])
2020-07-06 10:16:43,526:INFO:MSTransferor: using: set([u'T2_US_Wisconsin', u'T2_US_Nebraska']) for primary/parent/pileup data placement
2020-07-06 10:16:43,526:INFO:MSTransferor: Checking primary data volume for: amaltaro_TC_6Tasks_Scratch_HG2007_Val_200706_081024_5434, allowed PNNs: set([u'T2_US_Wisconsin', u'T2_US_Nebraska'])
2020-07-06 10:16:43,526:INFO:MSTransferor: Request amaltaro_TC_6Tasks_Scratch_HG2007_Val_200706_081024_5434 has 18 initial blocks from getPrimaryBlocks
2020-07-06 10:16:43,526:INFO:MSTransferor: Request amaltaro_TC_6Tasks_Scratch_HG2007_Val_200706_081024_5434 has 0 initial blocks from getParentBlocks
2020-07-06 10:16:43,526:INFO:MSTransferor: Primary/parent data volume currently available:
2020-07-06 10:16:43,526:INFO:MSTransferor:   PNN: T2_US_Wisconsin               Data volume: 0.0 GB
2020-07-06 10:16:43,526:INFO:MSTransferor:   PNN: T2_US_Nebraska                Data volume: 0.0 GB
2020-07-06 10:16:43,526:INFO:MSTransferor: The PNN that would require less data to be transferred is: set([u'T2_US_Wisconsin', u'T2_US_Nebraska'])
2020-07-06 10:16:43,526:INFO:MSTransferor: However, the available data volume is too small, so using all PNNs: set([u'T2_US_Wisconsin', u'T2_US_Nebraska'])
2020-07-06 10:16:43,526:INFO:MSTransferor: Forcing all primary/parent data to be placed under: set([u'T2_US_Wisconsin', u'T2_US_Nebraska'])
2020-07-06 10:16:43,526:INFO:MSTransferor: Request amaltaro_TC_6Tasks_Scratch_HG2007_Val_200706_081024_5434 has 18 initial blocks from getPrimaryBlocks
```


Changed files:
src/python/WMCore/MicroService/Unified/MSTransferor.py

diff --git a/src/python/WMCore/MicroService/Unified/MSTransferor.py b/src/python/WMCore/MicroService/Unified/MSTransferor.py
index 5c84202d2..4b2e6f919 100644
--- a/src/python/WMCore/MicroService/Unified/MSTransferor.py
+++ b/src/python/WMCore/MicroService/Unified/MSTransferor.py
@@ -16,7 +16,7 @@ from httplib import HTTPException
 from operator import itemgetter
 from pprint import pformat
 from retry import retry
-from random import randint
+from random import randint, choice
 from copy import deepcopy
 
 # WMCore modules
@@ -389,11 +389,14 @@ class MSTransferor(MSCore):
             elif size == maxSize:
                 finalPNN.add(pnn)
         self.logger.info("The PNN that would require less data to be transferred is: %s", finalPNN)
-        # TODO: 100GB is a magic number here. It really depends on how much CPU/h the
-        # workflow would require, and perhaps something else
-        if maxSize < 100 * 1024 ** 3:
-            finalPNN = set(volumeByPNN.keys())
-            self.logger.info("However, the available data volume is too small, so using all PNNs: %s", finalPNN)
+        if len(finalPNN) > 1:
+            # magically picks one site from the list. It could pick the one with highest
+            # available quota, but that might overload that one site...
+            # make sure it's a set object
+            finalPNN = choice(list(finalPNN))
+            finalPNN = {finalPNN}
+            self.logger.info("Randomly picked PNN: %s as final location", finalPNN)
+
         return finalPNN
 
     def checkPUDataLocation(self, wflow):