-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstorage_throttle.py
More file actions
381 lines (287 loc) · 11 KB
/
storage_throttle.py
File metadata and controls
381 lines (287 loc) · 11 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
# -*- coding: utf-8 -*-
# storage_throttle.py
"""
Storage Speed Throttle Manager
Automatic I/O throttling based on storage speed tiers.
Philosophy: "Just tag your storage speed and forget about it!"
Speed Tiers:
- SLOW: 10 concurrent writes (HDD, network drives)
- MODERATE: 25 concurrent writes (SATA SSD)
- FAST: 50 concurrent writes (NVMe like MP700)
- INSANE: 70 concurrent writes (Optane, RAM disk)
Usage:
@task_token_guard(operation_type='save_json', tags={'storage_speed': 'FAST'})
def save_json(data):
with open('file.json', 'w') as f:
json.dump(data, f)
# Automatic 50-concurrent throttle!
"""
import threading
import time
from typing import Dict, Callable, Any, Optional
from dataclasses import dataclass
# ============================================================================
# SPEED TIER CONFIGURATION
# ============================================================================
STORAGE_SPEEDS = {
'SLOW': 10, # HDD, network drives, slow storage
'MODERATE': 25, # SATA SSD, decent performance
'FAST': 50, # NVMe like MP700, high performance
'INSANE': 70 # Optane, RAM disk, extreme performance
}
@dataclass
class ThrottleStats:
"""Statistics for a single throttle tier."""
speed_tier: str
max_concurrent: int
current_active: int
total_operations: int
total_wait_time: float
def avg_wait_time(self) -> float:
"""Calculate average wait time."""
if self.total_operations == 0:
return 0.0
return self.total_wait_time / self.total_operations
class StorageThrottle:
"""
Single storage throttle for a specific speed tier.
Manages I/O concurrency for one storage speed level.
"""
def __init__(self, speed_tier: str, max_concurrent: int):
"""
Initialize storage throttle.
Args:
speed_tier: Speed tier name (SLOW/MODERATE/FAST/INSANE)
max_concurrent: Maximum concurrent I/O operations
"""
self.speed_tier = speed_tier
self.max_concurrent = max_concurrent
# Semaphore to limit concurrent I/O
self._semaphore = threading.Semaphore(max_concurrent)
# Lock to prevent thread contention
self._lock = threading.Lock()
# Metrics
self.current_active = 0
self.total_operations = 0
self.total_wait_time = 0.0
def throttle(self, func: Callable, *args, **kwargs) -> Any:
"""
Execute a function with I/O throttling.
Args:
func: Function to execute (should perform I/O)
*args: Function arguments
**kwargs: Function keyword arguments
Returns:
Function result
"""
# Record attempt
with self._lock:
self.total_operations += 1
# Wait for I/O slot
wait_start = time.time()
acquired = self._semaphore.acquire(blocking=True)
wait_duration = time.time() - wait_start
if not acquired:
raise Exception(f"Failed to acquire I/O slot for {self.speed_tier}")
try:
# Track wait time and active count
with self._lock:
self.total_wait_time += wait_duration
self.current_active += 1
# Execute the I/O operation
result = func(*args, **kwargs)
return result
finally:
# Release I/O slot
with self._lock:
self.current_active -= 1
self._semaphore.release()
def get_stats(self) -> ThrottleStats:
"""Get current throttle statistics."""
with self._lock:
return ThrottleStats(
speed_tier=self.speed_tier,
max_concurrent=self.max_concurrent,
current_active=self.current_active,
total_operations=self.total_operations,
total_wait_time=self.total_wait_time
)
class StorageThrottleManager:
"""
Manages multiple storage throttles for different speed tiers.
Automatically creates and manages throttles for each speed tier.
"""
def __init__(self, custom_speeds: Optional[Dict[str, int]] = None):
"""
Initialize storage throttle manager.
Args:
custom_speeds: Custom speed tier configuration (uses defaults if None)
"""
self.speed_config = custom_speeds or STORAGE_SPEEDS.copy()
# Create throttle for each speed tier
self.throttles: Dict[str, StorageThrottle] = {}
for tier, limit in self.speed_config.items():
self.throttles[tier] = StorageThrottle(tier, limit)
print("[STORAGE_THROTTLE] Initialized with speed tiers:")
for tier, limit in self.speed_config.items():
print(f" {tier}: {limit} concurrent I/O")
def get_throttle(self, speed_tier: str) -> StorageThrottle:
"""
Get throttle for a specific speed tier.
Args:
speed_tier: Speed tier name (SLOW/MODERATE/FAST/INSANE)
Returns:
StorageThrottle for that tier
"""
# Normalize tier name
tier = speed_tier.upper()
if tier not in self.throttles:
# Unknown tier - default to MODERATE
print(f"[STORAGE_THROTTLE] Warning: Unknown tier '{speed_tier}', using MODERATE")
tier = 'moderate'
return self.throttles[tier]
def throttle(self, speed_tier: str, func: Callable, *args, **kwargs) -> Any:
"""
Execute function with the appropriate throttling for speed tier.
Args:
speed_tier: Speed tier (SLOW/MODERATE/FAST/INSANE)
func: Function to execute
*args: Function arguments
**kwargs: Function keyword arguments
Returns:
Function result
"""
throttle = self.get_throttle(speed_tier)
return throttle.throttle(func, *args, **kwargs)
def get_stats(self) -> Dict[str, ThrottleStats]:
"""Get statistics for all throttles."""
return {
tier: throttle.get_stats()
for tier, throttle in self.throttles.items()
}
def print_stats(self):
"""Print statistics."""
print()
print("=" * 70)
print("STORAGE THROTTLE STATISTICS")
print("=" * 70)
print()
stats = self.get_stats()
for tier in ['SLOW', 'MODERATE', 'FAST', 'INSANE']:
if tier not in stats:
continue
stat = stats[tier]
print(f"{tier}:")
print(f" Max concurrent: {stat.max_concurrent}")
print(f" Current active: {stat.current_active}")
print(f" Total operations: {stat.total_operations}")
if stat.total_operations > 0:
print(f" Total wait time: {stat.total_wait_time:.2f}s")
print(f" Avg wait time: {stat.avg_wait_time():.3f}s")
print()
print("=" * 70)
def update_speed_limit(self, speed_tier: str, new_limit: int):
"""
Update the concurrent limit for a speed tier (hot reconfiguration).
Args:
speed_tier: Speed tier to update
new_limit: New concurrent I/O limit
"""
tier = speed_tier.upper()
if tier in self.throttles:
old_limit = self.speed_config[tier]
self.speed_config[tier] = new_limit
# Create new throttle with new limit
self.throttles[tier] = StorageThrottle(tier, new_limit)
print(f"[STORAGE_THROTTLE] Updated {tier}: {old_limit} -> {new_limit}")
else:
print(f"[STORAGE_THROTTLE] Unknown tier: {tier}")
# ============================================================================
# Global Singleton
# ============================================================================
_global_storage_throttle: Optional[StorageThrottleManager] = None
_storage_lock = threading.Lock()
def get_storage_throttle() -> StorageThrottleManager:
"""
Get the global storage throttle manager.
Creates one if it doesn't exist (a singleton pattern).
"""
global _global_storage_throttle
if _global_storage_throttle is None:
with _storage_lock:
if _global_storage_throttle is None:
_global_storage_throttle = StorageThrottleManager()
return _global_storage_throttle
def configure_storage_throttle(custom_speeds: Optional[Dict[str, int]] = None):
"""
Configure global storage throttle (call at startup).
Args:
custom_speeds: Custom speed tier configuration
Example: {'FAST': 60, 'SLOW': 5}
"""
global _global_storage_throttle
with _storage_lock:
_global_storage_throttle = StorageThrottleManager(custom_speeds)
return _global_storage_throttle
# ============================================================================
# TESTING
# ============================================================================
if __name__ == '__main__':
import json
from pathlib import Path
import concurrent.futures
print("=" * 70)
print("STORAGE THROTTLE MANAGER TEST")
print("=" * 70)
print()
# Initialize throttle
throttle = get_storage_throttle()
# Create a test directory
test_dir = Path('/tmp/storage_test')
test_dir.mkdir(exist_ok=True)
print("Writing files with different speed tiers...")
print()
# Test function
def write_test_file(index: int, speed_tier: str):
"""Test file write with specific speed tier."""
data = {'index': index, 'tier': speed_tier, 'data': list(range(100))}
filename = test_dir / f'{speed_tier.lower()}_{index:03d}.json'
# Throttled write
def _write():
with open(filename, 'w') as f:
json.dump(data, f, indent=2)
return filename
result = throttle.throttle(speed_tier, _write)
return result
# Execute writes across different tiers
print("Testing all speed tiers concurrently:")
print(" SLOW: 20 files (10 concurrent max)")
print(" MODERATE: 30 files (25 concurrent max)")
print(" FAST: 40 files (50 concurrent max)")
print()
start = time.time()
with concurrent.futures.ThreadPoolExecutor(max_workers=100) as executor:
futures = []
# SLOW tier
for i in range(20):
futures.append(executor.submit(write_test_file, i, 'SLOW'))
# MODERATE tier
for i in range(30):
futures.append(executor.submit(write_test_file, i, 'MODERATE'))
# FAST tier
for i in range(40):
futures.append(executor.submit(write_test_file, i, 'FAST'))
# Wait for completion
results = [f.result() for f in concurrent.futures.as_completed(futures)]
duration = time.time() - start
print()
print("=" * 70)
print("Results:")
print("=" * 70)
print(f"Total files written: {len(results)}")
print(f"Total time: {duration:.2f}s")
print()
# Show stats
throttle.print_stats()
print()
print(f"✅ Test complete! Files in: {test_dir}")