eolivelli commented on a change in pull request #10498:
URL: https://github.com/apache/pulsar/pull/10498#discussion_r632314094
##########
File path:
pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/ContextImpl.java
##########
@@ -706,4 +714,67 @@ public void close() {
logger.warn("Failed to close producers", e);
}
}
+
+ @Override
+ public void seek(String topic, int partition, MessageId messageId) throws
PulsarClientException {
+ Consumer<?> consumer = getConsumer(topic, partition);
+ consumer.seek(messageId);
+ }
+
+ @Override
+ public void pause(String topic, int partition) throws
PulsarClientException {
+ getConsumer(topic, partition).pause();
+ }
+
+ @Override
+ public void resume(String topic, int partition) throws
PulsarClientException {
+ getConsumer(topic, partition).resume();
+ }
+
+ public void setInputConsumers(List<Consumer<?>> inputConsumers) {
+ this.inputConsumers = inputConsumers;
+ inputConsumers.stream()
+ .flatMap(consumer ->
+ consumer instanceof MultiTopicsConsumerImpl
+ ? ((MultiTopicsConsumerImpl<?>)
consumer).getConsumers().stream()
+ : Stream.of(consumer))
+ .forEach(consumer ->
topicConsumers.putIfAbsent(TopicName.get(consumer.getTopic()), consumer));
+ }
+
+ @VisibleForTesting
+ Consumer<?> getConsumer(String topic, int partition) throws
PulsarClientException {
+ if (inputConsumers == null) {
+ throw new PulsarClientException("Getting consumer is not
supported");
+ }
+ for (int i = 0; i < 2; i++) {
+ Consumer<?> consumer =
topicConsumers.get(TopicName.get(topic).getPartition(partition));
+
+ if (consumer != null) {
+ return consumer;
+ }
+
+ if (partition == 0) {
+ consumer = topicConsumers.get(TopicName.get(topic));
+
+ if (consumer != null) {
+ return consumer;
+ }
+ }
+
+ if (i == 0) {
Review comment:
I am not sure I understand why in this "get" method we are modifying the
internal `inputConsumers` variable.
##########
File path:
pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/ContextImpl.java
##########
@@ -706,4 +714,67 @@ public void close() {
logger.warn("Failed to close producers", e);
}
}
+
+ @Override
+ public void seek(String topic, int partition, MessageId messageId) throws
PulsarClientException {
+ Consumer<?> consumer = getConsumer(topic, partition);
+ consumer.seek(messageId);
+ }
+
+ @Override
+ public void pause(String topic, int partition) throws
PulsarClientException {
+ getConsumer(topic, partition).pause();
+ }
+
+ @Override
+ public void resume(String topic, int partition) throws
PulsarClientException {
+ getConsumer(topic, partition).resume();
+ }
+
+ public void setInputConsumers(List<Consumer<?>> inputConsumers) {
Review comment:
missing Override ?
##########
File path:
pulsar-functions/instance/src/main/java/org/apache/pulsar/functions/instance/ContextImpl.java
##########
@@ -706,4 +714,67 @@ public void close() {
logger.warn("Failed to close producers", e);
}
}
+
+ @Override
+ public void seek(String topic, int partition, MessageId messageId) throws
PulsarClientException {
+ Consumer<?> consumer = getConsumer(topic, partition);
+ consumer.seek(messageId);
+ }
+
+ @Override
+ public void pause(String topic, int partition) throws
PulsarClientException {
+ getConsumer(topic, partition).pause();
+ }
+
+ @Override
+ public void resume(String topic, int partition) throws
PulsarClientException {
+ getConsumer(topic, partition).resume();
+ }
+
+ public void setInputConsumers(List<Consumer<?>> inputConsumers) {
+ this.inputConsumers = inputConsumers;
+ inputConsumers.stream()
+ .flatMap(consumer ->
+ consumer instanceof MultiTopicsConsumerImpl
+ ? ((MultiTopicsConsumerImpl<?>)
consumer).getConsumers().stream()
+ : Stream.of(consumer))
+ .forEach(consumer ->
topicConsumers.putIfAbsent(TopicName.get(consumer.getTopic()), consumer));
+ }
+
+ @VisibleForTesting
+ Consumer<?> getConsumer(String topic, int partition) throws
PulsarClientException {
+ if (inputConsumers == null) {
+ throw new PulsarClientException("Getting consumer is not
supported");
+ }
+ for (int i = 0; i < 2; i++) {
+ Consumer<?> consumer =
topicConsumers.get(TopicName.get(topic).getPartition(partition));
+
+ if (consumer != null) {
+ return consumer;
+ }
+
+ if (partition == 0) {
+ consumer = topicConsumers.get(TopicName.get(topic));
+
+ if (consumer != null) {
+ return consumer;
+ }
+ }
+
+ if (i == 0) {
+ // MultiTopicsConsumer's list of consumers could change
+ // if partitions changed or pattern(s) used to subscribe
+ inputConsumers.stream()
Review comment:
what about moving this duplicated code to a common method ?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
For queries about this service, please contact Infrastructure at:
[email protected]