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 23:42:04 UTC
[GitHub] [ozone] lamber-ken commented on a change in pull request #1810: HDDS-4651. Implement a sequence ID generator
lamber-ken commented on a change in pull request #1810:
URL: https://github.com/apache/ozone/pull/1810#discussion_r559257312
##########
File path: hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SequenceIDGenerator.java
##########
@@ -0,0 +1,50 @@
+/*
+ * 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 org.apache.hadoop.hdds.scm.exceptions.SCMException;
+
+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);
+ private final long lower34BitsMask = 0x3FFFFFFFFL;
+
+ public SequenceIDGenerator(long term) {
+ // move term to higher 30 bits and save it into curTermOnHigher30Bits.
+ curTermOnHigher30Bits = term << 34L;
+ }
+
+ public long nextID() throws SCMException {
+ long l = counter.getAndIncrement();
+ long countOnLower34Bits = l & lower34BitsMask;
+ if (countOnLower34Bits != l) {
+ throw new SCMException(
+ String.format("ID generator generates a non unique id, " +
+ "term:{}, count:{} ", curTermOnHigher30Bits >> 34L, l),
Review comment:
here, String format should use `%`, not `{}`.
----------------------------------------------------------------
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