Search in sources :

Example 41 with QuorumPeer

use of org.apache.zookeeper.server.quorum.QuorumPeer in project zookeeper by apache.

the class TruncateTest method testTruncate.

@Test
public void testTruncate() throws Exception {
    // Prime the server that is going to come in late with 50 txns
    String hostPort = "127.0.0.1:" + PortAssignment.unique();
    int maxCnxns = 100;
    ServerCnxnFactory factory = ClientBase.createNewServerInstance(null, hostPort, maxCnxns);
    ClientBase.startServerInstance(dataDir1, factory, hostPort, 1);
    ClientBase.shutdownServerInstance(factory, hostPort);
    // standalone starts with 0 epoch while quorum starts with 1
    File origfile = new File(new File(dataDir1, "version-2"), "snapshot.0");
    File newfile = new File(new File(dataDir1, "version-2"), "snapshot.100000000");
    origfile.renameTo(newfile);
    factory = ClientBase.createNewServerInstance(null, hostPort, maxCnxns);
    ClientBase.startServerInstance(dataDir1, factory, hostPort, 1);
    ZooKeeper zk = ClientBase.createZKClient(hostPort, 15000);
    for (int i = 0; i < 50; i++) {
        zk.create("/" + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }
    zk.close();
    ZKDatabase zkDb;
    {
        ZooKeeperServer zs = factory.getZooKeeperServer();
        zkDb = zs.getZKDatabase();
    }
    factory.shutdown();
    try {
        zkDb.close();
    } catch (IOException ie) {
        LOG.warn("Error closing logs ", ie);
    }
    int tickTime = 2000;
    int initLimit = 3;
    int syncLimit = 3;
    int connectToLearnerMasterLimit = 3;
    int port1 = PortAssignment.unique();
    int port2 = PortAssignment.unique();
    int port3 = PortAssignment.unique();
    // Start up two of the quorum and add 10 txns
    Map<Long, QuorumServer> peers = new HashMap<Long, QuorumServer>();
    peers.put(Long.valueOf(1), new QuorumServer(1, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", port1)));
    peers.put(Long.valueOf(2), new QuorumServer(2, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", port2)));
    peers.put(Long.valueOf(3), new QuorumServer(3, new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", PortAssignment.unique()), new InetSocketAddress("127.0.0.1", port3)));
    QuorumPeer s2 = new QuorumPeer(peers, dataDir2, dataDir2, port2, 3, 2, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit);
    s2.start();
    QuorumPeer s3 = new QuorumPeer(peers, dataDir3, dataDir3, port3, 3, 3, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit);
    s3.start();
    zk = ClientBase.createZKClient("127.0.0.1:" + port2, 15000);
    for (int i = 0; i < 10; i++) {
        zk.create("/" + i, new byte[0], ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.PERSISTENT);
    }
    zk.close();
    final ZooKeeper zk2 = ClientBase.createZKClient("127.0.0.1:" + port2, 15000);
    zk2.getData("/9", false, new Stat());
    try {
        zk2.getData("/10", false, new Stat());
        fail("Should have gotten an error");
    } catch (KeeperException.NoNodeException e) {
    // this is what we want
    }
    QuorumPeer s1 = new QuorumPeer(peers, dataDir1, dataDir1, port1, 3, 1, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit);
    s1.start();
    ZooKeeper zk1 = ClientBase.createZKClient("127.0.0.1:" + port1, 15000);
    zk1.getData("/9", false, new Stat());
    try {
        // /10 wont work because the session expiration
        // will match the zxid for /10 and so we wont
        // actually truncate the zxid for /10 creation
        // due to an artifact of switching the xid of the standalone
        // /11 is the last entry in the log for the xid
        // as a result /12 is the first of the truncated znodes to check for
        zk1.getData("/12", false, new Stat());
        fail("Should have gotten an error");
    } catch (KeeperException.NoNodeException e) {
    // this is what we want
    }
    zk1.close();
    QuorumBase.shutdown(s1);
    QuorumBase.shutdown(s2);
    QuorumBase.shutdown(s3);
}
Also used : HashMap(java.util.HashMap) QuorumServer(org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) ZKDatabase(org.apache.zookeeper.server.ZKDatabase) ZooKeeper(org.apache.zookeeper.ZooKeeper) Stat(org.apache.zookeeper.data.Stat) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) ServerCnxnFactory(org.apache.zookeeper.server.ServerCnxnFactory) File(java.io.File) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) KeeperException(org.apache.zookeeper.KeeperException) Test(org.junit.jupiter.api.Test)

Example 42 with QuorumPeer

use of org.apache.zookeeper.server.quorum.QuorumPeer in project zookeeper by apache.

the class QuorumBaseOracle_2Nodes method setupServer.

public void setupServer(int i) throws IOException {
    int tickTime = 2000;
    int initLimit = 3;
    int syncLimit = 3;
    int connectToLearnerMasterLimit = 3;
    if (peers == null) {
        peers = new HashMap<Long, QuorumPeer.QuorumServer>();
        peers.put(Long.valueOf(1), new QuorumPeer.QuorumServer(1, new InetSocketAddress(LOCALADDR, port1), new InetSocketAddress(LOCALADDR, portLE1), new InetSocketAddress(LOCALADDR, portClient1), QuorumPeer.LearnerType.PARTICIPANT));
        peers.put(Long.valueOf(2), new QuorumPeer.QuorumServer(2, new InetSocketAddress(LOCALADDR, port2), new InetSocketAddress(LOCALADDR, portLE2), new InetSocketAddress(LOCALADDR, portClient2), QuorumPeer.LearnerType.PARTICIPANT));
    }
    switch(i) {
        case 1:
            LOG.info("creating QuorumPeer 1 port {}", portClient1);
            s1 = new QuorumPeer(peers, s1dir, s1dir, portClient1, 3, 1, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit);
            assertEquals(portClient1, s1.getClientPort());
            break;
        case 2:
            LOG.info("creating QuorumPeer 2 port {}", portClient2);
            s2 = new QuorumPeer(peers, s2dir, s2dir, portClient2, 3, 2, tickTime, initLimit, syncLimit, connectToLearnerMasterLimit);
            assertEquals(portClient2, s2.getClientPort());
            break;
    }
}
Also used : InetSocketAddress(java.net.InetSocketAddress) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer)

Example 43 with QuorumPeer

use of org.apache.zookeeper.server.quorum.QuorumPeer in project zookeeper by apache.

the class QuorumBaseOracle_2Nodes method getPeersMatching.

public String getPeersMatching(QuorumPeer.ServerState state) {
    StringBuilder hosts = new StringBuilder();
    for (QuorumPeer p : getPeerList()) {
        if (p.getPeerState() == state) {
            hosts.append(String.format("%s:%d,", LOCALADDR, p.getClientAddress().getPort()));
        }
    }
    LOG.info("getPeersMatching ports are {}", hosts);
    return hosts.toString();
}
Also used : QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer)

Example 44 with QuorumPeer

use of org.apache.zookeeper.server.quorum.QuorumPeer in project zookeeper by apache.

the class ThrottledOpQuorumTest method testThrottledOpFollower.

@Test
public void testThrottledOpFollower() throws IOException, InterruptedException, KeeperException {
    ZooKeeper zk = null;
    try {
        int clientPort = (getLeaderClientPort() == portClient1) ? portClient2 : portClient1;
        zk = createClient("localhost:" + clientPort);
        QuorumPeer qp = (getLeaderClientPort() == portClient1) ? s2 : s1;
        ZooKeeperServer zs = qp.getActiveServer();
        ThrottledOpHelper test = new ThrottledOpHelper();
        test.testThrottledOp(zk, zs);
    } finally {
        if (zk != null) {
            zk.close();
        }
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) Test(org.junit.jupiter.api.Test)

Example 45 with QuorumPeer

use of org.apache.zookeeper.server.quorum.QuorumPeer in project zookeeper by apache.

the class ThrottledOpQuorumTest method testThrottledAclFollower.

@Test
public void testThrottledAclFollower() throws Exception {
    ZooKeeper zk = null;
    try {
        int clientPort = (getLeaderClientPort() == portClient1) ? portClient2 : portClient1;
        zk = createClient("localhost:" + clientPort);
        QuorumPeer qp = (getLeaderClientPort() == portClient1) ? s2 : s1;
        ZooKeeperServer zs = qp.getActiveServer();
        ThrottledOpHelper test = new ThrottledOpHelper();
        test.testThrottledAcl(zk, zs);
    } finally {
        if (zk != null) {
            zk.close();
        }
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) QuorumPeer(org.apache.zookeeper.server.quorum.QuorumPeer) ZooKeeperServer(org.apache.zookeeper.server.ZooKeeperServer) Test(org.junit.jupiter.api.Test)

Aggregations

QuorumPeer (org.apache.zookeeper.server.quorum.QuorumPeer)50 InetSocketAddress (java.net.InetSocketAddress)19 Test (org.junit.jupiter.api.Test)18 QuorumServer (org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer)17 IOException (java.io.IOException)9 ZooKeeper (org.apache.zookeeper.ZooKeeper)9 Test (org.junit.Test)9 File (java.io.File)8 ArrayList (java.util.ArrayList)6 HashMap (java.util.HashMap)6 FileTxnSnapLog (org.apache.zookeeper.server.persistence.FileTxnSnapLog)6 ZKDatabase (org.apache.zookeeper.server.ZKDatabase)5 QuorumCnxManager (org.apache.zookeeper.server.quorum.QuorumCnxManager)5 ByteBuffer (java.nio.ByteBuffer)4 LinkedHashSet (java.util.LinkedHashSet)3 Stat (org.apache.zookeeper.data.Stat)3 CreateRequest (org.apache.zookeeper.proto.CreateRequest)3 ZooKeeperServer (org.apache.zookeeper.server.ZooKeeperServer)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 ServerSocket (java.net.ServerSocket)2