Package org.apache.hadoop.hbase.zookeeper.lock

Source Code of org.apache.hadoop.hbase.zookeeper.lock.ZKInterProcessWriteLock

/**
* Copyright The Apache Software Foundation
*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements.  See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership.  The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License.  You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.hadoop.hbase.zookeeper.lock;

import java.io.IOException;
import java.util.List;
import java.util.TreeSet;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.classification.InterfaceAudience;
import org.apache.hadoop.hbase.zookeeper.ZKUtil;
import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
import org.apache.zookeeper.KeeperException;

/**
* ZooKeeper based write lock:
*/
@InterfaceAudience.Private
public class ZKInterProcessWriteLock extends ZKInterProcessLockBase {

  private static final Log LOG = LogFactory.getLog(ZKInterProcessWriteLock.class);

  public ZKInterProcessWriteLock(ZooKeeperWatcher zooKeeperWatcher,
      String znode, byte[] metadata, MetadataHandler handler) {
    super(zooKeeperWatcher, znode, metadata, handler, WRITE_LOCK_CHILD_NODE_PREFIX);
  }

  /**
   * {@inheritDoc}
   */
  @Override
  protected String getLockPath(String createdZNode, List<String> children)
  throws IOException, InterruptedException {
    TreeSet<String> sortedChildren =
        new TreeSet<String>(ZNodeComparator.COMPARATOR);
    sortedChildren.addAll(children);
    String pathToWatch = sortedChildren.lower(createdZNode);
    if (pathToWatch != null) {
      String nodeHoldingLock = sortedChildren.first();
      String znode = ZKUtil.joinZNode(parentLockNode, nodeHoldingLock);
      try {
        handleLockMetadata(znode);
      } catch (IOException e) {
        LOG.warn("Error processing lock metadata in " + nodeHoldingLock, e);
      }
    }
    return pathToWatch;
  }

  /**
   * Referred in zk recipe as "Revocable Shared Locks with Freaking Laser Beams"
   * (http://zookeeper.apache.org/doc/trunk/recipes.html).
   */
  public void reapAllLocks() throws IOException {
    List<String> children;
    try {
      children = ZKUtil.listChildrenNoWatch(zkWatcher, parentLockNode);
    } catch (KeeperException e) {
      LOG.error("Unexpected ZooKeeper error when listing children", e);
      throw new IOException("Unexpected ZooKeeper exception", e);
    }

    KeeperException deferred = null;
    for (String child : children) {
      if (isChildWriteLock(child)) {
        String znode = ZKUtil.joinZNode(parentLockNode, child);
        LOG.info("Reaping write lock for znode:" + znode);
        try {
          ZKUtil.deleteNodeFailSilent(zkWatcher, znode);
        } catch (KeeperException ex) {
          LOG.warn("Error reaping the znode for write lock :" + znode);
          deferred = ex;
        }
      }
    }
    if (deferred != null) {
      throw new IOException("ZK exception while reaping locks:", deferred);
    }
  }
}
TOP

Related Classes of org.apache.hadoop.hbase.zookeeper.lock.ZKInterProcessWriteLock

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.