def load(self): """ Instantiates and schedules the Ryu app eventlets in the service eventloop. """ # Some setups might not use REDIS if (self._magma_service.config['redis_enabled']): # Wait for redis as multiple controllers rely on it while not redisAvailable(self.rule_id_mapper.redis_cli): logging.warning("Pipelined waiting for redis...") time.sleep(1) else: self.rule_id_mapper._rule_nums_by_rule = {} self.rule_id_mapper._rules_by_rule_num = {} self.session_rule_version_mapper._version_by_imsi_and_rule = {} self.interface_to_prefix_mapper._prefix_by_interface = {} self.tunnel_id_mapper._tunnel_map = {} manager = AppManager.get_instance() manager.load_apps([app.module for app in self._apps]) contexts = manager.create_contexts() contexts['rule_id_mapper'] = self.rule_id_mapper contexts[ 'session_rule_version_mapper'] = self.session_rule_version_mapper contexts['interface_to_prefix_mapper'] = self.interface_to_prefix_mapper contexts['tunnel_id_mapper'] = self.tunnel_id_mapper contexts['app_futures'] = {app.name: Future() for app in self._apps} contexts['internal_ip_allocator'] = \ InternalIPAllocator(self._magma_service.config) contexts['config'] = self._magma_service.config contexts['mconfig'] = self._magma_service.mconfig contexts['loop'] = self._magma_service.loop contexts['service_manager'] = self sessiond_chan = ServiceRegistry.get_rpc_channel( 'sessiond', ServiceRegistry.LOCAL) mobilityd_chan = ServiceRegistry.get_rpc_channel( 'mobilityd', ServiceRegistry.LOCAL) contexts['rpc_stubs'] = { 'mobilityd': MobilityServiceStub(mobilityd_chan), 'sessiond': LocalSessionManagerStub(sessiond_chan), } # Instantiate and schedule apps for app in manager.instantiate_apps(**contexts): # Wrap the eventlet in asyncio so it will stop when the loop is # stopped future = aioeventlet.wrap_greenthread(app, self._magma_service.loop) # Schedule the eventlet for evaluation in service loop asyncio.ensure_future(future) # In development mode, run server so that if environment.is_dev_mode(): server_thread = of_rest_server.start(manager) future = aioeventlet.wrap_greenthread(server_thread, self._magma_service.loop) asyncio.ensure_future(future)
def test_wrap_greenlet_exc(self): def func(): raise ValueError(7) gl = greenlet.greenlet(func) fut = aioeventlet.wrap_greenthread(gl) gl.switch() self.assertRaises(ValueError, self.loop.run_until_complete, fut)
def func(): try: gt = eventlet.getcurrent() fut = aioeventlet.wrap_greenthread(gt) except Exception as exc: event.send_exception(exc) else: event.send(fut)
def load(self): """ Instantiates and schedules the Ryu app eventlets in the service eventloop. """ manager = AppManager.get_instance() manager.load_apps([app.module for app in self._apps]) contexts = manager.create_contexts() contexts['rule_id_mapper'] = RuleIDToNumMapper() contexts[ 'session_rule_version_mapper'] = self.session_rule_version_mapper contexts['app_futures'] = {app.name: Future() for app in self._apps} contexts['config'] = self._magma_service.config contexts['mconfig'] = self._magma_service.mconfig contexts['loop'] = self._magma_service.loop contexts['service_manager'] = self records_chan = ServiceRegistry.get_rpc_channel('meteringd_records', ServiceRegistry.CLOUD) sessiond_chan = ServiceRegistry.get_rpc_channel( 'sessiond', ServiceRegistry.LOCAL) mobilityd_chan = ServiceRegistry.get_rpc_channel( 'mobilityd', ServiceRegistry.LOCAL) contexts['rpc_stubs'] = { 'metering_cloud': MeteringdRecordsControllerStub(records_chan), 'mobilityd': MobilityServiceStub(mobilityd_chan), 'sessiond': LocalSessionManagerStub(sessiond_chan), } # Instantiate and schedule apps for app in manager.instantiate_apps(**contexts): # Wrap the eventlet in asyncio so it will stop when the loop is # stopped future = aioeventlet.wrap_greenthread(app, self._magma_service.loop) # Schedule the eventlet for evaluation in service loop asyncio.ensure_future(future) # In development mode, run server so that if environment.is_dev_mode(): server_thread = of_rest_server.start(manager) future = aioeventlet.wrap_greenthread(server_thread, self._magma_service.loop) asyncio.ensure_future(future)
def test_wrap_greenlet(self): def func(value): return value * 3 gl = greenlet.greenlet(func) fut = aioeventlet.wrap_greenthread(gl) gl.switch(5) result = self.loop.run_until_complete(fut) self.assertEqual(result, 15)
def test_wrap_greenlet_exc(self): self.loop.set_debug(True) def func(): raise ValueError(7) gt = eventlet.spawn_n(func) fut = aioeventlet.wrap_greenthread(gt) self.assertRaises(ValueError, self.loop.run_until_complete, fut)
def test_wrap_greenlet(self): def func(): eventlet.sleep(0.010) return "ok" gt = eventlet.spawn_n(func) fut = aioeventlet.wrap_greenthread(gt) result = self.loop.run_until_complete(fut) self.assertEqual(result, "ok")
def coro_wrap_greenthread(): result = [] gt = eventlet.spawn(eventlet_slow_append, result, 1, 0.020) value = yield From(aioeventlet.wrap_greenthread(gt)) result.append(value) gt = eventlet.spawn(eventlet_slow_append, result, 2, 0.010) value = yield From(aioeventlet.wrap_greenthread(gt)) result.append(value) gt = eventlet.spawn(eventlet_slow_error) try: yield From(aioeventlet.wrap_greenthread(gt)) except ValueError as exc: result.append(str(exc)) result.append(4) raise Return(result)
def test_wrap_greenthread_exc(self): self.loop.set_debug(True) def func(): raise ValueError(7) # FIXME: the unit test must fail!? with tests.mock.patch('traceback.print_exception') as print_exception: gt = eventlet.spawn(func) fut = aioeventlet.wrap_greenthread(gt) self.assertRaises(ValueError, self.loop.run_until_complete, fut) # the exception must not be logger by traceback: the caller must # consume the exception from the future object self.assertFalse(print_exception.called)
def func(value): gl = greenlet.getcurrent() return aioeventlet.wrap_greenthread(gl)
def func(): return aioeventlet.wrap_greenthread(gt)