1,Hmaster主循环主要这里主要有:

1,1 becomeActiveMaster(startupStatus);

1.2 finishInitialization

1.3 loop()

  1. becomeActiveMaster(startupStatus);
  2. // We are either the active master or we were asked to shutdown
  3. if (!this.stopped) {
  4. finishInitialization(startupStatus, false);
  5. loop();
  6. }

2。becomeActiveMaster(startupStatus);

2.1.1首先创建一个ActiveMasterManager,负责watch zk上的事件,这里主要是nodeCreated()。nodeDeleted()

  1. private boolean becomeActiveMaster(MonitoredTask startupStatus)
  2. throws InterruptedException {
  3. // TODO: This is wrong!!!! Should have new servername if we restart ourselves,
  4. // if we come back to life.
  5. //创建activeMasterManager对象
  6. this.activeMasterManager = new ActiveMasterManager(zooKeeper, this.serverName,
  7. this);
  8. //注冊activeMasterManager到zk
  9. this.zooKeeper.registerListener(activeMasterManager);
  10. stallIfBackupMaster(this.conf, this.activeMasterManager);
  11. // The ClusterStatusTracker is setup before the other
  12. // ZKBasedSystemTrackers because it's needed by the activeMasterManager
  13. // to check if the cluster should be shutdown.
  14. this.clusterStatusTracker = new ClusterStatusTracker(getZooKeeper(), this);
  15. this.clusterStatusTracker.start();
  16. return this.activeMasterManager.blockUntilBecomingActiveMaster(startupStatus);
  17. }

2.1.2 ActiveMasterManager上的事件处理这里不管是create还是都是delete节点都是一样的处

2.1.2.1 nodeCreated()与nodeDeleted事件处理

  1. @Override
  2. public void nodeCreated(String path) {
  3. handle(path);
  4. }
  5. @Override
  6. public void nodeDeleted(String path) {
  7. if(path.equals(watcher.clusterStateZNode) && !master.isStopped()) {
  8. clusterShutDown.set(true);
  9. }
  10. handle(path);
  11. }
  12. void handle(final String path) {
  13. if (path.equals(watcher.getMasterAddressZNode()) && !master.isStopped()) {
  14. handleMasterNodeChange();
  15. }
  16. }

2.1.2.2。终于的节点创建和删除处理函数,

2.1.2.2.1这里无论是创建还是删除节点都是同一处理函数,

2.1.2.2.2假设/hbase/master节点处在说明已经有active master了,

2.1.2.2.3另外这个 clusterHasActiveMaster.notifyAll();须要关注下,在后面的堵塞成为master会用到

  1. private void handleMasterNodeChange() {
  2. // Watch the node and check if it exists.
  3. try {
  4. synchronized(clusterHasActiveMaster) {
  5. if (ZKUtil.watchAndCheckExists(watcher, watcher.getMasterAddressZNode())) {
  6. // A master node exists, there is an active master
  7. LOG.debug("A master is now available");
  8. clusterHasActiveMaster.set(true);
  9. } else {
  10. // Node is no longer there, cluster does not have an active master
  11. LOG.debug("No master available. Notifying waiting threads");
  12. clusterHasActiveMaster.set(false);
  13. // Notify any thread waiting to become the active master
  14. clusterHasActiveMaster.notifyAll();
  15. }
  16. }
  17. } catch (KeeperException ke) {
  18. master.abort("Received an unexpected KeeperException, aborting", ke);
  19. }
  20. }

2.2堵塞成为master

  1. boolean blockUntilBecomingActiveMaster(MonitoredTask startupStatus) {
  2. while (true) {
  3. startupStatus.setStatus("Trying to register in ZK as active master");
  4. // Try to become the active master, watch if there is another master.
  5. // Write out our ServerName as versioned bytes.
  6. try {
  7. //backupZNode -->/hbase/backup-masters/sn(hostname,port,startcode)
  8. String backupZNode =
  9. ZKUtil.joinZNode(this.watcher.backupMasterAddressesZNode, this.sn.toString());
  10. // watcher.getMasterAddressZNode()-->/hbase/master
  11. if (MasterAddressTracker.setMasterAddress(this.watcher,
  12. this.watcher.getMasterAddressZNode(), this.sn)) {
  13. // If we were a backup master before, delete our ZNode from the backup
  14. // master directory since we are the active now)
  15. if (ZKUtil.checkExists(this.watcher, backupZNode) != -1) {
  16. LOG.info("Deleting ZNode for " + backupZNode + " from backup master directory");
  17. ZKUtil.deleteNodeFailSilent(this.watcher, backupZNode);
  18. }
  19. // Save the znode in a file, this will allow to check if we crash in the launch scripts
  20. ZNodeClearer.writeMyEphemeralNodeOnDisk(this.sn.toString());
  21. // We are the master, return
  22. startupStatus.setStatus("Successfully registered as active master.");
  23. this.clusterHasActiveMaster.set(true);
  24. LOG.info("Registered Active Master=" + this.sn);
  25. return true;
  26. }
  27. // There is another active master running elsewhere or this is a restart
  28. // and the master ephemeral node has not expired yet.
  29. this.clusterHasActiveMaster.set(true);
  30. /*
  31. * Add a ZNode for ourselves in the backup master directory since we are
  32. * not the active master.
  33. *
  34. * If we become the active master later, ActiveMasterManager will delete
  35. * this node explicitly. If we crash before then, ZooKeeper will delete
  36. * this node for us since it is ephemeral.
  37. */
  38. LOG.info("Adding ZNode for " + backupZNode + " in backup master directory");
  39. MasterAddressTracker.setMasterAddress(this.watcher, backupZNode, this.sn);
  40. String msg;
  41. byte[] bytes =
  42. ZKUtil.getDataAndWatch(this.watcher, this.watcher.getMasterAddressZNode());
  43. if (bytes == null) {
  44. msg = ("A master was detected, but went down before its address " +
  45. "could be read. Attempting to become the next active master");
  46. } else {
  47. ServerName currentMaster;
  48. try {
  49. currentMaster = ServerName.parseFrom(bytes);
  50. } catch (DeserializationException e) {
  51. LOG.warn("Failed parse", e);
  52. // Hopefully next time around we won't fail the parse. Dangerous.
  53. continue;
  54. }
  55. if (ServerName.isSameHostnameAndPort(currentMaster, this.sn)) {
  56. msg = ("Current master has this master's address, " +
  57. currentMaster + "; master was restarted? Deleting node.");
  58. // Hurry along the expiration of the znode.
  59. ZKUtil.deleteNode(this.watcher, this.watcher.getMasterAddressZNode());
  60. // We may have failed to delete the znode at the previous step, but
  61. // we delete the file anyway: a second attempt to delete the znode is likely to fail again.
  62. ZNodeClearer.deleteMyEphemeralNodeOnDisk();
  63. } else {
  64. msg = "Another master is the active master, " + currentMaster +
  65. "; waiting to become the next active master";
  66. }
  67. }
  68. LOG.info(msg);
  69. startupStatus.setStatus(msg);
  70. } catch (KeeperException ke) {
  71. master.abort("Received an unexpected KeeperException, aborting", ke);
  72. return false;
  73. }
  74. synchronized (this.clusterHasActiveMaster) {
  75. while (this.clusterHasActiveMaster.get() && !this.master.isStopped()) {
  76. try {
  77. this.clusterHasActiveMaster.wait();
  78. } catch (InterruptedException e) {
  79. // We expect to be interrupted when a master dies,
  80. // will fall out if so
  81. LOG.debug("Interrupted waiting for master to die", e);
  82. }
  83. }
  84. if (clusterShutDown.get()) {
  85. this.master.stop(
  86. "Cluster went down before this master became active");
  87. }
  88. if (this.master.isStopped()) {
  89. return false;
  90. }
  91. // there is no active master so we can try to become active master again
  92. }
  93. }
  94. }

2.2.1 创建暂时节点/hbase/master,这里主要看

2.2.1.1

  1. MasterAddressTracker.setMasterAddress(this.watcher,
  2. this.watcher.getMasterAddressZNode(), this.sn)

2.2.1.2

  1. public static boolean setMasterAddress(final ZooKeeperWatcher zkw,
  2. final String znode, final ServerName master)
  3. throws KeeperException {
  4. return ZKUtil.createEphemeralNodeAndWatch(zkw, znode, toByteArray(master));
  5. }

2.2.1.3,这里不论创建失败成功都会加入zkw事件watcher,成功则成为master。失败则可能是已经有master存在了

  1. public static boolean createEphemeralNodeAndWatch(ZooKeeperWatcher zkw,
  2. String znode, byte [] data)
  3. throws KeeperException {
  4. try {
  5. zkw.getRecoverableZooKeeper().create(znode, data, createACL(zkw, znode),
  6. CreateMode.EPHEMERAL);
  7. } catch (KeeperException.NodeExistsException nee) {
  8. if(!watchAndCheckExists(zkw, znode)) {
  9. // It did exist but now it doesn't, try again
  10. return createEphemeralNodeAndWatch(zkw, znode, data);
  11. }
  12. return false;
  13. } catch (InterruptedException e) {
  14. LOG.info("Interrupted", e);
  15. Thread.currentThread().interrupt();
  16. }
  17. return true;
  18. }

2.2.2假设失败则堵塞则

2.2.2.1

假设失败,说明不是active master,增加backup节点

  1. LOG.info("Adding ZNode for " + backupZNode + " in backup master directory");
  2. MasterAddressTracker.setMasterAddress(this.watcher, backupZNode, this.sn);

2.2.2.2 再次从zk获取master 的server地址。与自己比較。假设是则说明已经重新启动过

  1. byte[] bytes =
  2. ZKUtil.getDataAndWatch(this.watcher, this.watcher.getMasterAddressZNode());
  3. if (bytes == null) {
  4. msg = ("A master was detected, but went down before its address " +
  5. "could be read. Attempting to become the next active master");
  6. } else {
  7. ServerName currentMaster;
  8. try {
  9. currentMaster = ServerName.parseFrom(bytes);
  10. } catch (DeserializationException e) {
  11. LOG.warn("Failed parse", e);
  12. // Hopefully next time around we won't fail the parse. Dangerous.
  13. continue;
  14. }
  15. if (ServerName.isSameHostnameAndPort(currentMaster, this.sn)) {
  16. msg = ("Current master has this master's address, " +
  17. currentMaster + "; master was restarted? Deleting node.");
  18. // Hurry along the expiration of the znode.
  19. ZKUtil.deleteNode(this.watcher, this.watcher.getMasterAddressZNode());
  20. // We may have failed to delete the znode at the previous step, but
  21. // we delete the file anyway: a second attempt to delete the znode is likely to fail again.
  22. ZNodeClearer.deleteMyEphemeralNodeOnDisk();
  23. } else {
  24. msg = "Another master is the active master, " + currentMaster +
  25. "; waiting to become the next active master";
  26. }

2.2 堵塞在clusterHasActiveMaster,这里等待知道notify。由ActiveMasterManager(2.1.2)来触发

  1. synchronized (this.clusterHasActiveMaster) {
  2. while (this.clusterHasActiveMaster.get() && !this.master.isStopped()) {
  3. try {
  4. this.clusterHasActiveMaster.wait();
  5. } catch (InterruptedException e) {
  6. // We expect to be interrupted when a master dies,
  7. // will fall out if so
  8. LOG.debug("Interrupted waiting for master to die", e);
  9. }
  10. }
  11. if (clusterShutDown.get()) {
  12. this.master.stop(
  13. "Cluster went down before this master became active");
  14. }
  15. if (this.master.isStopped()) {
  16. return false;
  17. }
  18. // there is no active master so we can try to become active master again
  19. }

Hbase0.96源码之HMaster(二)Hmaster主要循环becomeActiveMaster的更多相关文章

  1. Hbase0.96源码之HMaster(一)

    从main()函数開始 public static void main(String [] args) { VersionInfo.logVersion(); new HMasterCommandLi ...

  2. Hbase0.96源码之HMaster(三)Hmaster主要循环

    1.Master初始化 1.1 if (!this.stopped) { finishInitialization(startupStatus, false); loop(); } 1.2 finis ...

  3. 【原】FMDB源码阅读(二)

    [原]FMDB源码阅读(二) 本文转载请注明出处 -- polobymulberry-博客园 1. 前言 上一篇只是简单地过了一下FMDB一个简单例子的基本流程,并没有涉及到FMDB的所有方方面面,比 ...

  4. 【原】AFNetworking源码阅读(二)

    [原]AFNetworking源码阅读(二) 本文转载请注明出处 —— polobymulberry-博客园 1. 前言 上一篇中我们在iOS Example代码中提到了AFHTTPSessionMa ...

  5. 【原】SDWebImage源码阅读(二)

    [原]SDWebImage源码阅读(二) 本文转载请注明出处 —— polobymulberry-博客园 1. 解决上一篇遗留的坑 上一篇中对sd_setImageWithURL函数简单分析了一下,还 ...

  6. YYModel 源码解读(二)之NSObject+YYModel.h (1)

    本篇文章主要介绍 _YYModelPropertyMeta 前边的内容 首先先解释一下前边的辅助函数和枚举变量,在写一个功能的时候,这些辅助的东西可能不是一开始就能想出来的,应该是在后续的编码过程中 ...

  7. Cwinux源码解析(二)

    我在我的个人博客上发表了第二篇解析文章.欢迎各位读者批评指正. Cwinux源码解析(二)

  8. DataTable源码分析(二)

    DataTable源码分析(二) ===================== DataTable函数分析 ---------------- DataTable作为整个插件的入口,完成了整个表格的数据初 ...

  9. 一个普通的 Zepto 源码分析(二) - ajax 模块

    一个普通的 Zepto 源码分析(二) - ajax 模块 普通的路人,普通地瞧.分析时使用的是目前最新 1.2.0 版本. Zepto 可以由许多模块组成,默认包含的模块有 zepto 核心模块,以 ...

随机推荐

  1. Android中关于JNI 的学习(六)JNI中注冊方法的实现

    在前面的样例中,我们会发现,当在Java类中定义一个方法的时候,例如以下: public class ParamTransferTest { public static int testval = 1 ...

  2. 图片本地预览 flash html5

    dataURI 一种能够在页面嵌入外部资源的URI方案.能够降低图片或者样式表的http请求数量,提高效率. ie8把dataURI 的属性值限制在32k以内. 图片本地预览: 由于安全原因,通过fi ...

  3. IE常见的CSS的BUG(二)

    之前介绍过IE浏览器的几种BUG解决的方法,今天我们继续研究IE的BUG.尽管IE6即将被淘汰,但是了解这些也对将来解决问题也是有一定帮助的.好了,闲话不多说,咱们继续看IE的BUG. 1.IE6下P ...

  4. hdu5179(数位dp)

    传送门:beautiful number 题意:令 A=∑ni=1ai?10n?i(1≤ai≤9)(n为A的位数).若A为“漂亮的数”当且仅当对于任意1≤i<n满足a[i]≥a[i+1]且对于任 ...

  5. iOS 搜索框控件 最简单的dome

    刚学习搜索框控件,写了个最简单的dome #import <UIKit/UIKit.h> .h @interface ViewController : UIViewController&l ...

  6. 在Windows通过使用MinGW静态编译Assimp

    使用MinGW静态编译Assimp 到了5月份了.没有写一篇日志,于是自己从知识库里面拿出一篇文章充数吧.这次将要解说怎样在Windows下使用MinGW静态编译Assimp. Assimp是眼下比較 ...

  7. net 面向接口框架

    Asp.net 面向接口框架之应用程序上下文作用域组件 在团队中推广面向接口开发两年左右,成果总体来说我还是挺满意的,使用面向接口开发的模块使用Unity容器配置的功能非常稳定,便于共享迁移(另一个项 ...

  8. MySql 安装及0基础使用具体解释

    1. sudo apt-get install mysql-server, input administrator password , '123' 2. enter mysql promot in ...

  9. JavaScript 使用Document记录cookie

    cookie对于我们使用者来说,有时帮助还是挺大的,比方对于一些不是特别重要的站点,比方公司的測试平台,每次登陆都要手动输入username和password 非常繁琐.所以为了更少的引入其他框架,就 ...

  10. Hdu 3410 【单调队列】.cpp

    题意: 给出一个数组,问你对于第i个数,从最后一个比它大的数到它之间比它小的数中最大的那个数的下标,以及它右边到第一个比它大的数中比它小的数中最大的那一个数的下标<下标从1开始>. eg: ...