Skip to content
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
614 changes: 358 additions & 256 deletions src/confluent_kafka/src/Admin.c

Large diffs are not rendered by default.

203 changes: 59 additions & 144 deletions src/confluent_kafka/src/Consumer.c

Large diffs are not rendered by default.

25 changes: 13 additions & 12 deletions src/confluent_kafka/src/Metadata.c
Original file line number Diff line number Diff line change
Expand Up @@ -366,18 +366,17 @@ PyObject *list_topics(Handle *self, PyObject *args, PyObject *kwargs) {
&tmout))
return NULL;

if (!self->rk) {
PyErr_SetString(PyExc_RuntimeError, ERR_MSG_HANDLE_CLOSED);
if (!Handle_common_enter(self))
return NULL;
}

if (topic != NULL) {
if (!(only_rkt = rd_kafka_topic_new(self->rk, topic, NULL))) {
return PyErr_Format(
PyExc_RuntimeError,
"Unable to create topic object "
"for \"%s\": %s",
topic, rd_kafka_err2str(rd_kafka_last_error()));
PyErr_Format(PyExc_RuntimeError,
"Unable to create topic object "
"for \"%s\": %s",
topic,
rd_kafka_err2str(rd_kafka_last_error()));
goto end; /* result and only_rkt are NULL */
}
}

Expand Down Expand Up @@ -409,6 +408,8 @@ PyObject *list_topics(Handle *self, PyObject *args, PyObject *kwargs) {
rd_kafka_topic_destroy(only_rkt);
}

Handle_common_exit(self);

return result;
}

Expand Down Expand Up @@ -608,7 +609,8 @@ PyObject *list_groups(Handle *self, PyObject *args, PyObject *kwargs) {
const struct rd_kafka_group_list *group_list = NULL;
const char *group = NULL;
double tmout = -1.0f;
static char *kws[] = {"group", "timeout", NULL};
static char *kws[] = {"group", "timeout",
NULL};

PyErr_WarnEx(PyExc_DeprecationWarning,
"list_groups() is deprecated, use list_consumer_groups() "
Expand All @@ -619,10 +621,8 @@ PyObject *list_groups(Handle *self, PyObject *args, PyObject *kwargs) {
&tmout))
return NULL;

if (!self->rk) {
PyErr_SetString(PyExc_RuntimeError, ERR_MSG_HANDLE_CLOSED);
if (!Handle_common_enter(self))
return NULL;
}

CallState_begin(self, &cs);

Expand All @@ -645,6 +645,7 @@ PyObject *list_groups(Handle *self, PyObject *args, PyObject *kwargs) {
if (group_list != NULL) {
rd_kafka_group_list_destroy(group_list);
}
Handle_common_exit(self);
return result;
}

Expand Down
67 changes: 30 additions & 37 deletions src/confluent_kafka/src/Producer.c
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
*
****************************************************************************/

static int Producer_rk_use_begin(Handle *self) {
return Handle_rk_use_begin(self, ERR_MSG_PRODUCER_CLOSED);
}


/**
* Per-message state.
*/
Expand Down Expand Up @@ -302,7 +307,7 @@
if (!dr_cb || dr_cb == Py_None)
dr_cb = self->u.Producer.default_dr_cb;

if (!Handle_enter_rk_use(self)) {
if (!Producer_rk_use_begin(self)) {
#ifdef RD_KAFKA_V_HEADERS
if (rd_headers)
rd_kafka_headers_destroy(rd_headers);
Expand All @@ -328,7 +333,7 @@
key_len, msgstate);
#endif

Handle_exit_rk_use(self);
Handle_rk_use_end(self);

if (err) {
if (msgstate)
Expand Down Expand Up @@ -437,12 +442,12 @@
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|d", kws, &tmout))
return NULL;

if (!Handle_enter_rk_use(self))
if (!Producer_rk_use_begin(self))
return NULL;

r = Producer_poll0(self, cfl_timeout_ms(tmout));

Handle_exit_rk_use(self);
Handle_rk_use_end(self);

if (r == -1)
return NULL;
Expand Down Expand Up @@ -472,7 +477,7 @@
* (raises KeyboardInterrupt if signal detected)
*/
static PyObject *
Producer_flush(Handle *self, PyObject *args, PyObject *kwargs) {

Check failure on line 480 in src/confluent_kafka/src/Producer.c

View check run for this annotation

SonarQube-Confluent / SonarQube Code Analysis

Refactor this function to reduce its Cognitive Complexity from 26 to the 25 allowed.

[S3776] Cognitive Complexity of functions should not be too high See more on https://sonarqube.confluent.io/project/issues?id=confluent-kafka-python&pullRequest=2346&issues=18e9a84b-8293-4b2b-b7ac-159905314a79&open=18e9a84b-8293-4b2b-b7ac-159905314a79
double tmout = -1;
int qlen = 0;
static char *kws[] = {"timeout", NULL};
Expand All @@ -487,7 +492,7 @@
if (!PyArg_ParseTupleAndKeywords(args, kwargs, "|d", kws, &tmout))
return NULL;

if (!Handle_enter_rk_use(self))
if (!Producer_rk_use_begin(self))
return NULL;

total_timeout_ms = cfl_timeout_ms(tmout);
Expand All @@ -506,7 +511,7 @@
* means all messages that were queued at that point have been
* delivered. Note: Messages produced after flush() starts are
* not included in the current flush. */
while (1) {

Check warning on line 514 in src/confluent_kafka/src/Producer.c

View check run for this annotation

SonarQube-Confluent / SonarQube Code Analysis

Reduce the number of nested "break" and "goto" statements from 5 to 1 authorized.

[S924] Loops should not have more than one "break" or "goto" statement See more on https://sonarqube.confluent.io/project/issues?id=confluent-kafka-python&pullRequest=2346&issues=c8f820d9-3026-4834-93c4-f94fd97726cc&open=c8f820d9-3026-4834-93c4-f94fd97726cc
/* Calculate timeout for this chunk */
chunk_timeout_ms = calculate_chunk_timeout(
total_timeout_ms, chunk_count, CHUNK_TIMEOUT_MS);
Expand Down Expand Up @@ -556,7 +561,7 @@
result = cfl_PyInt_FromInt(qlen);

exit:
Handle_exit_rk_use(self);
Handle_rk_use_end(self);
return result;
}

Expand All @@ -583,13 +588,7 @@
*/
if (!atomic_int_cas(&self->closing, 0, 1)) {
while (self->rk && atomic_int_get(&self->closing)) {
CallState_begin(self, &cs);
#ifdef _WIN32
Sleep(100);
#else
usleep(100000);
#endif
if (!CallState_end(self, &cs))
if (!Handle_sleep(self, 100))
return NULL;
}
if (!self->rk)
Expand All @@ -602,25 +601,19 @@
return NULL;
}

/* Record which thread won, so Handle_enter_rk_use() can let a
/* Record which thread won, so Handle_rk_use_begin() can let a
* reentrant call through if (and only if) it's this same thread --
* i.e. close()'s own delivery callback, fired synchronously from
* inside the flush() call below, calling back into the Producer. */
atomic_ulong_set(&self->closing_thread, PyThread_get_thread_ident());

/* Signal in-flight calls to stop, and wait for them to finish
* using self->rk before destroying it -- see Handle_enter_rk_use().
* using self->rk before destroying it -- see Handle_rk_use_begin().
* New calls will see `closing` and fail with ERR_MSG_PRODUCER_CLOSED. */
/* TODO NOGIL: replace this poll loop with a mutex/condvar wait so
* close() unblocks immediately instead of up to 100ms late. */
while (atomic_int_get(&self->active_calls) > 0) {
CallState_begin(self, &cs);
#ifdef _WIN32
Sleep(100);
#else
usleep(100000);
#endif
if (!CallState_end(self, &cs)) {
if (!Handle_sleep(self, 100)) {
/* Abort the attempt: rk was never touched, so
* reopen the gate for a future close() attempt.
*/
Expand Down Expand Up @@ -927,7 +920,7 @@
return cfl_PyInt_FromInt(0);
}

if (!Handle_enter_rk_use(self))
if (!Producer_rk_use_begin(self))
return NULL;

/* Allocate arrays for librdkafka messages and msgstates */
Expand Down Expand Up @@ -960,7 +953,7 @@
if (rkt)
rd_kafka_topic_destroy(rkt);

Handle_exit_rk_use(self);
Handle_rk_use_end(self);

/* Cleanup resources not tied to self->rk */
if (rkmessages)
Expand All @@ -983,7 +976,7 @@
if (!PyArg_ParseTuple(args, "|d", &tmout))
return NULL;

if (!Handle_enter_rk_use(self))
if (!Producer_rk_use_begin(self))
return NULL;

CallState_begin(self, &cs);
Expand All @@ -1006,19 +999,19 @@
Py_INCREF(result);

exit:
Handle_exit_rk_use(self);
Handle_rk_use_end(self);
return result;
}

static PyObject *Producer_begin_transaction(Handle *self) {
rd_kafka_error_t *error;

if (!Handle_enter_rk_use(self))
if (!Producer_rk_use_begin(self))
return NULL;

error = rd_kafka_begin_transaction(self->rk);

Handle_exit_rk_use(self);
Handle_rk_use_end(self);

if (error) {
cfl_PyErr_from_error_destroy(error);
Expand All @@ -1041,7 +1034,7 @@
if (!PyArg_ParseTuple(args, "OO|d", &offsets, &metadata, &tmout))
return NULL;

if (!Handle_enter_rk_use(self))
if (!Producer_rk_use_begin(self))
return NULL;

if (!(c_offsets = py_to_c_parts(offsets)))
Expand Down Expand Up @@ -1075,7 +1068,7 @@
rd_kafka_consumer_group_metadata_destroy(cgmd);
if (c_offsets)
rd_kafka_topic_partition_list_destroy(c_offsets);
Handle_exit_rk_use(self);
Handle_rk_use_end(self);
return result;
}

Expand All @@ -1088,7 +1081,7 @@
if (!PyArg_ParseTuple(args, "|d", &tmout))
return NULL;

if (!Handle_enter_rk_use(self))
if (!Producer_rk_use_begin(self))
return NULL;

CallState_begin(self, &cs);
Expand All @@ -1111,7 +1104,7 @@
Py_INCREF(result);

exit:
Handle_exit_rk_use(self);
Handle_rk_use_end(self);
return result;
}

Expand All @@ -1126,7 +1119,7 @@

/* abort_transaction is called as part of close() so even if this call
* is rejected here (because a close is in progress), it is safe.*/
if (!Handle_enter_rk_use(self))
if (!Producer_rk_use_begin(self))
return NULL;

CallState_begin(self, &cs);
Expand All @@ -1149,7 +1142,7 @@
Py_INCREF(result);

exit:
Handle_exit_rk_use(self);
Handle_rk_use_end(self);
return result;
}

Expand All @@ -1166,7 +1159,7 @@
&in_flight, &blocking))
return NULL;

if (!Handle_enter_rk_use(self))
if (!Producer_rk_use_begin(self))
return NULL;

if (in_queue)
Expand All @@ -1178,7 +1171,7 @@

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

Handle_exit_rk_use(self);
Handle_rk_use_end(self);

if (err) {
cfl_PyErr_Format(err, "Purge failed: %s",
Expand Down Expand Up @@ -1537,7 +1530,7 @@
static Py_ssize_t Producer__len__(Handle *self) {
Py_ssize_t len;

/* __len__ must never raise, so we can't use Handle_enter_rk_use()
/* __len__ must never raise, so we can't use Handle_rk_use_begin()
* (which sets an exception on failure) -- fall back to returning 0,
* , if the Handle is closed/closing. */
if (atomic_int_get(&self->closing) || !self->rk)
Expand Down
Loading
Loading