static void shutdownServerInstance(ServerCnxnFactory factory, String hostPort) { if (factory != null) { ZKDatabase zkDb; { ZooKeeperServer zs = getServer(factory); zkDb = zs.getZKDatabase(); } factory.shutdown(); try { zkDb.close(); } catch (IOException ie) { LOG.warn("Error closing logs ", ie); } final int PORT = getPort(hostPort); Assert.assertTrue("waiting for server down", ClientBaseWithFixes.waitForServerDown("127.0.0.1:" + PORT, CONNECTION_TIMEOUT)); } }
public QuorumPeer(Map<Long, QuorumServer> quorumPeers, File dataDir, File dataLogDir, int electionType, long myid, int tickTime, int initLimit, int syncLimit, boolean quorumListenOnAllIPs, ServerCnxnFactory cnxnFactory, QuorumVerifier quorumConfig) throws IOException { this(); this.cnxnFactory = cnxnFactory; this.quorumPeers = quorumPeers; this.electionType = electionType; this.myid = myid; this.tickTime = tickTime; this.initLimit = initLimit; this.syncLimit = syncLimit; this.quorumListenOnAllIPs = quorumListenOnAllIPs; this.logFactory = new FileTxnSnapLog(dataLogDir, dataDir); this.zkDb = new ZKDatabase(this.logFactory); if(quorumConfig == null) this.quorumConfig = new QuorumMaj(countParticipants(quorumPeers)); else this.quorumConfig = quorumConfig; }
static void shutdownServerInstance(ServerCnxnFactory factory, String hostPort) { if (factory != null) { ZKDatabase zkDb = null; { ZooKeeperServer zs = getServer(factory); if (zs != null) { zkDb = zs.getZKDatabase(); } } factory.shutdown(); try { if (zkDb != null) { zkDb.close(); } } catch (IOException ie) { LOG.warn("Error closing logs ", ie); } final int PORT = getPort(hostPort); Assert.assertTrue("waiting for server down", ClientBase.waitForServerDown("127.0.0.1:" + PORT, CONNECTION_TIMEOUT)); } }
/** * Walk through the target peer commmittedLog. * @param sessionId * @param peerId */ private void validateRequestLog(long sessionId, int peerId) { String session = Long.toHexString(sessionId); LOG.info("Searching for txn of session 0x " + session + " on peer " + peerId); String peerType = peerId == qb.getLeaderIndex() ? "leader" : "follower"; QuorumPeer peer = qb.getPeerList().get(peerId); ZKDatabase db = peer.getActiveServer().getZKDatabase(); for (Proposal p : db.getCommittedLog()) { Assert.assertFalse("Should not see " + TraceFormatter.op2String(p.request.type) + " request from local session 0x" + session + " on the " + peerType, p.request.sessionId == sessionId); } }
/** * Wait for all server to have the same lastProccessedZxid. Timeout in seconds */ private boolean waitForSync(QuorumUtil qu, int index, int timeout) throws InterruptedException{ LOG.info("Wait for server to sync"); int leaderIndex = (index == 1) ? 2 : 1; ZKDatabase restartedDb = qu.getPeer(index).peer.getActiveServer().getZKDatabase(); ZKDatabase cleanDb = qu.getPeer(3).peer.getActiveServer().getZKDatabase(); ZKDatabase leadDb = qu.getPeer(leaderIndex).peer.getActiveServer().getZKDatabase(); long leadZxid = 0; long cleanZxid = 0; long restartedZxid = 0; for (int i = 0; i < timeout; ++i) { leadZxid = leadDb.getDataTreeLastProcessedZxid(); cleanZxid = cleanDb.getDataTreeLastProcessedZxid(); restartedZxid = restartedDb.getDataTreeLastProcessedZxid(); if (leadZxid == cleanZxid && leadZxid == restartedZxid) { return true; } Thread.sleep(1000); } LOG.info("Timeout waiting for zxid to sync: leader 0x{}" + "clean 0x{}" + "restarted 0x{}", Long.toHexString(leadZxid), Long.toHexString(cleanZxid), Long.toHexString(restartedZxid)); return false; }
@After public void tearDown() throws IOException, InterruptedException { if (zks != null) { ZKDatabase zkDb = zks.getZKDatabase(); factory.shutdown(); try { zkDb.close(); } catch (IOException ie) { } final int PORT = Integer.parseInt(hostPort.split(":")[1]); Assert.assertTrue("waiting for server down", waitForServerDown("127.0.0.1:" + PORT, CONNECTION_TIMEOUT)); } }
public QuorumPeer(Map<Long, QuorumServer> quorumPeers, File dataDir, File dataLogDir, int electionType, long myid, int tickTime, int initLimit, int syncLimit, ServerCnxnFactory cnxnFactory, QuorumVerifier quorumConfig) throws IOException { this(); this.cnxnFactory = cnxnFactory; this.quorumPeers = quorumPeers; this.electionType = electionType; this.myid = myid; this.tickTime = tickTime; this.initLimit = initLimit; this.syncLimit = syncLimit; this.logFactory = new FileTxnSnapLog(dataLogDir, dataDir); this.zkDb = new ZKDatabase(this.logFactory); if(quorumConfig == null) this.quorumConfig = new QuorumMaj(countParticipants(quorumPeers)); else this.quorumConfig = quorumConfig; }
static void shutdownServerInstance(ServerCnxnFactory factory, String hostPort) { if (factory != null) { ZKDatabase zkDb; { ZooKeeperServer zs = getServer(factory); zkDb = zs.getZKDatabase(); } factory.shutdown(); try { zkDb.close(); } catch (IOException ie) { LOG.warn("Error closing logs ", ie); } final int PORT = getPort(hostPort); Assert.assertTrue("waiting for server down", ClientBase.waitForServerDown("127.0.0.1:" + PORT, CONNECTION_TIMEOUT)); } }
/** * Wait for all server to have the same lastProccessedZxid. Timeout in seconds */ private boolean waitForSync(QuorumUtil qu, int index, int timeout) throws InterruptedException{ LOG.info("Wait for server to sync"); int leaderIndex = (index == 1) ? 2 : 1; ZKDatabase restartedDb = qu.getPeer(index).peer.getActiveServer().getZKDatabase(); ZKDatabase cleanDb = qu.getPeer(3).peer.getActiveServer().getZKDatabase(); ZKDatabase leadDb = qu.getPeer(leaderIndex).peer.getActiveServer().getZKDatabase(); long leadZxid = 0; long cleanZxid = 0; long restartedZxid = 0; for (int i = 0; i < timeout; ++i) { leadZxid = leadDb.getDataTreeLastProcessedZxid(); cleanZxid = cleanDb.getDataTreeLastProcessedZxid(); restartedZxid = restartedDb.getDataTreeLastProcessedZxid(); if (leadZxid == cleanZxid && leadZxid == restartedZxid) { return true; } Thread.sleep(1000); } LOG.info("Timeout waiting for zxid to sync: leader 0x" + Long.toHexString(leadZxid)+ "clean 0x" + Long.toHexString(cleanZxid) + "restarted 0x" + Long.toHexString(restartedZxid)); return false; }
/** * @param port * @param dataDir * @throws IOException */ FollowerZooKeeperServer(FileTxnSnapLog logFactory,QuorumPeer self, DataTreeBuilder treeBuilder, ZKDatabase zkDb) throws IOException { super(logFactory, self.tickTime, self.minSessionTimeout, self.maxSessionTimeout, treeBuilder, zkDb, self); this.pendingSyncs = new ConcurrentLinkedQueue<Request>(); }
public LearnerZooKeeperServer(FileTxnSnapLog logFactory, int tickTime, int minSessionTimeout, int maxSessionTimeout, DataTreeBuilder treeBuilder, ZKDatabase zkDb, QuorumPeer self) throws IOException { super(logFactory, tickTime, minSessionTimeout, maxSessionTimeout, treeBuilder, zkDb, self); }
protected QuorumZooKeeperServer(FileTxnSnapLog logFactory, int tickTime, int minSessionTimeout, int maxSessionTimeout, DataTreeBuilder treeBuilder, ZKDatabase zkDb, QuorumPeer self) { super(logFactory, tickTime, minSessionTimeout, maxSessionTimeout, treeBuilder, zkDb); this.self = self; }
private void deserializeSnapshot(InputArchive ia) throws IOException { ZKDatabase zkdb = new ZKDatabase(null); zkdb.deserializeSnapshot(ia); String signature = ia.readString("signature"); assertEquals("BenWasHere", signature); }
private LeaderZooKeeperServer prepareLeader(File tmpDir, QuorumPeer peer) throws IOException, NoSuchFieldException, IllegalAccessException { FileTxnSnapLog logFactory = new FileTxnSnapLog(tmpDir, tmpDir); peer.setTxnFactory(logFactory); Field addrField = peer.getClass().getDeclaredField("myQuorumAddr"); addrField.setAccessible(true); addrField.set(peer, new InetSocketAddress(PortAssignment.unique())); ZKDatabase zkDb = new ZKDatabase(logFactory); LeaderZooKeeperServer zk = new LeaderZooKeeperServer(logFactory, peer, new ZooKeeperServer.BasicDataTreeBuilder(), zkDb); return zk; }
private ConversableFollower createFollower(File tmpDir, QuorumPeer peer) throws IOException { FileTxnSnapLog logFactory = new FileTxnSnapLog(tmpDir, tmpDir); peer.setTxnFactory(logFactory); ZKDatabase zkDb = new ZKDatabase(logFactory); FollowerZooKeeperServer zk = new FollowerZooKeeperServer(logFactory, peer, new ZooKeeperServer.BasicDataTreeBuilder(), zkDb); peer.setZKDatabase(zkDb); return new ConversableFollower(peer, zk); }
private ConversableObserver createObserver(File tmpDir, QuorumPeer peer) throws IOException { FileTxnSnapLog logFactory = new FileTxnSnapLog(tmpDir, tmpDir); peer.setTxnFactory(logFactory); DataTreeBuilder treeBuilder = new ZooKeeperServer.BasicDataTreeBuilder(); ZKDatabase zkDb = new ZKDatabase(logFactory); ObserverZooKeeperServer zk = new ObserverZooKeeperServer(logFactory, peer, treeBuilder, zkDb); peer.setZKDatabase(zkDb); return new ConversableObserver(peer, zk); }
@Test public void testInitialAcceptedCurrent() throws Exception { File tmpDir = File.createTempFile("test", ".dir", testData); tmpDir.delete(); tmpDir.mkdir(); try { FileTxnSnapLog logFactory = new FileTxnSnapLog(tmpDir, tmpDir); File version2 = new File(tmpDir, "version-2"); version2.mkdir(); long zxid = ZxidUtils.makeZxid(3, 3); TxnHeader hdr = new TxnHeader(1, 1, zxid, 1, ZooDefs.OpCode.error); ErrorTxn txn = new ErrorTxn(1); byte[] buf = Util.marshallTxnEntry(hdr, txn); Request req = new Request(null, 1, 1, ZooDefs.OpCode.error, ByteBuffer.wrap(buf), null); req.hdr = hdr; req.txn = txn; logFactory.append(req); logFactory.commit(); ZKDatabase zkDb = new ZKDatabase(logFactory); QuorumPeer peer = new QuorumPeer(); peer.setZKDatabase(zkDb); peer.setTxnFactory(logFactory); peer.getLastLoggedZxid(); Assert.assertEquals(3, peer.getAcceptedEpoch()); Assert.assertEquals(3, peer.getCurrentEpoch()); Assert.assertEquals(3, Integer .parseInt(readContentsOfFile(new File(version2, QuorumPeer.CURRENT_EPOCH_FILENAME)))); Assert.assertEquals(3, Integer .parseInt(readContentsOfFile(new File(version2, QuorumPeer.ACCEPTED_EPOCH_FILENAME)))); } finally { recursiveDelete(tmpDir); } }
@Test public void testTruncationStreamReset() throws Exception { File tmpdir = ClientBase.createTmpDir(); FileTxnSnapLog snaplog = new FileTxnSnapLog(tmpdir, tmpdir); ZKDatabase zkdb = new ZKDatabase(snaplog); for (int i = 1; i <= 100; i++) { append(zkdb, i); } zkdb.truncateLog(1); append(zkdb, 200); zkdb.close(); // verify that the truncation and subsequent append were processed // correctly FileTxnLog txnlog = new FileTxnLog(new File(tmpdir, "version-2")); TxnIterator iter = txnlog.read(1); TxnHeader hdr = iter.getHeader(); Record txn = iter.getTxn(); Assert.assertEquals(1, hdr.getZxid()); Assert.assertTrue(txn instanceof SetDataTxn); iter.next(); hdr = iter.getHeader(); txn = iter.getTxn(); Assert.assertEquals(200, hdr.getZxid()); Assert.assertTrue(txn instanceof SetDataTxn); iter.close(); ClientBase.recursiveDelete(tmpdir); }
private void append(ZKDatabase zkdb, int i) throws IOException { TxnHeader hdr = new TxnHeader(1, 1, i, 1, ZooDefs.OpCode.setData); Record txn = new SetDataTxn("/foo" + i, new byte[0], 1); Request req = new Request(null, 0, 0, 0, null, null); req.hdr = hdr; req.txn = txn; zkdb.append(req); zkdb.commit(); }
@Test public void testTruncationNullLog() throws Exception { File tmpdir = ClientBase.createTmpDir(); FileTxnSnapLog snaplog = new FileTxnSnapLog(tmpdir, tmpdir); ZKDatabase zkdb = new ZKDatabase(snaplog); for (int i = 1; i <= 100; i++) { append(zkdb, i); } zkdb.close(); File[] logs = snaplog.getDataDir().listFiles(); for(int i = 0; i < logs.length; i++) { LOG.debug("Deleting: {}", logs[i].getName()); Assert.assertTrue("Failed to delete log file: " + logs[i].getName(), logs[i].delete()); } try { zkdb.truncateLog(1); Assert.assertTrue("Should not get here", false); } catch(IOException e) { Assert.assertTrue("Should have received an IOException", true); } catch(NullPointerException npe) { Assert.fail("This should not throw NPE!"); } ClientBase.recursiveDelete(tmpdir); }
/** * @param port * @param dataDir * @throws IOException */ FollowerZooKeeperServer(FileTxnSnapLog logFactory,QuorumPeer self, ZKDatabase zkDb) throws IOException { super(logFactory, self.tickTime, self.minSessionTimeout, self.maxSessionTimeout, zkDb, self); this.pendingSyncs = new ConcurrentLinkedQueue<Request>(); }
public LearnerZooKeeperServer(FileTxnSnapLog logFactory, int tickTime, int minSessionTimeout, int maxSessionTimeout, ZKDatabase zkDb, QuorumPeer self) throws IOException { super(logFactory, tickTime, minSessionTimeout, maxSessionTimeout, zkDb, self); }
protected QuorumZooKeeperServer(FileTxnSnapLog logFactory, int tickTime, int minSessionTimeout, int maxSessionTimeout, ZKDatabase zkDb, QuorumPeer self) { super(logFactory, tickTime, minSessionTimeout, maxSessionTimeout, zkDb); this.self = self; }
private byte[] getKey(ZooKeeperServer zks) { ZKDatabase db = zks.getZKDatabase(); if (db != null) { try { Stat stat = new Stat(); return db.getData("/key", stat, null); } catch (NoNodeException e) { LOG.error("getData failed", e); } } return null; }
private LeaderZooKeeperServer prepareLeader(File tmpDir, QuorumPeer peer) throws IOException, NoSuchFieldException, IllegalAccessException { FileTxnSnapLog logFactory = new FileTxnSnapLog(tmpDir, tmpDir); peer.setTxnFactory(logFactory); ZKDatabase zkDb = new ZKDatabase(logFactory); LeaderZooKeeperServer zk = new LeaderZooKeeperServer(logFactory, peer, zkDb); return zk; }
private ConversableFollower createFollower(File tmpDir, QuorumPeer peer) throws IOException { FileTxnSnapLog logFactory = new FileTxnSnapLog(tmpDir, tmpDir); peer.setTxnFactory(logFactory); ZKDatabase zkDb = new ZKDatabase(logFactory); FollowerZooKeeperServer zk = new FollowerZooKeeperServer(logFactory, peer, zkDb); peer.setZKDatabase(zkDb); return new ConversableFollower(peer, zk); }