feat: threadsafe services

In the most cases it was enough to just add lock. In case of worker
trigger, since there is atomic operation on timer, it was also required
to add queue (coz python doesn't have atomics)
This commit is contained in:
2024-01-03 03:25:13 +02:00
parent aad607eaef
commit 1af04448c9
8 changed files with 150 additions and 75 deletions

View File

@ -51,7 +51,7 @@ class DistributedSystem(Trigger, WebClient):
"time_to_live": {
"type": "integer",
"coerce": "integer",
"min": 0,
"min": 1,
},
},
},

View File

@ -17,7 +17,8 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from threading import Timer
from collections import deque
from threading import Lock, Timer
from ahriman.core.configuration import Configuration
from ahriman.core.distributed.distributed_system import DistributedSystem
@ -30,7 +31,6 @@ class WorkerTrigger(DistributedSystem):
Attributes:
ping_interval(float): interval to call remote service in seconds, defined as ``worker.time_to_live / 4``
timer(Timer): timer object
"""
def __init__(self, repository_id: RepositoryId, configuration: Configuration) -> None:
@ -45,26 +45,47 @@ class WorkerTrigger(DistributedSystem):
section = next(iter(self.configuration_sections(configuration)))
self.ping_interval = configuration.getint(section, "time_to_live", fallback=60) / 4.0
self.timer = Timer(self.ping_interval, self.ping)
self._lock = Lock()
self._timers: deque[Timer] = deque() # because python doesn't have atomics
def create_timer(self) -> None:
"""
create timer object and put it to queue
"""
timer = Timer(self.ping_interval, self.ping)
timer.start()
self._timers.append(timer)
def on_start(self) -> None:
"""
trigger action which will be called at the start of the application
"""
self.logger.info("registering instance %s at %s", self.worker, self.address)
self.timer.start()
self.logger.info("registering instance %s in %s", self.worker, self.address)
with self._lock:
self.create_timer()
def on_stop(self) -> None:
"""
trigger action which will be called before the stop of the application
"""
self.logger.info("removing instance %s at %s", self.worker, self.address)
self.timer.cancel()
self.logger.info("removing instance %s in %s", self.worker, self.address)
with self._lock:
current_timers = self._timers.copy() # will be used later
self._timers.clear() # clear timer list
for timer in current_timers:
timer.cancel() # cancel remaining timers
def ping(self) -> None:
"""
register itself as alive worker and update the timer
"""
self.register()
self.timer = Timer(self.ping_interval, self.ping)
self.timer.start()
with self._lock:
if not self._timers: # make sure that there is related specific timer
return
self._timers.popleft() # pop first timer
self.register()
self.create_timer()

View File

@ -19,6 +19,8 @@
#
import time
from threading import Lock
from ahriman.core.configuration import Configuration
from ahriman.core.log import LazyLogging
from ahriman.models.worker import Worker
@ -40,6 +42,7 @@ class WorkersCache(LazyLogging):
configuration(Configuration): configuration instance
"""
self.time_to_live = configuration.getint("worker", "time_to_live", fallback=60)
self._lock = Lock()
self._workers: dict[str, tuple[Worker, float]] = {}
@property
@ -51,17 +54,19 @@ class WorkersCache(LazyLogging):
list[Worker]: list of currently registered workers which have been seen not earlier than :attr:`time_to_live`
"""
valid_from = time.monotonic() - self.time_to_live
return [
worker
for worker, last_seen in self._workers.values()
if last_seen > valid_from
]
with self._lock:
return [
worker
for worker, last_seen in self._workers.values()
if last_seen > valid_from
]
def workers_remove(self) -> None:
"""
remove all workers from the cache
"""
self._workers = {}
with self._lock:
self._workers = {}
def workers_update(self, worker: Worker) -> None:
"""
@ -70,4 +75,5 @@ class WorkersCache(LazyLogging):
Args:
worker(Worker): worker to register
"""
self._workers[worker.identifier] = (worker, time.monotonic())
with self._lock:
self._workers[worker.identifier] = (worker, time.monotonic())

View File

@ -57,7 +57,7 @@ class Spawn(Thread, LazyLogging):
self.args_parser = args_parser
self.command_arguments = command_arguments
self.lock = Lock()
self._lock = Lock()
self.active: dict[str, Process] = {}
# stupid pylint does not know that it is possible
self.queue: Queue[ProcessStatus | None] = Queue() # pylint: disable=unsubscriptable-object
@ -141,7 +141,7 @@ class Spawn(Thread, LazyLogging):
daemon=True)
process.start()
with self.lock:
with self._lock:
self.active[process_id] = process
return process_id
@ -155,7 +155,7 @@ class Spawn(Thread, LazyLogging):
Returns:
bool: True in case if process still counts as active and False otherwise
"""
with self.lock:
with self._lock:
return process_id in self.active
def key_import(self, key: str, server: str | None) -> str:
@ -273,7 +273,7 @@ class Spawn(Thread, LazyLogging):
self.logger.info("process %s has been terminated with status %s, consumed time %ss",
terminated.process_id, terminated.status, terminated.consumed_time / 1000)
with self.lock:
with self._lock:
process = self.active.pop(terminated.process_id, None)
if process is not None:

View File

@ -17,6 +17,8 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
from threading import Lock
from ahriman.core.database import SQLite
from ahriman.core.exceptions import UnknownPackageError
from ahriman.core.log import LazyLogging
@ -34,8 +36,6 @@ class Watcher(LazyLogging):
Attributes:
database(SQLite): database instance
known(dict[str, tuple[Package, BuildStatus]]): list of known packages. For the most cases :attr:`packages`
should be used instead
repository_id(RepositoryId): repository unique identifier
status(BuildStatus): daemon status
"""
@ -51,7 +51,8 @@ class Watcher(LazyLogging):
self.repository_id = repository_id
self.database = database
self.known: dict[str, tuple[Package, BuildStatus]] = {}
self._lock = Lock()
self._known: dict[str, tuple[Package, BuildStatus]] = {}
self.status = BuildStatus()
# special variables for updating logs
@ -65,15 +66,18 @@ class Watcher(LazyLogging):
Returns:
list[tuple[Package, BuildStatus]]: list of packages together with their statuses
"""
return list(self.known.values())
with self._lock:
return list(self._known.values())
def load(self) -> None:
"""
load packages from local database
"""
self.known = {} # reset state
for package, status in self.database.packages_get(self.repository_id):
self.known[package.base] = (package, status)
with self._lock:
self._known = {
package.base: (package, status)
for package, status in self.database.packages_get(self.repository_id)
}
def logs_get(self, package_base: str, limit: int = -1, offset: int = 0) -> list[tuple[float, str]]:
"""
@ -87,6 +91,7 @@ class Watcher(LazyLogging):
Returns:
list[tuple[float, str]]: package logs
"""
self.package_get(package_base)
return self.database.logs_get(package_base, limit, offset, self.repository_id)
def logs_remove(self, package_base: str, version: str | None) -> None:
@ -123,12 +128,8 @@ class Watcher(LazyLogging):
Returns:
Changes: package changes if available
Raises:
UnknownPackageError: if no package found
"""
if package_base not in self.known:
raise UnknownPackageError(package_base)
self.package_get(package_base)
return self.database.changes_get(package_base, self.repository_id)
def package_get(self, package_base: str) -> tuple[Package, BuildStatus]:
@ -145,7 +146,8 @@ class Watcher(LazyLogging):
UnknownPackageError: if no package found
"""
try:
return self.known[package_base]
with self._lock:
return self._known[package_base]
except KeyError:
raise UnknownPackageError(package_base) from None
@ -156,7 +158,8 @@ class Watcher(LazyLogging):
Args:
package_base(str): package base
"""
self.known.pop(package_base, None)
with self._lock:
self._known.pop(package_base, None)
self.database.package_remove(package_base, self.repository_id)
self.logs_remove(package_base, None)
@ -168,17 +171,12 @@ class Watcher(LazyLogging):
package_base(str): package base to update
status(BuildStatusEnum): new build status
package(Package | None): optional package description. In case if not set current properties will be used
Raises:
UnknownPackageError: if no package found
"""
if package is None:
try:
package, _ = self.known[package_base]
except KeyError:
raise UnknownPackageError(package_base) from None
package, _ = self.package_get(package_base)
full_status = BuildStatus(status)
self.known[package_base] = (package, full_status)
with self._lock:
self._known[package_base] = (package, full_status)
self.database.package_update(package, full_status, self.repository_id)
def patches_get(self, package_base: str, variable: str | None) -> list[PkgbuildPatch]:
@ -192,6 +190,7 @@ class Watcher(LazyLogging):
Returns:
list[PkgbuildPatch]: list of patches which are stored for the package
"""
self.package_get(package_base)
variables = [variable] if variable is not None else None
return self.database.patches_list(package_base, variables).get(package_base, [])