Skip to content

Commit

Permalink
Merge pull request #4371 from ChristophAmes/patch-4364-Add_command_li…
Browse files Browse the repository at this point in the history
…st_suspicious_replicas_to_API

API: Add command list-suspicious-replicas #4364
  • Loading branch information
bari12 committed Mar 19, 2021
2 parents 262ced1 + 9800e19 commit 398df65
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 0 deletions.
39 changes: 39 additions & 0 deletions bin/rucio
Expand Up @@ -42,6 +42,7 @@
# - Aristeidis Fkiaras <aristeidis.fkiaras@cern.ch>, 2020
# - Patrick Austin <patrick.austin@stfc.ac.uk>, 2020
# - Radu Carpa <radu.carpa@cern.ch>, 2021
# - Christoph Ames <christoph.ames@cern.ch>, 2021

from __future__ import print_function

Expand Down Expand Up @@ -1491,6 +1492,35 @@ def list_rses(args):
return SUCCESS


@exception_handler
def list_suspicious_replicas(args):
"""
%(prog)s list-suspicious-replicas [options] <field1=value1 field2=value2 ...>
List replicas marked as suspicious.
"""
client = get_client(args)
rse_expression = None
younger_than = None
nattempts = None
if args.rse_expression:
rse_expression = args.rse_expression
if args.younger_than:
younger_than = args.younger_than
if args.nattempts:
nattempts = args.nattempts
# Generator is a list with one entry, which itself is a list of lists.
replicas_gen = client.list_suspicious_replicas(rse_expression, younger_than, nattempts)
for i in replicas_gen:
replicas = i
table = []
for rep in replicas:
table.append([rep['rse'], rep['scope'], rep['created_at'], rep['cnt'], rep['name']])
print(tabulate.tabulate(table, headers=(['RSE Expression:', 'Scope:', 'Created at:', 'Nattempts:', 'File Name:'])))
return SUCCESS


@exception_handler
def list_rse_attributes(args):
"""
Expand Down Expand Up @@ -2271,6 +2301,15 @@ You can filter by account::
list_rses_parser.add_argument('--expression', dest='rse_expression', action='store', help='The RSE filter expression. A comprehensive help about RSE expressions \
can be found in ' + Color.BOLD + 'http://rucio.cern.ch/client_tutorial.html#adding-rules-for-replication' + Color.END)

# The list-suspicoius-replicas command
list_suspicious_replicas_parser = subparsers.add_parser('list-suspicious-replicas', help='Show the list of all replicas marked "suspicious".')
list_suspicious_replicas_parser.set_defaults(function=list_suspicious_replicas)
list_suspicious_replicas_parser.add_argument('--expression', dest='rse_expression', action='store', help='The RSE filter expression. A comprehensive help about RSE expressions \
can be found in ' + Color.BOLD + 'http://rucio.cern.ch/client_tutorial.html#adding-rules-for-replication' + Color.END)
list_suspicious_replicas_parser.add_argument('--younger_than', dest='younger_than', action='store', help='List files that have been marked suspicious since the date "younger_than".')
list_suspicious_replicas_parser.add_argument('--nattempts', dest='nattempts', action='store', help='Minimum number of failed attempts to access a suspicious file.')
list_suspicious_replicas_parser.add_argument('--state', dest='state', action='store', help='Either "BAD" or "SUSPICIOUS". If not specified, both bad and suspicious file are listed.')

# The list-rses-attributes command
list_rse_attributes_parser = subparsers.add_parser('list-rse-attributes', help='List the attributes of an RSE.', description='This command is useful to create RSE filter expressions.')
list_rse_attributes_parser.set_defaults(function=list_rse_attributes)
Expand Down
30 changes: 30 additions & 0 deletions lib/rucio/client/replicaclient.py
Expand Up @@ -27,6 +27,8 @@
# - Andrew Lister <andrew.lister@stfc.ac.uk>, 2019
# - Benedikt Ziemons <benedikt.ziemons@cern.ch>, 2020
# - Eric Vaandering <ewv@fnal.gov>, 2020
# - Christoph Ames <christoph.ames@cern.ch>, 2021


try:
from urllib import quote_plus
Expand Down Expand Up @@ -193,6 +195,34 @@ def list_replicas(self, dids, schemes=None, unavailable=False,
exc_cls, exc_msg = self._get_exception(headers=r.headers, status_code=r.status_code, data=r.content)
raise exc_cls(exc_msg)

def list_suspicious_replicas(self, rse_expression=None, younger_than=None, nattempts=None):
"""
List file replicas tagged as suspicious.
:param rse_expression: The RSE expression to restrict replicas on a set of RSEs.
:param younger_than: Datetime object to select the replicas which were declared since younger_than date. Default value = 10 days ago.
:param nattempts: The minimum number of replica appearances in the bad_replica DB table from younger_than date. Default value = 0.
:param state: State of the replica, either 'BAD' or 'SUSPICIOUS'. No value returns replicas with either state.
"""
params = {}
if rse_expression:
params['rse_expression'] = rse_expression

if younger_than:
params['younger_than'] = younger_than

if nattempts:
params['nattempts'] = nattempts

url = build_url(choice(self.list_hosts),
path='/'.join([self.REPLICAS_BASEURL, 'suspicious']))
r = self._send_request(url, type='GET', params=params)
if r.status_code == codes.ok:
return self._load_json_data(r)
exc_cls, exc_msg = self._get_exception(headers=r.headers, status_code=r.status_code, data=r.content)
raise exc_cls(exc_msg)

def add_replica(self, rse, scope, name, bytes, adler32, pfn=None, md5=None, meta={}):
"""
Add file replicas to a RSE.
Expand Down

0 comments on commit 398df65

Please sign in to comment.