Zookeeper分布式锁(多进程竞争)实现的代码示例分享

来源:互联网 发布:逍遥天地手游进阶数据 编辑:程序博客网 时间:2024/06/06 09:12

转自:http://blog.csdn.net/yangbutao/article/details/11669609

zookeeper分布式锁在实际的场景中应用很多,比如集群中多个节点的leader选举,数据库master-slave模式的主库的选择等等
解决方案依然很简单,需要加锁的进程先尝试在zookeeper上创建一个临时节点L,如果创建成功则加锁成功,
如果不成功(已存在)则在该节点上设置watch。
进程通过删除L来解锁(当进程意外终止,L也会被删除,不会造成死锁),当L被删除时,其它等待锁的进程会得到通知,
此时这些进程再次创建L来获得锁。
上面的方案,当竞争锁的进程比较多时,解锁时会引起Herd Effect,可对加锁规则进行限制,如按进程尝试加锁的顺序来分配锁。
在zookeeper上,每个加锁的进程创建一个带SEQUENTIAL标志的临时节点,每次让序号最小的节点获得锁,
这样每个节点只需要watch它前面节点的状态即可,当其前面
节点被删除时,其将被通知,并获得锁。
以下是笔者在项目中的分布式锁的示例代码,可供参考

zookeeper中的节点
    /election
          |---90338461809508352-192.168.1.111:8983
          |---90338461809508351-192.168.1.112:8983
          |---90338461809508356-192.168.1.113:8983
    /leader(90338461809508351-192.168.1.112:8983)

代码示例:

[html] view plaincopy
  1. /**  
  2.  * 分布式锁应用示例  
  3.  * 多个进程节点的leader选举  
  4.  * @author yangbutao  
  5.  *   
  6.  */  
  7. public class ElectionMain {  
  8.     private static Logger log = LoggerFactory.getLogger(ElectionMain.class);  
  9.   
  10.     private final static Pattern LEADER_SEQ = Pattern  
  11.             .compile(".*?/?.*?-n_(\\d+)");  
  12.     private final static Pattern SESSION_ID = Pattern  
  13.             .compile(".*?/?(.*?-.*?)-n_\\d+");  
  14.   
  15.     public static void main(String[] args)throws Exception {  
  16.         ZooKeeper zkClient = new ZooKeeper("10.1.1.20:2222", 3000, null);  
  17.         String electionPath = "/election";  
  18.         //当前节点名称  
  19.         String coreNodeName = "192.168.1.111:8983";  
  20.         //客户端节点的sessionId  
  21.         long sessionId = zkClient.getSessionId();  
  22.         String id = sessionId + "-" + coreNodeName;  
  23.         String leaderSeqPath = null;  
  24.         boolean cont = true;  
  25.         int tries = 0;  
  26.         while (cont) {  
  27.             try {  
  28.                 leaderSeqPath = zkClient.create(  
  29.                         electionPath + "/" + id + "-n_", null,  
  30.                         ZooDefs.Ids.OPEN_ACL_UNSAFE,  
  31.                         CreateMode.EPHEMERAL_SEQUENTIAL);  
  32.                 cont = false;  
  33.             } catch (ConnectionLossException e) {  
  34.                 List<String> entries = zkClient.getChildren(electionPath, true);  
  35.                 boolean foundId = false;  
  36.                 for (String entry : entries) {  
  37.                     String nodeId = getNodeId(entry);  
  38.                     if (id.equals(nodeId)) {  
  39.                         foundId = true;  
  40.                         break;  
  41.                     }  
  42.                 }  
  43.                 if (!foundId) {  
  44.                     cont = true;  
  45.                     if (tries++ > 20) {  
  46.                         throw new Exception( "server error", e);  
  47.                     }  
  48.                     try {  
  49.                         Thread.sleep(50);  
  50.                     } catch (InterruptedException e2) {  
  51.                         Thread.currentThread().interrupt();  
  52.                     }  
  53.                 }  
  54.   
  55.             } catch (KeeperException.NoNodeException e) {  
  56.                 if (tries++ > 20) {  
  57.                     throw new Exception( "server error", e);  
  58.                 }  
  59.                 cont = true;  
  60.                 try {  
  61.                     Thread.sleep(50);  
  62.                 } catch (InterruptedException e2) {  
  63.                     Thread.currentThread().interrupt();  
  64.                 }  
  65.             }  
  66.         }  
  67.         int seq = getSeq(leaderSeqPath);  
  68.         checkIfIamLeader(zkClient, seq);  
  69.     }  
  70.   
  71.     private static String getNodeId(String nStringSequence) {  
  72.         String id;  
  73.         Matcher m = SESSION_ID.matcher(nStringSequence);  
  74.         if (m.matches()) {  
  75.             id = m.group(1);  
  76.         } else {  
  77.             throw new IllegalStateException("Could not find regex match in:"  
  78.                     + nStringSequence);  
  79.         }  
  80.         return id;  
  81.     }  
  82.   
  83.     private static int getSeq(String nStringSequence) {  
  84.         int seq = 0;  
  85.         Matcher m = LEADER_SEQ.matcher(nStringSequence);  
  86.         if (m.matches()) {  
  87.             seq = Integer.parseInt(m.group(1));  
  88.         } else {  
  89.             throw new IllegalStateException("Could not find regex match in:"  
  90.                     + nStringSequence);  
  91.         }  
  92.         return seq;  
  93.     }  
  94.   
  95.     /**  
  96.      * 排序seq  
  97.      */  
  98.     private static void sortSeqs(List<String> seqs) {  
  99.         Collections.sort(seqs, new Comparator<String>() {  
  100.   
  101.             @Override  
  102.             public int compare(String o1, String o2) {  
  103.                 return Integer.valueOf(getSeq(o1)).compareTo(  
  104.                         Integer.valueOf(getSeq(o2)));  
  105.             }  
  106.         });  
  107.     }  
  108.   
  109.     private static List<Integer> getSeqs(List<String> seqs) {  
  110.         List<Integer> intSeqs = new ArrayList<Integer>(seqs.size());  
  111.         for (String seq : seqs) {  
  112.             intSeqs.add(getSeq(seq));  
  113.         }  
  114.         return intSeqs;  
  115.     }  
  116.   
  117.     private static void checkIfIamLeader(final ZooKeeper zkClient, final int seq)  
  118.             throws KeeperException, InterruptedException, IOException {  
  119.         // get all other numbers...  
  120.         final String holdElectionPath = "/election";  
  121.         List<String> seqs = zkClient.getChildren(holdElectionPath, true);  
  122.         sortSeqs(seqs);  
  123.         List<Integer> intSeqs = getSeqs(seqs);  
  124.         if (intSeqs.size() == 0) {  
  125.             return;  
  126.         }  
  127.         if (seq <= intSeqs.get(0)) {  
  128.             //删除来的leader节点  
  129.             try {  
  130.                 zkClient.delete("/leader", -1);  
  131.             } catch (Exception e) {  
  132.                 // fine  
  133.             }  
  134.             String seqStr = null;  
  135.             for (String currSeq : seqs) {  
  136.                 if (getSeq(currSeq) == seq) {  
  137.                     seqStr = currSeq;  
  138.                     break;  
  139.                 }  
  140.             }  
  141.             runIamLeaderProcess(zkClient, seqStr);  
  142.         } else {  
  143.             // I am not the leader - watch the node below me  
  144.             //当前节点不是leader,watcher比我小的节点  
  145.             int i = 1;  
  146.             for (; i < intSeqs.size(); i++) {  
  147.                 int s = intSeqs.get(i);  
  148.                 if (seq < s) {  
  149.                     // we found who we come before - watch the guy in front  
  150.                     //发现比我小的节点(节点列表全面经过排序),退出循环  
  151.                     break;  
  152.                 }  
  153.             }  
  154.             int index = i - 2;  
  155.             if (index < 0) {  
  156.                 log.warn("Our node is no longer in line to be leader");  
  157.                 return;  
  158.             }  
  159.             try {  
  160.                 //监控比当前节点seq次小的节点的值变化  
  161.                 zkClient.getData(holdElectionPath + "/" + seqs.get(index),  
  162.                         new Watcher() {  
  163.   
  164.                             @Override  
  165.                             public void process(WatchedEvent event) {  
  166.                                 if (EventType.None.equals(event.getType())) {  
  167.                                     return;  
  168.                                 }  
  169.                                 // 检查是否是可以做为leader  
  170.                                 try {  
  171.                                     checkIfIamLeader(zkClient, seq);  
  172.                                 } catch (InterruptedException e) {  
  173.                                     Thread.currentThread().interrupt();  
  174.                                     log.warn("", e);  
  175.                                 } catch (IOException e) {  
  176.                                     log.warn("", e);  
  177.                                 } catch (Exception e) {  
  178.                                     log.warn("", e);  
  179.                                 }  
  180.                             }  
  181.   
  182.                         }, null, true);  
  183.             } catch (Exception e) {  
  184.                 log.warn("Failed setting watch", e);  
  185.                 checkIfIamLeader(zkClient, seq);  
  186.             }  
  187.         }  
  188.     }  
  189.   
  190.       
  191.     protected static void runIamLeaderProcess(ZooKeeper zkClient, String seqStr)  
  192.             throws KeeperException, InterruptedException, IOException {  
  193.         final String id = seqStr.substring(seqStr.lastIndexOf("/") + 1);  
  194.         //设置leader节点  
  195.         zkClient.create("/leader", id.getBytes(),  
  196.                 ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL);  
  197.     }  
  198.   
  199. }  
0 0
原创粉丝点击