Skip to content

Add producer purge method with optional blocking argument #548

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

Merged
merged 14 commits into from
Oct 20, 2020
Merged
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
44 changes: 43 additions & 1 deletion confluent_kafka/src/Producer.c
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ static void dr_msg_cb (rd_kafka_t *rk, const rd_kafka_message_t *rkm,
goto done;

msgobj = Message_new0(self, rkm);

args = Py_BuildValue("(OO)", ((Message *)msgobj)->error, msgobj);

Py_DECREF(msgobj);
Expand Down Expand Up @@ -532,6 +532,36 @@ static PyObject *Producer_abort_transaction(Handle *self, PyObject *args) {
Py_RETURN_NONE;
}

static void *Producer_purge (Handle *self, PyObject *args,
Copy link
Contributor

Choose a reason for hiding this comment

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

I know the source is messed up tab/space-wise, but please try to use 8-space indents in new code, no tabs.
Thank you!

PyObject *kwargs) {
int in_queue = 1;
int in_flight = 1;
int blocking = 1;
int purge_strategy = 0;

rd_kafka_resp_err_t err;
static char *kws[] = { "in_queue", "in_flight", "blocking", NULL};

if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|bbb", kws, &in_queue, &in_flight, &blocking))
return NULL;
if (in_queue)
purge_strategy = RD_KAFKA_PURGE_F_QUEUE;
if (in_flight)
purge_strategy |= RD_KAFKA_PURGE_F_INFLIGHT;
if (blocking)
purge_strategy |= RD_KAFKA_PURGE_F_NON_BLOCKING;

err = rd_kafka_purge(self->rk, purge_strategy);

if (err) {
cfl_PyErr_Format(err, "Purge failed: %s", rd_kafka_err2str(err));
return NULL;
}

Py_RETURN_NONE;
}


static PyMethodDef Producer_methods[] = {
{ "produce", (PyCFunction)Producer_produce,
METH_VARARGS|METH_KEYWORDS,
Expand Down Expand Up @@ -597,6 +627,18 @@ static PyMethodDef Producer_methods[] = {
"callbacks may be triggered.\n"
"\n"
},
{ "purge", (PyCFunction)Producer_purge, METH_VARARGS|METH_KEYWORDS,
".. py:function:: purge([in_queue=True], [in_flight=True], [blocking=True])\n"
"\n"
" Purge messages currently handled by the producer instance.\n"
" The application will need to call poll() or flush() "
"afterwards to serve the delivery report callbacks of the purged messages."
"\n"
" :param: bool in_queue: Purge messages from internal queues. By default, true.\n"
" :param: bool in_flight: Purge messages in flight to or from the broker. By default, true.\n"
" :param: bool blocking: If set to False, will not wait on background thread queue\n"
"purging to finish. By default, true."
},
{ "list_topics", (PyCFunction)list_topics, METH_VARARGS|METH_KEYWORDS,
list_topics_doc
},
Expand Down
35 changes: 35 additions & 0 deletions tests/test_Producer.py
Original file line number Diff line number Diff line change
Expand Up @@ -235,3 +235,38 @@ def test_transaction_api():
assert ex.value.args[0].retriable() is False
assert ex.value.args[0].fatal() is False
assert ex.value.args[0].txn_requires_abort() is False


def test_purge():
"""
Verify that when we have a higher message.timeout.ms timeout, we can use purge()
to stop waiting for messages and get delivery reports
"""
p = Producer(
{"socket.timeout.ms": 10, "error_cb": error_cb, "message.timeout.ms": 30000}
) # 30 seconds

# Hack to detect on_delivery was called because inner functions can modify nonlocal objects.
# When python2 support is dropped, we can use the "nonlocal" keyword instead
cb_detector = {"on_delivery_called": False}

def on_delivery(err, msg):
cb_detector["on_delivery_called"] = True
# Because we are purging messages, we should see a PURGE_QUEUE kafka error
assert err.code() == KafkaError._PURGE_QUEUE

# Our message won't be delivered, but also won't timeout yet because our timeout is 30s.
p.produce(topic="some_topic", value="testing", partition=9, callback=on_delivery)
p.flush(0.002)
assert not cb_detector["on_delivery_called"]

# When in_queue set to false, we won't purge the message and get delivery callback
p.purge(in_queue=False)
p.flush(0.002)
assert not cb_detector["on_delivery_called"]

# When we purge including the queue, the message should have delivered a delivery report
# with a PURGE_QUEUE error
p.purge()
p.flush(0.002)
assert cb_detector["on_delivery_called"]