Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

genOid has low entropy (creates lots of 0 bits) #9741

Closed
timotheecour opened this issue Nov 17, 2018 · 4 comments
Closed

genOid has low entropy (creates lots of 0 bits) #9741

timotheecour opened this issue Nov 17, 2018 · 4 comments

Comments

@timotheecour
Copy link
Member

timotheecour commented Nov 17, 2018

root cause of:
[critical] inserted oid's have low entropy with lots of 0's (5bef5960c7dcd43e00000001 instead of 5bef58c408de32fe5a33a29b) · Issue #65 · SSPkrolik/nimongo

here's the D implementation for reference (which doesn't have this problem):
https://github.com/vibe-d/vibe.d/blob/ce494843201f15899353e6bfcdcbead3872c751c/data/vibe/data/bson.d#L900

	/** Generates a unique object ID.
	 *
	 *   By default it will use `Clock.currTime(UTC())` as the timestamp
	 *   which guarantees that `BsonObjectID`s are chronologically
	 *   sorted.
	*/
	static BsonObjectID generate(in SysTime time = Clock.currTime(UTC()))
	{
		import std.datetime;

		BsonObjectID ret = void;
		ret.m_bytes[0 .. 4] = toBigEndianData(cast(uint)time.toUnixTime())[];
		ret.m_bytes[4 .. 7] = toBsonData(MACHINE_ID)[0 .. 3];
		ret.m_bytes[7 .. 9] = toBsonData(cast(ushort)ms_pid)[];
		ret.m_bytes[9 .. 12] = toBigEndianData(ms_inc++)[1 .. 4];
		return ret;
	}
@narimiran
Copy link
Member

Closed by #10248.

@timotheecour timotheecour changed the title genOid has low entropy (creates lots of 0 bits) [TODO] genOid has low entropy (creates lots of 0 bits) Jan 11, 2019
@timotheecour
Copy link
Member Author

issue remains, despite fact it was moved to graveyard; depending on outcome of #10248 (comment) we should migrate this issue to graveyard or re-open

@Araq Araq reopened this Jan 11, 2019
@timotheecour timotheecour changed the title [TODO] genOid has low entropy (creates lots of 0 bits) genOid has low entropy (creates lots of 0 bits) Jan 31, 2019
@narimiran
Copy link
Member

Fixed by 9ac0cbd

@timotheecour
Copy link
Member Author

timotheecour commented Jan 31, 2019

/cc @narimiran thanks!
ok after #10500 results are consistent with what D (vibe.d) and mongo (shell) at least in the case of samples from same thread:

D:

    foreach(i;0..10){
      auto id = BsonObjectID.generate();
      writeln(id);
    }

mongo shell:

for(i=0;i<10;i++) {x = ObjectId(); print(x);}

sample results:

5c52a121aea679ec63a6d23b
5c52a121aea679ec63a6d23c
5c52a121aea679ec63a6d23d
5c52a121aea679ec63a6d23e
5c52a121aea679ec63a6d23f
5c52a121aea679ec63a6d240
5c52a121aea679ec63a6d241
5c52a121aea679ec63a6d242
5c52a121aea679ec63a6d243
5c52a121aea679ec63a6d244

last 2 digits only vary in all cases

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants