Skip to content

Commit

Permalink
[client][python] getLastMessageIdAsync C binding (#16255)
Browse files Browse the repository at this point in the history
* python cc binding for getLastMessageId

* add python Consumer class method and doc

* fix linter issues based on clang-format

* ubuntu linter fix

* try run unit test in ci

* fix doc comment

* test the test case can be ran

### Motivation

Python function getLastMessageId

It is a C binding for #16182 to implement get_last_message_id() in Python client.

### Modifications

Add Python/C binding code for get_last_message_id()

### Verifying this change

It compiles.
- [x] Make sure that the change passes the CI checks.

This change is a trivial rework / code cleanup without any test coverage.


### Does this pull request potentially affect one of the following parts:

*If `yes` was chosen, please highlight the changes*

  - Dependencies (does it add or upgrade a dependency): (no)
  - The public API: (yes)
  - The schema: (no)
  - The default values of configurations: (no)
  - The wire protocol: (no)
  - The rest endpoints: (no)
  - The admin cli options: (no)
  - Anything that affects deployment: (no)

### Documentation

Check the box below or label this PR directly.

Need to update docs? 

- [ ] `doc-required` 
(Your PR needs to update docs and you will update later)
  
- [ ] `doc-not-needed` 

  
- [x] `doc` 
Python Doc is updated in __init__.py

- [ ] `doc-complete`
(Docs have been already added)
  • Loading branch information
komalatammal authored and Technoboy- committed Aug 10, 2022
1 parent a553165 commit a5e0c58
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 3 deletions.
2 changes: 1 addition & 1 deletion pulsar-client-cpp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -281,7 +281,7 @@ ${PULSAR_PATH}/pulsar-test-service-stop.sh

## Requirements for Contributors

It's recommended to install [LLVM](https://llvm.org/builds/) for `clang-tidy` and `clang-format`. Pulsar C++ client use `clang-format` 6.0+ to format files.
It's required to install [LLVM](https://llvm.org/builds/) for `clang-tidy` and `clang-format`. Pulsar C++ client use `clang-format` 6.0+ to format files. `make format` automatically formats the files.

Use `pulsar-client-cpp/docker-format.sh` to ensure the C++ sources are correctly formatted.

Expand Down
3 changes: 3 additions & 0 deletions pulsar-client-cpp/include/pulsar/c/consumer.h
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,9 @@ PULSAR_PUBLIC pulsar_result pulsar_consumer_seek(pulsar_consumer_t *consumer, pu

PULSAR_PUBLIC int pulsar_consumer_is_connected(pulsar_consumer_t *consumer);

PULSAR_PUBLIC pulsar_result pulsar_consumer_get_last_message_id(pulsar_consumer_t *consumer,
pulsar_message_id_t *messageId);

#ifdef __cplusplus
}
#endif
5 changes: 5 additions & 0 deletions pulsar-client-cpp/lib/c/c_Consumer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,3 +143,8 @@ pulsar_result pulsar_consumer_seek(pulsar_consumer_t *consumer, pulsar_message_i
}

int pulsar_consumer_is_connected(pulsar_consumer_t *consumer) { return consumer->consumer.isConnected(); }

pulsar_result pulsar_consumer_get_last_message_id(pulsar_consumer_t *consumer,
pulsar_message_id_t *messageId) {
return (pulsar_result)consumer->consumer.getLastMessageId(messageId->messageId);
}
7 changes: 6 additions & 1 deletion pulsar-client-cpp/python/pulsar/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -1253,7 +1253,12 @@ def is_connected(self):
Check if the consumer is connected or not.
"""
return self._consumer.is_connected()


def get_last_message_id(self):
"""
Get the last message id.
"""
return self._consumer.get_last_message_id()


class Reader:
Expand Down
12 changes: 12 additions & 0 deletions pulsar-client-cpp/python/pulsar_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -753,6 +753,18 @@ def test_reader_argument_errors(self):
self._check_value_error(lambda: client.create_reader(topic, MessageId.earliest, reader_name=5))
client.close()

def test_get_last_message_id(self):
client = Client(self.serviceUrl)
consumer = client.subscribe(
"persistent://public/default/topic_name_test", "topic_name_test_sub", consumer_type=ConsumerType.Shared
)
producer = client.create_producer("persistent://public/default/topic_name_test")
msg_id = producer.send(b"hello")

msg = consumer.receive(TM)
self.assertEqual(msg.message_id(), msg_id)
client.close()

def test_publish_compact_and_consume(self):
client = Client(self.serviceUrl)
topic = "compaction_%s" % (uuid.uuid4())
Expand Down
13 changes: 12 additions & 1 deletion pulsar-client-cpp/python/src/consumer.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,16 @@ void Consumer_seek_timestamp(Consumer& consumer, uint64_t timestamp) {

bool Consumer_is_connected(Consumer& consumer) { return consumer.isConnected(); }

MessageId Consumer_get_last_message_id(Consumer& consumer) {
MessageId msgId;
Result res;
Py_BEGIN_ALLOW_THREADS res = consumer.getLastMessageId(msgId);
Py_END_ALLOW_THREADS

CHECK_RESULT(res);
return msgId;
}

void export_consumer() {
using namespace boost::python;

Expand All @@ -105,5 +115,6 @@ void export_consumer() {
.def("redeliver_unacknowledged_messages", &Consumer::redeliverUnacknowledgedMessages)
.def("seek", &Consumer_seek)
.def("seek", &Consumer_seek_timestamp)
.def("is_connected", &Consumer_is_connected);
.def("is_connected", &Consumer_is_connected)
.def("get_last_message_id", &Consumer_get_last_message_id);
}

0 comments on commit a5e0c58

Please sign in to comment.