Uploaded image for project: 'ZooKeeper'
  1. ZooKeeper
  2. ZOOKEEPER-4504

ZKUtil#deleteRecursive causing deadlock in HDFS HA functionality

    XMLWordPrintableJSON

Details

    Description

      Problem and Analysis:
      After integrating ZooKeeper 3.6.3 we observed deadlock in HDFS HA functionality as shown in below thread dumps.

      "main-EventThread" #33 daemon prio=5 os_prio=0 tid=0x00007f9c017f1000 nid=0x101b waiting for monitor entry [0x00007f9bda8a6000]
         java.lang.Thread.State: BLOCKED (on object monitor)
      	at org.apache.hadoop.ha.ActiveStandbyElector.processWatchEvent(ActiveStandbyElector.java:603)
      	- waiting to lock <0x00000000c17986c0> (a org.apache.hadoop.ha.ActiveStandbyElector)
      	at org.apache.hadoop.ha.ActiveStandbyElector$WatcherWithClientRef.process(ActiveStandbyElector.java:1193)
      	at org.apache.zookeeper.ClientCnxn$EventThread.processEvent(ClientCnxn.java:626)
      	at org.apache.zookeeper.ClientCnxn$EventThread.run(ClientCnxn.java:582)
      
      "main" #1 prio=5 os_prio=0 tid=0x00007f9c00060000 nid=0xea3 waiting on condition [0x00007f9c06404000]
         java.lang.Thread.State: WAITING (parking)
      	at sun.misc.Unsafe.park(Native Method)
      	- parking to wait for  <0x00000000c1b383c8> (a java.util.concurrent.Semaphore$NonfairSync)
      	at java.util.concurrent.locks.LockSupport.park(LockSupport.java:175)
      	at java.util.concurrent.locks.AbstractQueuedSynchronizer.parkAndCheckInterrupt(AbstractQueuedSynchronizer.java:838)
      	at java.util.concurrent.locks.AbstractQueuedSynchronizer.doAcquireSharedInterruptibly(AbstractQueuedSynchronizer.java:999)
      	at java.util.concurrent.locks.AbstractQueuedSynchronizer.acquireSharedInterruptibly(AbstractQueuedSynchronizer.java:1306)
      	at java.util.concurrent.Semaphore.acquire(Semaphore.java:467)
      	at org.apache.zookeeper.ZKUtil.deleteInBatch(ZKUtil.java:122)
      	at org.apache.zookeeper.ZKUtil.deleteRecursive(ZKUtil.java:64)
      	at org.apache.zookeeper.ZKUtil.deleteRecursive(ZKUtil.java:76)
      	at org.apache.hadoop.ha.ActiveStandbyElector$1.run(ActiveStandbyElector.java:386)
      	at org.apache.hadoop.ha.ActiveStandbyElector$1.run(ActiveStandbyElector.java:383)
      	at org.apache.hadoop.ha.ActiveStandbyElector.zkDoWithRetries(ActiveStandbyElector.java:1103)
      	at org.apache.hadoop.ha.ActiveStandbyElector.zkDoWithRetries(ActiveStandbyElector.java:1095)
      	at org.apache.hadoop.ha.ActiveStandbyElector.clearParentZNode(ActiveStandbyElector.java:383)
      	- locked <0x00000000c17986c0> (a org.apache.hadoop.ha.ActiveStandbyElector)
      	at org.apache.hadoop.ha.ZKFailoverController.formatZK(ZKFailoverController.java:290)
      	at org.apache.hadoop.ha.ZKFailoverController.doRun(ZKFailoverController.java:227)
      	at org.apache.hadoop.ha.ZKFailoverController.access$000(ZKFailoverController.java:66)
      	at org.apache.hadoop.ha.ZKFailoverController$1.run(ZKFailoverController.java:186)
      	at org.apache.hadoop.ha.ZKFailoverController$1.run(ZKFailoverController.java:182)
      	at java.security.AccessController.doPrivileged(Native Method)
      	at javax.security.auth.Subject.doAs(Subject.java:360)
      	at org.apache.hadoop.security.UserGroupInformation.doAs(UserGroupInformation.java:1741)
      	at org.apache.hadoop.security.SecurityUtil.doAsLoginUserOrFatal(SecurityUtil.java:498)
      	at org.apache.hadoop.ha.ZKFailoverController.run(ZKFailoverController.java:182)
      	at org.apache.hadoop.hdfs.tools.DFSZKFailoverController.main(DFSZKFailoverController.java:220)
      

      org.apache.hadoop.ha.ActiveStandbyElector#clearParentZNode is instance synchronized and calls ZKUtil.deleteRecursive(zk, pathRoot)

      ZKUtil.deleteRecursive is making async delete API call with MultiCallback as it callback.
      As processWatchEvent is being processed, pathRoot or one of the child paths must have set watcher for delete notification.

      When delete API is called, notification comes first to client then the actual delete response.
      In this case both notification and delete response are processed through callback and through common waitingEvents queue one by one.

      First notification is processed, but it cannot complete as it cannot take lock on processWatchEvent() method as lock is already taken by another thread while calling clearParentZNode()
      As delete notification cannot be processed, MultiCallback is not taken from queue for processing. It stays there in the queue forever.

       

      Why this problem was not happening with earlier versions (3.5.x)?

      In earlier ZK versions, ZKUtil.deleteRecursive was using sync delete API. So delete response was processed directly not though the callback.
      Sot both clearParentZNode and processWatchEvent were completing independently.

      Proposed Fix:
      There are two approaches to fix this problem.
      1. We can fix the problem in HDFS, modify the HDFS code to avoid the deadlock. But we may get similar bugs in other projects.
      2. Fix the problem in ZK. Make the API behavior same as the old behavior(use sync API to delete the ZK node) and provide new overloaded API with new behavior(use async API to delete the ZK node)

      I propose to fix the problem with 2nd approach.

      Attachments

        Issue Links

          Activity

            People

              arshad.mohammad Mohammad Arshad
              arshad.mohammad Mohammad Arshad
              Votes:
              0 Vote for this issue
              Watchers:
              5 Start watching this issue

              Dates

                Created:
                Updated:
                Resolved:

                Time Tracking

                  Estimated:
                  Original Estimate - Not Specified
                  Not Specified
                  Remaining:
                  Remaining Estimate - 0h
                  0h
                  Logged:
                  Time Spent - 50m
                  50m