Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PCollection data sampling for Java SDK harness #25064 #25354

Merged
merged 43 commits into from
Feb 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
43 commits
Select commit Hold shift + click to select a range
cd630e4
Data Sampling Java Impl
Jan 26, 2023
e530d14
comments
Jan 26, 2023
a09f5d6
add PBD id to context
rohdesamuel Jan 31, 2023
1cb08f9
merge
Jan 31, 2023
566c7b8
Add more tests and spotless
Jan 31, 2023
7395a2e
Finish Java data sampling impl with tests, adding comments
Feb 1, 2023
4c1253f
more comments, remove Payload
Feb 1, 2023
3d88254
more comments
Feb 1, 2023
5bbef91
spotless
Feb 1, 2023
6993b83
Encode in the nested context
rohdesamuel Feb 3, 2023
769902f
Update sdks/java/harness/src/main/java/org/apache/beam/fn/harness/con…
rohdesamuel Feb 7, 2023
5c06d4e
Apply suggestions from code review
rohdesamuel Feb 7, 2023
99087e8
address pr comments
Feb 9, 2023
5609e4a
give default pbd id to test context
rohdesamuel Feb 9, 2023
694c929
address spotlesscheck
rohdesamuel Feb 9, 2023
519aece
spotless apply
rohdesamuel Feb 9, 2023
8efbb15
style guide spotless apply
rohdesamuel Feb 9, 2023
cd3732d
add serviceloader
rohdesamuel Feb 9, 2023
415e3f0
change datasampling to modify the consumers and not graph for sampling
rohdesamuel Feb 10, 2023
553fc5e
remove redundant SamplerState obj
rohdesamuel Feb 10, 2023
e260eb4
spotless
rohdesamuel Feb 10, 2023
4f29308
replace mutex with atomics in output sampler to reduce contention
rohdesamuel Feb 10, 2023
8cbc6c8
spotless and fix OutputSamplerTest
rohdesamuel Feb 10, 2023
f67234f
Update sdks/java/harness/src/main/java/org/apache/beam/fn/harness/dat…
rohdesamuel Feb 13, 2023
6815330
Update sdks/java/harness/src/main/java/org/apache/beam/fn/harness/dat…
rohdesamuel Feb 13, 2023
4848725
Update sdks/java/harness/src/main/java/org/apache/beam/fn/harness/deb…
rohdesamuel Feb 13, 2023
6c16576
always init outputsampler
rohdesamuel Feb 13, 2023
822587d
add final to DataSampler in FnHarness
rohdesamuel Feb 13, 2023
fe9ab2a
spotless apply
rohdesamuel Feb 13, 2023
07d37ea
update from proto names
rohdesamuel Feb 13, 2023
5e9c4b0
spotless bugs
Feb 14, 2023
f5f97fb
Apply suggestions from code review
rohdesamuel Feb 14, 2023
c3db7c0
address pr comments
Feb 14, 2023
fce6d69
spotlessapply and add byte[] test
Feb 15, 2023
69d8bb4
validate datasampler args
Feb 15, 2023
be2ebe4
add concurrency tests
Feb 15, 2023
93af06e
Merge branch 'master' into data-sampling-java
lukecwik Feb 21, 2023
cbdbbc3
Update sdks/java/harness/src/main/java/org/apache/beam/fn/harness/deb…
rohdesamuel Feb 21, 2023
8cc8ee0
Update sdks/java/harness/src/test/java/org/apache/beam/fn/harness/deb…
rohdesamuel Feb 21, 2023
10aa7de
Update sdks/java/harness/src/test/java/org/apache/beam/fn/harness/deb…
rohdesamuel Feb 21, 2023
04c6c88
Update sdks/java/harness/src/test/java/org/apache/beam/fn/harness/deb…
rohdesamuel Feb 21, 2023
862439b
improve contention tests
Feb 22, 2023
490be4a
spotless
Feb 22, 2023
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
Prev Previous commit
Next Next commit
add PBD id to context
  • Loading branch information
rohdesamuel committed Feb 13, 2023
commit a09f5d6d8093810336fa138f84ed5106edded832
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,9 @@ interface Context {
/** A client for handling state requests. */
BeamFnStateClient getBeamFnStateClient();

/** The id of the parent ProcessBundleDescriptor. */
String getProcessBundleDescriptorId();

/** The id of the PTransform. */
String getPTransformId();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -319,6 +319,11 @@ public BeamFnStateClient getBeamFnStateClient() {
return beamFnStateClient;
}

@Override
public String getProcessBundleDescriptorId() {
return processBundleDescriptor.getId();
}

@Override
public String getPTransformId() {
return pTransformId;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class DataSampler {
public static class OutputSampler<T> {
Expand Down Expand Up @@ -92,24 +94,45 @@ private void clear() {
}
}

private final Map<String, OutputSampler<?>> outputSamplers = new HashMap<>();
private final Map<String, Map<String, OutputSampler<?>>> outputSamplers = new HashMap<>();

public <T>OutputSampler<T> sampleOutput(String outputName, Coder<T> coder) {
if (outputSamplers.containsKey(outputName)) {
return (OutputSampler<T>)outputSamplers.get(outputName);
}
public <T>OutputSampler<T> sampleOutput(String processBundleDescriptorId, String pcollectionId, Coder<T> coder) {
outputSamplers.putIfAbsent(processBundleDescriptorId, new HashMap<>());
Map<String, OutputSampler<?>> samplers = outputSamplers.get(processBundleDescriptorId);
samplers.putIfAbsent(pcollectionId, new OutputSampler<T>(coder));

OutputSampler<T> sampler = new OutputSampler<T>(coder);
outputSamplers.put(outputName, sampler);
return sampler;
return (OutputSampler<T>)samplers.get(pcollectionId);
}

public Map<String, List<byte[]>> samples() {
return samplesFor(new HashSet<>(), new HashSet<>());
}

public Map<String, List<byte[]>> samplesFor(Set<String> descriptors, Set<String> pcollections) {
rohdesamuel marked this conversation as resolved.
Show resolved Hide resolved
Map<String, List<byte[]>> samples = new HashMap<>();
outputSamplers.forEach((pcollectionId, outputSampler) -> {
samples.putIfAbsent(pcollectionId, new ArrayList<>());
samples.get(pcollectionId).addAll(outputSampler.samples());
outputSamplers.forEach((descriptorId, samplers) -> {
if (!descriptors.isEmpty() && !descriptors.contains(descriptorId)) {
return;
}

samplers.forEach((pcollectionId, outputSampler) -> {
if (!pcollections.isEmpty() && !pcollections.contains(pcollectionId)) {
return;
}

samples.putIfAbsent(pcollectionId, new ArrayList<>());
samples.get(pcollectionId).addAll(outputSampler.samples());
});
});

return samples;
}

public Map<String, List<byte[]>> samplesForDescriptors(Set<String> descriptors) {
return samplesFor(descriptors, new HashSet<>());
}

public Map<String, List<byte[]>> samplesForPCollections(Set<String> pcollections) {
return samplesFor(new HashSet<>(), pcollections);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.apache.beam.sdk.Pipeline;
import org.apache.beam.sdk.coders.Coder;
import org.apache.beam.sdk.coders.StringUtf8Coder;
import org.apache.beam.sdk.coders.VarIntCoder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
Expand All @@ -23,71 +24,13 @@

@RunWith(JUnit4.class)
public class DataSamplerTest {

@Test
public void testCreatingAndProcessingWithSampling() throws Exception {
DataSampler sampler = new DataSampler();

VarIntCoder coder = VarIntCoder.of();
sampler.sampleOutput("descriptor-id", "pcollection-id", coder).sample(12345);

// Create the DataSampling PTransform.
String pTransformId = "pTransformId";

RunnerApi.FunctionSpec functionSpec =
RunnerApi.FunctionSpec.newBuilder()
.setUrn(DataSamplingFnRunner.URN)
.build();
RunnerApi.PTransform pTransform =
RunnerApi.PTransform.newBuilder()
.setSpec(functionSpec)
.putInputs("input", "inputTarget")
.build();

// Populate fake input PCollections.
Map<String, RunnerApi.PCollection> pCollectionMap = new HashMap<>();
pCollectionMap.put(
"inputTarget",
RunnerApi.PCollection.newBuilder()
.setUniqueName("inputTarget")
.setCoderId("coder-id")
.build());

// Populate the PTransform context that includes the DataSampler.
DataSampler dataSampler = new DataSampler();
RunnerApi.Coder coder = CoderTranslation.toProto(StringUtf8Coder.of()).getCoder();
PTransformRunnerFactoryTestContext context =
PTransformRunnerFactoryTestContext.builder(pTransformId, pTransform)
.processBundleInstructionId("instruction-id")
.pCollections(pCollectionMap)
.coders(Collections.singletonMap("coder-id", coder))
.dataSampler(dataSampler)
.build();

// Create the runner which samples the input PCollection.
new DataSamplingFnRunner.Factory<>().createRunnerForPTransform(context);
assertThat(
context.getPCollectionConsumers().keySet(),
contains("inputTarget"));

// Send in a test value that should be sampled.
context.getPCollectionConsumer("inputTarget").accept(valueInGlobalWindow("Hello, World!"));

// Rehydrate the given utf-8 string coder.
RehydratedComponents rehydratedComponents =
RehydratedComponents.forComponents(
RunnerApi.Components.newBuilder()
.putAllCoders(context.getCoders())
.putAllPcollections(context.getPCollections())
.putAllWindowingStrategies(context.getWindowingStrategies())
.build())
.withPipeline(Pipeline.create());
Coder<String> rehydratedCoder = (Coder<String>)rehydratedComponents.getCoder("coder-id");

Map<String, List<byte[]>> samples = dataSampler.samples();
assertThat(samples.keySet(), contains("inputTarget"));

// Ensure that the value was sampled.
ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
rehydratedCoder.encode("Hello, World!", outputStream);
byte[] encodedValue = outputStream.toByteArray();
assertThat(samples.get("inputTarget"), contains(encodedValue));
System.out.println(sampler.samples());
}
}