diff options
author | Rodolfo Alonso Hernandez <ralonsoh@redhat.com> | 2019-07-24 11:17:19 +0000 |
---|---|---|
committer | Rodolfo Alonso Hernandez <ralonsoh@redhat.com> | 2019-08-01 17:11:04 +0000 |
commit | 8b7d2c8a93fdf69a828f14bd527d8f132b27bc6e (patch) | |
tree | 73b90260e0c52957a10c6e8590396dd1d33558e5 /neutron/notifiers | |
parent | 6d9283c1bc2878c9f29af45a4543df846f5c293e (diff) | |
download | neutron-8b7d2c8a93fdf69a828f14bd527d8f132b27bc6e.tar.gz |
Refactor the L3 agent batch notifier
This patch is the first one of a series of patches improving how the L3
agents update the router HA state to the Neutron server.
This patch partially reverts the previous patch [1]. When the batch
notifier sends events, it calls the callback method passed during the
initialization, in this case AgentMixin.notify_server. The batch
notifier spawns a new thread in charge of sending the notifications and
then wait the specified "batch_interval" time. If the callback method is
not synchronous with the notify thread execution (what [1] implemented),
the thread can finish while the RPC client is still sending the
HA router states. If another HA state update is received, then both
updates can be executed at the same time. It is possible then that a new
router state can be overwritten with an old one still not sent or
processed.
The batch notifier is refactored, to improve what initally was
implemented [2] and then updated [3]. Currently, each new event thread
can update the "pending_events" list. Then, a new thread is spawned to
process this event list. This thread decouples the current execution
from the calling thread, making the event processing a non-blocking
process.
But with the current implementation, each new process will spawn a new
thread, synchronized with the previous and new ones (using a
synchronized decorator). That means, during the batch interval time, the
system can have as many threads waiting as new events received. Those
threads will end secuentially when the previous threads end the batch
interval sleep time.
Instead of this, this patch receives and enqueue each new event and
allows only one thread to be alive while processing the event list. If
at the end of the processing loop new events are stored, the thread will
process then.
[1] I3f555a0c78fbc02d8214f12b62c37d140bc71da1
[2] I2f8cf261f48bdb632ac0bd643a337290b5297fce
[3] I82f403441564955345f47877151e0c457712dd2f
Partial-Bug: #1837635
Change-Id: I20cfa1cf5281198079f5e0dbf195755abc919581
Diffstat (limited to 'neutron/notifiers')
-rw-r--r-- | neutron/notifiers/batch_notifier.py | 41 |
1 files changed, 22 insertions, 19 deletions
diff --git a/neutron/notifiers/batch_notifier.py b/neutron/notifiers/batch_notifier.py index 9c5c77456f..6dc729c35c 100644 --- a/neutron/notifiers/batch_notifier.py +++ b/neutron/notifiers/batch_notifier.py @@ -10,19 +10,19 @@ # License for the specific language governing permissions and limitations # under the License. +import threading + import eventlet -from neutron_lib.utils import runtime -from oslo_utils import uuidutils from neutron.common import utils class BatchNotifier(object): def __init__(self, batch_interval, callback): - self.pending_events = [] + self._pending_events = eventlet.Queue() self.callback = callback self.batch_interval = batch_interval - self._lock_identifier = 'notifier-%s' % uuidutils.generate_uuid() + self._mutex = threading.Lock() def queue_event(self, event): """Called to queue sending an event with the next batch of events. @@ -35,32 +35,35 @@ class BatchNotifier(object): This replaces the loopingcall with a mechanism that creates a short-lived thread on demand whenever an event is queued. That thread - will wait for a lock, send all queued events and then sleep for - 'batch_interval' seconds to allow other events to queue up. + will check if the lock is released, send all queued events and then + sleep for 'batch_interval' seconds. If at the end of this sleep time, + other threads have added new events to the event queue, the same thread + will process them. - This effectively acts as a rate limiter to only allow 1 batch per - 'batch_interval' seconds. + At the same time, other threads will be able to add new events to the + queue and will spawn new "synced_send" threads to process them. But if + the mutex is locked, the spawned thread will end immediately. :param event: the event that occurred. """ if not event: return - self.pending_events.append(event) + self._pending_events.put(event) - @runtime.synchronized(self._lock_identifier) def synced_send(): - self._notify() - # sleeping after send while holding the lock allows subsequent - # events to batch up - eventlet.sleep(self.batch_interval) + if not self._mutex.locked(): + with self._mutex: + while not self._pending_events.empty(): + self._notify() + # sleeping after send while holding the lock allows + # subsequent events to batch up + eventlet.sleep(self.batch_interval) utils.spawn_n(synced_send) def _notify(self): - if not self.pending_events: - return - - batched_events = self.pending_events - self.pending_events = [] + batched_events = [] + while not self._pending_events.empty(): + batched_events.append(self._pending_events.get()) self.callback(batched_events) |