-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathchute_autoscaler.py
More file actions
3351 lines (2988 loc) · 144 KB
/
chute_autoscaler.py
File metadata and controls
3351 lines (2988 loc) · 144 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
Auto-scale chutes based on utilization.
"""
import gc
import os
import math
import asyncio
import argparse
import random
import uuid
from contextlib import asynccontextmanager
from functools import wraps
from collections import defaultdict
from loguru import logger
from datetime import timedelta, datetime, timezone
from typing import Dict, Optional, Set, List, Tuple
import aiohttp
from io import StringIO
from rich.console import Console
from rich.table import Table
from sqlalchemy import (
text,
select,
func,
and_,
or_,
)
from sqlalchemy.exc import OperationalError
import api.database.orms # noqa
from sqlalchemy.orm import selectinload, joinedload
from api.database import get_session
from api.config import settings
from api.bounty.util import (
check_bounty_exists,
delete_bounty,
get_bounty_info,
get_bounty_infos,
send_bounty_notification,
)
from api.user.service import chutes_user_id
from api.util import has_legacy_private_billing, notify_deleted
from api.chute.schemas import Chute, NodeSelector, RollingUpdate
from api.gpu import COMPUTE_UNIT_PRICE_BASIS
from api.instance.schemas import Instance, LaunchConfig
from api.instance.util import invalidate_instance_cache
from api.metrics.util import reconcile_connection_counts
from api.capacity_log.schemas import CapacityLog
from api.instance.util import purge, purge_and_notify # noqa
from api.constants import (
UNDERUTILIZED_CAP,
UTILIZATION_SCALE_UP,
UTILIZATION_SCALE_DOWN,
RATE_LIMIT_SCALE_UP,
SCALE_DOWN_LOOKBACK_MINUTES,
SCALE_DOWN_MAX_DROP_RATIO,
)
# Distributed lock to prevent concurrent autoscaler runs
AUTOSCALER_LOCK_KEY = "autoscaler:lock"
AUTOSCALER_LOCK_TTL = 180 # 3 minutes max (should complete in <1 min normally)
class LockNotAcquired(Exception):
"""Raised when the autoscaler lock cannot be acquired."""
pass
@asynccontextmanager
async def autoscaler_lock(soft_mode: bool = False, skip_lock: bool = False):
"""
Distributed lock using Redis to prevent concurrent autoscaler runs.
Uses SET NX with expiry to atomically acquire lock.
Releases lock on exit (or lets it expire if process crashes).
If soft_mode=True and lock is held, exits quietly instead of raising.
Full mode retries for up to 60 seconds before failing.
If skip_lock=True (for dry-run), yields immediately without acquiring lock.
"""
if skip_lock:
logger.info("Skipping lock acquisition (dry-run mode)")
yield
return
lock_id = str(uuid.uuid4())
acquired = False
max_retries = 12 # 12 retries * 5 seconds = 60 seconds
retry_delay = 5
try:
for attempt in range(max_retries):
acquired = await settings.redis_client.set(
AUTOSCALER_LOCK_KEY,
lock_id,
nx=True,
ex=AUTOSCALER_LOCK_TTL,
)
if acquired:
break
ttl = await settings.redis_client.ttl(AUTOSCALER_LOCK_KEY)
if soft_mode:
logger.info(f"Lock held (TTL: {ttl}s), skipping soft mode run")
raise LockNotAcquired()
# Full mode: retry with backoff
if attempt < max_retries - 1:
logger.info(
f"Lock held (TTL: {ttl}s), retrying in {retry_delay}s "
f"(attempt {attempt + 1}/{max_retries})"
)
await asyncio.sleep(retry_delay)
else:
raise RuntimeError(
f"Another autoscaler is running (lock TTL: {ttl}s). "
f"Gave up after {max_retries} retries (~{max_retries * retry_delay}s)."
)
logger.info(f"Acquired autoscaler lock: {lock_id}")
yield
finally:
if acquired:
current = await settings.redis_client.get(AUTOSCALER_LOCK_KEY)
if current and current.decode() == lock_id:
await settings.redis_client.delete(AUTOSCALER_LOCK_KEY)
logger.info(f"Released autoscaler lock: {lock_id}")
def retry_on_db_failure(max_retries=3, delay=1.0):
"""
Decorator to retry async DB operations on OperationalError (timeouts/deadlocks).
"""
def decorator(func):
@wraps(func)
async def wrapper(*args, **kwargs):
last_error = None
for attempt in range(max_retries):
try:
return await func(*args, **kwargs)
except OperationalError as e:
last_error = e
logger.warning(
f"Database operation {func.__name__} failed (attempt {attempt + 1}/{max_retries}): {e}. "
f"Retrying in {delay}s..."
)
await asyncio.sleep(delay)
logger.error(f"Database operation {func.__name__} failed after {max_retries} attempts.")
raise last_error
return wrapper
return decorator
@retry_on_db_failure()
async def get_scale_down_permission(
chute_id: str, current_count: int, proposed_target: int, current_rate_limit: float = 0
) -> Tuple[bool, str]:
"""
Check if scale-down is permitted based on historical capacity_log trends.
Returns (permitted, reason) tuple.
Scale-down is permitted if:
1. We have enough historical data (at least 3 samples)
2. Proposed target isn't drastically below recent average (within SCALE_DOWN_MAX_DROP_RATIO)
3. No significant rate limiting is currently occurring (uses current metrics, not historical)
This prevents thrashing and respects bursty traffic patterns.
"""
# Check for current rate limiting first (using live metrics passed from context)
# Previously this checked MAX(rate_limit) over 90 minutes which was too conservative -
# old rate limiting would block scale-downs even after traffic had subsided
if current_rate_limit >= 0.01:
return False, f"rate_limiting_in_window ({current_rate_limit:.1%})"
async with get_session() as session:
await session.execute(text("SET LOCAL statement_timeout = '5s'"))
result = await session.execute(
text("""
SELECT
AVG(target_count) as avg_target,
MAX(target_count) as max_target,
AVG(utilization_15m) as avg_util,
COUNT(*) as sample_count
FROM capacity_log
WHERE chute_id = :chute_id
AND timestamp >= NOW() - make_interval(mins => :lookback_minutes)
"""),
{"chute_id": chute_id, "lookback_minutes": SCALE_DOWN_LOOKBACK_MINUTES},
)
row = result.fetchone()
if not row or row.sample_count < 3:
return False, "insufficient_history"
# Check if proposed target is within acceptable range of rolling average
min_allowed_target = max(1, int(float(row.avg_target) * SCALE_DOWN_MAX_DROP_RATIO))
if proposed_target < min_allowed_target:
return (
False,
f"below_moving_avg (proposed={proposed_target}, avg={row.avg_target:.1f}, min_allowed={min_allowed_target})",
)
return True, "permitted"
# Constants
PROMETHEUS_URL = os.getenv("PROMETHEUS_URL", "http://prometheus-server")
MIN_CHUTES_FOR_SCALING = 10
PRICE_COMPATIBILITY_THRESHOLD = 0.67
AUTOSCALER_FULL_INTERVAL_SECONDS = int(os.getenv("AUTOSCALER_FULL_INTERVAL_SECONDS", "1200"))
AUTOSCALER_SOFT_INTERVAL_SECONDS = int(os.getenv("AUTOSCALER_SOFT_INTERVAL_SECONDS", "120"))
# Forced donation anti-thrashing settings
# Chutes that were starving recently cannot be donors (prevents A->B->A donation cycles)
STARVING_COOLDOWN_MINUTES = 90
# Redis key prefix for tracking recently starving chutes
STARVING_HISTORY_KEY_PREFIX = "starving:"
# Higher min instance counts for some chutes...
LIMIT_OVERRIDES = {}
FAILSAFE = {
"aac09863-35b4-5d9b-9b67-6e6a9d54273a": 5,
"08a7a60f-6956-5a9e-9983-5603c3ac5a38": 4,
"b048fe26-0352-5c46-acf7-335e527e7f3d": 12,
"e51e818e-fa63-570d-9f68-49d7d1b4d12f": 10,
"2ff25e81-4586-5ec8-b892-3a6f342693d7": 18,
"d899b064-d9ae-5612-99e6-413e9136671b": 4,
"ac059e33-eb27-541c-b9a9-24b214036475": 7,
}
class AutoScaleContext:
def __init__(
self,
chute_id,
metrics,
info,
supported_gpus,
instances: List[Instance],
db_now: datetime,
gpu_count=None,
avg_instance_counts: Dict[str, float] = None,
):
self.chute_id = chute_id
self.metrics = metrics
self.info = info
self.supported_gpus = supported_gpus
if gpu_count is None and info:
node_selector = getattr(info, "node_selector", None) or {}
gpu_count = node_selector.get("gpu_count")
self.gpu_count = gpu_count
self.tee = info.tee if info else False
self.current_version = info.version if info else None
self.instances = instances
self.db_now = db_now
# Map actual hardware to specific instance objects
# Only include established instances (active for 1+ hour) for donor consideration
self.hardware_map = defaultdict(list)
self.established_instance_count = 0
self.old_instance_count = 0
for inst in instances:
if inst.nodes:
is_established = db_now.replace(tzinfo=None) - inst.activated_at.replace(
tzinfo=None
) >= timedelta(minutes=63)
if is_established:
gpu_id = inst.nodes[0].gpu_identifier
self.hardware_map[gpu_id].append(inst)
self.established_instance_count += 1
if self.current_version and inst.version != self.current_version:
self.old_instance_count += 1
# Computed metrics
self.utilization_basis = max(
metrics["utilization"].get("5m", 0), metrics["utilization"].get("15m", 0)
)
self.utilization_2h = metrics["utilization"].get("2h", 0.0)
# Track all rate limit windows
self.rate_limit_5m = metrics["rate_limit_ratio"].get("5m", 0)
self.rate_limit_15m = metrics["rate_limit_ratio"].get("15m", 0)
self.rate_limit_1h = metrics["rate_limit_ratio"].get("1h", 0)
# For scale-up decisions, use the most recent rate limit values
self.rate_limit_basis = max(self.rate_limit_5m, self.rate_limit_15m)
# For scale-down prevention, ANY rate limiting in any window blocks it
self.any_rate_limiting = (
self.rate_limit_5m > 0 or self.rate_limit_15m > 0 or self.rate_limit_1h > 0
)
# Request volume for demand-based scaling
self.completed_5m = metrics["completed_requests"].get("5m", 0)
self.completed_15m = metrics["completed_requests"].get("15m", 0)
self.rate_limited_count_5m = metrics["rate_limited_requests"].get("5m", 0)
self.rate_limited_count_15m = metrics["rate_limited_requests"].get("15m", 0)
self.current_count = info.instance_count if info else 0
self.threshold = info.scaling_threshold if info else UTILIZATION_SCALE_UP
if not self.threshold:
self.threshold = UTILIZATION_SCALE_UP
# Scale-down threshold is proportionally lower than scale-up threshold
# Default: 0.35/0.6 = 0.583 ratio
self.scale_down_threshold = self.threshold * (UTILIZATION_SCALE_DOWN / UTILIZATION_SCALE_UP)
# Starving threshold: when utilization is high enough to justify forced donations
# Fixed at 80%, unless the chute's scaling threshold is >= 80%, then 10% above that (capped at 100%)
self.starving_threshold = min(1.0, max(0.80, self.threshold * 1.10))
self.has_rolling_update = info.has_rolling_update if info else False
# max_instances: None means unbounded, use a large number for comparisons
self.max_instances = info.max_instances if (info and info.max_instances) else 10000
self.public = info.public if info else True
# Pending instances: unverified but recently created (in process of starting up)
self.pending_instance_count = info.pending_instance_count if info else 0
# Concurrency: how many concurrent requests per instance before rate limiting
self.concurrency = info.concurrency if info else 1
# Optional manual boost multiplier (fine-tuning)
self.manual_boost = info.manual_boost if info else 1.0
# Decision outputs
self.target_count = self.current_count
self.action = "no_action"
self.urgency_score = 0.0
self.smoothed_urgency = 0.0
self.smoothed_util = self.utilization_basis
self.is_starving = False
self.is_donor = False
self.is_critical_donor = False
self.blocked_by_starving = False
self.downscale_amount = 0
self.upscale_amount = 0
self.preferred_downscale_gpus = set()
self.boost = 1.0
self.base_multiplier = 0.0 # Base compute multiplier from node selector
self.effective_multiplier = 0.0 # Total effective compute multiplier for miners
self.cm_delta_ratio = 0.0 # Ratio of effective/base (how much boost overall)
self.has_bounty = False
self.standard_template = info.standard_template if info else None
# Revenue-weighted urgency fields
# Total revenue from Prometheus, divided by time-weighted avg instance count from DB
rev_total = metrics.get("revenue_total", {})
avg_inst = avg_instance_counts or {}
self.avg_instances_2h = avg_inst.get("2h", 0.0)
rev_30m = rev_total.get("30m", 0.0) / max(avg_inst.get("30m", 0.0), 1.0)
rev_1h = rev_total.get("1h", 0.0) / max(avg_inst.get("1h", 0.0), 1.0)
rev_2h = rev_total.get("2h", 0.0) / max(avg_inst.get("2h", 0.0), 1.0)
# Recency-weighted per-instance revenue signal
self.revenue_per_instance = rev_30m * 1.0 + rev_1h * 0.3 + rev_2h * 0.1
self.revenue_factor = 1.0
# Hourly cost per instance for profitability calculation
try:
ns = NodeSelector(**info.node_selector) if info else None
self.hourly_cost = COMPUTE_UNIT_PRICE_BASIS * ns.compute_multiplier if ns else 0.0
except Exception:
self.hourly_cost = 0.0
# Hourly revenue per instance (2h total revenue / avg instances, scaled to hourly)
self.hourly_revenue_per_instance = rev_2h / 2.0 # 2h window / 2 = hourly rate
# Total hourly revenue (raw from Prometheus, not multiplied back up)
self.hourly_revenue_total = rev_total.get("2h", 0.0) / 2.0
self.profitable = True # default, computed in second pass
async def instance_cleanup():
"""
Clean up instances that should have been verified by now.
"""
async with get_session() as session:
query = (
select(Instance)
.join(LaunchConfig, Instance.config_id == LaunchConfig.config_id, isouter=True)
.where(
or_(
and_(
Instance.verified.is_(False),
or_(
and_(
Instance.config_id.isnot(None),
Instance.created_at <= func.now() - timedelta(hours=3, minutes=30),
),
and_(
Instance.config_id.is_(None),
Instance.created_at <= func.now() - timedelta(hours=3, minutes=30),
),
),
),
and_(
Instance.verified.is_(True),
Instance.active.is_(False),
Instance.config_id.isnot(None),
LaunchConfig.verified_at <= func.now() - timedelta(hours=3, minutes=0),
),
)
)
.options(joinedload(Instance.chute))
)
total = 0
for instance in (await session.execute(query)).unique().scalars().all():
delta = int((datetime.now() - instance.created_at.replace(tzinfo=None)).total_seconds())
logger.warning(
f"Purging instance {instance.instance_id} of {instance.chute.name} "
f"which was created {instance.created_at} ({delta} seconds ago)..."
)
logger.warning(f" {instance.verified=} {instance.active=}")
await purge_and_notify(
instance,
reason="autoscaler - instance failed to verify within a reasonable amount of time",
)
total += 1
if total:
logger.success(f"Purged {total} total unverified+old instances.")
# EMA smoothing for stability
EMA_ALPHA_URGENCY = 0.3 # For urgency/boost calculations
EMA_ALPHA_UTIL = 0.4 # For utilization (slightly more reactive)
EMA_ALPHA_TARGET = 0.3 # For target_count smoothing (prevents thrashing)
EMA_REDIS_TTL = 7200 # 2 hours - survive missed runs but not stale forever
async def get_smoothed_metrics(chute_ids: List[str]) -> Dict[str, Dict[str, float]]:
"""
Fetch previously smoothed metrics from Redis for all chutes.
Returns dict of chute_id -> {"urgency": float, "util": float}
"""
if not chute_ids:
return {}
pipe = settings.redis_client.pipeline()
for chute_id in chute_ids:
pipe.hgetall(f"smooth:{chute_id}")
results = await pipe.execute()
smoothed = {}
for chute_id, data in zip(chute_ids, results):
if data:
smoothed[chute_id] = {
"urgency": float(data.get(b"urgency", 0)),
"util": float(data.get(b"util", 0)),
"target": float(data.get(b"target", 0)),
}
return smoothed
async def save_smoothed_metrics(metrics: Dict[str, Dict[str, float]]):
"""
Save smoothed metrics to Redis.
metrics: dict of chute_id -> {"urgency": float, "util": float}
"""
if not metrics:
return
pipe = settings.redis_client.pipeline()
for chute_id, values in metrics.items():
mapping = {
"urgency": str(values["urgency"]),
"util": str(values["util"]),
}
if "target" in values:
mapping["target"] = str(values["target"])
pipe.hset(f"smooth:{chute_id}", mapping=mapping)
pipe.expire(f"smooth:{chute_id}", EMA_REDIS_TTL)
await pipe.execute()
def calculate_ema(current: float, previous: float | None, alpha: float) -> float:
"""
Calculate exponential moving average.
If no previous value, return current (first data point).
"""
if previous is None:
return current
return alpha * current + (1 - alpha) * previous
async def mark_chute_as_starving(chute_id: str):
"""
Mark a chute as recently starving in Redis.
This prevents the chute from being used as a forced donation donor
for STARVING_COOLDOWN_MINUTES, avoiding A->B->A donation thrashing.
"""
key = f"{STARVING_HISTORY_KEY_PREFIX}{chute_id}"
await settings.redis_client.set(key, "1", ex=STARVING_COOLDOWN_MINUTES * 60)
async def get_recently_starving_chutes(chute_ids: List[str]) -> Set[str]:
"""
Check which chutes have been starving recently (within STARVING_COOLDOWN_MINUTES).
Returns set of chute_ids that were recently starving.
"""
if not chute_ids:
return set()
pipe = settings.redis_client.pipeline()
for chute_id in chute_ids:
pipe.exists(f"{STARVING_HISTORY_KEY_PREFIX}{chute_id}")
results = await pipe.execute()
return {chute_id for chute_id, exists in zip(chute_ids, results) if exists}
# Compute multiplier adjustment timing constants
# No adjustment for the first N hours after activation (miners keep their original boost)
COMPUTE_MULTIPLIER_HOLD_HOURS = 2.0
# Total hours until fully adjusted to target (includes hold period)
COMPUTE_MULTIPLIER_FULL_ADJUST_HOURS = 8.0
# Ramp duration (after hold period)
COMPUTE_MULTIPLIER_RAMP_HOURS = COMPUTE_MULTIPLIER_FULL_ADJUST_HOURS - COMPUTE_MULTIPLIER_HOLD_HOURS
def _calculate_blended_multiplier(
current: float | None,
target: float,
hours_since_activation: float,
) -> float | None:
"""
Calculate the blended compute_multiplier based on time since activation.
Returns None if no update needed (still in hold period with existing value).
"""
if hours_since_activation <= COMPUTE_MULTIPLIER_HOLD_HOURS:
# In hold period - only set if NULL
return target if current is None else None
if hours_since_activation >= COMPUTE_MULTIPLIER_FULL_ADJUST_HOURS:
# Past full adjustment - clamp to target
return target
# In ramp period - ease-in blend (t² curve)
current_val = current if current is not None else target
t = (hours_since_activation - COMPUTE_MULTIPLIER_HOLD_HOURS) / COMPUTE_MULTIPLIER_RAMP_HOURS
blend = t * t
return current_val * (1 - blend) + target * blend
async def simulate_miner_scores(
chute_effective_multipliers: Dict[str, float],
) -> Dict[str, Dict]:
"""
Simulate what miner scores would be if the updated effective compute multipliers
were applied to instances. This uses the exact same scoring logic as
metasync/shared.py:get_scoring_data but with projected multiplier changes.
Args:
chute_effective_multipliers: Dict mapping chute_id -> new effective_compute_multiplier
(calculated from updated boosts in dry-run)
Returns:
Dict with:
- current_scores: current normalized scores per miner
- simulated_scores: projected scores if multipliers were updated
- current_raw: current raw compute_units per miner
- simulated_raw: projected raw compute_units per miner
- instance_changes: list of instance-level multiplier changes
- miner_changes: summary of score changes per miner
"""
from metasync.constants import SCORING_INTERVAL
logger.info("Simulating miner scores with updated compute multipliers...")
# Use the same interval as metasync scoring
interval = SCORING_INTERVAL
async with get_session() as session:
await session.execute(text("SET LOCAL statement_timeout = '30s'"))
metagraph_result = await session.execute(
text(f"""
SELECT hotkey, blacklist_reason
FROM metagraph_nodes
WHERE netuid = {settings.netuid} AND node_id >= 0
""")
)
active_hotkeys = set()
blacklisted_hotkeys = set()
for hotkey, blacklist_reason in metagraph_result:
active_hotkeys.add(hotkey)
if blacklist_reason:
blacklisted_hotkeys.add(hotkey)
current_query = text(f"""
WITH billed_instances AS (
SELECT
ia.miner_hotkey,
ia.instance_id,
ia.chute_id,
ia.created_at,
ia.activated_at,
ia.deleted_at,
ia.stop_billing_at,
ia.compute_multiplier,
ia.bounty,
GREATEST(ia.created_at, now() - interval '{interval}') as billing_start,
LEAST(
COALESCE(ia.stop_billing_at, now()),
COALESCE(ia.deleted_at, now()),
now()
) as billing_end
FROM instance_audit ia
WHERE ia.activated_at IS NOT NULL
AND (
(
ia.billed_to IS NULL
AND ia.deleted_at IS NOT NULL
AND ia.deleted_at - ia.activated_at >= INTERVAL '1 hour'
)
OR ia.valid_termination IS TRUE
OR ia.deletion_reason in (
'job has been terminated due to insufficient user balance',
'user-defined/private chute instance has not been used since shutdown_after_seconds',
'user has zero/negative balance (private chute)'
)
OR ia.deletion_reason LIKE '%has an old version%'
OR ia.deleted_at IS NULL
)
AND (ia.deleted_at IS NULL OR ia.deleted_at >= now() - interval '{interval}')
),
instance_weighted_compute AS (
SELECT
bi.instance_id,
bi.miner_hotkey,
bi.chute_id,
bi.billing_start,
bi.billing_end,
bi.bounty,
bi.compute_multiplier as fallback_multiplier,
COALESCE(
SUM(
EXTRACT(EPOCH FROM (
LEAST(COALESCE(ich.ended_at, now()), bi.billing_end)
- GREATEST(ich.started_at, bi.billing_start)
)) * ich.compute_multiplier
),
EXTRACT(EPOCH FROM (bi.billing_end - bi.billing_start)) * COALESCE(bi.compute_multiplier, 1.0)
) AS weighted_compute_units,
EXTRACT(EPOCH FROM (bi.billing_end - bi.billing_start)) AS billing_seconds
FROM billed_instances bi
LEFT JOIN instance_compute_history ich
ON ich.instance_id = bi.instance_id
AND ich.started_at < bi.billing_end
AND (ich.ended_at IS NULL OR ich.ended_at > bi.billing_start)
WHERE bi.billing_end > bi.billing_start
GROUP BY bi.instance_id, bi.miner_hotkey, bi.chute_id, bi.billing_start, bi.billing_end, bi.bounty, bi.compute_multiplier
)
SELECT
instance_id,
miner_hotkey,
chute_id,
billing_seconds,
weighted_compute_units,
fallback_multiplier
FROM instance_weighted_compute
""")
instance_result = await session.execute(current_query)
instances_data = []
for row in instance_result:
instances_data.append(
{
"instance_id": row.instance_id,
"miner_hotkey": row.miner_hotkey,
"chute_id": row.chute_id,
"billing_seconds": float(row.billing_seconds or 0),
"current_compute_units": float(row.weighted_compute_units or 0),
"current_multiplier": float(row.fallback_multiplier or 1.0),
}
)
active_instances_result = await session.execute(
text("""
SELECT
i.instance_id,
i.chute_id,
i.miner_hotkey,
i.compute_multiplier,
i.activated_at,
i.created_at
FROM instances i
WHERE i.active = true
AND i.verified = true
AND i.activated_at IS NOT NULL
""")
)
active_instances = {}
for row in active_instances_result:
active_instances[row.instance_id] = {
"chute_id": row.chute_id,
"miner_hotkey": row.miner_hotkey,
"current_multiplier": float(row.compute_multiplier or 1.0),
"activated_at": row.activated_at,
}
now = datetime.now()
instance_changes = []
for instance_id, inst_data in active_instances.items():
chute_id = inst_data["chute_id"]
target = chute_effective_multipliers.get(chute_id)
if target is None:
continue
hours_since = (
now - inst_data["activated_at"].replace(tzinfo=None)
).total_seconds() / 3600.0
current = inst_data["current_multiplier"]
new_value = _calculate_blended_multiplier(current, target, hours_since)
if new_value is not None and abs(current - new_value) >= 0.001:
instance_changes.append(
{
"instance_id": instance_id,
"chute_id": chute_id,
"miner_hotkey": inst_data["miner_hotkey"],
"current_multiplier": current,
"new_multiplier": new_value,
"target_multiplier": target,
"hours_since_activation": hours_since,
}
)
new_multipliers = {ic["instance_id"]: ic["new_multiplier"] for ic in instance_changes}
current_raw = defaultdict(float)
simulated_raw = defaultdict(float)
for inst in instances_data:
hotkey = inst["miner_hotkey"]
if not hotkey or hotkey not in active_hotkeys or hotkey in blacklisted_hotkeys:
continue
instance_id = inst["instance_id"]
billing_seconds = inst["billing_seconds"]
current_units = inst["current_compute_units"]
current_raw[hotkey] += current_units
# Simulated score: if this instance would get a new multiplier, recalculate
if instance_id in new_multipliers:
old_mult = inst["current_multiplier"]
new_mult = new_multipliers[instance_id]
if old_mult > 0:
simulated_units = current_units * (new_mult / old_mult)
else:
simulated_units = billing_seconds * new_mult
simulated_raw[hotkey] += simulated_units
else:
simulated_raw[hotkey] += current_units
current_sum = sum(max(0.0, v) for v in current_raw.values())
simulated_sum = sum(max(0.0, v) for v in simulated_raw.values())
if current_sum > 0:
current_scores = {hk: max(0.0, v) / current_sum for hk, v in current_raw.items()}
else:
n = max(len(current_raw), 1)
current_scores = {hk: 1.0 / n for hk in current_raw.keys()}
if simulated_sum > 0:
simulated_scores = {hk: max(0.0, v) / simulated_sum for hk, v in simulated_raw.items()}
else:
n = max(len(simulated_raw), 1)
simulated_scores = {hk: 1.0 / n for hk in simulated_raw.keys()}
miner_changes = []
all_hotkeys = set(current_scores.keys()) | set(simulated_scores.keys())
for hk in all_hotkeys:
curr = current_scores.get(hk, 0.0)
sim = simulated_scores.get(hk, 0.0)
curr_raw = current_raw.get(hk, 0.0)
sim_raw = simulated_raw.get(hk, 0.0)
if abs(curr - sim) > 0.000001:
miner_changes.append(
{
"hotkey": hk,
"current_score": curr,
"simulated_score": sim,
"score_change": sim - curr,
"score_change_pct": ((sim - curr) / curr * 100) if curr > 0 else 0,
"current_raw": curr_raw,
"simulated_raw": sim_raw,
}
)
miner_changes.sort(key=lambda x: abs(x["score_change"]), reverse=True)
return {
"current_scores": current_scores,
"simulated_scores": simulated_scores,
"current_raw": dict(current_raw),
"simulated_raw": dict(simulated_raw),
"instance_changes": instance_changes,
"miner_changes": miner_changes,
}
@retry_on_db_failure()
async def refresh_instance_compute_multipliers(chute_ids: List[str] = None):
"""
Refresh compute_multiplier for active instances based on current chute state.
Both public and private instances use the same gradual blending curve:
- 0-2 hours after activation: No change (instance keeps original multiplier)
- 2-8 hours: Ease-in blend toward target (slow at first, accelerates)
Uses t² curve where t is normalized time in the ramp window
- 8+ hours: Clamp to target value
The difference is in target calculation:
- Public: includes demand/revenue-based boost from the autoscaler.
- Private (billed_to IS NOT NULL): target is the base multiplier
(actual GPU * count * private bonus * manual boost * TEE) with no
demand/revenue-based adjustments. This corrects any stale boosts
on existing instances while preserving bounty decay through the
blending curve.
Public instances in the thrash penalty period are skipped entirely.
Pre-loads all instance values first, calculates new values in Python,
then issues static UPDATE statements to avoid read-modify-write locks.
"""
from api.chute.util import calculate_effective_compute_multiplier
from api.constants import THRASH_WINDOW_HOURS, THRASH_PENALTY_HOURS
from api.gpu import COMPUTE_MULTIPLIER
logger.info("Refreshing compute multipliers for active instances...")
async with get_session() as session:
await session.execute(text("SET LOCAL statement_timeout = '10s'"))
# Load chutes (optionally filtered)
query = select(Chute)
if chute_ids:
query = query.where(Chute.chute_id.in_(chute_ids))
result = await session.execute(query)
chutes = {c.chute_id: c for c in result.scalars().all()}
if not chutes:
logger.info("No chutes to process")
return
# Pre-load all active instances (both public and private)
instance_query = select(Instance).where(
Instance.chute_id.in_(chutes.keys()),
Instance.active.is_(True),
Instance.verified.is_(True),
Instance.activated_at.isnot(None),
)
instance_result = await session.execute(instance_query)
instances = instance_result.scalars().all()
if not instances:
logger.info("No active instances to update")
return
# Identify instances in thrash penalty period (only applies to public instances)
thrash_penalty_result = await session.execute(
text(
"""
SELECT i.instance_id
FROM instances i
WHERE i.chute_id = ANY(:chute_ids)
AND i.active = true
AND i.verified = true
AND i.activated_at IS NOT NULL
AND i.billed_to IS NULL
AND i.activated_at + INTERVAL ':penalty_hours hours' > NOW()
AND EXISTS (
SELECT 1
FROM instance_audit ia
WHERE ia.miner_hotkey = i.miner_hotkey
AND ia.chute_id = i.chute_id
AND ia.activated_at IS NOT NULL
AND ia.deleted_at IS NOT NULL
AND ia.deleted_at > i.created_at - INTERVAL ':window_hours hours'
AND ia.deleted_at <= i.created_at
AND ia.valid_termination IS NOT TRUE
)
""".replace(":penalty_hours", str(THRASH_PENALTY_HOURS)).replace(
":window_hours", str(THRASH_WINDOW_HOURS)
)
),
{"chute_ids": list(chutes.keys())},
)
thrash_penalty_instances = {row.instance_id for row in thrash_penalty_result}
if thrash_penalty_instances:
logger.info(
f"Skipping {len(thrash_penalty_instances)} instances in thrash penalty period"
)
# Calculate target multipliers for each chute (without bounty).
# For private chutes, calculate_effective_compute_multiplier already
# excludes demand-based boost thanks to the chute.public guard.
chute_targets = {}
for chute_id, chute in chutes.items():
effective_data = await calculate_effective_compute_multiplier(
chute, include_bounty=False
)
chute_targets[chute_id] = effective_data["effective_compute_multiplier"]
# For private instances, look up actual GPU identifiers so we can
# adjust from the node selector minimum to the real hardware.
private_instance_ids = [i.instance_id for i in instances if i.billed_to is not None]
instance_gpus = {}
if private_instance_ids:
gpu_result = await session.execute(
text("""
SELECT ino.instance_id, n.gpu_identifier
FROM instance_nodes ino
JOIN nodes n ON n.uuid = ino.node_id
WHERE ino.instance_id = ANY(:instance_ids)
"""),
{"instance_ids": private_instance_ids},
)
for row in gpu_result:
if row.instance_id not in instance_gpus:
instance_gpus[row.instance_id] = row.gpu_identifier
# Calculate new values in Python
now = datetime.now()
updates = [] # List of (instance_id, new_multiplier)
for inst in instances:
target = chute_targets.get(inst.chute_id)
if target is None:
continue
current = inst.compute_multiplier
if inst.billed_to is not None:
# Private instance: target is base multiplier (no demand boost)
# adjusted for actual GPU hardware. Bounty still decays via
# the same blending curve as public instances.
chute = chutes.get(inst.chute_id)
if not chute:
continue
actual_gpu = instance_gpus.get(inst.instance_id)
if actual_gpu and actual_gpu in COMPUTE_MULTIPLIER:
gpu_count = chute.node_selector.get("gpu_count", 1)
ns = NodeSelector(**chute.node_selector)
ns_min = ns.compute_multiplier
actual_base = gpu_count * COMPUTE_MULTIPLIER[actual_gpu]
if ns_min > 0 and actual_base != ns_min:
target *= actual_base / ns_min
else:
# Public instance: skip if in thrash penalty period
if inst.instance_id in thrash_penalty_instances:
continue
# Both public and private use the same blending curve.
# This preserves bounty during the 0-2h hold period, decays it
# over 2-8h, and clamps to the (bounty-free) target after 8h.
hours_since = (now - inst.activated_at.replace(tzinfo=None)).total_seconds() / 3600.0
new_value = _calculate_blended_multiplier(current, target, hours_since)
if new_value is None:
continue
if current is not None and abs(current - new_value) < 0.001:
continue
updates.append((inst.instance_id, new_value))
# Batch update with static values (no read-modify-write)
if updates:
private_count = sum(1 for iid, _ in updates if iid in set(private_instance_ids))
public_count = len(updates) - private_count
for instance_id, new_multiplier in updates:
await session.execute(
text("""
UPDATE instances
SET compute_multiplier = :multiplier
WHERE instance_id = :instance_id
"""),
{"instance_id": instance_id, "multiplier": new_multiplier},
)
await session.commit()
logger.success(
f"Updated compute_multiplier for {len(updates)} instances "
f"({public_count} public, {private_count} private corrections)"
)
else:
logger.info("No compute_multiplier updates needed")
async def rotate_private_instance_compute_history():
"""
For long-running private instances, periodically close the current open
compute history record and create a new one with the same multiplier.
This caps the penalty for improper deletion to ~6 hours of lost incentives
instead of the entire instance lifetime.
"""
async with get_session() as session:
await session.execute(text("SET LOCAL statement_timeout = '10s'"))
result = await session.execute(
text("""
SELECT ich.instance_id
FROM instance_compute_history ich
JOIN instances i ON i.instance_id = ich.instance_id