ManagedQuorumPeer.java 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  1. /**
  2. * Copyright 2008, Yahoo! Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package com.yahoo.zookeeper.server.quorum;
  17. import static com.yahoo.zookeeper.server.ServerConfig.getClientPort;
  18. import java.io.File;
  19. import java.io.IOException;
  20. import java.util.ArrayList;
  21. import java.util.concurrent.ConcurrentHashMap;
  22. import org.apache.log4j.Logger;
  23. import com.yahoo.zookeeper.jmx.MBeanRegistry;
  24. import com.yahoo.zookeeper.jmx.ZKMBeanInfo;
  25. import com.yahoo.zookeeper.jmx.server.ConnectionBean;
  26. import com.yahoo.zookeeper.jmx.server.DataTreeBean;
  27. import com.yahoo.zookeeper.jmx.server.quorum.FollowerBean;
  28. import com.yahoo.zookeeper.jmx.server.quorum.LeaderBean;
  29. import com.yahoo.zookeeper.jmx.server.quorum.LeaderElectionBean;
  30. import com.yahoo.zookeeper.jmx.server.quorum.LocalPeerBean;
  31. import com.yahoo.zookeeper.jmx.server.quorum.QuorumBean;
  32. import com.yahoo.zookeeper.jmx.server.quorum.RemotePeerBean;
  33. import com.yahoo.zookeeper.jmx.server.quorum.ServerBean;
  34. import com.yahoo.zookeeper.server.ManagedZooKeeperServer;
  35. import com.yahoo.zookeeper.server.NIOServerCnxn;
  36. import com.yahoo.zookeeper.server.ObservableNIOServerCnxn;
  37. import com.yahoo.zookeeper.server.ServerCnxn;
  38. import com.yahoo.zookeeper.server.ZooKeeperServer;
  39. import com.yahoo.zookeeper.server.ZooTrace;
  40. import com.yahoo.zookeeper.server.util.ConnectionObserver;
  41. import com.yahoo.zookeeper.server.util.ObserverManager;
  42. import com.yahoo.zookeeper.server.util.QuorumPeerObserver;
  43. import com.yahoo.zookeeper.server.util.ServerObserver;
  44. import com.yahoo.zookeeper.server.util.ZooKeeperObserverManager;
  45. /**
  46. * This class launches a replicated zookeeper server with JMX support
  47. * enabled. The users can connect to the server JVM and manage
  48. * the server state (such as currently open client connections) and view runtime
  49. * statistics using one of existing GUI JMX consoles (jconsole, for example).
  50. * Please refer to the JDK vendor documentation for further information on how
  51. * to enable JMX support in the JVM.
  52. * <p>
  53. * The server provides following MBeans:
  54. * <ul>
  55. * <li>Quorum MBean -- provides quorum runtime statistics, see {@link QuorumMXBean}.
  56. * <li>Peer MBean -- provides information about quorum peers (local and remote),
  57. * see {@link LocalPeerMXBean} and {@link RemotePeerMXBean}.
  58. * <li>Leader election MBean -- provides runtime info on leader election protocol,
  59. * see {@link LeaderElectionMXBean}
  60. * <li>Zookeeper server MBean -- provides various configuraton data and runtime
  61. * statistics, see {@link ZooKeeperServerMXBean}
  62. * <li>Data tree MBean -- provides runtime data tree statistics, see
  63. * {@link DataTreeMXBean}
  64. * <li>Client connection MBean -- provides runtime statistics as well as
  65. * connection management operations, see {@link ConnectionMXBean}
  66. * </ul>
  67. * The client connection is a dynamic resource and therefore the connection
  68. * MBeans are dynamically created and destroyed as the clients connect to and
  69. * disconnect from the server.
  70. */
  71. public class ManagedQuorumPeer extends ObservableQuorumPeer {
  72. private static final Logger LOG = Logger.getLogger(ManagedQuorumPeer.class);
  73. private QuorumBean quorumBean;
  74. private LocalPeerBean localPeerBean;
  75. private ServerBean svrBean;
  76. private LeaderElectionBean leBean;
  77. // tracking state of the quorum peer
  78. private class ManagedQuorumPeerObserver implements QuorumPeerObserver {
  79. public void onFollowerShutdown(QuorumPeer qp, Follower follower) {
  80. ZooTrace.logTraceMessage(LOG, ZooTrace.JMX_TRACE_MASK,
  81. "Follower shutdown "+follower);
  82. MBeanRegistry.getInstance().unregister(svrBean);
  83. svrBean=null;
  84. }
  85. public void onFollowerStarted(QuorumPeer qp, Follower newFollower) {
  86. ZooTrace.logTraceMessage(LOG, ZooTrace.JMX_TRACE_MASK,
  87. "Follower started "+newFollower);
  88. MBeanRegistry.getInstance().unregister(leBean);
  89. leBean=null;
  90. svrBean=new FollowerBean();
  91. MBeanRegistry.getInstance().register(svrBean, localPeerBean);
  92. }
  93. public void onLeaderElectionStarted(QuorumPeer qp) {
  94. ZooTrace.logTraceMessage(LOG, ZooTrace.JMX_TRACE_MASK,
  95. "Running leader election protocol...");
  96. leBean=new LeaderElectionBean();
  97. MBeanRegistry.getInstance().register(leBean, localPeerBean);
  98. }
  99. public void onLeaderShutdown(QuorumPeer qp, Leader leader) {
  100. ZooTrace.logTraceMessage(LOG, ZooTrace.JMX_TRACE_MASK,
  101. "Leader shutdown "+leader);
  102. MBeanRegistry.getInstance().unregister(svrBean);
  103. svrBean=null;
  104. }
  105. public void onLeaderStarted(QuorumPeer qp, Leader newLeader) {
  106. ZooTrace.logTraceMessage(LOG, ZooTrace.JMX_TRACE_MASK,
  107. "Leader started "+newLeader);
  108. MBeanRegistry.getInstance().unregister(leBean);
  109. leBean=null;
  110. svrBean=new LeaderBean();
  111. MBeanRegistry.getInstance().register(svrBean, localPeerBean);
  112. }
  113. public void onShutdown(QuorumPeer qp) {
  114. ZooTrace.logTraceMessage(LOG, ZooTrace.JMX_TRACE_MASK,
  115. "Shutting down quorum peer");
  116. MBeanRegistry.getInstance().unregisterAll();
  117. }
  118. public void onStartup(QuorumPeer qp) {
  119. ZooTrace.logTraceMessage(LOG, ZooTrace.JMX_TRACE_MASK,
  120. "Starting quorum peer");
  121. quorumBean=new QuorumBean(qp);
  122. MBeanRegistry.getInstance().register(quorumBean, null);
  123. for(QuorumServer s: qp.quorumPeers){
  124. ZKMBeanInfo p;
  125. if(qp.getId()==s.id)
  126. p=localPeerBean=new LocalPeerBean(qp);
  127. else
  128. p=new RemotePeerBean(s);
  129. MBeanRegistry.getInstance().register(p, quorumBean);
  130. }
  131. }
  132. }
  133. // on client connect/disconnect this observer will register/unregister
  134. // a connection MBean with the MBean server
  135. private class ManagedConnectionObserver implements ConnectionObserver {
  136. private ConcurrentHashMap<ServerCnxn,ConnectionBean> map=
  137. new ConcurrentHashMap<ServerCnxn,ConnectionBean>();
  138. public void onClose(ServerCnxn sc) {
  139. ConnectionBean b=map.remove(sc);
  140. if(b!=null){
  141. ZooTrace.logTraceMessage(LOG, ZooTrace.JMX_TRACE_MASK,
  142. "Un-registering a ConnectionBean: "+b);
  143. MBeanRegistry.getInstance().unregister(b);
  144. }
  145. }
  146. public void onNew(ServerCnxn sc) {
  147. ConnectionBean b=new ConnectionBean(sc,getActiveServer());
  148. map.put(sc, b);
  149. ZooTrace.logTraceMessage(LOG, ZooTrace.JMX_TRACE_MASK,
  150. "Registering new ConnectionBean: "+b);
  151. MBeanRegistry.getInstance().register(b, localPeerBean);
  152. }
  153. }
  154. // this observer tracks the state of the zookeeper server
  155. private class ManagedServerObserver implements ServerObserver {
  156. private DataTreeBean dataTreeBean;
  157. public void onShutdown(ZooKeeperServer server) {
  158. ZooTrace.logTraceMessage(LOG, ZooTrace.JMX_TRACE_MASK,
  159. "Shutdown zookeeper server: "+server);
  160. MBeanRegistry.getInstance().unregister(dataTreeBean);
  161. dataTreeBean=null;
  162. }
  163. public void onStartup(ZooKeeperServer server) {
  164. ZooTrace.logTraceMessage(LOG, ZooTrace.JMX_TRACE_MASK,
  165. "Started new zookeeper server: "+server);
  166. try {
  167. dataTreeBean = new DataTreeBean(server.dataTree);
  168. MBeanRegistry.getInstance().register(dataTreeBean, svrBean);
  169. } catch (Exception e) {
  170. LOG.warn("Failed to register Standalone ZooKeeperServerMBean "
  171. + e.getMessage());
  172. }
  173. }
  174. }
  175. private void setupObservers(){
  176. ObserverManager.getInstance().add(new ManagedQuorumPeerObserver());
  177. ObserverManager.getInstance().add(new ManagedServerObserver());
  178. ObserverManager.getInstance().add(new ManagedConnectionObserver());
  179. }
  180. public ManagedQuorumPeer(ArrayList<QuorumServer> quorumPeers, File dataDir,
  181. File dataLogDir,int electionAlg, int electionPort,long myid, int tickTime,
  182. int initLimit, int syncLimit,NIOServerCnxn.Factory cnxnFactory)
  183. throws IOException {
  184. super(quorumPeers, dataDir, dataLogDir,electionAlg, electionPort,myid,
  185. tickTime, initLimit, syncLimit,cnxnFactory);
  186. setupObservers();
  187. }
  188. public ManagedQuorumPeer(NIOServerCnxn.Factory cnxnFactory) throws IOException {
  189. super(cnxnFactory);
  190. setupObservers();
  191. }
  192. /**
  193. * To start the replicated server specify the configuration file name on the
  194. * command line.
  195. * @param args command line
  196. */
  197. public static void main(String[] args) {
  198. if (args.length == 2) {
  199. ManagedZooKeeperServer.main(args);
  200. return;
  201. }
  202. QuorumPeerConfig.parse(args);
  203. if (!QuorumPeerConfig.isStandalone()) {
  204. ZooKeeperObserverManager.setAsConcrete();
  205. runPeer(new QuorumPeer.Factory() {
  206. public QuorumPeer create(NIOServerCnxn.Factory cnxnFactory)
  207. throws IOException {
  208. return new ManagedQuorumPeer(cnxnFactory);
  209. }
  210. public NIOServerCnxn.Factory createConnectionFactory() throws IOException {
  211. return new ObservableNIOServerCnxn.Factory(getClientPort());
  212. }
  213. });
  214. }else{
  215. // there is only server in the quorum -- run as standalone
  216. ManagedZooKeeperServer.main(args);
  217. }
  218. }
  219. }