Comments (7)
profile not working in docker or k8s container by java process.
using status.podIP
as MY_POD_IP
environment variable, then recalculate node, see getInstance()
function below.
package com.example.sample.common.util;
import com.github.f4b6a3.tsid.Tsid;
import com.github.f4b6a3.tsid.TsidFactory;
/**
* Time-Sorted Unique Identifier
* <p> ref: <a href="https://github.com/f4b6a3/tsid-creator">f4b6a3/tsid-creator</a> </p>
*
*/
public class TsidUtil {
/**
* getInstance
*
* @return return instance
*/
private static Tsid getInstance() {
// get pod ip address
// `MY_POD_IP` env defined in k8s deployment yaml
// example: 172.16.1.9
String myPodIp = System.getenv("MY_POD_IP");
if (null == myPodIp) {
// when the 'MY_POD_IP' environment variable is not set in local development
myPodIp = "127.0.0.1";
}
String[] ipArr = myPodIp.split("\\.");
int node = (Integer.parseInt(ipArr[2])*256 + Integer.parseInt(ipArr[3]))%65536;
TsidFactory factory = TsidFactory.builder()
.withNodeBits(16) // max: 20 (in this case 16)
.withNode(node) // max: 2^nodeBits (in this case 2^16 = 65536)
.build();
return factory.create();
}
/**
* next long id
* <p>example: 38352658573940766</p>
*
* @return long value
*/
public static Long nextLongId() {
return getInstance().toLong();
}
/**
* next string id
* <p>example: 01226N0693HDH -> 01226n0693hdh</p>
* @return string value
*/
public static String nextId() {
return getInstance().toString().toLowerCase();
}
}
from tsid-creator.
Very good!
There's a similar strategy which also uses a wrapper file.
Contents of env-wrapper
:
#!/bin/bash
# the node number is derived from the least significant 16 bits of the k8s pod IP
export TSIDCREATOR_NODE=`hostname --ip-address | awk -F. '{ print ($3*256 + $4) }'`
export TSIDCREATOR_NODE_COUNT=65536 # 2^16 = 65536
$*
Contents of Dockerfile
:
...
COPY . .
RUN mv env-wrapper /bin/.
RUN chmod 755 /bin/env-wrapper
CMD env-wrapper myapp
Other suggestions: https://stackoverflow.com/questions/34911622/dockerfile-set-env-to-result-of-command
from tsid-creator.
I don't know. I don't know anything about Kubernetes.
Is pod ordinal index in a StatefulSet workload an option?
I found some links related to this here on Github:
I'll highlight this question so that some passerby can answer. It will be very helpful if anyone finds and shares a solution for this.
from tsid-creator.
This blog post shows how to generate snowflakes on Kubernetes: Generating Unique 64 bit IDs with Go on Kubernetes. The POD ID is derived from the IP address provided with an environment variable MY_IP
.
TSID Creator has an environment variable that can be used to provide the POD ID: TSIDCREATOR_NODE
. So you don't need to write extra Java code to detect the IP address and calculate the POD ID.
The lines below set the POD ID based on the pod IP address:
# append to ~/.profile
# POD identifier: x of 65536
# note that the maximum number of IDs per second per POD is reduced 64K/second/POD
# where x is the MODULO 65536 (2^16) of the first POD's IPv4 (if there's 1 or more addresses)
# for example, if the first address of the host is 10.42.10.1, the value of x is 2561 (10*256 + 1)
export TSIDCREATOR_NODE="`hostname -I | awk '{print $1}' | awk -F. '{print ($3*256 + $4) % 65536}'`"
export TSIDCREATOR_NODE_COUNT="65536"
The example takes the default cluster_cidr
into account, according to this page: https://www.suse.com/support/kb/doc/?id=000020167.
By default, the
cluster_cidr
block is 10.42.0.0/16 and thenode-cidr-mask-size
is 24. This gives the cluster 256 blocks of /24 networks to distribute out to the pool of nodes. For example, node1 will get 10.42.0.0/24, node2 will get 10.42.1.0/24, node3 will get 10.42.2.0/24 and so on.
from tsid-creator.
Hi @ycrao !
So my example using ~/.profile
is useless. I'll try to fix the example another day. Thanks for letting me know.
As for your Java class, the code in getInstance()
is all you need to do to derive the node
ID from the pod IP, besides defining the MY_POD_IP
environment variable in your k8s deployment yml file. Additionally, you can also set the node bits to 16 to accommodate all 65,536 possible values.
// ...
int node = (Integer.parseInt(ipArr[2])*256 + Integer.parseInt(ipArr[3]))%65536;
TsidFactory factory = TsidFactory.builder()
.withNodeBits(16) // max: 20 (in this case 16)
.withNode(node) // max: 2^nodeBits (in this case 2^16 = 65536)
.build();
//...
Thank you for your collaboration!
from tsid-creator.
@fabiolimace .profile
file works in local physical/virtual machine or cloud VPS. OS will initialize some config (user profile included) when machine power-on.
In docker container, it is difficult to pass a dynamic environment variable to a main process. Maybe we can write a shell file to source profile and then run java app in backgroud.
start.sh
code like below:
#!/bin/bash
source /root/.profile && java -jar /sample.jar
dockerfile
code:
FROM eclipse-temurin:8-jre
# ...
COPY .profile /root/.profile
COPY start.sh /start.sh
ADD sample.jar /sample.jar
# ENTRYPOINT exec java -jar /sample.jar
CMD ["/bin/bash", "/start.sh"]
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
- Question: Is the ThreadLocalRandom approach considerably safe on a clustered environment? HOT 5
- 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.