-
Notifications
You must be signed in to change notification settings - Fork 13
mspilup check added #36
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
emcgrady
wants to merge
1
commit into
FNALLPC:master
Choose a base branch
from
emcgrady:mspileup_check
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,14 @@ | ||
#!/usr/bin/env python3 | ||
|
||
"""Returns a list of files from a dataset including only files that are hosted on disk.""" | ||
""" | ||
Returns a list of files from a dataset including only files that are hosted on disk. | ||
Requires access to MSPileup and the Rucio client. | ||
""" | ||
|
||
import os,sys,getpass,warnings,glob,shlex,subprocess,argparse # pylint: disable=multiple-imports | ||
import os,sys,getpass,warnings,glob,shlex,subprocess,argparse,requests # pylint: disable=multiple-imports | ||
from rucio.client.client import Client | ||
from collections import defaultdict | ||
from pwd import getpwuid | ||
|
||
# prevent this script from ever being used in a batch job | ||
# to avoid DDOS of Rucio | ||
|
@@ -17,46 +22,68 @@ def getOS(): | |
osv = subprocess.check_output(shlex.split(cmd), encoding="utf-8").rstrip() | ||
return osv | ||
|
||
def getX509(): | ||
if os.getenv('X509_USER_PROXY'): | ||
return os.getenv('X509_USER_PROXY') | ||
elif os.path.exists(f'/tmp/x509up_u{getpwuid(os.getuid()).pw_uid}'): | ||
return f'/tmp/x509up_u{getpwuid(os.getuid()).pw_uid}' | ||
else: | ||
raise Exception('An active X509 proxy must be used to run this script') | ||
|
||
def getHosted(dataset, user, allow=None, block=None): | ||
"""Gets list of files on disk for a dataset, and list of sites along with how many files each site has""" | ||
if allow is not None and block is not None: | ||
raise RuntimeError("Cannot specify both allow list and block list, pick one") | ||
|
||
osv = getOS() | ||
rucio_path = f'/cvmfs/cms.cern.ch/rucio/x86_64/rhel{osv}/py3/current' | ||
os.environ['RUCIO_HOME'] = rucio_path | ||
os.environ['RUCIO_ACCOUNT'] = user | ||
full_rucio_path = glob.glob(rucio_path+'/lib/python*.*')[0] | ||
sys.path.insert(0,full_rucio_path+'/site-packages/') | ||
|
||
warnings.filterwarnings("ignore", message=".*cryptography.*") | ||
from rucio.client.client import Client # pylint: disable=import-error,import-outside-toplevel | ||
client = Client() | ||
|
||
# loop over blocks to avoid timeout error from too-large response | ||
all_blocks = list(client.list_content(scope='cms',name=dataset)) | ||
# batch some blocks together for fewer requests | ||
# not fully optimized, but n=10 tested to be ~15% faster than n=1 | ||
nblocks = 10 | ||
block_groups = [all_blocks[i:i+nblocks] for i in range(0, len(all_blocks), nblocks)] | ||
|
||
from rucio.client.replicaclient import ReplicaClient # pylint: disable=import-error,import-outside-toplevel | ||
rep_client = ReplicaClient() | ||
|
||
filelist = set() | ||
sitelist = defaultdict(int) | ||
def sitecond(site): | ||
return ("_Tape" not in site) and (allow is None or site in allow) and (block is None or site not in block) | ||
for block_group in block_groups: | ||
reps = list(rep_client.list_replicas([{'scope': 'cms', 'name': block['name']} for block in block_group])) | ||
for rep in reps: | ||
for site,state in rep['states'].items(): | ||
if state=='AVAILABLE' and sitecond(site): | ||
filelist.add(rep['name']) | ||
sitelist[site] += 1 | ||
|
||
sys.path.pop(0) | ||
return filelist, sitelist | ||
#check if a customName is defined for the dataset | ||
|
||
proxy = getX509() | ||
msPileup = requests.get(f'https://cmsweb-prod.cern.ch/ms-pileup/data/pileup?pileupName={dataset}', | ||
cert=proxy, verify=False).json()['result'] | ||
if len(msPileup) > 0: | ||
customName = msPileup[0].get('customName', '') | ||
client = Client() | ||
if customName: | ||
scope = 'group.wmcore' | ||
dataset = customName | ||
else: | ||
scope = 'cms' | ||
rules = list(client.list_replication_rules({'name': dataset, 'account': 'wmcore_pileup', 'scope': scope})) | ||
if (len(rules) > 0) and msPileup['active']: | ||
osv = getOS() | ||
rucio_path = f'/cvmfs/cms.cern.ch/rucio/x86_64/rhel{osv}/py3/current' | ||
os.environ['RUCIO_HOME'] = rucio_path | ||
os.environ['RUCIO_ACCOUNT'] = user | ||
full_rucio_path = glob.glob(rucio_path+'/lib/python*.*')[0] | ||
sys.path.insert(0,full_rucio_path+'/site-packages/') | ||
|
||
all_blocks = list(client.list_content(scope=scope, name=dataset)) | ||
print(f'Found {len(all_blocks)} for dataset {dataset}...') | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. put this and other |
||
# loop over blocks to avoid timeout error from too-large response | ||
# not fully optimized, but n=10 tested to be ~15% faster than n=1 | ||
nblocks = 10 | ||
block_groups = [all_blocks[i:i+nblocks] for i in range(0, len(all_blocks), nblocks)] | ||
|
||
filelist = set() | ||
sitelist = defaultdict(int) | ||
def sitecond(site): | ||
return ("_Tape" not in site) and (allow is None or site in allow) and (block is None or site not in block) | ||
for block_group in block_groups: | ||
reps = list(client.list_replicas([{'scope': scope, 'name': block['name']} for block in block_group])) | ||
for rep in reps: | ||
for site,state in rep['states'].items(): | ||
if state=='AVAILABLE' and sitecond(site): | ||
filelist.add(rep['name']) | ||
sitelist[site] += 1 | ||
|
||
sys.path.pop(0) | ||
return filelist, sitelist | ||
else: | ||
print('The given container either has no active rules or is not active in MSPileup') | ||
return None, None | ||
else: | ||
print('The given container was not found on MSPileup') | ||
return None, None | ||
|
||
def main(dataset, user, outfile=None, verbose=False, allow=None, block=None): | ||
"""Prints file list and site list""" | ||
|
@@ -85,4 +112,4 @@ def main(dataset, user, outfile=None, verbose=False, allow=None, block=None): | |
parser.add_argument("dataset",type=str,help="dataset to query") | ||
args = parser.parse_args() | ||
|
||
main(args.dataset, args.user, outfile=args.outfile, verbose=args.verbose, allow=args.allow, block=args.block) | ||
main(args.dataset, args.user, outfile=args.outfile, verbose=args.verbose, allow=args.allow, block=args.block) |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
making this a requirement to run the script is an unnecessary restriction (and actually alters the nature/usage of the script). This should be a preferred option in the case of a pileup dataset, but the script should still execute if a connection to ms-pileup is not available, or if the user is asking about a non-pileup dataset.