{@code
* snowflake:
- * node-id: 3
+ * datacenter-id: 1
+ * worker-id: 3
* epoch: 2024-01-01T00:00:00Z
* }
*
- * {@code nodeId} has no valid default on purpose: {@link com.fayupable.snowflake.config.SnowflakeConfig}
- * rejects a negative {@code nodeId}, so an application that forgets to set
- * {@code snowflake.node-id} fails fast at startup instead of silently colliding
- * with another node that happens to use the same default value.
+ * {@code datacenterId} and {@code workerId} have no valid default on purpose:
+ * {@link com.fayupable.snowflake.config.SnowflakeConfig} rejects a negative value
+ * for either, so an application that forgets to set them fails fast at startup
+ * instead of silently colliding with another node that happens to use the same
+ * default value.
*/
@ConfigurationProperties("snowflake")
public class SnowflakeProperties {
- private long nodeId = -1;
+ private long datacenterId = -1;
+ private long workerId = -1;
private Instant epoch = Instant.parse("2024-01-01T00:00:00Z");
/**
- * @return the node identifier this application instance will embed in every
+ * @return the datacenter identifier this application instance will embed in
+ * every generated id; must be unique per physical/logical datacenter
+ * sharing the same {@code epoch}, or -1 if unconfigured
+ */
+ public long getDatacenterId() {
+ return datacenterId;
+ }
+
+ /**
+ * @param datacenterId the datacenter identifier to embed in every id generated
+ * by this application instance; bound from
+ * {@code snowflake.datacenter-id}
+ */
+ public void setDatacenterId(long datacenterId) {
+ this.datacenterId = datacenterId;
+ }
+
+ /**
+ * @return the worker identifier this application instance will embed in every
* generated id; must be unique across all concurrently running
- * instances sharing the same {@code epoch}, or -1 if unconfigured
+ * instances within the same datacenter sharing the same
+ * {@code epoch}, or -1 if unconfigured
*/
- public long getNodeId() {
- return nodeId;
+ public long getWorkerId() {
+ return workerId;
}
/**
- * @param nodeId the node identifier to embed in every id generated by this
- * application instance; bound from {@code snowflake.node-id}
+ * @param workerId the worker identifier to embed in every id generated by this
+ * application instance; bound from {@code snowflake.worker-id}
*/
- public void setNodeId(long nodeId) {
- this.nodeId = nodeId;
+ public void setWorkerId(long workerId) {
+ this.workerId = workerId;
}
/**
diff --git a/snowflake-jpa-spring/src/test/java/com/fayupable/snowflake/jpaspring/SnowflakeIdentifierGeneratorIntegrationTest.java b/snowflake-jpa-spring/src/test/java/com/fayupable/snowflake/jpaspring/SnowflakeIdentifierGeneratorIntegrationTest.java
index 660cc67..ced020b 100644
--- a/snowflake-jpa-spring/src/test/java/com/fayupable/snowflake/jpaspring/SnowflakeIdentifierGeneratorIntegrationTest.java
+++ b/snowflake-jpa-spring/src/test/java/com/fayupable/snowflake/jpaspring/SnowflakeIdentifierGeneratorIntegrationTest.java
@@ -11,7 +11,7 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
@DataJpaTest
-@TestPropertySource(properties = "snowflake.node-id=5")
+@TestPropertySource(properties = {"snowflake.datacenter-id=1", "snowflake.worker-id=5"})
@ExtendWith(SnowflakeIdGeneratorHolderResetExtension.class)
class SnowflakeIdentifierGeneratorIntegrationTest {
diff --git a/snowflake-jpa/pom.xml b/snowflake-jpa/pom.xml
index cdc8a39..2726296 100644
--- a/snowflake-jpa/pom.xml
+++ b/snowflake-jpa/pom.xml
@@ -7,7 +7,7 @@