View Javadoc

1   /**
2    * Copyright The Apache Software Foundation
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  package org.apache.hadoop.hbase.zookeeper.lock;
21  
22  import org.apache.hadoop.hbase.classification.InterfaceAudience;
23  import org.apache.hadoop.hbase.InterProcessLock.MetadataHandler;
24  import org.apache.hadoop.hbase.InterProcessReadWriteLock;
25  import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
26  
27  /**
28   * ZooKeeper based implementation of {@link InterProcessReadWriteLock}. This lock is fair,
29   * not reentrant, and not revocable.
30   */
31  @InterfaceAudience.Private
32  public class ZKInterProcessReadWriteLock implements InterProcessReadWriteLock {
33  
34    private final ZooKeeperWatcher zkWatcher;
35    private final String znode;
36    private final MetadataHandler handler;
37  
38    /**
39     * Creates a DistributedReadWriteLock instance.
40     * @param zkWatcher
41     * @param znode ZNode path for the lock
42     * @param handler An object that will handle de-serializing and processing
43     *                the metadata associated with reader or writer locks
44     *                created by this object or null if none desired.
45     */
46    public ZKInterProcessReadWriteLock(ZooKeeperWatcher zkWatcher, String znode,
47        MetadataHandler handler) {
48      this.zkWatcher = zkWatcher;
49      this.znode = znode;
50      this.handler = handler;
51    }
52  
53    /**
54     * {@inheritDoc}
55     */
56    public ZKInterProcessReadLock readLock(byte[] metadata) {
57      return new ZKInterProcessReadLock(zkWatcher, znode, metadata, handler);
58    }
59  
60    /**
61     * {@inheritDoc}
62     */
63    public ZKInterProcessWriteLock writeLock(byte[] metadata) {
64      return new ZKInterProcessWriteLock(zkWatcher, znode, metadata, handler);
65    }
66  }