-
-
Notifications
You must be signed in to change notification settings - Fork 6
Optimize resource usage #112
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -3,11 +3,12 @@ | |
| import datetime | ||
| import hmac | ||
| import io | ||
| import tempfile | ||
| import json | ||
| from pathlib import Path | ||
| import sys | ||
| import time | ||
| from typing import Any, Optional | ||
| from typing import Any, Optional, BinaryIO | ||
| import aiohttp | ||
| import anyio | ||
| import anyio.abc | ||
|
|
@@ -227,7 +228,7 @@ async def _download_file( | |
| for _ in range(10): | ||
| size = 0 | ||
| hash = utils.get_hash_obj(file.hash) | ||
| tmp_file = io.BytesIO() | ||
| tmp_file = tempfile.TemporaryFile() | ||
| try: | ||
| async with session.get( | ||
| file.path | ||
|
|
@@ -242,6 +243,7 @@ async def _download_file( | |
| if hash.hexdigest() != file.hash or size != file.size: | ||
| await anyio.sleep(50) | ||
| raise Exception(f"hash mismatch, got {hash.hexdigest()} expected {file.hash}") | ||
| tmp_file.seek(0) | ||
| await self.upload_storage(file, tmp_file, size) | ||
| self.update_success() | ||
| except Exception as e: | ||
|
|
@@ -259,7 +261,7 @@ async def _download_file( | |
| async def upload_storage( | ||
| self, | ||
| file: BMCLAPIFile, | ||
| data: io.BytesIO, | ||
| data: BinaryIO, | ||
| size: int | ||
| ): | ||
| missing_storage = [ | ||
|
|
@@ -598,14 +600,19 @@ async def get_files(self) -> list[BMCLAPIFile]: | |
| if resp.status == 204: # no new files | ||
| #logger.tdebug("cluster.get_files.no_new_files", id=self.id) | ||
| return results | ||
| reader = utils.AvroParser(zstd.decompress(await resp.read())) | ||
| for _ in range(reader.read_long()): | ||
| results.append(BMCLAPIFile( | ||
| reader.read_string(), | ||
| reader.read_string(), | ||
| reader.read_long(), | ||
| reader.read_long() / 1000.0, | ||
| )) | ||
| tmp = tempfile.TemporaryFile() | ||
| async for chunk in resp.content.iter_chunked(1024 * 1024 * 4): | ||
| tmp.write(chunk) | ||
| tmp.seek(0) | ||
| with zstd.open(tmp, 'rb') as zf: | ||
|
||
| reader = utils.AvroParser(zf) | ||
| for _ in range(reader.read_long()): | ||
| results.append(BMCLAPIFile( | ||
| reader.read_string(), | ||
| reader.read_string(), | ||
| reader.read_long(), | ||
| reader.read_long() / 1000.0, | ||
| )) | ||
| self._last_modified = max(results, key=lambda x: x.mtime).mtime | ||
| logger.tdebug("cluster.get_files", id=self.id, name=self.display_name, count=len(results), size=units.format_bytes(sum([f.size for f in results])), last_modified=units.format_datetime_from_timestamp(self._last_modified)) | ||
| except: | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ | |
|
|
||
| import anyio.abc | ||
| import io | ||
| import tempfile | ||
| from typing import BinaryIO | ||
|
|
||
| from core import utils | ||
| from core.abc import BMCLAPIFile, ResponseFile, ResponseFileNotFound, ResponseFileMemory, ResponseFileLocal, ResponseFileRemote | ||
|
|
@@ -151,12 +153,12 @@ async def works(root_ids: list[int]): | |
| async def upload( | ||
| self, | ||
| path: str, | ||
| data: io.BytesIO, | ||
| data: BinaryIO, | ||
| size: int | ||
| ): | ||
| raise NotImplementedError | ||
|
|
||
| async def upload_download_file(self, path: str, data: io.BytesIO, size: int): | ||
| async def upload_download_file(self, path: str, data: BinaryIO, size: int): | ||
| if self.download_dir: | ||
| path = f"download/{path}" | ||
| await self.upload(f"download/{path}", data, size) | ||
|
|
@@ -196,12 +198,20 @@ def path(self) -> 'CPath': | |
| async def write_measure(self, size: int): | ||
| path = f"measure/{size}" | ||
| size = size * 1024 * 1024 | ||
|
|
||
| tmp = tempfile.TemporaryFile() | ||
| chunk = b"\x00" * (1024 * 1024) | ||
| remain = size | ||
| while remain > 0: | ||
| w = min(remain, len(chunk)) | ||
| tmp.write(chunk[:w]) | ||
| remain -= w | ||
| tmp.seek(0) | ||
| await self.upload( | ||
| path, | ||
| io.BytesIO(b"\x00" * size), | ||
| tmp, | ||
| size | ||
| ) | ||
| tmp.close() | ||
|
Comment on lines
+201
to
+214
|
||
| logger.tsuccess("storage.write_measure", size=int(size / (1024 * 1024)), name=self.name, type=self.type) | ||
|
|
||
|
|
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The seek(0) call is not within the try block but should be, as it's part of the file processing logic that could fail. If an exception occurs before this line, the file position will be incorrect for the upload_storage call.