예제 #1
0
class JobLogger(ConfigServiceLogger):
    def __init__(self,
                 args=None,
                 http_server_port=None,
                 sandesh_instance_id=None,
                 sandesh_instance=None):
        """
        Initialize Job Logger.

        :param args: Config params passed to job manager
        :param http_server_port: Required for Sandesh logger initialization
        :param sandesh_instance_id: Uniquely identifies the logger instance
        :param sandesh_instance: Optional sandesh instance
        """
        self.sandesh_instance_id = sandesh_instance_id
        self._sandesh = sandesh_instance
        module = Module.FABRIC_ANSIBLE
        module_pkg = "job_manager"
        self.context = "job_manager"
        super(JobLogger, self).__init__(module, module_pkg, args,
                                        http_server_port)

    def sandesh_init(self, http_server_port=None):
        if self._sandesh is not None:
            return

        self._sandesh = Sandesh()
        self.redefine_sandesh_handles()
        if not http_server_port:
            http_server_port = self._args.http_server_port

        self._instance_id = self.sandesh_instance_id

        self._sandesh.init_generator(
            self._module_name,
            self._hostname,
            self._node_type_name,
            self.sandesh_instance_id,
            self._args.random_collectors,
            '%s_context' % self.context,
            int(http_server_port), ['cfgm_common'],
            logger_class=self._args.logger_class,
            logger_config_file=self._args.logging_conf,
            config=self._args.sandesh_config)

        self._sandesh.set_logging_params(
            enable_local_log=self._args.log_local,
            category=self._args.log_category,
            level=self._args.log_level,
            file=self._args.log_file,
            enable_syslog=self._args.use_syslog,
            syslog_facility=self._args.syslog_facility)

        VncGreenlet.register_sandesh_handler()

        self._sandesh.trace_buffer_create(name="MessageBusNotifyTraceBuf",
                                          size=1000)
예제 #2
0
class JobLogger(ConfigServiceLogger):
    def __init__(self,
                 args=None,
                 http_server_port=None,
                 sandesh_instance_id=None):
        self.sandesh_instance_id = sandesh_instance_id
        module = Module.FABRIC_ANSIBLE
        module_pkg = "job_manager"
        self.context = "job_manager"
        super(JobLogger, self).__init__(module, module_pkg, args,
                                        http_server_port)

    def sandesh_init(self, http_server_port=None):
        """ Init sandesh """
        self._sandesh = Sandesh()
        self.redefine_sandesh_handles()
        if not http_server_port:
            http_server_port = self._args.http_server_port

        self._instance_id = self.sandesh_instance_id

        self._sandesh.init_generator(
            self._module_name,
            self._hostname,
            self._node_type_name,
            self.sandesh_instance_id,
            self._args.random_collectors,
            '%s_context' % self.context,
            int(http_server_port),
            ['cfgm_common', '%s.sandesh' % self.module_pkg],
            logger_class=self._args.logger_class,
            logger_config_file=self._args.logging_conf,
            config=self._args.sandesh_config)

        self._sandesh.set_logging_params(
            enable_local_log=self._args.log_local,
            category=self._args.log_category,
            level=self._args.log_level,
            file=self._args.log_file,
            enable_syslog=self._args.use_syslog,
            syslog_facility=self._args.syslog_facility)

        VncGreenlet.register_sandesh_handler()

        self._sandesh.trace_buffer_create(name="MessageBusNotifyTraceBuf",
                                          size=1000)
예제 #3
0
class JobLogger(ConfigServiceLogger):

    def __init__(self, args=None, http_server_port=None,
                 sandesh_instance_id=None, sandesh_instance=None):
        self.sandesh_instance_id = sandesh_instance_id
        self._sandesh = sandesh_instance
        module = Module.FABRIC_ANSIBLE
        module_pkg = "job_manager"
        self.context = "job_manager"
        super(JobLogger, self).__init__(
            module, module_pkg, args, http_server_port)

    def sandesh_init(self, http_server_port=None):
        """ Init sandesh """
        if self._sandesh is not None:
            return

        self._sandesh = Sandesh()
        self.redefine_sandesh_handles()
        if not http_server_port:
            http_server_port = self._args.http_server_port

        self._instance_id = self.sandesh_instance_id

        self._sandesh.init_generator(
            self._module_name, self._hostname, self._node_type_name,
            self.sandesh_instance_id, self._args.random_collectors,
            '%s_context' % self.context, int(http_server_port),
            ['cfgm_common'],
            logger_class=self._args.logger_class,
            logger_config_file=self._args.logging_conf,
            config=self._args.sandesh_config)

        self._sandesh.set_logging_params(
            enable_local_log=self._args.log_local,
            category=self._args.log_category,
            level=self._args.log_level,
            file=self._args.log_file,
            enable_syslog=self._args.use_syslog,
            syslog_facility=self._args.syslog_facility)

        VncGreenlet.register_sandesh_handler()

        self._sandesh.trace_buffer_create(name="MessageBusNotifyTraceBuf",
                                          size=1000)
예제 #4
0
class SandeshTraceTest(unittest.TestCase):
    def setUp(self):
        self._sandesh = Sandesh()
        http_port = get_free_port()
        self._sandesh.init_generator('sandesh_trace_test',
                                     socket.gethostname(), 'Test', 'Test',
                                     None, 'trace_test_ctxt', http_port)
        self._sandesh.set_logging_params(level=SandeshLevel.SYS_DEBUG,
                                         enable_local_log=True,
                                         enable_trace_print=True)
        self._trace_read_list = []

    # end setUp

    def sandesh_trace_read_handler(self, trace_msg, more):
        self._trace_read_list.append(trace_msg)

    # end sandesh_trace_read_handler

    def test_create_delete_trace_buffer(self):
        trace_buf_name = 'test_create_delete_trace_buffer'
        trace_buf_size = 5
        self._sandesh.trace_buffer_create(trace_buf_name, trace_buf_size)
        self.assertTrue(
            trace_buf_name in self._sandesh.trace_buffer_list_get())
        self.assertEqual(trace_buf_size,
                         self._sandesh.trace_buffer_size_get(trace_buf_name))

        # Read from empty trace buffer
        self._sandesh.trace_buffer_read(
            name=trace_buf_name,
            read_context='test',
            count=0,
            read_cb=self.sandesh_trace_read_handler)
        exp_trace_list = []
        self.assertEqual(exp_trace_list, self._trace_read_list)
        self._sandesh.trace_buffer_delete(trace_buf_name)
        self.assertFalse(
            trace_buf_name in self._sandesh.trace_buffer_list_get())

        # Read deleted trace buffer
        self._sandesh.trace_buffer_read(
            name=trace_buf_name,
            read_context='test',
            count=0,
            read_cb=self.sandesh_trace_read_handler)
        exp_trace_list = []
        self.assertEqual(exp_trace_list, self._trace_read_list)

    # end test_create_delete_trace_buffer

    def test_enable_disable_trace_buffer(self):
        trace_buf_name = 'test_enable_disable_trace_buffer'
        trace_buf_size = 5
        # Create trace buffer in disabled state
        self._sandesh.trace_buffer_create(trace_buf_name, trace_buf_size,
                                          False)
        tmsg1 = TraceTest(magicNo=1234, sandesh=self._sandesh)
        tmsg1.trace_msg(name=trace_buf_name, sandesh=self._sandesh)
        self._sandesh.trace_buffer_read(
            name=trace_buf_name,
            read_context='read1',
            count=0,
            read_cb=self.sandesh_trace_read_handler)
        # Trace buffer should be empty
        exp_trace_list = []
        self.assertEqual(exp_trace_list, self._trace_read_list)

        # Enable trace buffer
        self._sandesh.trace_buffer_enable(trace_buf_name)
        tmsg1.trace_msg(name=trace_buf_name, sandesh=self._sandesh)
        tmsg2 = TraceTest(magicNo=3456, sandesh=self._sandesh)
        tmsg2.trace_msg(name=trace_buf_name, sandesh=self._sandesh)
        self._sandesh.trace_buffer_read(
            name=trace_buf_name,
            read_context='read2',
            count=0,
            read_cb=self.sandesh_trace_read_handler)
        exp_trace_list = [tmsg1, tmsg2]
        self.assertEqual(exp_trace_list, self._trace_read_list)
        self._trace_read_list = []

        # Disable trace buffer
        self._sandesh.trace_buffer_disable(trace_buf_name)
        tmsg3 = TraceTest(magicNo=7890, sandesh=self._sandesh)
        tmsg3.trace_msg(name=trace_buf_name, sandesh=self._sandesh)
        self._sandesh.trace_buffer_read(
            name=trace_buf_name,
            read_context='read3',
            count=0,
            read_cb=self.sandesh_trace_read_handler)
        exp_trace_list = [tmsg1, tmsg2]
        self.assertEqual(exp_trace_list, self._trace_read_list)

    # end test_enable_disable_trace_buffer

    def test_enable_disable_trace(self):
        trace_buf_name = 'test_enable_disable_trace'
        trace_buf_size = 3
        # Disable trace
        self._sandesh.trace_disable()
        self._sandesh.trace_buffer_create(trace_buf_name, trace_buf_size)
        tmsg1 = TraceTest(magicNo=1234, sandesh=self._sandesh)
        tmsg1.trace_msg(name=trace_buf_name, sandesh=self._sandesh)
        self._sandesh.trace_buffer_read(
            name=trace_buf_name,
            read_context='read1',
            count=0,
            read_cb=self.sandesh_trace_read_handler)
        # Trace buffer should be empty
        exp_trace_list = []
        self.assertEqual(exp_trace_list, self._trace_read_list)

        # Enable trace
        self._sandesh.trace_enable()
        tmsg1.trace_msg(name=trace_buf_name, sandesh=self._sandesh)
        self._sandesh.trace_buffer_read(
            name=trace_buf_name,
            read_context='read2',
            count=0,
            read_cb=self.sandesh_trace_read_handler)
        exp_trace_list = [tmsg1]
        self.assertEqual(exp_trace_list, self._trace_read_list)

    # end test_enable_disable_trace

    def test_read_count_trace_buffer(self):
        trace_buf_name = 'test_read_count_trace_buffer'
        trace_buf_size = 10
        self._sandesh.trace_buffer_create(trace_buf_name, trace_buf_size)
        tmsg1 = TraceTest(magicNo=1, sandesh=self._sandesh)
        tmsg1.trace_msg(name=trace_buf_name, sandesh=self._sandesh)
        tmsg2 = TraceTest(magicNo=2, sandesh=self._sandesh)
        tmsg2.trace_msg(name=trace_buf_name, sandesh=self._sandesh)
        tmsg3 = TraceTest(magicNo=3, sandesh=self._sandesh)
        tmsg3.trace_msg(name=trace_buf_name, sandesh=self._sandesh)

        # Total messages in trace buffer = 3, count = 1
        self._sandesh.trace_buffer_read(
            name=trace_buf_name,
            read_context='read1',
            count=1,
            read_cb=self.sandesh_trace_read_handler)
        exp_trace_list = [tmsg1]
        self.assertEqual(exp_trace_list, self._trace_read_list)
        self._trace_read_list = []

        # count = 0, should read the last two messages
        self._sandesh.trace_buffer_read(
            name=trace_buf_name,
            read_context='read1',
            count=0,
            read_cb=self.sandesh_trace_read_handler)
        exp_trace_list = [tmsg2, tmsg3]
        self.assertEqual(exp_trace_list, self._trace_read_list)
        self._trace_read_list = []

        # Total messages in trace buffer = 3, count = 5 (< trace_buf_size)
        self._sandesh.trace_buffer_read(
            name=trace_buf_name,
            read_context='read2',
            count=5,
            read_cb=self.sandesh_trace_read_handler)
        exp_trace_list = [tmsg1, tmsg2, tmsg3]
        self.assertEqual(exp_trace_list, self._trace_read_list)
        self._trace_read_list = []

        # Total messages in trace buffer = 3, count = 20 (> trace_buf_size)
        self._sandesh.trace_buffer_read(
            name=trace_buf_name,
            read_context='read3',
            count=20,
            read_cb=self.sandesh_trace_read_handler)
        exp_trace_list = [tmsg1, tmsg2, tmsg3]
        self.assertEqual(exp_trace_list, self._trace_read_list)

    # end test_read_count_trace_buffer

    def test_overwrite_trace_buffer(self):
        trace_buf_name = 'test_overwrite_trace_buffer'
        trace_buf_size = 3
        self._sandesh.trace_buffer_create(trace_buf_name, trace_buf_size)
        tmsg1 = TraceTest(magicNo=123, sandesh=self._sandesh)
        tmsg1.trace_msg(name=trace_buf_name, sandesh=self._sandesh)
        tmsg2 = TraceTest(magicNo=345, sandesh=self._sandesh)
        tmsg2.trace_msg(name=trace_buf_name, sandesh=self._sandesh)
        tmsg3 = TraceTest(magicNo=567, sandesh=self._sandesh)
        tmsg3.trace_msg(name=trace_buf_name, sandesh=self._sandesh)
        self._sandesh.trace_buffer_read(
            name=trace_buf_name,
            read_context='read1',
            count=0,
            read_cb=self.sandesh_trace_read_handler)
        exp_trace_list = [tmsg1, tmsg2, tmsg3]
        self.assertEqual(exp_trace_list, self._trace_read_list)
        self._trace_read_list = []
        # Overwrite trace buffer
        tmsg4 = TraceTest(magicNo=789, sandesh=self._sandesh)
        tmsg4.trace_msg(name=trace_buf_name, sandesh=self._sandesh)
        self._sandesh.trace_buffer_read(
            name=trace_buf_name,
            read_context='read2',
            count=0,
            read_cb=self.sandesh_trace_read_handler)
        exp_trace_list = [tmsg2, tmsg3, tmsg4]
        self.assertEqual(exp_trace_list, self._trace_read_list)

    # end test_overwrite_trace_buffer

    def test_trace_buffer_read_context(self):
        trace_buf_name = 'test_trace_buffer_read_context'
        trace_buf_size = 3
        self._sandesh.trace_buffer_create(trace_buf_name, trace_buf_size)
        tmsg1 = TraceTest(magicNo=123, sandesh=self._sandesh)
        tmsg1.trace_msg(name=trace_buf_name, sandesh=self._sandesh)
        tmsg2 = TraceTest(magicNo=345, sandesh=self._sandesh)
        tmsg2.trace_msg(name=trace_buf_name, sandesh=self._sandesh)
        self._sandesh.trace_buffer_read(
            name=trace_buf_name,
            read_context='read1',
            count=0,
            read_cb=self.sandesh_trace_read_handler)
        exp_trace_list = [tmsg1, tmsg2]
        self.assertEqual(exp_trace_list, self._trace_read_list)
        self._trace_read_list = []

        # After reading the entire content of trace buffer,
        # dont delete the read context. Subsequent call to trace_buffer_read()
        # should not read any trace message
        self._sandesh.trace_buffer_read(
            name=trace_buf_name,
            read_context='read1',
            count=0,
            read_cb=self.sandesh_trace_read_handler)
        exp_trace_list = []
        self.assertEqual(exp_trace_list, self._trace_read_list)

        # We have not deleted the read context. Add more trace messages
        # and make sure we don't read the already read trace messages.
        tmsg3 = TraceTest(magicNo=56, sandesh=self._sandesh)
        tmsg3.trace_msg(name=trace_buf_name, sandesh=self._sandesh)
        tmsg4 = TraceTest(magicNo=67, sandesh=self._sandesh)
        tmsg4.trace_msg(name=trace_buf_name, sandesh=self._sandesh)
        self._sandesh.trace_buffer_read(
            name=trace_buf_name,
            read_context='read1',
            count=1,
            read_cb=self.sandesh_trace_read_handler)
        exp_trace_list = [tmsg3]
        self.assertEqual(exp_trace_list, self._trace_read_list)
        self._trace_read_list = []
        # Now read the last message
        self._sandesh.trace_buffer_read(
            name=trace_buf_name,
            read_context='read1',
            count=0,
            read_cb=self.sandesh_trace_read_handler)
        exp_trace_list = [tmsg4]
        self.assertEqual(exp_trace_list, self._trace_read_list)
        self._trace_read_list = []

        # Read the trace buffer with different read_context
        self._sandesh.trace_buffer_read(
            name=trace_buf_name,
            read_context='read2',
            count=2,
            read_cb=self.sandesh_trace_read_handler)
        exp_trace_list = [tmsg2, tmsg3]
        self.assertEqual(exp_trace_list, self._trace_read_list)
        self._trace_read_list = []

        # Delete read context
        self._sandesh.trace_buffer_read_done(name=trace_buf_name,
                                             context='read2')
        self._sandesh.trace_buffer_read(
            name=trace_buf_name,
            read_context='read2',
            count=1,
            read_cb=self.sandesh_trace_read_handler)
        exp_trace_list = [tmsg2]
        self.assertEqual(exp_trace_list, self._trace_read_list)
        self._trace_read_list = []

        # Interleave reading and writing of trace buffer - invalidate read
        # context
        tmsg5 = TraceTest(magicNo=78, sandesh=self._sandesh)
        tmsg5.trace_msg(name=trace_buf_name, sandesh=self._sandesh)
        tmsg6 = TraceTest(magicNo=89, sandesh=self._sandesh)
        tmsg6.trace_msg(name=trace_buf_name, sandesh=self._sandesh)
        self._sandesh.trace_buffer_read(
            name=trace_buf_name,
            read_context='read2',
            count=2,
            read_cb=self.sandesh_trace_read_handler)
        exp_trace_list = [tmsg4, tmsg5]
        self.assertEqual(exp_trace_list, self._trace_read_list)
        self._trace_read_list = []
        tmsg7 = TraceTest(magicNo=98, sandesh=self._sandesh)
        tmsg7.trace_msg(name=trace_buf_name, sandesh=self._sandesh)
        self._sandesh.trace_buffer_read(
            name=trace_buf_name,
            read_context='read1',
            count=0,
            read_cb=self.sandesh_trace_read_handler)
        exp_trace_list = [tmsg5, tmsg6, tmsg7]
        self.assertEqual(exp_trace_list, self._trace_read_list)