Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,31 @@
*/
package org.apache.beam.sdk.io.gcp.datastore;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.instanceOf;
import static org.junit.Assert.assertThrows;

import java.util.Collections;
import java.util.Map;
import java.util.UUID;
import org.apache.beam.runners.core.DoFnRunner;
import org.apache.beam.runners.core.DoFnRunners;
import org.apache.beam.runners.core.SideInputReader;
import org.apache.beam.sdk.metrics.Counter;
import org.apache.beam.sdk.options.PipelineOptionsFactory;
import org.apache.beam.sdk.testing.TestPipeline;
import org.apache.beam.sdk.transforms.Create;
import org.apache.beam.sdk.transforms.DoFnTester;
import org.apache.beam.sdk.transforms.DoFnTester.CloningBehavior;
import org.apache.beam.sdk.transforms.DoFnSchemaInformation;
import org.apache.beam.sdk.transforms.View;
import org.apache.beam.sdk.transforms.windowing.GlobalWindow;
import org.apache.beam.sdk.transforms.windowing.BoundedWindow;
import org.apache.beam.sdk.util.Sleeper;
import org.apache.beam.sdk.util.UserCodeException;
import org.apache.beam.sdk.util.WindowedValueMultiReceiver;
import org.apache.beam.sdk.values.PCollectionView;
import org.apache.beam.sdk.values.TupleTag;
import org.apache.beam.sdk.values.WindowedValue;
import org.apache.beam.sdk.values.WindowedValues;
import org.apache.beam.sdk.values.WindowingStrategy;
import org.apache.beam.vendor.guava.v32_1_2_jre.com.google.common.collect.ImmutableMap;
import org.joda.time.DateTimeUtils;
import org.joda.time.Duration;
Expand All @@ -52,8 +64,13 @@ public class RampupThrottlingFnTest {
mockCounter.inc(millis);
throw new RampupDelayException();
};
private DoFnTester<String, String> rampupThrottlingFnTester;
private DoFnRunner<String, String> rampupThrottlingFnRunner;

/**
* Runs the fn in-process rather than in a {@link TestPipeline}: the test fixes the Joda clock and
* injects a non-serializable sleeper and counter, and checks throttling between elements of one
* bundle.
*/
@Before
public void setUp() throws Exception {
MockitoAnnotations.openMocks(this);
Expand All @@ -62,20 +79,46 @@ public void setUp() throws Exception {
TestPipeline pipeline = TestPipeline.create();
PCollectionView<Instant> startTimeView =
pipeline.apply(Create.of(Instant.now())).apply(View.asSingleton());
RampupThrottlingFn<String> rampupThrottlingFn =
new RampupThrottlingFn<String>(1, startTimeView) {
@Override
@Setup
public void setup() {
super.setup();
this.sleeper = mockSleeper;
}
};
rampupThrottlingFnTester = DoFnTester.of(rampupThrottlingFn);
rampupThrottlingFnTester.setSideInput(startTimeView, GlobalWindow.INSTANCE, Instant.now());
rampupThrottlingFnTester.setCloningBehavior(CloningBehavior.DO_NOT_CLONE);
rampupThrottlingFnTester.startBundle();
Instant startTime = Instant.now();
RampupThrottlingFn<String> rampupThrottlingFn = new RampupThrottlingFn<>(1, startTimeView);
rampupThrottlingFn.setup();
rampupThrottlingFn.sleeper = mockSleeper;
rampupThrottlingFn.throttlingMsecs = mockCounter;

rampupThrottlingFnRunner =
DoFnRunners.simpleRunner(
PipelineOptionsFactory.create(),
rampupThrottlingFn,
new SideInputReader() {
@Override
@SuppressWarnings("unchecked")
public <T> T get(PCollectionView<T> view, BoundedWindow window) {
return (T) startTime;
}

@Override
public <T> boolean contains(PCollectionView<T> view) {
return view.equals(startTimeView);
}

@Override
public boolean isEmpty() {
return false;
}
},
new WindowedValueMultiReceiver() {
@Override
public <T> void output(TupleTag<T> tag, WindowedValue<T> output) {}
},
new TupleTag<>(),
Collections.emptyList(),
null,
null,
Collections.emptyMap(),
WindowingStrategy.globalDefault(),
DoFnSchemaInformation.create(),
Collections.emptyMap());
rampupThrottlingFnRunner.startBundle();
}

@After
Expand All @@ -101,11 +144,16 @@ public void testRampupThrottler() throws Exception {
for (Map.Entry<Duration, Integer> entry : rampupSchedule.entrySet()) {
DateTimeUtils.setCurrentMillisFixed(entry.getKey().getMillis());
for (int i = 0; i < entry.getValue(); i++) {
rampupThrottlingFnTester.processElement(UUID.randomUUID().toString());
processElement(UUID.randomUUID().toString());
}
assertThrows(RampupDelayException.class, () -> rampupThrottlingFnTester.processElement(null));
UserCodeException e = assertThrows(UserCodeException.class, () -> processElement(null));
assertThat(e.getCause(), instanceOf(RampupDelayException.class));
}
}

private void processElement(String element) {
rampupThrottlingFnRunner.processElement(WindowedValues.valueInGlobalWindow(element));
}

static class RampupDelayException extends InterruptedException {}
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,21 @@
package org.apache.beam.sdk.io.gcp.datastore;

import static org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.Read.NUM_QUERY_SPLITS_MIN;
import static org.junit.Assert.assertEquals;

import com.google.datastore.v1.Query;
import java.util.List;
import org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.Read.SplitQueryFn;
import org.apache.beam.sdk.io.gcp.datastore.DatastoreV1.Read.V1Options;
import org.apache.beam.sdk.options.ValueProvider.StaticValueProvider;
import org.apache.beam.sdk.transforms.DoFnTester;
import org.apache.beam.sdk.testing.PAssert;
import org.apache.beam.sdk.testing.TestPipeline;
import org.apache.beam.sdk.transforms.Count;
import org.apache.beam.sdk.transforms.Create;
import org.apache.beam.sdk.transforms.ParDo;
import org.apache.beam.sdk.values.PCollection;
import org.checkerframework.checker.nullness.qual.Nullable;
import org.joda.time.Duration;
import org.joda.time.Instant;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand All @@ -53,6 +57,8 @@
*/
@RunWith(JUnit4.class)
public class SplitQueryFnIT {
@Rule public final transient TestPipeline pipeline = TestPipeline.create();

private Instant readTime = Instant.now().minus(Duration.standardSeconds(10));

/** Tests {@link SplitQueryFn} to generate expected number of splits for a large dataset. */
Expand All @@ -67,6 +73,7 @@ public void testSplitQueryFnWithLargeDataset() throws Exception {
int expectedNumSplits = 32;
testSplitQueryFn(projectId, databaseId, kind, namespace, expectedNumSplits, null);
testSplitQueryFn(projectId, databaseId, kind, namespace, expectedNumSplits, readTime);
pipeline.run();
}

/** Tests {@link SplitQueryFn} to fallback to NUM_QUERY_SPLITS_MIN for a small dataset. */
Expand All @@ -79,9 +86,13 @@ public void testSplitQueryFnWithSmallDataset() throws Exception {
int expectedNumSplits = NUM_QUERY_SPLITS_MIN;
testSplitQueryFn(projectId, databaseId, kind, namespace, expectedNumSplits, null);
testSplitQueryFn(projectId, databaseId, kind, namespace, expectedNumSplits, readTime);
pipeline.run();
}

/** A helper method to test {@link SplitQueryFn} to generate the expected number of splits. */
/**
* Adds a branch to {@link #pipeline} that asserts {@link SplitQueryFn} generates the expected
* number of splits.
*/
private void testSplitQueryFn(
String projectId,
String databaseId,
Expand All @@ -98,10 +109,14 @@ private void testSplitQueryFn(
V1Options.from(projectId, StaticValueProvider.of(databaseId), namespace, null),
0,
readTime);
DoFnTester<Query, Query> doFnTester = DoFnTester.of(splitQueryFn);
String suffix = readTime == null ? " without readTime" : " with readTime";

List<Query> queries = doFnTester.processBundle(query.build());
assertEquals(expectedNumSplits, queries.size());
PCollection<Long> numSplits =
pipeline
.apply("Create query" + suffix, Create.of(query.build()))
.apply("Split query" + suffix, ParDo.of(splitQueryFn))
.apply("Count splits" + suffix, Count.globally());
PAssert.thatSingleton("Num splits" + suffix, numSplits).isEqualTo((long) expectedNumSplits);
}

// TODO (vikasrk): Create datasets under a different namespace and add tests.
Expand Down
Loading