【发布时间】:2020-05-15 18:33:42
【问题描述】:
全部
我刚刚从我们的应用程序中收到一个奇怪的错误:
当我用两个进程更新时,它抱怨一个带有唯一索引的集合出现重复键错误,但有问题的操作是一个 upsert。
案例代码:
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)
当我使用两个进程运行脚本时,Pymongo 异常:
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" }
环境:
mongodb 3.0.5,WiredTiger
单个 mongodb 实例
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
对这里可能出了什么问题有什么想法吗?
PS:
我在 MMAPV1 存储引擎中重试了同样的情况,它工作正常,为什么?
我在这里找到了一些相关的东西: https://jira.mongodb.org/browse/SERVER-18213
但是在这个bug修复之后,它会出现这个错误,所以看起来这个bug没有完全修复。
干杯
【问题讨论】:
-
您已在“时间戳”上创建了唯一索引。我猜你的代码试图插入具有重复时间戳值的文档?
-
是的,我同时用两个进程执行脚本,但是为什么wiredtiger每次更新都不锁定(upsert=True)?如果它在 mmapv1 上运行,一切都很好。
标签: mongodb