Bug: Add proper support for jvm-dtests to query logs
Status: Open
Priority: Normal
Created: 30/Jul/20 02:07

Right now we log to disk, but don’t expose a direct way to access the logs, this method is also a bit brittle as all the instances write to the same location (and cleanup between tests is missing).  To aid in this, we should properly support reading log files for each instance.

Below is a POC of what it takes to read the logs today

{code}
diff --git a/test/conf/logback-dtest.xml b/test/conf/logback-dtest.xml
index 370e1e5bb2..472ebc23ba 100644
--- a/test/conf/logback-dtest.xml
+++ b/test/conf/logback-dtest.xml
@@ -25,9 +25,9 @@

   <appender name="INSTANCEFILE" class="ch.qos.logback.core.rolling.RollingFileAppender">

-    <file>./build/test/logs/${cassandra.testtag}/TEST-${suitename}.log</file>
+    <file>./build/test/logs/${cassandra.testtag}/TEST-${suitename}-${instance_id}.log</file>
     <rollingPolicy class="ch.qos.logback.core.rolling.FixedWindowRollingPolicy">
-      <fileNamePattern>./build/test/logs/${cassandra.testtag}/TEST-${suitename}.log.%i.gz</fileNamePattern>
+      <fileNamePattern>./build/test/logs/${cassandra.testtag}/TEST-${suitename}-${instance_id}.log.%i.gz</fileNamePattern>
       <minIndex>1</minIndex>
       <maxIndex>20</maxIndex>
     </rollingPolicy>
{code}

And test case

{code}
package org.apache.cassandra.distributed.test;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.nio.charset.StandardCharsets;
import java.util.Iterator;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;

import com.google.common.io.Closeables;
import org.junit.Test;

import org.apache.cassandra.distributed.Cluster;
import org.apache.cassandra.distributed.impl.InstanceIDDefiner;
import org.apache.cassandra.utils.AbstractIterator;

public class AllTheLogs extends TestBaseImpl
{
    @Test
    public void test() throws IOException
    {
        try (final Cluster cluster = init(Cluster.build(1).start()))
        {
            String tag = System.getProperty("cassandra.testtag", "cassandra.testtag_IS_UNDEFINED");
            String suite = System.getProperty("suitename", "suitename_IS_UNDEFINED");

            String id = cluster.get(1).callOnInstance(() -> new InstanceIDDefiner().getPropertyValue());
            String log = String.format("build/test/logs/%s/TEST-%s-%s.log", tag, suite, id);

            grep(log, "Enqueuing flush of tables").forEach(l -> System.out.println("I found the thing: " + l));
        }
    }

    private static Stream<String> grep(String file, String regex) throws IOException
    {
        return grep(file, Pattern.compile(regex));
    }

    private static Stream<String> grep(String file, Pattern regex) throws IOException
    {
        BufferedReader reader = new BufferedReader(new InputStreamReader(new FileInputStream(file), StandardCharsets.UTF_8));

        Iterator<String> it = new AbstractIterator<String>()
        {
            protected String computeNext()
            {
                try
                {
                    String s;
                    while ((s = reader.readLine()) != null)
                    {
                        Matcher m = regex.matcher(s);
                        if (m.find())
                            return s;
                    }
                    reader.close();
                    return endOfData();
                }
                catch (IOException e)
                {
                    Closeables.closeQuietly(reader);
                    throw new UncheckedIOException(e);
                }
            }
        };

        return StreamSupport.stream(Spliterators.spliteratorUnknownSize(it, Spliterator.ORDERED), false);
    }
}
{code}