Spaces:
Sleeping
Sleeping
Factor Studios
commited on
Update test_ai_integration_http.py
Browse files- test_ai_integration_http.py +13 -24
test_ai_integration_http.py
CHANGED
|
@@ -22,7 +22,7 @@ logging.basicConfig(
|
|
| 22 |
format='%(asctime)s - %(levelname)s - %(message)s'
|
| 23 |
)
|
| 24 |
|
| 25 |
-
# HTTP connection manager with
|
| 26 |
@contextlib.contextmanager
|
| 27 |
def http_storage_manager(max_retries=5, retry_delay=2, timeout=30.0):
|
| 28 |
storage = None
|
|
@@ -32,35 +32,24 @@ def http_storage_manager(max_retries=5, retry_delay=2, timeout=30.0):
|
|
| 32 |
nonlocal storage
|
| 33 |
if storage:
|
| 34 |
try:
|
| 35 |
-
if
|
| 36 |
return True
|
| 37 |
storage.close()
|
| 38 |
except:
|
| 39 |
pass
|
| 40 |
-
storage = HTTPGPUStorage() # Create storage instance
|
| 41 |
-
|
| 42 |
-
# Set connection parameters after creation
|
| 43 |
-
storage.set_timeout(timeout)
|
| 44 |
-
storage.set_max_retries(max_retries)
|
| 45 |
-
storage.set_retry_delay(retry_delay)
|
| 46 |
-
|
| 47 |
-
# Enable persistent connection if available
|
| 48 |
-
if hasattr(storage, 'enable_keep_alive'):
|
| 49 |
-
storage.enable_keep_alive()
|
| 50 |
-
return storage.connect()
|
| 51 |
-
except Exception as e:
|
| 52 |
-
logging.error(f"Connection configuration error: {e}")
|
| 53 |
-
return False
|
| 54 |
|
| 55 |
# Initial connection with improved error handling
|
| 56 |
for attempt in range(max_retries):
|
| 57 |
try:
|
| 58 |
if try_connect():
|
| 59 |
logging.info("Successfully connected to GPU storage server via HTTP")
|
| 60 |
-
#
|
| 61 |
-
|
|
|
|
| 62 |
break
|
| 63 |
-
logging.warning("Connection established but
|
| 64 |
else:
|
| 65 |
logging.warning(f"HTTP connection attempt {attempt + 1} failed, retrying in {retry_delay}s...")
|
| 66 |
time.sleep(retry_delay * (1.5 ** attempt)) # Exponential backoff
|
|
@@ -285,14 +274,14 @@ def test_ai_integration_http():
|
|
| 285 |
for attempt in range(max_connection_attempts):
|
| 286 |
try:
|
| 287 |
if (components['storage'] and
|
| 288 |
-
components['storage'].
|
| 289 |
shared_storage = components['storage']
|
| 290 |
logging.info("Successfully reused existing HTTP connection")
|
| 291 |
break
|
| 292 |
else:
|
| 293 |
logging.warning("Existing connection unavailable, creating new HTTP connection...")
|
| 294 |
-
with http_storage_manager(
|
| 295 |
-
if new_storage and new_storage.
|
| 296 |
components['storage'] = new_storage
|
| 297 |
shared_storage = new_storage
|
| 298 |
logging.info("Successfully established new HTTP connection")
|
|
@@ -335,9 +324,9 @@ def test_ai_integration_http():
|
|
| 335 |
max_retry = 3
|
| 336 |
for retry in range(max_retry):
|
| 337 |
try:
|
| 338 |
-
if not shared_storage.
|
| 339 |
logging.warning(f"Connection check failed for chip {i}, attempt {retry + 1}")
|
| 340 |
-
shared_storage.
|
| 341 |
time.sleep(1)
|
| 342 |
continue
|
| 343 |
|
|
|
|
| 22 |
format='%(asctime)s - %(levelname)s - %(message)s'
|
| 23 |
)
|
| 24 |
|
| 25 |
+
# HTTP connection manager with retry handling
|
| 26 |
@contextlib.contextmanager
|
| 27 |
def http_storage_manager(max_retries=5, retry_delay=2, timeout=30.0):
|
| 28 |
storage = None
|
|
|
|
| 32 |
nonlocal storage
|
| 33 |
if storage:
|
| 34 |
try:
|
| 35 |
+
if storage.is_connected():
|
| 36 |
return True
|
| 37 |
storage.close()
|
| 38 |
except:
|
| 39 |
pass
|
| 40 |
+
storage = HTTPGPUStorage() # Create storage instance with default configuration
|
| 41 |
+
return storage._create_session()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 42 |
|
| 43 |
# Initial connection with improved error handling
|
| 44 |
for attempt in range(max_retries):
|
| 45 |
try:
|
| 46 |
if try_connect():
|
| 47 |
logging.info("Successfully connected to GPU storage server via HTTP")
|
| 48 |
+
# Test connection by making a simple request
|
| 49 |
+
status = storage.get_connection_status()
|
| 50 |
+
if status["connected"]:
|
| 51 |
break
|
| 52 |
+
logging.warning("Connection established but not responsive")
|
| 53 |
else:
|
| 54 |
logging.warning(f"HTTP connection attempt {attempt + 1} failed, retrying in {retry_delay}s...")
|
| 55 |
time.sleep(retry_delay * (1.5 ** attempt)) # Exponential backoff
|
|
|
|
| 274 |
for attempt in range(max_connection_attempts):
|
| 275 |
try:
|
| 276 |
if (components['storage'] and
|
| 277 |
+
components['storage'].is_connected()):
|
| 278 |
shared_storage = components['storage']
|
| 279 |
logging.info("Successfully reused existing HTTP connection")
|
| 280 |
break
|
| 281 |
else:
|
| 282 |
logging.warning("Existing connection unavailable, creating new HTTP connection...")
|
| 283 |
+
with http_storage_manager() as new_storage:
|
| 284 |
+
if new_storage and new_storage.is_connected():
|
| 285 |
components['storage'] = new_storage
|
| 286 |
shared_storage = new_storage
|
| 287 |
logging.info("Successfully established new HTTP connection")
|
|
|
|
| 324 |
max_retry = 3
|
| 325 |
for retry in range(max_retry):
|
| 326 |
try:
|
| 327 |
+
if not shared_storage.is_connected():
|
| 328 |
logging.warning(f"Connection check failed for chip {i}, attempt {retry + 1}")
|
| 329 |
+
shared_storage._create_session() # Attempt to reconnect
|
| 330 |
time.sleep(1)
|
| 331 |
continue
|
| 332 |
|