Spaces:
Sleeping
Sleeping
File size: 18,108 Bytes
40ee6b4 |
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 |
"""
Async S3 storage client for multi-agent MCTS framework.
Provides:
- aioboto3 async client
- Retry strategy with tenacity
- Exponential backoff for failures
- Content-hash based idempotent keys
- Store: configs, MCTS stats, traces, logs
- Compression support
"""
import asyncio
import gzip
import hashlib
import json
import os
from dataclasses import dataclass, field
from datetime import datetime
from typing import Any
import aioboto3
from botocore.config import Config as BotoConfig
from botocore.exceptions import ClientError
from tenacity import (
retry,
retry_if_exception_type,
stop_after_attempt,
wait_exponential,
)
from src.observability.logging import get_logger
@dataclass
class S3Config:
"""Configuration for S3 storage client."""
bucket_name: str = field(default_factory=lambda: os.environ.get("S3_BUCKET_NAME", "mcts-framework-storage"))
region_name: str = field(default_factory=lambda: os.environ.get("AWS_REGION", "us-east-1"))
endpoint_url: str | None = field(default_factory=lambda: os.environ.get("S3_ENDPOINT_URL"))
# Retry configuration
max_retries: int = 5
initial_wait_seconds: float = 1.0
max_wait_seconds: float = 60.0
exponential_base: float = 2.0
# Storage options
enable_compression: bool = True
compression_threshold_bytes: int = 1024 # Compress if larger than 1KB
use_content_hash_keys: bool = True
# Prefixes for different data types
prefix_configs: str = "configs/"
prefix_mcts_stats: str = "mcts-stats/"
prefix_traces: str = "traces/"
prefix_logs: str = "logs/"
prefix_checkpoints: str = "checkpoints/"
class S3StorageClient:
"""
Async S3 storage client with retry logic and compression.
Features:
- Automatic retries with exponential backoff
- Content-hash based idempotent keys for deduplication
- Gzip compression for large payloads
- Organized storage by data type
"""
def __init__(self, config: S3Config | None = None):
"""
Initialize S3 storage client.
Args:
config: S3 configuration (uses environment variables if not provided)
"""
self.config = config or S3Config()
self.logger = get_logger("storage.s3")
self._session: aioboto3.Session | None = None
self._initialized = False
# boto3 config with retries and timeouts
self._boto_config = BotoConfig(
retries={"max_attempts": 3, "mode": "adaptive"},
connect_timeout=10,
read_timeout=30,
max_pool_connections=25,
)
async def initialize(self) -> None:
"""Initialize the aioboto3 session."""
if self._initialized:
return
self._session = aioboto3.Session()
self._initialized = True
self.logger.info(f"S3 client initialized for bucket: {self.config.bucket_name}")
async def close(self) -> None:
"""Close the client (cleanup if needed)."""
self._initialized = False
self.logger.info("S3 client closed")
def _get_client_params(self) -> dict[str, Any]:
"""Get parameters for S3 client context manager."""
params = {
"region_name": self.config.region_name,
"config": self._boto_config,
}
if self.config.endpoint_url:
params["endpoint_url"] = self.config.endpoint_url
return params
def _compute_content_hash(self, data: bytes) -> str:
"""Compute SHA256 hash of content for idempotent keys."""
return hashlib.sha256(data).hexdigest()
def _compress_data(self, data: bytes) -> bytes:
"""Compress data using gzip."""
return gzip.compress(data)
def _decompress_data(self, data: bytes) -> bytes:
"""Decompress gzip data."""
return gzip.decompress(data)
def _should_compress(self, data: bytes) -> bool:
"""Determine if data should be compressed."""
return self.config.enable_compression and len(data) >= self.config.compression_threshold_bytes
def _generate_key(
self,
prefix: str,
name: str,
data: bytes | None = None,
timestamp: datetime | None = None,
) -> str:
"""
Generate S3 key for object.
Args:
prefix: Storage prefix (e.g., configs/, logs/)
name: Object name
data: Optional data for content-hash key generation
timestamp: Optional timestamp for key
Returns:
Full S3 key
"""
if timestamp is None:
timestamp = datetime.utcnow()
date_prefix = timestamp.strftime("%Y/%m/%d")
if self.config.use_content_hash_keys and data:
content_hash = self._compute_content_hash(data)[:12]
return f"{prefix}{date_prefix}/{name}_{content_hash}"
else:
timestamp_str = timestamp.strftime("%H%M%S_%f")
return f"{prefix}{date_prefix}/{name}_{timestamp_str}"
@retry(
retry=retry_if_exception_type((ClientError, asyncio.TimeoutError)),
stop=stop_after_attempt(5),
wait=wait_exponential(multiplier=1, min=1, max=60),
reraise=True,
)
async def _put_object_with_retry(
self,
key: str,
body: bytes,
content_type: str = "application/octet-stream",
metadata: dict[str, str] | None = None,
) -> dict[str, Any]:
"""
Put object to S3 with retry logic.
Uses tenacity for exponential backoff retry strategy.
"""
if not self._session:
await self.initialize()
async with self._session.client("s3", **self._get_client_params()) as s3:
extra_args = {
"ContentType": content_type,
}
if metadata:
extra_args["Metadata"] = metadata
response = await s3.put_object(
Bucket=self.config.bucket_name,
Key=key,
Body=body,
**extra_args,
)
self.logger.debug(
"Uploaded object to S3",
extra={
"s3_key": key,
"size_bytes": len(body),
"etag": response.get("ETag"),
},
)
return {
"key": key,
"etag": response.get("ETag"),
"size_bytes": len(body),
"version_id": response.get("VersionId"),
}
@retry(
retry=retry_if_exception_type((ClientError, asyncio.TimeoutError)),
stop=stop_after_attempt(5),
wait=wait_exponential(multiplier=1, min=1, max=60),
reraise=True,
)
async def _get_object_with_retry(self, key: str) -> bytes:
"""
Get object from S3 with retry logic.
"""
if not self._session:
await self.initialize()
async with self._session.client("s3", **self._get_client_params()) as s3:
response = await s3.get_object(
Bucket=self.config.bucket_name,
Key=key,
)
async with response["Body"] as stream:
data = await stream.read()
self.logger.debug(
"Downloaded object from S3",
extra={
"s3_key": key,
"size_bytes": len(data),
},
)
return data
async def store_config(
self,
config_name: str,
config_data: dict[str, Any],
session_id: str | None = None,
) -> dict[str, Any]:
"""
Store configuration data to S3.
Args:
config_name: Name of the configuration
config_data: Configuration dictionary
session_id: Optional session identifier
Returns:
Upload result with key and metadata
"""
json_data = json.dumps(config_data, indent=2, default=str).encode("utf-8")
key = self._generate_key(
prefix=self.config.prefix_configs,
name=config_name,
data=json_data if self.config.use_content_hash_keys else None,
)
if self._should_compress(json_data):
body = self._compress_data(json_data)
key += ".gz"
content_type = "application/gzip"
else:
body = json_data
content_type = "application/json"
metadata = {
"config_name": config_name,
"original_size": str(len(json_data)),
"compressed": str(len(body) != len(json_data)),
}
if session_id:
metadata["session_id"] = session_id
result = await self._put_object_with_retry(key, body, content_type, metadata)
self.logger.info(f"Stored config '{config_name}' to S3: {key}")
return result
async def store_mcts_stats(
self,
session_id: str,
stats: dict[str, Any],
iteration: int | None = None,
) -> dict[str, Any]:
"""
Store MCTS statistics to S3.
Args:
session_id: MCTS session identifier
stats: Statistics dictionary
iteration: Optional iteration number
Returns:
Upload result
"""
name = f"{session_id}_stats"
if iteration is not None:
name += f"_iter{iteration}"
json_data = json.dumps(stats, indent=2, default=str).encode("utf-8")
key = self._generate_key(
prefix=self.config.prefix_mcts_stats,
name=name,
data=json_data if self.config.use_content_hash_keys else None,
)
if self._should_compress(json_data):
body = self._compress_data(json_data)
key += ".gz"
content_type = "application/gzip"
else:
body = json_data
content_type = "application/json"
metadata = {
"session_id": session_id,
"data_type": "mcts_stats",
"original_size": str(len(json_data)),
}
if iteration is not None:
metadata["iteration"] = str(iteration)
result = await self._put_object_with_retry(key, body, content_type, metadata)
self.logger.info(f"Stored MCTS stats for session '{session_id}' to S3: {key}")
return result
async def store_traces(
self,
session_id: str,
trace_data: dict[str, Any] | list[dict[str, Any]],
) -> dict[str, Any]:
"""
Store trace data to S3.
Args:
session_id: Session identifier
trace_data: Trace spans/events
Returns:
Upload result
"""
json_data = json.dumps(trace_data, indent=2, default=str).encode("utf-8")
key = self._generate_key(
prefix=self.config.prefix_traces,
name=f"{session_id}_traces",
data=json_data if self.config.use_content_hash_keys else None,
)
if self._should_compress(json_data):
body = self._compress_data(json_data)
key += ".gz"
content_type = "application/gzip"
else:
body = json_data
content_type = "application/json"
metadata = {
"session_id": session_id,
"data_type": "traces",
"original_size": str(len(json_data)),
}
result = await self._put_object_with_retry(key, body, content_type, metadata)
self.logger.info(f"Stored traces for session '{session_id}' to S3: {key}")
return result
async def store_logs(
self,
session_id: str,
log_entries: list[dict[str, Any]],
) -> dict[str, Any]:
"""
Store log entries to S3.
Args:
session_id: Session identifier
log_entries: List of JSON log entries
Returns:
Upload result
"""
# Store as newline-delimited JSON (NDJSON)
ndjson_data = "\n".join(json.dumps(entry, default=str) for entry in log_entries).encode("utf-8")
key = self._generate_key(
prefix=self.config.prefix_logs,
name=f"{session_id}_logs",
data=ndjson_data if self.config.use_content_hash_keys else None,
)
if self._should_compress(ndjson_data):
body = self._compress_data(ndjson_data)
key += ".gz"
content_type = "application/gzip"
else:
body = ndjson_data
key += ".ndjson"
content_type = "application/x-ndjson"
metadata = {
"session_id": session_id,
"data_type": "logs",
"entry_count": str(len(log_entries)),
"original_size": str(len(ndjson_data)),
}
result = await self._put_object_with_retry(key, body, content_type, metadata)
self.logger.info(f"Stored {len(log_entries)} log entries for session '{session_id}' to S3: {key}")
return result
async def store_checkpoint(
self,
session_id: str,
checkpoint_data: dict[str, Any],
checkpoint_name: str = "checkpoint",
) -> dict[str, Any]:
"""
Store framework checkpoint/state to S3.
Args:
session_id: Session identifier
checkpoint_data: Checkpoint state
checkpoint_name: Name for the checkpoint
Returns:
Upload result
"""
json_data = json.dumps(checkpoint_data, indent=2, default=str).encode("utf-8")
key = self._generate_key(
prefix=self.config.prefix_checkpoints,
name=f"{session_id}_{checkpoint_name}",
data=json_data if self.config.use_content_hash_keys else None,
)
if self._should_compress(json_data):
body = self._compress_data(json_data)
key += ".gz"
content_type = "application/gzip"
else:
body = json_data
content_type = "application/json"
metadata = {
"session_id": session_id,
"data_type": "checkpoint",
"checkpoint_name": checkpoint_name,
"original_size": str(len(json_data)),
}
result = await self._put_object_with_retry(key, body, content_type, metadata)
self.logger.info(f"Stored checkpoint '{checkpoint_name}' for session '{session_id}' to S3: {key}")
return result
async def retrieve_object(self, key: str) -> bytes:
"""
Retrieve and decompress object from S3.
Args:
key: S3 object key
Returns:
Decompressed data bytes
"""
data = await self._get_object_with_retry(key)
# Auto-decompress if gzip
if key.endswith(".gz"):
data = self._decompress_data(data)
return data
async def retrieve_json(self, key: str) -> Any:
"""
Retrieve JSON object from S3.
Args:
key: S3 object key
Returns:
Parsed JSON data
"""
data = await self.retrieve_object(key)
return json.loads(data.decode("utf-8"))
async def list_objects(
self,
prefix: str,
max_keys: int = 1000,
) -> list[dict[str, Any]]:
"""
List objects with given prefix.
Args:
prefix: S3 key prefix
max_keys: Maximum objects to return
Returns:
List of object metadata
"""
if not self._session:
await self.initialize()
async with self._session.client("s3", **self._get_client_params()) as s3:
response = await s3.list_objects_v2(
Bucket=self.config.bucket_name,
Prefix=prefix,
MaxKeys=max_keys,
)
objects = []
for obj in response.get("Contents", []):
objects.append(
{
"key": obj["Key"],
"size": obj["Size"],
"last_modified": obj["LastModified"],
"etag": obj["ETag"],
}
)
return objects
async def delete_object(self, key: str) -> dict[str, Any]:
"""
Delete object from S3.
Args:
key: S3 object key
Returns:
Deletion result
"""
if not self._session:
await self.initialize()
async with self._session.client("s3", **self._get_client_params()) as s3:
response = await s3.delete_object(
Bucket=self.config.bucket_name,
Key=key,
)
self.logger.info(f"Deleted object from S3: {key}")
return {
"key": key,
"deleted": True,
"version_id": response.get("VersionId"),
}
async def health_check(self) -> dict[str, Any]:
"""
Check S3 connectivity and bucket access.
Returns:
Health check result
"""
if not self._session:
await self.initialize()
try:
async with self._session.client("s3", **self._get_client_params()) as s3:
# Try to head the bucket
await s3.head_bucket(Bucket=self.config.bucket_name)
return {
"status": "healthy",
"bucket": self.config.bucket_name,
"region": self.config.region_name,
"timestamp": datetime.utcnow().isoformat(),
}
except ClientError as e:
error_code = e.response.get("Error", {}).get("Code", "Unknown")
return {
"status": "unhealthy",
"bucket": self.config.bucket_name,
"error_code": error_code,
"error_message": str(e),
"timestamp": datetime.utcnow().isoformat(),
}
|