You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
importjava.net.NetworkInterface;
importjava.security.MessageDigest;
importjava.util.Enumeration;
importjava.util.Random;
publicclassNodeIdGenerator {
privatestaticfinalintMAX_NODE_ID = 1024; // 10-bit node ID space/** * Generate a unique node ID by combining current time in millis and a unique machine fingerprint. * Falls back to random salt if MAC address is unavailable. */publicstaticintgenerateNodeId() {
longepochMillis = System.currentTimeMillis();
StringmachineFingerprint = getMachineFingerprint();
if (machineFingerprint == null) {
machineFingerprint = generateRandomSalt();
}
// Combine epochMillis and machine fingerprint to generate a unique IDStringuniqueString = epochMillis + "-" + machineFingerprint;
returnhashToInt(uniqueString, MAX_NODE_ID);
}
/** * Get the MAC address as a unique identifier. */privatestaticStringgetMachineFingerprint() {
try {
Enumeration<NetworkInterface> networkInterfaces = NetworkInterface.getNetworkInterfaces();
while (networkInterfaces.hasMoreElements()) {
NetworkInterfacenetwork = networkInterfaces.nextElement();
byte[] mac = network.getHardwareAddress();
if (mac != null) {
StringBuildermacAddress = newStringBuilder();
for (byteb : mac) {
macAddress.append(String.format("%02X", b));
}
returnmacAddress.toString();
}
}
} catch (Exceptione) {
// Fallback if MAC address cannot be determinede.printStackTrace();
}
returnnull;
}
/** * Generate a random salt for cases where MAC address is unavailable. */privatestaticStringgenerateRandomSalt() {
Randomrandom = newRandom();
byte[] salt = newbyte[6]; // Generate 6 random bytesrandom.nextBytes(salt);
StringBuildersaltString = newStringBuilder();
for (byteb : salt) {
saltString.append(String.format("%02X", b));
}
returnsaltString.toString();
}
/** * Hash the input string to an integer and constrain it to the max value. */privatestaticinthashToInt(Stringinput, intmaxValue) {
try {
MessageDigestdigest = MessageDigest.getInstance("SHA-256");
byte[] hash = digest.digest(input.getBytes());
// Use the first 4 bytes of the hash to compute an integerintresult = 0;
for (inti = 0; i < 4; i++) {
result = (result << 8) | (hash[i] & 0xFF);
}
returnMath.abs(result) % maxValue;
} catch (Exceptione) {
thrownewRuntimeException("Error hashing input string", e);
}
}
publicstaticvoidmain(String[] args) {
intnodeId = generateNodeId();
System.out.println("Generated Node ID: " + nodeId);
}
}
The text was updated successfully, but these errors were encountered:
For backward compatibility: just use "time" converted to epoch millis as sequenceNumber, maybe combine it with properties of the "_id" field if epoch millis clash.
johanhaleby
changed the title
Implement global sequence number
Implement global position
Nov 15, 2024
Use snowflake-id and generate the id like this:
The text was updated successfully, but these errors were encountered: