private ZooKeeperStateServer(final Properties zkProperties) throws IOException, ConfigException { quorumPeerConfig = new QuorumPeerConfig(); quorumPeerConfig.parseProperties(zkProperties); }
private void startDistributed() throws IOException { logger.info("Starting Embedded ZooKeeper Peer"); try { transactionLog = new FileTxnSnapLog(new File(quorumPeerConfig.getDataLogDir()), new File(quorumPeerConfig.getDataDir())); connectionFactory = ServerCnxnFactory.createFactory(); connectionFactory.configure(quorumPeerConfig.getClientPortAddress(), quorumPeerConfig.getMaxClientCnxns()); quorumPeer = new QuorumPeer(); quorumPeer.setClientPortAddress(quorumPeerConfig.getClientPortAddress()); quorumPeer.setTxnFactory(new FileTxnSnapLog(new File(quorumPeerConfig.getDataLogDir()), new File(quorumPeerConfig.getDataDir()))); quorumPeer.setQuorumPeers(quorumPeerConfig.getServers()); quorumPeer.setElectionType(quorumPeerConfig.getElectionAlg()); quorumPeer.setMyid(quorumPeerConfig.getServerId()); quorumPeer.setTickTime(quorumPeerConfig.getTickTime()); quorumPeer.setMinSessionTimeout(quorumPeerConfig.getMinSessionTimeout()); quorumPeer.setMaxSessionTimeout(quorumPeerConfig.getMaxSessionTimeout()); quorumPeer.setInitLimit(quorumPeerConfig.getInitLimit()); quorumPeer.setSyncLimit(quorumPeerConfig.getSyncLimit()); quorumPeer.setQuorumVerifier(quorumPeerConfig.getQuorumVerifier()); quorumPeer.setCnxnFactory(connectionFactory); quorumPeer.setZKDatabase(new ZKDatabase(quorumPeer.getTxnFactory())); quorumPeer.setLearnerType(quorumPeerConfig.getPeerType()); quorumPeer.setSyncEnabled(quorumPeerConfig.getSyncEnabled()); quorumPeer.setQuorumListenOnAllIPs(quorumPeerConfig.getQuorumListenOnAllIPs()); quorumPeer.start(); } catch (final IOException ioe) { throw new IOException("Failed to start embedded ZooKeeper Peer", ioe); } catch (final Exception e) { throw new RuntimeException("Failed to start embedded ZooKeeper Peer", e); } }
/** * 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(); metricsProviderClassName = config.getMetricsProviderClassName(); metricsProviderConfiguration = config.getMetricsProviderConfiguration(); }
/** * 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(); }
dataDir.toFile().deleteOnExit(); QuorumPeerConfig quorumConfig = new QuorumPeerConfig(); try { quorumConfig.parseProperties(ConfigUtils.keyValueToProperties( "dataDir", dataDir.toAbsolutePath(), "clientPort", port new DatadirCleanupManager(quorumConfig.getDataDir(), quorumConfig.getDataLogDir(), quorumConfig.getSnapRetainCount(), quorumConfig.getPurgeInterval()); purgeManager.start();
EmbeddedZooKeeper(QuorumPeerConfig zkCfg) throws IOException { cnxnFactory = createCnxnFactory(zkCfg); setTxnFactory(new FileTxnSnapLog(zkCfg.getDataLogDir(), zkCfg.getDataDir())); enableLocalSessions(zkCfg.areLocalSessionsEnabled()); enableLocalSessionsUpgrading(zkCfg.isLocalSessionsUpgradingEnabled()); setElectionType(zkCfg.getElectionAlg()); setMyid(zkCfg.getServerId()); setTickTime(zkCfg.getTickTime()); setMinSessionTimeout(zkCfg.getMinSessionTimeout()); setMaxSessionTimeout(zkCfg.getMaxSessionTimeout()); setInitLimit(zkCfg.getInitLimit()); setSyncLimit(zkCfg.getSyncLimit()); setConfigFileName(zkCfg.getConfigFilename()); setZKDatabase(new ZKDatabase(getTxnFactory())); setQuorumVerifier(zkCfg.getQuorumVerifier(), false); if (zkCfg.getLastSeenQuorumVerifier() != null) { setLastSeenQuorumVerifier(zkCfg.getLastSeenQuorumVerifier(), false); } initConfigInZKDatabase(); setCnxnFactory(cnxnFactory); setLearnerType(zkCfg.getPeerType()); setSyncEnabled(zkCfg.getSyncEnabled()); setQuorumListenOnAllIPs(zkCfg.getQuorumListenOnAllIPs()); configureSasl(); purgeManager = new DatadirCleanupManager(zkCfg.getDataDir(), zkCfg.getDataLogDir(), zkCfg.getSnapRetainCount(), zkCfg.getPurgeInterval()); }
/** * 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) { LOG.error("Failed to start ZKServer", e); System.exit(-1); } }
try { metricsProvider = MetricsProviderBootstrap .startMetricsProvider(config.getMetricsProviderClassName(), config.getMetricsProviderConfiguration()); } catch (MetricsProviderLifeCycleException error) { throw new IOException("Cannot boot MetricsProvider " + config.getMetricsProviderClassName(), error); if (config.getClientPortAddress() != null) { cnxnFactory = ServerCnxnFactory.createFactory(); cnxnFactory.configure(config.getClientPortAddress(), config.getMaxClientCnxns(), false); if (config.getSecureClientPortAddress() != null) { secureCnxnFactory = ServerCnxnFactory.createFactory(); secureCnxnFactory.configure(config.getSecureClientPortAddress(), config.getMaxClientCnxns(), true); quorumPeer.setRootMetricsContext(metricsProvider.getRootContext()); quorumPeer.setTxnFactory(new FileTxnSnapLog( config.getDataLogDir(), config.getDataDir())); quorumPeer.enableLocalSessions(config.areLocalSessionsEnabled()); quorumPeer.enableLocalSessionsUpgrading( config.isLocalSessionsUpgradingEnabled()); quorumPeer.setElectionType(config.getElectionAlg());
/** * 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); }
protected void initializeAndRun(String[] args) throws ConfigException, IOException, AdminServerException { QuorumPeerConfig config = new QuorumPeerConfig(); if (args.length == 1) { config.parse(args[0]); } // Start and schedule the the purge task DatadirCleanupManager purgeMgr = new DatadirCleanupManager(config .getDataDir(), config.getDataLogDir(), config .getSnapRetainCount(), config.getPurgeInterval()); purgeMgr.start(); if (args.length == 1 && config.isDistributed()) { runFromConfig(config); } else { LOG.warn("Either no config or no quorum defined in config, running " + " in standalone mode"); // there is only server in the quorum -- run as standalone ZooKeeperServerMain.main(args); } }
QuorumPeerConfig conf = new QuorumPeerConfig(); conf.parseProperties(zkProps); if (conf.isDistributed()) { conf.getServers().size());
public static ZookeeperServer getZookeeperServer(Properties props) throws Exception { QuorumPeerConfig config = new QuorumPeerConfig(); config.parseProperties(props); if (config.getServers().size() > 0) { return new MyQuorumPeerMain(config); } else { return new MyZooKeeperServerMain(config); } }
@Override public void runFromConfig(QuorumPeerConfig config) throws IOException, AdminServerException { quorumPeer = new CustomQuorumPeer(config.getQuorumVerifier().getAllMembers(), config.getDataDir(), config.getDataLogDir(), config.getClientPortAddress().getPort(), config.getElectionAlg(), config.getServerId(), config.getTickTime(), config.getInitLimit(), config.getSyncLimit()); quorumPeer.setConfigFileName(config.getConfigFilename()); quorumPeer.start(); try { quorumPeer.join(); } catch (InterruptedException e) { LOG.warn("Quorum Peer interrupted", e); } } }
/** * Parse a ZooKeeper configuration file * @param path the patch of the configuration file * @throws ConfigException error processing configuration */ public void parse(String path) throws ConfigException { File configFile = new File(path); LOG.info("Reading configuration from: " + configFile); try { if (!configFile.exists()) { throw new IllegalArgumentException(configFile.toString() + " file is missing"); } Properties cfg = new Properties(); FileInputStream in = new FileInputStream(configFile); try { cfg.load(in); } finally { in.close(); } parseProperties(cfg); } catch (IOException e) { throw new ConfigException("Error processing " + path, e); } catch (IllegalArgumentException e) { throw new ConfigException("Error processing " + path, e); } }
public ZookeeperQueryLock(String zookeeperConfig, long clientCleanupInterval, String queryId) throws ConfigException { this.queryId = queryId; this.clientCleanupInterval = clientCleanupInterval; URI zookeeperConfigFile = null; try { zookeeperConfigFile = new Path(zookeeperConfig).toUri(); if (new File(zookeeperConfigFile).exists()) { QuorumPeerConfig zooConfig = new QuorumPeerConfig(); zooConfig.parse(zookeeperConfigFile.getPath()); StringBuilder builder = new StringBuilder(); for (QuorumServer server : zooConfig.getServers().values()) { if (builder.length() > 0) { builder.append(','); } builder.append(server.addr.getHostName()).append(':').append(zooConfig.getClientPortAddress().getPort()); } if (builder.length() == 0) { builder.append(zooConfig.getClientPortAddress().getHostName()).append(':').append(zooConfig.getClientPortAddress().getPort()); } zookeeperConfig = builder.toString(); } } catch (IllegalArgumentException iae) { // ok, try as is } this.zookeeperConfig = zookeeperConfig; }
/** * Test case for https://issues.apache.org/jira/browse/ZOOKEEPER-2847 */ @Test public void testClientAddrFromClientPort() throws IOException, ConfigException { long serverId = 1; QuorumPeerConfig quorumPeerConfig = new MockQuorumPeerConfig(serverId); Properties zkProp = getDefaultZKProperties(); int clientPort = 12345; zkProp.setProperty("clientPort", Integer.toString(clientPort)); zkProp.setProperty("server.1", "127.0.0.1:2889:3889:participant"); quorumPeerConfig.parseProperties(zkProp); QuorumServer qs = quorumPeerConfig.getQuorumVerifier().getAllMembers().get(serverId); InetSocketAddress expectedAddress = new InetSocketAddress("0.0.0.0", clientPort); assertEquals(expectedAddress, quorumPeerConfig.getClientPortAddress()); assertEquals(quorumPeerConfig.getClientPortAddress(), qs.clientAddr); }
parseProperties(cfg); } catch (IOException e) { throw new ConfigException("Error processing " + path, e); String version = getVersionFromFilename(dynamicConfigFileStr); inConfig.close(); setupQuorumPeerConfig(dynamicCfg, false); lastSeenQuorumVerifier = createQuorumVerifier(dynamicConfigNextCfg, isHierarchical); } catch (IOException e) { LOG.warn("NextQuorumVerifier is initiated to null");
public synchronized void start() throws IOException { if (started) { return; } if (quorumPeerConfig.getPurgeInterval() > 0) { datadirCleanupManager = new DatadirCleanupManager(quorumPeerConfig .getDataDir(), quorumPeerConfig.getDataLogDir(), quorumPeerConfig .getSnapRetainCount(), quorumPeerConfig.getPurgeInterval()); datadirCleanupManager.start(); } if (quorumPeerConfig.isDistributed()) { startDistributed(); } else { startStandalone(); } started = true; }
private static ServerCnxnFactory createCnxnFactory(QuorumPeerConfig zkCfg) throws IOException { final InetSocketAddress bindAddr = zkCfg.getClientPortAddress(); final ServerCnxnFactory cnxnFactory = ServerCnxnFactory.createFactory(); // Listen only on 127.0.0.1 because we do not want to expose ZooKeeper to others. cnxnFactory.configure(new InetSocketAddress("127.0.0.1", bindAddr != null ? bindAddr.getPort() : 0), zkCfg.getMaxClientCnxns()); return cnxnFactory; }
@Test public void testDataDirAndDataLogDir() throws Exception { File dataDir = createEmptyTestDir(); File dataLogDir = createEmptyTestDir(); // Arrange try { QuorumPeerConfig configMock = mock(QuorumPeerConfig.class); when(configMock.getDataDir()).thenReturn(dataDir); when(configMock.getDataLogDir()).thenReturn(dataLogDir); when(configMock.getMetricsProviderClassName()) .thenReturn(NullMetricsProvider.class.getName()); QuorumPeer qpMock = mock(QuorumPeer.class); doCallRealMethod().when(qpMock).setTxnFactory(any(FileTxnSnapLog.class)); when(qpMock.getTxnFactory()).thenCallRealMethod(); InjectableQuorumPeerMain qpMain = new InjectableQuorumPeerMain(qpMock); // Act qpMain.runFromConfig(configMock); // Assert FileTxnSnapLog txnFactory = qpMain.getQuorumPeer().getTxnFactory(); Assert.assertEquals(Paths.get(dataLogDir.getAbsolutePath(), "version-2").toString(), txnFactory.getDataDir().getAbsolutePath()); Assert.assertEquals(Paths.get(dataDir.getAbsolutePath(), "version-2").toString(), txnFactory.getSnapDir().getAbsolutePath()); } finally { FileUtils.deleteDirectory(dataDir); FileUtils.deleteDirectory(dataLogDir); } }