def execute(self, **kwargs): """Wrapper of action execution. This is mainly a wrapper that executes an action with cluster lock acquired. :returns: A tuple (res, reason) that indicates whether the execution was a success and why if it wasn't a success. """ # Try to lock cluster before do real operation forced = True if self.action == self.CLUSTER_DELETE else False res = senlin_lock.cluster_lock_acquire(self.context, self.target, self.id, self.owner, senlin_lock.CLUSTER_SCOPE, forced) if not res: return self.RES_ERROR, _('Failed in locking cluster.') try: res, reason = self._execute(**kwargs) finally: senlin_lock.cluster_lock_release(self.target, self.id, senlin_lock.CLUSTER_SCOPE) return res, reason
def execute(self, **kwargs): '''Wrapper of action execution. This is mainly a wrapper that executes an action with cluster lock acquired. :return: A tuple (res, reason) that indicates whether the execution was a success and why if it wasn't a success. ''' try: cluster = cluster_mod.Cluster.load(self.context, self.target) except exception.NotFound: reason = _('Cluster %(id)s not found') % {'id': self.target} LOG.error(_LE(reason)) return self.RES_ERROR, reason # Try to lock cluster before do real operation forced = True if self.action == self.CLUSTER_DELETE else False res = senlin_lock.cluster_lock_acquire(cluster.id, self.id, senlin_lock.CLUSTER_SCOPE, forced) if not res: return self.RES_ERROR, _('Failed locking cluster') try: res, reason = self._execute(cluster) finally: senlin_lock.cluster_lock_release(cluster.id, self.id, senlin_lock.CLUSTER_SCOPE) return res, reason
def execute(self, **kwargs): """Wrapper of action execution. This is mainly a wrapper that executes an action with cluster lock acquired. :returns: A tuple (res, reason) that indicates whether the execution was a success and why if it wasn't a success. """ # Try to lock cluster before do real operation forced = (self.action == consts.CLUSTER_DELETE) res = senlin_lock.cluster_lock_acquire(self.context, self.target, self.id, self.owner, senlin_lock.CLUSTER_SCOPE, forced) # Failed to acquire lock, return RES_RETRY if not res: return self.RES_RETRY, 'Failed in locking cluster.' try: # Refresh entity state to avoid stale data in action. self.entity = cluster_mod.Cluster.load(self.context, self.target) res, reason = self._execute(**kwargs) finally: senlin_lock.cluster_lock_release(self.target, self.id, senlin_lock.CLUSTER_SCOPE) return res, reason
def execute(self, **kwargs): """Interface function for action execution. :param dict kwargs: Parameters provided to the action, if any. :returns: A tuple containing the result and the related reason. """ # Since node.cluster_id could be reset to '' during action execution, # we record it here for policy check and cluster lock release. forced = (self.action in [consts.NODE_DELETE, consts.NODE_OPERATION]) saved_cluster_id = self.entity.cluster_id if saved_cluster_id: if self.cause == consts.CAUSE_RPC: res = senlin_lock.cluster_lock_acquire(self.context, self.entity.cluster_id, self.id, self.owner, senlin_lock.NODE_SCOPE, False) if not res: return self.RES_RETRY, 'Failed in locking cluster' try: self.policy_check(self.entity.cluster_id, 'BEFORE') finally: if self.data['status'] != pb.CHECK_OK: # Don't emit message since policy_check should have # done it senlin_lock.cluster_lock_release( saved_cluster_id, self.id, senlin_lock.NODE_SCOPE) return self.RES_ERROR, ('Policy check: ' + self.data['reason']) elif self.cause == consts.CAUSE_DERIVED_LCH: self.policy_check(saved_cluster_id, 'BEFORE') try: res = senlin_lock.node_lock_acquire(self.context, self.entity.id, self.id, self.owner, forced) if not res: res = self.RES_RETRY reason = 'Failed in locking node' else: res, reason = self._execute() if saved_cluster_id and self.cause == consts.CAUSE_RPC: self.policy_check(saved_cluster_id, 'AFTER') if self.data['status'] != pb.CHECK_OK: res = self.RES_ERROR reason = 'Policy check: ' + self.data['reason'] finally: senlin_lock.node_lock_release(self.entity.id, self.id) if saved_cluster_id and self.cause == consts.CAUSE_RPC: senlin_lock.cluster_lock_release(saved_cluster_id, self.id, senlin_lock.NODE_SCOPE) return res, reason
def execute(self, **kwargs): """Interface function for action execution. :param dict kwargs: Parameters provided to the action, if any. :returns: A tuple containing the result and the related reason. """ # Since node.cluster_id could be reset to None in _execute progress, # we record it here for policy check and cluster lock release. saved_cluster_id = self.node.cluster_id if self.node.cluster_id: if self.cause == base.CAUSE_RPC: res = senlin_lock.cluster_lock_acquire( self.context, self.node.cluster_id, self.id, self.owner, senlin_lock.NODE_SCOPE, False) if not res: return self.RES_RETRY, _('Failed in locking cluster') self.policy_check(self.node.cluster_id, 'BEFORE') if self.data['status'] != policy_mod.CHECK_OK: # Don't emit message here since policy_check should have # done it if self.cause == base.CAUSE_RPC: senlin_lock.cluster_lock_release( self.node.cluster_id, self.id, senlin_lock.NODE_SCOPE) return self.RES_ERROR, 'Policy check: ' + self.data['reason'] reason = '' try: res = senlin_lock.node_lock_acquire(self.context, self.node.id, self.id, self.owner, False) if not res: res = self.RES_ERROR reason = _('Failed in locking node') else: res, reason = self._execute() if res == self.RES_OK and saved_cluster_id is not None: self.policy_check(saved_cluster_id, 'AFTER') if self.data['status'] != policy_mod.CHECK_OK: res = self.RES_ERROR reason = 'Policy check: ' + self.data['reason'] else: res = self.RES_OK finally: senlin_lock.node_lock_release(self.node.id, self.id) if saved_cluster_id is not None and self.cause == base.CAUSE_RPC: senlin_lock.cluster_lock_release(saved_cluster_id, self.id, senlin_lock.NODE_SCOPE) return res, reason
def execute(self, **kwargs): # Since node.cluster_id could be reset to None in _execute progress, # we record it here for policy check and cluster lock release. saved_cluster_id = self.node.cluster_id if self.node.cluster_id: if self.cause == base.CAUSE_RPC: res = senlin_lock.cluster_lock_acquire( self.node.cluster_id, self.id, senlin_lock.NODE_SCOPE, False) if not res: return self.RES_RETRY, _('Failed in locking cluster') self.policy_check(self.node.cluster_id, 'BEFORE') if self.data['status'] != policy_mod.CHECK_OK: # Don't emit message here since policy_check should have # done it if self.cause == base.CAUSE_RPC: senlin_lock.cluster_lock_release( self.node.cluster_id, self.id, senlin_lock.NODE_SCOPE) return self.RES_ERROR, 'Policy check: ' + self.data['reason'] reason = '' try: res = senlin_lock.node_lock_acquire(self.node.id, self.id, False) if not res: res = self.RES_ERROR reason = _('Failed in locking node') else: res, reason = self._execute() if res == self.RES_OK and saved_cluster_id is not None: self.policy_check(saved_cluster_id, 'AFTER') if self.data['status'] != policy_mod.CHECK_OK: res = self.RES_ERROR reason = 'Policy check: ' + self.data['reason'] else: res = self.RES_OK finally: senlin_lock.node_lock_release(self.node.id, self.id) if saved_cluster_id is not None and self.cause == base.CAUSE_RPC: senlin_lock.cluster_lock_release(saved_cluster_id, self.id, senlin_lock.NODE_SCOPE) return res, reason
def test_cluster_lock_release(self, mock_release): actual = lockm.cluster_lock_release('C', 'A', 'S') self.assertEqual(mock_release.return_value, actual) mock_release.assert_called_once_with('C', 'A', 'S')