def test_index_poll_many(self): for _ in range(10): create_random_cg(self.parent_cg_path) cg_path = create_random_cg(self.parent_cg_path) job_q = queue.Queue() activity_q = queue.Queue() index = CgroupIndex(self.parent_cg_path, job_q, activity_q) index.open() self.assertHasNoMessages(activity_q) index.sync() for _ in range(11): self.assertHasMessageForCg(activity_q, NewCgroupMessage, self.ANY_CG) self.assertHasNoMessages(activity_q) set_memlimit(cg_path) index.sync() self.assertHasNoMessages(job_q) trigger_oom(cg_path) index.poll(10) # We might receive a few pressure notifications before we finally get # the OOM event. So, wait for the message 100 times. for _ in self.assertEvnetuallyHasMessageForCg( job_q, RestartRequestedMessage, cg_path ): index.poll(1) index.close()
def run_loop(root_cg_path, activity_path, sync_target_interval, restart_adapter, restart_grace_period): threading.current_thread().name = "index" job_queue = queue.Queue() activity_queue = queue.Queue() index = CgroupIndex(root_cg_path, job_queue, activity_queue) index.open() restarter = RestartEngine(restart_adapter, restart_grace_period, job_queue, activity_queue) restarter_thread = threading.Thread(target=restarter.run, name="restarter") restarter_thread.daemon = True activity = ActivityEngine(activity_path, activity_queue) activity_thread = threading.Thread(target=activity.run, name="activity") activity_thread.daemon = True # Now, fire an initial sync, then empty the activity queue (we don't want # to fire notifications for "new" containers if Captain Comeback is the one # that's starting), and start all worker threads. index.sync() while True: try: activity_queue.get_nowait() except queue.Empty: break restarter_thread.start() activity_thread.start() while True: index.sync() next_sync = time.time() + sync_target_interval while True: poll_timeout = next_sync - time.time() if poll_timeout <= 0: break logger.debug("poll with timeout: %s", poll_timeout) try: index.poll(poll_timeout) except IOError as e: if e.errno != errno.EINTR: raise logger.warning("interrupted") for thread in [activity_thread, restarter_thread]: if not thread.is_alive(): logger.critical("thread %s is dead", thread.name) return 1 return 0
def test_index_poll(self): cg_path = create_random_cg(self.parent_cg_path) q = queue.Queue() index = CgroupIndex(self.parent_cg_path, q) index.open() index.sync() self.assertRaises(queue.Empty, q.get_nowait) enable_memlimit_and_trigger_oom(cg_path) index.poll(10) msg = q.get_nowait() self.assertIsInstance(msg, RestartRequestedMessage) self.assertEqual(cg_path, msg.cg.path) index.close()
def test_index_poll(self): cg_path = create_random_cg(self.parent_cg_path) job_q = queue.Queue() activity_q = queue.Queue() index = CgroupIndex(self.parent_cg_path, job_q, activity_q) index.open() self.assertHasNoMessages(activity_q) index.sync() enable_memlimit_and_trigger_oom(cg_path) self.assertHasNoMessages(job_q) index.poll(10) self.assertHasMessageForCg(job_q, RestartRequestedMessage, cg_path) self.assertHasMessageForCg(activity_q, NewCgroupMessage, cg_path) index.close()
def main(root_cg_path, sync_target_interval, restart_grace_period): threading.current_thread().name = "index" job_queue = queue.Queue() index = CgroupIndex(root_cg_path, job_queue) index.open() restarter = RestartEngine(job_queue, restart_grace_period) restarter_thread = threading.Thread(target=restarter.run, name="restarter") restarter_thread.daemon = True restarter_thread.start() while True: index.sync() next_sync = time.time() + sync_target_interval while True: poll_timeout = next_sync - time.time() if poll_timeout <= 0: break logger.debug("poll with timeout: %s", poll_timeout) index.poll(poll_timeout)