use of org.apache.log4j.WriterAppender in project zookeeper by apache.
the class QuorumPeerMainTest method testElectionFraud.
/**
* This test validates that if a quorum member determines that it is leader without the support of the rest of the
* quorum (the other members do not believe it to be the leader) it will stop attempting to lead and become a follower.
*
* @throws IOException
* @throws InterruptedException
*/
@Test
public void testElectionFraud() throws IOException, InterruptedException {
// capture QuorumPeer logging
ByteArrayOutputStream os = new ByteArrayOutputStream();
String loggingPattern = ((PatternLayout) Logger.getRootLogger().getAppender("CONSOLE").getLayout()).getConversionPattern();
WriterAppender appender = new WriterAppender(new PatternLayout(loggingPattern), os);
appender.setThreshold(Level.INFO);
Logger qlogger = Logger.getLogger(QuorumPeer.class);
qlogger.addAppender(appender);
numServers = 3;
// used for assertions later
boolean foundLeading = false;
boolean foundLooking = false;
boolean foundFollowing = false;
try {
// spin up a quorum, we use a small ticktime to make the test run faster
servers = LaunchServers(numServers, 500);
// find the leader
int trueLeader = servers.findLeader();
Assert.assertTrue("There should be a leader", trueLeader >= 0);
// find a follower
int falseLeader = (trueLeader + 1) % numServers;
Assert.assertTrue("All servers should join the quorum", servers.mt[falseLeader].main.quorumPeer.follower != null);
// to keep the quorum peer running and force it to go into the looking state, we kill leader election
// and close the connection to the leader
servers.mt[falseLeader].main.quorumPeer.electionAlg.shutdown();
servers.mt[falseLeader].main.quorumPeer.follower.getSocket().close();
// wait for the falseLeader to disconnect
waitForOne(servers.zk[falseLeader], States.CONNECTING);
// convince falseLeader that it is the leader
servers.mt[falseLeader].main.quorumPeer.setPeerState(QuorumPeer.ServerState.LEADING);
// provide time for the falseleader to realize no followers have connected
// (this is twice the timeout used in Leader#getEpochToPropose)
Thread.sleep(2 * servers.mt[falseLeader].main.quorumPeer.initLimit * servers.mt[falseLeader].main.quorumPeer.tickTime);
// Restart leader election
servers.mt[falseLeader].main.quorumPeer.startLeaderElection();
// The previous client connection to falseLeader likely closed, create a new one
servers.zk[falseLeader] = new ZooKeeper("127.0.0.1:" + servers.mt[falseLeader].getClientPort(), ClientBase.CONNECTION_TIMEOUT, this);
// Wait for falseLeader to rejoin the quorum
waitForOne(servers.zk[falseLeader], States.CONNECTED);
// and ensure trueLeader is still the leader
Assert.assertTrue(servers.mt[trueLeader].main.quorumPeer.leader != null);
// Look through the logs for output that indicates the falseLeader is LEADING, then LOOKING, then FOLLOWING
LineNumberReader r = new LineNumberReader(new StringReader(os.toString()));
Pattern leading = Pattern.compile(".*myid=" + falseLeader + ".*LEADING.*");
Pattern looking = Pattern.compile(".*myid=" + falseLeader + ".*LOOKING.*");
Pattern following = Pattern.compile(".*myid=" + falseLeader + ".*FOLLOWING.*");
String line;
while ((line = r.readLine()) != null) {
if (!foundLeading) {
foundLeading = leading.matcher(line).matches();
} else if (!foundLooking) {
foundLooking = looking.matcher(line).matches();
} else if (following.matcher(line).matches()) {
foundFollowing = true;
break;
}
}
} finally {
qlogger.removeAppender(appender);
}
Assert.assertTrue("falseLeader never attempts to become leader", foundLeading);
Assert.assertTrue("falseLeader never gives up on leadership", foundLooking);
Assert.assertTrue("falseLeader never rejoins the quorum", foundFollowing);
}
use of org.apache.log4j.WriterAppender in project ignite by apache.
the class GridNodeMetricsLogSelfTest method testNodeMetricsLog.
/**
* @throws Exception If failed.
*/
public void testNodeMetricsLog() throws Exception {
// Log to string, to check log content
Layout layout = new SimpleLayout();
StringWriter strWr = new StringWriter();
WriterAppender app = new WriterAppender(layout, strWr);
Logger.getRootLogger().addAppender(app);
Ignite g1 = startGrid(1);
IgniteCache<Integer, String> cache1 = g1.createCache("TestCache1");
cache1.put(1, "one");
Ignite g2 = startGrid(2);
IgniteCache<Integer, String> cache2 = g2.createCache("TestCache2");
cache2.put(2, "two");
Thread.sleep(10_000);
// Check that nodes are alie
assertEquals("one", cache1.get(1));
assertEquals("two", cache2.get(2));
String fullLog = strWr.toString();
Logger.getRootLogger().removeAppender(app);
String msg = "Metrics are missing in the log or have an unexpected format";
// don't check the format strictly, but check that all expected metrics are present
assertTrue(msg, fullLog.contains("Metrics for local node (to disable set 'metricsLogFrequency' to 0)"));
assertTrue(msg, fullLog.matches("(?s).*Node \\[id=.*, name=.*, uptime=.*].*"));
assertTrue(msg, fullLog.matches("(?s).*H/N/C \\[hosts=.*, nodes=.*, CPUs=.*].*"));
assertTrue(msg, fullLog.matches("(?s).*CPU \\[cur=.*, avg=.*, GC=.*].*"));
assertTrue(msg, fullLog.matches("(?s).*PageMemory \\[pages=.*].*"));
assertTrue(msg, fullLog.matches("(?s).*Heap \\[used=.*, free=.*, comm=.*].*"));
assertTrue(msg, fullLog.matches("(?s).*Non heap \\[used=.*, free=.*, comm=.*].*"));
assertTrue(msg, fullLog.matches("(?s).*Outbound messages queue \\[size=.*].*"));
assertTrue(msg, fullLog.matches("(?s).*Public thread pool \\[active=.*, idle=.*, qSize=.*].*"));
assertTrue(msg, fullLog.matches("(?s).*System thread pool \\[active=.*, idle=.*, qSize=.*].*"));
assertTrue(msg, fullLog.matches("(?s).*" + CUSTOM_EXECUTOR_0 + " \\[active=.*, idle=.*, qSize=.*].*"));
assertTrue(msg, fullLog.matches("(?s).*" + CUSTOM_EXECUTOR_1 + " \\[active=.*, idle=.*, qSize=.*].*"));
}
use of org.apache.log4j.WriterAppender in project wso2-axis2-transports by wso2.
the class LogManager method setTestCase.
public void setTestCase(ManagedTestCase testCase) throws IOException {
if (appender != null) {
Logger.getRootLogger().removeAppender(appender);
appender.close();
appender = null;
}
if (logs != null) {
for (OutputStream log : logs) {
IOUtils.closeQuietly(log);
}
logs = null;
}
if (testCase == null) {
testCaseDir = null;
} else {
File testSuiteDir = new File(logDir, testCase.getTestClass().getName());
testCaseDir = new File(testSuiteDir, testCase.getId());
logs = new LinkedList<OutputStream>();
sequence = 1;
appender = new WriterAppender(new TTCCLayout(), createLog("debug"));
Logger.getRootLogger().addAppender(appender);
}
}
Aggregations