Search in sources :

Example 61 with OffsetRange

use of org.apache.beam.sdk.io.range.OffsetRange in project beam by apache.

the class OffsetRangeTrackerTest method testCheckDoneAfterTryClaimRightBeforeEndOfRange.

@Test
public void testCheckDoneAfterTryClaimRightBeforeEndOfRange() {
    OffsetRangeTracker tracker = new OffsetRangeTracker(new OffsetRange(100, 200));
    assertTrue(tracker.tryClaim(150L));
    assertTrue(tracker.tryClaim(175L));
    assertTrue(tracker.tryClaim(199L));
    tracker.checkDone();
}
Also used : OffsetRange(org.apache.beam.sdk.io.range.OffsetRange) Test(org.junit.Test)

Example 62 with OffsetRange

use of org.apache.beam.sdk.io.range.OffsetRange in project beam by apache.

the class OffsetRangeTrackerTest method testSmallRangeWithLargeValue.

@Test
public void testSmallRangeWithLargeValue() throws Exception {
    OffsetRangeTracker tracker = new OffsetRangeTracker(new OffsetRange(123456789012345677L, 123456789012345679L));
    assertTrue(tracker.tryClaim(123456789012345677L));
    SplitResult res = tracker.trySplit(0.5);
    assertEquals(new OffsetRange(123456789012345677L, 123456789012345678L), res.getPrimary());
    assertEquals(new OffsetRange(123456789012345678L, 123456789012345679L), res.getResidual());
    tracker = new OffsetRangeTracker(new OffsetRange(123456789012345681L, 123456789012345683L));
    assertTrue(tracker.tryClaim(123456789012345681L));
    res = tracker.trySplit(0.5);
    assertEquals(new OffsetRange(123456789012345681L, 123456789012345682L), res.getPrimary());
    assertEquals(new OffsetRange(123456789012345682L, 123456789012345683L), res.getResidual());
}
Also used : OffsetRange(org.apache.beam.sdk.io.range.OffsetRange) Test(org.junit.Test)

Example 63 with OffsetRange

use of org.apache.beam.sdk.io.range.OffsetRange in project beam by apache.

the class PCollectionViewsTest method assertNonEmptyRangesAndPositions.

private static void assertNonEmptyRangesAndPositions(Iterable<OffsetRange> ranges, Map<OffsetRange, Integer> nonOverlappingRangesToNumElementsPerPosition) {
    for (Map.Entry<OffsetRange, Integer> entry : nonOverlappingRangesToNumElementsPerPosition.entrySet()) {
        assertNotEquals(0, (int) entry.getValue());
    }
    ListMultimap<Long, Integer> positions = ArrayListMultimap.create();
    for (OffsetRange range : ranges) {
        for (long i = range.getFrom(); i < range.getTo(); ++i) {
            positions.put(i, 0);
        }
    }
    int position = 0;
    for (Long key : new TreeSet<>(positions.keySet())) {
        int size = positions.get(key).size();
        positions.replaceValues(key, Lists.newArrayList(IntStream.range(position, position + size).iterator()));
        position += size;
    }
    for (int i = 0; i < position; ++i) {
        KV<Long, Integer> computedPosition = computePositionForIndex(nonOverlappingRangesToNumElementsPerPosition, i);
        assertEquals(i, (int) positions.get(computedPosition.getKey()).get(computedPosition.getValue()));
    }
    assertThrows(IndexOutOfBoundsException.class, () -> computePositionForIndex(nonOverlappingRangesToNumElementsPerPosition, -1));
    int totalNumberOfElements = computeTotalNumElements(nonOverlappingRangesToNumElementsPerPosition);
    assertEquals(position, totalNumberOfElements);
    assertThrows(IndexOutOfBoundsException.class, () -> computePositionForIndex(nonOverlappingRangesToNumElementsPerPosition, totalNumberOfElements));
}
Also used : OffsetRange(org.apache.beam.sdk.io.range.OffsetRange) TreeSet(java.util.TreeSet) ImmutableMap(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.ImmutableMap) Map(java.util.Map)

Aggregations

OffsetRange (org.apache.beam.sdk.io.range.OffsetRange)63 Test (org.junit.Test)53 Instant (org.joda.time.Instant)8 ArrayList (java.util.ArrayList)5 OffsetRangeTracker (org.apache.beam.sdk.transforms.splittabledofn.OffsetRangeTracker)5 Progress (org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker.Progress)5 ProcessContinuation (org.apache.beam.sdk.transforms.DoFn.ProcessContinuation)4 PartitionMetadata (org.apache.beam.sdk.io.gcp.spanner.changestreams.model.PartitionMetadata)3 DoFn (org.apache.beam.sdk.transforms.DoFn)3 BigDecimal (java.math.BigDecimal)2 RestrictionTracker (org.apache.beam.sdk.transforms.splittabledofn.RestrictionTracker)2 Offset (com.google.cloud.pubsublite.Offset)1 SuppressFBWarnings (edu.umd.cs.findbugs.annotations.SuppressFBWarnings)1 Scope (io.opencensus.common.Scope)1 AttributeValue (io.opencensus.trace.AttributeValue)1 Tracer (io.opencensus.trace.Tracer)1 Tracing (io.opencensus.trace.Tracing)1 Serializable (java.io.Serializable)1 Map (java.util.Map)1 Optional (java.util.Optional)1