Comments (11)
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 :)
from idgen.
I just noticed Stopwatch.Start()
"starts or resumes" a stopwatch. So I could just call _sw.Start()
from the StopwatchTimeSource
ctor I guess. And then I could initialize the _sw
field with just new Stopwatch()
...
from idgen.
Will be in 2.1 release soon to be released.
from idgen.
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:
Time is now: 19-2-2020 14:36:59
Waiting 10 seconds...
Time is now: 19-2-2020 14:37:09, generating ID...
Generated id: 679682892149817344
Extracted timestamp: 19-2-2020 14:37:09
As you can see, the generator is instantiated at 14:36:59
, an ID is generated at 14:37:09
and when I 'extract' the timestamp from the ID I get that exact value?
from idgen.
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
Waiting 10 seconds...
Time is now: 19/02/2020 14:11:35, generating ID...
Generated id: 679691514317635584
Extracted timestamp: 19/02/2020 14:11:25 +00:00
from idgen.
Running in a .net core 3.1 console app
from idgen.
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.
Build | Project Reference | NuGet package |
---|---|---|
Debug | ||
Release |
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"
from idgen.
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 CreateID()
just before the end of the ctor
but that's ugly and may have unintentional side-effects...). I'm going to think about this. Suggestions welcome!
from idgen.
You could try adding a static constructor to the StopwatchTimeSource class, and instantiating the StopWatch there
from idgen.
See 1417890
Build | Project Reference | NuGet package (2.1) |
---|---|---|
Debug | ||
Release |
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!
from idgen.
Great! no problem, thanks.
from idgen.
Related Issues (20)
- GeneratorId for Azure Function(s) HOT 5
- Add to documentation HOT 1
- Feature: CreateId that accepts generator ID as argument HOT 6
- Id to datetime HOT 10
- Int use for Generator Id HOT 5
- Thread safety HOT 12
- Duplicate Ids in a two instance microservice HOT 4
- Creating ID for a specific point in past time HOT 7
- Using IdGen for migrating legacy data HOT 4
- SQL Server equivalent possible? HOT 3
- Drop support for netstandard 1.1 HOT 5
- Non-generic IIdGenerator HOT 5
- Avoid spending a sign bit by using ulongs HOT 10
- Tag 3.0.3 is missing HOT 1
- Documentation about generatorId collisions when id structure changes HOT 9
- IdGen is generating 17bit Id instead of 16bit? What should we change in the method so that it generate 16 bit Id instead of 17 bit now and in the future it continuous to generate 16 bit Id? HOT 11
- Is there a way to shorten the number of digits generated for an id? HOT 5
- Handling uniqueness ID across multiple instance of same service HOT 6
- IdGenerator does not contain a definition for 'Take' HOT 1
- Consider adding IdGenerator#CreateFromDateTime HOT 5
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from idgen.