Search in sources :

Example 21 with SequencedMessage

use of com.google.cloud.pubsublite.SequencedMessage in project java-pubsublite by googleapis.

the class BlockingPullSubscriberImplTest method onDataSuccess.

@Test
public void onDataSuccess() throws Exception {
    SequencedMessage message = SequencedMessage.of(Message.builder().build(), Timestamps.EPOCH, Offset.of(12), 30);
    Future<?> future = executorService.submit(() -> subscriber.onData().get());
    messageConsumer.accept(ImmutableList.of(message));
    future.get();
}
Also used : SequencedMessage(com.google.cloud.pubsublite.SequencedMessage) Test(org.junit.Test)

Example 22 with SequencedMessage

use of com.google.cloud.pubsublite.SequencedMessage in project java-pubsublite by googleapis.

the class SinglePartitionSubscriber method onMessages.

@VisibleForTesting
void onMessages(List<SequencedMessage> sequencedMessages) {
    try {
        for (SequencedMessage message : sequencedMessages) {
            PubsubMessage userMessage = transformer.transform(message);
            long bytes = message.byteSize();
            Runnable trackerConsumer = ackSetTracker.track(message);
            AckReplyConsumer clientConsumer = new AckReplyConsumer() {

                @Override
                public void ack() {
                    if (terminated()) {
                        // Drop acks after shutdown
                        return;
                    }
                    trackerConsumer.run();
                    try {
                        wireSubscriber.allowFlow(FlowControlRequest.newBuilder().setAllowedMessages(1).setAllowedBytes(bytes).build());
                    } catch (CheckedApiException e) {
                        onPermanentError(e);
                    }
                }

                @Override
                public void nack() {
                    if (terminated()) {
                        // Drop nacks after shutdown to allow nacking from reassignment handler
                        return;
                    }
                    ApiFuture<Void> nackDone = nackHandler.nack(userMessage);
                    ApiFutures.addCallback(nackDone, new ApiFutureCallback<Void>() {

                        @Override
                        public void onFailure(Throwable t) {
                            onPermanentError(ExtractStatus.toCanonical(t));
                        }

                        @Override
                        public void onSuccess(Void result) {
                            ack();
                        }
                    }, SystemExecutors.getFuturesExecutor());
                }
            };
            receiver.receiveMessage(userMessage, clientConsumer);
        }
    } catch (Throwable t) {
        onPermanentError(ExtractStatus.toCanonical(t));
    }
}
Also used : SequencedMessage(com.google.cloud.pubsublite.SequencedMessage) CheckedApiException(com.google.cloud.pubsublite.internal.CheckedApiException) AckReplyConsumer(com.google.cloud.pubsub.v1.AckReplyConsumer) PubsubMessage(com.google.pubsub.v1.PubsubMessage) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 23 with SequencedMessage

use of com.google.cloud.pubsublite.SequencedMessage in project java-pubsublite by googleapis.

the class BlockingPullSubscriberImpl method messageIfAvailable.

@Override
public synchronized Optional<SequencedMessage> messageIfAvailable() throws CheckedApiException {
    if (error.isPresent()) {
        throw error.get();
    }
    if (messages.isEmpty()) {
        return Optional.empty();
    }
    SequencedMessage msg = messages.remove();
    underlying.allowFlow(FlowControlRequest.newBuilder().setAllowedMessages(1).setAllowedBytes(msg.byteSize()).build());
    return Optional.of(msg);
}
Also used : SequencedMessage(com.google.cloud.pubsublite.SequencedMessage)

Example 24 with SequencedMessage

use of com.google.cloud.pubsublite.SequencedMessage in project java-pubsublite-spark by googleapis.

the class PslSparkUtilsTest method testToInternalRow.

@Test
public void testToInternalRow() {
    Timestamp publishTimestamp = Timestamp.newBuilder().setSeconds(20000000L).setNanos(20).build();
    Timestamp eventTimestamp = Timestamp.newBuilder().setSeconds(10000000L).setNanos(10).build();
    Message message = Message.builder().setKey(ByteString.copyFromUtf8("key")).setData(ByteString.copyFromUtf8("data")).setEventTime(eventTimestamp).setAttributes(ImmutableListMultimap.of("key1", ByteString.copyFromUtf8("val1"), "key1", ByteString.copyFromUtf8("val2"), "key2", ByteString.copyFromUtf8("val3"))).build();
    SequencedMessage sequencedMessage = SequencedMessage.of(message, publishTimestamp, UnitTestExamples.exampleOffset(), 10L);
    InternalRow row = PslSparkUtils.toInternalRow(sequencedMessage, UnitTestExamples.exampleSubscriptionPath(), UnitTestExamples.examplePartition());
    assertThat(row.getString(0)).isEqualTo(UnitTestExamples.exampleSubscriptionPath().toString());
    assertThat(row.getLong(1)).isEqualTo(UnitTestExamples.examplePartition().value());
    assertThat(row.getLong(2)).isEqualTo(UnitTestExamples.exampleOffset().value());
    assertThat(row.getBinary(3)).isEqualTo("key".getBytes(StandardCharsets.UTF_8));
    assertThat(row.getBinary(4)).isEqualTo("data".getBytes(StandardCharsets.UTF_8));
    assertThat(row.getLong(5)).isEqualTo(Timestamps.toMicros(publishTimestamp));
    assertThat(row.getLong(6)).isEqualTo(Timestamps.toMicros(eventTimestamp));
    ArrayData keys = row.getMap(7).keyArray();
    ArrayData values = row.getMap(7).valueArray();
    assertThat(keys.get(0, DataTypes.StringType).toString()).isEqualTo("key1");
    assertThat(keys.get(1, DataTypes.StringType).toString()).isEqualTo("key2");
    GenericArrayData valueOfKey1 = (GenericArrayData) values.get(0, DataTypes.createArrayType(DataTypes.BinaryType));
    GenericArrayData valueOfKey2 = (GenericArrayData) values.get(1, DataTypes.createArrayType(DataTypes.BinaryType));
    assertThat(valueOfKey1.getBinary(0)).isEqualTo("val1".getBytes(StandardCharsets.UTF_8));
    assertThat(valueOfKey1.getBinary(1)).isEqualTo("val2".getBytes(StandardCharsets.UTF_8));
    assertThat(valueOfKey2.getBinary(0)).isEqualTo("val3".getBytes(StandardCharsets.UTF_8));
}
Also used : GenericArrayData(org.apache.spark.sql.catalyst.util.GenericArrayData) SequencedMessage(com.google.cloud.pubsublite.SequencedMessage) Message(com.google.cloud.pubsublite.Message) SequencedMessage(com.google.cloud.pubsublite.SequencedMessage) Timestamp(com.google.protobuf.Timestamp) InternalRow(org.apache.spark.sql.catalyst.InternalRow) GenericArrayData(org.apache.spark.sql.catalyst.util.GenericArrayData) ArrayData(org.apache.spark.sql.catalyst.util.ArrayData) Test(org.junit.Test)

Aggregations

SequencedMessage (com.google.cloud.pubsublite.SequencedMessage)24 Test (org.junit.Test)20 Partition (com.google.cloud.pubsublite.Partition)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 Offset (com.google.cloud.pubsublite.Offset)4 PubsubMessage (com.google.pubsub.v1.PubsubMessage)4 TopicPath (com.google.cloud.pubsublite.TopicPath)3 FlowControlRequest (com.google.cloud.pubsublite.proto.FlowControlRequest)3 ArrayList (java.util.ArrayList)3 ApiFuture (com.google.api.core.ApiFuture)2 ApiFutureCallback (com.google.api.core.ApiFutureCallback)2 ApiFutures (com.google.api.core.ApiFutures)2 SettableApiFuture (com.google.api.core.SettableApiFuture)2 CheckedApiException (com.google.cloud.pubsublite.internal.CheckedApiException)2 CloseableMonitor (com.google.cloud.pubsublite.internal.CloseableMonitor)2 ExtractStatus (com.google.cloud.pubsublite.internal.ExtractStatus)2 ApiServiceUtils.blockingShutdown (com.google.cloud.pubsublite.internal.wire.ApiServiceUtils.blockingShutdown)2 KafkaExceptionUtils.toKafka (com.google.cloud.pubsublite.kafka.KafkaExceptionUtils.toKafka)2 InitialSubscribeRequest (com.google.cloud.pubsublite.proto.InitialSubscribeRequest)2 SeekRequest (com.google.cloud.pubsublite.proto.SeekRequest)2