Nilorea Library
C utilities for networking, threading, graphics
Loading...
Searching...
No Matches
n_kafka.h
Go to the documentation of this file.
1/*
2 * Nilorea Library
3 * Copyright (C) 2005-2026 Castagnier Mickael
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
14 * implied. See the License for the specific language governing
15 * permissions and limitations under the License.
16 *
17 * SPDX-License-Identifier: Apache-2.0
18 */
19
28#ifndef __N_KAFKA
29#define __N_KAFKA
30
31#ifdef __cplusplus
32extern "C" {
33#endif
34
40#include "nilorea/n_log.h"
41#include "nilorea/n_network.h"
42#include "cJSON.h"
43
44#include <stdio.h>
45#include <stdlib.h>
46#include <string.h>
47#include <locale.h>
48#include <libgen.h>
49#include <errno.h>
50#include <time.h>
51#include <unistd.h>
52
53#include "rdkafka.h"
54
56#define N_KAFKA_EVENT_QUEUED 0
58#define N_KAFKA_EVENT_WAITING_ACK 1
60#define N_KAFKA_EVENT_ERROR 2
62#define N_KAFKA_EVENT_OK 4
64#define N_KAFKA_EVENT_CREATED 5
65
87
151
153int32_t n_kafka_get_schema_from_char(const char* string);
155int32_t n_kafka_get_schema_from_nstr(const N_STR* string);
157int n_kafka_put_schema_in_char(char* string, int schema_id);
159int n_kafka_put_schema_in_nstr(N_STR* string, int schema_id);
160
162int n_kafka_get_status(N_KAFKA* kafka, size_t* nb_queued, size_t* nb_waiting, size_t* nb_error);
164void n_kafka_delete(N_KAFKA* kafka);
166N_KAFKA* n_kafka_new(int32_t poll_timeout, int32_t poll_interval, size_t errstr_len);
168int n_kafka_set_error_timeout(N_KAFKA* kafka, int32_t error_timeout);
170int n_kafka_set_sended_dir(N_KAFKA* kafka, const char* tosend_dir, const char* sended_dir);
173
175int n_kafka_new_headers(N_KAFKA_EVENT* event, size_t count);
177int n_kafka_add_header_ex(N_KAFKA_EVENT* event, char* key, size_t key_length, char* value, size_t value_length);
179int n_kafka_add_header(N_KAFKA_EVENT* event, N_STR* key, N_STR* value);
180
182int n_kafka_produce(N_KAFKA* kafka, N_KAFKA_EVENT* event);
183
185N_KAFKA_EVENT* n_kafka_new_event(int schema_id);
187N_KAFKA_EVENT* n_kafka_new_event_from_char(const char* string, size_t written, int schema_id);
189N_KAFKA_EVENT* n_kafka_new_event_from_string(const N_STR* string, int schema_id);
191N_KAFKA_EVENT* n_kafka_new_event_from_file(char* filename, int schema_id);
193void n_kafka_event_destroy_ptr(void* event);
196
198int n_kafka_poll(N_KAFKA* kafka);
212int n_kafka_dump_unprocessed(N_KAFKA* kafka, char* directory);
214int n_kafka_load_unprocessed(N_KAFKA* kafka, const char* directory);
215
218
223#ifdef __cplusplus
224}
225#endif
226
227#endif // header guard
static int mode
char * config_file
Definition ex_kafka.c:46
char * key
Structure of a generic LIST container.
Definition n_list.h:59
int32_t event_production_enabled
bool flag to suspend or restart event production
Definition n_kafka.h:141
int32_t poll_interval
poll interval in msecs
Definition n_kafka.h:125
rd_kafka_topic_partition_list_t * subscription
subscribed topics
Definition n_kafka.h:101
int polling_thread_status
polling thread status, 0 => off , 1 => on , 2 => wants to stop, will be turned out to 0 by exiting po...
Definition n_kafka.h:129
cJSON * configuration
kafka json configuration holder
Definition n_kafka.h:121
size_t nb_waiting
number of events waiting for an ack in the waiting list
Definition n_kafka.h:135
int mode
kafka handle mode: RD_KAFKA_CONSUMER or RD_KAFKA_PRODUCER
Definition n_kafka.h:119
char ** topics
list of topics to subscribe to
Definition n_kafka.h:97
int schema_id
kafka schema_id
Definition n_kafka.h:81
rd_kafka_topic_t * rd_kafka_topic
kafka topic handle
Definition n_kafka.h:111
rd_kafka_headers_t * rd_kafka_headers
kafka produce event headers structure handle
Definition n_kafka.h:77
int32_t error_timeout
retry interval in seconds for events stuck in N_KAFKA_EVENT_ERROR.
Definition n_kafka.h:143
int is_transactional
TRUE when a 'transactional.id' is set in the producer config: produces are then wrapped in kafka tran...
Definition n_kafka.h:145
pthread_rwlock_t rwlock
access lock
Definition n_kafka.h:117
N_STR * errstr
kafka error string holder
Definition n_kafka.h:113
time_t error_time
wall-clock time at which status flipped to N_KAFKA_EVENT_ERROR.
Definition n_kafka.h:85
rd_kafka_conf_t * rd_kafka_conf
kafka structure handle
Definition n_kafka.h:103
LIST * received_events
list of received N_KAFKA_EVENT
Definition n_kafka.h:93
LIST * events_to_send
list of N_KAFKA_EVENT to send
Definition n_kafka.h:91
int32_t poll_timeout
poll timeout in msecs
Definition n_kafka.h:123
pthread_t polling_thread
polling thread id
Definition n_kafka.h:131
char * sended_dir
base directory produced event files are moved to once acknowledged ('sended').
Definition n_kafka.h:149
rd_kafka_t * rd_kafka_handle
kafka handle (producer or consumer)
Definition n_kafka.h:105
LIST * received_headers
kafka consume event headers structure handle
Definition n_kafka.h:79
N_STR * event_string
string containing the topic id + payload
Definition n_kafka.h:69
char * event_cmd
eventual custom event_cmd
Definition n_kafka.h:99
int32_t monitored_directory_interval
monitored directory refresh interval in msecs
Definition n_kafka.h:127
char * tosend_dir
base directory the produced event files are read from ('to-send').
Definition n_kafka.h:147
char * groupid
consumer group id
Definition n_kafka.h:95
N_STR * event_files_to_delete
string containing the original event source file name if it is to be deleted when event is produced.
Definition n_kafka.h:71
int32_t event_consumption_enabled
bool flag to suspend or restart event consumption
Definition n_kafka.h:139
int schema_id
kafka schema id in network order
Definition n_kafka.h:115
struct N_KAFKA * parent_table
access lock
Definition n_kafka.h:83
size_t nb_queued
number of waiting events in the producer waiting list
Definition n_kafka.h:133
size_t nb_error
number of errored events
Definition n_kafka.h:137
char * bootstrap_servers
kafka bootstrap servers string
Definition n_kafka.h:109
char * from_topic
in case of received event, else NULL
Definition n_kafka.h:73
unsigned int status
state of the event: N_KAFKA_EVENT_CREATED ,N_KAFKA_EVENT_QUEUED , N_KAFKA_EVENT_WAITING_ACK ,...
Definition n_kafka.h:75
char * topic
kafka topic string
Definition n_kafka.h:107
int n_kafka_add_header_ex(N_KAFKA_EVENT *event, char *key, size_t key_length, char *value, size_t value_length)
add a header to an event (extended version)
Definition n_kafka.c:820
int n_kafka_dump_unprocessed(N_KAFKA *kafka, char *directory)
dump unprocessed events to a directory
Definition n_kafka.c:1441
N_KAFKA_EVENT * n_kafka_get_event(N_KAFKA *kafka)
get the next received event
Definition n_kafka.c:1515
int n_kafka_stop_polling_thread(N_KAFKA *kafka)
stop the kafka polling thread
Definition n_kafka.c:1342
int32_t n_kafka_get_schema_from_nstr(const N_STR *string)
get schema id from an N_STR string
Definition n_kafka.c:165
int n_kafka_new_headers(N_KAFKA_EVENT *event, size_t count)
create new headers for an event
Definition n_kafka.c:795
int n_kafka_put_schema_in_nstr(N_STR *string, int schema_id)
put schema id in an N_STR string
Definition n_kafka.c:191
int n_kafka_enable_event_production(N_KAFKA *kafka)
enable event production
Definition n_kafka.c:1414
int n_kafka_disable_event_consumption(N_KAFKA *kafka)
disable event consumption
Definition n_kafka.c:1401
N_KAFKA * n_kafka_load_config(char *config_file, int mode)
load kafka configuration from file
Definition n_kafka.c:463
int n_kafka_disable_event_production(N_KAFKA *kafka)
disable event production
Definition n_kafka.c:1427
int n_kafka_get_status(N_KAFKA *kafka, size_t *nb_queued, size_t *nb_waiting, size_t *nb_error)
get kafka queue status counters
Definition n_kafka.c:206
int n_kafka_put_schema_in_char(char *string, int schema_id)
put schema id in a char string
Definition n_kafka.c:178
int n_kafka_poll(N_KAFKA *kafka)
poll kafka for events
Definition n_kafka.c:1055
int n_kafka_start_polling_thread(N_KAFKA *kafka)
start the kafka polling thread
Definition n_kafka.c:1310
N_KAFKA_EVENT * n_kafka_new_event_from_char(const char *string, size_t written, int schema_id)
create a new kafka event from a char string
Definition n_kafka.c:946
N_KAFKA_EVENT * n_kafka_new_event_from_string(const N_STR *string, int schema_id)
create a new kafka event from an N_STR string
Definition n_kafka.c:984
int n_kafka_event_destroy(N_KAFKA_EVENT **event)
destroy a kafka event
Definition n_kafka.c:1043
int n_kafka_enable_event_consumption(N_KAFKA *kafka)
enable event consumption
Definition n_kafka.c:1388
void n_kafka_event_destroy_ptr(void *event)
destroy a kafka event (void pointer version for list destructor)
Definition n_kafka.c:1015
int32_t n_kafka_get_schema_from_char(const char *string)
get schema id from a char string
Definition n_kafka.c:151
int n_kafka_add_header(N_KAFKA_EVENT *event, N_STR *key, N_STR *value)
add a header to an event
Definition n_kafka.c:854
int n_kafka_set_sended_dir(N_KAFKA *kafka, const char *tosend_dir, const char *sended_dir)
set the 'to-send' and 'sended' base directories used to move acknowledged event files instead of dele...
Definition n_kafka.c:244
int n_kafka_load_unprocessed(N_KAFKA *kafka, const char *directory)
load unprocessed events from a directory
Definition n_kafka.c:1497
int n_kafka_produce(N_KAFKA *kafka, N_KAFKA_EVENT *event)
produce a kafka event
Definition n_kafka.c:870
N_KAFKA_EVENT * n_kafka_new_event(int schema_id)
create a new empty kafka event
Definition n_kafka.c:771
int n_kafka_set_error_timeout(N_KAFKA *kafka, int32_t error_timeout)
set the retry interval (in seconds) for errored events.
Definition n_kafka.c:225
N_KAFKA_EVENT * n_kafka_new_event_from_file(char *filename, int schema_id)
create a new kafka event from a file
Definition n_kafka.c:999
N_KAFKA * n_kafka_new(int32_t poll_timeout, int32_t poll_interval, size_t errstr_len)
create a new kafka handle
Definition n_kafka.c:407
void n_kafka_delete(N_KAFKA *kafka)
delete a kafka handle
Definition n_kafka.c:344
structure of a KAFKA consumer or producer handle
Definition n_kafka.h:89
structure of a KAFKA message
Definition n_kafka.h:67
A box including a string and his lenght.
Definition n_str.h:61
Generic log system.
Network Engine.