You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by "rohdesamuel (via GitHub)" <gi...@apache.org> on 2023/06/14 00:36:00 UTC

[GitHub] [beam] rohdesamuel opened a new pull request, #27121: Implement Java exception sampling

rohdesamuel opened a new pull request, #27121:
URL: https://github.com/apache/beam/pull/27121

   Implements sampling of elements during exceptions. The proto changes are not in yet, so the metadata will be filled in a follow-up PR.
   
   addresses #26704
   
   ------------------------
   
   Thank you for your contribution! Follow this checklist to help us incorporate your contribution quickly and easily:
   
    - [x] Mention the appropriate issue in your description (for example: `addresses #123`), if applicable. This will automatically add a link to the pull request in the issue. If you would like the issue to automatically close on merging the pull request, comment `fixes #<ISSUE NUMBER>` instead.
    - [ ] Update `CHANGES.md` with noteworthy changes.
    - [ ] If this contribution is large, please file an Apache [Individual Contributor License Agreement](https://www.apache.org/licenses/icla.pdf).
   
   See the [Contributor Guide](https://beam.apache.org/contribute) for more tips on [how to make review process smoother](https://beam.apache.org/contribute/get-started-contributing/#make-the-reviewers-job-easier).
   
   To check the build health, please visit [https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md](https://github.com/apache/beam/blob/master/.test-infra/BUILD_STATUS.md)
   
   GitHub Actions Tests Status (on master branch)
   ------------------------------------------------------------------------------------------------
   [![Build python source distribution and wheels](https://github.com/apache/beam/workflows/Build%20python%20source%20distribution%20and%20wheels/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Build+python+source+distribution+and+wheels%22+branch%3Amaster+event%3Aschedule)
   [![Python tests](https://github.com/apache/beam/workflows/Python%20tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Python+Tests%22+branch%3Amaster+event%3Aschedule)
   [![Java tests](https://github.com/apache/beam/workflows/Java%20Tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Java+Tests%22+branch%3Amaster+event%3Aschedule)
   [![Go tests](https://github.com/apache/beam/workflows/Go%20tests/badge.svg?branch=master&event=schedule)](https://github.com/apache/beam/actions?query=workflow%3A%22Go+tests%22+branch%3Amaster+event%3Aschedule)
   
   See [CI.md](https://github.com/apache/beam/blob/master/CI.md) for more information about GitHub Actions CI.
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] damondouglas commented on pull request #27121: Implement Java exception sampling

Posted by "damondouglas (via GitHub)" <gi...@apache.org>.
damondouglas commented on PR #27121:
URL: https://github.com/apache/beam/pull/27121#issuecomment-1593457550

   Run Java_Examples_Dataflow_Java11 PreCommit


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] rohdesamuel commented on a diff in pull request #27121: Implement Java exception sampling

Posted by "rohdesamuel (via GitHub)" <gi...@apache.org>.
rohdesamuel commented on code in PR #27121:
URL: https://github.com/apache/beam/pull/27121#discussion_r1231306375


##########
sdks/java/harness/src/test/java/org/apache/beam/fn/harness/debug/OutputSamplerTest.java:
##########
@@ -139,6 +139,69 @@ public void testActsLikeCircularBuffer() throws Exception {
     assertThat(samples, containsInAnyOrder(expected.toArray()));
   }
 
+  /**
+   * Test that elements with exceptions can be sampled. TODO: test that the exception metadata is
+   * set.
+   *
+   * @throws Exception when encoding fails (shouldn't happen).
+   */
+  @Test
+  public void testCanSampleExceptions() throws Exception {

Review Comment:
   Done



##########
sdks/java/harness/src/test/java/org/apache/beam/fn/harness/debug/OutputSamplerTest.java:
##########
@@ -139,6 +139,69 @@ public void testActsLikeCircularBuffer() throws Exception {
     assertThat(samples, containsInAnyOrder(expected.toArray()));
   }
 
+  /**
+   * Test that elements with exceptions can be sampled. TODO: test that the exception metadata is
+   * set.
+   *
+   * @throws Exception when encoding fails (shouldn't happen).
+   */
+  @Test
+  public void testCanSampleExceptions() throws Exception {
+    VarIntCoder coder = VarIntCoder.of();
+    OutputSampler<Integer> outputSampler = new OutputSampler<>(coder, 5, 20);
+
+    WindowedValue<Integer> windowedValue = WindowedValue.valueInGlobalWindow(1);
+    ElementSample<Integer> elementSample = outputSampler.sample(windowedValue);
+
+    Exception exception = new RuntimeException("Test exception");
+    outputSampler.exception(elementSample, exception);
+
+    // The first 10 are always sampled, but with maxSamples = 5, the first ten are downsampled to
+    // 4..9 inclusive. Then,
+    // the 20th element is sampled (19) and every 20 after.
+    List<BeamFnApi.SampledElement> expected = new ArrayList<>();
+    expected.add(encodeInt(1));
+
+    List<BeamFnApi.SampledElement> samples = outputSampler.samples();
+    assertThat(samples, containsInAnyOrder(expected.toArray()));
+  }
+
+  /**
+   * Tests that multiple samples don't push out exception samples. TODO: test that the exception
+   * metadata is set.
+   *
+   * @throws Exception when encoding fails (shouldn't happen).
+   */
+  @Test
+  public void testExceptionSamplesAreNotRemoved() throws Exception {

Review Comment:
   Done



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] rohdesamuel commented on a diff in pull request #27121: Implement Java exception sampling

Posted by "rohdesamuel (via GitHub)" <gi...@apache.org>.
rohdesamuel commented on code in PR #27121:
URL: https://github.com/apache/beam/pull/27121#discussion_r1231310970


##########
sdks/java/harness/src/main/java/org/apache/beam/fn/harness/debug/OutputSampler.java:
##########
@@ -129,20 +158,43 @@ public List<BeamFnApi.SampledElement> samples() throws IOException {
       resampleIndex = 0;
     }
 
+    // An element can live in both the main samples and exception buffer. Use a small look up table
+    // to deduplicate samples.
+    HashSet<Long> seen = new HashSet<>();
     ByteStringOutputStream stream = new ByteStringOutputStream();
     for (int i = 0; i < bufferToSend.size(); i++) {
       int index = (sampleIndex + i) % bufferToSend.size();
+      ElementSample<T> sample = bufferToSend.get(index);
+      seen.add(sample.id);
+
+      if (valueCoder != null) {
+        this.valueCoder.encode(sample.sample.getValue(), stream, Coder.Context.NESTED);
+      } else if (windowedValueCoder != null) {
+        this.windowedValueCoder.encode(sample.sample, stream, Coder.Context.NESTED);
+      }

Review Comment:
   I don't think so or at least it would be prohibitively weird to test. These coders are controlled by the constructor and are a one-of. And for those cases there are already tests.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] damondouglas commented on pull request #27121: Implement Java exception sampling

Posted by "damondouglas (via GitHub)" <gi...@apache.org>.
damondouglas commented on PR #27121:
URL: https://github.com/apache/beam/pull/27121#issuecomment-1593456809

   Run Java PreCommit


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] github-actions[bot] commented on pull request #27121: Implement Java exception sampling

Posted by "github-actions[bot] (via GitHub)" <gi...@apache.org>.
github-actions[bot] commented on PR #27121:
URL: https://github.com/apache/beam/pull/27121#issuecomment-1590254248

   Stopping reviewer notifications for this pull request: review requested by someone other than the bot, ceding control


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] damondouglas commented on pull request #27121: Implement Java exception sampling

Posted by "damondouglas (via GitHub)" <gi...@apache.org>.
damondouglas commented on PR #27121:
URL: https://github.com/apache/beam/pull/27121#issuecomment-1593458008

   Run Java_Examples_Dataflow_Java17 PreCommit


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] damondouglas commented on a diff in pull request #27121: Implement Java exception sampling

Posted by "damondouglas (via GitHub)" <gi...@apache.org>.
damondouglas commented on code in PR #27121:
URL: https://github.com/apache/beam/pull/27121#discussion_r1230153414


##########
sdks/java/harness/src/test/java/org/apache/beam/fn/harness/debug/OutputSamplerTest.java:
##########
@@ -139,6 +139,69 @@ public void testActsLikeCircularBuffer() throws Exception {
     assertThat(samples, containsInAnyOrder(expected.toArray()));
   }
 
+  /**
+   * Test that elements with exceptions can be sampled. TODO: test that the exception metadata is
+   * set.
+   *
+   * @throws Exception when encoding fails (shouldn't happen).
+   */
+  @Test
+  public void testCanSampleExceptions() throws Exception {

Review Comment:
   Instead of throws Exception, could we change this to IOException as required by `org.apache.beam.fn.harness.debug.OutputSampler.samples() throws IOException`? I realize all the other tests here do throws Exception. I thought it might be clearer to see the less generic Exception. Feel free to resolve this comment if you disagree.



##########
sdks/java/harness/src/test/java/org/apache/beam/fn/harness/debug/OutputSamplerTest.java:
##########
@@ -139,6 +139,69 @@ public void testActsLikeCircularBuffer() throws Exception {
     assertThat(samples, containsInAnyOrder(expected.toArray()));
   }
 
+  /**
+   * Test that elements with exceptions can be sampled. TODO: test that the exception metadata is
+   * set.
+   *
+   * @throws Exception when encoding fails (shouldn't happen).
+   */
+  @Test
+  public void testCanSampleExceptions() throws Exception {
+    VarIntCoder coder = VarIntCoder.of();
+    OutputSampler<Integer> outputSampler = new OutputSampler<>(coder, 5, 20);
+
+    WindowedValue<Integer> windowedValue = WindowedValue.valueInGlobalWindow(1);
+    ElementSample<Integer> elementSample = outputSampler.sample(windowedValue);
+
+    Exception exception = new RuntimeException("Test exception");
+    outputSampler.exception(elementSample, exception);
+
+    // The first 10 are always sampled, but with maxSamples = 5, the first ten are downsampled to
+    // 4..9 inclusive. Then,
+    // the 20th element is sampled (19) and every 20 after.
+    List<BeamFnApi.SampledElement> expected = new ArrayList<>();
+    expected.add(encodeInt(1));
+
+    List<BeamFnApi.SampledElement> samples = outputSampler.samples();
+    assertThat(samples, containsInAnyOrder(expected.toArray()));
+  }
+
+  /**
+   * Tests that multiple samples don't push out exception samples. TODO: test that the exception
+   * metadata is set.
+   *
+   * @throws Exception when encoding fails (shouldn't happen).
+   */
+  @Test
+  public void testExceptionSamplesAreNotRemoved() throws Exception {

Review Comment:
   Same as above but repeated here. Instead of throws Exception, could we change this to IOException as required by `org.apache.beam.fn.harness.debug.OutputSampler.samples() throws IOException`? I realize all the other tests here do throws Exception. I thought it might be clearer to see the less generic Exception. Feel free to resolve this comment if you disagree.



##########
sdks/java/harness/src/main/java/org/apache/beam/fn/harness/debug/OutputSampler.java:
##########
@@ -129,20 +158,43 @@ public List<BeamFnApi.SampledElement> samples() throws IOException {
       resampleIndex = 0;
     }
 
+    // An element can live in both the main samples and exception buffer. Use a small look up table
+    // to deduplicate samples.
+    HashSet<Long> seen = new HashSet<>();
     ByteStringOutputStream stream = new ByteStringOutputStream();
     for (int i = 0; i < bufferToSend.size(); i++) {
       int index = (sampleIndex + i) % bufferToSend.size();
+      ElementSample<T> sample = bufferToSend.get(index);
+      seen.add(sample.id);
+
+      if (valueCoder != null) {
+        this.valueCoder.encode(sample.sample.getValue(), stream, Coder.Context.NESTED);
+      } else if (windowedValueCoder != null) {
+        this.windowedValueCoder.encode(sample.sample, stream, Coder.Context.NESTED);
+      }

Review Comment:
   Would it make sense to add tests for the following cases? Feel free to just resolve this comment if you don't agree as you are closer to the code than I am. It looked like your new tests covered the valueCoder != null instantiating OutputSampler with a `VarIntCoder`, already. 
   
   | valueCoder | windowedValueCoder |
   | --- | --- |
   | null | null |
   | not null | null |
   | null | not null |
   | not null | not null |



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] damondouglas commented on pull request #27121: Implement Java exception sampling

Posted by "damondouglas (via GitHub)" <gi...@apache.org>.
damondouglas commented on PR #27121:
URL: https://github.com/apache/beam/pull/27121#issuecomment-1593457224

   Run Java_Examples_Dataflow PreCommit


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] damondouglas commented on pull request #27121: Implement Java exception sampling

Posted by "damondouglas (via GitHub)" <gi...@apache.org>.
damondouglas commented on PR #27121:
URL: https://github.com/apache/beam/pull/27121#issuecomment-1593477655

   Run Java PreCommit


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] damondouglas merged pull request #27121: Implement Java exception sampling

Posted by "damondouglas (via GitHub)" <gi...@apache.org>.
damondouglas merged PR #27121:
URL: https://github.com/apache/beam/pull/27121


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [beam] rohdesamuel commented on pull request #27121: Implement Java exception sampling

Posted by "rohdesamuel (via GitHub)" <gi...@apache.org>.
rohdesamuel commented on PR #27121:
URL: https://github.com/apache/beam/pull/27121#issuecomment-1590253618

   R: @damondouglas 


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org