Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 tweets. The insertion method I am using is insert_many(). It often runs into BulkWriteError.

Traceback (most recent call last):
  File "pipeline.py", line 105, in <module>
    timeline_db, meta_db, negative_db, log_col, dir_path)
  File "/media/haitao/Storage/twitter_pipeline/migrate_old.py", line 134, in migrate_dir
    timeline_db[user_id].insert_many(utility.temporal_sort(statuses))
  File "/home/haitao/anaconda3/envs/py27/lib/python2.7/site-packages/pymongo/collection.py", line 711, in insert_many
    blk.execute(self.write_concern.document)
  File "/home/haitao/anaconda3/envs/py27/lib/python2.7/site-packages/pymongo/bulk.py", line 493, in execute
    return self.execute_command(sock_info, generator, write_concern)
  File "/home/haitao/anaconda3/envs/py27/lib/python2.7/site-packages/pymongo/bulk.py", line 331, in execute_command
    raise BulkWriteError(full_result)
pymongo.errors.BulkWriteError: batch op errors occurred

This error seems to occur when there are duplicates of keys, which should not be the case for here. Are there other things that I can check to solve this issue?

Thanks in advance!

like image 697
htcai Avatar asked Mar 28 '17 15:03

htcai


2 Answers

sorry for the delay.

1) I replicated the error. The following is close to the end of the mongod.log.

I -        [ftdc] Assertion: 13538:couldn't open [/proc/5774/stat] errno:24 Too many open
files
W FTDC     [ftdc] Uncaught exception in 'Location13538: couldn't open [/proc/5774/stat] 
errno:24 Too many open files' in full-time diagnostic data capture subsystem. Shutting down 
the full-time diagnostic data capture subsystem.
E STORAGE  [conn2] WiredTiger (24) [1491020356:127332][5774:0x7f6f30e9d700], WT_SESSION
.create: /var/lib/mongodb/: directory-sync: open: Too many open files
I COMMAND  [conn2] command timeline_db.231731006 command: insert { insert: "231731006", 
ordered: true, documents: 1000 } ninserted:0 keyUpdates:0 writeConflicts:0 exception: 24: 
Too many open files code:8 numYields:0 reslen:123 locks:{ Global: { acquireCount: { r: 2, 
w: 2 } }, Database: { acquireCount: { w: 1, W: 1 } }, Collection: { acquireCount: { w: 1, 
W: 1 } } } protocol:op_query 511ms```

2) Yes, only one instance of MongoClient() is passed around.

3) No multi-processing was run.

After I posted the initial question, I started to use insert_one() which explicitly raised open file limit error. I changed the design of the database (mainly, reduced the number of collections) and solved the issue about open file limit. I am not sure, but the log seems to suggest that the actual cause of the BulkWriteError is also open file limit.

like image 153
htcai Avatar answered Oct 05 '22 11:10

htcai


For me, the error was occurring because of replication in _id during second time.

So if I used say items to add in Mongo, pymongo automatically inserts object_id aka _id and then if items are global, it will have the _id from the previous operation, which causes redundancy and ultimately BulkWriteError.

Clearing the cache solved it.

like image 26
jatin gupta Avatar answered Oct 05 '22 11:10

jatin gupta