pool.py 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608
  1. # Copyright 2011-2015 MongoDB, Inc.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License"); you
  4. # may not use this file except in compliance with the License. You
  5. # may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
  12. # implied. See the License for the specific language governing
  13. # permissions and limitations under the License.
  14. import contextlib
  15. import os
  16. import socket
  17. import threading
  18. from bson import DEFAULT_CODEC_OPTIONS
  19. from bson.py3compat import u, itervalues
  20. from pymongo import auth, helpers, thread_util
  21. from pymongo.errors import (AutoReconnect,
  22. ConnectionFailure,
  23. DocumentTooLarge,
  24. NetworkTimeout,
  25. NotMasterError,
  26. OperationFailure)
  27. from pymongo.ismaster import IsMaster
  28. from pymongo.monotonic import time as _time
  29. from pymongo.network import (command,
  30. receive_message,
  31. socket_closed)
  32. from pymongo.read_preferences import ReadPreference
  33. from pymongo.server_type import SERVER_TYPE
  34. # If the first getaddrinfo call of this interpreter's life is on a thread,
  35. # while the main thread holds the import lock, getaddrinfo deadlocks trying
  36. # to import the IDNA codec. Import it here, where presumably we're on the
  37. # main thread, to avoid the deadlock. See PYTHON-607.
  38. u('foo').encode('idna')
  39. try:
  40. from ssl import match_hostname, CertificateError
  41. except ImportError:
  42. # These don't require the ssl module
  43. from pymongo.ssl_match_hostname import match_hostname, CertificateError
  44. def _raise_connection_failure(address, error):
  45. """Convert a socket.error to ConnectionFailure and raise it."""
  46. host, port = address
  47. msg = '%s:%d: %s' % (host, port, error)
  48. if isinstance(error, socket.timeout):
  49. raise NetworkTimeout(msg)
  50. else:
  51. raise AutoReconnect(msg)
  52. class PoolOptions(object):
  53. __slots__ = ('__max_pool_size', '__connect_timeout', '__socket_timeout',
  54. '__wait_queue_timeout', '__wait_queue_multiple',
  55. '__ssl_context', '__ssl_match_hostname', '__socket_keepalive')
  56. def __init__(self, max_pool_size=100, connect_timeout=None,
  57. socket_timeout=None, wait_queue_timeout=None,
  58. wait_queue_multiple=None, ssl_context=None,
  59. ssl_match_hostname=True, socket_keepalive=False):
  60. self.__max_pool_size = max_pool_size
  61. self.__connect_timeout = connect_timeout
  62. self.__socket_timeout = socket_timeout
  63. self.__wait_queue_timeout = wait_queue_timeout
  64. self.__wait_queue_multiple = wait_queue_multiple
  65. self.__ssl_context = ssl_context
  66. self.__ssl_match_hostname = ssl_match_hostname
  67. self.__socket_keepalive = socket_keepalive
  68. @property
  69. def max_pool_size(self):
  70. """The maximum number of connections that the pool will open
  71. simultaneously. If this is set, operations will block if there
  72. are `max_pool_size` outstanding connections.
  73. """
  74. return self.__max_pool_size
  75. @property
  76. def connect_timeout(self):
  77. """How long a connection can take to be opened before timing out.
  78. """
  79. return self.__connect_timeout
  80. @property
  81. def socket_timeout(self):
  82. """How long a send or receive on a socket can take before timing out.
  83. """
  84. return self.__socket_timeout
  85. @property
  86. def wait_queue_timeout(self):
  87. """How long a thread will wait for a socket from the pool if the pool
  88. has no free sockets.
  89. """
  90. return self.__wait_queue_timeout
  91. @property
  92. def wait_queue_multiple(self):
  93. """Multiplied by max_pool_size to give the number of threads allowed
  94. to wait for a socket at one time.
  95. """
  96. return self.__wait_queue_multiple
  97. @property
  98. def ssl_context(self):
  99. """An SSLContext instance or None.
  100. """
  101. return self.__ssl_context
  102. @property
  103. def ssl_match_hostname(self):
  104. """Call ssl.match_hostname if cert_reqs is not ssl.CERT_NONE.
  105. """
  106. return self.__ssl_match_hostname
  107. @property
  108. def socket_keepalive(self):
  109. """Whether to send periodic messages to determine if a connection
  110. is closed.
  111. """
  112. return self.__socket_keepalive
  113. class SocketInfo(object):
  114. """Store a socket with some metadata.
  115. :Parameters:
  116. - `sock`: a raw socket object
  117. - `pool`: a Pool instance
  118. - `ismaster`: optional IsMaster instance, response to ismaster on `sock`
  119. - `address`: the server's (host, port)
  120. """
  121. def __init__(self, sock, pool, ismaster, address):
  122. self.sock = sock
  123. self.address = address
  124. self.authset = set()
  125. self.closed = False
  126. self.last_checkout = _time()
  127. self.is_writable = ismaster.is_writable if ismaster else None
  128. self.max_wire_version = ismaster.max_wire_version if ismaster else None
  129. self.max_bson_size = ismaster.max_bson_size if ismaster else None
  130. self.max_message_size = ismaster.max_message_size if ismaster else None
  131. self.max_write_batch_size = (
  132. ismaster.max_write_batch_size if ismaster else None)
  133. if ismaster:
  134. self.is_mongos = ismaster.server_type == SERVER_TYPE.Mongos
  135. else:
  136. self.is_mongos = None
  137. # The pool's pool_id changes with each reset() so we can close sockets
  138. # created before the last reset.
  139. self.pool_id = pool.pool_id
  140. def command(self, dbname, spec, slave_ok=False,
  141. read_preference=ReadPreference.PRIMARY,
  142. codec_options=DEFAULT_CODEC_OPTIONS, check=True,
  143. allowable_errors=None):
  144. """Execute a command or raise ConnectionFailure or OperationFailure.
  145. :Parameters:
  146. - `dbname`: name of the database on which to run the command
  147. - `spec`: a command document as a dict, SON, or mapping object
  148. - `slave_ok`: whether to set the SlaveOkay wire protocol bit
  149. - `read_preference`: a read preference
  150. - `codec_options`: a CodecOptions instance
  151. - `check`: raise OperationFailure if there are errors
  152. - `allowable_errors`: errors to ignore if `check` is True
  153. """
  154. try:
  155. return command(self.sock, dbname, spec,
  156. slave_ok, self.is_mongos, read_preference,
  157. codec_options, check, allowable_errors)
  158. except OperationFailure:
  159. raise
  160. # Catch socket.error, KeyboardInterrupt, etc. and close ourselves.
  161. except BaseException as error:
  162. self._raise_connection_failure(error)
  163. def send_message(self, message, max_doc_size):
  164. """Send a raw BSON message or raise ConnectionFailure.
  165. If a network exception is raised, the socket is closed.
  166. """
  167. if (self.max_bson_size is not None
  168. and max_doc_size > self.max_bson_size):
  169. raise DocumentTooLarge(
  170. "BSON document too large (%d bytes) - the connected server"
  171. "supports BSON document sizes up to %d bytes." %
  172. (max_doc_size, self.max_bson_size))
  173. try:
  174. self.sock.sendall(message)
  175. except BaseException as error:
  176. self._raise_connection_failure(error)
  177. def receive_message(self, operation, request_id):
  178. """Receive a raw BSON message or raise ConnectionFailure.
  179. If any exception is raised, the socket is closed.
  180. """
  181. try:
  182. return receive_message(self.sock, operation, request_id)
  183. except BaseException as error:
  184. self._raise_connection_failure(error)
  185. def legacy_write(self, request_id, msg, max_doc_size, with_last_error):
  186. """Send OP_INSERT, etc., optionally returning response as a dict.
  187. Can raise ConnectionFailure or OperationFailure.
  188. :Parameters:
  189. - `request_id`: an int.
  190. - `msg`: bytes, an OP_INSERT, OP_UPDATE, or OP_DELETE message,
  191. perhaps with a getlasterror command appended.
  192. - `max_doc_size`: size in bytes of the largest document in `msg`.
  193. - `with_last_error`: True if a getlasterror command is appended.
  194. """
  195. if not with_last_error and not self.is_writable:
  196. # Write won't succeed, bail as if we'd done a getlasterror.
  197. raise NotMasterError("not master")
  198. self.send_message(msg, max_doc_size)
  199. if with_last_error:
  200. response = self.receive_message(1, request_id)
  201. return helpers._check_gle_response(response)
  202. def write_command(self, request_id, msg):
  203. """Send "insert" etc. command, returning response as a dict.
  204. Can raise ConnectionFailure or OperationFailure.
  205. :Parameters:
  206. - `request_id`: an int.
  207. - `msg`: bytes, the command message.
  208. """
  209. self.send_message(msg, 0)
  210. response = helpers._unpack_response(self.receive_message(1, request_id))
  211. assert response['number_returned'] == 1
  212. result = response['data'][0]
  213. # Raises NotMasterError or OperationFailure.
  214. helpers._check_command_response(result)
  215. return result
  216. def check_auth(self, all_credentials):
  217. """Update this socket's authentication.
  218. Log in or out to bring this socket's credentials up to date with
  219. those provided. Can raise ConnectionFailure or OperationFailure.
  220. :Parameters:
  221. - `all_credentials`: dict, maps auth source to MongoCredential.
  222. """
  223. if all_credentials or self.authset:
  224. cached = set(itervalues(all_credentials))
  225. authset = self.authset.copy()
  226. # Logout any credentials that no longer exist in the cache.
  227. for credentials in authset - cached:
  228. auth.logout(credentials.source, self)
  229. self.authset.discard(credentials)
  230. for credentials in cached - authset:
  231. auth.authenticate(credentials, self)
  232. self.authset.add(credentials)
  233. def authenticate(self, credentials):
  234. """Log in to the server and store these credentials in `authset`.
  235. Can raise ConnectionFailure or OperationFailure.
  236. :Parameters:
  237. - `credentials`: A MongoCredential.
  238. """
  239. auth.authenticate(credentials, self)
  240. self.authset.add(credentials)
  241. def close(self):
  242. self.closed = True
  243. # Avoid exceptions on interpreter shutdown.
  244. try:
  245. self.sock.close()
  246. except:
  247. pass
  248. def _raise_connection_failure(self, error):
  249. # Catch *all* exceptions from socket methods and close the socket. In
  250. # regular Python, socket operations only raise socket.error, even if
  251. # the underlying cause was a Ctrl-C: a signal raised during socket.recv
  252. # is expressed as an EINTR error from poll. See internal_select_ex() in
  253. # socketmodule.c. All error codes from poll become socket.error at
  254. # first. Eventually in PyEval_EvalFrameEx the interpreter checks for
  255. # signals and throws KeyboardInterrupt into the current frame on the
  256. # main thread.
  257. #
  258. # But in Gevent and Eventlet, the polling mechanism (epoll, kqueue,
  259. # ...) is called in Python code, which experiences the signal as a
  260. # KeyboardInterrupt from the start, rather than as an initial
  261. # socket.error, so we catch that, close the socket, and reraise it.
  262. self.close()
  263. if isinstance(error, socket.error):
  264. _raise_connection_failure(self.address, error)
  265. else:
  266. raise error
  267. def __eq__(self, other):
  268. return self.sock == other.sock
  269. def __ne__(self, other):
  270. return not self == other
  271. def __hash__(self):
  272. return hash(self.sock)
  273. def __repr__(self):
  274. return "SocketInfo(%s)%s at %s" % (
  275. repr(self.sock),
  276. self.closed and " CLOSED" or "",
  277. id(self)
  278. )
  279. def _create_connection(address, options):
  280. """Given (host, port) and PoolOptions, connect and return a socket object.
  281. Can raise socket.error.
  282. This is a modified version of create_connection from CPython >= 2.6.
  283. """
  284. host, port = address
  285. # Check if dealing with a unix domain socket
  286. if host.endswith('.sock'):
  287. if not hasattr(socket, "AF_UNIX"):
  288. raise ConnectionFailure("UNIX-sockets are not supported "
  289. "on this system")
  290. sock = socket.socket(socket.AF_UNIX)
  291. try:
  292. sock.connect(host)
  293. return sock
  294. except socket.error:
  295. sock.close()
  296. raise
  297. # Don't try IPv6 if we don't support it. Also skip it if host
  298. # is 'localhost' (::1 is fine). Avoids slow connect issues
  299. # like PYTHON-356.
  300. family = socket.AF_INET
  301. if socket.has_ipv6 and host != 'localhost':
  302. family = socket.AF_UNSPEC
  303. err = None
  304. for res in socket.getaddrinfo(host, port, family, socket.SOCK_STREAM):
  305. af, socktype, proto, dummy, sa = res
  306. sock = socket.socket(af, socktype, proto)
  307. try:
  308. sock.setsockopt(socket.IPPROTO_TCP, socket.TCP_NODELAY, 1)
  309. sock.settimeout(options.connect_timeout)
  310. sock.setsockopt(socket.SOL_SOCKET, socket.SO_KEEPALIVE,
  311. options.socket_keepalive)
  312. sock.connect(sa)
  313. return sock
  314. except socket.error as e:
  315. err = e
  316. sock.close()
  317. if err is not None:
  318. raise err
  319. else:
  320. # This likely means we tried to connect to an IPv6 only
  321. # host with an OS/kernel or Python interpreter that doesn't
  322. # support IPv6. The test case is Jython2.5.1 which doesn't
  323. # support IPv6 at all.
  324. raise socket.error('getaddrinfo failed')
  325. def _configured_socket(address, options):
  326. """Given (host, port) and PoolOptions, return a configured socket.
  327. Can raise socket.error, ConnectionFailure, or CertificateError.
  328. Sets socket's SSL and timeout options.
  329. """
  330. sock = _create_connection(address, options)
  331. ssl_context = options.ssl_context
  332. if ssl_context is not None:
  333. try:
  334. sock = ssl_context.wrap_socket(sock)
  335. except IOError as exc:
  336. sock.close()
  337. raise ConnectionFailure("SSL handshake failed: %s" % (str(exc),))
  338. if ssl_context.verify_mode and options.ssl_match_hostname:
  339. try:
  340. match_hostname(sock.getpeercert(), hostname=address[0])
  341. except CertificateError:
  342. sock.close()
  343. raise
  344. sock.settimeout(options.socket_timeout)
  345. return sock
  346. # Do *not* explicitly inherit from object or Jython won't call __del__
  347. # http://bugs.jython.org/issue1057
  348. class Pool:
  349. def __init__(self, address, options, handshake=True):
  350. """
  351. :Parameters:
  352. - `address`: a (hostname, port) tuple
  353. - `options`: a PoolOptions instance
  354. - `handshake`: whether to call ismaster for each new SocketInfo
  355. """
  356. # Check a socket's health with socket_closed() every once in a while.
  357. # Can override for testing: 0 to always check, None to never check.
  358. self._check_interval_seconds = 1
  359. self.sockets = set()
  360. self.lock = threading.Lock()
  361. # Keep track of resets, so we notice sockets created before the most
  362. # recent reset and close them.
  363. self.pool_id = 0
  364. self.pid = os.getpid()
  365. self.address = address
  366. self.opts = options
  367. self.handshake = handshake
  368. if (self.opts.wait_queue_multiple is None or
  369. self.opts.max_pool_size is None):
  370. max_waiters = None
  371. else:
  372. max_waiters = (
  373. self.opts.max_pool_size * self.opts.wait_queue_multiple)
  374. self._socket_semaphore = thread_util.create_semaphore(
  375. self.opts.max_pool_size, max_waiters)
  376. def reset(self):
  377. with self.lock:
  378. self.pool_id += 1
  379. self.pid = os.getpid()
  380. sockets, self.sockets = self.sockets, set()
  381. for sock_info in sockets:
  382. sock_info.close()
  383. def connect(self):
  384. """Connect to Mongo and return a new SocketInfo.
  385. Can raise ConnectionFailure or CertificateError.
  386. Note that the pool does not keep a reference to the socket -- you
  387. must call return_socket() when you're done with it.
  388. """
  389. sock = None
  390. try:
  391. sock = _configured_socket(self.address, self.opts)
  392. if self.handshake:
  393. ismaster = IsMaster(command(sock, 'admin', {'ismaster': 1},
  394. False, False,
  395. ReadPreference.PRIMARY,
  396. DEFAULT_CODEC_OPTIONS))
  397. else:
  398. ismaster = None
  399. return SocketInfo(sock, self, ismaster, self.address)
  400. except socket.error as error:
  401. if sock is not None:
  402. sock.close()
  403. _raise_connection_failure(self.address, error)
  404. @contextlib.contextmanager
  405. def get_socket(self, all_credentials, checkout=False):
  406. """Get a socket from the pool. Use with a "with" statement.
  407. Returns a :class:`SocketInfo` object wrapping a connected
  408. :class:`socket.socket`.
  409. This method should always be used in a with-statement::
  410. with pool.get_socket(credentials, checkout) as socket_info:
  411. socket_info.send_message(msg)
  412. data = socket_info.receive_message(op_code, request_id)
  413. The socket is logged in or out as needed to match ``all_credentials``
  414. using the correct authentication mechanism for the server's wire
  415. protocol version.
  416. Can raise ConnectionFailure or OperationFailure.
  417. :Parameters:
  418. - `all_credentials`: dict, maps auth source to MongoCredential.
  419. - `checkout` (optional): keep socket checked out.
  420. """
  421. # First get a socket, then attempt authentication. Simplifies
  422. # semaphore management in the face of network errors during auth.
  423. sock_info = self._get_socket_no_auth()
  424. try:
  425. sock_info.check_auth(all_credentials)
  426. yield sock_info
  427. except:
  428. # Exception in caller. Decrement semaphore.
  429. self.return_socket(sock_info)
  430. raise
  431. else:
  432. if not checkout:
  433. self.return_socket(sock_info)
  434. def _get_socket_no_auth(self):
  435. """Get or create a SocketInfo. Can raise ConnectionFailure."""
  436. # We use the pid here to avoid issues with fork / multiprocessing.
  437. # See test.test_client:TestClient.test_fork for an example of
  438. # what could go wrong otherwise
  439. if self.pid != os.getpid():
  440. self.reset()
  441. # Get a free socket or create one.
  442. if not self._socket_semaphore.acquire(
  443. True, self.opts.wait_queue_timeout):
  444. self._raise_wait_queue_timeout()
  445. # We've now acquired the semaphore and must release it on error.
  446. try:
  447. try:
  448. # set.pop() isn't atomic in Jython less than 2.7, see
  449. # http://bugs.jython.org/issue1854
  450. with self.lock:
  451. sock_info, from_pool = self.sockets.pop(), True
  452. except KeyError:
  453. # Can raise ConnectionFailure or CertificateError.
  454. sock_info, from_pool = self.connect(), False
  455. if from_pool:
  456. # Can raise ConnectionFailure.
  457. sock_info = self._check(sock_info)
  458. except:
  459. self._socket_semaphore.release()
  460. raise
  461. sock_info.last_checkout = _time()
  462. return sock_info
  463. def return_socket(self, sock_info):
  464. """Return the socket to the pool, or if it's closed discard it."""
  465. if self.pid != os.getpid():
  466. self.reset()
  467. else:
  468. if sock_info.pool_id != self.pool_id:
  469. sock_info.close()
  470. elif not sock_info.closed:
  471. with self.lock:
  472. self.sockets.add(sock_info)
  473. self._socket_semaphore.release()
  474. def _check(self, sock_info):
  475. """This side-effecty function checks if this pool has been reset since
  476. the last time this socket was used, or if the socket has been closed by
  477. some external network error, and if so, attempts to create a new socket.
  478. If this connection attempt fails we reset the pool and reraise the
  479. ConnectionFailure.
  480. Checking sockets lets us avoid seeing *some*
  481. :class:`~pymongo.errors.AutoReconnect` exceptions on server
  482. hiccups, etc. We only do this if it's been > 1 second since
  483. the last socket checkout, to keep performance reasonable - we
  484. can't avoid AutoReconnects completely anyway.
  485. """
  486. error = False
  487. # How long since socket was last checked out.
  488. age = _time() - sock_info.last_checkout
  489. if (self._check_interval_seconds is not None
  490. and (
  491. 0 == self._check_interval_seconds
  492. or age > self._check_interval_seconds)):
  493. if socket_closed(sock_info.sock):
  494. sock_info.close()
  495. error = True
  496. if not error:
  497. return sock_info
  498. else:
  499. return self.connect()
  500. def _raise_wait_queue_timeout(self):
  501. raise ConnectionFailure(
  502. 'Timed out waiting for socket from pool with max_size %r and'
  503. ' wait_queue_timeout %r' % (
  504. self.opts.max_pool_size, self.opts.wait_queue_timeout))
  505. def __del__(self):
  506. # Avoid ResourceWarnings in Python 3
  507. for sock_info in self.sockets:
  508. sock_info.close()