[PATCH 07 of 23 cubicweb V2] [database/pool] add some debug logging

Laurent Peuch cortex at worlddomination.be
Fri Jan 17 14:37:04 CET 2020


# HG changeset patch
# User Laurent Peuch <cortex at worlddomination.be>
# Date 1576831122 -3600
#      Fri Dec 20 09:38:42 2019 +0100
# Node ID 0e4e519f2bf08caf3fb07430ba5914d18a9efde3
# Parent  7097ddcc444aee07cbec28c4dfae0a24b6a2a345
# Available At https://hg.logilab.org/users/lpeuch/cubicweb
#              hg pull https://hg.logilab.org/users/lpeuch/cubicweb -r 0e4e519f2bf0
[database/pool] add some debug logging

diff --git a/cubicweb/server/repository.py b/cubicweb/server/repository.py
--- a/cubicweb/server/repository.py
+++ b/cubicweb/server/repository.py
@@ -242,11 +242,14 @@ class _CnxSetPool:
         return self._queue.qsize()
 
     def get(self):
+        self.debug(f"[pool] free queue size is {self.qsize()}/{self.current_size.value} "
+                   f"(max {self.max_size})")
         if self._queue is None:
             return self._source.wrapped_connection()
 
         # if we have something in the queue, return immediately
         if self.qsize():
+            self.debug("[pool] we have an available connection, returns it")
             try:
                 connection = self._queue.get(block=False)
                 self._maybe_close_connection()
@@ -263,8 +266,12 @@ class _CnxSetPool:
                     # we have load, open another connection
                     cnxset = self.source.wrapped_connection()
                     self._cnxsets.append(cnxset)
+                    self.debug("[pool] we had to open a new connection, connection number: %s/%s" %
+                               (self.current_size, self.max_size))
                     self.current_size.increment()
                     return cnxset
+            finally:
+                self.debug("[pool] we got a connection after minimum timeout")
 
         # here, we can't open new connections because we are full, just wait
         try:
@@ -306,6 +313,8 @@ class _CnxSetPool:
             cnxset.close(True)
         else:
             self._queue.put_nowait(cnxset)
+            self.debug(f"[pool] release {cnxset} [{id(cnxset)}], free queue size is "
+                       f"{self.qsize()}/{self.current_size.value} (max {self.max_size})")
 
     def __iter__(self):
         for cnxset in self._cnxsets:
@@ -345,6 +354,7 @@ class _CnxSetPool:
                 self.exception('error while closing %s, error: %s' % (connection, e))
             else:
                 self.current_size.decrement()
+                self.debug("[pool] load is low, close a connection")
                 self._last_time_connection_close = time.time()
 
     def close(self):
@@ -358,6 +368,10 @@ class _CnxSetPool:
                 except Exception as e:
                     self.exception('error while closing %s, error: %s' % (cnxset, e))
 
+    # these are overridden by set_log_methods below
+    # only defining here to prevent pylint from complaining
+    info = warning = error = critical = exception = debug = lambda msg, *a, **kw: None
+
 
 class Repository(object):
     """a repository provides access to a set of persistent storages for
@@ -1086,3 +1100,4 @@ class Repository(object):
 
 
 set_log_methods(Repository, getLogger('cubicweb.repository'))
+set_log_methods(_CnxSetPool, getLogger('cubicweb.repository.pool'))



More information about the cubicweb-devel mailing list