8000 Use only a single global lock for multiproc. by brian-brazil · Pull Request #167 · prometheus/client_python · GitHub
[go: up one dir, main page]

Skip to content

Use only a single global lock for multiproc. #167

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

Merged
merged 1 commit into from
Jun 7, 2017
Merged
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
28 changes: 15 additions & 13 deletions prometheus_client/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,7 @@ def get(self):
with self._lock:
return self._value


class _MmapedDict(object):
"""A dict of doubles, backed by an mmapped file.

Expand All @@ -317,9 +318,10 @@ class _MmapedDict(object):
There's then a number of entries, consisting of a 4 byte int which is the
size of the next field, a utf-8 encoded string key, padding to a 8 byte
alignment, and then a 8 byte float which is the value.

Not thread safe.
"""
def __init__(self, filename):
self._lock = Lock()
self._f = open(filename, 'a+b')
if os.fstat(self._f.fileno()).st_size == 0:
self._f.truncate(_INITIAL_MMAP_SIZE)
Expand Down Expand Up @@ -371,17 +373,15 @@ def read_all_values(self):
yield k, v

def read_value(self, key):
with self._lock:
if key not in self._positions:
self._init_value(key)
if key not in self._positions:
self._init_value(key)
pos = self._positions[key]
# We assume that reading from an 8 byte aligned value is atomic
return struct.unpack_from(b'd', self._m, pos)[0]

def write_value(self, key, value):
with self._lock:
if key not in self._positions:
self._init_value(key)
if key not in self._positions:
self._init_value(key)
pos = self._positions[key]
# We assume that writing to an 8 byte aligned value is atomic
struct.pack_into(b'd', self._m, pos, value)
Expand All @@ -395,7 +395,10 @@ def close(self):
def _MultiProcessValue(__pid=os.getpid()):
pid = __pid
files = {}
files_lock = Lock()
# Use a single global lock when in multi-processing mode
# as we presume this means there is no threading going on.
# This avoids the need to also have mutexes in __MmapDict.
lock = Lock()

class _MmapedValue(object):
'''A float protected by a mutex backed by a per-process mmaped file.'''
Expand All @@ -407,28 +410,27 @@ def __init__(self, typ, metric_name, name, labelnames, labelvalues, multiprocess
file_prefix = typ + '_' + multiprocess_mode
else:
file_prefix = typ
with files_lock:
with lock:
if file_prefix not in files:
filename = os.path.join(
os.environ['prometheus_multiproc_dir'], '{0}_{1}.db'.format(file_prefix, pid))
files[file_prefix] = _MmapedDict(filename)
self._file = files[file_prefix]
self._key = json.dumps((metric_name, name, labelnames, labelvalues))
self._value = self._file.read_value(self._key)
self._lock = Lock()

def inc(self, amount):
with self._lock:
with lock:
self._value += amount
self._file.write_value(self._key, self._value)

def set(self, value):
with self._lock:
with lock:
self._value = value
self._file.write_value(self._key, self._value)

def get(self):
with self._lock:
with lock:
return self._value

return _MmapedValue
Expand Down
0