This is an automated email from the ASF dual-hosted git repository.
shashikant pushed a commit to branch HDDS-2823
in repository https://gitbox.apache.org/repos/asf/ozone.git
The following commit(s) were added to refs/heads/HDDS-2823 by this push:
new cb79d30 HDDS-4651. Implement a sequence ID generator (#1810)
cb79d30 is described below
commit cb79d3031259841a949c07883b5564b89af67b4c
Author: Rui Wang <[email protected]>
AuthorDate: Wed Jan 20 06:45:20 2021 -0800
HDDS-4651. Implement a sequence ID generator (#1810)
---
.../hadoop/hdds/scm/ha/SequenceIDGenerator.java | 62 ++++++++++++++++++++++
.../hdds/scm/ha/TestSequenceIDGenerator.java | 40 ++++++++++++++
2 files changed, 102 insertions(+)
diff --git
a/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SequenceIDGenerator.java
b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SequenceIDGenerator.java
new file mode 100644
index 0000000..2e9ccd7
--- /dev/null
+++
b/hadoop-hdds/server-scm/src/main/java/org/apache/hadoop/hdds/scm/ha/SequenceIDGenerator.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
+ * <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 org.apache.hadoop.util.ExitUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+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 static final Logger LOG =
+ LoggerFactory.getLogger(SequenceIDGenerator.class);
+
+ // term on higher 30 bits
+ private final long curTermOnHigherBits;
+ private final AtomicLong counter = new AtomicLong(0L);
+ // lower 34 bits mask
+ private static final long LOWER_BITS_MASK = 0x3FFFFFFFFL;
+
+ public SequenceIDGenerator(long term) {
+ // move term to higher 30 bits and save it into curTermOnHigher30Bits.
+ curTermOnHigherBits = term << 34L;
+ }
+
+ public long nextID() throws SCMException {
+ long l = counter.getAndIncrement();
+ long countOnLower34Bits = l & LOWER_BITS_MASK;
+ if (countOnLower34Bits != l) {
+ Throwable t = new SCMException(
+ String.format("ID generator generates a non unique id, " +
+ "term:%s, count:%s", curTermOnHigherBits >> 34L, l),
+ SCMException.ResultCodes.INTERNAL_ERROR);
+ // When IDs are exhausted for a term, we should terminate SCM to force
+ // a leader re-election.
+ LOG.error("Facing a fatal problem in SequenceIDGenerator", t);
+ ExitUtil.terminate(1, t);
+ }
+ return countOnLower34Bits | curTermOnHigherBits;
+ }
+}
diff --git
a/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/ha/TestSequenceIDGenerator.java
b/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/ha/TestSequenceIDGenerator.java
new file mode 100644
index 0000000..6a5a42b
--- /dev/null
+++
b/hadoop-hdds/server-scm/src/test/java/org/apache/hadoop/hdds/scm/ha/TestSequenceIDGenerator.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 org.junit.Assert;
+import org.junit.Test;
+
+public class TestSequenceIDGenerator {
+ @Test
+ public void testSequenceIDGen() throws Exception {
+ SequenceIDGenerator idGenerator = new SequenceIDGenerator(0);
+ Assert.assertEquals(0L, idGenerator.nextID());
+ Assert.assertEquals(1L, idGenerator.nextID());
+ Assert.assertEquals(2L, idGenerator.nextID());
+
+ idGenerator = new SequenceIDGenerator(1);
+ Assert.assertEquals(17179869184L, idGenerator.nextID());
+ Assert.assertEquals(17179869185L, idGenerator.nextID());
+ Assert.assertEquals(17179869186L, idGenerator.nextID());
+
+ idGenerator = new SequenceIDGenerator(100);
+ Assert.assertEquals(1717986918400L, idGenerator.nextID());
+ Assert.assertEquals(1717986918401L, idGenerator.nextID());
+ Assert.assertEquals(1717986918402L, idGenerator.nextID());
+ }
+}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]