def test_writer_chaotic(self): lock = lockutils.ReaderWriterLock() activated = collections.deque() def chaotic_writer(blow_up): with lock.write_lock(): if blow_up: raise RuntimeError("Broken") else: activated.append(lock.owner_type) def happy_reader(): with lock.read_lock(): activated.append(lock.owner_type) # Test that every 4th reader blows up and that we get the expected # number of owners with this occuring. max_workers = 8 with futures.ThreadPoolExecutor(max_workers=max_workers) as e: for i in range(0, max_workers): if i % 2 == 0: e.submit(chaotic_writer, blow_up=bool(i % 4 == 0)) else: e.submit(happy_reader) writers = [ a for a in activated if a == lockutils.ReaderWriterLock.WRITER ] readers = [ a for a in activated if a == lockutils.ReaderWriterLock.READER ] self.assertEqual(2, len(writers)) self.assertEqual(4, len(readers))
def test_double_reader_abort(self): lock = lockutils.ReaderWriterLock() activated = collections.deque() def double_bad_reader(): with lock.read_lock(): with lock.read_lock(): raise RuntimeError("Broken") def happy_writer(): with lock.write_lock(): activated.append(lock.owner_type) # Submit a bunch of work to a pool, and then ensure that the correct # number of writers eventually executed (every other thread will # be a reader thread that will fail)... max_workers = 8 with futures.ThreadPoolExecutor(max_workers=max_workers) as e: for i in range(0, max_workers): if i % 2 == 0: e.submit(double_bad_reader) else: e.submit(happy_writer) self.assertEqual( max_workers / 2, len([ a for a in activated if a == lockutils.ReaderWriterLock.WRITER ]))
def __init__(self, context, plugin_rpc, local_vlan_map=None, defer_refresh_firewall=False, integration_bridge=None): self.context = context self.plugin_rpc = plugin_rpc self.init_firewall(defer_refresh_firewall, integration_bridge) # _latest_port_filter_lock will point to the lock created for the # most recent thread to enter _apply_port_filters(). self._latest_port_filter_lock = lockutils.ReaderWriterLock()
def decorated_function(self, *args, **kwargs): lock = lockutils.ReaderWriterLock() # Tracking the most recent lock at the instance level allows # waiters to only wait for the most recent lock to be released # instead of waiting until all locks have been released. self._latest_port_filter_lock = lock with lock.write_lock(): return func(self, *args, **kwargs)
def test_double_reader(self): lock = lockutils.ReaderWriterLock() with lock.read_lock(): self.assertTrue(lock._is_reader()) self.assertFalse(lock._is_writer()) with lock.read_lock(): self.assertTrue(lock._is_reader()) self.assertTrue(lock._is_reader()) self.assertFalse(lock._is_reader()) self.assertFalse(lock._is_writer())
def __init__(self): self._ctxt_mgrs = {} self._last_ctxt_mgr = None self._default_ctxt_mgr = None # NOTE(danms): Use a ReaderWriterLock to synchronize our # global database muckery here. If we change global db state # to point to a cell, we need to take an exclusive lock to # prevent any other calls to get_context_manager() until we # reset to the default. self._cell_lock = lockutils.ReaderWriterLock()
def test_reader_abort(self): lock = lockutils.ReaderWriterLock() self.assertFalse(lock.owner_type) def blow_up(): with lock.read_lock(): self.assertEqual(lock.READER, lock.owner_type) raise RuntimeError("Broken") self.assertRaises(RuntimeError, blow_up) self.assertFalse(lock.owner_type)
def test_writer_abort(self): # Ensures that the lock is released when the writer has an # exception... lock = lockutils.ReaderWriterLock() self.assertFalse(lock.owner_type) def blow_up(): with lock.write_lock(): self.assertEqual(lock.WRITER, lock.owner_type) raise RuntimeError("Broken") self.assertRaises(RuntimeError, blow_up) self.assertFalse(lock.owner_type)
def test_writer_to_reader(self): lock = lockutils.ReaderWriterLock() def reader_func(): with lock.read_lock(): pass with lock.write_lock(): self.assertRaises(RuntimeError, reader_func) self.assertFalse(lock._is_reader()) self.assertFalse(lock._is_reader()) self.assertFalse(lock._is_writer())
def test_single_reader_writer(self): results = [] lock = lockutils.ReaderWriterLock() with lock.read_lock(): self.assertTrue(lock._is_reader()) self.assertEqual(0, len(results)) with lock.write_lock(): results.append(1) self.assertTrue(lock._is_writer()) with lock.read_lock(): self.assertTrue(lock._is_reader()) self.assertEqual(1, len(results)) self.assertFalse(lock._is_reader()) self.assertFalse(lock._is_writer())
def __init__(self, nas_address, nas_username, nas_password): self.nas_address = nas_address self.nas_username = nas_username self.nas_password = nas_password self.url = None self.session = None self.semaphore = threading.Semaphore(30) self.call_lock = lockutils.ReaderWriterLock() LOG.warning("Suppressing requests library SSL Warnings") requests.packages.urllib3.disable_warnings( requests.packages.urllib3.exceptions.InsecureRequestWarning) requests.packages.urllib3.disable_warnings( requests.packages.urllib3.exceptions.InsecurePlatformWarning)
def __init__(self, adapter, host_uuid): super(LocalStorage, self).__init__(adapter, host_uuid) # Query to get the Volume Group UUID if not CONF.powervm.volume_group_name: raise npvmex.OptRequiredIfOtherOptValue( if_opt='disk_driver', if_value='localdisk', then_opt='volume_group_name') self.vg_name = CONF.powervm.volume_group_name self._vios_uuid, self.vg_uuid = tsk_stg.find_vg(self.vg_name) self.image_cache_mgr = imagecache.ImageManager(self._vios_uuid, self.vg_uuid, adapter) self.cache_lock = lockutils.ReaderWriterLock() LOG.info(_LI("Local Storage driver initialized: volume group: '%s'"), self.vg_name)
def _spawn_variation(cls, readers, writers, max_workers=None): """Spawns the given number of readers and writers.""" start_stops = collections.deque() lock = lockutils.ReaderWriterLock() def read_func(ident): with lock.read_lock(): # TODO(harlowja): sometime in the future use a monotonic clock # here to avoid problems that can be caused by ntpd resyncing # the clock while we are actively running. enter_time = time.time() time.sleep(cls.WORK_TIMES[ident % len(cls.WORK_TIMES)]) exit_time = time.time() start_stops.append((lock.READER, enter_time, exit_time)) time.sleep(cls.NAPPY_TIME) def write_func(ident): with lock.write_lock(): enter_time = time.time() time.sleep(cls.WORK_TIMES[ident % len(cls.WORK_TIMES)]) exit_time = time.time() start_stops.append((lock.WRITER, enter_time, exit_time)) time.sleep(cls.NAPPY_TIME) if max_workers is None: max_workers = max(0, readers) + max(0, writers) if max_workers > 0: with futures.ThreadPoolExecutor(max_workers=max_workers) as e: count = 0 for _i in range(0, readers): e.submit(read_func, count) count += 1 for _i in range(0, writers): e.submit(write_func, count) count += 1 writer_times = [] reader_times = [] for (lock_type, start, stop) in list(start_stops): if lock_type == lock.WRITER: writer_times.append((start, stop)) else: reader_times.append((start, stop)) return (writer_times, reader_times)
def test_double_reader_writer(self): lock = lockutils.ReaderWriterLock() activated = collections.deque() active = threading.Event() def double_reader(): with lock.read_lock(): active.set() # Wait for the writer thread to get into pending mode using a # simple spin-loop... while not lock._has_pending_writers(): time.sleep(0.001) with lock.read_lock(): activated.append(lock.owner_type) def happy_writer(): with lock.write_lock(): activated.append(lock.owner_type) reader = threading.Thread(target=double_reader) reader.daemon = True reader.start() # Wait for the reader to become the active reader. active.wait() self.assertTrue(active.is_set()) # Start up the writer (the reader will wait until its going). writer = threading.Thread(target=happy_writer) writer.daemon = True writer.start() # Ensure it went in the order we expected. reader.join() writer.join() self.assertEqual(2, len(activated)) self.assertEqual([ lockutils.ReaderWriterLock.READER, lockutils.ReaderWriterLock.WRITER ], list(activated))
def __init__(self, adapter, host_uuid): super(LocalStorage, self).__init__(adapter, host_uuid) # Query to get the Volume Group UUID if not CONF.powervm.volume_group_name: raise npvmex.OptRequiredIfOtherOptValue( if_opt='disk_driver', if_value='localdisk', then_opt='volume_group_name') self.vg_name = CONF.powervm.volume_group_name vios_w, vg_w = tsk_stg.find_vg(adapter, self.vg_name) self._vios_uuid = vios_w.uuid self.vg_uuid = vg_w.uuid self.image_cache_mgr = imagecache.ImageManager(self._vios_uuid, self.vg_uuid, adapter) self.cache_lock = lockutils.ReaderWriterLock() # Set the 'snapshot' capability dynamically. If we're hosting I/O on # the management partition, we can snapshot. If we're hosting I/O on # traditional VIOS, we are limited by the fact that a VSCSI device # can't be mapped to two partitions (the VIOS and the management) at # once. self.capabilities['snapshot'] = self.mp_uuid == self._vios_uuid LOG.info("Local Storage driver initialized: volume group: '%s'", self.vg_name)
from oslo_service import loopingcall from oslo_utils import fileutils from oslo_utils import importutils import six from neutron._i18n import _ from neutron.agent.common import resource_processing_queue as queue from neutron.agent.linux import dhcp from neutron.agent.linux import external_process from neutron.agent.metadata import driver as metadata_driver from neutron.agent import rpc as agent_rpc from neutron.common import utils from neutron import manager LOG = logging.getLogger(__name__) _SYNC_STATE_LOCK = lockutils.ReaderWriterLock() DEFAULT_PRIORITY = 255 DHCP_PROCESS_GREENLET_MAX = 32 DHCP_PROCESS_GREENLET_MIN = 8 def _sync_lock(f): """Decorator to block all operations for a global sync call.""" @six.wraps(f) def wrapped(*args, **kwargs): with _SYNC_STATE_LOCK.write_lock(): return f(*args, **kwargs) return wrapped
from oslo_concurrency import lockutils from oslo_utils import timeutils import time import random import threading rwlock = lockutils.ReaderWriterLock() cache = [] index = 0 dur = 10 def push(): global index global rwlock with timeutils.StopWatch(duration=dur) as w: while not w.expired(): with rwlock.write_lock() as l: num = index index += 1 print("pushing start: %d" % num) time.sleep(random.random()) cache.append(num) time.sleep(random.random()) print("pushing over : %d" % num) time.sleep(1)
import six import pypowervm.exceptions as ex from pypowervm.i18n import _ import pypowervm.tasks.partition as tpar import pypowervm.utils.transaction as tx import pypowervm.wrappers.iocard as card import pypowervm.wrappers.managed_system as ms LOG = logging.getLogger(__name__) # Take read_lock on operations that create/delete VFs (including VNIC). This # is a read_lock so we don't serialize all VF creation globally. # Take write_lock on operations that modify properties of physical ports and # rely on knowing the usage counts thereon (e.g. changing port labels). PPORT_MOD_LOCK = lock.ReaderWriterLock() def set_vnic_back_devs(vnic_w, pports, sys_w=None, vioses=None, redundancy=1, capacity=None, check_port_status=False): """Set a vNIC's backing devices over given SRIOV physical ports and VIOSes. Assign the backing devices to a iocard.VNIC wrapper using an anti-affinity algorithm. That is, the method attempts to distribute the backing devices across as diverse a range of physical SRIOV adapters and VIOSes as possible, using the least-saturated ports first. For example, given: