Search in sources :

Example 1 with SequencedMessage

use of com.google.cloud.pubsublite.proto.SequencedMessage in project beam by apache.

the class RowHandlerTest method fullMessageToRow.

@Test
public void fullMessageToRow() {
    RowHandler rowHandler = new RowHandler(FULL_READ_SCHEMA);
    Instant event = Instant.now();
    Instant publish = Instant.now();
    PubSubMessage userMessage = PubSubMessage.newBuilder().setKey(ByteString.copyFromUtf8("val1")).setData(ByteString.copyFromUtf8("val2")).setEventTime(Timestamps.fromMillis(event.getMillis())).putAttributes("key1", AttributeValues.newBuilder().addValues(ByteString.copyFromUtf8("attr1")).addValues(ByteString.copyFromUtf8("attr2")).build()).putAttributes("key2", AttributeValues.newBuilder().addValues(ByteString.copyFromUtf8("attr3")).build()).build();
    SequencedMessage sequencedMessage = SequencedMessage.newBuilder().setMessage(userMessage).setPublishTime(Timestamps.fromMillis(publish.getMillis())).build();
    Row expected = Row.withSchema(FULL_READ_SCHEMA).withFieldValue(RowHandler.MESSAGE_KEY_FIELD, "val1".getBytes(UTF_8)).withFieldValue(RowHandler.PAYLOAD_FIELD, "val2".getBytes(UTF_8)).withFieldValue(RowHandler.EVENT_TIMESTAMP_FIELD, event).withFieldValue(RowHandler.PUBLISH_TIMESTAMP_FIELD, publish).withFieldValue(RowHandler.ATTRIBUTES_FIELD, ImmutableList.of(Row.withSchema(RowHandler.ATTRIBUTES_ENTRY_SCHEMA).attachValues("key1", ImmutableList.of("attr1".getBytes(UTF_8), "attr2".getBytes(UTF_8))), Row.withSchema(RowHandler.ATTRIBUTES_ENTRY_SCHEMA).attachValues("key2", ImmutableList.of("attr3".getBytes(UTF_8))))).build();
    assertEquals(expected, rowHandler.messageToRow(sequencedMessage));
}
Also used : Instant(org.joda.time.Instant) PubSubMessage(com.google.cloud.pubsublite.proto.PubSubMessage) SequencedMessage(com.google.cloud.pubsublite.proto.SequencedMessage) Row(org.apache.beam.sdk.values.Row) Test(org.junit.Test)

Example 2 with SequencedMessage

use of com.google.cloud.pubsublite.proto.SequencedMessage in project beam by apache.

the class UuidDeduplicationTransformTest method dedupesBasedOnReturnedUuid.

@Test
public void dedupesBasedOnReturnedUuid() {
    byte[] bytes = { (byte) 0x123, (byte) 0x456 };
    // These messages have different uuids, so they would both appear in the output collection if
    // the extractor is not respected.
    SequencedMessage message1 = newMessage();
    SequencedMessage message2 = newMessage();
    TestStream<SequencedMessage> messageStream = TestStream.create(ProtoCoder.of(SequencedMessage.class)).advanceWatermarkTo(START).addElements(message1, message2).advanceWatermarkToInfinity();
    PCollection<SequencedMessage> results = pipeline.apply(messageStream).apply(new UuidDeduplicationTransform(UuidDeduplicationOptions.newBuilder().setUuidExtractor(message -> Uuid.of(ByteString.copyFrom(bytes))).build()));
    PAssert.that(results).satisfies(messages -> {
        Preconditions.checkArgument(Iterables.size(messages) == 1);
        return null;
    });
    pipeline.run();
}
Also used : PAssert(org.apache.beam.sdk.testing.PAssert) Duration(org.joda.time.Duration) RunWith(org.junit.runner.RunWith) Timestamps(com.google.protobuf.util.Timestamps) Test(org.junit.Test) JUnit4(org.junit.runners.JUnit4) PCollection(org.apache.beam.sdk.values.PCollection) ByteString(com.google.protobuf.ByteString) UuidDeduplicationOptions(org.apache.beam.sdk.io.gcp.pubsublite.UuidDeduplicationOptions) SequencedMessage(com.google.cloud.pubsublite.proto.SequencedMessage) Rule(org.junit.Rule) ProtoCoder(org.apache.beam.sdk.extensions.protobuf.ProtoCoder) TestPipeline(org.apache.beam.sdk.testing.TestPipeline) Preconditions(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.base.Preconditions) Iterables(org.apache.beam.vendor.guava.v26_0_jre.com.google.common.collect.Iterables) Instant(org.joda.time.Instant) AttributeValues(com.google.cloud.pubsublite.proto.AttributeValues) PubSubMessage(com.google.cloud.pubsublite.proto.PubSubMessage) TestStream(org.apache.beam.sdk.testing.TestStream) Deduplicate(org.apache.beam.sdk.transforms.Deduplicate) Cursor(com.google.cloud.pubsublite.proto.Cursor) SequencedMessage(com.google.cloud.pubsublite.proto.SequencedMessage) Test(org.junit.Test)

Example 3 with SequencedMessage

use of com.google.cloud.pubsublite.proto.SequencedMessage in project beam by apache.

the class SubscriptionPartitionProcessorImplTest method successfulClaimThenTimeout.

@Test
public void successfulClaimThenTimeout() throws Exception {
    when(tracker.tryClaim(any())).thenReturn(true);
    SettableApiFuture<Void> runDone = SettableApiFuture.create();
    SystemExecutors.getFuturesExecutor().execute(() -> {
        assertEquals(ProcessContinuation.resume(), processor.runFor(Duration.standardSeconds(3)));
        runDone.set(null);
    });
    SequencedMessage message1 = messageWithOffset(1);
    SequencedMessage message3 = messageWithOffset(3);
    leakedConsumer.accept(ImmutableList.of(message1, message3));
    runDone.get();
    InOrder order = inOrder(tracker, receiver, subscriber);
    order.verify(tracker).tryClaim(OffsetByteProgress.of(Offset.of(3), message1.getSizeBytes() + message3.getSizeBytes()));
    order.verify(receiver).outputWithTimestamp(message1, new Instant(Timestamps.toMillis(message1.getPublishTime())));
    order.verify(receiver).outputWithTimestamp(message3, new Instant(Timestamps.toMillis(message3.getPublishTime())));
    order.verify(subscriber).allowFlow(FlowControlRequest.newBuilder().setAllowedMessages(2).setAllowedBytes(message1.getSizeBytes() + message3.getSizeBytes()).build());
    assertEquals(processor.lastClaimed().get(), Offset.of(3));
}
Also used : InOrder(org.mockito.InOrder) Instant(org.joda.time.Instant) SequencedMessage(com.google.cloud.pubsublite.proto.SequencedMessage) Test(org.junit.Test)

Example 4 with SequencedMessage

use of com.google.cloud.pubsublite.proto.SequencedMessage in project beam by apache.

the class ReadWriteIT method testReadWrite.

@Test
public void testReadWrite() throws Exception {
    pipeline.getOptions().as(StreamingOptions.class).setStreaming(true);
    pipeline.getOptions().as(TestPipelineOptions.class).setBlockOnRun(false);
    TopicPath topic = createTopic(getProject(pipeline.getOptions()));
    SubscriptionPath subscription = null;
    Exception lastException = null;
    for (int i = 0; i < 30; ++i) {
        // Sleep for topic creation to propagate.
        Thread.sleep(1000);
        try {
            subscription = createSubscription(topic);
            break;
        } catch (Exception e) {
            lastException = e;
            LOG.info("Retrying exception on subscription creation.", e);
        }
    }
    if (subscription == null) {
        throw lastException;
    }
    // Publish some messages
    writeMessages(topic, pipeline);
    // Read some messages. They should be deduplicated by the time we see them, so there should be
    // exactly numMessages, one for every index in [0,MESSAGE_COUNT).
    PCollection<SequencedMessage> messages = readMessages(subscription, pipeline);
    PCollection<Integer> ids = messages.apply(MapElements.via(extractIds()));
    ids.apply("PubsubSignalTest", signal.signalSuccessWhen(BigEndianIntegerCoder.of(), testIds()));
    Supplier<Void> start = signal.waitForStart(Duration.standardMinutes(5));
    pipeline.apply(signal.signalStart());
    PipelineResult job = pipeline.run();
    start.get();
    LOG.info("Running!");
    signal.waitForSuccess(Duration.standardMinutes(5));
    // A runner may not support cancel
    try {
        job.cancel();
    } catch (UnsupportedOperationException exc) {
    // noop
    }
}
Also used : TopicPath(com.google.cloud.pubsublite.TopicPath) SubscriptionPath(com.google.cloud.pubsublite.SubscriptionPath) StreamingOptions(org.apache.beam.sdk.options.StreamingOptions) PipelineResult(org.apache.beam.sdk.PipelineResult) TestPipelineOptions(org.apache.beam.sdk.testing.TestPipelineOptions) SequencedMessage(com.google.cloud.pubsublite.proto.SequencedMessage) Test(org.junit.Test)

Example 5 with SequencedMessage

use of com.google.cloud.pubsublite.proto.SequencedMessage in project beam by apache.

the class UuidDeduplicationTransformTest method unrelatedUuidsProxied.

@Test
public void unrelatedUuidsProxied() {
    SequencedMessage message1 = newMessage();
    SequencedMessage message2 = newMessage();
    TestStream<SequencedMessage> messageStream = TestStream.create(ProtoCoder.of(SequencedMessage.class)).advanceWatermarkTo(START).addElements(message1).advanceWatermarkTo(START.plus(Deduplicate.DEFAULT_DURATION.dividedBy(2))).addElements(message2).advanceWatermarkToInfinity();
    PCollection<SequencedMessage> results = pipeline.apply(messageStream).apply(new UuidDeduplicationTransform(UuidDeduplicationOptions.newBuilder().build()));
    PAssert.that(results).containsInAnyOrder(message1, message2);
    pipeline.run();
}
Also used : SequencedMessage(com.google.cloud.pubsublite.proto.SequencedMessage) Test(org.junit.Test)

Aggregations

SequencedMessage (com.google.cloud.pubsublite.proto.SequencedMessage)8 Test (org.junit.Test)7 Instant (org.joda.time.Instant)4 PubSubMessage (com.google.cloud.pubsublite.proto.PubSubMessage)2 SubscriptionPath (com.google.cloud.pubsublite.SubscriptionPath)1 TopicPath (com.google.cloud.pubsublite.TopicPath)1 AttributeValues (com.google.cloud.pubsublite.proto.AttributeValues)1 Cursor (com.google.cloud.pubsublite.proto.Cursor)1 ByteString (com.google.protobuf.ByteString)1 Timestamps (com.google.protobuf.util.Timestamps)1 Nullable (javax.annotation.Nullable)1 PipelineResult (org.apache.beam.sdk.PipelineResult)1 ProtoCoder (org.apache.beam.sdk.extensions.protobuf.ProtoCoder)1 UuidDeduplicationOptions (org.apache.beam.sdk.io.gcp.pubsublite.UuidDeduplicationOptions)1 StreamingOptions (org.apache.beam.sdk.options.StreamingOptions)1 PAssert (org.apache.beam.sdk.testing.PAssert)1 TestPipeline (org.apache.beam.sdk.testing.TestPipeline)1 TestPipelineOptions (org.apache.beam.sdk.testing.TestPipelineOptions)1 TestStream (org.apache.beam.sdk.testing.TestStream)1 Deduplicate (org.apache.beam.sdk.transforms.Deduplicate)1