Search in sources :

Example 11 with ServerCnxnFactory

use of org.apache.zookeeper.server.ServerCnxnFactory in project zookeeper by apache.

the class StandaloneTest method testStandaloneReconfigFails.

/**
 * Verify that reconfiguration in standalone mode fails with
 * KeeperException.UnimplementedException.
 */
@Test
public void testStandaloneReconfigFails() throws Exception {
    ClientBase.setupTestEnv();
    final int CLIENT_PORT = PortAssignment.unique();
    final String HOSTPORT = "127.0.0.1:" + CLIENT_PORT;
    File tmpDir = ClientBase.createTmpDir();
    ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    ServerCnxnFactory f = ServerCnxnFactory.createFactory(CLIENT_PORT, -1);
    f.startup(zks);
    Assert.assertTrue("waiting for server being up ", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT));
    CountdownWatcher watcher = new CountdownWatcher();
    ZooKeeper zk = new ZooKeeper(HOSTPORT, CONNECTION_TIMEOUT, watcher);
    ZooKeeperAdmin zkAdmin = new ZooKeeperAdmin(HOSTPORT, CONNECTION_TIMEOUT, watcher);
    watcher.waitForConnected(CONNECTION_TIMEOUT);
    List<String> joiners = new ArrayList<String>();
    joiners.add("server.2=localhost:1234:1235;1236");
    // generate some transactions that will get logged
    try {
        zkAdmin.addAuthInfo("digest", "super:test".getBytes());
        zkAdmin.reconfigure(joiners, null, null, -1, new Stat());
        Assert.fail("Reconfiguration in standalone should trigger " + "UnimplementedException");
    } catch (KeeperException.UnimplementedException ex) {
    // expected
    }
    zk.close();
    zks.shutdown();
    f.shutdown();
    Assert.assertTrue("waiting for server being down ", ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT));
}
Also used : CountdownWatcher(org.apache.zookeeper.test.ClientBase.CountdownWatcher) ArrayList(java.util.ArrayList) ZooKeeperAdmin(org.apache.zookeeper.admin.ZooKeeperAdmin) ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) KeeperException(org.apache.zookeeper.KeeperException) Test(org.junit.Test)

Example 12 with ServerCnxnFactory

use of org.apache.zookeeper.server.ServerCnxnFactory in project zookeeper by apache.

the class QuorumPeerMain method runFromConfig.

public void runFromConfig(QuorumPeerConfig config) throws IOException, AdminServerException {
    try {
        ManagedUtil.registerLog4jMBeans();
    } catch (JMException e) {
        LOG.warn("Unable to register log4j JMX control", e);
    }
    LOG.info("Starting quorum peer");
    try {
        ServerCnxnFactory cnxnFactory = null;
        ServerCnxnFactory secureCnxnFactory = null;
        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 = getQuorumPeer();
        quorumPeer.setTxnFactory(new FileTxnSnapLog(config.getDataLogDir(), config.getDataDir()));
        quorumPeer.enableLocalSessions(config.areLocalSessionsEnabled());
        quorumPeer.enableLocalSessionsUpgrading(config.isLocalSessionsUpgradingEnabled());
        // quorumPeer.setQuorumPeers(config.getAllMembers());
        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.setConfigFileName(config.getConfigFilename());
        quorumPeer.setZKDatabase(new ZKDatabase(quorumPeer.getTxnFactory()));
        quorumPeer.setQuorumVerifier(config.getQuorumVerifier(), false);
        if (config.getLastSeenQuorumVerifier() != null) {
            quorumPeer.setLastSeenQuorumVerifier(config.getLastSeenQuorumVerifier(), false);
        }
        quorumPeer.initConfigInZKDatabase();
        quorumPeer.setCnxnFactory(cnxnFactory);
        quorumPeer.setSecureCnxnFactory(secureCnxnFactory);
        quorumPeer.setLearnerType(config.getPeerType());
        quorumPeer.setSyncEnabled(config.getSyncEnabled());
        quorumPeer.setQuorumListenOnAllIPs(config.getQuorumListenOnAllIPs());
        // sets quorum sasl authentication configurations
        quorumPeer.setQuorumSaslEnabled(config.quorumEnableSasl);
        if (quorumPeer.isQuorumSaslAuthEnabled()) {
            quorumPeer.setQuorumServerSaslRequired(config.quorumServerRequireSasl);
            quorumPeer.setQuorumLearnerSaslRequired(config.quorumLearnerRequireSasl);
            quorumPeer.setQuorumServicePrincipal(config.quorumServicePrincipal);
            quorumPeer.setQuorumServerLoginContext(config.quorumServerLoginContext);
            quorumPeer.setQuorumLearnerLoginContext(config.quorumLearnerLoginContext);
        }
        quorumPeer.setQuorumCnxnThreadsSize(config.quorumCnxnThreadsSize);
        quorumPeer.initialize();
        quorumPeer.start();
        quorumPeer.join();
    } catch (InterruptedException e) {
        // warn, but generally this is ok
        LOG.warn("Quorum Peer interrupted", e);
    }
}
Also used : JMException(javax.management.JMException) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) ZKDatabase(org.apache.zookeeper.server.ZKDatabase) FileTxnSnapLog(org.apache.zookeeper.server.persistence.FileTxnSnapLog)

Example 13 with ServerCnxnFactory

use of org.apache.zookeeper.server.ServerCnxnFactory in project zookeeper by apache.

the class GetProposalFromTxnTest method testGetProposalFromTxn.

/**
 * Test loading proposal from txnlog
 *
 * @throws Exception
 *             an exception might be thrown here
 */
@Test
public void testGetProposalFromTxn() throws Exception {
    File tmpDir = ClientBase.createTmpDir();
    ClientBase.setupTestEnv();
    ZooKeeperServer zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    SyncRequestProcessor.setSnapCount(100);
    final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]);
    ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1);
    f.startup(zks);
    Assert.assertTrue("waiting for server being up ", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT));
    ZooKeeper zk = ClientBase.createZKClient(HOSTPORT);
    // Generate transaction so we will have some txnlog
    Long[] zxids = new Long[MSG_COUNT];
    try {
        String data = "data";
        byte[] bytes = data.getBytes();
        for (int i = 0; i < MSG_COUNT; i++) {
            Stat stat = new Stat();
            zk.create("/invalidsnap-" + i, bytes, Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
            zk.getData("/invalidsnap-" + i, null, stat);
            zxids[i] = stat.getCzxid();
        }
    } finally {
        zk.close();
    }
    // shutdown and start zookeeper again
    f.shutdown();
    zks.shutdown();
    Assert.assertTrue("waiting for server to shutdown", ClientBase.waitForServerDown(HOSTPORT, CONNECTION_TIMEOUT));
    zks = new ZooKeeperServer(tmpDir, tmpDir, 3000);
    zks.startdata();
    ZKDatabase db = zks.getZKDatabase();
    // Set sizeLimit to be very high number, so we can pull all transactions
    // from txnlog
    Iterator<Proposal> itr = db.getProposalsFromTxnLog(zxids[0], 10000000);
    int createCount = 0;
    ArrayList<Long> retrievedZxids = new ArrayList<Long>(MSG_COUNT);
    // Get zxid of create requests
    while (itr.hasNext()) {
        Proposal proposal = itr.next();
        TxnHeader hdr = new TxnHeader();
        Record rec = SerializeUtils.deserializeTxn(proposal.packet.getData(), hdr);
        if (hdr.getType() == OpCode.create) {
            retrievedZxids.add(hdr.getZxid());
            createCount++;
        }
    }
    // All zxid should match what we created
    Assert.assertTrue("Zxids missmatches", Arrays.equals(zxids, retrievedZxids.toArray(new Long[0])));
    // There should be 2000 create requests
    Assert.assertTrue("create proposal count == " + MSG_COUNT, (createCount == MSG_COUNT));
    // We are requesting half the number of transaction from the snapshot
    // this should exceed threshold (ZKDatabase.snapshotSizeFactor)
    db.setSnapshotSizeFactor(0.33);
    long sizeLimit = db.calculateTxnLogSizeLimit();
    itr = db.getProposalsFromTxnLog(zxids[MSG_COUNT / 2], sizeLimit);
    Assert.assertFalse("Expect empty proposal", (itr.hasNext()));
    f.shutdown();
    zks.shutdown();
}
Also used : ArrayList(java.util.ArrayList) ZKDatabase(org.apache.zookeeper.server.ZKDatabase) ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) Record(org.apache.jute.Record) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) Proposal(org.apache.zookeeper.server.quorum.Leader.Proposal) TxnHeader(org.apache.zookeeper.txn.TxnHeader) Test(org.junit.Test)

Example 14 with ServerCnxnFactory

use of org.apache.zookeeper.server.ServerCnxnFactory in project zookeeper by apache.

the class InvalidSnapshotTest method testSnapshot.

/**
 * test the snapshot
 * @throws Exception an exception could be expected
 */
@Test
public void testSnapshot() throws Exception {
    File snapDir = new File(testData, "invalidsnap");
    ZooKeeperServer zks = new ZooKeeperServer(snapDir, snapDir, 3000);
    SyncRequestProcessor.setSnapCount(1000);
    final int PORT = Integer.parseInt(HOSTPORT.split(":")[1]);
    ServerCnxnFactory f = ServerCnxnFactory.createFactory(PORT, -1);
    f.startup(zks);
    LOG.info("starting up the zookeeper server .. waiting");
    Assert.assertTrue("waiting for server being up", ClientBase.waitForServerUp(HOSTPORT, CONNECTION_TIMEOUT));
    ZooKeeper zk = ClientBase.createZKClient(HOSTPORT);
    try {
        // we know this from the data files
        // this node is the last node in the snapshot
        Assert.assertTrue(zk.exists("/9/9/8", false) != null);
    } finally {
        zk.close();
    }
    f.shutdown();
    zks.shutdown();
    Assert.assertTrue("waiting for server down", ClientBase.waitForServerDown(HOSTPORT, ClientBase.CONNECTION_TIMEOUT));
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) Test(org.junit.Test)

Example 15 with ServerCnxnFactory

use of org.apache.zookeeper.server.ServerCnxnFactory in project zookeeper by apache.

the class LocalPeerBeanTest method testClientAddress.

/**
 * Test case for https://issues.apache.org/jira/browse/ZOOKEEPER-2299
 */
@Test
public void testClientAddress() throws Exception {
    QuorumPeer quorumPeer = new QuorumPeer();
    LocalPeerBean remotePeerBean = new LocalPeerBean(quorumPeer);
    /**
     * Case 1: When cnxnFactory is null
     */
    String result = remotePeerBean.getClientAddress();
    assertNotNull(result);
    assertEquals(0, result.length());
    /**
     * Case 2: When only client port is configured
     */
    ServerCnxnFactory cnxnFactory = ServerCnxnFactory.createFactory();
    int clientPort = PortAssignment.unique();
    InetSocketAddress address = new InetSocketAddress(clientPort);
    cnxnFactory.configure(address, 5, false);
    quorumPeer.setCnxnFactory(cnxnFactory);
    result = remotePeerBean.getClientAddress();
    String ipv4 = "0.0.0.0:" + clientPort;
    String ipv6 = "0:0:0:0:0:0:0:0:" + clientPort;
    assertTrue(result.equals(ipv4) || result.equals(ipv6));
    // cleanup
    cnxnFactory.shutdown();
    /**
     * Case 3: When both client port and client address is configured
     */
    clientPort = PortAssignment.unique();
    InetAddress clientIP = InetAddress.getLoopbackAddress();
    address = new InetSocketAddress(clientIP, clientPort);
    cnxnFactory = ServerCnxnFactory.createFactory();
    cnxnFactory.configure(address, 5, false);
    quorumPeer.setCnxnFactory(cnxnFactory);
    result = remotePeerBean.getClientAddress();
    String expectedResult = clientIP.getHostAddress() + ":" + clientPort;
    assertEquals(expectedResult, result);
    // cleanup
    cnxnFactory.shutdown();
}
Also used : InetSocketAddress(java.net.InetSocketAddress) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Aggregations

ServerCnxnFactory (org.apache.zookeeper.server.ServerCnxnFactory)23 ZooKeeperServer (org.apache.zookeeper.server.ZooKeeperServer)20 Test (org.junit.Test)19 ZooKeeper (org.apache.zookeeper.ZooKeeper)17 File (java.io.File)16 ArrayList (java.util.ArrayList)7 Stat (org.apache.zookeeper.data.Stat)6 IOException (java.io.IOException)4 ZKDatabase (org.apache.zookeeper.server.ZKDatabase)4 InetSocketAddress (java.net.InetSocketAddress)3 ACL (org.apache.zookeeper.data.ACL)3 FileTxnSnapLog (org.apache.zookeeper.server.persistence.FileTxnSnapLog)3 PrintWriter (java.io.PrintWriter)2 InetAddress (java.net.InetAddress)2 CountDownLatch (java.util.concurrent.CountDownLatch)2 KeeperException (org.apache.zookeeper.KeeperException)2 InvalidACLException (org.apache.zookeeper.KeeperException.InvalidACLException)2 Proposal (org.apache.zookeeper.server.quorum.Leader.Proposal)2 StringWriter (java.io.StringWriter)1 NetworkInterface (java.net.NetworkInterface)1