Search in sources :

Example 1 with StringDecorator

use of org.apache.storm.st.utils.StringDecorator in project storm by apache.

the class WindowVerifier method runAndVerifyTime.

/**
 * Run the topology and verify that the number and contents of time based windows is as expected
 * once the spout and bolt have emitted sufficient tuples.
 * The spout and bolt are required to log exactly one log line per emit/window using {@link StringDecorator}
 */
public void runAndVerifyTime(int windowSec, int slideSec, TestableTopology testable, TopoWrap topo) throws IOException, TException, java.net.MalformedURLException {
    topo.submitSuccessfully();
    final int minSpoutEmits = 100;
    final int minBoltEmits = 5;
    String boltName = testable.getBoltName();
    String spoutName = testable.getSpoutName();
    // Waiting for spout tuples isn't strictly necessary since we also wait for bolt emits, but do it anyway
    // Allow two minutes for topology startup, then wait for at most the time it should take to produce 10 windows
    topo.assertProgress(minSpoutEmits, testable.getSpoutExecutors(), spoutName, 180 + 10 * slideSec);
    topo.assertProgress(minBoltEmits, testable.getBoltExecutors(), boltName, 180 + 10 * slideSec);
    final List<TimeData> allSpoutLogLines = topo.getDeserializedDecoratedLogLines(spoutName, TimeData::fromJson);
    final List<TimeDataWindow> allBoltLogLines = topo.getDeserializedDecoratedLogLines(boltName, TimeDataWindow::fromJson);
    Assert.assertTrue(allBoltLogLines.size() >= minBoltEmits, "Expecting min " + minBoltEmits + " bolt emits, found: " + allBoltLogLines.size() + " \n\t" + allBoltLogLines);
    final DateTime firstWindowEndTime = TimeUtil.ceil(new DateTime(allSpoutLogLines.get(0).getDate()).withZone(DateTimeZone.UTC), slideSec);
    final int numberOfWindows = allBoltLogLines.size();
    /*
         * Windows should be aligned to the slide size, starting at firstWindowEndTime - windowSec.
         * Because all windows are aligned to the slide size, we can partition the spout emitted timestamps by which window they should fall in.
         * This checks that the partitioned spout emits fall in the expected windows, based on the logs from the spout and bolt.
         */
    for (int i = 0; i < numberOfWindows; ++i) {
        final DateTime windowEnd = firstWindowEndTime.plusSeconds(i * slideSec);
        final DateTime windowStart = windowEnd.minusSeconds(windowSec);
        LOG.info("Comparing window: " + windowStart + " to " + windowEnd + " iter " + (i + 1) + "/" + numberOfWindows);
        final List<TimeData> expectedSpoutEmitsInWindow = allSpoutLogLines.stream().filter(spoutLog -> {
            DateTime spoutLogTime = new DateTime(spoutLog.getDate());
            // The window boundaries are )windowStart, windowEnd)
            return spoutLogTime.isAfter(windowStart) && spoutLogTime.isBefore(windowEnd.plusMillis(1));
        }).collect(Collectors.toList());
        TimeDataWindow expectedWindow = new TimeDataWindow(expectedSpoutEmitsInWindow);
        final TimeDataWindow actualWindow = allBoltLogLines.get(i);
        LOG.info("Actual window: " + actualWindow.getDescription());
        LOG.info("Expected window: " + expectedWindow.getDescription());
        for (TimeData oneLog : expectedWindow.getTimeData()) {
            Assertions.assertTrue(actualWindow.getTimeData().contains(oneLog), () -> String.format("Missing: '%s' \n\tActual: '%s' \n\tComputed window: '%s'", oneLog, actualWindow, expectedWindow));
        }
        for (TimeData oneLog : actualWindow.getTimeData()) {
            Assertions.assertTrue(expectedWindow.getTimeData().contains(oneLog), () -> String.format("Extra: '%s' \n\tActual: '%s' \n\tComputed window: '%s'", oneLog, actualWindow, expectedWindow));
        }
    }
}
Also used : TimeDataWindow(org.apache.storm.st.topology.window.data.TimeDataWindow) DateTimeZone(org.joda.time.DateTimeZone) Logger(org.slf4j.Logger) MalformedURLException(java.net.MalformedURLException) StringDecorator(org.apache.storm.st.utils.StringDecorator) DecoratedLogLine(org.apache.storm.st.wrapper.DecoratedLogLine) DateTime(org.joda.time.DateTime) LoggerFactory(org.slf4j.LoggerFactory) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) TException(org.apache.storm.thrift.TException) TimeData(org.apache.storm.st.topology.window.data.TimeData) List(java.util.List) TopoWrap(org.apache.storm.st.wrapper.TopoWrap) Assert(org.testng.Assert) Assertions(org.junit.jupiter.api.Assertions) TimeUtil(org.apache.storm.st.utils.TimeUtil) TimeDataWindow(org.apache.storm.st.topology.window.data.TimeDataWindow) TestableTopology(org.apache.storm.st.topology.TestableTopology) TimeData(org.apache.storm.st.topology.window.data.TimeData) DateTime(org.joda.time.DateTime)

Example 2 with StringDecorator

use of org.apache.storm.st.utils.StringDecorator in project storm by apache.

the class TopoWrap method getDecoratedLogLines.

/**
 * Get the log lines that contain the unique {@link StringDecorator} string for the given component.
 * Test spouts and bolts can write logs containing the StringDecorator string, which can be fetched using this method.
 */
public List<DecoratedLogLine> getDecoratedLogLines(final String componentId) throws IOException, TException, MalformedURLException {
    final String logs = getLogs(componentId);
    final String dateRegex = "\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}\\.\\d{3}";
    Pattern pattern = Pattern.compile("(?=\\n" + dateRegex + ")");
    final String[] logLines = pattern.split(logs);
    List<DecoratedLogLine> sortedLogs = Arrays.asList(logLines).stream().filter(log -> log != null && StringDecorator.isDecorated(componentId, log)).map(DecoratedLogLine::new).sorted().collect(Collectors.toList());
    LOG.info("Found " + sortedLogs.size() + " items for component: " + componentId);
    return sortedLogs;
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) Arrays(java.util.Arrays) StringUtils(org.apache.commons.lang.StringUtils) URL(java.net.URL) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) NumberFormat(java.text.NumberFormat) ArrayList(java.util.ArrayList) AlreadyAliveException(org.apache.storm.generated.AlreadyAliveException) HashSet(java.util.HashSet) AssertUtil(org.apache.storm.st.utils.AssertUtil) Assert(org.testng.Assert) StormTopology(org.apache.storm.generated.StormTopology) Locale(java.util.Locale) Map(java.util.Map) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) TimeUtil(org.apache.storm.st.utils.TimeUtil) StormSubmitter(org.apache.storm.StormSubmitter) Logger(org.slf4j.Logger) ExecutorStats(org.apache.storm.generated.ExecutorStats) ImmutableMap(com.google.common.collect.ImmutableMap) MalformedURLException(java.net.MalformedURLException) ComponentPageInfo(org.apache.storm.generated.ComponentPageInfo) StringDecorator(org.apache.storm.st.utils.StringDecorator) Collection(java.util.Collection) ExceptionUtils(org.apache.commons.lang.exception.ExceptionUtils) Set(java.util.Set) ExecutorAggregateStats(org.apache.storm.generated.ExecutorAggregateStats) IOException(java.io.IOException) FileUtils(org.apache.commons.io.FileUtils) TopologyInfo(org.apache.storm.generated.TopologyInfo) FromJson(org.apache.storm.st.topology.window.data.FromJson) Utils(org.apache.storm.utils.Utils) Collectors(java.util.stream.Collectors) TException(org.apache.storm.thrift.TException) File(java.io.File) StandardCharsets(java.nio.charset.StandardCharsets) ExecutorSummary(org.apache.storm.generated.ExecutorSummary) IOUtils(org.apache.commons.io.IOUtils) AuthorizationException(org.apache.storm.generated.AuthorizationException) URLEncoder(java.net.URLEncoder) List(java.util.List) Config(org.apache.storm.Config) Pattern(java.util.regex.Pattern) TopologySummary(org.apache.storm.generated.TopologySummary) InvalidTopologyException(org.apache.storm.generated.InvalidTopologyException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Pattern(java.util.regex.Pattern)

Aggregations

IOException (java.io.IOException)2 MalformedURLException (java.net.MalformedURLException)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 StringDecorator (org.apache.storm.st.utils.StringDecorator)2 TimeUtil (org.apache.storm.st.utils.TimeUtil)2 TException (org.apache.storm.thrift.TException)2 Logger (org.slf4j.Logger)2 LoggerFactory (org.slf4j.LoggerFactory)2 Assert (org.testng.Assert)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 File (java.io.File)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 URL (java.net.URL)1 URLEncoder (java.net.URLEncoder)1 StandardCharsets (java.nio.charset.StandardCharsets)1 NumberFormat (java.text.NumberFormat)1 ArrayList (java.util.ArrayList)1 Arrays (java.util.Arrays)1 Collection (java.util.Collection)1