Skip to content

Commit 2f8b0bd

Browse files
committed
Common: Update query to use sqlalchemy #127
1 parent 8602fb3 commit 2f8b0bd

1 file changed

Lines changed: 40 additions & 20 deletions

File tree

common/check_stuck_rules

Lines changed: 40 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
#!/usr/bin/env python
2-
# Copyright European Organization for Nuclear Research (CERN) 2013
1+
#!/usr/bin/env python3
2+
# Copyright European Organization for Nuclear Research (CERN) since 2012
33
#
44
# Licensed under the Apache License, Version 2.0 (the "License");
5-
# You may not use this file except in compliance with the License.
6-
# You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0
5+
# you may not use this file except in compliance with the License.
6+
# You may obtain a copy of the License at
77
#
8-
# Authors:
9-
# - Martin Barisits, <martin.barisits@cern.ch>, 2014
10-
# - Eric Vaandering, <ewv@fnal.gov>, 2019-2021
11-
# - Thomas Beermann, <thomas.beermann@cern.ch>, 2019
8+
# http://www.apache.org/licenses/LICENSE-2.0
9+
#
10+
# Unless required by applicable law or agreed to in writing, software
11+
# distributed under the License is distributed on an "AS IS" BASIS,
12+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
# See the License for the specific language governing permissions and
14+
# limitations under the License.
1215

1316
"""
1417
Probe to check the backlog of stuck rules.
@@ -19,19 +22,17 @@ import sys
1922
import traceback
2023

2124
from prometheus_client import CollectorRegistry, Gauge, push_to_gateway
25+
from sqlalchemy.sql import and_, func, null, or_, select
26+
2227
from rucio.common.config import config_get
23-
from rucio.db.sqla.session import BASE, get_session
28+
from rucio.db.sqla import models
29+
from rucio.db.sqla.session import get_session
2430

2531
from utils.common import probe_metrics
2632

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

30-
if BASE.metadata.schema:
31-
schema = BASE.metadata.schema + '.'
32-
else:
33-
schema = ''
34-
3536
PROM_SERVERS = config_get('monitor', 'prometheus_servers', raise_exception=False, default='')
3637
if PROM_SERVERS != '':
3738
PROM_SERVERS = PROM_SERVERS.split(',')
@@ -40,15 +41,34 @@ if __name__ == "__main__":
4041
try:
4142
registry = CollectorRegistry()
4243
session = get_session()
43-
sql = 'SELECT COUNT(1) FROM {schema}RULES where state=\'S\' and (error !=\'MissingSourceReplica\' or error IS NULL)'.format(
44-
schema=schema)
45-
result = session.execute(sql).fetchone()[0]
44+
without_missing_replica_statement = select(
45+
func.count()
46+
).select_from(
47+
models.ReplicationRule
48+
).where(
49+
and_(
50+
models.ReplicationRule.state == "S",
51+
or_(
52+
models.ReplicationRule.error != "MissingSourceReplica",
53+
models.ReplicationRule.error == null()
54+
)
55+
)
56+
)
57+
result = session.execute(without_missing_replica_statement).scalar_one()
4658
probe_metrics.gauge(name='judge.stuck_rules_without_missing_source_replica').set(result)
4759
Gauge('judge_stuck_rules_without_missing_source_replica', '', registry=registry).set(result)
4860

49-
sql = 'SELECT COUNT(1) FROM {schema}RULES where state=\'S\' and error =\'MissingSourceReplica\''.format(
50-
schema=schema)
51-
result = session.execute(sql).fetchone()[0]
61+
with_missing_replica_statement = select(
62+
func.count()
63+
).select_from(
64+
models.ReplicationRule
65+
).where(
66+
and_(
67+
models.ReplicationRule.state == "S",
68+
models.ReplicationRule.error == "MissingSourceReplica"
69+
)
70+
)
71+
result = session.execute(with_missing_replica_statement).scalar_one()
5272
probe_metrics.gauge(name='judge.stuck_rules_with_missing_source_replica').set(result)
5373
Gauge('judge_stuck_rules_with_missing_source_replica', '', registry=registry).set(result)
5474

0 commit comments

Comments
 (0)