Commit Graph

32 Commits

Author SHA1 Message Date
Takashi Kajinami d51723fa64 Drop fallback to DEFAULT section
The fallback was added 9 years ago, in 0.1.0 release[1].

[1] c98d5edc44

Change-Id: I3b66c5f9df58eaeea9fc51a6c0b83af2e39a525b
2024-02-07 23:17:36 +00:00
Balazs Gibizer ee3f73a133 Fix fair internal lock used from eventlet.spawn_n
The fasteners lib in version 0.15.0 removed the
threading.current_thread workaround for eventlet[1] because eventlet
seemed to fixed the current_thread issues tracked in [2]. However the
fix for [2] only fixed half of the problem. The threading.current_thread
call works if it is called from thread created by eventlet.spawn.
However if the thread is created with eventlet.spawn_n then
threading.current_thread is still broken and returns the ID of the
python native thread.

The fasteners' ReaderWriterLock depends heavily on
threading.current_thread to decide which thread holds a lock and to
allow re-entry of that thread. This leads to the situation that
multiple threads created from spawn_n could take the same
ReaderWriterLock at the same time.

The fair internal lock in oslo.concurrency uses ReaderWriterLock and
as a result such lock is broken for threads created with spawn_n.

Note that this issue was raised with eventlet in [3] when the nova team
detected it via a direct usage of ReaderWriterLock in the nova test
code. As [3] did not lead to a solution in eventlet nova implemented a
nova local fix for the test code in [4].

However now we detected that oslo.concurrency is affected by this issue
as well.

This patch restores the workaround that was removed by [1].

Note that a fasteners issue [5] also opened to restore the
workaround[1].

[1] 467ed75ee1
[2] https://github.com/eventlet/eventlet/issues/172
[3] https://github.com/eventlet/eventlet/issues/731
[4] https://review.opendev.org/c/openstack/nova/+/813114
[5] https://github.com/harlowja/fasteners/issues/96

Closes-Bug: #1988311
Change-Id: Ia873bcc6b07121c9bd0b94c593567d537b4c1112
2022-09-05 13:29:04 +02:00
Gorka Eguileor 9800d58cb3 Log when waiting to acquire lock
We currently have no log entry when we start trying to acquire a lock.

In general this is ok, but there are cases where it can be problematic,
for example if we have a deadlock situation or if a lock takes a very
long time to be acquired.

In those scenarios looking at the logs we would see the operation
proceed normally and suddenly go completely silent without knowing that
it's waiting for a lock to be freed.

This patch adds a debug log message right before trying to acquire the
lock so we can detect those situations.

Change-Id: I1354dfb98b0927ae167802ecc4ab1d34f6b4d720
2022-07-07 20:01:48 +02:00
Thiago Brito 11210a957c Fix RequiredOptError when missing lock_path
When lock_file is not present on the [oslo_concurrency] section of the
config file, we are getting a misleading error saying that it is not
preset on the DEFAULT group. This commit fixes it by providing the group
arg to the RequiredOptError constructor.

Closes-Bug: #1966320

Signed-off-by: Thiago Brito <thiago.brito@windriver.com>
Change-Id: Idee6987739917c7eae4faee15a66085a9ef7f6d2
2022-03-24 15:13:48 -03:00
Romain Poupin a9ccf0a64d Add support for non-blocking locks
This adds support for a non-blocking behavior of the lock : the context-based
lock now raises an AcquireLockFailedException if the lock can't be acquired on
the first try.

At a higher layer, we just caught this exception inside the 'synchronized'
decorator to prevent calling the wrapped function. In which case, we then
trace this acquisition failure.

For now at least, disabling blocking is not supported when using fair locks
because the ReaderWriterLock.write_lock() provided by the fasteners module
doesn't implements this behavior.

Change-Id: I409da79007c9ba4fb8585da881e3d56998b0b98b
2021-06-24 09:45:48 +02:00
Zuul 80a6e1d489 Merge "Don't warn on lock removal if file doesn't exist" 2020-06-08 17:05:16 +00:00
Andreas Jaeger dd5a141771 Remove six usage
Remove the usage of six, the python2-python3 compatibility lib.

Change-Id: I56e479d0749504d65be1cf2b5235d454b0b90a1a
2020-05-09 16:42:30 +02:00
Gorka Eguileor e57a1c829d Don't warn on lock removal if file doesn't exist
There may be cases were library users want to request the removal of an
external lock that may not have been created and don't want to pollute
the logs when the file is not present.

We raise the log level from info to warning, but we don't log it if the
file didn't exist, since the end result the caller wanted is there.

Change-Id: I5ce8be34c9f2c4c59ea99dabc6760c3300f743a3
2020-03-18 14:57:49 +01:00
Eric Fried c08159119e Spiff up docs for *_with_prefix
Following up on comments in [1], clarify and unify the docs for the
following lockutils methods:

- lock_with_prefix
- synchronized_with_prefix
- remove_external_lock_file_with_prefix

[1] I4e723ee3be1e57c543684390b607c84388c6e930

Change-Id: I4179e8732dba7367bd0c835cbd11939ca7b8cc42
2019-11-04 15:28:27 -06:00
Eric Fried fec03875e9 Add lock_with_prefix convenience utility
There's a convenience wrapper around the lockutils.synchronized
decorator that lets oslo.concurrency consumers set up a prefix to use
for all lock files (presumably to obviate collisions with other
consumers when using jejune lock names, like 'lock'). This commit adds
an equivalent wrapper around lockutils.lock, the context manager
counterpart to the lockutils.synchronized decorator.

Note that the unit test added for lock_with_prefix is pretty bare; but
it follows the precedent set by the existing tests. Future commits
should make all these tests more thorough/robust.

Change-Id: I4e723ee3be1e57c543684390b607c84388c6e930
2019-09-17 16:23:01 -05:00
Zuul 0767ddf4c2 Merge "Add support for fair locks" 2018-11-05 23:22:41 +00:00
Chris Friesen 2b55da68ae Add support for fair locks
This adds support for a "fair" variant of the lock.  When there are
multiple entities within a single process that are blocked waiting
for the lock the fair lock will ensure that they acquire the lock
in FIFO order.

For now at least, when fair locks are in use we don't support
the "semaphores" argument.

If external locks are enabled, the inter-process ordering will be
determined by the underlying OS lock ordering and process scheduling.

Change-Id: I37577becff4978bf643c65fa9bc2d78d342ea35a
2018-11-02 10:40:23 -06:00
Vu Cong Tuan a9d728b71e Switch to stestr
According to Openstack summit session [1],
stestr is maintained project to which all Openstack projects should migrate.
Let's switch to stestr as other projects have already moved to it.

[1] https://etherpad.openstack.org/p/YVR-python-pti

Change-Id: I753f32ecf3275cf49d8c93bf648a6a26bc6da8e7
2018-07-04 08:18:31 +07:00
Gary Kotton 9b3c8f34a9 Add debug log to indicate when external lock is taken
In a case when a number of threads are concurrently accessing a
resource that has a external lock there is no indication when
one of the threads has taken the external lock.

This patch adds in a debug log to try and add some clarity.

Change-Id: Ibd7cfd3b39370b1a54f2ab50c75c6b92a876ae1a
2017-07-26 17:54:27 +03:00
loooosy f4b6622407 Remove log translations
Log messages are no longer being translated.
This removes all use of the _LE, _LI, and _LW
translation markers to simplify logging and to
avoid confusion with new contributions.

See:
http://lists.openstack.org/pipermail/openstack-i18n/2016-November/002574.html
http://lists.openstack.org/pipermail/openstack-dev/2017-March/113365.html

Change-Id: Iec2ef31f8543d51c8b6e1aed53f950800813c30b
2017-07-11 02:40:05 +00:00
ChangBo Guo(gcb) 7279c316c8 Fix wrong import example in docstring
Change-Id: I0abc5403851f9ab2f78cdf26d29b82c3ec76ec25
2016-05-11 19:21:43 +08:00
Yuriy Taraday 499d5aa36d Revert "Use tempfile.tempdir for lock_path if OSLO_LOCK_PATH is not set"
This reverts commit 5021ef82fd.

That commit set default location for lock files to a world-writable
directory (like /tmp) which violates security precaution stated right in
the help string of lock_path config variable: lock_path should be
writable by the user of the process using it only.

There is some history behind this:

- when we decided to not do tempfile.mkdtemp if lock_path were not set
  (because it was broken), it broke upgrades for all components
  (starting with Cinder;
- it was suggested to provide the same default value:
  https://review.openstack.org/60274
  but it was not accepted because it weakens security;
- there was a thread about this in ML, please read it for more history:
  http://lists.openstack.org/pipermail/openstack-dev/2013-December/021055.html
  it ended up with understanding that only a project using the library
  can provide sane default value for lock_path, not the library itself.

Change-Id: I801004ad3c83862849696f00c6e7ca9877fd496b
2016-02-26 16:36:17 +03:00
Sean M. Collins 5021ef82fd Use tempfile.tempdir for lock_path if OSLO_LOCK_PATH is not set
DevStack spends a lot of time setting lock_path in a bunch of
locations.

http://codesearch.openstack.org/?q=oslo_concurrency%20lock_path&i=nope&files=&repos=

The default for lock_path is an environment variable
OSLO_LOCK_PATH.

http://codesearch.openstack.org/?q=OSLO_LOCK_PATH&i=nope&files=&repos=

Since the default is to consult an environment variable, which may not
even be set, this means that really there is no default. If you do
not set it - you end up getting errors like:

http://paste.openstack.org/show/488108/

Ideally - a library should have a reasonable default, where if a user
doesn't set something, the default will at least work. So, let's use
Python's built in tempfile module, which has fairly complex rules for
determining a sane directory to place temporary items in.

https://docs.python.org/2/library/tempfile.html#tempfile.tempdir

Change-Id: I6906af43bc0255cd215f2d9584ea000c81f5880e
2016-02-25 15:34:52 +00:00
Joshua Harlow 68d941b5ea Add complementary remove lock with prefix function
It seems we provide (and projects use) the provided decorator
'synchronized_with_prefix' but we don't provide an equivalent function
to complement that function to clean up its created lock files.

Providing a complementary method seems pretty useful for projects
that actually clean up these lock files (if any actually do in the
first place).

Change-Id: I601ce3992411e6a2ddded13aba4ac068cf8f14e2
2015-11-30 12:13:44 -08:00
Victor Stinner e01418018f Use versionadded and versionchanged in doc
Document in which version new types and functions were added using
".. versionadded:: x.y". Document changes using
".. versionchanged:: x.y."

For watchdog module, add the versionadded tag in the module top
docstring, not on each type/function.

Add docstring to ssh_execute().

I used "git blame" + "git tag --contains=SHA1" to find these version,
and then I checked manually each version.

Change-Id: I56a7d8a4335833e9b49eedab026d20adfeedf942
2015-11-05 13:31:40 +01:00
Joshua Harlow 3aaa63986c Use oslo_utils reflection to get 'f' callable name
Using the utility function gets a better name.

For example:

$ python

>>> from oslo_utils import reflection
>>> class A(object):
...   def m(self):
...     pass
... 
>>> z = A()
>>> reflection.get_callable_name(z.m)
'__main__.A.m'

Versus:

>>> z.m.__name__
'm'

Change-Id: I2daadd969383aaf49ad87876ba108dd80dd56f08
2015-08-09 00:07:41 +00:00
Joshua Harlow a0fabfc688 Use better timing mechanisms instead of time.time()
To ensure we show accurate timing information about
lock acqusition and release times use a timing mechanism
which can not/should not move backwards.

Change-Id: I9559b20cf7de67fc474e6e17eda23791ecc4122e
2015-06-22 10:34:30 -07:00
Joshua Harlow 9f22c45397 Replace locks and replace with fasteners library provides ones
The fasteners library (extracted from this library and a couple other
variations) provides the interprocess lock logic and the reader writer
lock logic so we can remove the local version and we can just use it
from that library instead.

The tests that were ensuring the internals of this file lock have
now moved to the repo where that library is (for the time being),
currently travis is testing that repo against py2.6, py2.7 and py3.4.

https://github.com/harlowja/fasteners/tree/master/fasteners/tests

Docs also exist at:

http://fasteners.readthedocs.org/en/latest/

Change-Id: I98565b22e68358efe28fea62f74f8ebfcc438ff7
2015-06-06 00:44:22 +00:00
Doug Hellmann 46fcdd3aca Add lockutils.get_lock_path() function
Provide an API to discover the path being used to store external lock
files. Tempest will use this to set up some additional locking and to
test its own behavior. Other projects might use it for error reporting
or other purposes.

Change-Id: Iad40c67072333cc25a6d3e39d7535ff14b573504
2015-03-11 19:51:54 +00:00
Doug Hellmann da8176dbdf Update Oslo imports to remove namespace package
Change-Id: I4ec9b2a310471e4e07867073e9577731ac34027d
Blueprint: drop-namespace-packages
2015-01-21 18:00:50 -05:00
Jenkins 590444c1f5 Merge "Add a reader/writer lock" 2015-01-06 22:59:33 +00:00
Joshua Harlow 90663364f8 Add a reader/writer lock
Taskflow has a reader/writer lock that is likely useful
to other projects; and it seems better at home in this
module.

The class provides a way to create reader/writer locks
where there may be many readers at the same time (but
only one writer). It does not allow (currently) for privilege
escalation (but this could be added with limited support
in the future).

Change-Id: Ie763ef92f31c34869d83a533bc8761b0fbd77217
2014-12-19 15:39:25 -08:00
Ihar Hrachyshka 9dd6d212f0 Don't use ConfigFilter for lockutils
In Neutron, we usually specify lock_path as:

lock_path = $state_path/lock

It turned out that ConfigFilter does not work correctly with
substitution feature, so its usage blocks Neutron adoption of the
library.

The issue is also not specific to Neutron: any deployer should be able
to use substitutions ($smth) in their configuration files without
determining whether those configuration options he's interested in are
defined as ConfigOpts or ConfigFilter.

So the best short term approach to push forward the adoption of the
library is to refrain from using ConfigFilter at least until there is
consensus on how to support substitution feature with the class.

Once oslo.config sorts the issue out, we may get back to using
ConfigFilter for lockutils.

Change-Id: I7b0b5c840381b2d9afc00ff116fee1d970743183
Closes-Bug: #1399897
2014-12-15 11:36:10 +01:00
Joshua Harlow 19f07c600c Add a TODO for retrying pull request #20
Sometime in a future retrying version there will
likely/hopefully exist a ability to more easily
filter on specific exceptions; this commit adds a
note to the upstream pull request that may add this
feature.

Change-Id: I44f1c9fe4fbbd5f77032ca7bfff4e2e6b7ff7622
2014-11-25 14:09:06 -08:00
Joshua Harlow 46c836ee28 Allow the lock delay to be provided
When a lock can't be acquired there is currently a hard coded
delay (0.01) that is used before trying again, instead of having
a hard coded delay we should allow this delay to be configured
since having it set at a hard coded value can limit concurrency (if
the delay is actually way to high) or cause to much contention (if
the delay is actually way to low).

This review adds on that logic and also uses the retrying library
to perform the acquisition attempts (and associated failures when/if
they occur); as well as shows logs after a given amount of time has
elapsed with the logs being output at a given periodicity.

Change-Id: Ideeefba1439ddd677c608d01becb4f6a0d4bc83d
2014-11-20 22:09:45 -08:00
Joshua Harlow 3bda65ccbb Allow for providing a customized semaphore container
It can be undesirable to have a globally shared sempahore
container, especially since oslo.concurrency can now be shared
among many disjoint applications and libraries.

When a single container is used it is now possible to have those
disjoint applications/libraries collide on the same sempahore names.
This is not a good pattern to continue with, so in order to move away
from it allow a custom container to be provided (which defaults to the
existing global one) so that users of oslo.concurrency may provide there
own container if they so desire.

Change-Id: I9aab42e21ba0f52997de3e7c9b0fea51db5c7289
2014-11-19 17:15:40 -08:00
Doug Hellmann bca4a0d827 Move out of the oslo namespace package
Move the public API out of oslo.concurrency to oslo_concurrency. Retain
the ability to import from the old namespace package for backwards
compatibility for this release cycle.

bp/drop-namespace-packages

Change-Id: I20d1647b1c3ef8cab3b69eccfe168eeb01703b72
2014-11-14 17:17:25 -05:00