You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@zookeeper.apache.org by ma...@apache.org on 2021/10/02 05:58:38 UTC

[zookeeper] branch master updated: ZOOKEEPER-4372: Added check and unit test for negative sequence numbers in lock recipe

This is an automated email from the ASF dual-hosted git repository.

maoling pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/zookeeper.git


The following commit(s) were added to refs/heads/master by this push:
     new 2dcc64d  ZOOKEEPER-4372: Added check and unit test for negative sequence numbers in lock recipe
2dcc64d is described below

commit 2dcc64d9411e7b24402222437215ab2fe1e59c2e
Author: Colin McIntosh <cm...@netflix.com>
AuthorDate: Sat Oct 2 13:58:31 2021 +0800

    ZOOKEEPER-4372: Added check and unit test for negative sequence numbers in lock recipe
    
    I took a stab at fixing this and adding a simple case to one of the existing unit tests to check for negative sequence numbers. This is my first time working in the ZK Java implementation so please let me know if I need to make any adjustments. I updated the code to check if there is a prior "-" character in the index before the last "-" and move the index back one if there is.
    
    Author: Colin McIntosh <cm...@netflix.com>
    
    Reviewers: maoling <ma...@apache.org>
    
    Closes #1751 from colinmcintosh/cm/zk-lock-recipe-seq
---
 .../src/main/java/org/apache/zookeeper/recipes/lock/ZNodeName.java | 5 ++++-
 .../test/java/org/apache/zookeeper/recipes/lock/ZNodeNameTest.java | 7 ++++++-
 2 files changed, 10 insertions(+), 2 deletions(-)

diff --git a/zookeeper-recipes/zookeeper-recipes-lock/src/main/java/org/apache/zookeeper/recipes/lock/ZNodeName.java b/zookeeper-recipes/zookeeper-recipes-lock/src/main/java/org/apache/zookeeper/recipes/lock/ZNodeName.java
index 24d6f13..3fc9269 100644
--- a/zookeeper-recipes/zookeeper-recipes-lock/src/main/java/org/apache/zookeeper/recipes/lock/ZNodeName.java
+++ b/zookeeper-recipes/zookeeper-recipes-lock/src/main/java/org/apache/zookeeper/recipes/lock/ZNodeName.java
@@ -51,11 +51,14 @@ class ZNodeName implements Comparable<ZNodeName> {
     public ZNodeName(final String name) {
         this.name = Objects.requireNonNull(name, "ZNode name cannot be null");
 
-        final int idx = name.lastIndexOf('-');
+        int idx = name.lastIndexOf('-');
         if (idx < 0) {
             this.prefix = name;
             this.sequence = Optional.empty();
         } else {
+            if (idx > 0 && name.charAt(idx - 1) == '-') {
+                idx = idx - 1;
+            }
             this.prefix = name.substring(0, idx);
             this.sequence = Optional.ofNullable(parseSequenceString(name.substring(idx + 1)));
         }
diff --git a/zookeeper-recipes/zookeeper-recipes-lock/src/test/java/org/apache/zookeeper/recipes/lock/ZNodeNameTest.java b/zookeeper-recipes/zookeeper-recipes-lock/src/test/java/org/apache/zookeeper/recipes/lock/ZNodeNameTest.java
index aac76cc..7d82627 100644
--- a/zookeeper-recipes/zookeeper-recipes-lock/src/test/java/org/apache/zookeeper/recipes/lock/ZNodeNameTest.java
+++ b/zookeeper-recipes/zookeeper-recipes-lock/src/test/java/org/apache/zookeeper/recipes/lock/ZNodeNameTest.java
@@ -34,7 +34,7 @@ public class ZNodeNameTest {
 
     @Test
     public void testOrderWithSamePrefix() throws Exception {
-        final String[] names = {"x-3", "x-5", "x-11", "x-1"};
+        final String[] names = {"x-3", "x-5", "x-11", "x-1", "x--20"};
         ZNodeName zname;
 
         final Collection<ZNodeName> nodeNames = Arrays.asList(names).stream()
@@ -43,6 +43,11 @@ public class ZNodeNameTest {
         final Iterator<ZNodeName> it = nodeNames.iterator();
 
         zname = it.next();
+        assertEquals("x--20", zname.getName());
+        assertEquals("x", zname.getPrefix());
+        assertEquals(Integer.valueOf(-20), zname.getSequence().get());
+
+        zname = it.next();
         assertEquals("x-1", zname.getName());
         assertEquals("x", zname.getPrefix());
         assertEquals(Integer.valueOf(1), zname.getSequence().get());