File size: 8,182 Bytes
fd357f4 |
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 |
#!/usr/bin/env python3
"""
DTO Cache Client - Interface to Dragonfly for live status caching
"""
import json
import redis
from typing import Dict, Any, Optional, List
from datetime import datetime, timezone
class DTOCacheClient:
def __init__(self, host: str = "localhost", port: int = 18000,
password: Optional[str] = None):
self.host = host
self.port = port
self.password = password
self.redis_client = None
def connect(self) -> bool:
"""Connect to Dragonfly server"""
try:
self.redis_client = redis.Redis(
host=self.host,
port=self.port,
password=self.password,
decode_responses=True,
socket_timeout=5,
socket_connect_timeout=5
)
# Test connection
self.redis_client.ping()
print(f"β
Connected to Dragonfly at {self.host}:{self.port}")
return True
except redis.ConnectionError as e:
print(f"β Failed to connect to Dragonfly: {e}")
return False
except Exception as e:
print(f"β Error connecting to Dragonfly: {e}")
return False
def update_run_status(self, run_id: str, status: str,
metadata: Optional[Dict[str, Any]] = None) -> bool:
"""Update run status in cache"""
if not self.redis_client:
if not self.connect():
return False
try:
key = f"dto:run:{run_id}:status"
timestamp = int(datetime.now(timezone.utc).timestamp())
status_data = {
"status": status,
"updated_at": timestamp,
"run_id": run_id
}
if metadata:
status_data.update(metadata)
# Store as hash with TTL
self.redis_client.hset(key, mapping=status_data)
self.redis_client.expire(key, 3600) # 1 hour TTL
print(f"β
Updated run status: {run_id} -> {status}")
return True
except Exception as e:
print(f"β Error updating run status: {e}")
return False
def get_run_status(self, run_id: str) -> Optional[Dict[str, Any]]:
"""Get run status from cache"""
if not self.redis_client:
if not self.connect():
return None
try:
key = f"dto:run:{run_id}:status"
status_data = self.redis_client.hgetall(key)
if status_data:
# Convert numeric values
if 'updated_at' in status_data:
status_data['updated_at'] = int(status_data['updated_at'])
if 'progress_percent' in status_data:
status_data['progress_percent'] = float(status_data['progress_percent'])
return status_data
else:
return None
except Exception as e:
print(f"β Error getting run status: {e}")
return None
def update_run_progress(self, run_id: str, progress_percent: float,
transferred_bytes: int, total_bytes: int,
throughput_mbps: float) -> bool:
"""Update run progress metrics"""
if not self.redis_client:
if not self.connect():
return False
try:
key = f"dto:run:{run_id}:progress"
timestamp = int(datetime.now(timezone.utc).timestamp())
progress_data = {
"progress_percent": progress_percent,
"transferred_bytes": transferred_bytes,
"total_bytes": total_bytes,
"throughput_mbps": throughput_mbps,
"updated_at": timestamp
}
# Store as hash with TTL
self.redis_client.hset(key, mapping=progress_data)
self.redis_client.expire(key, 3600) # 1 hour TTL
print(f"β
Updated run progress: {run_id} -> {progress_percent}%")
return True
except Exception as e:
print(f"β Error updating run progress: {e}")
return False
def add_artifact(self, run_id: str, artifact_type: str, artifact_path: str) -> bool:
"""Add artifact reference to cache"""
if not self.redis_client:
if not self.connect():
return False
try:
key = f"dto:run:{run_id}:artifacts:{artifact_type}"
self.redis_client.sadd(key, artifact_path)
self.redis_client.expire(key, 86400) # 24 hours TTL
print(f"β
Added artifact: {run_id} -> {artifact_type}: {artifact_path}")
return True
except Exception as e:
print(f"β Error adding artifact: {e}")
return False
def get_artifacts(self, run_id: str, artifact_type: str) -> List[str]:
"""Get artifacts for a run"""
if not self.redis_client:
if not self.connect():
return []
try:
key = f"dto:run:{run_id}:artifacts:{artifact_type}"
artifacts = self.redis_client.smembers(key)
return list(artifacts) if artifacts else []
except Exception as e:
print(f"β Error getting artifacts: {e}")
return []
def publish_alert(self, job_id: str, alert_type: str, message: str,
severity: str = "warning") -> bool:
"""Publish alert to cache"""
if not self.redis_client:
if not self.connect():
return False
try:
key = f"dto:alerts:{job_id}"
alert_data = {
"type": alert_type,
"message": message,
"severity": severity,
"timestamp": int(datetime.utcnow().timestamp())
}
# Store as hash with TTL
self.redis_client.hset(key, mapping=alert_data)
self.redis_client.expire(key, 172800) # 48 hours TTL
# Also publish to pub/sub for real-time notifications
self.redis_client.publish("dto:alerts", json.dumps(alert_data))
print(f"β
Published alert: {job_id} -> {alert_type}: {message}")
return True
except Exception as e:
print(f"β Error publishing alert: {e}")
return False
# Test function
def test_cache_connectivity():
"""Test Dragonfly cache connectivity"""
client = DTOCacheClient()
if client.connect():
# Test run status update
test_run_id = "test-run-001"
# Update status
client.update_run_status(test_run_id, "IN_PROGRESS", {
"manifest_path": "/test/manifest.yaml",
"data_class": "CLASS_A"
})
# Update progress
client.update_run_progress(test_run_id, 25.5, 26843545600, 107374182400, 604.0)
# Add artifact
client.add_artifact(test_run_id, "logs", "/data/adaptai/platform/dataops/dto/logs/test-run-001.log")
# Get status back
status = client.get_run_status(test_run_id)
print(f"Retrieved status: {status}")
# Get artifacts
artifacts = client.get_artifacts(test_run_id, "logs")
print(f"Retrieved artifacts: {artifacts}")
# Publish alert
client.publish_alert("test-job-001", "SLO_BREACH", "Throughput below expected 500 Mbps", "critical")
print("β
All cache operations completed successfully")
return True
else:
print("β Cache connectivity test failed")
return False
if __name__ == "__main__":
print("Testing DTO Cache Client...")
print("=" * 50)
test_cache_connectivity() |