Sfoglia il codice sorgente

ZOOKEEPER-44. Create sequence flag children with prefixes of 0's so that they can be lexicographically sorted. (Jakob Homan via mahadev)

git-svn-id: https://svn.apache.org/repos/asf/hadoop/zookeeper/trunk@681910 13f79535-47bb-0310-9956-ffa450edef68
Mahadev Konar 17 anni fa
parent
commit
dbd65f592b

+ 8 - 2
src/java/main/org/apache/zookeeper/ZooKeeper.java

@@ -828,12 +828,15 @@ public class ZooKeeper {
      * triggered by a successful operation that deletes the node of the given
      * triggered by a successful operation that deletes the node of the given
      * path or creates/delete a child under the node.
      * path or creates/delete a child under the node.
      * <p>
      * <p>
+     * The list of children returned is not sorted and no guarantee is provided
+     * as to its natural or lexical order.
+     * <p>
      * A KeeperException with error code KeeperException.NoNode will be thrown
      * A KeeperException with error code KeeperException.NoNode will be thrown
      * if no node with the given path exists.
      * if no node with the given path exists.
      *
      *
      * @param path
      * @param path
      * @param watcher explicit watcher
      * @param watcher explicit watcher
-     * @return an array of children of the node with the given path
+     * @return an unordered array of children of the node with the given path
      * @throws InterruptedException If the server transaction is interrupted.
      * @throws InterruptedException If the server transaction is interrupted.
      * @throws KeeperException If the server signals an error with a non-zero error code.
      * @throws KeeperException If the server signals an error with a non-zero error code.
      */
      */
@@ -864,12 +867,15 @@ public class ZooKeeper {
      * triggered by a successful operation that deletes the node of the given
      * triggered by a successful operation that deletes the node of the given
      * path or creates/delete a child under the node.
      * path or creates/delete a child under the node.
      * <p>
      * <p>
+     * The list of children returned is not sorted and no guarantee is provided
+     * as to its natural or lexical order.
+     * <p>
      * A KeeperException with error code KeeperException.NoNode will be thrown
      * A KeeperException with error code KeeperException.NoNode will be thrown
      * if no node with the given path exists.
      * if no node with the given path exists.
      *
      *
      * @param path
      * @param path
      * @param watch
      * @param watch
-     * @return an array of children of the node with the given path
+     * @return an unordered array of children of the node with the given path
      * @throws InterruptedException If the server transaction is interrupted.
      * @throws InterruptedException If the server transaction is interrupted.
      * @throws KeeperException If the server signals an error with a non-zero error code.
      * @throws KeeperException If the server signals an error with a non-zero error code.
      */
      */

+ 0 - 1
src/java/main/org/apache/zookeeper/server/DataTree.java

@@ -292,7 +292,6 @@ public class DataTree {
         synchronized (n) {
         synchronized (n) {
             ArrayList<String> children = new ArrayList<String>();
             ArrayList<String> children = new ArrayList<String>();
             children.addAll(n.children);
             children.addAll(n.children);
-            Collections.sort(children);
             if (watcher != null) {
             if (watcher != null) {
                 childWatches.addWatch(path, watcher);
                 childWatches.addWatch(path, watcher);
             }
             }

+ 1 - 1
src/java/main/org/apache/zookeeper/server/PrepRequestProcessor.java

@@ -204,7 +204,7 @@ public class PrepRequestProcessor extends Thread implements RequestProcessor {
                         request.authInfo);
                         request.authInfo);
                 int parentCVersion = parentRecord.stat.getCversion();
                 int parentCVersion = parentRecord.stat.getCversion();
                 if ((createRequest.getFlags() & CreateFlags.SEQUENCE) != 0) {
                 if ((createRequest.getFlags() & CreateFlags.SEQUENCE) != 0) {
-                    path = path + parentCVersion;
+                    path = path + String.format("%010d", parentCVersion);
                 }
                 }
                 try {
                 try {
                     if (getRecordForPath(path) != null) {
                     if (getRecordForPath(path) != null) {

+ 41 - 0
src/java/test/org/apache/zookeeper/test/ClientTest.java

@@ -278,6 +278,47 @@ public class ClientTest extends ClientBase implements Watcher {
         }
         }
     }
     }
 
 
+    // Test that sequential filenames are being created correctly,
+    // with 0-padding in the filename
+    public void testSequentialNodeNames() throws IOException, InterruptedException, KeeperException {
+    	String path = "/SEQUENCE";
+	String file = "TEST";
+	String filepath = path + "/" + file;
+
+        ZooKeeper zk = null;
+        try {
+            zk =createClient(this);
+            zk.create(path, new byte[0], Ids.OPEN_ACL_UNSAFE, 0);
+            zk.create(filepath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateFlags.SEQUENCE);
+            List<String> children = zk.getChildren(path, false);
+            assertEquals(1, children.size());
+            assertEquals(file + "0000000000", children.get(0));
+            
+            zk.create(filepath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateFlags.SEQUENCE);
+            children = zk.getChildren(path, false);
+            assertEquals(2, children.size());
+            assertTrue(children.contains(file + "0000000001"));
+            
+            zk.create(filepath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateFlags.SEQUENCE);
+            children = zk.getChildren(path, false);
+            assertEquals(3, children.size());
+            assertTrue(children.contains(file + "0000000002"));
+            
+            // The pattern is holding so far.  Let's run the counter a bit
+            // to be sure it continues to spit out the correct answer
+            for(int i = children.size(); i < 105; i++)
+               zk.create(filepath, new byte[0], Ids.OPEN_ACL_UNSAFE, CreateFlags.SEQUENCE);
+
+            children = zk.getChildren(path, false);
+            assertTrue(children.contains(file + "0000000104"));
+            	
+        }
+        finally {
+        	if(zk != null)
+        		zk.close();
+        }
+    }
+    
     private void notestConnections() throws IOException, InterruptedException, KeeperException {
     private void notestConnections() throws IOException, InterruptedException, KeeperException {
         ZooKeeper zk;
         ZooKeeper zk;
         for(int i = 0; i < 2000; i++) {
         for(int i = 0; i < 2000; i++) {