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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
