1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.hadoop.hbase.zookeeper.lock;
21
22 import java.io.IOException;
23 import java.util.List;
24 import java.util.SortedSet;
25 import java.util.TreeSet;
26
27 import org.apache.commons.logging.Log;
28 import org.apache.commons.logging.LogFactory;
29 import org.apache.hadoop.classification.InterfaceAudience;
30 import org.apache.hadoop.hbase.zookeeper.ZKUtil;
31 import org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher;
32
33
34
35
36
37 @InterfaceAudience.Private
38 public class ZKInterProcessReadLock extends ZKInterProcessLockBase {
39
40 private static final Log LOG = LogFactory.getLog(ZKInterProcessReadLock.class);
41
42 public ZKInterProcessReadLock(ZooKeeperWatcher zooKeeperWatcher,
43 String znode, byte[] metadata, MetadataHandler handler) {
44 super(zooKeeperWatcher, znode, metadata, handler, READ_LOCK_CHILD_NODE_PREFIX);
45 }
46
47
48
49
50 @Override
51 protected String getLockPath(String createdZNode, List<String> children)
52 throws IOException, InterruptedException {
53 TreeSet<String> writeChildren =
54 new TreeSet<String>(ZNodeComparator.COMPARATOR);
55 for (String child : children) {
56 if (isChildWriteLock(child)) {
57 writeChildren.add(child);
58 }
59 }
60 if (writeChildren.isEmpty()) {
61 return null;
62 }
63 SortedSet<String> lowerChildren = writeChildren.headSet(createdZNode);
64 if (lowerChildren.isEmpty()) {
65 return null;
66 }
67 String pathToWatch = lowerChildren.last();
68 String nodeHoldingLock = lowerChildren.first();
69 String znode = ZKUtil.joinZNode(parentLockNode, nodeHoldingLock);
70 try {
71 handleLockMetadata(znode);
72 } catch (IOException e) {
73 LOG.warn("Error processing lock metadata in " + nodeHoldingLock, e);
74 }
75 return pathToWatch;
76 }
77
78 @Override
79 public void reapAllLocks() throws IOException {
80 throw new UnsupportedOperationException(
81 "Lock reaping is not supported for ZK based read locks");
82 }
83 }