forked from mtusi2026/mtusi2026lab5
69 lines
2.0 KiB
Python
69 lines
2.0 KiB
Python
"""Read-only operator probe after a full restart: LIST and ranged GET of existing objects.
|
|
|
|
An open S3 socket / HEAD bucket does not prove that SeaweedFS volume locations
|
|
have been registered. Read one byte per nonempty object, including staging.
|
|
This is a bounded recovery hook, not an expensive per-request health check.
|
|
"""
|
|
import asyncio
|
|
import sys
|
|
|
|
from . import config
|
|
from .s3 import S3
|
|
|
|
|
|
async def probe(s3):
|
|
count = 0
|
|
batch = []
|
|
|
|
async def read(key, size):
|
|
params = {"Bucket": config.S3_BUCKET, "Key": key}
|
|
if size:
|
|
params["Range"] = "bytes=0-0"
|
|
response = await s3.client.get_object(**params)
|
|
async with response["Body"] as stream:
|
|
data = await stream.read()
|
|
if len(data) != min(size, 1):
|
|
raise ValueError("unexpected ranged read size")
|
|
|
|
async def flush():
|
|
results = await asyncio.gather(*(read(key, size) for key, size in batch), return_exceptions=True)
|
|
for result in results:
|
|
if isinstance(result, BaseException):
|
|
raise result
|
|
|
|
await s3.ping()
|
|
async for key, size, _ in s3.list_keys(""):
|
|
batch.append((key, size))
|
|
count += 1
|
|
if len(batch) == 8:
|
|
await flush()
|
|
batch.clear()
|
|
if batch:
|
|
await flush()
|
|
return count
|
|
|
|
|
|
async def main():
|
|
s3 = S3()
|
|
await s3.start()
|
|
try:
|
|
async with asyncio.timeout(45):
|
|
while True:
|
|
try:
|
|
count = await probe(s3)
|
|
print(f"PASS: S3 recovery, {count} existing objects readable")
|
|
return 0
|
|
except Exception as exc:
|
|
# Do not print SDK requests/credentials or object contents.
|
|
print(f"Waiting for S3 data volumes: {type(exc).__name__}", flush=True)
|
|
await asyncio.sleep(1)
|
|
except TimeoutError:
|
|
print("FAIL: S3 data recovery exceeded 45 seconds")
|
|
return 1
|
|
finally:
|
|
await s3.stop()
|
|
|
|
|
|
if __name__ == "__main__":
|
|
sys.exit(asyncio.run(main()))
|