Skip to content

Commit 8509c56

Browse files
committed
Fixing test issues by reframing connect.
1 parent 8cfbdff commit 8509c56

File tree

4 files changed

+25
-21
lines changed

4 files changed

+25
-21
lines changed

ols/runners/quota_scheduler.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@
3232
# Reconciliation SQL for user quotas - updates all user rows
3333
RECONCILE_USER_QUOTA_LIMITS_STATEMENT = """
3434
UPDATE quota_limits
35-
SET quota_limit = %s,
36-
available = available + (%s - quota_limit),
35+
SET available = available + (%s - quota_limit),
36+
quota_limit = %s,
3737
updated_at = NOW()
3838
WHERE subject = 'u'
3939
AND quota_limit != %s
@@ -43,8 +43,8 @@
4343
# Reconciliation SQL for cluster quotas - updates cluster row
4444
RECONCILE_CLUSTER_QUOTA_LIMITS_STATEMENT = """
4545
UPDATE quota_limits
46-
SET quota_limit = %s,
47-
available = available + (%s - quota_limit),
46+
SET available = available + (%s - quota_limit),
47+
quota_limit = %s,
4848
updated_at = NOW()
4949
WHERE subject = 'c'
5050
AND quota_limit != %s
@@ -203,8 +203,8 @@ def reconcile_quota_limits(
203203
cursor.execute(
204204
reconcile_sql,
205205
(
206-
new_quota, # new quota_limit value
207206
new_quota, # used in calculation: available + (new - old)
207+
new_quota, # new quota_limit value to set
208208
new_quota, # WHERE quota_limit != ?
209209
),
210210
)

ols/src/quota/quota_limiter_factory.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
from ols.app.models.config import PostgresConfig, QuotaHandlersConfig
77
from ols.src.quota.cluster_quota_limiter import ClusterQuotaLimiter
88
from ols.src.quota.quota_limiter import QuotaLimiter
9+
from ols.src.quota.revokable_quota_limiter import RevokableQuotaLimiter
910
from ols.src.quota.user_quota_limiter import UserQuotaLimiter
1011

1112
logger = logging.getLogger(__name__)
@@ -42,6 +43,8 @@ def quota_limiters(config: QuotaHandlersConfig) -> list[QuotaLimiter]:
4243
limiter = QuotaLimiterFactory.create_limiter(
4344
storage_config, limiter_type, initial_quota, increase_by
4445
)
46+
if isinstance(limiter, RevokableQuotaLimiter):
47+
limiter.reconcile_quota_limits()
4548
limiters.append(limiter)
4649
logger.info("Set up quota limiter '%s'", name)
4750
return limiters

ols/src/quota/revokable_quota_limiter.py

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -52,8 +52,8 @@ class RevokableQuotaLimiter(QuotaLimiter):
5252
# Reconciliation SQL for user quotas - updates all user rows
5353
RECONCILE_USER_QUOTA_LIMITS = """
5454
UPDATE quota_limits
55-
SET quota_limit = %s,
56-
available = available + (%s - quota_limit),
55+
SET available = available + (%s - quota_limit),
56+
quota_limit = %s,
5757
updated_at = NOW()
5858
WHERE subject = 'u'
5959
AND quota_limit != %s
@@ -63,8 +63,8 @@ class RevokableQuotaLimiter(QuotaLimiter):
6363
# Reconciliation SQL for cluster quotas - updates cluster row
6464
RECONCILE_CLUSTER_QUOTA_LIMITS = """
6565
UPDATE quota_limits
66-
SET quota_limit = %s,
67-
available = available + (%s - quota_limit),
66+
SET available = available + (%s - quota_limit),
67+
quota_limit = %s,
6868
updated_at = NOW()
6969
WHERE subject = 'c'
7070
AND quota_limit != %s
@@ -84,20 +84,18 @@ def __init__(
8484
self.increase_by = increase_by
8585
self.connection_config = connection_config
8686

87-
def connect(self) -> None:
88-
"""Initialize connection to database and reconcile quota limits.
87+
def reconcile_quota_limits(self) -> None:
88+
"""Reconcile quota limits with current configuration.
8989
90-
Reconciliation ensures database quota limits match OLSConfig after updates.
91-
This happens on first request after config changes, ensuring immediate sync.
92-
The quota_scheduler also runs reconciliation periodically as a backup.
90+
Public method to trigger reconciliation after initialization.
91+
Should be called after quota limiters are created to ensure
92+
database quota limits match OLSConfig after updates.
9393
"""
94-
super().connect()
95-
# Reconcile on connect so changes take effect on first request after OLSConfig update
9694
try:
9795
self._reconcile_quota_limits()
9896
except Exception as e:
9997
logger.warning(
100-
"Quota reconciliation on connect failed (will retry on scheduler): %s", e
98+
"Quota reconciliation failed (will retry on scheduler): %s", e
10199
)
102100

103101
@connection
@@ -232,8 +230,8 @@ def _reconcile_quota_limits(self) -> None:
232230
cursor.execute(
233231
reconcile_sql,
234232
(
235-
self.initial_quota, # new quota_limit
236233
self.initial_quota, # used in calculation: available + (new - old)
234+
self.initial_quota, # new quota_limit to set
237235
self.initial_quota, # WHERE quota_limit != ?
238236
),
239237
)

ols/utils/config.py

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -83,9 +83,12 @@ def conversation_cache(self) -> Cache:
8383
def quota_limiters(self) -> list[QuotaLimiter]:
8484
"""Return all quota limiters."""
8585
if self._quota_limiters is None:
86-
self._quota_limiters = QuotaLimiterFactory.quota_limiters(
87-
self.ols_config.quota_handlers
88-
)
86+
if self.ols_config.quota_handlers is not None:
87+
self._quota_limiters = QuotaLimiterFactory.quota_limiters(
88+
self.ols_config.quota_handlers
89+
)
90+
else:
91+
self._quota_limiters = []
8992
return self._quota_limiters
9093

9194
@property

0 commit comments

Comments
 (0)