40 Commits

Author SHA1 Message Date
Ben Nemec
f1688a70f2 Use file locks by default again
After a lot of discussion[1], it was decided that this is the
safest way forward.  In the future we can investigate alternative
locking methods, probably as opt-ins so we don't break any
existing consumers of these locks.

[1] http://lists.openstack.org/pipermail/openstack-dev/2014-August/043090.html

Change-Id: I49ff98abc395d3263d55aeff26081b462a8a294e
Partial-Bug: 1327946
2014-08-22 17:13:25 +00:00
Yuriy Taraday
1761de8f02 Make all tests pass 2014-08-07 22:27:31 +04:00
Julien Danjou
5e9187e866 lockutils: split tests and run in Python 3
Split tests so eventlet specific tests are run in a separate file and
ignored in Python 3.

Change-Id: Ie2cc486a58af36b653e2bcc183548d07f807ddfd
2014-07-17 16:10:36 +02:00
Thomas Herve
39e2f751f8 Allow test_lockutils to run in isolation
While trying to port oslo to python 3, we discovered that test_lockutils
wasn't working when not run with the global test suite. The fix can be
tested running:

$ python -m openstack.common.lockutils python -m testtools.run \
  tests.unit.test_lockutils

Change-Id: I51461f63cdd8e7eb6f8b6fdfa838748a582ecdad
2014-07-03 10:23:07 +02:00
Ihar Hrachyshka
f4c5c4c612 pep8: fixed multiple violations
Fixed violations:
* E128 continuation line under-indented for visual indent
* E251 unexpected spaces around keyword / parameter equals
* E265 block comment should start with '# '
* E713 test for membership should be 'not in'
* F402 import shadowed by loop variable
* H305  imports not grouped correctly
* H307  like imports should be grouped together'
* H402  one line docstring needs punctuation'
* H703  Multiple positional placeholders

Also enabled H803 check that didn't have any violations.

Change-Id: I957ab273ddc61b02763d6b60b21d11ed4e73d472
2014-06-20 23:29:23 +02:00
Victor Sergeyev
b13d73acb4 Use oslotest instead of common test module
Module openstack.common.test is obsolete, so we should use
oslotest library instead of it.
Modified tests and common database code, new requirement added.

Change-Id: I853e548f11a4c3785eaf75124510a6d789536634
2014-04-15 17:53:05 +03:00
Ben Nemec
c626b293d7 Allow external locks to work with threads
External locks by themselves do not work with greenthreads in a
single process because multiple greenthreads are allowed to grab
the file lock at once.  Previous lockutils code also grabbed a
local semaphore for external locks so this was not a problem, but
after the recent refactoring that was no longer happening, which
caused some issues in other projects.

Since it's less confusing to have external locks also work as
internal locks, this change switches the behavior back to what it
was before.  It also adds some test cases to verify these external
lock behaviors.

Change-Id: If66d358c7bd3960c1cc9c8d392fcc158963943bc
2014-03-04 00:46:36 +00:00
Ben Nemec
20587c14fd Re-enable file-based locking behavior
There are places in Nova (at least) that require file-based locking
behavior for locking on shared filesystems.  These situations can
be recognized because those calls to synchronized specify a
lock_path, so when that happens we need to return a file-based lock
instead of a posix lock.

Change-Id: I21dd17d58d1c321566c72e808a055a0bd625c647
2014-02-28 23:31:29 +00:00
Julien Danjou
71619f5094 Use Posix IPC in lockutils
Rather than creating temporary files in a file system let's leverage the
Posix IPC interface to create our inter process locking mechanism.

Blueprint: lockutils-posix-ipc

Change-Id: Ia87c3bdad64f0b0f6cb9fc2878add3fd1708041f
2014-02-27 15:30:43 +01:00
Jenkins
2eafeab012 Merge "Add remove external lock files API in lockutils" 2014-02-21 14:17:29 +00:00
jichenjc
48e09f934b Add remove external lock files API in lockutils
when external lock are used, there will be a lock files which
is used for external file lock, it will not be removed and remain
forever. it will be a problem when tens of thousands of this kind
of actions. Add a API to allow other component to remove lock file
internal lock will be acquired before further action

Change-Id: I4d996b91bcc55becb0ebf4ec77ac80546f433ae9
Closes-Bug: 1256306
2014-01-26 00:25:30 +08:00
Joshua Harlow
ef72b05476 Use threading.ThreadError instead of reraising IOError
The threading API specifies that ThreadError should
be used as the common exception raised from threading
and locking apis so instead of reraising an IOError
reraise a new ThreadError one instead.

Change-Id: Ic06dd211a8a5da074623ac449d27e963dd5ba754
2014-01-17 19:50:32 -08:00
Joshua Harlow
fab74ff451 Have the interprocess lock follow lock conventions
The python locking api specifies a acquire/release
function pair. It is nice to have the interprocess
lock api also follow said api so that its objects
can be used in areas where the standard threading
locks are used.

Change-Id: If393bec863ced8c9ef413b4baa3267d2a5b38416
2014-01-17 19:34:22 -08:00
Julien Danjou
fc8f4361de lockutils: remove lock_path parameter
This remove the parameter lock_path so the configured lock_path is
always used instead. There's only a few places where this is used in
OpenStack, and it's not heavily justified to have it even in this
places.

This is needed to simplify the interface toward moving to IPC locking
mechanism. With such a locking mechanism in place, the lock_path
parameter has absolutely no sense. So isolating this to make it only
relevant to some part of the driver and not in the public interface
makes more sense.

Change-Id: I72bd683209090ae6e3911a709e4d213879508aca
Blueprint: lockutils-posix-ipc
2014-01-16 15:10:01 +01:00
Wu Wenxiang
c569770656 Remove unused variables
Refactor codes, remove unused variables. This patch is the first step.
F841 check in tox.ini will be enabled to make sure that there are no
userd variables in project anymore.

Change-Id: I3edd5bb5e3c203d1d3d92261ed74a201878d6f5a
Partial-Bug: #1268234
2014-01-13 22:32:46 +08:00
Julien Danjou
b1560bb1c0 lockutils: fix testcase wrt Semaphore
Nothing returns anymore an eventlet semaphore, so stop trying to check
for that.

Change-Id: I272b754875259604565c9b658cf76a3378f71cc2
2013-12-27 14:08:21 +01:00
Chang Bo Guo
acf0dc1b72 Unify different names between Python2/3 with six.moves
Some modules use different names in Python2 and Python3. Use six.moves
to make them work well in Python2 and Python3.
This is changes mapping:

six.moves      Python 2                 Python 3
builtins        __builtin__ 	        builtins
filter 	       itertools.ifilter() 	filter()
reduce 	       reduce() 	        functools.reduce()
reload_module  reload() 	        imp.reload()

Implements: blueprint make-python3-compatible
Change-Id: I97971f2ab40385bfc2c73ae7e8a7620c4d64a03c
2013-11-26 07:08:21 -08:00
Joe Gordon
579704f994 Remove vim header
No need to set tabstop 189 times, this can be set in your vimrc file
instead.  Also if set incorrectly gate (pep8 check) will catch your
mistakes.

Change-Id: Ic6f0c0ef94e8194a5c121598305d1ec3c74e4843
2013-11-25 12:16:11 +08:00
Yuriy Taraday
27f918c916 Add main() to lockutils that creates temp dir for locks
This change would allow us to create one directory to store all locks
from all processes involved in testing in projects.

Here are examples on how to use this:
Heat - https://review.openstack.org/47360
Ceilometer - https://review.openstack.org/44809
(see tox.ini)

Change-Id: I9e1260e202b7d8feb9c762a879b9804ed85ccbd6
2013-10-25 21:40:50 +04:00
Yuriy Taraday
ec4e3cfa14 Allow lockutils to get lock_path conf from envvar
This would allow us to provide a common dir for all locks in all
processes involved in testing or running software.

Change-Id: I64fccddc92ada67a3df6daf86d73750a41bbce05
2013-10-25 21:00:52 +04:00
Davanum Srinivas
2a2a0880a6 Move LockFixture into a fixtures module
In Nova and other projects, fixtures are in test-requirements.txt and
NOT in requirements.txt so updating nova to use latest lockutils.py
is not possible. So let us move LockFixture out into it a separate
fixtures.py module where in theory we can add other fixtures as
well. See https://review.openstack.org/#/c/52153/ for an attempt to
try latest lockutils.py in Nova.

Change-Id: I12942d6390d06bb889d4145e708692fa886d99b5
2013-10-20 22:08:22 +00:00
Matthew Treinish
b225345cd2 Add LockFixture to lockutils
This commit adds a fixture class to be used instead of external
locking with the synchronized decorator. When running tests in
parallel the synchronized decorator won't synchronize the tearDown
and addCleanup methods for a test. By using the fixture which does
LockFixture the unlocking is done by using addCleanup() ensuring that
tearDown and later addCleanup methods are run inside the lock
context.

Closes-Bug: #1229437

Change-Id: I61af5e9d385bb7ef2e6402366fb8c5c09f749986
2013-10-09 13:45:04 -04:00
Victor Sergeyev
803ede7370 Modify lockutils.py due to dispose of eventlet
Change lockutils.py due to use threading.Semaphore instead of
eventlet.semaphore.Semaphore. Tests modified properly.

Change-Id: Ic3c5934577aff856ec71ba7e44b54aa963fda69c
2013-08-28 11:11:55 +03:00
Sergey Kraynev
b2e26dc920 Replace using tests.utils part2
It is the second step to replace using tests.utils with
stack.common.test.
In this patch second part of tests is changed for using
openstack.common.test and added needed fixtures in test classes.

Change-Id: I81e3ff41827f7153b650255e219ce25cb282507b
bp: common-unit-tests
2013-08-23 08:43:05 +00:00
Zhongyue Luo
bdfdf7bbd2 Bump hacking to 0.7.0
Fixes H202, H602 errors

Change-Id: Ic3045f15116d1d7a28b63bc94a0a341b9304ffdd
2013-08-17 02:34:58 +09:00
Ben Nemec
3eaacea25e Fix locking bug
When lock_path is not set, lockutils creates a new temp dir for
each new call to synchronized.  This results in no actual lock
enforcement.  Require setting of lock_path by throwing an
exception if it is not found.

Fixes bug 1065531
Fixes bug 1162047

Change-Id: I178684a1d8649b5bcfcb768be0a68c8efa3f00e4
2013-07-18 12:11:49 -05:00
Flaper Fesp
4a5687b0b5 Move synchronized body to a first-class function
synchronized is being widely used throughout OpenStack for managing
locks by decorating methods / functions. However, it's functionality is
trapped within that decorator definition and makes it difficult to
manage locks by using it in other cases (like w/o decorating).

This patch moves decorator's body into a first-class function called
lock which is intended to be used as a context manager type (with
statement).

Some examples:

    with lockutils.lock("test") as sem:
        print('Inside the lock')

Things I'm not 100% convinced:

    * The `lock` function yields a Semaphore when external is False and
    an InterProcessLock instance otherwise. Although it is not 'good' to
    yield different values depending on the input parameters, it's
    pretty explicit (by reading the documentation) that external locks
    are handled differently. Other options for this case are:

        1. Always yield a Semaphore instance
        2. Don't yield anything

The patch doesn't break backward compatibility so no change is needed to
existing projects.

Implements blueprint cache-backend-abstraction

Change-Id: I96b2ee84da5a2dcd7f0bd469f8a1e52b74e50c75
2013-07-15 17:32:44 +02:00
Flaper Fesp
82433a46dc Make lock_file_prefix optional
Currently, the lock_file_prefix says it should end with an hypen if
specified. It seems that it was intended to be optional. Since that
behavior makes sense, this patch changes the synchronized's signature
and makes lock_file_prefix optional.

With this patch, the prefix will not be required to end with an hypen
anymore, such sign will be added to the prefix if specified and if not
already present.

This patch doesn't change synchronized's behavior at all, which means it
doesn't break backward compatibility.

Change-Id: I8841da1e8d105aac4a86b247228a9c935e00afc5
2013-07-15 17:32:44 +02:00
Sergey Lukjanov
1dd5fcd0fc Enable hacking H402 test
H402  one line docstring needs punctuation

Change-Id: Ie848453cace318d8310cdf0234c512f4c1121119
2013-06-03 07:53:21 +04:00
Zhongyue Luo
e70e9ba566 Added convenience APIs for lockutils
The lock_file_prefix for each project doesn't need to be configurable or
frequently changed. This patch provides a convenience API which returns
a partial object of the synchronized decorator to avoid passing the prefix
each time locks are used.

The set_defaults method is also provided to change the default value of
lock_path when needed.

Fixes bug #1065524

Change-Id: I7b67f0a482da4be6d53a70db5bbd22dc91bdc10c
2013-05-09 13:20:29 +09:00
Gary Kotton
aa51749bb8 Locking edge case when lock_path does not exist
Fixes bug 1158179

In the case that the lock_path does not exist and there is a contested
resource then the process that was waiting on the lock will not
be locking due to the fact that the directory was deleted.

Change-Id: I75d720d4df499e85386d3e2cc86b927b017e12ac
2013-03-22 10:37:26 +00:00
Mark McLoughlin
bda7d1e8cd lockutils: add a failing unit test
Add a unit test which fails because of #1107950 - in this case,
lockutils is deleting the lock directory every time the lock is dropped
even though another process might have acquired a lock using that
directory.

Change-Id: Ic82409f9462e570bc102ab469334c53aafc6d7ac
2013-03-22 10:33:57 +00:00
Mark McLoughlin
74fc415f44 lockutils: improve the external locks test
Use a separate directory for the lockutils external locks and for the
test's flocks which are used to check that serialization is actually
occurring. By using the same directory, we can't test the code in
lockutils which auto-creates this directory because the dir gets
deleted by a process by lockutils cleanup while another directory
is using it for flocks.

Also, assume the the handles directory has been created in the parent
rather than having each child attempt to create it.

Related, add a try/finally block so that when a child process throws an
exception it immediately exits rather than deleting the temporary
directories created by the parent.

Change-Id: I32d7e8e05fb3f22cf38fa586f8bc97646c83f182
2013-03-22 10:14:48 +00:00
Zhongyue Luo
2fbb82b14b Removes unused imports in the tests module
Fixes bug #1157596

Change-Id: I36d5484eaa2f0e21188eed6e70cc1ad785233d6a
2013-03-20 16:24:31 +08:00
Monty Taylor
2670fa0557 Replace direct use of testtools BaseTestCase.
Using the BaseTestCase across the tests in the tree lets us put in log
fixtures and consistently handle mox and stubout.

Part of blueprint grizzly-testtools.

Change-Id: Iba7eb2c63b0c514009b2c28e5930b27726a147b0
2013-01-24 16:30:26 +11:00
Monty Taylor
72cadd4f38 Use testtools as test base class.
On the path to testr migration, we need to replace the unittest base classes
with testtools.
Replace tearDown with addCleanup, addCleanup is more resilient than tearDown.
The fixtures library has excellent support for managing and cleaning
tempfiles. Use it.
Replace skip_ with testtools.skipTest

Part of blueprint grizzly-testtools.

Change-Id: I45e11bbb1ff9b31f3278d3b016737dcb7850cd98
2013-01-24 16:26:43 +11:00
Zhongyue Luo
2ce696b94d Fixes import order errors
Change-Id: I3e35230dd2d96ab9f5a8c11b9ec1cd8d2d00e347
2013-01-18 18:06:50 +08:00
Davanum Srinivas
30b597b322 Eliminate sleep in the lockutils test case (across processes)
Fork many processes and try to lock the same set of files
using flock without blocking and make sure we wait for all the processes
to finish as well.

Fixes LP #1068316

Change-Id: I09964b2c5af63f31b5ddee1f18eaf646f8d8ba58
2013-01-07 08:56:03 -05:00
Mark McLoughlin
b6b2f86b94 Disable lockutils test_synchronized_externally
We're seeing this test fail very regularly in Jenkins and it's become
a serious distraction for everyone.

Disable the test temporarily and use a bug #1095957 to track that the
test itself needs fixing.

Change-Id: I0645d8f5f740d40f83d82e5ef7a048f33a44ac2b
2013-01-04 10:44:26 +00:00
Michael Still
3aabd9f0ae Move nova's util.synchronized decorator to openstack common.
In the end I needed to port utils.ensure_tree as well. Resolves
bug 1063230.

Change-Id: I6e6fa8201de2cac3f17e6c60d7b16f7df7c64116
2012-10-18 09:09:17 -07:00