@Override public T copy(T t) { try { return CoderUtils.clone(coder, t); } catch (CoderException e) { throw new RuntimeException("Could not clone.", e); } }
/** Like {@link CoderUtils#clone} but without a checked exception. */ private static <T> T uncheckedClone(Coder<T> coder, T value) { try { return CoderUtils.clone(coder, value); } catch (CoderException e) { throw new RuntimeException(e); } } }
@Override public T copy(T t) { try { return CoderUtils.clone(coder, t); } catch (CoderException e) { throw new RuntimeException("Could not clone.", e); } }
@Override public T copy(T t) { try { return CoderUtils.clone(coder, t); } catch (CoderException e) { throw new RuntimeException("Could not clone.", e); } }
@ProcessElement public void processElement(ProcessContext context) throws CoderException { context.output(CoderUtils.clone(coder, context.element())); } }))
@Override public T apply(KV<AvroKey<T>, NullWritable> input) { try { return CoderUtils.clone(coder, input.getKey().datum()); } catch (CoderException e) { throw new RuntimeException(e); } } };
private void verifyUnmodifiedThrowingCheckedExceptions() throws CoderException { // Since there is no guarantee that cloning an object via the coder will // return the exact same type as value, We are cloning the possiblyModifiedObject // before getting it's structural value. This way we are guaranteed to compare the same // types. T possiblyModifiedClonedValue = CoderUtils.clone(coder, possiblyModifiedObject); Object newStructuralValue = coder.structuralValue(possiblyModifiedClonedValue); if (originalStructuralValue.equals(newStructuralValue)) { return; } else if (Objects.deepEquals( encodedOriginalObject, CoderUtils.encodeToByteArray(coder, possiblyModifiedObject))) { LOG.warn( "{} of type {} has a #structuralValue method which does not return true when the " + "encoding of the elements is equal. Element {}", Coder.class.getSimpleName(), coder.getClass(), possiblyModifiedObject); return; } illegalMutation(clonedOriginalObject, possiblyModifiedClonedValue); }
/** * Create a mutation detector for the provided {@code value}, using the provided {@link Coder} * for cloning and checking serialized forms for equality. */ public CodedValueMutationDetector(T value, Coder<T> coder) throws CoderException { this.coder = coder; // We need to clone the original value before getting it's structural value. // If the object is consistent with equals, the Structural value will be the // exact same object reference making it impossible to detect changes. clonedOriginalValue = CoderUtils.clone(coder, value); this.originalStructuralValue = coder.structuralValue(clonedOriginalValue); this.possiblyModifiedObject = value; this.encodedOriginalObject = CoderUtils.encodeToByteArray(coder, value); this.clonedOriginalObject = CoderUtils.decodeFromByteArray(coder, encodedOriginalObject); }
@Override public UncommittedBundle<T> add(WindowedValue<T> element) { try { // Use the cloned value to ensure that if the coder behaves poorly (e.g. a NoOpCoder that // does not expect to be used) that is reflected in the values given to downstream // transforms WindowedValue<T> clone = element.withValue(CoderUtils.clone(coder, element.getValue())); underlying.add(clone); } catch (CoderException e) { throw UserCodeException.wrap(e); } return this; }
private UnboundedReader<OutputT> getReader(UnboundedSourceShard<OutputT, CheckpointMarkT> shard) throws IOException { UnboundedReader<OutputT> existing = shard.getExistingReader(); if (existing == null) { CheckpointMarkT checkpoint = shard.getCheckpoint(); if (checkpoint != null) { checkpoint = CoderUtils.clone(shard.getSource().getCheckpointMarkCoder(), checkpoint); } return shard.getSource().createReader(options, checkpoint); } else { return existing; } }
@Override protected KV<AvroKey, NullWritable> nextPair() throws IOException, InterruptedException { // Not only is the AvroKey reused by the file format, but the underlying GenericRecord is as well. KV<AvroKey, NullWritable> kv = super.nextPair(); GenericRecord gr = (GenericRecord) kv.getKey().datum(); gr = CoderUtils.clone(AvroCoder.of(gr.getSchema()), gr); return KV.of(new AvroKey(gr), kv.getValue()); }
@Override protected KV<AvroKey, NullWritable> nextPair() throws IOException, InterruptedException { // Not only is the AvroKey reused by the file format, but the underlying GenericRecord is as well. KV<AvroKey, NullWritable> kv = super.nextPair(); GenericRecord gr = (GenericRecord) kv.getKey().datum(); gr = CoderUtils.clone(AvroCoder.of(gr.getSchema()), gr); return KV.of(new AvroKey(gr), kv.getValue()); }
@Test public void encodeDecodeLargeMessage() throws Exception { Message message = Message.Factory.create(); message.setAddress("address"); message.setSubject("subject"); String body = Joiner.on("").join(Collections.nCopies(32 * 1024 * 1024, " ")); message.setBody(new AmqpValue(body)); AmqpMessageCoder coder = AmqpMessageCoder.of(); Message clone = CoderUtils.clone(coder, message); clone.getBody().toString().equals(message.getBody().toString()); } }
CoderUtils.clone(actualSource.getCheckpointMarkCoder(), checkpoint); assertThat(checkpoint.subscriptionPath, not(nullValue())); assertThat(checkpoint.subscriptionPath, equalTo(deserCheckpoint.subscriptionPath));
@Test public void testFailureWithExceptionEncodedDecoded() throws IOException { Throwable error; try { throwWrappedError(); throw new IllegalStateException("Should have failed"); } catch (Throwable e) { error = e; } SuccessOrFailure failure = SuccessOrFailure.failure(PAssert.PAssertionSite.capture("here"), error); SuccessOrFailure res = CoderUtils.clone(SerializableCoder.of(SuccessOrFailure.class), failure); assertEquals( "Encode-decode failed SuccessOrFailure", Throwables.getStackTraceAsString(failure.assertionError()), Throwables.getStackTraceAsString(res.assertionError())); }
@Test public void encodeDecode() throws Exception { Message message = Message.Factory.create(); message.setBody(new AmqpValue("body")); message.setAddress("address"); message.setSubject("test"); AmqpMessageCoder coder = AmqpMessageCoder.of(); Message clone = CoderUtils.clone(coder, message); assertEquals("AmqpValue{body}", clone.getBody().toString()); assertEquals("address", clone.getAddress()); assertEquals("test", clone.getSubject()); }
CoderUtils.clone( source.getCheckpointMarkCoder(), (KafkaCheckpointMark) reader.getCheckpointMark());
CoderUtils.clone( source.getCheckpointMarkCoder(), (KafkaCheckpointMark) reader.getCheckpointMark()); reader = source.createReader(null, mark);
@Test public void testUnboundedSourceCheckpointMark() throws Exception { UnboundedSource<Long, CounterMark> source = CountingSource.unboundedWithTimestampFn(new ValueAsTimestampFn()); UnboundedReader<Long> reader = source.createReader(null, null); final long numToSkip = 3; assertTrue(reader.start()); // Advance the source numToSkip elements and manually save state. for (long l = 0; l < numToSkip; ++l) { reader.advance(); } // Confirm that we get the expected element in sequence before checkpointing. assertEquals(numToSkip, (long) reader.getCurrent()); assertEquals(numToSkip, reader.getCurrentTimestamp().getMillis()); // Checkpoint and restart, and confirm that the source continues correctly. CounterMark mark = CoderUtils.clone(source.getCheckpointMarkCoder(), (CounterMark) reader.getCheckpointMark()); reader = source.createReader(null, mark); assertTrue(reader.start()); // Confirm that we get the next element in sequence. assertEquals(numToSkip + 1, (long) reader.getCurrent()); assertEquals(numToSkip + 1, reader.getCurrentTimestamp().getMillis()); } }