use of org.apache.zookeeper.server.ZKDatabase in project zookeeper by apache.
the class SyncRequestProcessorMetricTest method setup.
@BeforeEach
public void setup() throws Exception {
ZKDatabase db = mock(ZKDatabase.class);
when(db.append(any(Request.class))).thenReturn(true);
doAnswer(invocation -> {
Thread.sleep(100);
return null;
}).when(db).commit();
zks = mock(ZooKeeperServer.class);
when(zks.getZKDatabase()).thenReturn(db);
nextProcessor = mock(RequestProcessor.class);
doAnswer(invocationOnMock -> {
allRequestsFlushed.countDown();
return null;
}).when(nextProcessor).processRequest(any(Request.class));
}
use of org.apache.zookeeper.server.ZKDatabase in project zookeeper by apache.
the class WatchLeakTest method testWatchesLeak.
/**
* Check that if session has expired then no watch can be set
*/
@ParameterizedTest
@ValueSource(booleans = { true, false })
public void testWatchesLeak(boolean sessionTimedout) throws Exception {
NIOServerCnxnFactory serverCnxnFactory = mock(NIOServerCnxnFactory.class);
final SelectionKey sk = new FakeSK();
MockSelectorThread selectorThread = mock(MockSelectorThread.class);
when(selectorThread.addInterestOpsUpdateRequest(any(SelectionKey.class))).thenAnswer(new Answer<Boolean>() {
@Override
public Boolean answer(InvocationOnMock invocation) throws Throwable {
SelectionKey sk = (SelectionKey) invocation.getArguments()[0];
NIOServerCnxn nioSrvCnx = (NIOServerCnxn) sk.attachment();
sk.interestOps(nioSrvCnx.getInterestOps());
return true;
}
});
ZKDatabase database = new ZKDatabase(null);
database.setlastProcessedZxid(2L);
QuorumPeer quorumPeer = mock(QuorumPeer.class);
FileTxnSnapLog logfactory = mock(FileTxnSnapLog.class);
// Directories are not used but we need it to avoid NPE
when(logfactory.getDataDir()).thenReturn(new File(""));
when(logfactory.getSnapDir()).thenReturn(new File(""));
FollowerZooKeeperServer fzks = null;
try {
// Create a new follower
fzks = new FollowerZooKeeperServer(logfactory, quorumPeer, database);
fzks.startup();
fzks.setServerCnxnFactory(serverCnxnFactory);
quorumPeer.follower = new MyFollower(quorumPeer, fzks);
LOG.info("Follower created");
// Simulate a socket channel between a client and a follower
final SocketChannel socketChannel = createClientSocketChannel();
// Create the NIOServerCnxn that will handle the client requests
final MockNIOServerCnxn nioCnxn = new MockNIOServerCnxn(fzks, socketChannel, sk, serverCnxnFactory, selectorThread);
sk.attach(nioCnxn);
// Send the connection request as a client do
nioCnxn.doIO(sk);
LOG.info("Client connection sent");
// Send the valid or invalid session packet to the follower
QuorumPacket qp = createValidateSessionPacketResponse(!sessionTimedout);
quorumPeer.follower.processPacket(qp);
LOG.info("Session validation sent");
// OK, now the follower knows that the session is valid or invalid, let's try
// to send the watches
nioCnxn.doIO(sk);
// wait for the the request processor to do his job
Thread.sleep(1000L);
LOG.info("Watches processed");
// If session has not been validated, there must be NO watches
int watchCount = database.getDataTree().getWatchCount();
if (sessionTimedout) {
// Session has not been re-validated !
LOG.info("session is not valid, watches = {}", watchCount);
assertEquals(0, watchCount, "Session is not valid so there should be no watches");
} else {
// Session has been re-validated
LOG.info("session is valid, watches = {}", watchCount);
assertEquals(1, watchCount, "Session is valid so the watch should be there");
}
} finally {
if (fzks != null) {
fzks.shutdown();
}
}
}
use of org.apache.zookeeper.server.ZKDatabase in project zookeeper by apache.
the class ZabUtils method prepareLeader.
private static 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;
}
use of org.apache.zookeeper.server.ZKDatabase in project zookeeper by apache.
the class FollowerResyncConcurrencyTest method verifyState.
private void verifyState(QuorumUtil qu, int index, Leader leader) {
LOG.info("Verifying state");
assertTrue(qu.getPeer(index).peer.follower != null, "Not following");
long epochF = (qu.getPeer(index).peer.getActiveServer().getZxid() >> 32L);
long epochL = (leader.getEpoch() >> 32L);
assertTrue(epochF == epochL, "Zxid: " + qu.getPeer(index).peer.getActiveServer().getZKDatabase().getDataTreeLastProcessedZxid() + "Current epoch: " + epochF);
int leaderIndex = (index == 1) ? 2 : 1;
Collection<Long> sessionsRestarted = qu.getPeer(index).peer.getActiveServer().getZKDatabase().getSessions();
Collection<Long> sessionsNotRestarted = qu.getPeer(leaderIndex).peer.getActiveServer().getZKDatabase().getSessions();
for (Long l : sessionsRestarted) {
assertTrue(sessionsNotRestarted.contains(l), "Should have same set of sessions in both servers, did not expect: " + l);
}
assertEquals(sessionsNotRestarted.size(), sessionsRestarted.size(), "Should have same number of sessions");
ZKDatabase restarted = qu.getPeer(index).peer.getActiveServer().getZKDatabase();
ZKDatabase clean = qu.getPeer(3).peer.getActiveServer().getZKDatabase();
ZKDatabase lead = qu.getPeer(leaderIndex).peer.getActiveServer().getZKDatabase();
for (Long l : sessionsRestarted) {
LOG.info("Validating ephemeral for session id 0x{}", Long.toHexString(l));
assertTrue(sessionsNotRestarted.contains(l), "Should have same set of sessions in both servers, did not expect: " + l);
Set<String> ephemerals = restarted.getEphemerals(l);
Set<String> cleanEphemerals = clean.getEphemerals(l);
for (String o : cleanEphemerals) {
if (!ephemerals.contains(o)) {
LOG.info("Restarted follower doesn't contain ephemeral {} zxid 0x{}", o, Long.toHexString(clean.getDataTree().getNode(o).stat.getMzxid()));
}
}
for (String o : ephemerals) {
if (!cleanEphemerals.contains(o)) {
LOG.info("Restarted follower has extra ephemeral {} zxid 0x{}", o, Long.toHexString(restarted.getDataTree().getNode(o).stat.getMzxid()));
}
}
Set<String> leadEphemerals = lead.getEphemerals(l);
for (String o : leadEphemerals) {
if (!cleanEphemerals.contains(o)) {
LOG.info("Follower doesn't contain ephemeral from leader {} zxid 0x{}", o, Long.toHexString(lead.getDataTree().getNode(o).stat.getMzxid()));
}
}
for (String o : cleanEphemerals) {
if (!leadEphemerals.contains(o)) {
LOG.info("Leader doesn't contain ephemeral from follower {} zxid 0x{}", o, Long.toHexString(clean.getDataTree().getNode(o).stat.getMzxid()));
}
}
assertEquals(ephemerals.size(), cleanEphemerals.size(), "Should have same number of ephemerals in both followers");
assertEquals(lead.getEphemerals(l).size(), cleanEphemerals.size(), "Leader should equal follower");
}
}
use of org.apache.zookeeper.server.ZKDatabase in project zookeeper by apache.
the class LocalSessionRequestTest method validateRequestLog.
/**
* 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()) {
assertFalse(p.request.sessionId == sessionId, "Should not see " + Request.op2String(p.request.type) + " request from local session 0x" + session + " on the " + peerType);
}
}
Aggregations