Search in sources :

Example 6 with ReadableInstant

use of org.joda.time.ReadableInstant in project beam by apache.

the class WindowFnTestUtils method validateGetOutputTimestamps.

/**
   * Verifies that later-ending merged windows from any of the timestamps hold up output of
   * earlier-ending windows, using the provided {@link WindowFn} and {@link TimestampCombiner}.
   *
   * <p>Given a list of lists of timestamps, where each list is expected to merge into a single
   * window with end times in ascending order, assigns and merges windows for each list (as though
   * each were a separate key/user session). Then combines each timestamp in the list according to
   * the provided {@link TimestampCombiner}.
   *
   * <p>Verifies that a overlapping windows do not hold each other up via the watermark.
   */
public static <T, W extends IntervalWindow> void validateGetOutputTimestamps(WindowFn<T, W> windowFn, TimestampCombiner timestampCombiner, List<List<Long>> timestampsPerWindow) throws Exception {
    // Assign windows to each timestamp, then merge them, storing the merged windows in
    // a list in corresponding order to timestampsPerWindow
    final List<W> windows = new ArrayList<>();
    for (List<Long> timestampsForWindow : timestampsPerWindow) {
        final Set<W> windowsToMerge = new HashSet<>();
        for (long timestamp : timestampsForWindow) {
            windowsToMerge.addAll(WindowFnTestUtils.<T, W>assignedWindows(windowFn, timestamp));
        }
        windowFn.mergeWindows(windowFn.new MergeContext() {

            @Override
            public Collection<W> windows() {
                return windowsToMerge;
            }

            @Override
            public void merge(Collection<W> toBeMerged, W mergeResult) throws Exception {
                windows.add(mergeResult);
            }
        });
    }
    // Map every list of input timestamps to an output timestamp
    final List<Instant> combinedOutputTimestamps = new ArrayList<>();
    for (int i = 0; i < timestampsPerWindow.size(); ++i) {
        List<Long> timestampsForWindow = timestampsPerWindow.get(i);
        W window = windows.get(i);
        List<Instant> outputInstants = new ArrayList<>();
        for (long inputTimestamp : timestampsForWindow) {
            outputInstants.add(assignOutputTime(timestampCombiner, new Instant(inputTimestamp), window));
        }
        combinedOutputTimestamps.add(combineOutputTimes(timestampCombiner, outputInstants));
    }
    // Consider windows in increasing order of max timestamp; ensure the output timestamp is after
    // the max timestamp of the previous
    @Nullable W earlierEndingWindow = null;
    for (int i = 0; i < windows.size(); ++i) {
        W window = windows.get(i);
        ReadableInstant outputTimestamp = combinedOutputTimestamps.get(i);
        if (earlierEndingWindow != null) {
            assertThat(outputTimestamp, greaterThan((ReadableInstant) earlierEndingWindow.maxTimestamp()));
        }
        earlierEndingWindow = window;
    }
}
Also used : ReadableInstant(org.joda.time.ReadableInstant) ReadableInstant(org.joda.time.ReadableInstant) Instant(org.joda.time.Instant) ArrayList(java.util.ArrayList) Collection(java.util.Collection) Nullable(javax.annotation.Nullable) HashSet(java.util.HashSet)

Example 7 with ReadableInstant

use of org.joda.time.ReadableInstant in project beam by apache.

the class LateDataUtilsTest method garbageCollectionTimeAfterEndOfGlobalWindowWithLateness.

@Test
public void garbageCollectionTimeAfterEndOfGlobalWindowWithLateness() {
    FixedWindows windowFn = FixedWindows.of(Duration.standardMinutes(5));
    Duration allowedLateness = Duration.millis(Long.MAX_VALUE);
    WindowingStrategy<?, ?> strategy = WindowingStrategy.globalDefault().withWindowFn(windowFn).withAllowedLateness(allowedLateness);
    IntervalWindow window = windowFn.assignWindow(new Instant(-100));
    assertThat(window.maxTimestamp().plus(allowedLateness), Matchers.<ReadableInstant>greaterThan(GlobalWindow.INSTANCE.maxTimestamp()));
    assertThat(LateDataUtils.garbageCollectionTime(window, strategy), equalTo(GlobalWindow.INSTANCE.maxTimestamp()));
}
Also used : FixedWindows(org.apache.beam.sdk.transforms.windowing.FixedWindows) ReadableInstant(org.joda.time.ReadableInstant) Instant(org.joda.time.Instant) Duration(org.joda.time.Duration) IntervalWindow(org.apache.beam.sdk.transforms.windowing.IntervalWindow) Test(org.junit.Test)

Aggregations

ReadableInstant (org.joda.time.ReadableInstant)7 Instant (org.joda.time.Instant)5 FixedWindows (org.apache.beam.sdk.transforms.windowing.FixedWindows)2 IntervalWindow (org.apache.beam.sdk.transforms.windowing.IntervalWindow)2 Test (org.junit.Test)2 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 Nullable (javax.annotation.Nullable)1 BytesReference (org.elasticsearch.common.bytes.BytesReference)1 Writer (org.elasticsearch.common.io.stream.Writeable.Writer)1 Matchers.containsString (org.hamcrest.Matchers.containsString)1 Chronology (org.joda.time.Chronology)1 DateTime (org.joda.time.DateTime)1 DateTimeZone (org.joda.time.DateTimeZone)1