-
-
Notifications
You must be signed in to change notification settings - Fork 149
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
Generated Ids have timestamps from the past #18
Comments
Can you please give an example / testcase so I can reproduce? This is how I tested: Console.WriteLine($"Time is now: {DateTime.Now}");
var g = new IdGenerator(0);
var shiftbits = g.MaskConfig.GeneratorIdBits + g.MaskConfig.SequenceBits;
var offset = g.TimeSource.Epoch.ToLocalTime();
Console.WriteLine($"Waiting 10 seconds...");
Thread.Sleep(1000 * 10);
var id = g.CreateId();
Console.WriteLine($"Time is now: {DateTime.Now}, generating ID...");
Console.WriteLine($"Generated id: {id}");
var date = offset + TimeSpan.FromMilliseconds(id >> shiftbits);
Console.WriteLine($"Extracted timestamp: {date}"); And this is my output:
As you can see, the generator is instantiated at |
Thanks for getting back on this. I have just tried your code and I get this following output... Time is now: 19/02/2020 14:11:24 |
Running in a .net core 3.1 console app |
Hmmm. appears to be a difference between referencing the IDGen project and using the NuGet package and between release/debug builds. Your initial reasoning is sound and your suggested workaround works. I'll look into fixing this.
Out of curiosity: why are you 'extracting' the timestamp from the generated ID's? Even though it's documented how an ID is generated, you're supposed to treat is as a "blob" 😉 |
Ah I wasnt, its just we had two nodes populating records in a shared database table, using this component to generate the ids. Fetching the records, ordering by id and then ordering by creation date yielded different results (despite the clocks being synced). Took me a while to work it out :) |
That must've been a pain to debug, I bet! Sorry 'bout that! I'll make sure this gets fixed; not sure how yet (I could call |
You could try adding a static constructor to the StopwatchTimeSource class, and instantiating the StopWatch there |
I just noticed |
See 1417890
That seems to fix it. I'll create and release a new NuGet package soon. Thanks for reporting, working and thinking with me on this one! |
Great! no problem, thanks. |
🎉 Yay! See version 2.1 now also on NuGet along with the new IdGen.Configuration package 🎉 |
The stopwatch is declared/instantiated as a static variable.
So it wont actually be started until it is referenced.
This results in timestamps that are marked back in time. The elapsed time is only measured after the stopwatch starts and the stopwatch only starts after the first call to CreateId().
Initializing with a call to CreateId on process startup as a workaround.
The text was updated successfully, but these errors were encountered: