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

Reduce synchronization scope when generating the TSID #25

Closed
vladmihalcea opened this issue Jan 29, 2023 · 5 comments
Closed

Reduce synchronization scope when generating the TSID #25

vladmihalcea opened this issue Jan 29, 2023 · 5 comments
Labels
enhancement New feature or request

Comments

@vladmihalcea
Copy link

Currently, the TsidFactory.create method is synchronized:

public synchronized Tsid create() {

	final long _time = getTime() << RANDOM_BITS;
	final long _node = (long) this.node << this.counterBits;
	final long _counter = (long) this.counter & this.counterMask;

	return new Tsid(_time | _node | _counter);
}

But, the synchronized is needed for getTime only. So, it is getTime that should be synchronized instead because of the timestamp monotonicity issues:

private synchronized long getTime() {

	long time = clock.millis();

	if (time <= this.lastTime) {
		this.counter++;
		// Carry is 1 if an overflow occurs after ++.
		int carry = this.counter >>> this.counterBits;
		this.counter = this.counter & this.counterMask;
		time = this.lastTime + carry; // increment time
	} else {
		// If the system clock has advanced as expected,
		// simply reset the counter to a new random value.
		this.counter = this.getRandomCounter();
	}

	// save current time
	this.lastTime = time;

	// adjust to the custom epoch
	return time - this.customEpoch;
}

This will reduce the scope of the Thread contention, especially when using a ThreadLocalRandom generator.

@fabiolimace fabiolimace added the enhancement New feature or request label Jan 29, 2023
@fabiolimace
Copy link
Member

fabiolimace commented Feb 5, 2023

I did as you suggested, Vlad. Thanks for the tip!

@fabiolimace
Copy link
Member

Released v5.2.2. 🎉

@vladmihalcea
Copy link
Author

Thanks. I plan on creating a Tsid Hibernate generator based on your Tsid.

@fabiolimace
Copy link
Member

fabiolimace commented Feb 5, 2023

This is going to be great!

Take what you need from this project and use it on your own. Go ahead and make a better TSID!

I don't intend to add more features here. From now on, this project will only be maintained. I'm getting tired.

I wish you good luck!

@vladmihalcea
Copy link
Author

All the best 👍

vladmihalcea added a commit to vladmihalcea/hypersistence-tsid that referenced this issue Feb 13, 2023
"I don't intend to add more features here. From now on, this project will only be maintained. I'm getting tired. I wish you good luck!"

f4b6a3#25 (comment)
vladmihalcea added a commit to vladmihalcea/hypersistence-tsid that referenced this issue Feb 13, 2023
vladmihalcea added a commit to vladmihalcea/hypersistence-tsid that referenced this issue Feb 13, 2023
vladmihalcea added a commit to vladmihalcea/hypersistence-tsid that referenced this issue Feb 13, 2023
vladmihalcea added a commit to vladmihalcea/hypersistence-tsid that referenced this issue Feb 13, 2023
vladmihalcea added a commit to vladmihalcea/hypersistence-tsid that referenced this issue Feb 13, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants