Skip to content

Commit f29544b

Browse files
committed
Ensure connection before we insert PYTHON-414
Inserts require that we know the max_bson_size and max_message_size of the current primary or master. We have to be connected to get those values.
1 parent c573646 commit f29544b

File tree

5 files changed

+43
-6
lines changed

5 files changed

+43
-6
lines changed

pymongo/collection.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -341,6 +341,11 @@ def insert(self, doc_or_docs, manipulate=True,
341341
342342
.. mongodoc:: insert
343343
"""
344+
# Batch inserts require us to know the connected master's
345+
# max_bson_size and max_message_size. We have to be connected
346+
# to a master to know that.
347+
self.database.connection._ensure_connected(True)
348+
344349
docs = doc_or_docs
345350
return_one = False
346351
if isinstance(docs, dict):

pymongo/master_slave_connection.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,11 @@ def set_cursor_manager(self, manager_class):
159159
for slave in self.__slaves:
160160
slave.set_cursor_manager(manager_class)
161161

162+
def _ensure_connected(self, sync):
163+
"""Ensure the master is connected to a mongod/s.
164+
"""
165+
self.__master._ensure_connected(sync)
166+
162167
# _connection_to_use is a hack that we need to include to make sure
163168
# that killcursor operations can be sent to the same instance on which
164169
# the cursor actually resides...

pymongo/mongo_client.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,13 @@ def __socket(self):
778778
raise
779779
return sock_info
780780

781+
def _ensure_connected(self, dummy):
782+
"""Ensure this client instance is connected to a mongod/s.
783+
"""
784+
host, port = (self.__host, self.__port)
785+
if host is None or (port is None and '/' not in host):
786+
self.__find_node()
787+
781788
def disconnect(self):
782789
"""Disconnect from MongoDB.
783790

pymongo/mongo_replica_set_client.py

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1224,6 +1224,22 @@ def __socket(self, member, force=False):
12241224
raise
12251225
return sock_info
12261226

1227+
def _ensure_connected(self, sync=False):
1228+
"""Ensure this client instance is connected to a primary.
1229+
"""
1230+
# This may be the first time we're connecting to the set.
1231+
if self.__monitor and not self.__monitor.started:
1232+
try:
1233+
self.__monitor.start()
1234+
# Minor race condition. It's possible that two (or more)
1235+
# threads could call monitor.start() consecutively. Just pass.
1236+
except RunTimeError:
1237+
pass
1238+
if sync:
1239+
rs_state = self.__rs_state
1240+
if not rs_state.primary_member:
1241+
self.__schedule_refresh(sync)
1242+
12271243
def disconnect(self):
12281244
"""Disconnect from the replica set primary, unpin all members, and
12291245
refresh our view of the replica set.
@@ -1390,9 +1406,7 @@ def _send_message(self, msg,
13901406
- `with_last_error`: check getLastError status after sending the
13911407
message
13921408
"""
1393-
# This may be the first time we're connecting to the set.
1394-
if self.__monitor and not self.__monitor.started:
1395-
self.__monitor.start()
1409+
self._ensure_connected()
13961410

13971411
if _connection_to_use in (None, -1):
13981412
member = self.__find_primary()
@@ -1494,9 +1508,7 @@ def _send_message_with_response(self, msg, _connection_to_use=None,
14941508
used by Cursor for getMore and killCursors messages.
14951509
- `_must_use_master`: If True, send to primary.
14961510
"""
1497-
# This may be the first time we're connecting to the set.
1498-
if self.__monitor and not self.__monitor.started:
1499-
self.__monitor.start()
1511+
self._ensure_connected()
15001512

15011513
rs_state = self.__rs_state
15021514
tag_sets = kwargs.get('tag_sets', [{}])

test/test_replica_set_client.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,14 @@ def test_init_disconnected(self):
126126
c.pymongo_test.test.update({}, {}) # Auto-connect for write.
127127
self.assertTrue(c.primary)
128128

129+
c = self._get_client(_connect=False)
130+
c.pymongo_test.test.insert({}) # Auto-connect for write.
131+
self.assertTrue(c.primary)
132+
133+
c = self._get_client(_connect=False)
134+
c.pymongo_test.test.remove({}) # Auto-connect for write.
135+
self.assertTrue(c.primary)
136+
129137
c = MongoReplicaSetClient(
130138
"somedomainthatdoesntexist.org", replicaSet="rs",
131139
connectTimeoutMS=1, _connect=False)

0 commit comments

Comments
 (0)