You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@ozone.apache.org by GitBox <gi...@apache.org> on 2021/01/17 15:23:37 UTC

[GitHub] [ozone] GlenGeng commented on a change in pull request #1810: HDDS-4651. Implement a sequence ID generator

GlenGeng commented on a change in pull request #1810:
URL: https://github.com/apache/ozone/pull/1810#discussion_r559196298



##########
File path: hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SequenceIDGenerator.java
##########
@@ -0,0 +1,40 @@
+/*
+ * 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
+ * <p>
+ * <p>http://www.apache.org/licenses/LICENSE-2.0
+ * <p>
+ * <p>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.hadoop.hdds.scm.ha;
+
+import java.util.concurrent.atomic.AtomicLong;
+
+/**
+ * SequenceIDGenerator uses higher 30 bits to save the term, and lower 34 bits
+ * to save a count (increase from 0). Each call of nextID() will increase the
+ * count on the lower 34 bits by 1, thus SequenceIDGenerator always generates
+ * unique number within a specific term.
+ */
+public class SequenceIDGenerator {
+  private final long curTermOnHigher30Bits;
+  private final AtomicLong counter = new AtomicLong(0L);
+
+  public SequenceIDGenerator(long term) {
+    // move term to higher 30 bits and save it into curTermOnHigher30Bits.
+    curTermOnHigher30Bits = term << 34;
+  }
+
+  public long nextID() {
+    long l = counter.getAndIncrement();
+    return l & 0x3FFFFFFF | curTermOnHigher30Bits;

Review comment:
       Thanks @amaliujia for the work !
   
   The up limit of 34 bits is 17 billion, and I consider we have to do such a check, and need to exit SCM when it reaches the limit. Since customer might deploy a single-server raft cluster in the non-HA mode, there will be no leader election after SCM starts.




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

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



---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@ozone.apache.org
For additional commands, e-mail: issues-help@ozone.apache.org