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();
}
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));
}
}
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);
}
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));
}
Aggregations