MongoClient exception on collection insert

372 views
Skip to first unread message

Umesh Deshpande

unread,
Mar 17, 2017, 4:09:38 PM3/17/17
to mongodb-user
Hi,

I'm running a sharded cluster. In my python program, I connect to the mongos and insert documents. While it's writing documents in a loop, I kill the primary shard server. I expected the insert to throw the ServerSelectionTimeoutError exception, but it immediately throws an OperationFailure exception. How can I make the driver to wait for 5 seconds, until the serverSelectionTimeoutMS=5000 expires?

Here's the code snippet which threw the exception.

connection = pymongo.MongoClient("mongodb://localhost:27019", serverSelectionTimeoutMS=5000, connectTimeoutMS=500)
....
while True:
  try:
    collection.insert(batch)
    print('inserted doc batch')
  except pymongo.errors.ServerSelectionTimeoutError:
    print('timeout error')
    continue
  except pymongo.errors.OperationFailure as err:
    print('operation failure: %s' % err)
    continue


If I catch the pymongo.errors.OperationFailure exception and print the error. It shows the following message, where host1:27018 is the killed primary.
write results unavailable from host1:27018 :: caused by :: Location11002: socket exception [CONNECT_ERROR] for host1:27018

If I let it fail, without catching exception.
Unexpected error: <class 'pymongo.errors.WriteError'> , for index 349999
Traceback (most recent call last):
File "loaddb.py", line 46, in <module>
collection.insert(batch)
File "/usr/local/lib/python3.5/dist-packages/pymongo/collection.py", line 2469, in insert
check_keys, manipulate, write_concern)
File "/usr/local/lib/python3.5/dist-packages/pymongo/collection.py", line 611, in _insert
_check_write_command_response(results)
File "/usr/local/lib/python3.5/dist-packages/pymongo/helpers.py", line 315, in _check_write_command_response
raise WriteError(error.get("errmsg"), error.get("code"), error)
pymongo.errors.WriteError: interrupted at shutdown

Thanks,
Umesh



Bernie Hackett

unread,
Mar 20, 2017, 4:43:34 PM3/20/17
to mongodb-user
I responded to your previous post on this topic.
Reply all
Reply to author
Forward
0 new messages