Ejemplo n.º 1
0
class Singleton(type):
    _instances = {}
    _semaphores = lockutils.Semaphores()

    def __call__(cls, *args, **kwargs):
        with lockutils.lock('singleton_lock', semaphores=cls._semaphores):
            if cls not in cls._instances:
                cls._instances[cls] = super(
                    Singleton, cls).__call__(*args, **kwargs)
        return cls._instances[cls]
Ejemplo n.º 2
0
    def test_lock_internally_different_collections(self):
        s1 = lockutils.Semaphores()
        s2 = lockutils.Semaphores()
        trigger = threading.Event()
        who_ran = collections.deque()

        def f(name, semaphores, pull_trigger):
            with lockutils.internal_lock('testing', semaphores=semaphores):
                if pull_trigger:
                    trigger.set()
                else:
                    trigger.wait()
                who_ran.append(name)

        threads = [
            threading.Thread(target=f, args=(1, s1, True)),
            threading.Thread(target=f, args=(2, s2, False)),
        ]
        for thread in threads:
            thread.start()
        for thread in threads:
            thread.join()
        self.assertEqual([1, 2], sorted(who_ran))
Ejemplo n.º 3
0
 def setUp(self):
     super(BaseTest, self).setUp()
     init_test_conf()
     self.session = db.get_session()
     engine = db.get_engine()
     db.Base.metadata.create_all(engine)
     engine.connect()
     self.addCleanup(db.get_engine().dispose)
     plugins_base._HOOKS_MGR = None
     node_cache._SEMAPHORES = lockutils.Semaphores()
     for name in ('_', '_LI', '_LW', '_LE', '_LC'):
         patch = mock.patch.object(i18n, name, lambda s: s)
         patch.start()
         # 'p=patch' magic is due to how closures work
         self.addCleanup(lambda p=patch: p.stop())
Ejemplo n.º 4
0
 def setUp(self):
     super(BaseTest, self).setUp()
     if not self.IS_FUNCTIONAL:
         self.init_test_conf()
     self.session = db.get_writer_session()
     engine = self.session.get_bind()
     db.Base.metadata.create_all(engine)
     engine.connect()
     self.addCleanup(engine.dispose)
     plugins_base._HOOKS_MGR = None
     node_cache._SEMAPHORES = lockutils.Semaphores()
     patch = mock.patch.object(i18n, '_', lambda s: s)
     patch.start()
     # 'p=patch' magic is due to how closures work
     self.addCleanup(lambda p=patch: p.stop())
     utils._EXECUTOR = futurist.SynchronousExecutor(green=True)
Ejemplo n.º 5
0
from sqlalchemy.orm import exc as orm_errors
from sqlalchemy import text

from ironic_inspector.common.i18n import _
from ironic_inspector.common import ironic as ir_utils
from ironic_inspector import db
from ironic_inspector import introspection_state as istate
from ironic_inspector import utils

CONF = cfg.CONF

LOG = utils.getProcessingLogger(__name__)

MACS_ATTRIBUTE = 'mac'
_LOCK_TEMPLATE = 'node-%s'
_SEMAPHORES = lockutils.Semaphores()


def _get_lock(uuid):
    """Get lock object for a given node UUID."""
    return lockutils.internal_lock(_LOCK_TEMPLATE % uuid,
                                   semaphores=_SEMAPHORES)


def _get_lock_ctx(uuid):
    """Get context manager yielding a lock object for a given node UUID."""
    return lockutils.lock(_LOCK_TEMPLATE % uuid, semaphores=_SEMAPHORES)


class NodeInfo(object):
    """Record about a node in the cache.
Ejemplo n.º 6
0
 def do_test():
     s = lockutils.Semaphores()
     with lockutils.lock('testlock', 'test-', semaphores=s, fair=True):
         pass