-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathstate_archival.py
More file actions
411 lines (334 loc) · 13.3 KB
/
state_archival.py
File metadata and controls
411 lines (334 loc) · 13.3 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
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
"""State archival service for S3 cold storage.
This service handles archiving Python session states from Redis to S3
for long-term storage, and restoring them on demand.
Hybrid storage architecture:
- Hot storage: Redis with 2-hour TTL (fast access)
- Cold storage: S3 with configurable TTL (long-term archival)
When a state is accessed:
1. Check Redis first (hot storage)
2. If not found, check S3 (cold storage)
3. If found in S3, restore to Redis
States are archived to S3 when:
- TTL in Redis drops below archive_after_seconds threshold
- This indicates the session has been inactive for a while
"""
import asyncio
import io
from datetime import datetime, timezone
from typing import Optional, Dict, Any
import structlog
from botocore.exceptions import ClientError
from ..config import settings
from .state import StateService
logger = structlog.get_logger(__name__)
class StateArchivalService:
"""Manages archiving and restoring Python session states to/from S3.
States are stored in S3 under the path:
states/{session_id}/state.dat
Metadata is stored as S3 object metadata:
- archived_at: ISO timestamp
- original_size: Size before any host-side compression
- session_id: The session identifier
"""
STATE_PREFIX = "states"
def __init__(
self,
state_service: Optional[StateService] = None,
s3_client: Optional[Any] = None,
):
"""Initialize the archival service.
Args:
state_service: StateService instance for Redis operations
s3_client: Optional boto3 S3 client (creates new one if not provided)
"""
self.state_service = state_service or StateService()
self.s3_client = s3_client or settings.s3.make_client()
self.bucket_name = settings.s3_bucket
self._bucket_checked = False
def _get_state_object_key(self, session_id: str) -> str:
"""Generate S3 object key for a session state."""
return f"{self.STATE_PREFIX}/{session_id}/state.dat"
async def _ensure_bucket_exists(self) -> None:
"""Ensure the S3 bucket exists."""
if self._bucket_checked:
return
try:
loop = asyncio.get_event_loop()
try:
await loop.run_in_executor(
None,
lambda: self.s3_client.head_bucket(Bucket=self.bucket_name),
)
except ClientError as e:
if e.response["Error"]["Code"] in ("404", "NoSuchBucket"):
await loop.run_in_executor(
None,
lambda: self.s3_client.create_bucket(Bucket=self.bucket_name),
)
logger.info(
"Created S3 bucket for state archival", bucket=self.bucket_name
)
else:
raise
self._bucket_checked = True
except ClientError as e:
logger.error(
"Failed to ensure bucket exists", error=str(e), bucket=self.bucket_name
)
raise
async def archive_state(self, session_id: str, state_data: str) -> bool:
"""Archive a session state to S3.
Args:
session_id: Session identifier
state_data: Base64-encoded state data (already lz4 compressed)
Returns:
True if archived successfully
"""
try:
await self._ensure_bucket_exists()
object_key = self._get_state_object_key(session_id)
state_bytes = state_data.encode("utf-8")
metadata = {
"archived_at": datetime.now(timezone.utc).isoformat(),
"original_size": str(len(state_bytes)),
"session_id": session_id,
}
loop = asyncio.get_event_loop()
data_stream = io.BytesIO(state_bytes)
await loop.run_in_executor(
None,
lambda: self.s3_client.put_object(
Bucket=self.bucket_name,
Key=object_key,
Body=data_stream,
ContentLength=len(state_bytes),
ContentType="application/octet-stream",
Metadata=metadata,
),
)
logger.info(
"Archived state to S3",
session_id=session_id[:12],
size_bytes=len(state_bytes),
object_key=object_key,
)
return True
except Exception as e:
logger.error(
"Failed to archive state", session_id=session_id[:12], error=str(e)
)
return False
async def restore_state(self, session_id: str) -> Optional[str]:
"""Restore a session state from S3.
If found, the state is also saved back to Redis for fast access.
Args:
session_id: Session identifier
Returns:
Base64-encoded state data, or None if not found
"""
try:
await self._ensure_bucket_exists()
object_key = self._get_state_object_key(session_id)
loop = asyncio.get_event_loop()
try:
response = await loop.run_in_executor(
None,
lambda: self.s3_client.get_object(
Bucket=self.bucket_name, Key=object_key
),
)
state_bytes = response["Body"].read()
except ClientError as e:
if e.response["Error"]["Code"] == "NoSuchKey":
logger.debug("No archived state found", session_id=session_id[:12])
return None
raise
state_data = state_bytes.decode("utf-8")
# Only restore to Redis if under the size threshold
import base64 as _b64
raw_size = len(_b64.b64decode(state_data))
max_redis_bytes = settings.state_max_redis_size_mb * 1024 * 1024
if raw_size <= max_redis_bytes:
await self.state_service.save_state(
session_id, state_data, ttl_seconds=settings.state_ttl_seconds
)
else:
await self.state_service.save_state_pointer(
session_id, state_data, ttl_seconds=settings.state_ttl_seconds
)
logger.info(
"State too large for Redis, kept in S3 only",
session_id=session_id[:12],
state_size_mb=round(raw_size / 1024 / 1024, 1),
)
logger.info(
"Restored state from S3",
session_id=session_id[:12],
size_bytes=len(state_bytes),
)
return state_data
except Exception as e:
logger.error(
"Failed to restore state", session_id=session_id[:12], error=str(e)
)
return None
async def delete_archived_state(self, session_id: str) -> bool:
"""Delete an archived state from S3.
Args:
session_id: Session identifier
Returns:
True if deleted (or didn't exist)
"""
try:
await self._ensure_bucket_exists()
object_key = self._get_state_object_key(session_id)
loop = asyncio.get_event_loop()
# boto3 delete_object is idempotent — no error on missing key
await loop.run_in_executor(
None,
lambda: self.s3_client.delete_object(
Bucket=self.bucket_name, Key=object_key
),
)
logger.debug("Deleted archived state", session_id=session_id[:12])
return True
except Exception as e:
logger.error(
"Failed to delete archived state",
session_id=session_id[:12],
error=str(e),
)
return False
async def has_archived_state(self, session_id: str) -> bool:
"""Check if a session has archived state in S3.
Args:
session_id: Session identifier
Returns:
True if archived state exists
"""
try:
await self._ensure_bucket_exists()
object_key = self._get_state_object_key(session_id)
loop = asyncio.get_event_loop()
try:
await loop.run_in_executor(
None,
lambda: self.s3_client.head_object(
Bucket=self.bucket_name, Key=object_key
),
)
return True
except ClientError as e:
if e.response["Error"]["Code"] == "404":
return False
raise
except Exception as e:
logger.error(
"Failed to check archived state",
session_id=session_id[:12],
error=str(e),
)
return False
async def archive_inactive_states(self) -> Dict[str, Any]:
"""Archive inactive states from Redis to S3.
This is the main archival task that runs periodically.
It finds states with low TTL (indicating inactivity) and archives them.
Returns:
Summary of archival operation
"""
if not settings.state_archive_enabled:
return {"archived": 0, "skipped": "archival disabled"}
summary = {
"archived": 0,
"failed": 0,
"already_archived": 0,
}
try:
states_to_archive = await self.state_service.get_states_for_archival()
for session_id, remaining_ttl, size in states_to_archive:
try:
if await self.has_archived_state(session_id):
summary["already_archived"] += 1
continue
state_data = await self.state_service.get_state(session_id)
if not state_data:
continue
if await self.archive_state(session_id, state_data):
summary["archived"] += 1
else:
summary["failed"] += 1
except Exception as e:
logger.warning(
"Failed to archive individual state",
session_id=session_id[:12],
error=str(e),
)
summary["failed"] += 1
if summary["archived"] > 0:
logger.info(
"Completed state archival batch",
archived=summary["archived"],
failed=summary["failed"],
already_archived=summary["already_archived"],
)
return summary
except Exception as e:
logger.error("State archival batch failed", error=str(e))
summary["error"] = str(e)
return summary
async def cleanup_expired_archives(self) -> Dict[str, Any]:
"""Clean up archived states that have exceeded their TTL.
Returns:
Summary of cleanup operation
"""
if not settings.state_archive_enabled:
return {"deleted": 0, "skipped": "archival disabled"}
summary = {
"deleted": 0,
"failed": 0,
}
try:
await self._ensure_bucket_exists()
loop = asyncio.get_event_loop()
prefix = f"{self.STATE_PREFIX}/"
ttl_days = settings.state_archive_ttl_days
cutoff = datetime.now(timezone.utc).timestamp() - (ttl_days * 24 * 3600)
objects = await loop.run_in_executor(
None,
lambda: list(
self.s3_client.get_paginator("list_objects_v2")
.paginate(Bucket=self.bucket_name, Prefix=prefix)
.search("Contents[]")
),
)
for entry in objects:
if entry is None:
continue
try:
last_modified = entry.get("LastModified")
if last_modified and last_modified.timestamp() < cutoff:
parts = entry["Key"].split("/")
if len(parts) >= 2:
session_id = parts[1]
if await self.delete_archived_state(session_id):
summary["deleted"] += 1
else:
summary["failed"] += 1
except Exception as e:
logger.warning(
"Failed to cleanup archived state",
object_name=entry.get("Key"),
error=str(e),
)
summary["failed"] += 1
if summary["deleted"] > 0:
logger.info(
"Cleaned up expired archived states",
deleted=summary["deleted"],
failed=summary["failed"],
)
return summary
except Exception as e:
logger.error("Archive cleanup failed", error=str(e))
summary["error"] = str(e)
return summary