Skip to content

Commit f9c2bde

Browse files
committed
Merge pull request pandas-dev#47 from manahl/bugfix/MDP-621-should-mongo_retry-in-the-mongoose.__getitem__
Bugfix/mdp 621 should mongo retry in the mongoose. getitem
2 parents 71b229c + 9f720e6 commit f9c2bde

File tree

2 files changed

+40
-17
lines changed

2 files changed

+40
-17
lines changed

arctic/arctic.py

+12-8
Original file line numberDiff line numberDiff line change
@@ -100,18 +100,19 @@ def __init__(self, mongo_host, app_name=APPLICATION_NAME, allow_secondary=False,
100100
mongo_host.server_info()
101101
self.mongo_host = ",".join(["{}:{}".format(x[0], x[1]) for x in mongo_host.nodes])
102102
self._adminDB = self._conn.admin
103-
103+
104104
@property
105+
@mongo_retry
105106
def _conn(self):
106107
with self._lock:
107108
if self.__conn is None:
108109
host = get_mongodb_uri(self.mongo_host)
109110
logger.info("Connecting to mongo: {0} ({1})".format(self.mongo_host, host))
110-
self.__conn = mongo_retry(pymongo.MongoClient)(host=host,
111-
maxPoolSize=self._MAX_CONNS,
112-
socketTimeoutMS=self._socket_timeout,
113-
connectTimeoutMS=self._connect_timeout,
114-
serverSelectionTimeoutMS=self._server_selection_timeout)
111+
self.__conn = pymongo.MongoClient(host=host,
112+
maxPoolSize=self._MAX_CONNS,
113+
socketTimeoutMS=self._socket_timeout,
114+
connectTimeoutMS=self._connect_timeout,
115+
serverSelectionTimeoutMS=self._server_selection_timeout)
115116
self._adminDB = self.__conn.admin
116117

117118
# Authenticate against admin for the user
@@ -232,9 +233,12 @@ def get_library(self, library):
232233
except (OperationFailure, AutoReconnect), e:
233234
error = e
234235

235-
if error or not lib_type:
236-
raise LibraryNotFoundException("Library %s was not correctly initialized in %s.\nReason: %s" %
236+
if error:
237+
raise LibraryNotFoundException("Library %s was not correctly initialized in %s.\nReason: %r)" %
237238
(library, self, error))
239+
elif not lib_type:
240+
raise LibraryNotFoundException("Library %s was not correctly initialized in %s." %
241+
(library, self))
238242
elif lib_type not in LIBRARY_TYPES:
239243
raise LibraryNotFoundException("Couldn't load LibraryType '%s' for '%s' (has the class been registered?)" %
240244
(lib_type, library))

tests/unit/test_arctic.py

+28-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import cPickle as pickle
22
from mock import patch, MagicMock, sentinel, create_autospec, Mock, call
33
import pytest
4-
from pymongo.errors import OperationFailure
4+
from pymongo.errors import OperationFailure, ServerSelectionTimeoutError, AutoReconnect
55
from pymongo.mongo_client import MongoClient
66

77
from arctic.auth import Credential
@@ -77,10 +77,10 @@ def test_arctic_connect_hostname():
7777
serverSelectionTimeoutMS=sentinel.select_timeout)
7878
# do something to trigger lazy arctic init
7979
store.list_libraries()
80-
ar(mc).assert_called_once_with(host=gmu('hostname'), maxPoolSize=4,
81-
socketTimeoutMS=sentinel.socket_timeout,
82-
connectTimeoutMS=sentinel.connect_timeout,
83-
serverSelectionTimeoutMS=sentinel.select_timeout)
80+
mc.assert_called_once_with(host=gmu('hostname'), maxPoolSize=4,
81+
socketTimeoutMS=sentinel.socket_timeout,
82+
connectTimeoutMS=sentinel.connect_timeout,
83+
serverSelectionTimeoutMS=sentinel.select_timeout)
8484

8585

8686
def test_arctic_connect_with_environment_name():
@@ -94,10 +94,10 @@ def test_arctic_connect_with_environment_name():
9494
# do something to trigger lazy arctic init
9595
store.list_libraries()
9696
assert gmfe.call_args_list == [call('live')]
97-
assert ar(mc).call_args_list == [call(host=gmfe.return_value, maxPoolSize=4,
98-
socketTimeoutMS=sentinel.socket_timeout,
99-
connectTimeoutMS=sentinel.connect_timeout,
100-
serverSelectionTimeoutMS=sentinel.select_timeout)]
97+
assert mc.call_args_list == [call(host=gmfe.return_value, maxPoolSize=4,
98+
socketTimeoutMS=sentinel.socket_timeout,
99+
connectTimeoutMS=sentinel.connect_timeout,
100+
serverSelectionTimeoutMS=sentinel.select_timeout)]
101101

102102

103103
@pytest.mark.parametrize(
@@ -336,3 +336,22 @@ def test_arctic_set_get_state():
336336
assert mnew._socket_timeout == 1234
337337
assert mnew._connect_timeout == 2345
338338
assert mnew._server_selection_timeout == 3456
339+
340+
341+
def test__conn_auth_issue():
342+
auth_timeout = [0]
343+
344+
a = Arctic("host:12345")
345+
sentinel.creds = Mock()
346+
347+
def flaky_auth(*args, **kwargs):
348+
if not auth_timeout[0]:
349+
auth_timeout[0] = 1
350+
raise AutoReconnect()
351+
352+
with patch('arctic.arctic.authenticate', flaky_auth), \
353+
patch('arctic.arctic.get_auth', return_value=sentinel.creds), \
354+
patch('arctic.decorators._handle_error') as he:
355+
a._conn
356+
assert he.call_count == 1
357+
assert auth_timeout[0]

0 commit comments

Comments
 (0)