-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
tickets/dm-2281 #4
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
#!/usr/bin/env python | ||
|
||
# LSST Data Management System | ||
# Copyright 2008-2015 LSST Corporation. | ||
# | ||
# This product includes software developed by the | ||
# LSST Project (http://www.lsst.org/). | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the LSST License Statement and | ||
# the GNU General Public License along with this program. If not, | ||
# see <http://www.lsstcorp.org/LegalNotices/>. | ||
|
||
""" | ||
This module implements a very basic connection pool - a dictionary of named "db" | ||
objects. Optionally, each db object can have it's connection refreshed to protect | ||
against connection timeout. | ||
|
||
@author Jacek Becla, SLAC | ||
|
||
""" | ||
|
||
# standard library imports | ||
import logging as log | ||
from time import time | ||
|
||
# local imports | ||
from lsst.db.db import Db | ||
from lsst.db.exception import produceExceptionClass | ||
|
||
#################################################################################### | ||
|
||
DbPoolException = produceExceptionClass('DbException', [ | ||
(1600, "ENTRY_NOT_FOUND", "Requested Db entry not found."), | ||
(1605, "ENTRY_EXISTS", "The Db entry already exists.")]) | ||
|
||
#################################################################################### | ||
class DbPool(object): | ||
""" | ||
@brief A pool of Db objects. | ||
|
||
This class implements a pool of Db objects. Optionally, it can maintain live | ||
connection and automatically recover from time outs. | ||
""" | ||
|
||
def __init__(self): | ||
""" | ||
Create a DbPool instance. | ||
""" | ||
self._pool = {} | ||
self._log = log.getLogger("lsst.db.DbPool") | ||
|
||
##### Connection-related functions ############################################# | ||
def addConn(self, cName, dbConn, checkFreq=600): | ||
""" | ||
Add Db Connection Object to the pool. | ||
|
||
@param cName Name of this connection | ||
@param dbConn Db Connection Object | ||
@param checkFreq Frequency of rechecking if connection is alive in seconds, | ||
-1 for "don't check it at all" | ||
|
||
If will raise ENTRY_EXISTS exception if the entry already exists. | ||
""" | ||
if cName in self._pool: | ||
raise DbPoolException(DbPoolException.ENTRY_EXISTS, cName) | ||
|
||
# Remember timestamp when connection was last checked. | ||
lastChecked = 0 | ||
|
||
# the pool of named Db objects along with checkFreq and lastCheck time | ||
self._pool[cName] = (dbConn, checkFreq, lastChecked) | ||
|
||
def delConn(self, cName): | ||
""" | ||
Remove Db Connection Object corresponding to a given name from the pool. | ||
|
||
If will raise ENTRY_NOT_FOUND exception if the entry is not found for a | ||
given name. | ||
""" | ||
if cName not in self._pool: | ||
raise DbPoolException(DbPoolException.ENTRY_NOT_FOUND, cName) | ||
del self._pool[cName] | ||
|
||
def getConn(self, cName): | ||
""" | ||
Return Db Connection Object corresponding to a given name, optionally | ||
ensure connection did not time out and reconnect if needed. | ||
|
||
If will raise ENTRY_NOT_FOUND exception if the entry is not found for a | ||
given name. It can raise MySQLdb.* exceptions if connection checking is on. | ||
""" | ||
if cName not in self._pool: | ||
raise DbPoolException(DbPoolException.ENTRY_NOT_FOUND, cName) | ||
|
||
(db, checkFreq, lastChecked) = self._pool[cName] | ||
if checkFreq != -1: | ||
if time() - lastChecked > checkFreq: | ||
self._log.debug( | ||
"Checking connection for '%s' before executing query.", cName) | ||
if not db.isConnected(): | ||
self._log.debug("Attempting to reconnect for '%s'.", cName) | ||
db.connect() | ||
self._pool[cName] = (db, checkFreq, time()) | ||
return db |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# -*- python -*- | ||
from lsst.sconsUtils import scripts | ||
scripts.BasicSConscript.tests(pyList=["testDbLocal.py", "testDbRemote.py"]) | ||
scripts.BasicSConscript.tests( | ||
pyList=["testDbLocal.py", "testDbRemote.py", "testDbPool.py"]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -43,7 +43,6 @@ | |
""" | ||
|
||
# standard library | ||
import ConfigParser | ||
import logging as log | ||
import os | ||
import tempfile | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
#!/usr/bin/env python | ||
|
||
# LSST Data Management System | ||
# Copyright 2015 LSST Corporation. | ||
# | ||
# This product includes software developed by the | ||
# LSST Project (http://www.lsst.org/). | ||
# | ||
# This program is free software: you can redistribute it and/or modify | ||
# it under the terms of the GNU General Public License as published by | ||
# the Free Software Foundation, either version 3 of the License, or | ||
# (at your option) any later version. | ||
# | ||
# This program is distributed in the hope that it will be useful, | ||
# but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
# GNU General Public License for more details. | ||
# | ||
# You should have received a copy of the LSST License Statement and | ||
# the GNU General Public License along with this program. If not, | ||
# see <http://www.lsstcorp.org/LegalNotices/>. | ||
|
||
""" | ||
This is a unittest for the DbPool class. | ||
|
||
The test requires credential file ~/.lsst/dbAuth-test.txt config file with | ||
the following: | ||
[mysql] | ||
user = <userName> | ||
passwd = <passwd> # this is optional | ||
host = localhost | ||
port = 3306 | ||
|
||
@author Jacek Becla, SLAC | ||
""" | ||
|
||
# standard library | ||
import logging as log | ||
import os | ||
import time | ||
import unittest | ||
|
||
# local | ||
from lsst.db.db import Db | ||
from lsst.db.dbPool import DbPool, DbPoolException | ||
|
||
class TestDbPool(unittest.TestCase): | ||
CREDFILE = "~/.lsst/dbAuth-test.txt" | ||
|
||
def testBasics(self): | ||
""" | ||
Basic test: add, get, delete. | ||
""" | ||
dbPool = DbPool() | ||
dbPool.addConn("a", Db(read_default_file=TestDbPool.CREDFILE), 5) | ||
dbPool.addConn("b", Db(read_default_file=TestDbPool.CREDFILE), 1) | ||
dbPool.addConn("c", Db(read_default_file=TestDbPool.CREDFILE)) | ||
self.assertRaises(DbPoolException, dbPool.addConn, "c", | ||
Db(read_default_file=TestDbPool.CREDFILE)) | ||
dbPool.getConn("a").execCommand0("SHOW DATABASES LIKE '%Stripe82%'") | ||
dbPool.getConn("b").execCommand0("SHOW DATABASES LIKE '%Stripe82%'") | ||
dbPool.getConn("c").execCommand0("SHOW DATABASES LIKE '%Stripe82%'") | ||
time.sleep(2) | ||
dbPool.getConn("a").execCommand0("SHOW DATABASES LIKE '%Stripe82%'") | ||
dbPool.getConn("b").execCommand0("SHOW DATABASES LIKE '%Stripe82%'") | ||
dbPool.getConn("c").execCommand0("SHOW DATABASES LIKE '%Stripe82%'") | ||
dbPool.getConn("a").disconnect() | ||
dbPool.getConn("a").execCommand0("SHOW DATABASES LIKE '%Stripe82%'") | ||
dbPool.delConn("b") | ||
dbPool.getConn("a").execCommand0("SHOW DATABASES LIKE '%Stripe82%'") | ||
self.assertRaises(DbPoolException, dbPool.getConn, "b") | ||
dbPool.getConn("a").execCommand0("SHOW DATABASES LIKE '%Stripe82%'") | ||
dbPool.getConn("c").execCommand0("SHOW DATABASES LIKE '%Stripe82%'") | ||
dbPool.delConn("a") | ||
self.assertRaises(DbPoolException, dbPool.delConn, "b") | ||
dbPool.delConn("c") | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is it possible to add test for re-connect? Probably just doing |
||
#################################################################################### | ||
def main(): | ||
log.basicConfig( | ||
format='%(asctime)s %(name)s %(levelname)s: %(message)s', | ||
datefmt='%m/%d/%Y %I:%M:%S', | ||
level=log.DEBUG) | ||
|
||
credFile = os.path.expanduser(TestDbPool.CREDFILE) | ||
if not os.path.isfile(credFile): | ||
log.warning("Required file with credentials '%s' not found.", credFile) | ||
else: | ||
unittest.main() | ||
|
||
if __name__ == "__main__": | ||
main() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -40,7 +40,6 @@ | |
""" | ||
|
||
# standard library | ||
import ConfigParser | ||
import logging as log | ||
import os | ||
import tempfile | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It looks like one can overwrite existing connection name with different connection, it is intended? If not then it needs to be checked.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Not intended, I fixed it