/** * 启动伪集群模式 * @param config * @throws IOException * @throws InterruptedException * @throws ConfigException */ public void startFakeCluster(QuorumPeerConfig config) throws IOException{ ServerCnxnFactory cnxnFactory = new NIOServerCnxnFactory(); cnxnFactory.configure(config.getClientPortAddress(), config.getMaxClientCnxns()); QuorumPeer quorumPeer = new QuorumPeer(config.getServers(), config.getDataDir(), config.getDataLogDir(), config.getElectionAlg(), config.getServerId(), config.getTickTime(), config.getInitLimit(), config.getSyncLimit(), config.getQuorumListenOnAllIPs(), cnxnFactory, config.getQuorumVerifier()); quorumPeer.setClientAddress(config.getClientPortAddress()); quorumPeer.setTxnFactory(new FileTxnSnapLog(config.getDataLogDir(), config.getDataDir())); quorumPeer.setElectionType(config.getElectionAlg()); quorumPeer.setMyid(config.getServerId()); quorumPeer.setTickTime(config.getTickTime()); quorumPeer.setMinSessionTimeout(config.getMinSessionTimeout()); quorumPeer.setMaxSessionTimeout(config.getMaxSessionTimeout()); quorumPeer.setInitLimit(config.getInitLimit()); quorumPeer.setSyncLimit(config.getSyncLimit()); quorumPeer.setQuorumVerifier(config.getQuorumVerifier(), true); quorumPeer.setCnxnFactory(cnxnFactory); quorumPeer.setZKDatabase(new ZKDatabase(quorumPeer.getTxnFactory())); quorumPeer.setLearnerType(config.getPeerType()); quorumPeer.setSyncEnabled(config.getSyncEnabled()); quorumPeer.setQuorumListenOnAllIPs(config.getQuorumListenOnAllIPs()); quorumPeer.start(); LOGGER.info("ZkServerCluster Started! ClientPortAddress={}", config.getClientPortAddress()); }
@Before public void setup() throws InterruptedException { System.setProperty(authProvider, superDigest); QuorumPeerConfig.setReconfigEnabled(true); // Get a three server quorum. qu = new QuorumUtil(1); qu.disableJMXTest = true; try { qu.startAll(); } catch (IOException e) { Assert.fail("Fail to start quorum servers."); } resetZKAdmin(); }
private static void startZkLocal() throws Exception { final File zkTmpDir = File.createTempFile("zookeeper", "test"); if (zkTmpDir.delete() && zkTmpDir.mkdir()) { Properties zkProperties = new Properties(); zkProperties.setProperty("dataDir", zkTmpDir.getAbsolutePath()); zkProperties.setProperty("clientPort", String.valueOf(ZK_PORT)); ServerConfig configuration = new ServerConfig(); QuorumPeerConfig quorumConfiguration = new QuorumPeerConfig(); quorumConfiguration.parseProperties(zkProperties); configuration.readFrom(quorumConfiguration); new Thread() { public void run() { try { new ZooKeeperServerMain().runFromConfig(configuration); } catch (IOException e) { System.out.println("Start of Local ZooKeeper Failed"); e.printStackTrace(System.err); } } }.start(); } else { System.out.println("Failed to delete or create data dir for Zookeeper"); } }
/** * Parse ZooKeeper configuration from HBase XML config and run a QuorumPeer. * @param args String[] of command line arguments. Not used. */ public static void main(String[] args) { Configuration conf = HBaseConfiguration.create(); try { Properties zkProperties = ZKConfig.makeZKProps(conf); writeMyID(zkProperties); QuorumPeerConfig zkConfig = new QuorumPeerConfig(); zkConfig.parseProperties(zkProperties); // login the zookeeper server principal (if using security) ZKUtil.loginServer(conf, HConstants.ZK_SERVER_KEYTAB_FILE, HConstants.ZK_SERVER_KERBEROS_PRINCIPAL, zkConfig.getClientPortAddress().getHostName()); runZKServer(zkConfig); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } }
public void testUpdateConfig() throws Exception { final File tempDir = new File("target"); IMocksControl control = EasyMock.createControl(); BundleContext bc = control.createMock(BundleContext.class); expect(bc.getDataFile("")).andReturn(tempDir); final MyZooKeeperServerMain mockServer = control.createMock(MyZooKeeperServerMain.class); control.replay(); ZookeeperStarter starter = new ZookeeperStarter(bc) { @Override protected void startFromConfig(QuorumPeerConfig config) { assertEquals(1234, config.getClientPortAddress().getPort()); assertTrue(config.getDataDir().contains(tempDir + File.separator + "zkdata")); assertEquals(2000, config.getTickTime()); assertEquals(10, config.getInitLimit()); assertEquals(5, config.getSyncLimit()); this.main = mockServer; } }; Dictionary<String, Object> props = new Hashtable<String, Object>(); props.put("clientPort", "1234"); starter.updated(props); assertNotNull(starter.main); control.verify(); }
/** * Parse ZooKeeper configuration from HBase XML config and run a QuorumPeer. * @param args String[] of command line arguments. Not used. */ public static void main(String[] args) { Configuration conf = HBaseConfiguration.create(); try { Properties zkProperties = ZKConfig.makeZKProps(conf); writeMyID(zkProperties); QuorumPeerConfig zkConfig = new QuorumPeerConfig(); zkConfig.parseProperties(zkProperties); // login the zookeeper server principal (if using security) ZKUtil.loginServer(conf, "hbase.zookeeper.server.keytab.file", "hbase.zookeeper.server.kerberos.principal", zkConfig.getClientPortAddress().getHostName()); runZKServer(zkConfig); } catch (Exception e) { e.printStackTrace(); System.exit(-1); } }
public TestingZookeeperServerEmbedded(int clientPort, File baseDir) throws Exception { Properties p = new Properties(); String host = "localhost"; String dataDir = "data"; File dir = new File(baseDir, dataDir); p.setProperty("syncEnabled", "false"); p.setProperty("dataDir", dir.getAbsolutePath()); p.setProperty("clientPort", clientPort + ""); p.setProperty("authProvider.1", "org.apache.zookeeper.server.auth.SASLAuthenticationProvider"); p.setProperty("kerberos.removeHostFromPrincipal", "true"); p.setProperty("kerberos.removeRealmFromPrincipal", "true"); config = new QuorumPeerConfig(); config.parseProperties(p); }
public TestingZookeeperServerEmbedded(int clientPort, File baseDir) throws Exception { this.clientPort = clientPort; Properties p = new Properties(); String host = "localhost"; String dataDir = "data"; File dir = new File(baseDir, dataDir); p.setProperty("syncEnabled", "false"); p.setProperty("dataDir", dir.getAbsolutePath()); p.setProperty("clientPort", clientPort + ""); p.setProperty("authProvider.1", "org.apache.zookeeper.server.auth.SASLAuthenticationProvider"); p.setProperty("kerberos.removeHostFromPrincipal", "true"); p.setProperty("kerberos.removeRealmFromPrincipal", "true"); config = new QuorumPeerConfig(); config.parseProperties(p); }
public void start() throws Exception { if ( !state.compareAndSet(State.LATENT, State.STARTED) ) { return; } new Thread(new Runnable() { public void run() { try { QuorumPeerConfig config = configBuilder.buildConfig(thisInstanceIndex); main.runFromConfig(config); } catch ( Exception e ) { logger.error(String.format("From testing server (random state: %s) for instance: %s", String.valueOf(configBuilder.isFromRandom()), getInstanceSpec()), e); } } }).start(); main.blockUntilStarted(); }
ZookeeperLaucher create(Properties zkProperties) throws ConfigException, IOException { QuorumPeerConfig zkConfig = new QuorumPeerConfig(); zkConfig.parseProperties(zkProperties); DatadirCleanupManager purgeMgr = new DatadirCleanupManager( zkConfig.getDataDir(), zkConfig.getDataLogDir(), zkConfig.getSnapRetainCount(), zkConfig.getPurgeInterval()); purgeMgr.start(); if (zkConfig.getServers().size() > 0) { return new QuorumPeerMainExt(zkConfig); } else { logger.warn( "Either no config or no quorum defined in config, running in standalone mode" ); // there is only server in the quorum -- run as standalone return new ZooKeeperServerMainExt(zkConfig) ; } }
ZookeeperLaucher create(Properties zkProperties) throws ConfigException, IOException { QuorumPeerConfig zkConfig = new QuorumPeerConfig(); zkConfig.parseProperties(zkProperties); DatadirCleanupManager purgeMgr = new DatadirCleanupManager(zkConfig.getDataDir(), zkConfig.getDataLogDir(), zkConfig.getSnapRetainCount(), zkConfig.getPurgeInterval()); purgeMgr.start(); if (zkConfig.getServers().size() > 0) { return new QuorumPeerMainExt(zkConfig); } else { System.out .println("Either no config or no quorum defined in config, running in standalone mode"); // there is only server in the quorum -- run as standalone return new ZooKeeperServerMainExt(zkConfig); } }
public SpliceZoo(QuorumPeerConfig config, int number) throws IOException { this.config = config; ServerCnxnFactory cnxnFactory = ServerCnxnFactory.createFactory(); cnxnFactory.configure(config.getClientPortAddress(),config.getMaxClientCnxns()); peer = new QuorumPeer(config.getServers(), null, null, config.getElectionAlg(), number, config.getTickTime(), config.getInitLimit(), config.getSyncLimit(), cnxnFactory); peer.setClientPortAddress(config.getClientPortAddress()); peer.setTxnFactory(new FileTxnSnapLog(new File(config.getDataLogDir()), new File(config.getDataDir()))); peer.setQuorumPeers(config.getServers()); peer.setElectionType(config.getElectionAlg()); peer.setMyid(config.getServerId()); peer.setTickTime(config.getTickTime()); peer.setMinSessionTimeout(config.getMinSessionTimeout()); peer.setMaxSessionTimeout(config.getMaxSessionTimeout()); peer.setInitLimit(config.getInitLimit()); peer.setSyncLimit(config.getSyncLimit()); peer.setQuorumVerifier(config.getQuorumVerifier()); peer.setCnxnFactory(cnxnFactory); peer.setZKDatabase(new ZKDatabase(peer.getTxnFactory())); peer.setLearnerType(config.getPeerType()); peer.setMyid(number); }
public SpliceZoo(QuorumPeerConfig config, int number) throws IOException { this.config = config; this.peer = new QuorumPeer(); ServerCnxnFactory cnxnFactory = ServerCnxnFactory.createFactory(); cnxnFactory.configure(config.getClientPortAddress(),config.getMaxClientCnxns()); peer.setClientPortAddress(config.getClientPortAddress()); peer.setTxnFactory(new FileTxnSnapLog(new File(config.getDataLogDir()), new File(config.getDataDir()))); peer.setQuorumPeers(config.getServers()); peer.setElectionType(config.getElectionAlg()); peer.setMyid(config.getServerId()); peer.setTickTime(config.getTickTime()); peer.setMinSessionTimeout(config.getMinSessionTimeout()); peer.setMaxSessionTimeout(config.getMaxSessionTimeout()); peer.setInitLimit(config.getInitLimit()); peer.setSyncLimit(config.getSyncLimit()); peer.setQuorumVerifier(config.getQuorumVerifier()); peer.setCnxnFactory(cnxnFactory); peer.setZKDatabase(new ZKDatabase(peer.getTxnFactory())); peer.setLearnerType(config.getPeerType()); peer.setMyid(number); }
public SpliceZoo(QuorumPeerConfig config, int number) throws IOException { this.config = config; ServerCnxnFactory cnxnFactory = ServerCnxnFactory.createFactory(); cnxnFactory.configure(config.getClientPortAddress(),config.getMaxClientCnxns()); peer.setClientPortAddress(config.getClientPortAddress()); peer.setTxnFactory(new FileTxnSnapLog(new File(config.getDataLogDir()), new File(config.getDataDir()))); peer.setQuorumPeers(config.getServers()); peer.setElectionType(config.getElectionAlg()); peer.setMyid(config.getServerId()); peer.setTickTime(config.getTickTime()); peer.setMinSessionTimeout(config.getMinSessionTimeout()); peer.setMaxSessionTimeout(config.getMaxSessionTimeout()); peer.setInitLimit(config.getInitLimit()); peer.setSyncLimit(config.getSyncLimit()); peer.setQuorumVerifier(config.getQuorumVerifier()); peer.setCnxnFactory(cnxnFactory); peer.setZKDatabase(new ZKDatabase(peer.getTxnFactory())); peer.setLearnerType(config.getPeerType()); peer.setMyid(number); }
/** * Parse a ZooKeeper configuration file * @param path the patch of the configuration file * @return ServerConfig configured wrt arguments * @throws ConfigException error processing configuration */ public void parse(String path) throws ConfigException { QuorumPeerConfig config = new QuorumPeerConfig(); config.parse(path); // let qpconfig parse the file and then pull the stuff we are // interested in readFrom(config); }
/** * Read attributes from a QuorumPeerConfig. * @param config */ public void readFrom(QuorumPeerConfig config) { clientPortAddress = config.getClientPortAddress(); dataDir = config.getDataDir(); dataLogDir = config.getDataLogDir(); tickTime = config.getTickTime(); maxClientCnxns = config.getMaxClientCnxns(); minSessionTimeout = config.getMinSessionTimeout(); maxSessionTimeout = config.getMaxSessionTimeout(); }
/** * Read attributes from a QuorumPeerConfig. * @param config */ public void readFrom(QuorumPeerConfig config) { clientPortAddress = config.getClientPortAddress(); secureClientPortAddress = config.getSecureClientPortAddress(); dataDir = config.getDataDir(); dataLogDir = config.getDataLogDir(); tickTime = config.getTickTime(); maxClientCnxns = config.getMaxClientCnxns(); minSessionTimeout = config.getMinSessionTimeout(); maxSessionTimeout = config.getMaxSessionTimeout(); }
@Test(timeout = 10000) public void testReconfigDisabled() throws InterruptedException { QuorumPeerConfig.setReconfigEnabled(false); try { reconfigPort(); Assert.fail("Reconfig should be disabled."); } catch (KeeperException e) { Assert.assertTrue(e.code() == KeeperException.Code.RECONFIGDISABLED); } }
@Before public void setup() throws InterruptedException { QuorumPeerConfig.setReconfigEnabled(true); // Get a three server quorum. qu = new QuorumUtil(1); qu.disableJMXTest = true; try { qu.startAll(); } catch (IOException e) { Assert.fail("Fail to start quorum servers."); } instantiateZKAdmin(); }
private static void runZKServer(QuorumPeerConfig zkConfig) throws UnknownHostException, IOException { if (zkConfig.isDistributed()) { QuorumPeerMain qp = new QuorumPeerMain(); qp.runFromConfig(zkConfig); } else { ZooKeeperServerMain zk = new ZooKeeperServerMain(); ServerConfig serverConfig = new ServerConfig(); serverConfig.readFrom(zkConfig); zk.runFromConfig(serverConfig); } }
public void start() throws Exception { try { // zkDir = genZookeeperDataDir(); zkConfig = genZookeeperConfig(zkDir); port = Integer.valueOf(zkConfig.getProperty("clientPort")); QuorumPeerConfig qpConfig = new QuorumPeerConfig(); qpConfig.parseProperties(zkConfig); final ServerConfig sConfig = new ServerConfig(); sConfig.readFrom(qpConfig); thread = new Thread() { @Override public void run() { try { LOGGER.info("Starting ZK server"); runFromConfig(sConfig); } catch (Throwable t) { LOGGER.error("Failure in embedded ZooKeeper", t); } } }; thread.start(); Thread.sleep(500); } catch (Throwable t) { throw new Exception("Cannot start embedded zookeeper", t); } }
public void start() throws IOException, ConfigException, InterruptedException { log.info("Starting Zookeeper on port {}", port); Properties properties = new Properties(); properties.setProperty("dataDir", getDataDir().getAbsolutePath()); properties.setProperty("clientPort", Integer.toString(getPort())); QuorumPeerConfig quorumConfig = new QuorumPeerConfig(); quorumConfig.parseProperties(properties); cleanupManager = new DatadirCleanupManager(quorumConfig.getDataDir(), quorumConfig.getDataLogDir(), quorumConfig.getSnapRetainCount(), quorumConfig.getPurgeInterval()); cleanupManager.start(); ServerConfig serverConfig = new ServerConfig(); serverConfig.readFrom(quorumConfig); zkServer = new ZooKeeperServer(); zkServer.setTickTime(serverConfig.getTickTime()); zkServer.setMinSessionTimeout(serverConfig.getMinSessionTimeout()); zkServer.setMaxSessionTimeout(serverConfig.getMaxSessionTimeout()); transactionLog = new FileTxnSnapLog(new File(serverConfig.getDataLogDir().toString()), new File(serverConfig.getDataDir().toString())); zkServer.setTxnLogFactory(transactionLog); connectionFactory = ServerCnxnFactory.createFactory(); connectionFactory.configure(serverConfig.getClientPortAddress(), serverConfig.getMaxClientCnxns()); connectionFactory.startup(zkServer); }