Search in sources :

Example 6 with FileTxnSnapLog

use of org.apache.zookeeper.server.persistence.FileTxnSnapLog in project fabric8 by jboss-fuse.

the class ZooKeeperServerFactory method activateInternal.

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());
    }
    // Remove the dependency on the current dir from dataDir
    String dataDir = props.getProperty("dataDir");
    if (dataDir != null && !Paths.get(dataDir).isAbsolute()) {
        dataDir = runtimeProperties.get().getDataPath().resolve(dataDir).toFile().getAbsolutePath();
        props.setProperty("dataDir", dataDir);
    }
    props.put("clientPortAddress", bootstrapConfiguration.get().getBindAddress());
    // 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 \"{}\" on address {}", quorumPeer.getMyid(), peerConfig.getClientPortAddress());
            quorumPeer.start();
            LOGGER.debug("Started quorum peer \"{}\"", quorumPeer.getMyid());
        } catch (Exception e) {
            LOGGER.warn("Failed to start quorum peer \"{}\", reason : {} ", 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 {}", peerConfig.getClientPortAddress());
            cnxnFactory.startup(zkServer);
            LOGGER.debug("Started ZooKeeper server");
        } catch (Exception e) {
            LOGGER.warn("Failed to start ZooKeeper server, reason : {}", e);
            cnxnFactory.shutdown();
            throw e;
        }
        // Register stats provider
        SimpleServer server = new SimpleServer(zkServer, cnxnFactory);
        registration = context.registerService(ServerStats.Provider.class, server, null);
        startCleanupManager(serverConfig, props);
        return server;
    }
}
Also used : IOException(java.io.IOException) RuntimeProperties(io.fabric8.api.RuntimeProperties) Properties(java.util.Properties) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) ConfigException(org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException) IOException(java.io.IOException) QuorumPeerConfig(org.apache.zookeeper.server.quorum.QuorumPeerConfig) FileOutputStream(java.io.FileOutputStream) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) File(java.io.File)

Example 7 with FileTxnSnapLog

use of org.apache.zookeeper.server.persistence.FileTxnSnapLog in project wildfly-camel by wildfly-extras.

the class ZKServerFactoryBean method afterPropertiesSet.

public void afterPropertiesSet() throws Exception {
    if (purge) {
        deleteFilesInDir(getDataLogDir());
        deleteFilesInDir(getDataDir());
    }
    FileTxnSnapLog ftxn = new FileTxnSnapLog(getDataLogDir(), getDataDir());
    zooKeeperServer.setTxnLogFactory(ftxn);
    zooKeeperServer.setTickTime(getTickTime());
    zooKeeperServer.setMinSessionTimeout(getMinSessionTimeout());
    zooKeeperServer.setMaxSessionTimeout(getMaxSessionTimeout());
    connectionFactory = new NIOServerCnxnFactory() {

        @Override
        protected void configureSaslLogin() throws IOException {
        // do nothing
        }
    };
    connectionFactory.configure(getClientPortAddress(), getMaxClientConnections());
    connectionFactory.startup(zooKeeperServer);
}
Also used : NIOServerCnxnFactory(org.apache.zookeeper.server.NIOServerCnxnFactory) IOException(java.io.IOException) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog)

Example 8 with FileTxnSnapLog

use of org.apache.zookeeper.server.persistence.FileTxnSnapLog in project airavata by apache.

the class AiravataZKUtils method runZKFromConfig.

public static void runZKFromConfig(ServerConfig config, ServerCnxnFactory cnxnFactory) throws IOException {
    AiravataZKUtils.logger.info("Starting Zookeeper server...");
    FileTxnSnapLog txnLog = null;
    try {
        // Note that this thread isn't going to be doing anything else,
        // so rather than spawning another thread, we will just call
        // run() in this thread.
        // create a file logger url from the command line args
        ZooKeeperServer zkServer = new ZooKeeperServer();
        txnLog = new FileTxnSnapLog(new File(config.getDataDir()), new File(config.getDataDir()));
        zkServer.setTxnLogFactory(txnLog);
        zkServer.setTickTime(config.getTickTime());
        zkServer.setMinSessionTimeout(config.getMinSessionTimeout());
        zkServer.setMaxSessionTimeout(config.getMaxSessionTimeout());
        cnxnFactory = ServerCnxnFactory.createFactory();
        cnxnFactory.configure(config.getClientPortAddress(), config.getMaxClientCnxns());
        cnxnFactory.startup(zkServer);
        cnxnFactory.join();
        if (zkServer.isRunning()) {
            zkServer.shutdown();
        }
    } catch (InterruptedException e) {
        // warn, but generally this is ok
        AiravataZKUtils.logger.warn("Server interrupted", e);
        System.exit(1);
    } finally {
        if (txnLog != null) {
            txnLog.close();
        }
    }
}
Also used : File(java.io.File) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer)

Example 9 with FileTxnSnapLog

use of org.apache.zookeeper.server.persistence.FileTxnSnapLog in project ecf by eclipse.

the class ZooDiscoveryContainer method startStandAlone.

/**
 * Start a ZooKeeer server locally to write nodes to. Implied by
 * {@link IDiscoveryConfig#ZOODISCOVERY_FLAVOR_STANDALONE} configuration.
 *
 * @param conf
 */
void startStandAlone(final Configuration conf) {
    if (ZooDiscoveryContainer.zooKeeperServer != null && ZooDiscoveryContainer.zooKeeperServer.isRunning())
        return;
    else if (ZooDiscoveryContainer.zooKeeperServer != null && !ZooDiscoveryContainer.zooKeeperServer.isRunning())
        try {
            ZooDiscoveryContainer.zooKeeperServer.startup();
            return;
        } catch (Exception e) {
            // $NON-NLS-1$
            Logger.log(LogService.LOG_DEBUG, "Zookeeper server cannot be started! ", e);
        }
    try {
        ZooDiscoveryContainer.zooKeeperServer = new ZooKeeperServer();
        FileTxnSnapLog fileTxnSnapLog = new FileTxnSnapLog(conf.getZookeeperDataFile(), conf.getZookeeperDataFile());
        ZooDiscoveryContainer.zooKeeperServer.setTxnLogFactory(fileTxnSnapLog);
        ZooDiscoveryContainer.zooKeeperServer.setTickTime(conf.getTickTime());
        Factory cnxnFactory = new NIOServerCnxn.Factory(new InetSocketAddress(conf.getClientPort()));
        cnxnFactory.startup(ZooDiscoveryContainer.zooKeeperServer);
    } catch (Exception e) {
        Logger.log(LogService.LOG_ERROR, "Zookeeper server cannot be started! Possibly another instance is already running on the same port. ", e);
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) Factory(org.apache.zookeeper.server.NIOServerCnxn.Factory) IOException(java.io.IOException) ContainerConnectException(org.eclipse.ecf.core.ContainerConnectException) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog)

Example 10 with FileTxnSnapLog

use of org.apache.zookeeper.server.persistence.FileTxnSnapLog in project zookeeper by apache.

the class ZkDatabaseCorruptionTest method testCorruption.

@Test
public void testCorruption() throws Exception {
    ClientBase.waitForServerUp(qb.hostPort, 10000);
    ClientBase.waitForServerUp(qb.hostPort, 10000);
    ZooKeeper zk = ClientBase.createZKClient(qb.hostPort, 10000);
    SyncRequestProcessor.setSnapCount(100);
    for (int i = 0; i < 2000; i++) {
        zk.create("/0-" + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, new NoopStringCallback(), null);
    }
    zk.close();
    long leaderSid = 1;
    QuorumPeer leader = null;
    // find out who is the leader and kill it
    for (QuorumPeer quorumPeer : Arrays.asList(qb.s1, qb.s2, qb.s3, qb.s4, qb.s5)) {
        if (quorumPeer.getPeerState() == ServerState.LEADING) {
            leader = quorumPeer;
            break;
        }
        ++leaderSid;
    }
    assertNotNull(leader, "Cannot find the leader.");
    leader.shutdown();
    // now corrupt the leader's database
    FileTxnSnapLog snapLog = leader.getTxnFactory();
    File snapDir = snapLog.getSnapDir();
    // corrupt all the snapshot in the snapshot directory
    corruptAllSnapshots(snapDir);
    qb.shutdownServers();
    qb.setupServers();
    if (leaderSid != 1) {
        qb.s1.start();
    } else {
        leader = qb.s1;
    }
    if (leaderSid != 2) {
        qb.s2.start();
    } else {
        leader = qb.s2;
    }
    if (leaderSid != 3) {
        qb.s3.start();
    } else {
        leader = qb.s3;
    }
    if (leaderSid != 4) {
        qb.s4.start();
    } else {
        leader = qb.s4;
    }
    if (leaderSid != 5) {
        qb.s5.start();
    } else {
        leader = qb.s5;
    }
    try {
        leader.start();
        assertTrue(false);
    } catch (RuntimeException re) {
        LOG.info("Got an error: expected", re);
    }
    // wait for servers to be up
    String[] list = qb.hostPort.split(",");
    for (int i = 0; i < 5; i++) {
        if (leaderSid != (i + 1)) {
            String hp = list[i];
            assertTrue(ClientBase.waitForServerUp(hp, CONNECTION_TIMEOUT), "waiting for server up");
            LOG.info("{} is accepting client connections", hp);
        } else {
            LOG.info("Skipping the leader");
        }
    }
    zk = qb.createClient();
    SyncRequestProcessor.setSnapCount(100);
    for (int i = 2000; i < 4000; i++) {
        zk.create("/0-" + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT, new NoopStringCallback(), null);
    }
    zk.close();
    if (leaderSid != 1) {
        QuorumBase.shutdown(qb.s1);
    }
    if (leaderSid != 2) {
        QuorumBase.shutdown(qb.s2);
    }
    if (leaderSid != 3) {
        QuorumBase.shutdown(qb.s3);
    }
    if (leaderSid != 4) {
        QuorumBase.shutdown(qb.s4);
    }
    if (leaderSid != 5) {
        QuorumBase.shutdown(qb.s5);
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) RandomAccessFile(java.io.RandomAccessFile) File(java.io.File) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog) Test(org.junit.jupiter.api.Test)

Aggregations

FileTxnSnapLog (org.apache.zookeeper.server.persistence.FileTxnSnapLog)54 File (java.io.File)37 Test (org.junit.jupiter.api.Test)20 ZKDatabase (org.apache.zookeeper.server.ZKDatabase)17 IOException (java.io.IOException)13 NIOServerCnxnFactory (org.apache.zookeeper.server.NIOServerCnxnFactory)10 ZooKeeperServer (org.apache.zookeeper.server.ZooKeeperServer)10 ServerConfig (org.apache.zookeeper.server.ServerConfig)8 ZooKeeper (org.apache.zookeeper.ZooKeeper)6 QuorumPeer (org.apache.zookeeper.server.quorum.QuorumPeer)6 ArrayList (java.util.ArrayList)5 ServerCnxnFactory (org.apache.zookeeper.server.ServerCnxnFactory)5 TxnHeader (org.apache.zookeeper.txn.TxnHeader)5 BinaryOutputArchive (org.apache.jute.BinaryOutputArchive)4 ConfigException (org.apache.zookeeper.server.quorum.QuorumPeerConfig.ConfigException)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 Socket (java.net.Socket)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 Record (org.apache.jute.Record)3 Test (org.junit.Test)3