Skip to content

Commit

Permalink
Updated service.py and its dependencies
Browse files Browse the repository at this point in the history
This is to avoid service module dependency on importutils that are now
moved to oslo.utils.

The following changes are included:

 * neutron/openstack/common/eventlet_backdoor.py
  5d40e14 Remove code that moved to oslo.i18n
  90ae24b Remove redundant default=None for config options
  fcf517d Update oslo log messages with translation domains

 * neutron/openstack/common/loopingcall.py
  5d40e14 Remove code that moved to oslo.i18n
  e377393 Changes calcuation of variable delay
  ab5d5f1 Use timestamp in loopingcall
  bc48099 Log the function name of looping call
  fb4e863 Remove deprecated LoopingCall
  fcf517d Update oslo log messages with translation domains

 * neutron/openstack/common/service.py
  5d40e14 Remove code that moved to oslo.i18n
  6ede600 rpc, notifier: remove deprecated modules

 * neutron/openstack/common/systemd.py
  17c4e21 Fix docstring indentation in systemd

 * neutron/openstack/common/threadgroup.py
  5a1a016 Make stop_timers() method public
  fdc8883 Add graceful stop function to ThreadGroup.stop
  2d06d6c Simple typo correction
  4d18b57 threadgroup: use threading rather than greenthread

Change-Id: I4887545f861a93223e2c7cbcdd39efe991bff547
  • Loading branch information
booxter committed Oct 17, 2014
1 parent c1bc6ff commit 623a30b
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 41 deletions.
9 changes: 5 additions & 4 deletions neutron/openstack/common/eventlet_backdoor.py
Expand Up @@ -29,7 +29,7 @@
import greenlet
from oslo.config import cfg

from neutron.openstack.common.gettextutils import _
from neutron.openstack.common._i18n import _LI
from neutron.openstack.common import log as logging

help_for_backdoor_port = (
Expand All @@ -41,7 +41,6 @@
"chosen port is displayed in the service's log file.")
eventlet_backdoor_opts = [
cfg.StrOpt('backdoor_port',
default=None,
help="Enable eventlet backdoor. %s" % help_for_backdoor_port)
]

Expand Down Expand Up @@ -137,8 +136,10 @@ def displayhook(val):
# In the case of backdoor port being zero, a port number is assigned by
# listen(). In any case, pull the port number out here.
port = sock.getsockname()[1]
LOG.info(_('Eventlet backdoor listening on %(port)s for process %(pid)d') %
{'port': port, 'pid': os.getpid()})
LOG.info(
_LI('Eventlet backdoor listening on %(port)s for process %(pid)d') %
{'port': port, 'pid': os.getpid()}
)
eventlet.spawn_n(eventlet.backdoor.backdoor_server, sock,
locals=backdoor_locals)
return port
46 changes: 24 additions & 22 deletions neutron/openstack/common/loopingcall.py
Expand Up @@ -16,31 +16,36 @@
# under the License.

import sys
import time

from eventlet import event
from eventlet import greenthread

from neutron.openstack.common.gettextutils import _
from neutron.openstack.common._i18n import _LE, _LW
from neutron.openstack.common import log as logging
from neutron.openstack.common import timeutils

LOG = logging.getLogger(__name__)

# NOTE(zyluo): This lambda function was declared to avoid mocking collisions
# with time.time() called in the standard logging module
# during unittests.
_ts = lambda: time.time()


class LoopingCallDone(Exception):
"""Exception to break out and stop a LoopingCall.
"""Exception to break out and stop a LoopingCallBase.
The poll-function passed to LoopingCall can raise this exception to
The poll-function passed to LoopingCallBase can raise this exception to
break out of the loop normally. This is somewhat analogous to
StopIteration.
An optional return-value can be included as the argument to the exception;
this return-value will be returned by LoopingCall.wait()
this return-value will be returned by LoopingCallBase.wait()
"""

def __init__(self, retvalue=True):
""":param retvalue: Value that LoopingCall.wait() should return."""
""":param retvalue: Value that LoopingCallBase.wait() should return."""
self.retvalue = retvalue


Expand Down Expand Up @@ -72,21 +77,22 @@ def _inner():

try:
while self._running:
start = timeutils.utcnow()
start = _ts()
self.f(*self.args, **self.kw)
end = timeutils.utcnow()
end = _ts()
if not self._running:
break
delay = interval - timeutils.delta_seconds(start, end)
if delay <= 0:
LOG.warn(_('task run outlasted interval by %s sec') %
-delay)
greenthread.sleep(delay if delay > 0 else 0)
delay = end - start - interval
if delay > 0:
LOG.warn(_LW('task %(func_name)s run outlasted '
'interval by %(delay).2f sec'),
{'func_name': repr(self.f), 'delay': delay})
greenthread.sleep(-delay if delay < 0 else 0)
except LoopingCallDone as e:
self.stop()
done.send(e.retvalue)
except Exception:
LOG.exception(_('in fixed duration looping call'))
LOG.exception(_LE('in fixed duration looping call'))
done.send_exception(*sys.exc_info())
return
else:
Expand All @@ -98,11 +104,6 @@ def _inner():
return self.done


# 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.
Expand All @@ -126,14 +127,15 @@ def _inner():

if periodic_interval_max is not None:
idle = min(idle, periodic_interval_max)
LOG.debug(_('Dynamic looping call sleeping for %.02f '
'seconds'), idle)
LOG.debug('Dynamic looping call %(func_name)s sleeping '
'for %(idle).02f seconds',
{'func_name': repr(self.f), 'idle': idle})
greenthread.sleep(idle)
except LoopingCallDone as e:
self.stop()
done.send(e.retvalue)
except Exception:
LOG.exception(_('in dynamic looping call'))
LOG.exception(_LE('in dynamic looping call'))
done.send_exception(*sys.exc_info())
return
else:
Expand Down
10 changes: 1 addition & 9 deletions neutron/openstack/common/service.py
Expand Up @@ -38,14 +38,12 @@
from oslo.config import cfg

from neutron.openstack.common import eventlet_backdoor
from neutron.openstack.common.gettextutils import _LE, _LI, _LW
from neutron.openstack.common import importutils
from neutron.openstack.common._i18n import _LE, _LI, _LW
from neutron.openstack.common import log as logging
from neutron.openstack.common import systemd
from neutron.openstack.common import threadgroup


rpc = importutils.try_import('neutron.openstack.common.rpc')
CONF = cfg.CONF
LOG = logging.getLogger(__name__)

Expand Down Expand Up @@ -180,12 +178,6 @@ def _wait_for_exit_or_signal(self, ready_callback=None):
status = exc.code
finally:
self.stop()
if rpc:
try:
rpc.cleanup()
except Exception:
# We're shutting down, so it doesn't matter at this point.
LOG.exception(_LE('Exception during rpc cleanup.'))

return status, signo

Expand Down
4 changes: 3 additions & 1 deletion neutron/openstack/common/systemd.py
Expand Up @@ -50,14 +50,16 @@ def _sd_notify(unset_env, msg):

def notify():
"""Send notification to Systemd that service is ready.
For details see
http://www.freedesktop.org/software/systemd/man/sd_notify.html
http://www.freedesktop.org/software/systemd/man/sd_notify.html
"""
_sd_notify(False, 'READY=1')


def notify_once():
"""Send notification once to Systemd that service is ready.
Systemd sets NOTIFY_SOCKET environment variable with the name of the
socket listening for notifications from services.
This method removes the NOTIFY_SOCKET environment variable to ensure
Expand Down
28 changes: 23 additions & 5 deletions neutron/openstack/common/threadgroup.py
Expand Up @@ -11,10 +11,10 @@
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import threading

import eventlet
from eventlet import greenpool
from eventlet import greenthread

from neutron.openstack.common import log as logging
from neutron.openstack.common import loopingcall
Expand Down Expand Up @@ -51,7 +51,7 @@ def link(self, func, *args, **kwargs):


class ThreadGroup(object):
"""The point of the ThreadGroup classis to:
"""The point of the ThreadGroup class is to:
* keep track of timers and greenthreads (making it easier to stop them
when need be).
Expand Down Expand Up @@ -85,8 +85,8 @@ def add_thread(self, callback, *args, **kwargs):
def thread_done(self, thread):
self.threads.remove(thread)

def stop(self):
current = greenthread.getcurrent()
def _stop_threads(self):
current = threading.current_thread()

# Iterate over a copy of self.threads so thread_done doesn't
# modify the list while we're iterating
Expand All @@ -99,13 +99,31 @@ def stop(self):
except Exception as ex:
LOG.exception(ex)

def stop_timers(self):
for x in self.timers:
try:
x.stop()
except Exception as ex:
LOG.exception(ex)
self.timers = []

def stop(self, graceful=False):
"""stop function has the option of graceful=True/False.
* In case of graceful=True, wait for all threads to be finished.
Never kill threads.
* In case of graceful=False, kill threads immediately.
"""
self.stop_timers()
if graceful:
# In case of graceful=True, wait for all threads to be
# finished, never kill threads
self.wait()
else:
# In case of graceful=False(Default), kill threads
# immediately
self._stop_threads()

def wait(self):
for x in self.timers:
try:
Expand All @@ -114,7 +132,7 @@ def wait(self):
pass
except Exception as ex:
LOG.exception(ex)
current = greenthread.getcurrent()
current = threading.current_thread()

# Iterate over a copy of self.threads so thread_done doesn't
# modify the list while we're iterating
Expand Down

0 comments on commit 623a30b

Please sign in to comment.