14

all

I just got a weird error sent through from our applcation:

when i updated with two processes, it was complaining of a duplicate key error on a collection with a unique index on it, but the operation in question was an upsert.

case code:

import time
from bson import Binary
from pymongo import MongoClient, DESCENDING

bucket = MongoClient('127.0.0.1', 27017)['test']['foo']
bucket.drop()
bucket.update({'timestamp': 0}, {'$addToSet': {'_exists_caps': 'cap15'}}, upsert=True, safe=True, w=1, wtimeout=10)
bucket.create_index([('timestamp', DESCENDING)], unique=True)
while True:
    timestamp =  str(int(1000000 * time.time()))
    bucket.update({'timestamp': timestamp}, {'$addToSet': {'_exists_foos': 'fooxxxxx'}}, upsert=True, safe=True, w=1, wtimeout=10)

When i run script with two processes, Pymongo Exception:

Traceback (most recent call last):
  File "test_mongo_update.py", line 11, in <module>
    bucket.update({'timestamp': timestamp}, {'$addToSet': {'_exists_foos': 'fooxxxxx'}}, upsert=True, safe=True, w=1, wtimeout=10)
  File "build/bdist.linux-x86_64/egg/pymongo/collection.py", line 552, in update
  File "build/bdist.linux-x86_64/egg/pymongo/helpers.py", line 202, in _check_write_command_response
pymongo.errors.DuplicateKeyError: E11000 duplicate key error collection: test.foo index: timestamp_-1 dup key: { : "1439374020348044" }

Env:

  • mongodb 3.0.5, WiredTiger

  • single mongodb instance

  • pymongo 2.8.1

mongo.conf

systemLog:
   destination: file
   logAppend: true
   logRotate: reopen
   path: /opt/lib/log/mongod.log

# Where and how to store data.
storage:
   dbPath: /opt/lib/mongo
   journal:
     enabled: true

   engine: "wiredTiger"
   directoryPerDB: true

# how the process runs
processManagement:
   fork: true  # fork and run in background
   pidFilePath: /opt/lib/mongo/mongod.pid

# network interfaces
net:
   port: 27017
   bindIp: 0.0.0.0  # Listen to local interface only, comment to listen on all interfaces.

setParameter:
   enableLocalhostAuthBypass: false

Any thoughts on what could be going wrong here?

PS:

I retried the same case in MMAPV1 storage engine, it works fine, why?

I found something related here: https://jira.mongodb.org/browse/SERVER-18213

but after this bug fix, it cases this error, so it looks like this bug is not fixed completely.

Cheers

2
  • you've created a unique index on 'timestamp'. I'd guess your code has attempted to insert documents with duplicate timestamp values? Aug 12, 2015 at 16:41
  • Yes, I execute the script with two processes at the same time, but why wiredtiger doesn't lock for each update(upsert=True)? if it runs on mmapv1, it's all fine. Aug 12, 2015 at 23:52

4 Answers 4

9

I found the bug at: https://jira.mongodb.org/browse/SERVER-14322

Please feel free to vote for it and watch it for further updates.

8

An upsert does both a check for an existing document to update, or inserts a new document.

My best guess is you are running into a timing issue where:

  1. Process 2 checks for existence, which it doesn't
  2. Process 1 checks for existence, which it doesn't
  3. Process 2 inserts, which works
  4. Process 1 inserts, which raises dupe key.

Check what native query your python library is sending underneath first. Confirm it's what you expect on the native mongo side. Then if you can reproduce this semi regularly on wiredtiger but never on mmap, raise a bug with mongo to confirm what their expected behavior is. It's sometimes hard to pick what they guarantee to be atomic.

This is a good example of why Mongo ObjectIDs combine a timestamp, a machine id, a pid and a counter for uniqueness.

1
  • Thanks matt. That makes two of us. I think update(upsert=true) will be atomic, so i think it's a bug.Maybe i need to take a ticket for mongo. Aug 13, 2015 at 7:33
1

http://docs.mongodb.org/manual/core/storage/

With WiredTiger, all write operations happen within the context of a document level lock. As a result, multiple clients can modify more than one document in a single collection at the same time.

Your multiple clients can simultaneously update the collection. Wiredtiger will lock the document you're updating, rather than the collection.

4
  • so why wiredtiger raise DuplicateKeyError when i update with upsert? Aug 13, 2015 at 6:31
  • because there is another entry already in the collection with the same timestamp value as the one you're trying to add. Aug 13, 2015 at 6:56
  • 1
    That's what an upsert is meant to handle, update existing or insert
    – Matt
    Aug 13, 2015 at 7:01
  • hi,robjwilkins and Matt,thanks for your apply. I execute update(upsert=True .....),i think mongo will lock when i modify the document at the same time, so it'will update twice, not raise duplicatekey error. anything wrong? And i tested in MMAPv1 storage engine, it works fine. Aug 13, 2015 at 7:04
0

I use MongoDB to manage directory tree data for my storage system. I had the same issue: the update command with upsert=True sometimes creates duplicate documents.

But I used MMAPv1, I don't known what happen when use WiredTiger engine. I think it is a race condition problem of MongoDB. Finally, I decided to code a locking mechanism (document lock level) with python-redis-lock. It is working perfectly!

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.