8000 v2.2.8: fix double connection · postgrespro/mamonsu@b4e133c · GitHub
[go: up one dir, main page]

Skip to content

Commit b4e133c

Browse files
committed
v2.2.8: fix double connection
1 parent 7852280 commit b4e133c

File tree

6 files changed

+77
-34
lines changed

6 files changed

+77
-34
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
*.pyc
22
*.egg-info
33
*.swp
4+
*.idea
45
__pycache__
56
.ropeproject
67

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__ = 'info@postgrespro.ru'
33
__description__ = 'Monitoring agent for PostgreSQL'
4-
__version__ = '2.2.7'
4+
__version__ = '2.2.8'
55
__licence__ = 'BSD'
66

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

mamonsu/plugins/pgsql/driver/_connection.py renamed to mamonsu/plugins/pgsql/driver/connection.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -9,27 +9,37 @@
99

1010
class ConnectionInfo(object):
1111

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.con 10000 nection_info.get('db') or os.environ.get('PGDATABASE')
19-
self.timeout = self.connection_info.get('timeout') or int(
12+
def __init__(self, connection_hash={}):
13+
self.host = connection_hash.get('host') or os.environ.get('PGHOST')
14+
self.port = connection_hash.get('port') or int(os.environ.get('PGPORT') or 5432)
15+
self.user = connection_hash.get('user') or os.environ.get('PGUSER')
16+
self.passwd = connection_hash.get('passwd') or os.environ.get('PGPASSWORD')
17+
self.db = connection_hash.get('db') or os.environ.get('PGDATABASE')
18+
self.timeout = connection_hash.get('timeout') or int(
2019
os.environ.get('PGTIMEOUT') or 1)
21-
self.appname = self.connection_info.get('appname') or os.environ.get('PGAPPNAME')
22-
self.log = logging.getLogger('PGSQL-({0})'.format(self._connection_string()))
20+
self.appname = connection_hash.get('appname') or os.environ.get('PGAPPNAME')
21+
self.log = logging.getLogger('PGSQL-({0})'.format(self.to_string()))
2322

24-
def _connection_string(self):
23+
def to_string(self):
2524
return 'host={0} db={1} user={2} port={3}'.format(
26-
self.host, self.default_db, self.user, self.port)
25+
self.host, self.db, self.user, self.port)
26+
27+
def get_hash(self):
28+
return {
29+
'host': self.host,
30+
'port': self.port,
31+
'user': self.user,
32+
'passwd': self.user,
33+
'db': self.db,
34+
'timeout': self.timeout,
35+
'appname': self.appname
36+
}
2737

2838

2939
class Connection(ConnectionInfo):
3040

3141
def __init__(self, info={}):
32-
super(Connection, self).__init__()
42+
super(Connection, self).__init__(info)
3343
self.lock = threading.Lock()
3444
self.conn = None
3545
self.connected = False
@@ -74,7 +84,7 @@ def _connect(self):
7484
unix_sock=unix_sock,
7585
host=host,
7686
port=self.port,
77-
database=self.default_db,
87+
database=self.db,
7888
application_name=self.appname
7989
)
8090
self.log.debug('connected')

mamonsu/plugins/pgsql/driver/pool.py

Lines changed: 41 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import mamonsu.lib.platform as platform
22
from distutils.version import LooseVersion
3-
from ._connection import Connection, ConnectionInfo
3+
from connection import Connection, ConnectionInfo
44

55

6-
class Pool(ConnectionInfo):
6+
class Pool(object):
77

88
ExcludeDBs = ['template0', 'template1', 'postgres']
99

@@ -38,8 +38,9 @@ class Pool(ConnectionInfo):
3838
),
3939
}
4040

41-
def __init__(self):
42-
super(Pool, self).__init__()
41+
def __init__(self, params={}):
42+
self._params = params
43+
self._primary_connection_hash = None
4344
self._connections = {}
4445
self._cache = {
4546
'server_version': {'storage': {}},
@@ -49,38 +50,38 @@ def __init__(self):
4950
'pgproee': {'storage': {}}
5051
}
5152

52-
def get_with_default_db(self, db=None):
53-
if db is None:
54-
return self.default_db
55-
return db
56-
5753
def connection_string(self, db=None):
58-
db = self.get_with_default_db(db)
59-
return self._connections[db]._connection_string()
54+
db = self._normalize_db(db)
55+
return self._connections[db].to_string()
6056

6157
def query(self, query, db=None):
62-
db = self.get_with_default_db(db)
58+
db = self._normalize_db(db)
6359
self._init_connection(db)
6460
return self._connections[db].query(query)
6561

6662
def server_version(self, db=None):
63+
db = self._normalize_db(db)
6764
if db in self._cache['server_version']['storage']:
6865
return self._cache['server_version']['storage'][db]
6966
if platform.PY2:
7067
result = self.query('show server_version', db)[0][0]
7168
elif platform.PY3:
72-
result = bytes(self.query('show server_version', db)[0][0], 'utf-8')
69+
result = bytes(
70+
self.query('show server_version', db)[0][0], 'utf-8')
7371
self._cache['server_version']['storage'][db] = '{0}'.format(
7472
result.decode('ascii'))
7573
return self._cache['server_version']['storage'][db]
7674

7775
def server_version_greater(self, version, db=None):
76+
db = self._normalize_db(db)
7877
return self.server_version(db) >= LooseVersion(version)
7978

8079
def server_version_less(self, version, db=None):
80+
db = self._normalize_db(db)
8181
return self.server_version(db) <= LooseVersion(version)
8282

8383
def in_recovery(self, db=None):
84+
db = self._normalize_db(db)
8485
if db in self._cache['recovery']['storage']:
8586
if self._cache['recovery']['counter'] < self._cache['recovery']['cache']:
8687
self._cache['recovery']['counter'] += 1
@@ -91,6 +92,7 @@ def in_recovery(self, db=None):
9192
return self._cache['recovery']['storage'][db]
9293

9394
def is_bootstraped(self, db=None):
95+
db = self._normalize_db(db)
9496
if db in self._cache['bootstrap']['storage']:
9597
if self._cache['bootstrap']['counter'] < self._cache['bootstrap']['cache']:
9698
self._cache['bootstrap']['counter'] += 1
@@ -104,10 +106,12 @@ def is_bootstraped(self, db=None):
104106
self._connections[db].log.info('Found mamonsu bootstrap')
105107
else:
106108
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')
109+
self._connections[db].log.info(
110+
'hint: run `mamonsu bootstrap` if you want to run without superuser rights')
108111
return self._cache['bootstrap']['storage'][db]
109112

110113
def is_pgpro(self, db=None):
114+
db = self._normalize_db(db)
111115
if db in self._cache['pgpro']:
112116
return self._cache['pgpro'][db]
113117
try:
@@ -118,6 +122,7 @@ def is_pgpro(self, db=None):
118122
return self._cache['pgpro'][db]
119123

120124
def is_pgpro_ee(self, db=None):
125+
db = self._normalize_db(db)
121126
if not self.is_pgpro(db):
122127
return False
123128
if db in self._cache['pgproee']:
@@ -128,6 +133,7 @@ def is_pgpro_ee(self, db=None):
128133
return self._cache['pgproee'][db]
129134

130135
def extension_installed(self, ext, db=None):
136+
db = self._normalize_db(db)
131137
result = self.query('select count(*) from pg_catalog.pg_extension\
132138
where extname = \'{0}\''.format(ext), db)
133139
return (int(result[0][0])) == 1
@@ -141,6 +147,7 @@ def databases(self):
141147
return databases
142148

143149
def get_sql(self, typ, db=None):
150+
db = self._normalize_db(db)
144151
if typ not in self.SQL:
145152
raise LookupError("Unknown SQL type: '{0}'".format(typ))
146153
result = self.SQL[typ]
@@ -152,10 +159,26 @@ def get_sql(self, typ, db=None):
152159
def run_sql_type(self, typ, db=None):
153160
return self.query(self.get_sql(typ, db), db)
154161

162+
def _normalize_db(self, db=None):
163+
if db is None:
164+
connection_hash = self._get_primary_connection_hash()
165+
db = connection_hash['db']
166+
return db
167+
168+
# cache function for get primary connection params
169+
def _get_primary_connection_hash(self):
170+
if self._primary_connection_hash is None:
171+
self._primary_connection_hash = ConnectionInfo(self._params).get_hash()
172+
return self._primary_connection_hash
173+
174+
# build connection hash
175+
def _build_connection_hash(self, db):
176+
info = ConnectionInfo(self._get_primary_connection_hash()).get_hash()
177+
info['db'] = self._normalize_db(db)
178+
return info
179+
155180
def _init_connection(self, db):
156-
db = self.get_with_default_db(db)
181+
db = self._normalize_db(db)
157182
if db not in self._connections:
158183
# create new connection
159-
connection_info = self.connection_info
160-
connection_info['db'] = db
161-
self._connections[db] = Connection(connection_info)
184+
self._connections[db] = Connection(self._build_connection_hash(db))

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.8-1) stable; urgency=low
2+
3+
* fix double connection.
4+
5+
-- PostgresPro DBA <dba@postgrespro.ru> Mon, 28 Nov 2016 16:00:00 +0300
6+
17
mamonsu (2.2.7-1) stable; urgency=low
28

39
* add new metrics.

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.7
2+
Version: 2.2.8
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 28 2016 Dmitry Vasilyev <d.vasilyev@postgrespro.ru> - 2.2.8-1
74+
- fix double connection
75+
7376
* Thu Nov 24 2016 Dmitry Vasilyev <d.vasilyev@postgrespro.ru> - 2.2.7-1
7477
- add new metrics
7578

0 commit comments

Comments
 (0)
0