Skip to content

Commit 80e3728

Browse files
committed
fix double connection to default database
1 parent d727c4f commit 80e3728

File tree

5 files changed

+44
-31
lines changed

5 files changed

+44
-31
lines changed

mamonsu/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
__author__ = 'Dmitry Vasilyev'
22
__author_email__ = '[email protected]'
33
__description__ = 'Monitoring agent for PostgreSQL'
4-
__version__ = '2.2.5'
4+
__version__ = '2.2.6'
55
__licence__ = 'BSD'
66

77
__url__ = 'https://github.com/postgrespro/mamonsu'

mamonsu/plugins/pgsql/driver/_connection.py

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,21 @@
99

1010
class ConnectionInfo(object):
1111

12-
def __init__(self, info={}):
13-
self.info = info
14-
self.host = self.info.get('host') or os.environ.get('PGHOST')
15-
self.port = self.info.get('port') or int(os.environ.get('PGPORT') or 5432)
16-
self.user = self.info.get('user') or os.environ.get('PGUSER')
17-
self.passwd = self.info.get('passwd') or os.environ.get('PGPASSWORD')
18-
self.db = self.info.get('db') or os.environ.get('PGDATABASE')
19-
self.timeout = self.info.get('timeout') or int(
12+
def __init__(self, connection_info={}):
13+
self.connection_info = connection_info
14+
self.host = self.connection_info.get('host') or os.environ.get('PGHOST')
15+
self.port = self.connection_info.get('port') or int(os.environ.get('PGPORT') or 5432)
16+
self.user = self.connection_info.get('user') or os.environ.get('PGUSER')
17+
self.passwd = self.connection_info.get('passwd') or os.environ.get('PGPASSWORD')
18+
self.default_db = self.connection_info.get('db') or os.environ.get('PGDATABASE')
19+
self.timeout = self.connection_info.get('timeout') or int(
2020
os.environ.get('PGTIMEOUT') or 1)
21-
self.appname = self.info.get('appname') or os.environ.get('PGAPPNAME')
22-
self.log = logging.getLogger('PGSQL-({0})'.format(self.conn_str()))
21+
self.appname = self.connection_info.get('appname') or os.environ.get('PGAPPNAME')
22+
self.log = logging.getLogger('PGSQL-({0})'.format(self._connection_string()))
2323

24-
def conn_str(self):
24+
def _connection_string(self):
2525
return 'host={0} db={1} user={2} port={3}'.format(
26-
self.host, self.db, self.user, self.port)
26+
self.host, self.default_db, self.user, self.port)
2727

2828

2929
class Connection(ConnectionInfo):
@@ -74,7 +74,7 @@ def _connect(self):
7474
unix_sock=unix_sock,
7575
host=host,
7676
port=self.port,
77-
database=self.db,
77+
database=self.default_db,
7878
application_name=self.appname
7979
)
8080
self.log.debug('connected')

mamonsu/plugins/pgsql/driver/pool.py

Lines changed: 20 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ class Pool(ConnectionInfo):
4040

4141
def __init__(self):
4242
super(Pool, self).__init__()
43-
self.all_connections = {}
43+
self._connections = {}
4444
self._cache = {
4545
'server_version': {'storage': {}},
4646
'bootstrap': {'storage': {}, 'counter': 0, 'cache': 10},
@@ -49,24 +49,27 @@ def __init__(self):
4949
'pgproee': {'storage': {}}
5050
}
5151

52+
def get_with_default_db(self, db=None):
53+
if db is None:
54+
return self.default_db
55+
return db
56+
5257
def connection_string(self, db=None):
53-
self._init_connection(db)
54-
return self.all_connections[db].conn_str()
58+
db = self.get_with_default_db(db)
59+
return self._connections[db]._connection_string()
5560

5661
def query(self, query, db=None):
57-
if db is None:
58-
db = self.db
62+
db = self.get_with_default_db(db)
5963
self._init_connection(db)
60-
return self.all_connections[db].query(query)
64+
return self._connections[db].query(query)
6165

6266
def server_version(self, db=None):
6367
if db in self._cache['server_version']['storage']:
6468
return self._cache['server_version']['storage'][db]
6569
if platform.PY2:
6670
result = self.query('show server_version', db)[0][0]
6771
elif platform.PY3:
68-
result = bytes(
69-
self.query('show server_version', db)[0][0], 'utf-8')
72+
result = bytes(self.query('show server_version', db)[0][0], 'utf-8')
7073
self._cache['server_version']['storage'][db] = '{0}'.format(
7174
result.decode('ascii'))
7275
return self._cache['server_version']['storage'][db]
@@ -84,7 +87,7 @@ def in_recovery(self, db=None):
8487
return self._cache['recovery']['storage'][db]
8588
self._cache['recovery']['counter'] = 0
8689
self._cache['recovery']['storage'][db] = self.query(
87-
"select pg_catalog.pg_is_in_recovery()")[0][0]
90+
"select pg_catalog.pg_is_in_recovery()", db)[0][0]
8891
return self._cache['recovery']['storage'][db]
8992

9093
def is_bootstraped(self, db=None):
@@ -98,10 +101,10 @@ def is_bootstraped(self, db=None):
98101
result = int(self.query(sql, db)[0][0])
99102
self._cache['bootstrap']['storage'][db] = (result == 1)
100103
if self._cache['bootstrap']['storage'][db]:
101-
self.all_connections[db].log.info('Found mamonsu bootstrap')
104+
self._connections[db].log.info('Found mamonsu bootstrap')
102105
else:
103-
self.all_connections[db].log.info('Can\'t found mamonsu bootstrap')
104-
self.all_connections[db].log.info('hint: run `mamonsu bootstrap` if you want to run without superuser rights')
106+
self._connections[db].log.info('Can\'t found mamonsu bootstrap')
107+
self._connections[db].log.info('hint: run `mamonsu bootstrap` if you want to run without superuser rights')
105108
return self._cache['bootstrap']['storage'][db]
106109

107110
def is_pgpro(self, db=None):
@@ -150,8 +153,9 @@ def run_sql_type(self, typ, db=None):
150153
return self.query(self.get_sql(typ, db), db)
151154

152155
def _init_connection(self, db):
153-
if db not in self.all_connections:
156+
db = self.get_with_default_db(db)
157+
if db not in self._connections:
154158
# create new connection
155-
info = self.info
156-
info['db'] = db
157-
self.all_connections[db] = Connection(info)
159+
connection_info = self.connection_info
160+
connection_info['db'] = db
161+
self._connections[db] = Connection(connection_info)

packaging/debian/changelog

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
mamonsu (2.2.6-1) stable; urgency=low
2+
3+
* bug fixes: memory and autovacuum plugin.
4+
5+
-- PostgresPro DBA <[email protected]> Mon, 21 Nov 2016 16:00:00 +0300
6+
17
mamonsu (2.2.5-1) stable; urgency=low
28

39
* memory and cfs plugin fixes

packaging/rpm/SPECS/mamonsu.spec

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
Name: mamonsu
2-
Version: 2.2.5
2+
Version: 2.2.6
33
Release: 1%{?dist}
44
Summary: Monitoring agent for PostgreSQL
55
Group: Applications/Internet
@@ -70,6 +70,9 @@ chown mamonsu.mamonsu /var/log/mamonsu
7070
/sbin/chkconfig --del mamonsu
7171

7272
%changelog
73+
* Mon Nov 21 2016 Dmitry Vasilyev <[email protected]> - 2.2.6-1
74+
- bug fixes: memory and autovacuum plugin
75+
7376
* Fri Nov 18 2016 Dmitry Vasilyev <[email protected]> - 2.2.5-1
7477
- memory and cfs plugin fixes
7578

0 commit comments

Comments
 (0)