2013-03-28 19:01:04 +01:00
|
|
|
import logging
|
2014-01-11 00:42:07 +01:00
|
|
|
import select
|
2017-11-16 00:53:11 +01:00
|
|
|
import time
|
2018-11-21 04:08:14 +01:00
|
|
|
from typing import Any, Dict, List, Tuple
|
2013-03-28 19:01:04 +01:00
|
|
|
|
2017-11-16 00:53:11 +01:00
|
|
|
from django.conf import settings
|
2017-08-25 20:01:20 +02:00
|
|
|
from tornado.ioloop import IOLoop, PollIOLoop
|
2017-11-16 00:53:11 +01:00
|
|
|
|
2017-08-25 20:01:20 +02:00
|
|
|
# There isn't a good way to get at what the underlying poll implementation
|
|
|
|
# will be without actually constructing an IOLoop, so we just assume it will
|
|
|
|
# be epoll.
|
|
|
|
orig_poll_impl = select.epoll
|
2016-11-29 07:22:02 +01:00
|
|
|
|
2018-11-07 07:03:26 +01:00
|
|
|
# This is used for a somewhat hacky way of passing the port number
|
|
|
|
# into this early-initialized module.
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
logging_data: Dict[str, str] = {}
|
2018-11-07 07:03:26 +01:00
|
|
|
|
2017-08-25 20:01:20 +02:00
|
|
|
class InstrumentedPollIOLoop(PollIOLoop):
|
2020-04-22 04:13:37 +02:00
|
|
|
def initialize(self, **kwargs): # type: ignore[no-untyped-def] # TODO investigate likely buggy monkey patching here
|
2017-10-27 08:28:23 +02:00
|
|
|
super().initialize(impl=InstrumentedPoll(), **kwargs)
|
2016-11-29 07:22:02 +01:00
|
|
|
|
2017-10-26 11:38:28 +02:00
|
|
|
def instrument_tornado_ioloop() -> None:
|
2017-08-25 20:01:20 +02:00
|
|
|
IOLoop.configure(InstrumentedPollIOLoop)
|
2013-03-28 19:01:04 +01:00
|
|
|
|
|
|
|
# A hack to keep track of how much time we spend working, versus sleeping in
|
|
|
|
# the event loop.
|
|
|
|
#
|
|
|
|
# Creating a new event loop instance with a custom impl object fails (events
|
|
|
|
# don't get processed), so instead we modify the ioloop module variable holding
|
|
|
|
# the default poll implementation. We need to do this before any Tornado code
|
|
|
|
# runs that might instantiate the default event loop.
|
|
|
|
|
2017-11-05 11:52:10 +01:00
|
|
|
class InstrumentedPoll:
|
2017-10-26 11:38:28 +02:00
|
|
|
def __init__(self) -> None:
|
2013-03-28 19:01:04 +01:00
|
|
|
self._underlying = orig_poll_impl()
|
python: Convert assignment type annotations to Python 3.6 style.
This commit was split by tabbott; this piece covers the vast majority
of files in Zulip, but excludes scripts/, tools/, and puppet/ to help
ensure we at least show the right error messages for Xenial systems.
We can likely further refine the remaining pieces with some testing.
Generated by com2ann, with whitespace fixes and various manual fixes
for runtime issues:
- invoiced_through: Optional[LicenseLedger] = models.ForeignKey(
+ invoiced_through: Optional["LicenseLedger"] = models.ForeignKey(
-_apns_client: Optional[APNsClient] = None
+_apns_client: Optional["APNsClient"] = None
- notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- signup_notifications_stream: Optional[Stream] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
+ signup_notifications_stream: Optional["Stream"] = models.ForeignKey('Stream', related_name='+', null=True, blank=True, on_delete=CASCADE)
- author: Optional[UserProfile] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
+ author: Optional["UserProfile"] = models.ForeignKey('UserProfile', blank=True, null=True, on_delete=CASCADE)
- bot_owner: Optional[UserProfile] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
+ bot_owner: Optional["UserProfile"] = models.ForeignKey('self', null=True, on_delete=models.SET_NULL)
- default_sending_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
- default_events_register_stream: Optional[Stream] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_sending_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
+ default_events_register_stream: Optional["Stream"] = models.ForeignKey('zerver.Stream', null=True, related_name='+', on_delete=CASCADE)
-descriptors_by_handler_id: Dict[int, ClientDescriptor] = {}
+descriptors_by_handler_id: Dict[int, "ClientDescriptor"] = {}
-worker_classes: Dict[str, Type[QueueProcessingWorker]] = {}
-queues: Dict[str, Dict[str, Type[QueueProcessingWorker]]] = {}
+worker_classes: Dict[str, Type["QueueProcessingWorker"]] = {}
+queues: Dict[str, Dict[str, Type["QueueProcessingWorker"]]] = {}
-AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional[LDAPSearch] = None
+AUTH_LDAP_REVERSE_EMAIL_SEARCH: Optional["LDAPSearch"] = None
Signed-off-by: Anders Kaseorg <anders@zulipchat.com>
2020-04-22 01:09:50 +02:00
|
|
|
self._times: List[Tuple[float, float]] = []
|
2016-01-26 04:08:05 +01:00
|
|
|
self._last_print = 0.0
|
2013-03-28 19:01:04 +01:00
|
|
|
|
|
|
|
# Python won't let us subclass e.g. select.epoll, so instead
|
|
|
|
# we proxy every method. __getattr__ handles anything we
|
|
|
|
# don't define elsewhere.
|
2017-10-26 11:38:28 +02:00
|
|
|
def __getattr__(self, name: str) -> Any:
|
2013-03-28 19:01:04 +01:00
|
|
|
return getattr(self._underlying, name)
|
|
|
|
|
|
|
|
# Call the underlying poll method, and report timing data.
|
2017-10-26 11:38:28 +02:00
|
|
|
def poll(self, timeout: float) -> Any:
|
2016-09-10 21:10:41 +02:00
|
|
|
|
2013-03-28 19:01:04 +01:00
|
|
|
# Avoid accumulating a bunch of insignificant data points
|
|
|
|
# from short timeouts.
|
|
|
|
if timeout < 1e-3:
|
|
|
|
return self._underlying.poll(timeout)
|
|
|
|
|
|
|
|
# Record start and end times for the underlying poll
|
|
|
|
t0 = time.time()
|
|
|
|
result = self._underlying.poll(timeout)
|
|
|
|
t1 = time.time()
|
|
|
|
|
|
|
|
# Log this datapoint and restrict our log to the past minute
|
|
|
|
self._times.append((t0, t1))
|
|
|
|
while self._times and self._times[0][0] < t1 - 60:
|
|
|
|
self._times.pop(0)
|
|
|
|
|
|
|
|
# Report (at most once every 5s) the percentage of time spent
|
|
|
|
# outside poll
|
|
|
|
if self._times and t1 - self._last_print >= 5:
|
|
|
|
total = t1 - self._times[0][0]
|
2015-11-01 17:10:46 +01:00
|
|
|
in_poll = sum(b-a for a, b in self._times)
|
2013-03-28 19:01:04 +01:00
|
|
|
if total > 0:
|
2016-01-24 03:56:05 +01:00
|
|
|
percent_busy = 100 * (1 - in_poll / total)
|
2017-09-14 20:52:40 +02:00
|
|
|
if settings.PRODUCTION:
|
2018-11-07 07:03:26 +01:00
|
|
|
logging.info('Tornado %s %5.1f%% busy over the past %4.1f seconds'
|
|
|
|
% (logging_data.get('port', 'unknown'), percent_busy, total))
|
2013-08-01 23:12:15 +02:00
|
|
|
self._last_print = t1
|
2013-03-28 19:01:04 +01:00
|
|
|
|
|
|
|
return result
|