def rpc_unbond_entity(self, ctxt, entity, **kwargs): """从库收到解绑主库命令""" dbtype = self._dbtype(entity) dbinfo = self.konwn_database[entity] if dbinfo.get('slave') == 0: return resultutils.AgentRpcResult( agent_id=self.manager.agent_id, resultcode=manager_common.RESULT_ERROR, ctxt=ctxt, result= 'Can not unbond from master, database is master database?') dbmanager = utils.impl_cls('rpc', dbtype) cfgfile = self._db_conf(entity, dbtype) with self.lock(entity, timeout=3): p = self._entity_process(entity) if not p: return resultutils.AgentRpcResult( agent_id=self.manager.agent_id, ctxt=ctxt, result='unbond to master faile, slave db process not exist' ) dbmanager.unbond(cfgfile, postrun=None, timeout=None, **kwargs) return resultutils.AgentRpcResult(agent_id=self.manager.agent_id, ctxt=ctxt, result='unbond from master success')
def rpc_slave_entity(self, ctxt, entity, **kwargs): """主库收到绑定从库命令""" bond = kwargs.pop('bond') dbtype = self._dbtype(entity) dbinfo = self.konwn_database[entity] port = self._get_port(entity) replication = privilegeutils.mysql_replprivileges( bond.get('database_id'), bond.get('host')) kwargs['replication'] = replication if dbinfo.get('slave') != 0: return resultutils.AgentRpcResult( agent_id=self.manager.agent_id, resultcode=manager_common.RESULT_ERROR, ctxt=ctxt, result='Can not bond a slave, database is not master database?' ) dbmanager = utils.impl_cls('rpc', dbtype) cfgfile = self._db_conf(entity, dbtype) with self.lock(entity, timeout=3): p = self._entity_process(entity) if not p: return resultutils.AgentRpcResult( agent_id=self.manager.agent_id, resultcode=manager_common.RESULT_ERROR, ctxt=ctxt, result='Cat not bond a slave, master db process not exist') def _bond_slave(_binlog, _scheams): LOG.debug('Try bond slave database') self.client.database_bond(database_id=bond.get('database_id'), body={ 'master': dbinfo.get('database_id'), 'host': self.manager.local_ip, 'port': port, 'passwd': replication.get('passwd'), 'file': _binlog.get('File'), 'position': _binlog.get('Position'), 'schemas': _scheams, }) dbmanager.bondslave(cfgfile, postrun=_bond_slave, timeout=None, dbinfo=dbinfo, **kwargs) return resultutils.AgentRpcResult( agent_id=self.manager.agent_id, ctxt=ctxt, result='bond slave for master success')
def rpc_revoke_entity(self, ctxt, entity, **kwargs): dbtype = self._dbtype(entity) dbmanager = utils.impl_cls('rpc', dbtype) cfgfile = self._db_conf(entity, dbtype) with self.lock(entity, timeout=3): p = self._entity_process(entity) if not p: return resultutils.AgentRpcResult( agent_id=self.manager.agent_id, ctxt=ctxt, result='revoke entity fail, process not exist') dbmanager.revoke(cfgfile, postrun=None, timeout=None, **kwargs) return resultutils.AgentRpcResult(agent_id=self.manager.agent_id, ctxt=ctxt, result='revoke to master success')
def rpc_stop_entity(self, ctxt, entity, **kwargs): dbtype = self._dbtype(entity) dbmanager = utils.impl_cls('rpc', dbtype) p = self._entity_process(entity) if p: cfgfile = self._db_conf(entity, dbtype) dbmanager.stop(cfgfile, postrun=None, timeout=None, process=p) return resultutils.AgentRpcResult( agent_id=self.manager.agent_id, ctxt=ctxt, result='stop database entity success')
def rpc_entity_replication_ready(self, ctxt, entity, **kwargs): dbtype = self._dbtype(entity) dbmanager = utils.impl_cls('rpc', dbtype) cfgfile = self._db_conf(entity, dbtype) with self.lock(entity, timeout=3): p = self._entity_process(entity) if not p: return resultutils.AgentRpcResult( agent_id=self.manager.agent_id, ctxt=ctxt, result='get replication status fail, process not exist') success, msg = dbmanager.replication_status(cfgfile, postrun=None, timeout=None, **kwargs) return resultutils.AgentRpcResult( agent_id=self.manager.agent_id, resultcode=manager_common.RESULT_SUCCESS if success else manager_common.RESULT_ERROR, ctxt=ctxt, result=msg)
def rpc_start_entity(self, ctxt, entity, **kwargs): dbtype = self._dbtype(entity) dbmanager = utils.impl_cls('rpc', dbtype) cfgfile = self._db_conf(entity, dbtype) p = self._entity_process(entity) with self.lock(entity, timeout=3): if not p: dbmanager.start(cfgfile) eventlet.sleep(0.5) p = self._entity_process(entity) if not p: return resultutils.AgentRpcResult( agent_id=self.manager.agent_id, resultcode=manager_common.RESULT_ERROR, ctxt=ctxt, result='start entity faile, process not exist after start') return resultutils.AgentRpcResult( agent_id=self.manager.agent_id, ctxt=ctxt, result='start entity success, runinng on pid %d' % p.info.get('pid'))
def rpc_create_entity(self, ctxt, entity, **kwargs): memory = psutil.virtual_memory() leftmem = memory.cached / (1024 * 1024) + memory.free / (1024 * 1024) if leftmem < 1000: return resultutils.AgentRpcResult( agent_id=self.manager.agent_id, resultcode=manager_common.RESULT_ERROR, ctxt=ctxt, result='create database fail, memory left %d MB' % leftmem) entity = int(entity) with self.lock(entity, timeout=3): if entity in self.entitys: return resultutils.AgentRpcResult( agent_id=self.manager.agent_id, resultcode=manager_common.RESULT_ERROR, ctxt=ctxt, result='create %s database fail, entity exist' % entity) timeout = count_timeout(ctxt, kwargs) try: port = self.create_entity(entity, timeout, **kwargs) resultcode = manager_common.RESULT_SUCCESS result = 'create database success' except Exception as e: resultcode = manager_common.RESULT_ERROR result = 'create database fail with %s:%s' % ( e.__class__.__name__, str(e.message) if hasattr( e, 'message') else 'unknown err msg') return resultutils.AgentRpcResult( agent_id=self.manager.agent_id, ctxt=ctxt, resultcode=resultcode, result=result, ) return CreateResult(agent_id=self.manager.agent_id, ctxt=ctxt, resultcode=resultcode, result=result, connection=self.manager.local_ip, port=port)
def rpc_status_entity(self, ctxt, entity, **kwargs): dbtype = self._dbtype(entity) p = self._entity_process(entity) database_id = self.konwn_database[entity].get('database_id') if not p: result = '%s entity %d(database_id %d) not running' % ( dbtype, entity, database_id) else: result = '%s entity %d(database_id %d) running at pid %d' % ( dbtype, entity, database_id, p.pid) return resultutils.AgentRpcResult(agent_id=self.manager.agent_id, ctxt=ctxt, result=result)
def rpc_delete_entity(self, ctxt, entity, **kwargs): entity = int(entity) token = kwargs.pop('token') timeout = count_timeout(ctxt, kwargs if kwargs else {}) while self.frozen: if timeout < 1: raise RpcTargetLockException(self.namespace, str(entity), 'endpoint locked') eventlet.sleep(1) timeout -= 1 timeout = min(1, timeout) details = [] with self.lock(entity, timeout): if entity not in set(self.entitys): return resultutils.AgentRpcResult( agent_id=self.manager.agent_id, resultcode=manager_common.RESULT_ERROR, ctxt=ctxt, result='delete database fail, entity not exist') try: self.delete_entity(entity, token) resultcode = manager_common.RESULT_SUCCESS result = 'delete %d success' % entity except Exception as e: resultcode = manager_common.RESULT_ERROR result = 'delete %d fail with %s:%s' % ( entity, e.__class__.__name__, str(e.message) if hasattr( e, 'message') else 'unknown err msg') details.append( dict(detail_id=entity, resultcode=resultcode, result=result)) self.manager.change_performance() return resultutils.AgentRpcResult(agent_id=self.manager.agent_id, ctxt=ctxt, resultcode=resultcode, result=result, details=details)