Search in sources :

Example 1 with NonDeterministicException

use of org.apache.beam.sdk.coders.Coder.NonDeterministicException in project beam by apache.

the class WindowTest method testNonDeterministicWindowCoder.

@Test
public void testNonDeterministicWindowCoder() throws NonDeterministicException {
    FixedWindows mockWindowFn = Mockito.mock(FixedWindows.class);
    @SuppressWarnings({ "unchecked", "rawtypes" }) Class<Coder<IntervalWindow>> coderClazz = (Class) Coder.class;
    Coder<IntervalWindow> mockCoder = Mockito.mock(coderClazz);
    when(mockWindowFn.windowCoder()).thenReturn(mockCoder);
    NonDeterministicException toBeThrown = new NonDeterministicException(mockCoder, "Its just not deterministic.");
    Mockito.doThrow(toBeThrown).when(mockCoder).verifyDeterministic();
    thrown.expect(IllegalArgumentException.class);
    thrown.expectCause(Matchers.sameInstance(toBeThrown));
    thrown.expectMessage("Window coders must be deterministic");
    Window.into(mockWindowFn);
}
Also used : Coder(org.apache.beam.sdk.coders.Coder) StringUtf8Coder(org.apache.beam.sdk.coders.StringUtf8Coder) NonDeterministicException(org.apache.beam.sdk.coders.Coder.NonDeterministicException) Test(org.junit.Test)

Example 2 with NonDeterministicException

use of org.apache.beam.sdk.coders.Coder.NonDeterministicException in project beam by apache.

the class ProtobufUtil method verifyDeterministic.

/**
   * Recursively checks whether the specified class uses any Protocol Buffers fields that cannot
   * be deterministically encoded.
   *
   * @throws NonDeterministicException if the object cannot be encoded deterministically.
   */
static void verifyDeterministic(ProtoCoder<?> coder) throws NonDeterministicException {
    Class<? extends Message> message = coder.getMessageType();
    ExtensionRegistry registry = coder.getExtensionRegistry();
    Set<Descriptor> descriptors = getRecursiveDescriptorsForClass(message, registry);
    for (Descriptor d : descriptors) {
        for (FieldDescriptor fd : d.getFields()) {
            // be encoded deterministically.
            if (fd.isMapField()) {
                String reason = String.format("Protocol Buffers message %s transitively includes Map field %s (from file %s)." + " Maps cannot be deterministically encoded.", message.getName(), fd.getFullName(), fd.getFile().getFullName());
                throw new NonDeterministicException(coder, reason);
            }
        }
    }
}
Also used : NonDeterministicException(org.apache.beam.sdk.coders.Coder.NonDeterministicException) GenericDescriptor(com.google.protobuf.Descriptors.GenericDescriptor) Descriptor(com.google.protobuf.Descriptors.Descriptor) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor) ExtensionRegistry(com.google.protobuf.ExtensionRegistry) FieldDescriptor(com.google.protobuf.Descriptors.FieldDescriptor)

Example 3 with NonDeterministicException

use of org.apache.beam.sdk.coders.Coder.NonDeterministicException in project beam by apache.

the class CoderTest method testNonDeterministicException.

@Test
public void testNonDeterministicException() {
    NonDeterministicException rootCause = new NonDeterministicException(VoidCoder.of(), "Root Cause");
    NonDeterministicException exception = new NonDeterministicException(StringUtf8Coder.of(), "Problem", rootCause);
    assertEquals(rootCause, exception.getCause());
    assertThat(exception.getReasons(), contains("Problem"));
    assertThat(exception.toString(), containsString("Problem"));
    assertThat(exception.toString(), containsString("is not deterministic"));
}
Also used : NonDeterministicException(org.apache.beam.sdk.coders.Coder.NonDeterministicException) Test(org.junit.Test)

Aggregations

NonDeterministicException (org.apache.beam.sdk.coders.Coder.NonDeterministicException)3 Test (org.junit.Test)2 Descriptor (com.google.protobuf.Descriptors.Descriptor)1 FieldDescriptor (com.google.protobuf.Descriptors.FieldDescriptor)1 GenericDescriptor (com.google.protobuf.Descriptors.GenericDescriptor)1 ExtensionRegistry (com.google.protobuf.ExtensionRegistry)1 Coder (org.apache.beam.sdk.coders.Coder)1 StringUtf8Coder (org.apache.beam.sdk.coders.StringUtf8Coder)1