Search in sources :

Example 16 with WriterAppender

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);
}
Also used : Pattern(java.util.regex.Pattern) ZooKeeper(org.apache.zookeeper.ZooKeeper) PatternLayout(org.apache.log4j.PatternLayout) StringReader(java.io.StringReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) WriterAppender(org.apache.log4j.WriterAppender) Logger(org.apache.log4j.Logger) LineNumberReader(java.io.LineNumberReader) Test(org.junit.Test)

Example 17 with WriterAppender

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=.*].*"));
}
Also used : SimpleLayout(org.apache.log4j.SimpleLayout) StringWriter(java.io.StringWriter) SimpleLayout(org.apache.log4j.SimpleLayout) Layout(org.apache.log4j.Layout) Ignite(org.apache.ignite.Ignite) WriterAppender(org.apache.log4j.WriterAppender)

Example 18 with WriterAppender

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);
    }
}
Also used : OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) TTCCLayout(org.apache.log4j.TTCCLayout) WriterAppender(org.apache.log4j.WriterAppender) File(java.io.File)

Aggregations

WriterAppender (org.apache.log4j.WriterAppender)18 Test (org.junit.Test)11 Logger (org.apache.log4j.Logger)9 ByteArrayOutputStream (java.io.ByteArrayOutputStream)8 Layout (org.apache.log4j.Layout)8 PatternLayout (org.apache.log4j.PatternLayout)8 LineNumberReader (java.io.LineNumberReader)6 StringReader (java.io.StringReader)6 StringWriter (java.io.StringWriter)6 Appender (org.apache.log4j.Appender)6 Pattern (java.util.regex.Pattern)5 SimpleLayout (org.apache.log4j.SimpleLayout)5 Ignite (org.apache.ignite.Ignite)2 ZooKeeper (org.apache.zookeeper.ZooKeeper)2 AppenderProvider (org.ow2.proactive.scheduler.common.util.logforwarder.AppenderProvider)2 JobIdImpl (org.ow2.proactive.scheduler.job.JobIdImpl)2 File (java.io.File)1 FileOutputStream (java.io.FileOutputStream)1 OutputStream (java.io.OutputStream)1 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)1