File size: 18,778 Bytes
b190b45 |
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 |
#!/usr/bin/env python3
"""
Multi-Source Fallback Engine
Implements cascading fallback system with 10+ sources per data type
NEVER FAILS - Always returns data or cached data
"""
import httpx
import asyncio
import logging
import json
import time
from typing import Dict, Any, List, Optional, Callable, Tuple
from datetime import datetime, timedelta
from pathlib import Path
from enum import Enum
logger = logging.getLogger(__name__)
class DataType(Enum):
"""Supported data types"""
MARKET_PRICES = "market_prices"
OHLC_CANDLESTICK = "ohlc_candlestick"
BLOCKCHAIN_EXPLORER = "blockchain_explorer"
NEWS_FEEDS = "news_feeds"
SENTIMENT_DATA = "sentiment_data"
ONCHAIN_ANALYTICS = "onchain_analytics"
WHALE_TRACKING = "whale_tracking"
class SourceStatus(Enum):
"""Source availability status"""
AVAILABLE = "available"
RATE_LIMITED = "rate_limited"
TEMPORARILY_DOWN = "temporarily_down"
PERMANENTLY_FAILED = "permanently_failed"
class MultiSourceCache:
"""Simple in-memory cache with TTL"""
def __init__(self):
self._cache: Dict[str, Tuple[Any, float, float]] = {} # key: (data, timestamp, ttl)
def get(self, key: str) -> Optional[Any]:
"""Get cached data if not expired"""
if key in self._cache:
data, timestamp, ttl = self._cache[key]
if time.time() - timestamp < ttl:
logger.info(f"β
Cache HIT: {key}")
return data
else:
# Expired
del self._cache[key]
logger.debug(f"β° Cache EXPIRED: {key}")
return None
def set(self, key: str, data: Any, ttl: int):
"""Set cache with TTL in seconds"""
self._cache[key] = (data, time.time(), ttl)
logger.debug(f"πΎ Cache SET: {key} (TTL: {ttl}s)")
def get_stale(self, key: str, max_age: int) -> Optional[Any]:
"""Get cached data even if expired, within max_age"""
if key in self._cache:
data, timestamp, _ = self._cache[key]
age = time.time() - timestamp
if age < max_age:
logger.warning(f"β οΈ Cache STALE: {key} (age: {age:.0f}s)")
return data
return None
def clear(self):
"""Clear all cache"""
self._cache.clear()
class SourceMonitor:
"""Monitor source performance and availability"""
def __init__(self):
self._source_stats: Dict[str, Dict[str, Any]] = {}
self._source_status: Dict[str, SourceStatus] = {}
self._unavailable_until: Dict[str, float] = {} # timestamp when source becomes available again
def record_success(self, source_name: str, response_time: float):
"""Record successful request"""
if source_name not in self._source_stats:
self._source_stats[source_name] = {
"success_count": 0,
"failure_count": 0,
"total_response_time": 0,
"last_success": None,
"last_failure": None
}
stats = self._source_stats[source_name]
stats["success_count"] += 1
stats["total_response_time"] += response_time
stats["last_success"] = time.time()
# Mark as available
self._source_status[source_name] = SourceStatus.AVAILABLE
if source_name in self._unavailable_until:
del self._unavailable_until[source_name]
logger.debug(f"β
{source_name}: Success ({response_time:.2f}s)")
def record_failure(self, source_name: str, error_type: str, status_code: Optional[int] = None):
"""Record failed request"""
if source_name not in self._source_stats:
self._source_stats[source_name] = {
"success_count": 0,
"failure_count": 0,
"total_response_time": 0,
"last_success": None,
"last_failure": None
}
stats = self._source_stats[source_name]
stats["failure_count"] += 1
stats["last_failure"] = time.time()
stats["last_error"] = error_type
stats["last_status_code"] = status_code
# Handle different error types
if status_code == 429:
# Rate limited - mark unavailable for 60 minutes
self._source_status[source_name] = SourceStatus.RATE_LIMITED
self._unavailable_until[source_name] = time.time() + 3600
logger.warning(f"β οΈ {source_name}: RATE LIMITED (unavailable for 60 min)")
elif status_code in [500, 502, 503, 504]:
# Server error - mark unavailable for 5 minutes
self._source_status[source_name] = SourceStatus.TEMPORARILY_DOWN
self._unavailable_until[source_name] = time.time() + 300
logger.warning(f"β οΈ {source_name}: TEMPORARILY DOWN (unavailable for 5 min)")
elif status_code in [401, 403]:
# Auth error - mark unavailable for 24 hours
self._source_status[source_name] = SourceStatus.TEMPORARILY_DOWN
self._unavailable_until[source_name] = time.time() + 86400
logger.error(f"β {source_name}: AUTH FAILED (unavailable for 24 hours)")
else:
logger.warning(f"β οΈ {source_name}: Failed ({error_type})")
def is_available(self, source_name: str) -> bool:
"""Check if source is available"""
if source_name in self._unavailable_until:
if time.time() < self._unavailable_until[source_name]:
return False
else:
# Became available again
del self._unavailable_until[source_name]
self._source_status[source_name] = SourceStatus.AVAILABLE
return True
def get_stats(self, source_name: str) -> Dict[str, Any]:
"""Get source statistics"""
if source_name not in self._source_stats:
return {}
stats = self._source_stats[source_name]
total_requests = stats["success_count"] + stats["failure_count"]
return {
"total_requests": total_requests,
"success_count": stats["success_count"],
"failure_count": stats["failure_count"],
"success_rate": stats["success_count"] / total_requests if total_requests > 0 else 0,
"avg_response_time": stats["total_response_time"] / stats["success_count"] if stats["success_count"] > 0 else 0,
"last_success": stats.get("last_success"),
"last_failure": stats.get("last_failure"),
"status": self._source_status.get(source_name, SourceStatus.AVAILABLE).value
}
def get_all_stats(self) -> Dict[str, Dict[str, Any]]:
"""Get all source statistics"""
return {name: self.get_stats(name) for name in self._source_stats.keys()}
class MultiSourceFallbackEngine:
"""
Core engine for multi-source data fetching with automatic failover
"""
def __init__(self, config_path: Optional[str] = None):
"""Initialize the fallback engine"""
# Load configuration
if config_path is None:
config_path = Path(__file__).parent / "multi_source_config.json"
with open(config_path, 'r') as f:
self.config = json.load(f)
# Initialize components
self.cache = MultiSourceCache()
self.monitor = SourceMonitor()
logger.info("β
Multi-Source Fallback Engine initialized")
def _get_sources_for_data_type(self, data_type: DataType, **kwargs) -> List[Dict[str, Any]]:
"""Get all sources for a data type in priority order"""
sources = []
if data_type == DataType.MARKET_PRICES:
config = self.config["api_sources"]["market_prices"]
sources.extend(config.get("primary", []))
sources.extend(config.get("secondary", []))
sources.extend(config.get("tertiary", []))
elif data_type == DataType.OHLC_CANDLESTICK:
config = self.config["api_sources"]["ohlc_candlestick"]
sources.extend(config.get("primary", []))
sources.extend(config.get("secondary", []))
# HuggingFace datasets as fallback
sources.extend(config.get("huggingface_datasets", []))
elif data_type == DataType.BLOCKCHAIN_EXPLORER:
chain = kwargs.get("chain", "ethereum")
config = self.config["api_sources"]["blockchain_explorer"]
sources.extend(config.get(chain, []))
elif data_type == DataType.NEWS_FEEDS:
config = self.config["api_sources"]["news_feeds"]
sources.extend(config.get("api_sources", []))
sources.extend(config.get("rss_feeds", []))
elif data_type == DataType.SENTIMENT_DATA:
config = self.config["api_sources"]["sentiment_data"]
sources.extend(config.get("primary", []))
sources.extend(config.get("social_analytics", []))
elif data_type == DataType.ONCHAIN_ANALYTICS:
sources.extend(self.config["api_sources"]["onchain_analytics"])
elif data_type == DataType.WHALE_TRACKING:
sources.extend(self.config["api_sources"]["whale_tracking"])
# Sort by priority
sources.sort(key=lambda x: x.get("priority", 999))
# Filter out unavailable sources
available_sources = [s for s in sources if self.monitor.is_available(s["name"])]
logger.info(f"π {data_type.value}: {len(available_sources)}/{len(sources)} sources available")
return available_sources
async def _fetch_from_source(
self,
source: Dict[str, Any],
fetch_func: Callable,
**kwargs
) -> Optional[Dict[str, Any]]:
"""Fetch data from a single source"""
source_name = source["name"]
try:
start_time = time.time()
# Call the fetch function
result = await fetch_func(source, **kwargs)
response_time = time.time() - start_time
# Validate result
if result and self._validate_result(result):
self.monitor.record_success(source_name, response_time)
return result
else:
logger.warning(f"β οΈ {source_name}: Invalid result")
self.monitor.record_failure(source_name, "invalid_result")
return None
except httpx.HTTPStatusError as e:
status_code = e.response.status_code
logger.warning(f"β οΈ {source_name}: HTTP {status_code}")
self.monitor.record_failure(source_name, f"http_{status_code}", status_code)
return None
except httpx.TimeoutException as e:
logger.warning(f"β οΈ {source_name}: Timeout")
self.monitor.record_failure(source_name, "timeout")
return None
except Exception as e:
logger.error(f"β {source_name}: {type(e).__name__}: {str(e)}")
self.monitor.record_failure(source_name, type(e).__name__)
return None
def _validate_result(self, result: Any) -> bool:
"""Validate result data"""
if not result:
return False
# Basic validation - can be extended
if isinstance(result, dict):
return True
elif isinstance(result, list):
return len(result) > 0
return False
async def fetch_with_fallback(
self,
data_type: DataType,
fetch_func: Callable,
cache_key: str,
**kwargs
) -> Dict[str, Any]:
"""
Fetch data with automatic fallback through multiple sources
Args:
data_type: Type of data to fetch
fetch_func: Async function to fetch from a source
cache_key: Unique cache key
**kwargs: Additional parameters for fetch function
Returns:
Data from successful source or cache
"""
# Check cache first
cached = self.cache.get(cache_key)
if cached:
return {
"success": True,
"data": cached,
"source": "cache",
"cached": True,
"timestamp": datetime.utcnow().isoformat()
}
# Get all sources for this data type
sources = self._get_sources_for_data_type(data_type, **kwargs)
if not sources:
logger.error(f"β No sources available for {data_type.value}")
# Try stale cache as emergency fallback
return self._emergency_fallback(cache_key, data_type)
# Try each source in order
attempts = 0
for source in sources:
attempts += 1
source_name = source["name"]
logger.info(f"π Attempt {attempts}/{len(sources)}: Trying {source_name}")
result = await self._fetch_from_source(source, fetch_func, **kwargs)
if result:
# Success! Cache and return
cache_ttl = self.config["caching"].get(data_type.value, {}).get("ttl_seconds", 60)
self.cache.set(cache_key, result, cache_ttl)
logger.info(f"β
SUCCESS: {source_name} (attempt {attempts}/{len(sources)})")
return {
"success": True,
"data": result,
"source": source_name,
"cached": False,
"attempts": attempts,
"total_sources": len(sources),
"timestamp": datetime.utcnow().isoformat()
}
# All sources failed - try emergency fallback
logger.error(f"β All {len(sources)} sources failed for {data_type.value}")
return self._emergency_fallback(cache_key, data_type)
def _emergency_fallback(self, cache_key: str, data_type: DataType) -> Dict[str, Any]:
"""Emergency fallback when all sources fail"""
# Try stale cache
max_age = self.config["caching"].get(data_type.value, {}).get("max_age_seconds", 3600)
stale_data = self.cache.get_stale(cache_key, max_age)
if stale_data:
logger.warning(f"β οΈ EMERGENCY FALLBACK: Using stale cache for {cache_key}")
return {
"success": True,
"data": stale_data,
"source": "stale_cache",
"cached": True,
"stale": True,
"warning": "Data may be outdated",
"timestamp": datetime.utcnow().isoformat()
}
# No cache available
logger.error(f"β COMPLETE FAILURE: No data available for {cache_key}")
return {
"success": False,
"error": "All sources failed and no cached data available",
"data_type": data_type.value,
"timestamp": datetime.utcnow().isoformat()
}
async def fetch_parallel(
self,
data_type: DataType,
fetch_func: Callable,
cache_key: str,
max_parallel: int = 3,
**kwargs
) -> Dict[str, Any]:
"""
Fetch from multiple sources in parallel and return first successful result
Args:
data_type: Type of data to fetch
fetch_func: Async function to fetch from a source
cache_key: Unique cache key
max_parallel: Maximum number of parallel requests
**kwargs: Additional parameters for fetch function
Returns:
Data from first successful source
"""
# Check cache first
cached = self.cache.get(cache_key)
if cached:
return {
"success": True,
"data": cached,
"source": "cache",
"cached": True,
"timestamp": datetime.utcnow().isoformat()
}
# Get sources
sources = self._get_sources_for_data_type(data_type, **kwargs)[:max_parallel]
if not sources:
return self._emergency_fallback(cache_key, data_type)
logger.info(f"π Parallel fetch from {len(sources)} sources")
# Create tasks for parallel execution
tasks = [
self._fetch_from_source(source, fetch_func, **kwargs)
for source in sources
]
# Wait for first successful result
for completed in asyncio.as_completed(tasks):
try:
result = await completed
if result:
# Cache and return first success
cache_ttl = self.config["caching"].get(data_type.value, {}).get("ttl_seconds", 60)
self.cache.set(cache_key, result, cache_ttl)
logger.info(f"β
PARALLEL SUCCESS: Got first result")
return {
"success": True,
"data": result,
"source": "parallel_fetch",
"cached": False,
"timestamp": datetime.utcnow().isoformat()
}
except:
continue
# All parallel requests failed
logger.error(f"β All parallel requests failed")
return self._emergency_fallback(cache_key, data_type)
def get_monitoring_stats(self) -> Dict[str, Any]:
"""Get monitoring statistics for all sources"""
return {
"sources": self.monitor.get_all_stats(),
"timestamp": datetime.utcnow().isoformat()
}
def clear_cache(self):
"""Clear all cached data"""
self.cache.clear()
logger.info("ποΈ Cache cleared")
# Global instance
_engine_instance: Optional[MultiSourceFallbackEngine] = None
def get_fallback_engine() -> MultiSourceFallbackEngine:
"""Get or create global fallback engine instance"""
global _engine_instance
if _engine_instance is None:
_engine_instance = MultiSourceFallbackEngine()
return _engine_instance
__all__ = [
"MultiSourceFallbackEngine",
"DataType",
"SourceStatus",
"get_fallback_engine"
]
|