Skip to content
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

Common: Rewrite check_obsolete_replicas #145

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
109 changes: 39 additions & 70 deletions common/check_obsolete_replicas
Original file line number Diff line number Diff line change
@@ -1,84 +1,53 @@
#!/usr/bin/env python
# Copyright European Organization for Nuclear Research (CERN) 2013
#!/usr/bin/env python3
# Copyright European Organization for Nuclear Research (CERN) since 2012
#
# Licensed under the Apache License, Version 2.0 (the "License");
# You may not use this file except in compliance with the License.
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# Authors:
# - Vincent Garonne, <[email protected]>, 2015
# - Cedric Serfon, <[email protected]>, 2018
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

'''
Probe to check the backlog of obsolete replicas.
'''
"""
Calculate the amount of obsolete replicas and update the RSE usage counters.
"""

import sys
import traceback

from sqlalchemy import func, select

from rucio.db.sqla import models
from rucio.db.sqla.constants import OBSOLETE
from rucio.db.sqla.session import get_session
from rucio.gateway.rse import list_rses, set_rse_usage

# Exit statuses
OK, WARNING, CRITICAL, UNKNOWN = 0, 1, 2, 3


if __name__ == "__main__":
if __name__ == '__main__':
try:
SESSION = get_session()
QUERY = '''BEGIN
FOR u in (SELECT
a.rse_id AS rse_id,
NVL(b.files, 0) AS files,
NVL(b.bytes, 0) AS bytes,
SYS_EXTRACT_UTC(localtimestamp) AS updated_at
FROM
(
SELECT
id AS rse_id
FROM
atlas_rucio.rses
WHERE
deleted=0) a
LEFT OUTER JOIN
(
SELECT
/*+ INDEX_FFS(replicas REPLICAS_TOMBSTONE_IDX) */
rse_id,
COUNT(1) AS files,
SUM(bytes) AS bytes
FROM
atlas_rucio.replicas
WHERE
(
CASE
WHEN tombstone IS NOT NULL
THEN rse_id
END) IS NOT NULL
AND tombstone=to_date('1-1-1970 00:00:00','MM-DD-YYYY HH24:Mi:SS')
GROUP BY
rse_id) b
ON
a.rse_id=b.rse_id)

LOOP
MERGE INTO atlas_rucio.RSE_USAGE
USING DUAL
ON (atlas_rucio.RSE_USAGE.rse_id = u.rse_id and source = 'obsolete')
WHEN NOT MATCHED THEN INSERT(rse_id, source, used, files, updated_at, created_at)
VALUES (u.rse_id, 'obsolete', u.bytes, u.files, u.updated_at, u.updated_at)
WHEN MATCHED THEN UPDATE SET used=u.bytes, files=u.files, updated_at=u.updated_at;

MERGE INTO ATLAS_RUCIO.RSE_USAGE_HISTORY H
USING DUAL
ON (h.rse_id = u.rse_id and h.source = 'obsolete' and h.updated_at = u.updated_at)
WHEN NOT MATCHED THEN INSERT(rse_id, source, used, files, updated_at, created_at)
VALUES (u.rse_id, 'obsolete', u.bytes, u.files, u.updated_at, u.updated_at);

COMMIT;
END LOOP;
END;
'''
SESSION.execute(QUERY)
except Exception as error:
print error
session = get_session()
for rse in list_rses(session=session):
query = select(
func.coalesce(func.sum(models.RSEFileAssociation.bytes), 0).label('bytes'),
func.count().label('files')
).with_hint(
models.RSEFileAssociation, 'INDEX(replicas REPLICAS_RSE_ID_TOMBSTONE_IDX)', 'oracle'
).where(
models.RSEFileAssociation.rse_id == rse['id'],
models.RSEFileAssociation.tombstone == OBSOLETE
)

bytes_, files = session.execute(query).one()
set_rse_usage(rse=rse['rse'], source='obsolete', used=bytes_,
free=None, issuer='root', files=files,
session=session)
except Exception:
print(traceback.format_exc())
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should there be a session.rollback() here?

Copy link
Contributor Author

@dchristidis dchristidis Aug 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More importantly, I’m missing a session.commit(), so many thanks for your comment.

But I’m not yet convinced what I’m doing here is a good idea. If we didn’t have a custom query, we wouldn’t try to tie the gateway calls to a particular session. Is there a benefit to ensuring either all of the RSE usage counters are updated or none at all? I’m not sure. My initial motivation was to be able to add a dry-run mode easily, but this may not be the best way to proceed.

sys.exit(UNKNOWN)
sys.exit(OK)