Anonymous avatar Anonymous committed 4ceb537

handle ZK connection loss when adding node to queue

Comments (0)

Files changed (1)

src/main/java/org/cdlib/mrt/queue/DistributedQueue.java

 import org.apache.zookeeper.ZooKeeper;
 import org.apache.zookeeper.data.ACL;
 import org.apache.zookeeper.data.Stat;
+import org.apache.zookeeper.KeeperException.ConnectionLossException;
+
 
 
 /**
         }
     }
         
-    public boolean submit(byte[] data) throws KeeperException, InterruptedException{
+    public boolean submit(byte[] data) throws KeeperException, InterruptedException, ConnectionLossException{
+	int attempts = 0;
+	String node = null;
         while (true) {
+            Item i = new Item(Item.PENDING, data,  new Date());
             try {
-                Item i = new Item(Item.PENDING, data,  new Date());
-                zookeeper.create(this.name, i.getBytes(), 
+                node = zookeeper.create(this.name, i.getBytes(), 
                                  acl, CreateMode.PERSISTENT_SEQUENTIAL);
                 return true;
+            } catch (ConnectionLossException cle) {
+		// did submit fail?
+		if (attempts >= 3) throw new ConnectionLossException();  
+		if (node == null) {  
+                    System.err.println("[error] DistributedQueue.submit lost connection, requeuing: " + cle.getMessage());
+		    attempts++;
+		} else {
+		    return true;
+		}
             } catch (KeeperException.NoNodeException e){
                 zookeeper.create(dir, new byte[0], acl, CreateMode.PERSISTENT);
             }
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.