Comments (5)
I think that a guard clause would avoid error prone by incorrect configurations and avoid sillently behavior to the client.
And there is a precedence, for example withNodeBits function that has it limit validation assuring the correct setup.
But one way or another it could be mentioned in README/JavaDoc.
Regards!!
from tsid-creator.
I don't consider it safe, because collisions can happen even if there are few nodes.
Actually, the problem is not with the random number generator itself (ThreadLocalRandom), but with the unavailability to provide unique node identifiers for each TSID generator.
Let's say today 10 machines are started in a cluster. Each TSID generator will get a random node ID. If all these TSID generators are given different random node numbers from each other, it can be said that no collision will occur. So it's safe for today.
But if those machines are restarted for some reason tomorrow, the node IDs will be randomized again. If at least two TSID generators receive the same node number, say 42, the IDs generated by those two TSID generators can collide during the day.
The class below has a test method that starts 10 threads, assigning a new TSID generator to each thread.
package com.example;
import static org.junit.Assert.assertNull;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ThreadLocalRandom;
import java.util.logging.Logger;
import org.junit.Test;
import com.github.f4b6a3.tsid.TsidFactory;
public class ClashTest {
static Logger LOGGER = Logger.getLogger(ClashTest.class.getName());
private TsidFactory newFactory(int nodeBits) {
return TsidFactory.builder().withRandomFunction(() -> ThreadLocalRandom.current().nextInt())
.withNodeBits(nodeBits) // 8 bits: 256 nodes; 10 bits: 1024 nodes...
.build();
}
@Test
public void test() throws InterruptedException {
int nodeBits = 8;
int threadCount = 10;
int iterationCount = 100_000;
CountDownLatch endLatch = new CountDownLatch(threadCount);
ConcurrentMap<Long, Integer> tsidMap = new ConcurrentHashMap<>();
for (int i = 0; i < threadCount; i++) {
final int threadId = i;
// one generator PER THREAD
TsidFactory factory = newFactory(nodeBits);
new Thread(() -> {
for (int j = 0; j < iterationCount; j++) {
Long tsid = factory.create().toLong();
assertNull("TSID clash detected!", tsidMap.put(tsid, (threadId * iterationCount) + j));
}
endLatch.countDown();
}).start();
}
LOGGER.info("Started threads");
endLatch.await();
LOGGER.info("Done");
}
}
The code was adapted from the code implemented by Vlad Mihalcea in one of his excellent articles. You can change the nodeBits
, threadCount
, iterationCount
variables during the tests.
from tsid-creator.
Thanks for the quick reply, @fabiolimace.
I have another doubt about node ids. At Factory builder function, javadoc says the function withNode(Integer node)
has a limit defined in 2^nodeBits-1.
Taking into consideration the limits of library, the max value allowed, considering a 20 bit length size is 1048575, but this value is not validated in runtime.
What happens to the Node Identifier used to generate the TSID if the parameter overflows this hard limit? Eg: 1048576
, or Int.MAX_VALUE (2.147.483.647)
, equivalent to 32 bit length? These extra bits are trimmed?
Regards.
from tsid-creator.
Yes, the most significant extra bits are silently trimmed.
Here is the line that strips off the extra bits: TsidFactory(Builder builder)
.
In your example, 1048576
becomes 0
because:
1048576 & (2^20-1) == 0
It's the same as mod(1048576, 2^20)
:
1048576 % 2^20 == 0
Should it throw an exception or should the javadoc be fixed? Or both?
My best regards.
from tsid-creator.
Released v5.2.1. 🎉
from tsid-creator.
Related Issues (20)
- Version 4.0.0 HOT 2
- Provide OSGI support HOT 2
- Compare internal field as unsigned integers HOT 1
- Increment the counter when the random function returns null or empty HOT 1
- handle clock drift HOT 4
- Add support for RandomGenerator in Java 17 HOT 2
- Optimize comparison and hash HOT 2
- Rewrite docs HOT 1
- Add a fast method to generate identifiers HOT 1
- Add the system property and environment variable to configure the node count HOT 1
- Add simple format methods HOT 1
- Make the Tsid class extensible HOT 1
- Suggestions for generating a node ID on Kubernetes HOT 7
- Reduce synchronization scope when generating the TSID HOT 5
- Fix sync HOT 1
- Using the Tsid.encode method in the JDK 8 environment HOT 3
- ArrayIndexOutOfBoundsException on input with multiple bytes for a character HOT 1
- TSID as database primary keys in multi tenant environment HOT 2
- Add benchmark code to compare TSID with UUID HOT 1
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 tsid-creator.