From 23a99acc066aa18781521636991d6e5346fa8476 Mon Sep 17 00:00:00 2001 From: Michael Still Date: Tue, 9 Apr 2013 13:42:47 +1000 Subject: [PATCH] Update to the latest oslo loopingcall. This renames a class from loopingcall.LoopingCall to loopingcall.FixedIntervalLoopingCall. Change-Id: If51d3f4cc2a393f730cd168b16d444725151dbf4 --- quantum/agent/dhcp_agent.py | 3 +- quantum/agent/l3_agent.py | 3 +- quantum/openstack/common/loopingcall.py | 64 +++++++++++++++++-- quantum/openstack/common/threadgroup.py | 2 +- .../agent/linuxbridge_quantum_agent.py | 3 +- .../plugins/nec/agent/nec_quantum_agent.py | 3 +- .../openvswitch/agent/ovs_quantum_agent.py | 3 +- quantum/service.py | 5 +- 8 files changed, 72 insertions(+), 14 deletions(-) diff --git a/quantum/agent/dhcp_agent.py b/quantum/agent/dhcp_agent.py index 42c9f0a04..e29e963af 100644 --- a/quantum/agent/dhcp_agent.py +++ b/quantum/agent/dhcp_agent.py @@ -686,7 +686,8 @@ class DhcpAgentWithStateReport(DhcpAgent): 'agent_type': constants.AGENT_TYPE_DHCP} report_interval = cfg.CONF.AGENT.report_interval if report_interval: - self.heartbeat = loopingcall.LoopingCall(self._report_state) + self.heartbeat = loopingcall.FixedIntervalLoopingCall( + self._report_state) self.heartbeat.start(interval=report_interval) def _report_state(self): diff --git a/quantum/agent/l3_agent.py b/quantum/agent/l3_agent.py index 39ea511fc..549fb6c14 100644 --- a/quantum/agent/l3_agent.py +++ b/quantum/agent/l3_agent.py @@ -697,7 +697,8 @@ class L3NATAgentWithStateReport(L3NATAgent): 'agent_type': l3_constants.AGENT_TYPE_L3} report_interval = cfg.CONF.AGENT.report_interval if report_interval: - self.heartbeat = loopingcall.LoopingCall(self._report_state) + self.heartbeat = loopingcall.FixedIntervalLoopingCall( + self._report_state) self.heartbeat.start(interval=report_interval) def _report_state(self): diff --git a/quantum/openstack/common/loopingcall.py b/quantum/openstack/common/loopingcall.py index efce59528..68b3ab772 100644 --- a/quantum/openstack/common/loopingcall.py +++ b/quantum/openstack/common/loopingcall.py @@ -46,12 +46,23 @@ class LoopingCallDone(Exception): self.retvalue = retvalue -class LoopingCall(object): +class LoopingCallBase(object): def __init__(self, f=None, *args, **kw): self.args = args self.kw = kw self.f = f self._running = False + self.done = None + + def stop(self): + self._running = False + + def wait(self): + return self.done.wait() + + +class FixedIntervalLoopingCall(LoopingCallBase): + """A fixed interval looping call.""" def start(self, interval, initial_delay=None): self._running = True @@ -77,7 +88,7 @@ class LoopingCall(object): self.stop() done.send(e.retvalue) except Exception: - LOG.exception(_('in looping call')) + LOG.exception(_('in fixed duration looping call')) done.send_exception(*sys.exc_info()) return else: @@ -88,8 +99,49 @@ class LoopingCall(object): greenthread.spawn_n(_inner) return self.done - def stop(self): - self._running = False - def wait(self): - return self.done.wait() +# TODO(mikal): this class name is deprecated in Havana and should be removed +# in the I release +LoopingCall = FixedIntervalLoopingCall + + +class DynamicLoopingCall(LoopingCallBase): + """A looping call which sleeps until the next known event. + + The function called should return how long to sleep for before being + called again. + """ + + def start(self, initial_delay=None, periodic_interval_max=None): + self._running = True + done = event.Event() + + def _inner(): + if initial_delay: + greenthread.sleep(initial_delay) + + try: + while self._running: + idle = self.f(*self.args, **self.kw) + if not self._running: + break + + if periodic_interval_max is not None: + idle = min(idle, periodic_interval_max) + LOG.debug(_('Dynamic looping call sleeping for %.02f ' + 'seconds'), idle) + greenthread.sleep(idle) + except LoopingCallDone, e: + self.stop() + done.send(e.retvalue) + except Exception: + LOG.exception(_('in dynamic looping call')) + done.send_exception(*sys.exc_info()) + return + else: + done.send(True) + + self.done = done + + greenthread.spawn(_inner) + return self.done diff --git a/quantum/openstack/common/threadgroup.py b/quantum/openstack/common/threadgroup.py index ecabe0d05..4442cf7cf 100644 --- a/quantum/openstack/common/threadgroup.py +++ b/quantum/openstack/common/threadgroup.py @@ -63,7 +63,7 @@ class ThreadGroup(object): def add_timer(self, interval, callback, initial_delay=None, *args, **kwargs): - pulse = loopingcall.LoopingCall(callback, *args, **kwargs) + pulse = loopingcall.FixedIntervalLoopingCall(callback, *args, **kwargs) pulse.start(interval=interval, initial_delay=initial_delay) self.timers.append(pulse) diff --git a/quantum/plugins/linuxbridge/agent/linuxbridge_quantum_agent.py b/quantum/plugins/linuxbridge/agent/linuxbridge_quantum_agent.py index c69aa55b6..269101df3 100755 --- a/quantum/plugins/linuxbridge/agent/linuxbridge_quantum_agent.py +++ b/quantum/plugins/linuxbridge/agent/linuxbridge_quantum_agent.py @@ -517,7 +517,8 @@ class LinuxBridgeQuantumAgentRPC(sg_rpc.SecurityGroupAgentRpcMixin): consumers) report_interval = cfg.CONF.AGENT.report_interval if report_interval: - heartbeat = loopingcall.LoopingCall(self._report_state) + heartbeat = loopingcall.FixedIntervalLoopingCall( + self._report_state) heartbeat.start(interval=report_interval) def setup_linux_bridge(self, interface_mappings): diff --git a/quantum/plugins/nec/agent/nec_quantum_agent.py b/quantum/plugins/nec/agent/nec_quantum_agent.py index 8e121a91e..82b76d7a2 100755 --- a/quantum/plugins/nec/agent/nec_quantum_agent.py +++ b/quantum/plugins/nec/agent/nec_quantum_agent.py @@ -169,7 +169,8 @@ class NECQuantumAgent(object): report_interval = config.CONF.AGENT.report_interval if report_interval: - heartbeat = loopingcall.LoopingCall(self._report_state) + heartbeat = loopingcall.FixedIntervalLoopingCall( + self._report_state) heartbeat.start(interval=report_interval) def _report_state(self): diff --git a/quantum/plugins/openvswitch/agent/ovs_quantum_agent.py b/quantum/plugins/openvswitch/agent/ovs_quantum_agent.py index a49cda34f..8efce182f 100644 --- a/quantum/plugins/openvswitch/agent/ovs_quantum_agent.py +++ b/quantum/plugins/openvswitch/agent/ovs_quantum_agent.py @@ -225,7 +225,8 @@ class OVSQuantumAgent(sg_rpc.SecurityGroupAgentRpcCallbackMixin): consumers) report_interval = cfg.CONF.AGENT.report_interval if report_interval: - heartbeat = loopingcall.LoopingCall(self._report_state) + heartbeat = loopingcall.FixedIntervalLoopingCall( + self._report_state) heartbeat.start(interval=report_interval) def get_net_uuid(self, vif_id): diff --git a/quantum/service.py b/quantum/service.py index 33529288a..2856020a0 100644 --- a/quantum/service.py +++ b/quantum/service.py @@ -141,7 +141,7 @@ class Service(service.Service): self.manager.init_host() super(Service, self).start() if self.report_interval: - pulse = loopingcall.LoopingCall(self.report_state) + pulse = loopingcall.FixedIntervalLoopingCall(self.report_state) pulse.start(interval=self.report_interval, initial_delay=self.report_interval) self.timers.append(pulse) @@ -152,7 +152,8 @@ class Service(service.Service): else: initial_delay = None - periodic = loopingcall.LoopingCall(self.periodic_tasks) + periodic = loopingcall.FixedIntervalLoopingCall( + self.periodic_tasks) periodic.start(interval=self.periodic_interval, initial_delay=initial_delay) self.timers.append(periodic) -- 2.45.2