Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support rd_kafka_memberid() from python clients #1154

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
# Confluent's Python client for Apache Kafka

## v1.8.0

v1.8.0 is a maintenance release with the following fixes and enhancements:

### Enhancements

- Support rd_kafka_memberid() from python clients (#1154).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't expose the librdkafka name here, but the python one, so something like:
- Added consumer.memberid() method to retrieve the consumer's group member id (#1154)


confluent-kafka-python is based on librdkafka v1.8.0, see the
[librdkafka release notes](https://github.com/edenhill/librdkafka/releases/tag/v1.8.0)
for a complete list of changes, enhancements, fixes and upgrade considerations.


## v1.7.0

v1.7.0 is a maintenance release with the following fixes and enhancements:
Expand Down
35 changes: 35 additions & 0 deletions src/confluent_kafka/src/Consumer.c
Original file line number Diff line number Diff line change
Expand Up @@ -980,6 +980,28 @@ static PyObject *Consumer_poll (Handle *self, PyObject *args,
}


static PyObject *Consumer_memberid (Handle *self, PyObject *args,
PyObject *kwargs) {
char *memberid;
PyObject *memberidobj;
if (!self->rk) {
PyErr_SetString(PyExc_RuntimeError,
"Consumer closed");
return NULL;
}

memberid = rd_kafka_memberid(self->rk);

if (!memberid)
Py_RETURN_NONE;

memberidobj = Py_BuildValue("s", memberid);
rd_kafka_mem_free(self->rk, memberid);

return memberidobj;
jliunyu marked this conversation as resolved.
Show resolved Hide resolved
}


static PyObject *Consumer_consume (Handle *self, PyObject *args,
PyObject *kwargs) {
unsigned int num_messages = 1;
Expand Down Expand Up @@ -1409,6 +1431,19 @@ static PyMethodDef Consumer_methods[] = {
" :raises: RuntimeError if called on a closed consumer\n"
"\n"
},
{ "memberid", (PyCFunction)Consumer_memberid, METH_NOARGS,
".. py:function:: memberid()\n"
"\n"
" Return this client's broker-assigned group member id.\n"
"\n"
" The member id is assigned by the group coordinator and"
" is propagated to the consumer during rebalance.\n"
"\n"
" :returns: Member id string or None\n"
" :rtype: string\n"
" :raises: RuntimeError if called on a closed consumer\n"
"\n"
},
{ "close", (PyCFunction)Consumer_close, METH_NOARGS,
"\n"
" Close down and terminate the Kafka Consumer.\n"
Expand Down
42 changes: 42 additions & 0 deletions tests/integration/consumer/test_consumer_memberid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# Copyright 2021 Confluent Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limit


def test_consumer_memberid(kafka_cluster):
"""
Test consumer memberid.
"""

consumer_conf = {'group.id': 'test'}

topic = "testmemberid"

kafka_cluster.create_topic(topic)

consumer = kafka_cluster.consumer(consumer_conf)

jliunyu marked this conversation as resolved.
Show resolved Hide resolved
assert consumer is not None
assert len(consumer.memberid()) == 0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you should check that the memberid is None rather than have a 0 length (which could be an empty string or some other type).

Copy link
Contributor Author

@jliunyu jliunyu Jul 12, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hi Magnus, thanks for reviewing.

I found that the consumer.memberid() is "" here when I do the test. But do you think None should be the expected result or "" is also Okay?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. I think it should be None as that seems more Pythonic.

kafka_cluster.seed_topic(topic, value_source=[b'memberid'])

consumer.subscribe([topic])
msg = consumer.poll(10)
assert msg is not None
assert msg.value() == b'memberid'
assert isinstance(consumer.memberid(), str) is True
assert len(consumer.memberid()) > 0
jliunyu marked this conversation as resolved.
Show resolved Hide resolved
consumer.close()