You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@polaris.apache.org by "adam-christian-software (via GitHub)" <gi...@apache.org> on 2025/07/18 01:47:41 UTC

[PR] WIP feat(idgen): Start Implementation of NoSQL with the ID Generation Framework [polaris]

adam-christian-software opened a new pull request, #2131:
URL: https://github.com/apache/polaris/pull/2131

   # Motivation
   This implementation starts the series of PRs to implement the NoSQL work presented in https://docs.google.com/document/d/1POUWe0xMZOBoaJ6Rgiw35ziEoc6OEYCiW7Zk6bR9H6M/edit?tab=t.0#heading=h.nx9vzhg2x8v2 with the first implementation [here](https://github.com/apache/polaris/pull/1189).
   
   # Description
   This PR is dedicated to creating the ID Generation Framework.
   
   Related to #[650](https://github.com/apache/polaris/issues/650) & #[844](https://github.com/apache/polaris/issues/844)
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] WIP feat(idgen): Start Implementation of NoSQL with the ID Generation Framework [polaris]

Posted by "adam-christian-software (via GitHub)" <gi...@apache.org>.
adam-christian-software commented on code in PR #2131:
URL: https://github.com/apache/polaris/pull/2131#discussion_r2216814699


##########
persistence/nosql/idgen/api/src/main/java/org/apache/polaris/ids/api/SnowflakeIdGenerator.java:
##########
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.ids.api;
+
+import jakarta.annotation.Nonnull;
+import java.time.Instant;
+import java.time.ZoneId;
+import java.util.UUID;
+
+public interface SnowflakeIdGenerator extends IdGenerator {
+  /** Offset of the snowflake ID generator since the 1970-01-01T00:00:00Z epoch instant. */
+  Instant EPOCH_OFFSET =

Review Comment:
   I like ID_EPOCH. I'll do that.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] WIP feat(idgen): Start Implementation of NoSQL with the ID Generation Framework [polaris]

Posted by "adutra (via GitHub)" <gi...@apache.org>.
adutra commented on code in PR #2131:
URL: https://github.com/apache/polaris/pull/2131#discussion_r2219183760


##########
persistence/nosql/idgen/impl/src/main/java/org/apache/polaris/ids/impl/SnowflakeIdGeneratorFactory.java:
##########
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.ids.impl;
+
+import static org.apache.polaris.ids.impl.SnowflakeIdGeneratorImpl.validateArguments;
+
+import java.time.Instant;
+import java.util.Map;
+import java.util.function.LongSupplier;
+import org.apache.polaris.ids.api.SnowflakeIdGenerator;
+import org.apache.polaris.ids.spi.IdGeneratorFactory;
+import org.apache.polaris.ids.spi.IdGeneratorSource;
+
+public class SnowflakeIdGeneratorFactory implements IdGeneratorFactory<SnowflakeIdGenerator> {
+  @Override
+  public void validateParameters(Map<String, String> params, IdGeneratorSource idGeneratorSource) {
+    int timestampBits =
+        Integer.parseInt(
+            params.getOrDefault(
+                "timestamp-bits", "" + SnowflakeIdGenerator.DEFAULT_TIMESTAMP_BITS));
+    int nodeIdBits =
+        Integer.parseInt(
+            params.getOrDefault("node-id-bits", "" + SnowflakeIdGenerator.DEFAULT_NODE_ID_BITS));
+    int sequenceBits =
+        Integer.parseInt(
+            params.getOrDefault("sequence-bits", "" + SnowflakeIdGenerator.DEFAULT_SEQUENCE_BITS));
+    var offsetMillis = SnowflakeIdGenerator.EPOCH_OFFSET_MILLIS;
+    var offset = params.get("offset");
+    if (offset != null) {
+      offsetMillis = Instant.parse(offset).toEpochMilli();
+    }
+
+    validateArguments(timestampBits, sequenceBits, nodeIdBits, offsetMillis, idGeneratorSource);
+  }
+
+  @Override
+  public SnowflakeIdGenerator buildSystemIdGenerator(
+      Map<String, String> params, LongSupplier clockMillis) {
+    return buildIdGenerator(
+        params,
+        new IdGeneratorSource() {
+          @Override
+          public int nodeId() {
+            return 0;
+          }
+
+          @Override
+          public long currentTimeMillis() {
+            return SnowflakeIdGenerator.EPOCH_OFFSET_MILLIS;

Review Comment:
   My initial comment stems from the fact that the `clockMillis` parameter is part of the method signature (thus part of the API) but is not used, so I don't know why it is there.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(idgen): Start Implementation of NoSQL with the ID Generation Framework [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on PR #2131:
URL: https://github.com/apache/polaris/pull/2131#issuecomment-3183897096

   I propose to merge this PR "as is" and address comments after we get the whole NoSQL persistence merged.
   
   I'll deal with merge conflicts presently.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] WIP feat(idgen): Start Implementation of NoSQL with the ID Generation Framework [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #2131:
URL: https://github.com/apache/polaris/pull/2131#discussion_r2216769499


##########
persistence/nosql/idgen/api/src/main/java/org/apache/polaris/ids/api/SnowflakeIdGenerator.java:
##########
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.ids.api;
+
+import jakarta.annotation.Nonnull;
+import java.time.Instant;
+import java.time.ZoneId;
+import java.util.UUID;
+
+public interface SnowflakeIdGenerator extends IdGenerator {
+  /** Offset of the snowflake ID generator since the 1970-01-01T00:00:00Z epoch instant. */
+  Instant EPOCH_OFFSET =

Review Comment:
   `ID_GENERATOR_EPOCH` LGTM. Alt: `ID_EPOCH`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] WIP feat(idgen): Start Implementation of NoSQL with the ID Generation Framework [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #2131:
URL: https://github.com/apache/polaris/pull/2131#discussion_r2214750693


##########
persistence/nosql/idgen/api/src/main/java/org/apache/polaris/ids/api/SnowflakeIdGenerator.java:
##########
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.ids.api;
+
+import jakarta.annotation.Nonnull;
+import java.time.Instant;
+import java.time.ZoneId;
+import java.util.UUID;
+
+public interface SnowflakeIdGenerator extends IdGenerator {
+  /** Offset of the snowflake ID generator since the 1970-01-01T00:00:00Z epoch instant. */
+  Instant EPOCH_OFFSET =

Review Comment:
   this is not technically an offset, but an exact moment in time. How about just `EPOCH`?



##########
persistence/nosql/idgen/api/src/main/java/org/apache/polaris/ids/api/IdGenerator.java:
##########
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.ids.api;
+
+public interface IdGenerator {
+  /** Generate a new, unique ID. */
+  long generateId();
+
+  /** Generate the system ID for a node, solely used by/for node management purposes. */
+  long systemIdForNode(int nodeId);
+
+  default String describeId(long id) {
+    return Long.toString(id);
+  }
+
+  IdGenerator NONE =
+      new IdGenerator() {
+        @Override
+        public long generateId() {
+          throw new UnsupportedOperationException("NONE IdGenerator");

Review Comment:
   nit: sometimes exception messages are separated from class names. Having a more descriptive message would be nice. Suggestion: `NONE IdGenerator cannot actually generate IDs`



##########
persistence/nosql/idgen/api/src/main/java/org/apache/polaris/ids/api/MonotonicClock.java:
##########
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.ids.api;
+
+import java.time.Instant;
+
+/**
+ * Provides a clock providing the current time in milliseconds, microseconds and instant since
+ * 1970-01-01-00:00:00.000. The returned timestamp values increase monotonically.
+ *
+ * <p>The functions provide nanosecond/microsecond/millisecond precision, but not necessarily the
+ * same resolution (how frequently the value changes) - no guarantees are made.
+ *
+ * <p>Implementation <em>may</em> adjust to wall clocks advancing faster than the real time. If and
+ * how exactly depends on the implementation, as long as none of the time values available via this
+ * interface "goes backwards".
+ *
+ * <p>Implementer notes: {@link System#nanoTime() System.nanoTime()} does not guarantee that the
+ * values will be monotonically increasing when invocations happen from different
+ * CPUs/cores/threads.
+ *
+ * <p>A default implementation of {@link MonotonicClock} can be injected as an application scoped
+ * bean in CDI.
+ */
+public interface MonotonicClock extends AutoCloseable {
+  /**
+   * Current timestamp as microseconds since epoch, can be used as a monotonically increasing wall
+   * clock.
+   */
+  long currentTimeMicros();
+
+  /**
+   * Current timestamp as milliseconds since epoch, can be used as a monotonically increasing wall
+   * clock.
+   */
+  long currentTimeMillis();
+
+  /**
+   * Current instant with nanosecond precision, can be used as a monotonically increasing wall
+   * clock.
+   */
+  Instant currentInstant();
+
+  /** Monotonically increasing timestamp with nanosecond precision, not related to wall clock. */
+  long nanoTime();
+
+  void sleepMillis(long millis);
+
+  @Override
+  void close();

Review Comment:
   why override? We do not add anything new here :thinking: 
   
   Note: it can still throw `RuntimeException`.



##########
persistence/nosql/idgen/impl/src/main/resources/META-INF/beans.xml:
##########
@@ -0,0 +1,24 @@
+<!--
+  ~ Licensed to the Apache Software Foundation (ASF) under one
+  ~ or more contributor license agreements.  See the NOTICE file
+  ~ distributed with this work for additional information
+  ~ regarding copyright ownership.  The ASF licenses this file
+  ~ to you under the Apache License, Version 2.0 (the
+  ~ "License"); you may not use this file except in compliance
+  ~ with the License.  You may obtain a copy of the License at
+  ~
+  ~   http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing,
+  ~ software distributed under the License is distributed on an
+  ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  ~ KIND, either express or implied.  See the License for the
+  ~ specific language governing permissions and limitations
+  ~ under the License.
+  -->
+
+<beans xmlns="https://jakarta.ee/xml/ns/jakartaee"
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/beans_4_0.xsd">
+    <!-- File required by Weld (used for testing), not by Quarkus -->

Review Comment:
   Would it be possible to move this file to the test source tree?



##########
persistence/nosql/idgen/impl/src/main/java/org/apache/polaris/ids/impl/SnowflakeIdGeneratorFactory.java:
##########
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.ids.impl;
+
+import static org.apache.polaris.ids.impl.SnowflakeIdGeneratorImpl.validateArguments;
+
+import java.time.Instant;
+import java.util.Map;
+import java.util.function.LongSupplier;
+import org.apache.polaris.ids.api.SnowflakeIdGenerator;
+import org.apache.polaris.ids.spi.IdGeneratorFactory;
+import org.apache.polaris.ids.spi.IdGeneratorSource;
+
+public class SnowflakeIdGeneratorFactory implements IdGeneratorFactory<SnowflakeIdGenerator> {
+  @Override
+  public void validateParameters(Map<String, String> params, IdGeneratorSource idGeneratorSource) {
+    int timestampBits =
+        Integer.parseInt(
+            params.getOrDefault(
+                "timestamp-bits", "" + SnowflakeIdGenerator.DEFAULT_TIMESTAMP_BITS));
+    int nodeIdBits =
+        Integer.parseInt(
+            params.getOrDefault("node-id-bits", "" + SnowflakeIdGenerator.DEFAULT_NODE_ID_BITS));
+    int sequenceBits =
+        Integer.parseInt(
+            params.getOrDefault("sequence-bits", "" + SnowflakeIdGenerator.DEFAULT_SEQUENCE_BITS));
+    var offsetMillis = SnowflakeIdGenerator.EPOCH_OFFSET_MILLIS;
+    var offset = params.get("offset");
+    if (offset != null) {
+      offsetMillis = Instant.parse(offset).toEpochMilli();
+    }
+
+    validateArguments(timestampBits, sequenceBits, nodeIdBits, offsetMillis, idGeneratorSource);
+  }
+
+  @Override
+  public SnowflakeIdGenerator buildSystemIdGenerator(
+      Map<String, String> params, LongSupplier clockMillis) {

Review Comment:
   `clockMillis` is unused and callers always have it as a constant `0`. Do we need this parameter?



##########
persistence/nosql/idgen/api/src/main/java/org/apache/polaris/ids/api/IdGenerator.java:
##########
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.ids.api;
+
+public interface IdGenerator {
+  /** Generate a new, unique ID. */
+  long generateId();
+
+  /** Generate the system ID for a node, solely used by/for node management purposes. */
+  long systemIdForNode(int nodeId);

Review Comment:
   Is this ID expected to be the same for all `IdGenerator` implementations? What if a future impl. is not node-based?
   
   Should this be pushed down to the Snowflake ID code?



##########
persistence/nosql/idgen/spi/src/main/java/org/apache/polaris/ids/spi/IdGeneratorFactory.java:
##########
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.ids.spi;
+
+import java.util.Map;
+import java.util.ServiceLoader;
+import java.util.function.LongSupplier;
+import org.apache.polaris.ids.api.IdGenerator;
+
+public interface IdGeneratorFactory<I extends IdGenerator> {
+  String name();
+
+  void validateParameters(Map<String, String> params, IdGeneratorSource idGeneratorSource);
+
+  I buildIdGenerator(Map<String, String> params, IdGeneratorSource idGeneratorSource);
+
+  I buildSystemIdGenerator(Map<String, String> params, LongSupplier clockMillis);

Review Comment:
   It would be nice to add javadoc about the differences between `buildSystemIdGenerator` and `buildIdGenerator`.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(idgen): Start Implementation of NoSQL with the ID Generation Framework [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on PR #2131:
URL: https://github.com/apache/polaris/pull/2131#issuecomment-3202816897

   updated to the latest `main` and resolved conflicts.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(idgen): Start Implementation of NoSQL with the ID Generation Framework [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #2131:
URL: https://github.com/apache/polaris/pull/2131#discussion_r2319418594


##########
persistence/nosql/idgen/spi/build.gradle.kts:
##########
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+plugins {
+  alias(libs.plugins.jandex)
+  id("polaris-server")
+}
+
+description = "Polaris ID generation SPI"
+
+dependencies {
+  implementation(project(":polaris-idgen-api"))
+
+  compileOnly(libs.jakarta.annotation.api)
+  compileOnly(libs.jakarta.validation.api)
+  compileOnly(libs.jakarta.inject.api)
+  compileOnly(libs.jakarta.enterprise.cdi.api)
+
+  compileOnly(libs.smallrye.config.core)
+  compileOnly(platform(libs.quarkus.bom))
+  compileOnly("io.quarkus:quarkus-core")
+
+  compileOnly(project(":polaris-immutables"))

Review Comment:
   `IdGeneratorSource` in this PR matches https://github.com/snazy/polaris/blob/persistence-nosql/persistence/nosql/idgen/spi/src/main/java/org/apache/polaris/ids/spi/IdGeneratorSource.java
   
   I believe the plan is to address node ID concerns in the ID generator after we merge the whole NoSQL code. I'll file an issue for that.
   
   I'll remove the unused dep on `polaris-immutables` for now.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(idgen): Start Implementation of NoSQL with the ID Generation Framework [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on PR #2131:
URL: https://github.com/apache/polaris/pull/2131#issuecomment-3202829633

   If no objections, I propose to merge and open issue for follow-up on the topics discovered during the review.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] WIP feat(idgen): Start Implementation of NoSQL with the ID Generation Framework [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #2131:
URL: https://github.com/apache/polaris/pull/2131#discussion_r2216089530


##########
persistence/nosql/idgen/impl/src/main/resources/META-INF/beans.xml:
##########
@@ -0,0 +1,24 @@
+<!--
+  ~ Licensed to the Apache Software Foundation (ASF) under one
+  ~ or more contributor license agreements.  See the NOTICE file
+  ~ distributed with this work for additional information
+  ~ regarding copyright ownership.  The ASF licenses this file
+  ~ to you under the Apache License, Version 2.0 (the
+  ~ "License"); you may not use this file except in compliance
+  ~ with the License.  You may obtain a copy of the License at
+  ~
+  ~   http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing,
+  ~ software distributed under the License is distributed on an
+  ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  ~ KIND, either express or implied.  See the License for the
+  ~ specific language governing permissions and limitations
+  ~ under the License.
+  -->
+
+<beans xmlns="https://jakarta.ee/xml/ns/jakartaee"
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/beans_4_0.xsd">
+    <!-- File required by Weld (used for testing), not by Quarkus -->

Review Comment:
   sorry for the false alarm :sweat_smile: 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] WIP feat(idgen): Start Implementation of NoSQL with the ID Generation Framework [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #2131:
URL: https://github.com/apache/polaris/pull/2131#discussion_r2214750693


##########
persistence/nosql/idgen/api/src/main/java/org/apache/polaris/ids/api/SnowflakeIdGenerator.java:
##########
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.ids.api;
+
+import jakarta.annotation.Nonnull;
+import java.time.Instant;
+import java.time.ZoneId;
+import java.util.UUID;
+
+public interface SnowflakeIdGenerator extends IdGenerator {
+  /** Offset of the snowflake ID generator since the 1970-01-01T00:00:00Z epoch instant. */
+  Instant EPOCH_OFFSET =

Review Comment:
   nit: this is not technically an offset, but an exact moment in time. How about just `EPOCH`?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] WIP feat(idgen): Start Implementation of NoSQL with the ID Generation Framework [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #2131:
URL: https://github.com/apache/polaris/pull/2131#discussion_r2216777142


##########
persistence/nosql/idgen/impl/src/main/java/org/apache/polaris/ids/impl/SnowflakeIdGeneratorFactory.java:
##########
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.ids.impl;
+
+import static org.apache.polaris.ids.impl.SnowflakeIdGeneratorImpl.validateArguments;
+
+import java.time.Instant;
+import java.util.Map;
+import java.util.function.LongSupplier;
+import org.apache.polaris.ids.api.SnowflakeIdGenerator;
+import org.apache.polaris.ids.spi.IdGeneratorFactory;
+import org.apache.polaris.ids.spi.IdGeneratorSource;
+
+public class SnowflakeIdGeneratorFactory implements IdGeneratorFactory<SnowflakeIdGenerator> {
+  @Override
+  public void validateParameters(Map<String, String> params, IdGeneratorSource idGeneratorSource) {
+    int timestampBits =
+        Integer.parseInt(
+            params.getOrDefault(
+                "timestamp-bits", "" + SnowflakeIdGenerator.DEFAULT_TIMESTAMP_BITS));
+    int nodeIdBits =
+        Integer.parseInt(
+            params.getOrDefault("node-id-bits", "" + SnowflakeIdGenerator.DEFAULT_NODE_ID_BITS));
+    int sequenceBits =
+        Integer.parseInt(
+            params.getOrDefault("sequence-bits", "" + SnowflakeIdGenerator.DEFAULT_SEQUENCE_BITS));
+    var offsetMillis = SnowflakeIdGenerator.EPOCH_OFFSET_MILLIS;
+    var offset = params.get("offset");
+    if (offset != null) {
+      offsetMillis = Instant.parse(offset).toEpochMilli();
+    }
+
+    validateArguments(timestampBits, sequenceBits, nodeIdBits, offsetMillis, idGeneratorSource);
+  }
+
+  @Override
+  public SnowflakeIdGenerator buildSystemIdGenerator(
+      Map<String, String> params, LongSupplier clockMillis) {
+    return buildIdGenerator(
+        params,
+        new IdGeneratorSource() {
+          @Override
+          public int nodeId() {
+            return 0;
+          }
+
+          @Override
+          public long currentTimeMillis() {
+            return SnowflakeIdGenerator.EPOCH_OFFSET_MILLIS;

Review Comment:
   I tend to think node ID generation should be internal to the Snowflake ID code. A sharable `NodeIdGenerator` generator might be an overkill, but that is a secondary concern.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] WIP feat(idgen): Start Implementation of NoSQL with the ID Generation Framework [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #2131:
URL: https://github.com/apache/polaris/pull/2131#discussion_r2216739615


##########
persistence/nosql/idgen/api/src/main/java/org/apache/polaris/ids/api/IdGenerator.java:
##########
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.ids.api;
+
+public interface IdGenerator {
+  /** Generate a new, unique ID. */
+  long generateId();
+
+  /** Generate the system ID for a node, solely used by/for node management purposes. */
+  long systemIdForNode(int nodeId);

Review Comment:
   +1 to Adam's point 1.
   
   ... however, I have a bigger concern. Suppose we run with Snowflake IDs for a while and then change to another ID generator. Assume `generateId()` outputs do not clash. Still, do we expect `systemIdForNode(X)` to return the same value for all generator implementations and for all possible values of `X`?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] WIP feat(idgen): Start Implementation of NoSQL with the ID Generation Framework [polaris]

Posted by "adam-christian-software (via GitHub)" <gi...@apache.org>.
adam-christian-software commented on code in PR #2131:
URL: https://github.com/apache/polaris/pull/2131#discussion_r2216601495


##########
persistence/nosql/idgen/api/src/main/java/org/apache/polaris/ids/api/IdGenerator.java:
##########
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.ids.api;
+
+public interface IdGenerator {
+  /** Generate a new, unique ID. */
+  long generateId();
+
+  /** Generate the system ID for a node, solely used by/for node management purposes. */
+  long systemIdForNode(int nodeId);
+
+  default String describeId(long id) {
+    return Long.toString(id);
+  }
+
+  IdGenerator NONE =
+      new IdGenerator() {
+        @Override
+        public long generateId() {
+          throw new UnsupportedOperationException("NONE IdGenerator");

Review Comment:
   Updated.



##########
persistence/nosql/idgen/api/src/main/java/org/apache/polaris/ids/api/SnowflakeIdGenerator.java:
##########
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.ids.api;
+
+import jakarta.annotation.Nonnull;
+import java.time.Instant;
+import java.time.ZoneId;
+import java.util.UUID;
+
+public interface SnowflakeIdGenerator extends IdGenerator {
+  /** Offset of the snowflake ID generator since the 1970-01-01T00:00:00Z epoch instant. */
+  Instant EPOCH_OFFSET =

Review Comment:
   @dimas-b  - At the risk of sounding too verbose, how about ID_GENERATOR_EPOCH? I'd like to distinguish this from Unix Epoch?
   
   And, I agree @adutra . I'll update.



##########
persistence/nosql/idgen/impl/src/main/java/org/apache/polaris/ids/impl/SnowflakeIdGeneratorFactory.java:
##########
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.ids.impl;
+
+import static org.apache.polaris.ids.impl.SnowflakeIdGeneratorImpl.validateArguments;
+
+import java.time.Instant;
+import java.util.Map;
+import java.util.function.LongSupplier;
+import org.apache.polaris.ids.api.SnowflakeIdGenerator;
+import org.apache.polaris.ids.spi.IdGeneratorFactory;
+import org.apache.polaris.ids.spi.IdGeneratorSource;
+
+public class SnowflakeIdGeneratorFactory implements IdGeneratorFactory<SnowflakeIdGenerator> {
+  @Override
+  public void validateParameters(Map<String, String> params, IdGeneratorSource idGeneratorSource) {
+    int timestampBits =
+        Integer.parseInt(
+            params.getOrDefault(
+                "timestamp-bits", "" + SnowflakeIdGenerator.DEFAULT_TIMESTAMP_BITS));
+    int nodeIdBits =
+        Integer.parseInt(
+            params.getOrDefault("node-id-bits", "" + SnowflakeIdGenerator.DEFAULT_NODE_ID_BITS));
+    int sequenceBits =
+        Integer.parseInt(
+            params.getOrDefault("sequence-bits", "" + SnowflakeIdGenerator.DEFAULT_SEQUENCE_BITS));
+    var offsetMillis = SnowflakeIdGenerator.EPOCH_OFFSET_MILLIS;
+    var offset = params.get("offset");
+    if (offset != null) {
+      offsetMillis = Instant.parse(offset).toEpochMilli();
+    }
+
+    validateArguments(timestampBits, sequenceBits, nodeIdBits, offsetMillis, idGeneratorSource);
+  }
+
+  @Override
+  public SnowflakeIdGenerator buildSystemIdGenerator(
+      Map<String, String> params, LongSupplier clockMillis) {
+    return buildIdGenerator(
+        params,
+        new IdGeneratorSource() {
+          @Override
+          public int nodeId() {
+            return 0;
+          }
+
+          @Override
+          public long currentTimeMillis() {
+            return SnowflakeIdGenerator.EPOCH_OFFSET_MILLIS;

Review Comment:
   So, I think that there are some special-casing that is happening and is leaking into our interfaces and implementation. See here as well: https://github.com/apache/polaris/pull/2131/files#r2214779625
   
   We need the Snowflake ID Generation for Commit IDs and Entity Object IDs.
   
   We need a Node ID. Node IDs cannot be Snowflake IDs because Snowflake IDs require a NodeID.
   
   So, I actually think that we need a NodeIdGenerator concept for Node IDs and the concepts presented here for the other IDs. Right now, we are blending them into one interface and I think that is making things a bit confusing.
   
   For example:
   1. IdGeneratorFactory#buildSystemIdGenerator
   2. IdGenerator#systemIdForNode
   
   I think that we probably need to separate these concepts.
   
   What do y'all think, @dimas-b & @snazy?



##########
persistence/nosql/idgen/api/src/main/java/org/apache/polaris/ids/api/IdGenerator.java:
##########
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.ids.api;
+
+public interface IdGenerator {
+  /** Generate a new, unique ID. */
+  long generateId();
+
+  /** Generate the system ID for a node, solely used by/for node management purposes. */
+  long systemIdForNode(int nodeId);

Review Comment:
   Actually, the more that I read about the node-id lease stuff. I don't know whether this should be in this module. Here's my thinking:
   1. It seems that NodeID generation is special and not a Snowflake ID generation. The implementation is only based upon the node id passed in and some system-wide variables such as SnowflakeIdGeneratorImpl#timestampMax, SnowflakeIdGeneratorImpl#timestampShift, & SnowflakeIdGeneratorImpl#sequenceBits. So, in practice, it's really just the Node ID passed in.
   2. So, given that, I think we could pull this out and just put it into the node leasing modules. That way, we can keep the IdGenerator clean for the cases that require the distributed id generation.
   
   What do y'all think?



##########
persistence/nosql/idgen/spi/src/main/java/org/apache/polaris/ids/spi/IdGeneratorFactory.java:
##########
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.ids.spi;
+
+import java.util.Map;
+import java.util.ServiceLoader;
+import java.util.function.LongSupplier;
+import org.apache.polaris.ids.api.IdGenerator;
+
+public interface IdGeneratorFactory<I extends IdGenerator> {
+  String name();
+
+  void validateParameters(Map<String, String> params, IdGeneratorSource idGeneratorSource);
+
+  I buildIdGenerator(Map<String, String> params, IdGeneratorSource idGeneratorSource);
+
+  I buildSystemIdGenerator(Map<String, String> params, LongSupplier clockMillis);

Review Comment:
   This is related to https://github.com/apache/polaris/pull/2131/files#r2215597303



##########
persistence/nosql/idgen/impl/src/main/java/org/apache/polaris/ids/impl/SnowflakeIdGeneratorFactory.java:
##########
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.ids.impl;
+
+import static org.apache.polaris.ids.impl.SnowflakeIdGeneratorImpl.validateArguments;
+
+import java.time.Instant;
+import java.util.Map;
+import java.util.function.LongSupplier;
+import org.apache.polaris.ids.api.SnowflakeIdGenerator;
+import org.apache.polaris.ids.spi.IdGeneratorFactory;
+import org.apache.polaris.ids.spi.IdGeneratorSource;
+
+public class SnowflakeIdGeneratorFactory implements IdGeneratorFactory<SnowflakeIdGenerator> {
+  @Override
+  public void validateParameters(Map<String, String> params, IdGeneratorSource idGeneratorSource) {
+    int timestampBits =
+        Integer.parseInt(
+            params.getOrDefault(
+                "timestamp-bits", "" + SnowflakeIdGenerator.DEFAULT_TIMESTAMP_BITS));
+    int nodeIdBits =
+        Integer.parseInt(
+            params.getOrDefault("node-id-bits", "" + SnowflakeIdGenerator.DEFAULT_NODE_ID_BITS));
+    int sequenceBits =
+        Integer.parseInt(
+            params.getOrDefault("sequence-bits", "" + SnowflakeIdGenerator.DEFAULT_SEQUENCE_BITS));
+    var offsetMillis = SnowflakeIdGenerator.EPOCH_OFFSET_MILLIS;
+    var offset = params.get("offset");
+    if (offset != null) {
+      offsetMillis = Instant.parse(offset).toEpochMilli();
+    }
+
+    validateArguments(timestampBits, sequenceBits, nodeIdBits, offsetMillis, idGeneratorSource);
+  }
+
+  @Override
+  public SnowflakeIdGenerator buildSystemIdGenerator(
+      Map<String, String> params, LongSupplier clockMillis) {

Review Comment:
   I don't believe so. Removing.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] WIP feat(idgen): Start Implementation of NoSQL with the ID Generation Framework [polaris]

Posted by "adutra (via GitHub)" <gi...@apache.org>.
adutra commented on code in PR #2131:
URL: https://github.com/apache/polaris/pull/2131#discussion_r2215586374


##########
persistence/nosql/idgen/api/src/main/java/org/apache/polaris/ids/api/IdGeneratorSpec.java:
##########
@@ -0,0 +1,52 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.ids.api;
+
+import com.fasterxml.jackson.databind.annotation.JsonDeserialize;
+import com.fasterxml.jackson.databind.annotation.JsonSerialize;
+import io.smallrye.config.WithDefault;
+import java.util.Map;
+import org.apache.polaris.immutables.PolarisImmutable;
+import org.immutables.value.Value;
+
+@PolarisImmutable
+@JsonSerialize(as = ImmutableIdGeneratorSpec.class)
+@JsonDeserialize(as = ImmutableIdGeneratorSpec.class)
+public interface IdGeneratorSpec {
+  @WithDefault("snowflake")
+  String type();
+
+  Map<String, String> params();
+
+  @PolarisImmutable
+  interface BuildableIdGeneratorSpec extends IdGeneratorSpec {

Review Comment:
   This class looks odd? Afaict we could use `ImmutableIdGeneratorSpec.builder()` instead. The only difference seems to be that there is a default type here, whereas in `IdGeneratorSpec` there isn't, but it's certainly possible to overcome this limitation somehow.



##########
persistence/nosql/idgen/api/src/main/java/org/apache/polaris/ids/api/MonotonicClock.java:
##########
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.ids.api;
+
+import java.time.Instant;
+
+/**
+ * Provides a clock providing the current time in milliseconds, microseconds and instant since
+ * 1970-01-01-00:00:00.000. The returned timestamp values increase monotonically.
+ *
+ * <p>The functions provide nanosecond/microsecond/millisecond precision, but not necessarily the
+ * same resolution (how frequently the value changes) - no guarantees are made.
+ *
+ * <p>Implementation <em>may</em> adjust to wall clocks advancing faster than the real time. If and
+ * how exactly depends on the implementation, as long as none of the time values available via this
+ * interface "goes backwards".
+ *
+ * <p>Implementer notes: {@link System#nanoTime() System.nanoTime()} does not guarantee that the
+ * values will be monotonically increasing when invocations happen from different
+ * CPUs/cores/threads.
+ *
+ * <p>A default implementation of {@link MonotonicClock} can be injected as an application scoped
+ * bean in CDI.
+ */
+public interface MonotonicClock extends AutoCloseable {
+  /**
+   * Current timestamp as microseconds since epoch, can be used as a monotonically increasing wall
+   * clock.
+   */
+  long currentTimeMicros();
+
+  /**
+   * Current timestamp as milliseconds since epoch, can be used as a monotonically increasing wall
+   * clock.
+   */
+  long currentTimeMillis();
+
+  /**
+   * Current instant with nanosecond precision, can be used as a monotonically increasing wall
+   * clock.
+   */
+  Instant currentInstant();
+
+  /** Monotonically increasing timestamp with nanosecond precision, not related to wall clock. */
+  long nanoTime();
+
+  void sleepMillis(long millis);
+
+  @Override
+  void close();
+
+  void waitUntilTimeMillisAdvanced();

Review Comment:
   Could be good to add javadocs here (and above), it's not immediately clear what this method is supposed to do (spin-wait until the clock ticks?).
   
   Also neither this method nor `sleepMillis` throw `InterruptedException`, which is surprising since they are clearly blocking. It could be good to add an `@implSpec` note about how the interrupt flag is expected to be handled.



##########
persistence/nosql/idgen/spi/build.gradle.kts:
##########
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+plugins {
+  alias(libs.plugins.jandex)
+  id("polaris-server")
+}
+
+description = "Polaris ID generation SPI"
+
+dependencies {
+  implementation(project(":polaris-idgen-api"))
+
+  compileOnly(libs.jakarta.annotation.api)
+  compileOnly(libs.jakarta.validation.api)
+  compileOnly(libs.jakarta.inject.api)
+  compileOnly(libs.jakarta.enterprise.cdi.api)
+
+  compileOnly(libs.smallrye.config.core)
+  compileOnly(platform(libs.quarkus.bom))
+  compileOnly("io.quarkus:quarkus-core")
+
+  compileOnly(project(":polaris-immutables"))

Review Comment:
   This dependency seems unused. I wonder if you didn't forget to annotate `IdGeneratorSource` with `@PolarisImmutable`? It seems like a good candidate for that (lots of anonymous classes implementing this interface).



##########
persistence/nosql/idgen/impl/src/main/java/org/apache/polaris/ids/impl/SnowflakeIdGeneratorImpl.java:
##########
@@ -0,0 +1,408 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.ids.impl;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkState;
+import static java.lang.String.format;
+
+import com.google.common.annotations.VisibleForTesting;
+import jakarta.annotation.Nonnull;
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
+import java.util.UUID;
+import java.util.concurrent.atomic.AtomicLongFieldUpdater;
+import org.apache.polaris.ids.api.MonotonicClock;
+import org.apache.polaris.ids.api.SnowflakeIdGenerator;
+import org.apache.polaris.ids.spi.IdGeneratorSource;
+
+/**
+ * Implementation of a local, per-node generator for so-called "snowflake IDs", which are unique
+ * integer IDs in a distributed environment.
+ *
+ * <p>A monotonically increasing clock is <em>strictly required</em>. Invocations of {@link
+ * #generateId()} fail hard, if the clock walks backwards, which means it returns a lower value than
+ * before. It is recommended to use an implementation of {@link MonotonicClock} as the clock source.
+ *
+ * <p>The implementation is thread-safe.
+ *
+ * <p>Reference: <a
+ * href="https://medium.com/@jitenderkmr/demystifying-snowflake-ids-a-unique-identifier-in-distributed-computing-72796a827c9d">Article
+ * on medium.com</a>, <a
+ * href="https://github.com/twitter-archive/snowflake/tree/b3f6a3c6ca8e1b6847baa6ff42bf72201e2c2231">Twitter
+ * GitHub repository (archived)</a>
+ */
+class SnowflakeIdGeneratorImpl implements SnowflakeIdGenerator {
+
+  // TODO add a specialized implementation using hard-coded values for the standardized parameters
+
+  private static final AtomicLongFieldUpdater<SnowflakeIdGeneratorImpl> LAST_ID_UPDATER =
+      AtomicLongFieldUpdater.newUpdater(SnowflakeIdGeneratorImpl.class, "lastId");
+
+  private final IdGeneratorSource idGeneratorSource;
+
+  // Used in hot generateId()
+  private volatile long lastId;
+  private final long epochOffset;
+  private final long timestampMax;
+  private final int timestampShift;
+  private final int sequenceBits;
+  private final long sequenceMask;
+  private final long nodeMask;
+
+  SnowflakeIdGeneratorImpl(IdGeneratorSource idGeneratorSource) {
+    this(
+        DEFAULT_TIMESTAMP_BITS,
+        DEFAULT_SEQUENCE_BITS,
+        DEFAULT_NODE_ID_BITS,
+        EPOCH_OFFSET_MILLIS,
+        idGeneratorSource);
+  }
+
+  SnowflakeIdGeneratorImpl(
+      int timestampBits,
+      int sequenceBits,
+      int nodeBits,
+      long epochOffset,
+      IdGeneratorSource idGeneratorSource) {
+    validateArguments(timestampBits, sequenceBits, nodeBits, epochOffset, idGeneratorSource);
+    this.timestampShift = sequenceBits + nodeBits;
+    this.timestampMax = 1L << timestampBits;
+    this.nodeMask = (1L << nodeBits) - 1;
+    this.sequenceBits = sequenceBits;
+    this.sequenceMask = (1L << sequenceBits) - 1;
+    this.epochOffset = epochOffset;
+    this.idGeneratorSource = idGeneratorSource;
+  }
+
+  static void validateArguments(
+      int timestampBits,
+      int sequenceBits,
+      int nodeBits,
+      long epochOffset,
+      IdGeneratorSource idGeneratorSource) {
+    var nowMillis = idGeneratorSource != null ? idGeneratorSource.currentTimeMillis() : -1;
+    var now = Instant.ofEpochMilli(nowMillis);
+    var timestampMax = 1L << timestampBits;
+    checkArgs(
+        () -> checkArgument(idGeneratorSource != null, "IdGeneratorSource must not be null"),
+        () ->
+            checkArgument(
+                nowMillis >= epochOffset,
+                "Clock returns a timestamp %s less than the configured epoch %s",
+                now,
+                Instant.ofEpochMilli(epochOffset)),
+        () ->
+            checkArgument(
+                nowMillis - epochOffset < timestampMax,
+                "Clock already returns a timestamp %s greater of after %s",
+                now,
+                Instant.ofEpochMilli(timestampMax)),
+        () ->
+            checkArgument(
+                nodeBits >= 2
+                    && sequenceBits >= 5
+                    && timestampBits >= 5 // this is REALLY low !
+                    && nodeBits < 64
+                    && sequenceBits < 64
+                    && timestampBits < 64,
+                "value of nodeBits %s or sequenceBits %s or timestampBits %s is too low or too high",
+                nodeBits,
+                sequenceBits,
+                timestampBits),
+        () ->
+            checkArgument(
+                timestampBits + nodeBits + sequenceBits == 63,
+                "Sum of timestampBits + nodeBits + sequenceBits must be == 63"),
+        () -> {
+          if (idGeneratorSource != null) {
+            var nodeId = idGeneratorSource.nodeId();
+            var nodeMax = 1L << nodeBits;
+            checkArgument(
+                nodeId >= 0 && nodeId < nodeMax, "nodeId %s out of range [0..%s[", nodeId, nodeMax);
+          }
+        });
+  }
+
+  static void checkArgs(Runnable... checks) {
+    var violations = new ArrayList<String>();
+    for (Runnable check : checks) {
+      try {
+        check.run();
+      } catch (IllegalArgumentException iae) {
+        violations.add(iae.getMessage());
+      }
+    }
+    if (!violations.isEmpty()) {
+      throw new IllegalArgumentException(String.join(", ", violations));
+    }
+  }
+
+  @Override
+  public long systemIdForNode(int nodeId) {
+    return constructIdUnsafe(timestampMax - 1, 0, nodeId);
+  }
+
+  private long constructIdUnsafe(long timestamp, long sequence, long nodeId) {
+    return (timestamp << timestampShift) | (nodeId << sequenceBits) | sequence;
+  }
+
+  @Override
+  public long constructId(long timestamp, long sequence, long nodeId) {

Review Comment:
   This method effectively bypasses the Id generator source. Maybe it shouldn't be public? Having two methods in the same API that generate IDs using different mechanisms seems error-prone. OTOH this method seems only called from `timeUuidToId`, in this same class, so it seems it could be private.



##########
persistence/nosql/idgen/impl/src/main/java/org/apache/polaris/ids/impl/SnowflakeIdGeneratorFactory.java:
##########
@@ -0,0 +1,107 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.ids.impl;
+
+import static org.apache.polaris.ids.impl.SnowflakeIdGeneratorImpl.validateArguments;
+
+import java.time.Instant;
+import java.util.Map;
+import java.util.function.LongSupplier;
+import org.apache.polaris.ids.api.SnowflakeIdGenerator;
+import org.apache.polaris.ids.spi.IdGeneratorFactory;
+import org.apache.polaris.ids.spi.IdGeneratorSource;
+
+public class SnowflakeIdGeneratorFactory implements IdGeneratorFactory<SnowflakeIdGenerator> {
+  @Override
+  public void validateParameters(Map<String, String> params, IdGeneratorSource idGeneratorSource) {
+    int timestampBits =
+        Integer.parseInt(
+            params.getOrDefault(
+                "timestamp-bits", "" + SnowflakeIdGenerator.DEFAULT_TIMESTAMP_BITS));
+    int nodeIdBits =
+        Integer.parseInt(
+            params.getOrDefault("node-id-bits", "" + SnowflakeIdGenerator.DEFAULT_NODE_ID_BITS));
+    int sequenceBits =
+        Integer.parseInt(
+            params.getOrDefault("sequence-bits", "" + SnowflakeIdGenerator.DEFAULT_SEQUENCE_BITS));
+    var offsetMillis = SnowflakeIdGenerator.EPOCH_OFFSET_MILLIS;
+    var offset = params.get("offset");
+    if (offset != null) {
+      offsetMillis = Instant.parse(offset).toEpochMilli();
+    }
+
+    validateArguments(timestampBits, sequenceBits, nodeIdBits, offsetMillis, idGeneratorSource);
+  }
+
+  @Override
+  public SnowflakeIdGenerator buildSystemIdGenerator(
+      Map<String, String> params, LongSupplier clockMillis) {
+    return buildIdGenerator(
+        params,
+        new IdGeneratorSource() {
+          @Override
+          public int nodeId() {
+            return 0;
+          }
+
+          @Override
+          public long currentTimeMillis() {
+            return SnowflakeIdGenerator.EPOCH_OFFSET_MILLIS;

Review Comment:
   This looks suspicious as it is returning a fixed timestamp. Did you mean `clockMillis.getAsLong()`?
   
   ```suggestion
               return clockMillis.getAsLong();
   ```



##########
persistence/nosql/idgen/api/src/main/java/org/apache/polaris/ids/api/SnowflakeIdGenerator.java:
##########
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.ids.api;
+
+import jakarta.annotation.Nonnull;
+import java.time.Instant;
+import java.time.ZoneId;
+import java.util.UUID;
+
+public interface SnowflakeIdGenerator extends IdGenerator {
+  /** Offset of the snowflake ID generator since the 1970-01-01T00:00:00Z epoch instant. */
+  Instant EPOCH_OFFSET =
+      Instant.EPOCH.atZone(ZoneId.of("GMT")).withYear(2025).withMonth(3).toInstant();
+
+  /**
+   * Offset of the snowflake ID generator in milliseconds since the 1970-01-01T00:00:00Z epoch
+   * instant.
+   */
+  long EPOCH_OFFSET_MILLIS = EPOCH_OFFSET.toEpochMilli();
+
+  int DEFAULT_NODE_ID_BITS = 10;
+  int DEFAULT_TIMESTAMP_BITS = 41;
+  int DEFAULT_SEQUENCE_BITS = 12;
+
+  long constructId(long timestamp, long sequence, long node);
+
+  long timestampFromId(long id);

Review Comment:
   I suppose this is understood as "since the Snowflake Epoch (2025-03-01)". Could be good to add javadocs to clarify.



##########
persistence/nosql/idgen/api/src/main/java/org/apache/polaris/ids/api/SnowflakeIdGenerator.java:
##########
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.ids.api;
+
+import jakarta.annotation.Nonnull;
+import java.time.Instant;
+import java.time.ZoneId;
+import java.util.UUID;
+
+public interface SnowflakeIdGenerator extends IdGenerator {
+  /** Offset of the snowflake ID generator since the 1970-01-01T00:00:00Z epoch instant. */
+  Instant EPOCH_OFFSET =
+      Instant.EPOCH.atZone(ZoneId.of("GMT")).withYear(2025).withMonth(3).toInstant();
+
+  /**
+   * Offset of the snowflake ID generator in milliseconds since the 1970-01-01T00:00:00Z epoch
+   * instant.
+   */
+  long EPOCH_OFFSET_MILLIS = EPOCH_OFFSET.toEpochMilli();
+
+  int DEFAULT_NODE_ID_BITS = 10;
+  int DEFAULT_TIMESTAMP_BITS = 41;
+  int DEFAULT_SEQUENCE_BITS = 12;
+
+  long constructId(long timestamp, long sequence, long node);
+
+  long timestampFromId(long id);
+
+  long timestampUtcFromId(long id);

Review Comment:
   "Timestamp UTC" does not make sense to me 🤔
   From the implementation class, it seems you meant "timestamp since Unix Epoch" instead.



##########
persistence/nosql/idgen/api/src/main/java/org/apache/polaris/ids/api/SnowflakeIdGenerator.java:
##########
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.ids.api;
+
+import jakarta.annotation.Nonnull;
+import java.time.Instant;
+import java.time.ZoneId;
+import java.util.UUID;
+
+public interface SnowflakeIdGenerator extends IdGenerator {
+  /** Offset of the snowflake ID generator since the 1970-01-01T00:00:00Z epoch instant. */
+  Instant EPOCH_OFFSET =

Review Comment:
   Also, the expression is unnecessarily complex, the below one is 100% equivalent and imho easier to grasp:
   
   ```java
   Instant.parse("2025-03-01T00:00:00Z")
   ```



##########
persistence/nosql/idgen/impl/src/main/java/org/apache/polaris/ids/impl/SnowflakeIdGeneratorImpl.java:
##########
@@ -0,0 +1,408 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.ids.impl;
+
+import static com.google.common.base.Preconditions.checkArgument;
+import static com.google.common.base.Preconditions.checkState;
+import static java.lang.String.format;
+
+import com.google.common.annotations.VisibleForTesting;
+import jakarta.annotation.Nonnull;
+import java.time.Instant;
+import java.time.LocalDateTime;
+import java.time.ZoneOffset;
+import java.util.ArrayList;
+import java.util.UUID;
+import java.util.concurrent.atomic.AtomicLongFieldUpdater;
+import org.apache.polaris.ids.api.MonotonicClock;
+import org.apache.polaris.ids.api.SnowflakeIdGenerator;
+import org.apache.polaris.ids.spi.IdGeneratorSource;
+
+/**
+ * Implementation of a local, per-node generator for so-called "snowflake IDs", which are unique
+ * integer IDs in a distributed environment.
+ *
+ * <p>A monotonically increasing clock is <em>strictly required</em>. Invocations of {@link
+ * #generateId()} fail hard, if the clock walks backwards, which means it returns a lower value than
+ * before. It is recommended to use an implementation of {@link MonotonicClock} as the clock source.
+ *
+ * <p>The implementation is thread-safe.
+ *
+ * <p>Reference: <a
+ * href="https://medium.com/@jitenderkmr/demystifying-snowflake-ids-a-unique-identifier-in-distributed-computing-72796a827c9d">Article
+ * on medium.com</a>, <a
+ * href="https://github.com/twitter-archive/snowflake/tree/b3f6a3c6ca8e1b6847baa6ff42bf72201e2c2231">Twitter
+ * GitHub repository (archived)</a>
+ */
+class SnowflakeIdGeneratorImpl implements SnowflakeIdGenerator {
+
+  // TODO add a specialized implementation using hard-coded values for the standardized parameters
+
+  private static final AtomicLongFieldUpdater<SnowflakeIdGeneratorImpl> LAST_ID_UPDATER =
+      AtomicLongFieldUpdater.newUpdater(SnowflakeIdGeneratorImpl.class, "lastId");
+
+  private final IdGeneratorSource idGeneratorSource;
+
+  // Used in hot generateId()
+  private volatile long lastId;
+  private final long epochOffset;
+  private final long timestampMax;
+  private final int timestampShift;
+  private final int sequenceBits;
+  private final long sequenceMask;
+  private final long nodeMask;
+
+  SnowflakeIdGeneratorImpl(IdGeneratorSource idGeneratorSource) {
+    this(
+        DEFAULT_TIMESTAMP_BITS,
+        DEFAULT_SEQUENCE_BITS,
+        DEFAULT_NODE_ID_BITS,
+        EPOCH_OFFSET_MILLIS,
+        idGeneratorSource);
+  }
+
+  SnowflakeIdGeneratorImpl(
+      int timestampBits,
+      int sequenceBits,
+      int nodeBits,
+      long epochOffset,
+      IdGeneratorSource idGeneratorSource) {
+    validateArguments(timestampBits, sequenceBits, nodeBits, epochOffset, idGeneratorSource);
+    this.timestampShift = sequenceBits + nodeBits;
+    this.timestampMax = 1L << timestampBits;
+    this.nodeMask = (1L << nodeBits) - 1;
+    this.sequenceBits = sequenceBits;
+    this.sequenceMask = (1L << sequenceBits) - 1;
+    this.epochOffset = epochOffset;
+    this.idGeneratorSource = idGeneratorSource;
+  }
+
+  static void validateArguments(
+      int timestampBits,
+      int sequenceBits,
+      int nodeBits,
+      long epochOffset,
+      IdGeneratorSource idGeneratorSource) {
+    var nowMillis = idGeneratorSource != null ? idGeneratorSource.currentTimeMillis() : -1;
+    var now = Instant.ofEpochMilli(nowMillis);
+    var timestampMax = 1L << timestampBits;
+    checkArgs(
+        () -> checkArgument(idGeneratorSource != null, "IdGeneratorSource must not be null"),
+        () ->
+            checkArgument(
+                nowMillis >= epochOffset,
+                "Clock returns a timestamp %s less than the configured epoch %s",
+                now,
+                Instant.ofEpochMilli(epochOffset)),
+        () ->
+            checkArgument(
+                nowMillis - epochOffset < timestampMax,
+                "Clock already returns a timestamp %s greater of after %s",
+                now,
+                Instant.ofEpochMilli(timestampMax)),
+        () ->
+            checkArgument(
+                nodeBits >= 2
+                    && sequenceBits >= 5
+                    && timestampBits >= 5 // this is REALLY low !
+                    && nodeBits < 64
+                    && sequenceBits < 64
+                    && timestampBits < 64,
+                "value of nodeBits %s or sequenceBits %s or timestampBits %s is too low or too high",
+                nodeBits,
+                sequenceBits,
+                timestampBits),
+        () ->
+            checkArgument(
+                timestampBits + nodeBits + sequenceBits == 63,
+                "Sum of timestampBits + nodeBits + sequenceBits must be == 63"),
+        () -> {
+          if (idGeneratorSource != null) {
+            var nodeId = idGeneratorSource.nodeId();
+            var nodeMax = 1L << nodeBits;
+            checkArgument(
+                nodeId >= 0 && nodeId < nodeMax, "nodeId %s out of range [0..%s[", nodeId, nodeMax);
+          }
+        });
+  }
+
+  static void checkArgs(Runnable... checks) {
+    var violations = new ArrayList<String>();
+    for (Runnable check : checks) {
+      try {
+        check.run();
+      } catch (IllegalArgumentException iae) {
+        violations.add(iae.getMessage());
+      }
+    }
+    if (!violations.isEmpty()) {
+      throw new IllegalArgumentException(String.join(", ", violations));
+    }
+  }
+
+  @Override
+  public long systemIdForNode(int nodeId) {
+    return constructIdUnsafe(timestampMax - 1, 0, nodeId);
+  }
+
+  private long constructIdUnsafe(long timestamp, long sequence, long nodeId) {
+    return (timestamp << timestampShift) | (nodeId << sequenceBits) | sequence;
+  }
+
+  @Override
+  public long constructId(long timestamp, long sequence, long nodeId) {
+    checkArgument(
+        (timestamp & (timestampMax - 1)) != timestampMax - 1,
+        "timestamp argument %s out of range",
+        timestamp);
+    checkArgument(
+        (sequence & sequenceMask) == sequence, "sequence argument %s out of range", sequence);
+    checkArgument((nodeId & nodeMask) == nodeId, "nodeId argument %s out of range", nodeId);
+    return constructIdUnsafe(timestamp, sequence, nodeId);
+  }
+
+  @Override
+  public long generateId() {
+    var nodeId = idGeneratorSource.nodeId();
+    checkState(nodeId >= 0, "Cannot generate a new ID, shutting down?");
+    var nodeIdPattern = ((long) nodeId) << sequenceBits;

Review Comment:
   Nit: move this variable declaration to line 228. It's not needed before.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(idgen): Start Implementation of NoSQL with the ID Generation Framework [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #2131:
URL: https://github.com/apache/polaris/pull/2131#discussion_r2319439782


##########
persistence/nosql/idgen/spi/build.gradle.kts:
##########
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+plugins {
+  alias(libs.plugins.jandex)
+  id("polaris-server")
+}
+
+description = "Polaris ID generation SPI"
+
+dependencies {
+  implementation(project(":polaris-idgen-api"))
+
+  compileOnly(libs.jakarta.annotation.api)
+  compileOnly(libs.jakarta.validation.api)
+  compileOnly(libs.jakarta.inject.api)
+  compileOnly(libs.jakarta.enterprise.cdi.api)
+
+  compileOnly(libs.smallrye.config.core)
+  compileOnly(platform(libs.quarkus.bom))
+  compileOnly("io.quarkus:quarkus-core")
+
+  compileOnly(project(":polaris-immutables"))

Review Comment:
   #2494



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] WIP feat(idgen): Start Implementation of NoSQL with the ID Generation Framework [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #2131:
URL: https://github.com/apache/polaris/pull/2131#discussion_r2216054902


##########
persistence/nosql/idgen/api/src/main/java/org/apache/polaris/ids/api/MonotonicClock.java:
##########
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.ids.api;
+
+import java.time.Instant;
+
+/**
+ * Provides a clock providing the current time in milliseconds, microseconds and instant since
+ * 1970-01-01-00:00:00.000. The returned timestamp values increase monotonically.
+ *
+ * <p>The functions provide nanosecond/microsecond/millisecond precision, but not necessarily the
+ * same resolution (how frequently the value changes) - no guarantees are made.
+ *
+ * <p>Implementation <em>may</em> adjust to wall clocks advancing faster than the real time. If and
+ * how exactly depends on the implementation, as long as none of the time values available via this
+ * interface "goes backwards".
+ *
+ * <p>Implementer notes: {@link System#nanoTime() System.nanoTime()} does not guarantee that the
+ * values will be monotonically increasing when invocations happen from different
+ * CPUs/cores/threads.
+ *
+ * <p>A default implementation of {@link MonotonicClock} can be injected as an application scoped
+ * bean in CDI.
+ */
+public interface MonotonicClock extends AutoCloseable {
+  /**
+   * Current timestamp as microseconds since epoch, can be used as a monotonically increasing wall
+   * clock.
+   */
+  long currentTimeMicros();
+
+  /**
+   * Current timestamp as milliseconds since epoch, can be used as a monotonically increasing wall
+   * clock.
+   */
+  long currentTimeMillis();
+
+  /**
+   * Current instant with nanosecond precision, can be used as a monotonically increasing wall
+   * clock.
+   */
+  Instant currentInstant();
+
+  /** Monotonically increasing timestamp with nanosecond precision, not related to wall clock. */
+  long nanoTime();
+
+  void sleepMillis(long millis);
+
+  @Override
+  void close();

Review Comment:
   noted



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] WIP feat(idgen): Start Implementation of NoSQL with the ID Generation Framework [polaris]

Posted by "snazy (via GitHub)" <gi...@apache.org>.
snazy commented on code in PR #2131:
URL: https://github.com/apache/polaris/pull/2131#discussion_r2215052076


##########
persistence/nosql/idgen/impl/src/main/resources/META-INF/beans.xml:
##########
@@ -0,0 +1,24 @@
+<!--
+  ~ Licensed to the Apache Software Foundation (ASF) under one
+  ~ or more contributor license agreements.  See the NOTICE file
+  ~ distributed with this work for additional information
+  ~ regarding copyright ownership.  The ASF licenses this file
+  ~ to you under the Apache License, Version 2.0 (the
+  ~ "License"); you may not use this file except in compliance
+  ~ with the License.  You may obtain a copy of the License at
+  ~
+  ~   http://www.apache.org/licenses/LICENSE-2.0
+  ~
+  ~ Unless required by applicable law or agreed to in writing,
+  ~ software distributed under the License is distributed on an
+  ~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+  ~ KIND, either express or implied.  See the License for the
+  ~ specific language governing permissions and limitations
+  ~ under the License.
+  -->
+
+<beans xmlns="https://jakarta.ee/xml/ns/jakartaee"
+  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+  xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/beans_4_0.xsd">
+    <!-- File required by Weld (used for testing), not by Quarkus -->

Review Comment:
   no, there's an application scoped bean in here



##########
persistence/nosql/idgen/api/src/main/java/org/apache/polaris/ids/api/IdGenerator.java:
##########
@@ -0,0 +1,44 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.ids.api;
+
+public interface IdGenerator {
+  /** Generate a new, unique ID. */
+  long generateId();
+
+  /** Generate the system ID for a node, solely used by/for node management purposes. */
+  long systemIdForNode(int nodeId);

Review Comment:
   This function isn't specific to the particular implementation.
   See the upcoming node-id-lease stuff: there is _one_ implementation with _one_ constant configuration per setup.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(idgen): Start Implementation of NoSQL with the ID Generation Framework [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on PR #2131:
URL: https://github.com/apache/polaris/pull/2131#issuecomment-3245993163

   Resolved conflicts


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] feat(idgen): Start Implementation of NoSQL with the ID Generation Framework [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b merged PR #2131:
URL: https://github.com/apache/polaris/pull/2131


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] WIP feat(idgen): Start Implementation of NoSQL with the ID Generation Framework [polaris]

Posted by "dimas-b (via GitHub)" <gi...@apache.org>.
dimas-b commented on code in PR #2131:
URL: https://github.com/apache/polaris/pull/2131#discussion_r2216778666


##########
persistence/nosql/idgen/spi/src/main/java/org/apache/polaris/ids/spi/IdGeneratorFactory.java:
##########
@@ -0,0 +1,43 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.ids.spi;
+
+import java.util.Map;
+import java.util.ServiceLoader;
+import java.util.function.LongSupplier;
+import org.apache.polaris.ids.api.IdGenerator;
+
+public interface IdGeneratorFactory<I extends IdGenerator> {
+  String name();
+
+  void validateParameters(Map<String, String> params, IdGeneratorSource idGeneratorSource);
+
+  I buildIdGenerator(Map<String, String> params, IdGeneratorSource idGeneratorSource);
+
+  I buildSystemIdGenerator(Map<String, String> params, LongSupplier clockMillis);

Review Comment:
   Closing this thread for brevity. Let's continue on the linked thread.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] WIP feat(idgen): Start Implementation of NoSQL with the ID Generation Framework [polaris]

Posted by "snazy (via GitHub)" <gi...@apache.org>.
snazy commented on code in PR #2131:
URL: https://github.com/apache/polaris/pull/2131#discussion_r2215036515


##########
persistence/nosql/idgen/api/src/main/java/org/apache/polaris/ids/api/MonotonicClock.java:
##########
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.polaris.ids.api;
+
+import java.time.Instant;
+
+/**
+ * Provides a clock providing the current time in milliseconds, microseconds and instant since
+ * 1970-01-01-00:00:00.000. The returned timestamp values increase monotonically.
+ *
+ * <p>The functions provide nanosecond/microsecond/millisecond precision, but not necessarily the
+ * same resolution (how frequently the value changes) - no guarantees are made.
+ *
+ * <p>Implementation <em>may</em> adjust to wall clocks advancing faster than the real time. If and
+ * how exactly depends on the implementation, as long as none of the time values available via this
+ * interface "goes backwards".
+ *
+ * <p>Implementer notes: {@link System#nanoTime() System.nanoTime()} does not guarantee that the
+ * values will be monotonically increasing when invocations happen from different
+ * CPUs/cores/threads.
+ *
+ * <p>A default implementation of {@link MonotonicClock} can be injected as an application scoped
+ * bean in CDI.
+ */
+public interface MonotonicClock extends AutoCloseable {
+  /**
+   * Current timestamp as microseconds since epoch, can be used as a monotonically increasing wall
+   * clock.
+   */
+  long currentTimeMicros();
+
+  /**
+   * Current timestamp as milliseconds since epoch, can be used as a monotonically increasing wall
+   * clock.
+   */
+  long currentTimeMillis();
+
+  /**
+   * Current instant with nanosecond precision, can be used as a monotonically increasing wall
+   * clock.
+   */
+  Instant currentInstant();
+
+  /** Monotonically increasing timestamp with nanosecond precision, not related to wall clock. */
+  long nanoTime();
+
+  void sleepMillis(long millis);
+
+  @Override
+  void close();

Review Comment:
   Eliminiate the _checked_ exception.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscribe@polaris.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org