Ejemplo n.º 1
0
    def __init__(self, server_description, topology, pool, topology_settings):
        """Class to monitor a MongoDB server on a background thread.

        Pass an initial ServerDescription, a Topology, a Pool, and
        TopologySettings.

        The Topology is weakly referenced. The Pool must be exclusive to this
        Monitor.
        """
        self._server_description = server_description
        self._pool = pool
        self._settings = topology_settings
        self._avg_round_trip_time = MovingAverage()

        # We strongly reference the executor and it weakly references us via
        # this closure. When the monitor is freed, stop the executor soon.
        def target():
            monitor = self_ref()
            if monitor is None:
                return False  # Stop the executor.
            Monitor._run(monitor)
            return True

        executor = periodic_executor.PeriodicExecutor(
            condition_class=self._settings.condition_class,
            interval=common.HEARTBEAT_FREQUENCY,
            min_interval=common.MIN_HEARTBEAT_INTERVAL,
            target=target,
            name="pymongo_server_monitor_thread")

        self._executor = executor

        # Avoid cycles. When self or topology is freed, stop executor soon.
        self_ref = weakref.ref(self, executor.close)
        self._topology = weakref.proxy(topology, executor.close)
                def __init__(self, server_description, topology, pool,
                             topology_settings):
                    """Have to copy entire constructor from Monitor so that we
                    can override _run and change the periodic executor's
                     interval."""

                    self._server_description = server_description
                    self._pool = pool
                    self._settings = topology_settings
                    self._avg_round_trip_time = MovingAverage()
                    options = self._settings._pool_options
                    self._listeners = options.event_listeners
                    self._publish = self._listeners is not None

                    def target():
                        monitor = self_ref()
                        if monitor is None:
                            return False
                        MockMonitor._run(monitor)  # Change target to subclass
                        return True

                    # Shorten interval
                    executor = periodic_executor.PeriodicExecutor(
                        interval=0.1,
                        min_interval=0.1,
                        target=target,
                        name="pymongo_server_monitor_thread")
                    self._executor = executor
                    self_ref = weakref.ref(self, executor.close)
                    self._topology = weakref.proxy(topology, executor.close)
Ejemplo n.º 3
0
 def test_moving_average(self):
     avg = MovingAverage()
     self.assertIsNone(avg.get())
     avg.add_sample(10)
     self.assertAlmostEqual(10, avg.get())
     avg.add_sample(20)
     self.assertAlmostEqual(12, avg.get())
     avg.add_sample(30)
     self.assertAlmostEqual(15.6, avg.get())
 def test_trivial_moving_average(self):
     avg = MovingAverage(1)
     self.assertEqual(None, avg.get())
     avg.update(10)
     self.assertEqual(10, avg.get())
     avg.update(20)
     self.assertEqual(20, avg.get())
     avg.update(0)
     self.assertEqual(0, avg.get())
 def test_2_sample_moving_average(self):
     avg = MovingAverage(2)
     self.assertEqual(None, avg.get())
     avg.update(10)
     self.assertEqual(10, avg.get())
     avg.update(20)
     self.assertEqual(15, avg.get())
     avg.update(30)
     self.assertEqual(25, avg.get())
     avg.update(-100)
     self.assertEqual(-35, avg.get())
Ejemplo n.º 6
0
    def run_scenario(self):
        moving_average = MovingAverage()

        if scenario_def['avg_rtt_ms'] != "NULL":
            moving_average.add_sample(scenario_def['avg_rtt_ms'])

        if scenario_def['new_rtt_ms'] != "NULL":
            moving_average.add_sample(scenario_def['new_rtt_ms'])

        self.assertAlmostEqual(moving_average.get(),
                               scenario_def['new_avg_rtt'])
Ejemplo n.º 7
0
    def __init__(self, topology, topology_settings, pool):
        """Maintain round trip times for a server.

        The Topology is weakly referenced.
        """
        super(_RttMonitor,
              self).__init__(topology, "pymongo_server_rtt_thread",
                             topology_settings.heartbeat_frequency,
                             common.MIN_HEARTBEAT_INTERVAL)

        self._pool = pool
        self._moving_average = MovingAverage()
        self._lock = threading.Lock()
 def test_moving_average(self):
     avg = MovingAverage([10])
     self.assertEqual(10, avg.get())
     avg2 = avg.clone_with(20)
     self.assertEqual(15, avg2.get())
     avg3 = avg2.clone_with(30)
     self.assertEqual(20, avg3.get())
     avg4 = avg3.clone_with(-100)
     self.assertEqual((10 + 20 + 30 - 100) / 4., avg4.get())
     avg5 = avg4.clone_with(17)
     self.assertEqual((10 + 20 + 30 - 100 + 17) / 5., avg5.get())
     avg6 = avg5.clone_with(43)
     self.assertEqual((20 + 30 - 100 + 17 + 43) / 5., avg6.get())
     avg7 = avg6.clone_with(-1111)
     self.assertEqual((30 - 100 + 17 + 43 - 1111) / 5., avg7.get())
 def test_5_sample_moving_average(self):
     avg = MovingAverage(5)
     self.assertEqual(None, avg.get())
     avg.update(10)
     self.assertEqual(10, avg.get())
     avg.update(20)
     self.assertEqual(15, avg.get())
     avg.update(30)
     self.assertEqual(20, avg.get())
     avg.update(-100)
     self.assertEqual((10 + 20 + 30 - 100) / 4, avg.get())
     avg.update(17)
     self.assertEqual((10 + 20 + 30 - 100 + 17) / 5., avg.get())
     avg.update(43)
     self.assertEqual((20 + 30 - 100 + 17 + 43) / 5., avg.get())
     avg.update(-1111)
     self.assertEqual((30 - 100 + 17 + 43 - 1111) / 5., avg.get())
Ejemplo n.º 10
0
    def __init__(self, server_description, topology, pool, topology_settings):
        """Class to monitor a MongoDB server on a background thread.

        Pass an initial ServerDescription, a Topology, a Pool, and
        TopologySettings.

        The Topology is weakly referenced. The Pool must be exclusive to this
        Monitor.
        """
        super(Monitor,
              self).__init__(topology, "pymongo_server_monitor_thread",
                             topology_settings.heartbeat_frequency,
                             common.MIN_HEARTBEAT_INTERVAL)
        self._server_description = server_description
        self._pool = pool
        self._settings = topology_settings
        self._avg_round_trip_time = MovingAverage()
        self._listeners = self._settings._pool_options.event_listeners
        pub = self._listeners is not None
        self._publish = pub and self._listeners.enabled_for_server_heartbeat
 def test_empty_moving_average(self):
     avg = MovingAverage(0)
     self.assertEqual(None, avg.get())
     avg.update(10)
     self.assertEqual(None, avg.get())