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.hbase.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) throws IOException {
52 TreeSet<String> writeChildren =
53 new TreeSet<String>(ZNodeComparator.COMPARATOR);
54 for (String child : children) {
55 if (isChildWriteLock(child)) {
56 writeChildren.add(child);
57 }
58 }
59 if (writeChildren.isEmpty()) {
60 return null;
61 }
62 SortedSet<String> lowerChildren = writeChildren.headSet(createdZNode);
63 if (lowerChildren.isEmpty()) {
64 return null;
65 }
66 String pathToWatch = lowerChildren.last();
67 String nodeHoldingLock = lowerChildren.first();
68 String znode = ZKUtil.joinZNode(parentLockNode, nodeHoldingLock);
69 handleLockMetadata(znode);
70
71 return pathToWatch;
72 }
73 }