001/* 002 * Licensed to the Apache Software Foundation (ASF) under one 003 * or more contributor license agreements. See the NOTICE file 004 * distributed with this work for additional information 005 * regarding copyright ownership. The ASF licenses this file 006 * to you under the Apache License, Version 2.0 (the 007 * "License"); you may not use this file except in compliance 008 * with the License. You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.apache.hadoop.hbase.zookeeper; 019 020import java.util.concurrent.CountDownLatch; 021import org.apache.yetus.audience.InterfaceAudience; 022import org.apache.zookeeper.KeeperException; 023import org.slf4j.Logger; 024import org.slf4j.LoggerFactory; 025 026/** 027 * A ZooKeeper watcher meant to detect deletions of ZNodes. 028 */ 029@InterfaceAudience.Private 030public class DeletionListener extends ZKListener { 031 032 private static final Logger LOG = LoggerFactory.getLogger(DeletionListener.class); 033 034 private final String pathToWatch; 035 private final CountDownLatch deletedLatch; 036 037 private volatile Throwable exception; 038 039 /** 040 * Create a new instance of the deletion watcher. 041 * @param zkWatcher ZookeeperWatcher instance 042 * @param pathToWatch (Fully qualified) ZNode path that we are waiting to be deleted. 043 * @param deletedLatch Count down on this latch when deletion has occurred. 044 */ 045 public DeletionListener(ZKWatcher zkWatcher, String pathToWatch, CountDownLatch deletedLatch) { 046 super(zkWatcher); 047 this.pathToWatch = pathToWatch; 048 this.deletedLatch = deletedLatch; 049 exception = null; 050 } 051 052 /** 053 * Check if an exception has occurred when re-setting the watch. 054 * @return True if we were unable to re-set a watch on a ZNode due to an exception. 055 */ 056 public boolean hasException() { 057 return exception != null; 058 } 059 060 /** 061 * Get the last exception which has occurred when re-setting the watch. Use hasException() to 062 * check whether or not an exception has occurred. 063 * @return The last exception observed when re-setting the watch. 064 */ 065 public Throwable getException() { 066 return exception; 067 } 068 069 @Override 070 public void nodeDataChanged(String path) { 071 if (!path.equals(pathToWatch)) { 072 return; 073 } 074 try { 075 if (!(ZKUtil.setWatchIfNodeExists(watcher, pathToWatch))) { 076 deletedLatch.countDown(); 077 } 078 } catch (KeeperException ex) { 079 exception = ex; 080 deletedLatch.countDown(); 081 LOG.error("Error when re-setting the watch on " + pathToWatch, ex); 082 } 083 } 084 085 @Override 086 public void nodeDeleted(String path) { 087 if (!path.equals(pathToWatch)) { 088 return; 089 } 090 if (LOG.isDebugEnabled()) { 091 LOG.debug("Processing delete on {}", pathToWatch); 092 } 093 deletedLatch.countDown(); 094 } 095}