From 4ecbd22c1907cd2adcbb83726ee9db36b9710ec0 Mon Sep 17 00:00:00 2001 From: Glenn Maynard Date: Sun, 10 Jun 2012 12:28:19 -0400 Subject: [PATCH] Initialize ObjectId._inc to a random value. This is required to prevent collisions on very large clusters. See https://groups.google.com/forum/?fromgroups#!topic/mongodb-user/ajgO9wUxbos for details. Other drivers also initialize this field to a random value. --- bson/objectid.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/bson/objectid.py b/bson/objectid.py index ee1364ea0..36fac79bb 100644 --- a/bson/objectid.py +++ b/bson/objectid.py @@ -26,6 +26,7 @@ except ImportError: # for Python < 2.5 import md5 _md5func = md5.new import os +import random import socket import struct import threading @@ -51,7 +52,7 @@ class ObjectId(object): """A MongoDB ObjectId. """ - _inc = 0 + _inc = random.randint(0, 0xFFFFFF) _inc_lock = threading.Lock() _machine_bytes = _machine_bytes()