summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authorMike Bayer <mike_mp@zzzcomputing.com>2017-04-12 11:37:19 -0400
committerMike Bayer <mike_mp@zzzcomputing.com>2017-04-12 12:53:40 -0400
commitcef4e5ff38dc7d2200800837c110ab6beec10d8a (patch)
tree24eac58c1fb6e3105c9c4c7046d1aa2b5259a67d /lib
parent1b463058e3282c73d0fb361f78e96ecaa23ce9f4 (diff)
downloadsqlalchemy-cef4e5ff38dc7d2200800837c110ab6beec10d8a.tar.gz
Warn on _compiled_cache growth
Added warnings to the LRU "compiled cache" used by the :class:`.Mapper` (and ultimately will be for other ORM-based LRU caches) such that when the cache starts hitting its size limits, the application will emit a warning that this is a performance-degrading situation that may require attention. The LRU caches can reach their size limits primarily if an application is making use of an unbounded number of :class:`.Engine` objects, which is an antipattern. Otherwise, this may suggest an issue that should be brought to the SQLAlchemy developer's attention. Additionally, adjusted the test_memusage algorithm again as the previous one could still allow a growing memory size to be missed. Change-Id: I020d1ceafb7a08f6addfa990a1e7acd09f933240
Diffstat (limited to 'lib')
-rw-r--r--lib/sqlalchemy/orm/mapper.py15
-rw-r--r--lib/sqlalchemy/util/_collections.py13
2 files changed, 26 insertions, 2 deletions
diff --git a/lib/sqlalchemy/orm/mapper.py b/lib/sqlalchemy/orm/mapper.py
index 27456c35b..28a5c5b9b 100644
--- a/lib/sqlalchemy/orm/mapper.py
+++ b/lib/sqlalchemy/orm/mapper.py
@@ -2718,7 +2718,20 @@ class Mapper(InspectionAttr):
@_memoized_configured_property
def _compiled_cache(self):
- return util.LRUCache(self._compiled_cache_size)
+ return util.LRUCache(self._compiled_cache_size,
+ size_alert=self._alert_lru_cache_limit)
+
+ def _alert_lru_cache_limit(self):
+ util.warn(
+ "Compiled statement cache for mapper %s is "
+ "reaching its size threshold of %d, based on _compiled_cache_size "
+ "of %d. Please refer to "
+ "http://docs.sqlalchemy.org/en/latest/faq/performance.html"
+ "#faq_compiled_cache_threshold"
+ " for best practices." %
+ (self,
+ self._compiled_cache.size_threshold,
+ self._compiled_cache_size))
@_memoized_configured_property
def _sorted_tables(self):
diff --git a/lib/sqlalchemy/util/_collections.py b/lib/sqlalchemy/util/_collections.py
index bdd40fc79..d94af5f62 100644
--- a/lib/sqlalchemy/util/_collections.py
+++ b/lib/sqlalchemy/util/_collections.py
@@ -868,9 +868,12 @@ class LRUCache(dict):
"""
- def __init__(self, capacity=100, threshold=.5):
+ __slots__ = 'capacity', 'threshold', 'size_alert', '_counter', '_mutex'
+
+ def __init__(self, capacity=100, threshold=.5, size_alert=None):
self.capacity = capacity
self.threshold = threshold
+ self.size_alert = size_alert
self._counter = 0
self._mutex = threading.Lock()
@@ -910,11 +913,19 @@ class LRUCache(dict):
item[1] = value
self._manage_size()
+ @property
+ def size_threshold(self):
+ return self.capacity + self.capacity * self.threshold
+
def _manage_size(self):
if not self._mutex.acquire(False):
return
try:
+ size_alert = bool(self.size_alert)
while len(self) > self.capacity + self.capacity * self.threshold:
+ if size_alert:
+ size_alert = False
+ self.size_alert()
by_counter = sorted(dict.values(self),
key=operator.itemgetter(2),
reverse=True)