Pymongo와 몽고디비 샤딩 클러스터 연동 이슈

2020. 2. 13. 16:49서버 프로그래밍

Node.js와 몽고디비 샤딩 클러스터를 연동하여 대량의 데이터를 처리하는 것에 문제가 없었던 탓에, Pymongo 역시 몽고디비 샤딩 클러스터와 연동하는데 별다른 문제가 없을 것으로 예상했으나 다른 점이 존재한다.

일단 단일 몽고 디비 서버에 pymongo로 insert_one을 6만회 이상 수행해도 문제가 없었으나, 샤딩 클러스터를 구성하고 pymongo로 동일한 insert_one을 6만회 이상 수행하려고 하면 7000회 이상 수행을 하면 갑자기 insert_one 함수 부분에서 멈추는 문제가 발생한다.

pymongo를 이용하여 몽고디비에 접속할 때 사용하는 옵션을 바꾸거나, 일부러 디비 커넥션을 매번 연결해본다던지, 세션을 이용한다는 지(샤딩 클러스터에는 세션 이용 불가, 몽고디비 4.2부터 가능하다는 것 같은데 현재 4.0을 세팅해놓아서 포기), 일부러 딜레이를 주는 등 다양하게 테스트를 해보았으나, 계속 임의의 회차에서 멈춰버리는 문제가 계속 발생했다.

Node.js에서는 문제 없이 처리했던 것을 감안하여, 동기 방식으로 처리되는 pymongo의 문제가 아닌지 의심하고 motor와 asyncio를 이용하여 비동기 방식으로 insert를 수행하니 깔끔하게 처리 완료. Python으로 몽고디비 샤딩 클러스터를 연동하여 사용하는 경우가 많을텐데, 이렇게 레퍼런스가 없는 것은 의아함.

https://motor.readthedocs.io/en/stable/tutorial-asyncio.html

 

Tutorial: Using Motor With asyncio — Motor 2.1.0 documentation

A guide to using MongoDB and asyncio with Motor. You can learn about MongoDB with the MongoDB Tutorial before you learn Motor. Using Python 3.4 or later, do: $ python3 -m pip install motor This tutorial assumes that a MongoDB instance is running on the def

motor.readthedocs.io

 

아래는 밤새 삽질시에 참고했던 레퍼런스들...

https://docs.mongodb.com/manual/core/retryable-writes/

 

Retryable Writes — MongoDB Manual

Retryable Writes Retryable writes allow MongoDB drivers to automatically retry certain write operations a single time if they encounter network errors, or if they cannot find a healthy primary in the replica sets or sharded cluster. Prerequisites Retryable

docs.mongodb.com

https://api.mongodb.com/python/current/faq.html

 

Frequently Asked Questions — PyMongo 3.9.0 documentation

PyMongo is not fork-safe. Care must be taken when using instances of MongoClient with fork(). Specifically, instances of MongoClient must not be copied from a parent process to a child process. Instead, the parent process and each child process must create

api.mongodb.com

https://api.mongodb.com/python/current/api/pymongo/collection.html

 

collection – Collection level operations — PyMongo 3.9.0 documentation

collection – Collection level operations Collection level utilities for Mongo. pymongo.ASCENDING = 1 Ascending sort order. pymongo.DESCENDING = -1 Descending sort order. pymongo.GEO2D = '2d' Index specifier for a 2-dimensional geospatial index. pymongo.GEO

api.mongodb.com

https://api.mongodb.com/python/current/api/pymongo/client_session.html

 

client_session – Logical sessions for sequential operations — PyMongo 3.9.0 documentation

client_session – Logical sessions for sequential operations Logical sessions for ordering sequential operations. Requires MongoDB 3.6. Causally Consistent Reads with client.start_session(causal_consistency=True) as session: collection = client.db.collectio

api.mongodb.com

https://stackoverflow.com/questions/43073956/pymongo-errors-bulkwriteerror-batch-op-errors-occurred-mongodb-3-4-2-pymongo

 

pymongo.errors.BulkWriteError: batch op errors occurred (MongoDB 3.4.2, pymongo 3.4.0, python 2.7.13)

I am migrating several hundred million tweets of the format {'id_str': , 'created_at': , 'text': } from text files into MongoDB using pymongo. A collection is created for each user to store his/her

stackoverflow.com