beryllw commented on code in PR #2161:
URL: https://github.com/apache/fluss/pull/2161#discussion_r2615954997


##########
fluss-server/src/main/java/org/apache/fluss/server/kv/autoinc/SegmentIDGenerator.java:
##########
@@ -0,0 +1,195 @@
+/*
+ * 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.fluss.server.kv.autoinc;
+
+import org.apache.fluss.config.ConfigOptions;
+import org.apache.fluss.config.Configuration;
+import org.apache.fluss.metadata.TablePath;
+import org.apache.fluss.server.SequenceIDCounter;
+import org.apache.fluss.utils.concurrent.Scheduler;
+
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import java.util.concurrent.atomic.AtomicBoolean;
+import java.util.concurrent.locks.ReentrantLock;
+
+import static org.apache.fluss.utils.Preconditions.checkArgument;
+import static org.apache.fluss.utils.concurrent.LockUtils.inLock;
+
+/**
+ * SegmentIDGenerator is used to generate auto increment column ID. It uses 
two segments to store
+ * the IDs and prefetch ids in background.
+ */
+public class SegmentIDGenerator implements IncIDGenerator {
+    private static final Logger LOG = 
LoggerFactory.getLogger(SegmentIDGenerator.class);
+    private static final int RETRY_INTERVAL_MS = 100;
+    private static final int MAX_RETRY_TIMES = 200;
+
+    private final SequenceIDCounter sequenceIDCounter;
+    private final TablePath tablePath;
+    private final int schemaId;
+    private final int columnIdx;
+    private final String columnName;
+
+    AutoIncIdSegment[] segments;
+    private int currentSegmentIdx = 0;
+
+    private volatile boolean isInit = false;
+
+    private final AtomicBoolean isFetching = new AtomicBoolean(false);
+    private final long batchSize;
+    private final long lowWaterMark;
+    private final Scheduler fetchIdScheduler;
+
+    private final ReentrantLock lock = new ReentrantLock();
+
+    public SegmentIDGenerator(
+            TablePath tablePath,
+            int schemaId,
+            int columnIdx,
+            String columnName,
+            SequenceIDCounter sequenceIDCounter,
+            Scheduler scheduler,
+            Configuration properties) {
+        double lowWaterMarkRatio =
+                
properties.getDouble(ConfigOptions.TABLE_AUTO_INC_PREFETCH_LOW_WATER_MARK_RATIO);
+        batchSize = 
properties.getLong(ConfigOptions.TABLE_AUTO_INC_BATCH_SIZE);
+        lowWaterMark = (long) (lowWaterMarkRatio * batchSize);
+        checkArgument(lowWaterMark > 0, "auto inc low level water mark must be 
greater than 0");
+
+        this.columnName = columnName;
+        this.tablePath = tablePath;
+        this.schemaId = schemaId;
+        this.columnIdx = columnIdx;
+        this.fetchIdScheduler = scheduler;
+        this.segments = new AutoIncIdSegment[] {new AutoIncIdSegment(), new 
AutoIncIdSegment()};
+        this.sequenceIDCounter = sequenceIDCounter;
+    }
+
+    private void prefetchSegmentIds(int segmentIdx) {

Review Comment:
   Considering this scenario, the optimization of two segments is indeed not 
appropriate. May be, we could discard the ID sequence used in the previous 
snapshot after completing the new snapshot, and retain the IDs associated with 
the current snapshot as well as unused IDs.



-- 
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]

Reply via email to