Search in sources :

Example 1 with TopicPath

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

the class ReadWriteIT method createTopic.

private TopicPath createTopic(ProjectId id) throws Exception {
    TopicPath toReturn = TopicPath.newBuilder().setProject(id).setLocation(ZONE).setName(TopicName.of(randomName())).build();
    Topic.Builder topic = Topic.newBuilder().setName(toReturn.toString());
    topic.getPartitionConfigBuilder().setCount(2).setCapacity(Capacity.newBuilder().setPublishMibPerSec(4).setSubscribeMibPerSec(4));
    topic.getRetentionConfigBuilder().setPerPartitionBytes(30 * (1L << 30));
    cleanupActions.addLast(() -> {
        try (AdminClient client = newAdminClient()) {
            client.deleteTopic(toReturn).get();
        } catch (Throwable t) {
            LOG.error("Failed to clean up topic.", t);
        }
    });
    try (AdminClient client = newAdminClient()) {
        client.createTopic(topic.build()).get();
    }
    return toReturn;
}
Also used : TopicPath(com.google.cloud.pubsublite.TopicPath) Topic(com.google.cloud.pubsublite.proto.Topic) AdminClient(com.google.cloud.pubsublite.AdminClient)

Example 2 with TopicPath

use of com.google.cloud.pubsublite.TopicPath 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)

Aggregations

TopicPath (com.google.cloud.pubsublite.TopicPath)2 AdminClient (com.google.cloud.pubsublite.AdminClient)1 SubscriptionPath (com.google.cloud.pubsublite.SubscriptionPath)1 SequencedMessage (com.google.cloud.pubsublite.proto.SequencedMessage)1 Topic (com.google.cloud.pubsublite.proto.Topic)1 PipelineResult (org.apache.beam.sdk.PipelineResult)1 StreamingOptions (org.apache.beam.sdk.options.StreamingOptions)1 TestPipelineOptions (org.apache.beam.sdk.testing.TestPipelineOptions)1 Test (org.junit.Test)1