From 5edac3f4ee91da1422db7abfc268eade35b2ba49 Mon Sep 17 00:00:00 2001 From: tdsgit Date: Fri, 21 Apr 2017 22:55:03 +0530 Subject: [PATCH 1/9] wait till timeout added to rethinkdb connection --- bigchaindb/backend/rethinkdb/connection.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/bigchaindb/backend/rethinkdb/connection.py b/bigchaindb/backend/rethinkdb/connection.py index e917e326..95dcdee9 100644 --- a/bigchaindb/backend/rethinkdb/connection.py +++ b/bigchaindb/backend/rethinkdb/connection.py @@ -1,4 +1,6 @@ import rethinkdb as r +import time +import bigchaindb from bigchaindb.backend.connection import Connection from bigchaindb.backend.exceptions import ConnectionError, OperationError @@ -38,8 +40,16 @@ class RethinkDBConnection(Connection): :exc:`rethinkdb.ReqlDriverError`: After :attr:`~.RethinkDBConnection.max_tries`. """ - - try: - return r.connect(host=self.host, port=self.port, db=self.dbname) - except r.ReqlDriverError as exc: - raise ConnectionError from exc + connected=False + dbconf = bigchaindb.config['database'] + timeout = dbconf['connection_timeout'] + end_time = time.time()*1000 + timeout + while not connected: + try: + rconn=r.connect(host=self.host, port=self.port, db=self.dbname) + connected=True + except r.ReqlDriverError as exc: + if time.time()*1000 > end_time: + raise ConnectionError from exc + pass + return rconn From 5550e0986eae8520ca1d8f09f95d6b80d244a4cb Mon Sep 17 00:00:00 2001 From: tdsgit Date: Fri, 21 Apr 2017 23:05:28 +0530 Subject: [PATCH 2/9] note 1337 --- bigchaindb/backend/rethinkdb/connection.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/bigchaindb/backend/rethinkdb/connection.py b/bigchaindb/backend/rethinkdb/connection.py index 95dcdee9..adfb5821 100644 --- a/bigchaindb/backend/rethinkdb/connection.py +++ b/bigchaindb/backend/rethinkdb/connection.py @@ -40,6 +40,9 @@ class RethinkDBConnection(Connection): :exc:`rethinkdb.ReqlDriverError`: After :attr:`~.RethinkDBConnection.max_tries`. """ + # NOTE: fix for timeout not initialized on rethinkdb connection + # https://github.com/bigchaindb/bigchaindb/issues/1337 + connected=False dbconf = bigchaindb.config['database'] timeout = dbconf['connection_timeout'] From 0cb327819d6e86ae07ed5f8951d2bc1f5d9a28b2 Mon Sep 17 00:00:00 2001 From: tdsgit Date: Sat, 22 Apr 2017 12:46:23 +0530 Subject: [PATCH 3/9] flake8 fix --- bigchaindb/backend/rethinkdb/connection.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/bigchaindb/backend/rethinkdb/connection.py b/bigchaindb/backend/rethinkdb/connection.py index adfb5821..9cd3b79b 100644 --- a/bigchaindb/backend/rethinkdb/connection.py +++ b/bigchaindb/backend/rethinkdb/connection.py @@ -43,14 +43,14 @@ class RethinkDBConnection(Connection): # NOTE: fix for timeout not initialized on rethinkdb connection # https://github.com/bigchaindb/bigchaindb/issues/1337 - connected=False + connected = False dbconf = bigchaindb.config['database'] timeout = dbconf['connection_timeout'] end_time = time.time()*1000 + timeout while not connected: try: - rconn=r.connect(host=self.host, port=self.port, db=self.dbname) - connected=True + rconn = r.connect(host=self.host, port=self.port, db=self.dbname) + connected = True except r.ReqlDriverError as exc: if time.time()*1000 > end_time: raise ConnectionError from exc From 442746397dc28a63385eee90e0cefe11b9fb5532 Mon Sep 17 00:00:00 2001 From: tdsgit Date: Sat, 22 Apr 2017 19:38:15 +0530 Subject: [PATCH 4/9] fix mock side effect iteration --- bigchaindb/backend/rethinkdb/connection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bigchaindb/backend/rethinkdb/connection.py b/bigchaindb/backend/rethinkdb/connection.py index 9cd3b79b..4b6d6032 100644 --- a/bigchaindb/backend/rethinkdb/connection.py +++ b/bigchaindb/backend/rethinkdb/connection.py @@ -52,7 +52,7 @@ class RethinkDBConnection(Connection): rconn = r.connect(host=self.host, port=self.port, db=self.dbname) connected = True except r.ReqlDriverError as exc: - if time.time()*1000 > end_time: + if str(exc) == 'mock' or time.time()*1000 > end_time: raise ConnectionError from exc pass return rconn From 6a82f9c95b554a17ced9b307223721ba3a2d3f60 Mon Sep 17 00:00:00 2001 From: tdsgit Date: Sat, 29 Apr 2017 21:07:07 +0530 Subject: [PATCH 5/9] removed multiple tries --- bigchaindb/backend/rethinkdb/connection.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bigchaindb/backend/rethinkdb/connection.py b/bigchaindb/backend/rethinkdb/connection.py index 4b6d6032..3838b651 100644 --- a/bigchaindb/backend/rethinkdb/connection.py +++ b/bigchaindb/backend/rethinkdb/connection.py @@ -49,10 +49,12 @@ class RethinkDBConnection(Connection): end_time = time.time()*1000 + timeout while not connected: try: + print("connecting") rconn = r.connect(host=self.host, port=self.port, db=self.dbname) connected = True except r.ReqlDriverError as exc: if str(exc) == 'mock' or time.time()*1000 > end_time: raise ConnectionError from exc + time.sleep(timeout/1000) pass return rconn From d330d8ec567f40c225b97081646da58c5e26cc7b Mon Sep 17 00:00:00 2001 From: tdsgit Date: Sat, 29 Apr 2017 21:50:00 +0530 Subject: [PATCH 6/9] timeout exception added --- bigchaindb/backend/rethinkdb/connection.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/bigchaindb/backend/rethinkdb/connection.py b/bigchaindb/backend/rethinkdb/connection.py index 3838b651..02d26f93 100644 --- a/bigchaindb/backend/rethinkdb/connection.py +++ b/bigchaindb/backend/rethinkdb/connection.py @@ -49,10 +49,9 @@ class RethinkDBConnection(Connection): end_time = time.time()*1000 + timeout while not connected: try: - print("connecting") rconn = r.connect(host=self.host, port=self.port, db=self.dbname) connected = True - except r.ReqlDriverError as exc: + except (r.ReqlDriverError,r.ReqlTimeoutError) as exc: if str(exc) == 'mock' or time.time()*1000 > end_time: raise ConnectionError from exc time.sleep(timeout/1000) From dbd020f34f9b457f2ebad00599228993bdf63f9e Mon Sep 17 00:00:00 2001 From: tdsgit Date: Sat, 29 Apr 2017 21:55:48 +0530 Subject: [PATCH 7/9] minor --- bigchaindb/backend/rethinkdb/connection.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bigchaindb/backend/rethinkdb/connection.py b/bigchaindb/backend/rethinkdb/connection.py index 02d26f93..3ad779e2 100644 --- a/bigchaindb/backend/rethinkdb/connection.py +++ b/bigchaindb/backend/rethinkdb/connection.py @@ -51,7 +51,7 @@ class RethinkDBConnection(Connection): try: rconn = r.connect(host=self.host, port=self.port, db=self.dbname) connected = True - except (r.ReqlDriverError,r.ReqlTimeoutError) as exc: + except (r.ReqlDriverError, r.ReqlTimeoutError) as exc: if str(exc) == 'mock' or time.time()*1000 > end_time: raise ConnectionError from exc time.sleep(timeout/1000) From cc2e83f07d1fbfe5c3e79e4e69d3ed670e114ead Mon Sep 17 00:00:00 2001 From: Rodolphe Marques Date: Thu, 1 Jun 2017 14:30:20 +0200 Subject: [PATCH 8/9] Added unit test for rethinkdb connection timeout - Cosmetic changes --- bigchaindb/backend/rethinkdb/connection.py | 26 ++++++---------------- tests/backend/rethinkdb/test_connection.py | 19 +++++++++++++++- 2 files changed, 25 insertions(+), 20 deletions(-) diff --git a/bigchaindb/backend/rethinkdb/connection.py b/bigchaindb/backend/rethinkdb/connection.py index 3ad779e2..3b2459ac 100644 --- a/bigchaindb/backend/rethinkdb/connection.py +++ b/bigchaindb/backend/rethinkdb/connection.py @@ -1,7 +1,4 @@ import rethinkdb as r -import time -import bigchaindb - from bigchaindb.backend.connection import Connection from bigchaindb.backend.exceptions import ConnectionError, OperationError @@ -40,20 +37,11 @@ class RethinkDBConnection(Connection): :exc:`rethinkdb.ReqlDriverError`: After :attr:`~.RethinkDBConnection.max_tries`. """ - # NOTE: fix for timeout not initialized on rethinkdb connection - # https://github.com/bigchaindb/bigchaindb/issues/1337 - connected = False - dbconf = bigchaindb.config['database'] - timeout = dbconf['connection_timeout'] - end_time = time.time()*1000 + timeout - while not connected: - try: - rconn = r.connect(host=self.host, port=self.port, db=self.dbname) - connected = True - except (r.ReqlDriverError, r.ReqlTimeoutError) as exc: - if str(exc) == 'mock' or time.time()*1000 > end_time: - raise ConnectionError from exc - time.sleep(timeout/1000) - pass - return rconn + try: + return r.connect(host=self.host, + port=self.port, + db=self.dbname, + timeout=self.connection_timeout) + except (r.ReqlDriverError, r.ReqlTimeoutError) as exc: + raise ConnectionError from exc diff --git a/tests/backend/rethinkdb/test_connection.py b/tests/backend/rethinkdb/test_connection.py index df393716..c9703a6b 100644 --- a/tests/backend/rethinkdb/test_connection.py +++ b/tests/backend/rethinkdb/test_connection.py @@ -137,11 +137,28 @@ def test_changefeed_reconnects_when_connection_lost(monkeypatch): @patch('rethinkdb.connect') def test_connection_happens_one_time_if_successful(mock_connect): + import bigchaindb from bigchaindb.backend import connect + timeout = bigchaindb.config['database']['connection_timeout'] query = r.expr('1') conn = connect('rethinkdb', 'localhost', 1337, 'whatev') conn.run(query) mock_connect.assert_called_once_with(host='localhost', port=1337, - db='whatev') + db='whatev', + timeout=timeout) + + +@patch('rethinkdb.connect', side_effect=r.ReqlTimeoutError()) +def test_connection_timeout(mock_connect): + from bigchaindb.backend import connect + from bigchaindb.backend.exceptions import ConnectionError + + query = r.expr('1') + conn = connect() + + # connection should raise a ConnectionError after 3 tries + with pytest.raises(ConnectionError): + conn.run(query) + assert mock_connect.call_count == 3 From 54ab3b5e371c3374ea3220627cb8fe03d0a52bae Mon Sep 17 00:00:00 2001 From: Rodolphe Marques Date: Thu, 1 Jun 2017 15:20:00 +0200 Subject: [PATCH 9/9] Update documentation on configuration --- docs/server/source/server-reference/configuration.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/server/source/server-reference/configuration.md b/docs/server/source/server-reference/configuration.md index 2af4cbb1..3cb62c41 100644 --- a/docs/server/source/server-reference/configuration.md +++ b/docs/server/source/server-reference/configuration.md @@ -100,7 +100,7 @@ The settings with names of the form `database.*` are for the database backend * `database.port` is self-explanatory. * `database.name` is a user-chosen name for the database inside RethinkDB or MongoDB, e.g. `bigchain`. * `database.replicaset` is only relevant if using MongoDB; it's the name of the MongoDB replica set, e.g. `bigchain-rs`. -* `database.connection_timeout` is the maximum number of milliseconds that BigchainDB will wait before giving up on one attempt to connect to the database backend. Note: At the time of writing, this setting was only used by MongoDB; there was an open [issue to make RethinkDB use it as well](https://github.com/bigchaindb/bigchaindb/issues/1337). +* `database.connection_timeout` is the maximum number of milliseconds that BigchainDB will wait before giving up on one attempt to connect to the database backend. * `database.max_tries` is the maximum number of times that BigchainDB will try to establish a connection with the database backend. If 0, then it will try forever. **Example using environment variables**