Skip to content

World's most powerful open data catalog for building a high-performance, geo-distributed and federated metadata lake.

[#2165] Improvement(core): make operator calculation clearer (#2176)

YongXingc5070c10e703
diff --git a/core/src/main/java/com/datastrato/gravitino/storage/kv/TransactionIdGeneratorImpl.java b/core/src/main/java/com/datastrato/gravitino/storage/kv/TransactionIdGeneratorImpl.java
index e9554c444c..90e8844089 100644
--- a/core/src/main/java/com/datastrato/gravitino/storage/kv/TransactionIdGeneratorImpl.java
+++ b/core/src/main/java/com/datastrato/gravitino/storage/kv/TransactionIdGeneratorImpl.java
@@ -116,13 +116,13 @@ public class TransactionIdGeneratorImpl implements TransactionIdGenerator {
}
/**
- * We use the timestamp as the high 48 bits and the incrementId as the low 16 bits. The timestamp
+ * We use the timestamp as the high 46 bits and the incrementId as the low 18 bits. The timestamp
* is always incremental.
*/
@Override
public synchronized long nextId() {
incrementId++;
- if (incrementId >= (1 << 18 - 1)) {
+ if (incrementId >= ((1 << 18) - 1)) {
incrementId = 0;
}
diff --git a/core/src/test/java/com/datastrato/gravitino/storage/kv/TestTransactionIdGenerator.java b/core/src/test/java/com/datastrato/gravitino/storage/kv/TestTransactionIdGenerator.java
index 376fa6407e..70792e3b65 100644
--- a/core/src/test/java/com/datastrato/gravitino/storage/kv/TestTransactionIdGenerator.java
+++ b/core/src/test/java/com/datastrato/gravitino/storage/kv/TestTransactionIdGenerator.java
@@ -62,6 +62,34 @@ public class TestTransactionIdGenerator {
Assertions.assertNotNull(kvBackend.get(TransactionIdGeneratorImpl.LAST_TIMESTAMP));
}
+ @Test
+ void testNextId() throws IOException {
+ Config config = getConfig();
+ KvBackend kvBackend = getKvBackEnd(config);
+ TransactionIdGenerator transactionIdGenerator =
+ new TransactionIdGeneratorImpl(kvBackend, config);
+ transactionIdGenerator.start();
+
+ long id1 = transactionIdGenerator.nextId();
+ long id2 = transactionIdGenerator.nextId();
+
+ // Test that nextId generates different IDs
+ Assertions.assertNotEquals(id1, id2);
+ // Test that nextId generates increasing IDs
+ Assertions.assertTrue(id2 > id1);
+
+ // Test that incrementId reset to 0 after reaching its maximum value
+ for (int i = 2; i < (1 << 18); i++) {
+ transactionIdGenerator.nextId();
+ }
+ long idAfterReset = transactionIdGenerator.nextId();
+ Assertions.assertTrue(idAfterReset > id2);
+
+ // Test that nextId generates increasing IDs even after incrementId reset
+ long idAfterReset2 = transactionIdGenerator.nextId();
+ Assertions.assertTrue(idAfterReset2 > idAfterReset);
+ }
+
@ParameterizedTest
@ValueSource(ints = {16})
@Disabled("It's very time-consuming, so we disable it by default.")