Example usage for org.apache.zookeeper.server.quorum QuorumPeer getTxnFactory

List of usage examples for org.apache.zookeeper.server.quorum QuorumPeer getTxnFactory

Introduction

In this page you can find the example usage for org.apache.zookeeper.server.quorum QuorumPeer getTxnFactory.

Prototype

public FileTxnSnapLog getTxnFactory() 

Source Link

Usage

From source file:io.fabric8.core.zookeeper.FabricZooKeeperServer.java

License:Apache License

private Destroyable activateInternal(Map<String, ?> configuration) throws Exception {
    LOGGER.info("Creating zookeeper server with: {}", configuration);

    Properties props = new Properties();
    for (Map.Entry<String, ?> entry : configuration.entrySet()) {
        props.put(entry.getKey(), entry.getValue());
    }//from  w  ww.  j a  va  2  s .co  m

    //Check required directories exist or create them.
    if (!dataDir.exists() && !dataDir.mkdirs()) {
        throw new IOException("Failed to create ZooKeeper dataDir at: " + dataDir.getAbsolutePath());
    }

    if (!dataLogDir.exists() && !dataLogDir.mkdirs()) {
        throw new IOException("Failed to create ZooKeeper dataLogDir at: " + dataLogDir.getAbsolutePath());
    }

    // Create myid file
    String serverId = (String) props.get(SERVER_ID);
    if (serverId != null) {
        props.remove(SERVER_ID);
        File myId = new File(dataDir, MY_ID);
        if (myId.exists() && !myId.delete()) {
            throw new IOException("Failed to delete " + myId);
        }
        if (myId.getParentFile() == null
                || (!myId.getParentFile().exists() && !myId.getParentFile().mkdirs())) {
            throw new IOException("Failed to create " + myId.getParent());
        }
        FileOutputStream fos = new FileOutputStream(myId);
        try {
            fos.write((serverId + "\n").getBytes());
        } finally {
            fos.close();
        }
    }

    QuorumPeerConfig peerConfig = getPeerConfig(props);

    if (!peerConfig.getServers().isEmpty()) {
        NIOServerCnxnFactory cnxnFactory = new NIOServerCnxnFactory();
        cnxnFactory.configure(peerConfig.getClientPortAddress(), peerConfig.getMaxClientCnxns());

        QuorumPeer quorumPeer = new QuorumPeer();
        quorumPeer.setClientPortAddress(peerConfig.getClientPortAddress());
        quorumPeer.setTxnFactory(
                new FileTxnSnapLog(new File(peerConfig.getDataLogDir()), new File(peerConfig.getDataDir())));
        quorumPeer.setQuorumPeers(peerConfig.getServers());
        quorumPeer.setElectionType(peerConfig.getElectionAlg());
        quorumPeer.setMyid(peerConfig.getServerId());
        quorumPeer.setTickTime(peerConfig.getTickTime());
        quorumPeer.setMinSessionTimeout(peerConfig.getMinSessionTimeout());
        quorumPeer.setMaxSessionTimeout(peerConfig.getMaxSessionTimeout());
        quorumPeer.setInitLimit(peerConfig.getInitLimit());
        quorumPeer.setSyncLimit(peerConfig.getSyncLimit());
        quorumPeer.setQuorumVerifier(peerConfig.getQuorumVerifier());
        quorumPeer.setCnxnFactory(cnxnFactory);
        quorumPeer.setZKDatabase(new ZKDatabase(quorumPeer.getTxnFactory()));
        quorumPeer.setLearnerType(peerConfig.getPeerType());

        try {
            LOGGER.debug("Starting quorum peer \"%s\" on address %s", quorumPeer.getMyid(),
                    peerConfig.getClientPortAddress());
            quorumPeer.start();
            LOGGER.debug("Started quorum peer \"%s\"", quorumPeer.getMyid());
        } catch (Exception e) {
            LOGGER.warn(String.format("Failed to start quorum peer \"%s\", reason : %s ", quorumPeer.getMyid(),
                    e.getMessage()));
            quorumPeer.shutdown();
            throw e;
        }

        // Register stats provider
        ClusteredServer server = new ClusteredServer(quorumPeer);
        return server;
    } else {
        ServerConfig serverConfig = getServerConfig(peerConfig);

        ZooKeeperServer zkServer = new ZooKeeperServer();
        FileTxnSnapLog ftxn = new FileTxnSnapLog(new File(serverConfig.getDataLogDir()),
                new File(serverConfig.getDataDir()));
        zkServer.setTxnLogFactory(ftxn);
        zkServer.setTickTime(serverConfig.getTickTime());
        zkServer.setMinSessionTimeout(serverConfig.getMinSessionTimeout());
        zkServer.setMaxSessionTimeout(serverConfig.getMaxSessionTimeout());
        NIOServerCnxnFactory cnxnFactory = new NIOServerCnxnFactory() {
            protected void configureSaslLogin() throws IOException {
            }
        };
        cnxnFactory.configure(serverConfig.getClientPortAddress(), serverConfig.getMaxClientCnxns());

        try {
            LOGGER.debug("Starting ZooKeeper server on address %s", peerConfig.getClientPortAddress());
            cnxnFactory.startup(zkServer);
            LOGGER.debug("Started ZooKeeper server");
        } catch (Exception e) {
            LOGGER.warn(String.format("Failed to start ZooKeeper server, reason : %s", e));
            cnxnFactory.shutdown();
            throw e;
        }

        // Register stats provider
        SimpleServer server = new SimpleServer(zkServer, cnxnFactory);

        return server;
    }
}

From source file:io.fabric8.zookeeper.bootstrap.ZooKeeperServerFactory.java

License:Apache License

public ZooKeeperServerFactory(QuorumPeerConfig peerConfig, String serverId)
        throws IOException, InterruptedException {
    this.peerConfig = peerConfig;
    this.serverId = serverId;

    LOGGER.info("Creating zookeeper server with: {}", peerConfig);

    if (!peerConfig.getServers().isEmpty()) {
        NIOServerCnxnFactory cnxnFactory = new NIOServerCnxnFactory();
        cnxnFactory.configure(peerConfig.getClientPortAddress(), peerConfig.getMaxClientCnxns());

        QuorumPeer quorumPeer = new QuorumPeer();
        quorumPeer.setClientPortAddress(peerConfig.getClientPortAddress());
        quorumPeer.setTxnFactory(/*from  w  w  w.  ja v  a2 s .c  om*/
                new FileTxnSnapLog(new File(peerConfig.getDataLogDir()), new File(peerConfig.getDataDir())));
        quorumPeer.setQuorumPeers(peerConfig.getServers());
        quorumPeer.setElectionType(peerConfig.getElectionAlg());
        quorumPeer.setMyid(peerConfig.getServerId());
        quorumPeer.setTickTime(peerConfig.getTickTime());
        quorumPeer.setMinSessionTimeout(peerConfig.getMinSessionTimeout());
        quorumPeer.setMaxSessionTimeout(peerConfig.getMaxSessionTimeout());
        quorumPeer.setInitLimit(peerConfig.getInitLimit());
        quorumPeer.setSyncLimit(peerConfig.getSyncLimit());
        quorumPeer.setQuorumVerifier(peerConfig.getQuorumVerifier());
        quorumPeer.setCnxnFactory(cnxnFactory);
        quorumPeer.setZKDatabase(new ZKDatabase(quorumPeer.getTxnFactory()));
        quorumPeer.setLearnerType(peerConfig.getPeerType());

        try {
            LOGGER.debug("Starting quorum peer \"%s\" on address %s", quorumPeer.getMyid(),
                    peerConfig.getClientPortAddress());
            quorumPeer.start();
            LOGGER.debug("Started quorum peer \"%s\"", quorumPeer.getMyid());
        } catch (Exception e) {
            LOGGER.warn(String.format("Failed to start quorum peer \"%s\", reason : %s ", quorumPeer.getMyid(),
                    e.getMessage()));
            quorumPeer.shutdown();
            throw e;
        }

        updateZooKeeperURL(cnxnFactory.getLocalAddress(), cnxnFactory.getLocalPort());

        // Register stats provider
        this.clusteredServer = new ClusteredServer(quorumPeer);
        /*
                    registration = context.registerService(QuorumStats.Provider.class, server, null);
                
        */
    } else {
        ServerConfig serverConfig = getServerConfig(peerConfig);

        ZooKeeperServer zkServer = new ZooKeeperServer();
        FileTxnSnapLog ftxn = new FileTxnSnapLog(new File(serverConfig.getDataLogDir()),
                new File(serverConfig.getDataDir()));
        zkServer.setTxnLogFactory(ftxn);
        zkServer.setTickTime(serverConfig.getTickTime());
        zkServer.setMinSessionTimeout(serverConfig.getMinSessionTimeout());
        zkServer.setMaxSessionTimeout(serverConfig.getMaxSessionTimeout());
        NIOServerCnxnFactory cnxnFactory = new NIOServerCnxnFactory() {
            protected void configureSaslLogin() throws IOException {
            }
        };
        InetSocketAddress clientPortAddress = serverConfig.getClientPortAddress();
        cnxnFactory.configure(clientPortAddress, serverConfig.getMaxClientCnxns());
        updateZooKeeperURL(cnxnFactory.getLocalAddress(), cnxnFactory.getLocalPort());

        try {
            LOGGER.debug("Starting ZooKeeper server on address %s", peerConfig.getClientPortAddress());
            cnxnFactory.startup(zkServer);
            LOGGER.debug("Started ZooKeeper server");
        } catch (Exception e) {
            LOGGER.warn(String.format("Failed to start ZooKeeper server, reason : %s", e));
            cnxnFactory.shutdown();
            throw e;
        }

        // Register stats provider
        this.simplerServer = new SimpleServer(zkServer, cnxnFactory);
        /*
                    registration = context.registerService(ServerStats.Provider.class, server, null);
        */
    }
}

From source file:org.fusesource.fabric.zookeeper.bootstrap.ZooKeeperServerFactory.java

License:Apache License

private Destroyable activateInternal(BundleContext context, Map<String, ?> configuration) throws Exception {
    LOGGER.info("Creating zookeeper server with: {}", configuration);

    Properties props = new Properties();
    for (Entry<String, ?> entry : configuration.entrySet()) {
        props.put(entry.getKey(), entry.getValue());
    }/*from   w w  w  .  j a  va 2 s .c  o  m*/

    // Remove the dependency on the current dir from dataDir
    String dataDir = props.getProperty("dataDir");
    if (dataDir != null && dataDir.startsWith(CreateEnsembleOptions.DEFAULT_DATA_DIR)) {
        RuntimeProperties sysprops = runtimeProperties.get();
        dataDir = dataDir.substring(dataDir.indexOf('/'));
        dataDir = sysprops.getProperty(SystemProperties.KARAF_DATA) + dataDir;
        props.setProperty("dataDir", dataDir);
    }

    // Create myid file
    String serverId = (String) props.get("server.id");
    if (serverId != null) {
        props.remove("server.id");
        File myId = new File(dataDir, "myid");
        if (myId.exists() && !myId.delete()) {
            throw new IOException("Failed to delete " + myId);
        }
        if (myId.getParentFile() == null
                || (!myId.getParentFile().exists() && !myId.getParentFile().mkdirs())) {
            throw new IOException("Failed to create " + myId.getParent());
        }
        FileOutputStream fos = new FileOutputStream(myId);
        try {
            fos.write((serverId + "\n").getBytes());
        } finally {
            fos.close();
        }
    }

    QuorumPeerConfig peerConfig = getPeerConfig(props);

    if (!peerConfig.getServers().isEmpty()) {
        NIOServerCnxnFactory cnxnFactory = new NIOServerCnxnFactory();
        cnxnFactory.configure(peerConfig.getClientPortAddress(), peerConfig.getMaxClientCnxns());

        QuorumPeer quorumPeer = new QuorumPeer();
        quorumPeer.setClientPortAddress(peerConfig.getClientPortAddress());
        quorumPeer.setTxnFactory(
                new FileTxnSnapLog(new File(peerConfig.getDataLogDir()), new File(peerConfig.getDataDir())));
        quorumPeer.setQuorumPeers(peerConfig.getServers());
        quorumPeer.setElectionType(peerConfig.getElectionAlg());
        quorumPeer.setMyid(peerConfig.getServerId());
        quorumPeer.setTickTime(peerConfig.getTickTime());
        quorumPeer.setMinSessionTimeout(peerConfig.getMinSessionTimeout());
        quorumPeer.setMaxSessionTimeout(peerConfig.getMaxSessionTimeout());
        quorumPeer.setInitLimit(peerConfig.getInitLimit());
        quorumPeer.setSyncLimit(peerConfig.getSyncLimit());
        quorumPeer.setQuorumVerifier(peerConfig.getQuorumVerifier());
        quorumPeer.setCnxnFactory(cnxnFactory);
        quorumPeer.setZKDatabase(new ZKDatabase(quorumPeer.getTxnFactory()));
        quorumPeer.setLearnerType(peerConfig.getPeerType());

        try {
            LOGGER.debug("Starting quorum peer \"%s\" on address %s", quorumPeer.getMyid(),
                    peerConfig.getClientPortAddress());
            quorumPeer.start();
            LOGGER.debug("Started quorum peer \"%s\"", quorumPeer.getMyid());
        } catch (Exception e) {
            LOGGER.warn(String.format("Failed to start quorum peer \"%s\", reason : %s ", quorumPeer.getMyid(),
                    e.getMessage()));
            quorumPeer.shutdown();
            throw e;
        }

        // Register stats provider
        ClusteredServer server = new ClusteredServer(quorumPeer);
        registration = context.registerService(QuorumStats.Provider.class, server, null);

        return server;
    } else {
        ServerConfig serverConfig = getServerConfig(peerConfig);

        ZooKeeperServer zkServer = new ZooKeeperServer();
        FileTxnSnapLog ftxn = new FileTxnSnapLog(new File(serverConfig.getDataLogDir()),
                new File(serverConfig.getDataDir()));
        zkServer.setTxnLogFactory(ftxn);
        zkServer.setTickTime(serverConfig.getTickTime());
        zkServer.setMinSessionTimeout(serverConfig.getMinSessionTimeout());
        zkServer.setMaxSessionTimeout(serverConfig.getMaxSessionTimeout());
        NIOServerCnxnFactory cnxnFactory = new NIOServerCnxnFactory() {
            protected void configureSaslLogin() throws IOException {
            }
        };
        cnxnFactory.configure(serverConfig.getClientPortAddress(), serverConfig.getMaxClientCnxns());

        try {
            LOGGER.debug("Starting ZooKeeper server on address %s", peerConfig.getClientPortAddress());
            cnxnFactory.startup(zkServer);
            LOGGER.debug("Started ZooKeeper server");
        } catch (Exception e) {
            LOGGER.warn(String.format("Failed to start ZooKeeper server, reason : %s", e));
            cnxnFactory.shutdown();
            throw e;
        }

        // Register stats provider
        SimpleServer server = new SimpleServer(zkServer, cnxnFactory);
        registration = context.registerService(ServerStats.Provider.class, server, null);

        return server;
    }
}

From source file:org.fusesource.fabric.zookeeper.internal.ZKServerFactoryBean.java

License:Apache License

@Override
protected Object doCreate(Dictionary properties) throws Exception {
    LOGGER.info("Creating zookeeper server with properties: {}", properties);
    Properties props = new Properties();
    for (Enumeration ek = properties.keys(); ek.hasMoreElements();) {
        Object key = ek.nextElement();
        Object val = properties.get(key);
        props.put(key.toString(), val != null ? val.toString() : "");
    }/*from w ww .ja  v a  2s . c  o m*/

    // Create myid file
    String serverId = props.getProperty("server.id");
    if (serverId != null) {
        props.remove("server.id");
        File myId = new File(props.getProperty("dataDir"), "myid");
        if (myId.exists() && !myId.delete()) {
            throw new IOException("Failed to delete " + myId);
        }
        if (myId.getParentFile() == null
                || (!myId.getParentFile().exists() && !myId.getParentFile().mkdirs())) {
            throw new IOException("Failed to create " + myId.getParent());
        }
        FileOutputStream fos = new FileOutputStream(myId);
        try {
            fos.write((serverId + "\n").getBytes());
        } finally {
            fos.close();
        }
    }

    // Load properties
    QuorumPeerConfig config = new QuorumPeerConfig();
    config.parseProperties(props);
    if (!config.getServers().isEmpty()) {
        NIOServerCnxnFactory cnxnFactory = new NIOServerCnxnFactory();
        cnxnFactory.configure(config.getClientPortAddress(), config.getMaxClientCnxns());

        QuorumPeer quorumPeer = new QuorumPeer();
        quorumPeer.setClientPortAddress(config.getClientPortAddress());
        quorumPeer.setTxnFactory(
                new FileTxnSnapLog(new File(config.getDataLogDir()), new File(config.getDataDir())));
        quorumPeer.setQuorumPeers(config.getServers());
        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());
        quorumPeer.setCnxnFactory(cnxnFactory);
        quorumPeer.setZKDatabase(new ZKDatabase(quorumPeer.getTxnFactory()));
        quorumPeer.setLearnerType(config.getPeerType());

        try {
            LOGGER.debug("Starting quorum peer \"%s\" on address %s", quorumPeer.getMyid(),
                    config.getClientPortAddress());
            quorumPeer.start();
            LOGGER.debug("Started quorum peer \"%s\"", quorumPeer.getMyid());
        } catch (Exception e) {
            LOGGER.warn(String.format("Failed to start quorum peer \"%s\", reason : %s ", quorumPeer.getMyid(),
                    e.getMessage()));
            quorumPeer.shutdown();
            throw e;
        }
        return new ClusteredServer(quorumPeer);
    } else {
        ServerConfig cfg = new ServerConfig();
        cfg.readFrom(config);

        ZooKeeperServer zkServer = new ZooKeeperServer();
        FileTxnSnapLog ftxn = new FileTxnSnapLog(new File(cfg.getDataLogDir()), new File(cfg.getDataDir()));
        zkServer.setTxnLogFactory(ftxn);
        zkServer.setTickTime(cfg.getTickTime());
        zkServer.setMinSessionTimeout(cfg.getMinSessionTimeout());
        zkServer.setMaxSessionTimeout(cfg.getMaxSessionTimeout());
        NIOServerCnxnFactory cnxnFactory = new NIOServerCnxnFactory();
        cnxnFactory.configure(cfg.getClientPortAddress(), cfg.getMaxClientCnxns());

        try {
            LOGGER.debug("Starting ZooKeeper server on address %s", config.getClientPortAddress());
            cnxnFactory.startup(zkServer);
            LOGGER.debug("Started ZooKeeper server");
        } catch (Exception e) {
            LOGGER.warn(String.format("Failed to start ZooKeeper server, reason : %s", e));
            cnxnFactory.shutdown();
            throw e;
        }
        return new SimpleServer(zkServer, cnxnFactory);
    }
}