Skip to content
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

refactor: create_standby: add semaphore during delta generating to limit pending tasks in memory #320

Merged
merged 6 commits into from
Jun 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/otaclient/app/configs.py
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class BaseConfig(_InternalSettings):
# now only REBUILD mode is available
STANDBY_CREATION_MODE = CreateStandbyMechanism.REBUILD
MAX_CONCURRENT_PROCESS_FILE_TASKS = 512
MAX_PROCESS_FILE_THREAD = 6
CREATE_STANDBY_RETRY_MAX = 1024
CREATE_STANDBY_BACKOFF_FACTOR = 1
CREATE_STANDBY_BACKOFF_MAX = 6
Expand Down
17 changes: 14 additions & 3 deletions src/otaclient/app/create_standby/common.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
import random
import threading
import time
from concurrent.futures import ThreadPoolExecutor
from concurrent.futures import Future, ThreadPoolExecutor
from dataclasses import dataclass
from hashlib import sha256
from pathlib import Path
Expand Down Expand Up @@ -325,13 +325,23 @@ def _process_delta_src(self):

# ------ create the threadpool executor ------ #
thread_local = threading.local()
max_pending_tasks = threading.Semaphore(cfg.MAX_CONCURRENT_PROCESS_FILE_TASKS)

def _initializer():
thread_local.buffer = buffer = bytearray(cfg.CHUNK_SIZE)
thread_local.view = memoryview(buffer)

def _task_done_callback(fut: Future[Any]):
max_pending_tasks.release() # always release se first
if exc := fut.exception():
logger.warning(
f"detect error during file preparing, still continue: {exc!r}"
)

pool = ThreadPoolExecutor(
thread_name_prefix="scan_slot", initializer=_initializer
max_workers=cfg.MAX_PROCESS_FILE_THREAD,
thread_name_prefix="scan_slot",
initializer=_initializer,
)

# scan old slot and generate delta based on path,
Expand Down Expand Up @@ -417,11 +427,12 @@ def _initializer():
self._rm.append(str(canonical_fpath))
continue

max_pending_tasks.acquire()
pool.submit(
self._prepare_local_copy_from_active_slot,
delta_src_fpath,
thread_local=thread_local,
)
).add_done_callback(_task_done_callback)

# wait for all files being processed
pool.shutdown(wait=True)
Expand Down
Loading