Skip to content
Open
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
4 changes: 4 additions & 0 deletions searchkit/exception.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ class FileSearchException(Exception):
""" Exception raised when an error occurs during a file search. """
def __init__(self, msg):
self.msg = msg


class ResultsPurgeTimeout(FileSearchException):
""" Exception raised when results purge times out. """
30 changes: 15 additions & 15 deletions searchkit/results_store.py
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
""" Search result store implementations. """
import copy
import multiprocessing
import os
from collections import UserDict

from searchkit.log import log
from searchkit.exception import ResultStoreException

RESULTS_STORE_LOCK = multiprocessing.Lock()


def rs_locked(f):
def _rs_locked_inner(*args, **kwargs):
with RESULTS_STORE_LOCK:
return f(*args, **kwargs)
def _rs_locked_inner(self, *args, **kwargs):
with self.lock:
return f(self, *args, **kwargs)

return _rs_locked_inner

Expand Down Expand Up @@ -129,6 +126,9 @@ class ResultStoreParallel(ResultStoreBase):

def __init__(self, mgr, **kwargs):
super().__init__(**kwargs)
# Per-store (not module-global) so that a lock abandoned by a killed
# worker is never reused by a later search.
self.lock = mgr.Lock()
# Replace super attributes with MP-safe equivalents
self.alloc_pointer = mgr.Value('i', 0)
self.data = mgr.dict()
Expand All @@ -140,27 +140,27 @@ def __init__(self, mgr, **kwargs):
# with the main store until the end which is a slow action.
self._local_store = None

@rs_locked
def preallocate(self, size):
""" Allocate a block of indexes. This is passed to worker processes
so they can save data to their local store with global indexes. """
with RESULTS_STORE_LOCK:
current = self.alloc_pointer.value
allocated = list(range(current, current + size))
self.alloc_pointer.value += size
log.debug("owner=%s allocated range: %s:%s",
self._local_store['owner'],
allocated[0], allocated[-1])
current = self.alloc_pointer.value
allocated = list(range(current, current + size))
self.alloc_pointer.value += size
log.debug("owner=%s allocated range: %s:%s",
self._local_store['owner'],
allocated[0], allocated[-1])

return allocated

@rs_locked
def _allocate_next(self, value):
"""
We allocate from the shared store but save to the local store. This way
allocations are global/shared between all workers. When we finally sync
the local store to the shared store the allocations will not conflict.
"""
with RESULTS_STORE_LOCK:
return super()._allocate_next(value)
return super()._allocate_next(value)

@property
def local(self):
Expand Down
Loading
Loading