def configure_logging(level): from mysql.fabric.handler import MySQLHandler handler = None mysql_handler = None formatter = logging.Formatter( "[%(levelname)s] %(created)f - %(threadName)s - %(message)s" ) mysql_handler = MySQLHandler() if options.log_file: # Configuring handler. handler = FileHandler(options.log_file, 'w') handler.setFormatter(formatter) elif options.log_level: # If a log-level is given, but no log-file, the assumption is # that the user want to see the output, so then we output the # log to standard output. handler = StreamHandler(sys.stdout) handler.setFormatter(formatter) else: # If neither log file nor log level is given, we assume that # the user just want a test report. handler = NullHandler() # Logging levels. logging_levels = { "CRITICAL" : logging.CRITICAL, "ERROR" : logging.ERROR, "WARNING" : logging.WARNING, "INFO" : logging.INFO, "DEBUG" : logging.DEBUG } # Setting logging for "mysql.fabric". logger = logging.getLogger("mysql.fabric") logger.addHandler(handler) logger.addHandler(mysql_handler) logger.setLevel(logging_levels["DEBUG"]) # Setting logging for "tests". logger = logging.getLogger("tests") logger.addHandler(handler) logger.addHandler(mysql_handler) logger.setLevel(logging_levels["DEBUG"]) # Setting debugging level. mysql_handler.setLevel(logging_levels["DEBUG"]) try: handler.setLevel(logging_levels[level]) except KeyError: handler.setLevel(logging_levels["DEBUG"])
def test_node_view(self): """Test basic properties/methods in the MySQLHandler. """ # Retrieve information on Fabric node. Note though # that there is no specific view to retrieve such # information. node_id_1 = _uuid.uuid4() node_startup_1 = _utils.get_time() _LOGGER.debug("Fabric Node started.", extra={ 'subject' : str(node_id_1), 'category' : MySQLHandler.NODE, 'type' : MySQLHandler.START, 'reported' : node_startup_1, } ) node_stop_1 = _utils.get_time() _LOGGER.debug("Fabric Node started.", extra={ 'subject' : str(node_id_1), 'category' : MySQLHandler.NODE, 'type' : MySQLHandler.STOP, 'reported' : node_stop_1, } ) node_id_2 = _uuid.uuid4() node_startup_2 = _utils.get_time() _LOGGER.debug("Fabric Node started.", extra={ 'subject' : str(node_id_2), 'category' : MySQLHandler.NODE, 'type' : MySQLHandler.START, 'reported' : node_startup_2, } ) node_view = ("SELECT subject as node_id, " "TIMEDIFF(UTC_TIMESTAMP(), reported) as node_uptime, " "reported as node_startup FROM log WHERE category = %s " "and type = %s ORDER BY node_id, node_startup" ) persister = _persistence.current_persister() res = persister.exec_stmt( node_view, { "params" : ( MySQLHandler.idx_category(MySQLHandler.NODE), MySQLHandler.idx_type(MySQLHandler.START) ) } ) self.assertEqual(len(res), 2)
def execute(self, group_id=None): """Statistics on a Group. It returns how many promotions and demotions were executed within a group. Specifically, a list with the following fields are returned: group_id, number of promotions, number of demotions. :param group_id: Group one wants to retrieve information on. """ rset = ResultSet( names=('group_id', 'call_count', 'call_abort'), types=(str, long, long), ) for row in MySQLHandler.group_view(group_id): rset.append_row(row) return CommandResult(None, results=rset)
def execute(self, procedure_name=None): """Statistics on the Fabric node. It returns information on procedures that match the %procedure_name% pattern. The information is returned is a list in which each member of the list is also a list with the following fields: procedure name, number of successful calls, number of unsuccessful calls. :param procedure_name: Procedure one wants to retrieve information on. """ rset = ResultSet( names=('proc_name', 'call_count', 'call_abort'), types=(str, long, long), ) for row in MySQLHandler.procedure_view(procedure_name): rset.append_row(row) return CommandResult(None, results=rset)
def test_group_view(self): # Try to retrieve non-existent group. res = MySQLHandler.group_view("non-existent") self.assertEqual(len(res), 0) # Retrieve information on a group and check demote and promote. _LOGGER.debug("Master is being promoted.", extra={ 'subject' : 'group_id_1', 'category' : MySQLHandler.GROUP, 'type' : MySQLHandler.PROMOTE, 'reporter' : 'test_handler' } ) _LOGGER.debug("Master is being demoted.", extra={ 'subject' : 'group_id_1', 'category' : MySQLHandler.GROUP, 'type' : MySQLHandler.DEMOTE, 'reporter' : 'test_handler' } ) _LOGGER.debug("Master is being promoted.", extra={ 'subject' : 'group_id_1', 'category' : MySQLHandler.GROUP, 'type' : MySQLHandler.PROMOTE, 'reporter' : 'test_handler' } ) res = MySQLHandler.group_view("group_id_1") self.assertEqual(len(res), 1) self.assertEqual(res[0][1], 2) self.assertEqual(res[0][2], 1) # Retrieve information on groups with similar name patterns. _LOGGER.debug("Master is being promoted.", extra={ 'subject' : 'group_id_2', 'category' : MySQLHandler.GROUP, 'type' : MySQLHandler.PROMOTE, 'reporter' : 'test_handler' } ) res = MySQLHandler.group_view("group_id_2") self.assertEqual(len(res), 1) self.assertEqual(res[0][1], 1) self.assertEqual(res[0][2], 0) res = MySQLHandler.group_view("group_id") self.assertEqual(len(res), 2) # Retrieve information on groups with different name patterns, # i.e. all groups. _LOGGER.debug("Master is being promoted.", extra={ 'subject' : 'abc_group', 'category' : MySQLHandler.GROUP, 'type' : MySQLHandler.PROMOTE, 'reporter' : 'test_handler' } ) res = MySQLHandler.group_view() self.assertEqual(len(res), 3)
def test_procedure_view(self): # Try to retrieve non-existent procedure. res = MySQLHandler.procedure_view("non-existent") self.assertEqual(len(res), 0) # Retrieve procedures and check stop and abort. for i in range(5): _LOGGER.debug("Report Message.", extra={ 'subject' : 'proc_id_1', 'category' : MySQLHandler.PROCEDURE, 'type' : MySQLHandler.START, 'reporter' : 'test_handler' } ) _LOGGER.debug("Report Message.", extra={ 'subject' : 'proc_id_1', 'category' : MySQLHandler.PROCEDURE, 'type' : MySQLHandler.STOP, 'reporter' : 'test_handler' } ) _LOGGER.debug("Report Message.", extra={ 'subject' : 'proc_id_1', 'category' : MySQLHandler.PROCEDURE, 'type' : MySQLHandler.START, 'reporter' : 'test_handler' } ) _LOGGER.debug("Report Message.", extra={ 'subject' : 'proc_id_1', 'category' : MySQLHandler.PROCEDURE, 'type' : MySQLHandler.ABORT, 'reporter' : 'test_handler' } ) res = MySQLHandler.procedure_view("proc_id_1") self.assertEqual(res[0][0], "proc_id_1") self.assertEqual(res[0][1], 6) self.assertEqual(res[0][2], 1) # Retrieve procedures with similar name patterns. _LOGGER.debug("Report Message.", extra={ 'subject' : 'proc_id', 'category' : MySQLHandler.PROCEDURE, 'type' : MySQLHandler.START, 'reporter' : 'test_handler' } ) _LOGGER.debug("Report Message.", extra={ 'subject' : 'proc_id', 'category' : MySQLHandler.PROCEDURE, 'type' : MySQLHandler.STOP, 'reporter' : 'test_handler' } ) res = MySQLHandler.procedure_view("proc_id") self.assertEqual(len(res), 2) # Retrieve procedures with different name patterns, i.e. all procedures. _LOGGER.debug("Report Message.", extra={ 'subject' : 'other', 'category' : MySQLHandler.PROCEDURE, 'type' : MySQLHandler.START, 'reporter' : 'test_handler' } ) _LOGGER.debug("Report Message", extra={ 'subject' : 'other', 'category' : MySQLHandler.PROCEDURE, 'type' : MySQLHandler.STOP, 'reporter' : 'test_handler' } ) res = MySQLHandler.procedure_view() self.assertTrue(len(res) >= 3)