def test_resource_lock(self): with NamedTemporaryFile('rw') as file: with ResourceFile(file.name) as lock: self.assertEquals(lock.used(), False) lock.acquire({'foo': 'bar', 'pid': os.getpid()}) # Used should return a dict with ResourceFile(file.name) as lock: info = lock.used() self.assertEquals(info['foo'], 'bar') self.assertEquals(info['pid'], os.getpid())
def new_func(self, request): # Get some info about the controller call info = inspect(self, request, lock_file_path) # create a ResourceFile object to manage volume ownership resource = ResourceFile(info['lock_file']) try: # Attempt to claim exclusive ownership of the volume claim(resource, info) # Execute the controller method return func(self, request, resource) finally: # Remove the resource file if we own it resource.remove()
def new_func(self, request): # Get some info about the controller call info = inspect(self, request, lock_file_path) # create a ResourceFile object to manage volume ownership resource = ResourceFile(info['lock_file']) try: # Attempt to claim exclusive ownership of the volume claim(resource, info) # Execute the controller method return func(self, request, resource) finally: # Remove the resource file if we own it resource.remove()
def test_interruptible_lock(self): resource = ResourceFile(self.lockfile) fake_controller = FakeController(volume_id='foo', id='bar', run_dir='somewhere') req_1 = FakeRequest(method='PUT', path="something") req_2 = FakeRequest(method='PUT', path="something/else") @lock(self.lockfile) def killable_func(obj, req, resource): with resource: resource.update({'interruptible': True}) while True: time.sleep(1) @lock(self.lockfile) def killing_func(obj, req, resource): while True: time.sleep(1) # Go killable func child! child1 = os.fork() if not child1: killable_func(fake_controller, req_1) else: self.children.append(child1) time.sleep(1) with resource: used = resource.used() self.assertEquals(used['interruptible'], True) self.assertEquals(used['uri'], 'PUT something') self.assertEquals(used['pid'], child1) # Go killing func child! child2 = os.fork() if not child2: killing_func(fake_controller, req_2) else: self.children.append(child2) time.sleep(1) with resource: used = resource.used() self.assertEquals(used['interruptible'], False) self.assertEquals(used['uri'], 'PUT something/else') self.assertEquals(used['pid'], child2)
def test_interruptible_lock(self): resource = ResourceFile(self.lockfile) fake_controller = FakeController(volume_id='foo', id='bar', run_dir='somewhere') req_1 = FakeRequest(method='PUT', path="something") req_2 = FakeRequest(method='PUT', path="something/else") @lock(self.lockfile) def killable_func(obj, req, resource): with resource: resource.update({'interruptible': True}) while True: time.sleep(1) @lock(self.lockfile) def killing_func(obj, req, resource): while True: time.sleep(1) # Go killable func child! child1 = os.fork() if not child1: killable_func(fake_controller, req_1) else: self.children.append(child1) time.sleep(1) with resource: used = resource.used() self.assertEquals(used['interruptible'], True) self.assertEquals(used['uri'], 'PUT something') self.assertEquals(used['pid'], child1) # Go killing func child! child2 = os.fork() if not child2: killing_func(fake_controller, req_2) else: self.children.append(child2) time.sleep(1) with resource: used = resource.used() self.assertEquals(used['interruptible'], False) self.assertEquals(used['uri'], 'PUT something/else') self.assertEquals(used['pid'], child2)
def _in_use(self, volume_id): resource_file = self._resource_file(volume_id) if not exists(resource_file): return False with ResourceFile(resource_file) as lock: return lock.used()
def setUp(self): self.scratch = mkdtemp() self.storage = MockStorageNode(self.scratch) self._orig_subprocess = utils.subprocess utils.subprocess = MockSubprocess(storage=self.storage) self.conf = LunrConfig({ 'export': { 'ietd_config': os.path.join(self.scratch, 'ietd.conf'), 'proc_iet_volume': self.storage.proc_iet_volume, 'proc_iet_session': self.storage.proc_iet_session, 'device_prefix': self.storage.device_prefix, 'initiators_allow': os.path.join(self.scratch, 'initiators.allow'), }, 'storage': { 'skip_fork': True, 'run_dir': self.storage.run_dir }, 'volume': { 'device_prefix': self.storage.device_prefix }, 'disk': { 'path': os.path.join(self.scratch, 'backups') }, 'glance': { 'glance_urls': 'snet1,snet2', 'glance_mgmt_urls': 'mgmt1, mgmt2', } }) self.lockfile = os.path.join(self.scratch, 'lock') self.lock = ResourceFile(self.lockfile)
def test_lock_used(self): resource = ResourceFile(self.lockfile) child = os.fork() if not child: # Child grabs resource and sleeps until killed. with resource: resource.acquire({'pid': os.getpid()}) while True: time.sleep(5) time.sleep(0.2) with resource: used = resource.used() self.assert_(used) self.assertEquals(used['pid'], child) os.kill(child, signal.SIGTERM) os.waitpid(child, 0)
def test_lock_used(self): resource = ResourceFile(self.lockfile) child = os.fork() if not child: # Child grabs resource and sleeps until killed. with resource: resource.acquire({'pid': os.getpid()}) while True: time.sleep(5) time.sleep(0.2) with resource: used = resource.used() self.assert_(used) self.assertEquals(used['pid'], child) os.kill(child, signal.SIGTERM) os.waitpid(child, 0)
def lock(self, req): info = inspect(self, req, "volumes/%(id)s/resource") with ResourceFile(info['lock_file']) as lock: used = lock.used() if used: resp = {'in-use': True} resp.update(used) return Response(resp) pass return Response({'in-use': False})
def get(self, volume, backup_id): running = self._backup_is_running(volume['id'], backup_id) if not running: raise NotFound("no active backup running on '%s' called '%s'" % (volume['id'], backup_id)) stats_file = self._stats_file(volume['id']) with ResourceFile(stats_file) as lock: stats = lock.read() return { 'lock': self._resource_file(volume['id']), 'status': 'RUNNING', 'stats': stats }
def test_interruptible_claim(self): resource = ResourceFile(self.lockfile) child = os.fork() if not child: # Child grabs resource and sleeps until killed. with resource: resource.acquire({'pid': os.getpid(), 'uri': 'child'}) time.sleep(2) with resource: resource.acquire({'interruptible': True}) while(True): time.sleep(0.5) else: self.children.append(child) # This is racy. Child is originally uninterruptible, but after a short # sleep, he marks himself interruptible time.sleep(1) info = {'uri': 'parent', 'pid': os.getpid()} self.assertRaises(HTTPConflict, claim, resource, info) time.sleep(2) claim(resource, info) time.sleep(1) # should be killed by now. pid, status = os.waitpid(child, os.WNOHANG) self.assertEquals(pid, child) with resource: used = resource.used() self.assert_(used) self.assertEquals(used['pid'], os.getpid()) self.assertEquals(used['uri'], 'parent') # Bug: lock.acquire only updates the keys you give it. # So I'm marked interruptible unknowingly. # lunr.storage.controller.base.inspect was updated to always # set interruptible to False because of this. self.assertEquals(used['interruptible'], True)