Search in sources :

Example 6 with QuorumVerifier

use of org.apache.zookeeper.server.quorum.flexible.QuorumVerifier in project zookeeper by apache.

the class QuorumCnxManager method connectOne.

/**
     * Try to establish a connection to server with id sid.
     * 
     *  @param sid  server id
     */
synchronized void connectOne(long sid) {
    if (senderWorkerMap.get(sid) != null) {
        LOG.debug("There is a connection already for server " + sid);
        return;
    }
    synchronized (self.QV_LOCK) {
        boolean knownId = false;
        // Resolve hostname for the remote server before attempting to
        // connect in case the underlying ip address has changed.
        self.recreateSocketAddresses(sid);
        Map<Long, QuorumPeer.QuorumServer> lastCommittedView = self.getView();
        QuorumVerifier lastSeenQV = self.getLastSeenQuorumVerifier();
        Map<Long, QuorumPeer.QuorumServer> lastProposedView = lastSeenQV.getAllMembers();
        if (lastCommittedView.containsKey(sid)) {
            knownId = true;
            if (connectOne(sid, lastCommittedView.get(sid).electionAddr))
                return;
        }
        if (lastSeenQV != null && lastProposedView.containsKey(sid) && (!knownId || (lastProposedView.get(sid).electionAddr != lastCommittedView.get(sid).electionAddr))) {
            knownId = true;
            if (connectOne(sid, lastProposedView.get(sid).electionAddr))
                return;
        }
        if (!knownId) {
            LOG.warn("Invalid server id: " + sid);
            return;
        }
    }
}
Also used : QuorumVerifier(org.apache.zookeeper.server.quorum.flexible.QuorumVerifier)

Example 7 with QuorumVerifier

use of org.apache.zookeeper.server.quorum.flexible.QuorumVerifier in project zookeeper by apache.

the class QuorumPeer method recreateSocketAddresses.

/**
     * Resolves hostname for a given server ID.
     *
     * This method resolves hostname for a given server ID in both quorumVerifer
     * and lastSeenQuorumVerifier. If the server ID matches the local server ID,
     * it also updates myQuorumAddr and myElectionAddr.
     */
public void recreateSocketAddresses(long id) {
    QuorumVerifier qv = getQuorumVerifier();
    if (qv != null) {
        QuorumServer qs = qv.getAllMembers().get(id);
        if (qs != null) {
            qs.recreateSocketAddresses();
            if (id == getId()) {
                setQuorumAddress(qs.addr);
                setElectionAddress(qs.electionAddr);
            }
        }
    }
    qv = getLastSeenQuorumVerifier();
    if (qv != null) {
        QuorumServer qs = qv.getAllMembers().get(id);
        if (qs != null) {
            qs.recreateSocketAddresses();
        }
    }
}
Also used : QuorumVerifier(org.apache.zookeeper.server.quorum.flexible.QuorumVerifier)

Example 8 with QuorumVerifier

use of org.apache.zookeeper.server.quorum.flexible.QuorumVerifier in project zookeeper by apache.

the class FastLeaderElection method sendNotifications.

/**
     * Send notifications to all peers upon a change in our vote
     */
private void sendNotifications() {
    for (long sid : self.getCurrentAndNextConfigVoters()) {
        QuorumVerifier qv = self.getQuorumVerifier();
        ToSend notmsg = new ToSend(ToSend.mType.notification, proposedLeader, proposedZxid, logicalclock.get(), QuorumPeer.ServerState.LOOKING, sid, proposedEpoch, qv.toString().getBytes());
        if (LOG.isDebugEnabled()) {
            LOG.debug("Sending Notification: " + proposedLeader + " (n.leader), 0x" + Long.toHexString(proposedZxid) + " (n.zxid), 0x" + Long.toHexString(logicalclock.get()) + " (n.round), " + sid + " (recipient), " + self.getId() + " (myid), 0x" + Long.toHexString(proposedEpoch) + " (n.peerEpoch)");
        }
        sendqueue.offer(notmsg);
    }
}
Also used : QuorumVerifier(org.apache.zookeeper.server.quorum.flexible.QuorumVerifier)

Example 9 with QuorumVerifier

use of org.apache.zookeeper.server.quorum.flexible.QuorumVerifier in project zookeeper by apache.

the class ReconfigTest method testRemoveAddOne.

@Test
public void testRemoveAddOne() throws Exception {
    // create 3 servers
    qu = new QuorumUtil(1);
    qu.disableJMXTest = true;
    qu.startAll();
    ZooKeeper[] zkArr = createHandles(qu);
    ZooKeeperAdmin[] zkAdminArr = createAdminHandles(qu);
    List<String> leavingServers = new ArrayList<String>();
    List<String> joiningServers = new ArrayList<String>();
    int leaderIndex = getLeaderId(qu);
    // during first iteration, leavingIndex will correspond to a follower
    // during second iteration leavingIndex will be the index of the leader
    int leavingIndex = (leaderIndex == 1) ? 2 : 1;
    for (int i = 0; i < 2; i++) {
        // some of the operations will be executed by a client connected to
        // the removed server
        // while others are invoked by a client connected to some other
        // server.
        // when we're removing the leader, zk1 will be the client connected
        // to removed server
        ZooKeeper zk1 = (leavingIndex == leaderIndex) ? zkArr[leaderIndex] : zkArr[(leaderIndex % qu.ALL) + 1];
        ZooKeeper zk2 = (leavingIndex == leaderIndex) ? zkArr[(leaderIndex % qu.ALL) + 1] : zkArr[leaderIndex];
        ZooKeeperAdmin zkAdmin1 = (leavingIndex == leaderIndex) ? zkAdminArr[leaderIndex] : zkAdminArr[(leaderIndex % qu.ALL) + 1];
        ZooKeeperAdmin zkAdmin2 = (leavingIndex == leaderIndex) ? zkAdminArr[(leaderIndex % qu.ALL) + 1] : zkAdminArr[leaderIndex];
        leavingServers.add(Integer.toString(leavingIndex));
        // remember this server so we can add it back later
        joiningServers.add("server." + leavingIndex + "=localhost:" + qu.getPeer(leavingIndex).peer.getQuorumAddress().getPort() + ":" + qu.getPeer(leavingIndex).peer.getElectionAddress().getPort() + ":participant;localhost:" + qu.getPeer(leavingIndex).peer.getClientPort());
        String configStr = reconfig(zkAdmin1, null, leavingServers, null, -1);
        testServerHasConfig(zk2, null, leavingServers);
        testNormalOperation(zk2, zk1);
        QuorumVerifier qv = qu.getPeer(1).peer.configFromString(configStr);
        long version = qv.getVersion();
        // checks that conditioning on version works properly
        try {
            reconfig(zkAdmin2, joiningServers, null, null, version + 1);
            Assert.fail("reconfig succeeded even though version condition was incorrect!");
        } catch (KeeperException.BadVersionException e) {
        }
        reconfig(zkAdmin2, joiningServers, null, null, version);
        testNormalOperation(zk1, zk2);
        testServerHasConfig(zk1, joiningServers, null);
        // second iteration of the loop will remove the leader
        // and add it back (as follower)
        leavingIndex = leaderIndex = getLeaderId(qu);
        leavingServers.clear();
        joiningServers.clear();
    }
    closeAllHandles(zkArr, zkAdminArr);
}
Also used : ArrayList(java.util.ArrayList) ZooKeeperAdmin(org.apache.zookeeper.admin.ZooKeeperAdmin) QuorumVerifier(org.apache.zookeeper.server.quorum.flexible.QuorumVerifier) ZooKeeper(org.apache.zookeeper.ZooKeeper) KeeperException(org.apache.zookeeper.KeeperException) Test(org.junit.Test)

Example 10 with QuorumVerifier

use of org.apache.zookeeper.server.quorum.flexible.QuorumVerifier in project zookeeper by apache.

the class ReconfigTest method testInitialConfigHasPositiveVersion.

@Test
public void testInitialConfigHasPositiveVersion() throws Exception {
    // create 3 servers
    qu = new QuorumUtil(1);
    qu.disableJMXTest = true;
    qu.startAll();
    ZooKeeper[] zkArr = createHandles(qu);
    testNormalOperation(zkArr[1], zkArr[2]);
    for (int i = 1; i < 4; i++) {
        String configStr = testServerHasConfig(zkArr[i], null, null);
        QuorumVerifier qv = qu.getPeer(i).peer.configFromString(configStr);
        long version = qv.getVersion();
        Assert.assertTrue(version == 0x100000000L);
    }
}
Also used : ZooKeeper(org.apache.zookeeper.ZooKeeper) QuorumVerifier(org.apache.zookeeper.server.quorum.flexible.QuorumVerifier) Test(org.junit.Test)

Aggregations

QuorumVerifier (org.apache.zookeeper.server.quorum.flexible.QuorumVerifier)16 IOException (java.io.IOException)7 SetDataTxn (org.apache.zookeeper.txn.SetDataTxn)4 TxnHeader (org.apache.zookeeper.txn.TxnHeader)4 ByteBuffer (java.nio.ByteBuffer)3 Request (org.apache.zookeeper.server.Request)3 AtomicLong (java.util.concurrent.atomic.AtomicLong)2 Record (org.apache.jute.Record)2 KeeperException (org.apache.zookeeper.KeeperException)2 ZooKeeper (org.apache.zookeeper.ZooKeeper)2 QuorumServer (org.apache.zookeeper.server.quorum.QuorumPeer.QuorumServer)2 Test (org.junit.Test)2 StringReader (java.io.StringReader)1 BindException (java.net.BindException)1 ConnectException (java.net.ConnectException)1 InetSocketAddress (java.net.InetSocketAddress)1 SocketException (java.net.SocketException)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1