summaryrefslogtreecommitdiff
path: root/common/src/kafka.cpp
blob: cab4642e78e4d723aa0f3630ebb341d7e9233d9a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
#include <stdlib.h>
#include <string.h>

#include "kafka.h"
#include "tfe_utils.h"
#include <MESA/MESA_prof_load.h>
#include <librdkafka/rdkafka.h>

#define MAX_SYMBOL_LEN 128

struct config
{
    char brokerlist[MAX_SYMBOL_LEN];
    char sasl_username[MAX_SYMBOL_LEN];
    char sasl_passwd[MAX_SYMBOL_LEN];
    char topic_name[MAX_TOPIC_NUM][MAX_SYMBOL_LEN];
};

struct per_producer_per_topic
{
    rd_kafka_t *producer;
    rd_kafka_topic_t *topic;
};

struct kafka
{
    struct config cfg;
    struct per_producer_per_topic *pppt[MAX_TOPIC_NUM];
};

/******************************************************************************
 * Private API
 ******************************************************************************/

static void per_producer_per_topic_free(struct per_producer_per_topic *pppt)
{
    if (pppt)
    {
        if (pppt->topic)
        {
            rd_kafka_topic_destroy(pppt->topic);
            pppt->topic = NULL;
        }

        if (pppt->producer)
        {
            rd_kafka_destroy(pppt->producer);
            pppt->producer = NULL;
        }

        free(pppt);
        pppt = NULL;
    }
}

static struct per_producer_per_topic *per_producer_per_topic_new(const char *brokerlist, const char *sasl_username, const char *sasl_passwd, const char *topic_name)
{
    char err_str[1024] = {0};
    struct per_producer_per_topic *pppt = (struct per_producer_per_topic *)calloc(1, sizeof(struct per_producer_per_topic));
    if (!pppt)
    {
        return NULL;
    }

    rd_kafka_conf_t *conf = rd_kafka_conf_new();
    if (!conf)
    {
        TFE_LOG_ERROR(g_default_logger, "KAFKA: failed to create kafka conf");
        goto error_out;
    }
    if (rd_kafka_conf_set(conf, "queue.buffering.max.messages", "1000000", err_str, sizeof(err_str)) != RD_KAFKA_CONF_OK)
    {
        TFE_LOG_ERROR(g_default_logger, "KAFKA: failed to set kafka queue.buffering.max.messages, %s", err_str);
        goto error_out;
    }
    if (rd_kafka_conf_set(conf, "topic.metadata.refresh.interval.ms", "600000", err_str, sizeof(err_str)) != RD_KAFKA_CONF_OK)
    {
        TFE_LOG_ERROR(g_default_logger, "KAFKA: failed to set kafka topic.metadata.refresh.interval.ms, %s", err_str);
        goto error_out;
    }
    if (rd_kafka_conf_set(conf, "client.id", topic_name, err_str, sizeof(err_str)) != RD_KAFKA_CONF_OK)
    {
        TFE_LOG_ERROR(g_default_logger, "KAFKA: failed to set kafka client.id, %s", err_str);
        goto error_out;
    }
    if (strlen(sasl_username) > 0 && strlen(sasl_passwd) > 0)
    {
        if (rd_kafka_conf_set(conf, "security.protocol", "sasl_plaintext", err_str, sizeof(err_str)) != RD_KAFKA_CONF_OK)
        {
            TFE_LOG_ERROR(g_default_logger, "KAFKA: failed to set kafka security.protocol, %s", err_str);
            goto error_out;
        }
        if (rd_kafka_conf_set(conf, "sasl.mechanisms", "PLAIN", err_str, sizeof(err_str)) != RD_KAFKA_CONF_OK)
        {
            TFE_LOG_ERROR(g_default_logger, "KAFKA: failed to set kafka sasl.mechanisms, %s", err_str);
            goto error_out;
        }
        if (rd_kafka_conf_set(conf, "sasl.username", sasl_username, err_str, sizeof(err_str)) != RD_KAFKA_CONF_OK)
        {
            TFE_LOG_ERROR(g_default_logger, "KAFKA: failed to set kafka sasl.username, %s", err_str);
            goto error_out;
        }
        if (rd_kafka_conf_set(conf, "sasl.password", sasl_passwd, err_str, sizeof(err_str)) != RD_KAFKA_CONF_OK)
        {
            TFE_LOG_ERROR(g_default_logger, "KAFKA: failed to set kafka sasl.password, %s", err_str);
            goto error_out;
        }
    }
    else
    {
        if (rd_kafka_conf_set(conf, "security.protocol", "plaintext", err_str, sizeof(err_str)) != RD_KAFKA_CONF_OK)
        {
            TFE_LOG_ERROR(g_default_logger, "KAFKA: failed to set kafka security.protocol, %s", err_str);
            goto error_out;
        }
    }

    // The conf object is freed by this function and must not be used or destroyed by the application sub-sequently.
    pppt->producer = rd_kafka_new(RD_KAFKA_PRODUCER, conf, err_str, sizeof(err_str));
    conf = NULL;
    if (pppt->producer == NULL)
    {
        TFE_LOG_ERROR(g_default_logger, "KAFKA: failed to create kafka producer, %s", err_str);
        goto error_out;
    }

    if (rd_kafka_brokers_add(pppt->producer, brokerlist) == 0)
    {
        TFE_LOG_ERROR(g_default_logger, "KAFKA: failed to add kafka brokers");
        goto error_out;
    }

    pppt->topic = rd_kafka_topic_new(pppt->producer, topic_name, NULL);
    if (pppt->topic == NULL)
    {
        TFE_LOG_ERROR(g_default_logger, "KAFKA: failed to create kafka topic: %s", topic_name);
        goto error_out;
    }

    return pppt;

error_out:
    if (conf)
    {
        rd_kafka_conf_destroy(conf);
    }

    per_producer_per_topic_free(pppt);
    return NULL;
}

/******************************************************************************
 * Public API -- Kafka
 ******************************************************************************/

// due to limit by client.id, need per producer per topic
struct kafka *kafka_create(const char *profile)
{
    struct kafka *handle = (struct kafka *)calloc(1, sizeof(struct kafka));
    if (!handle)
    {
        return NULL;
    }

    MESA_load_profile_string_def(profile, "kafka", "brokerlist", handle->cfg.brokerlist, sizeof(handle->cfg.brokerlist), "");
    MESA_load_profile_string_def(profile, "kafka", "sasl_username", handle->cfg.sasl_username, sizeof(handle->cfg.sasl_username), "");
    MESA_load_profile_string_def(profile, "kafka", "sasl_passwd", handle->cfg.sasl_passwd, sizeof(handle->cfg.sasl_passwd), "");
    MESA_load_profile_string_def(profile, "kafka", "rule_hits_topic", handle->cfg.topic_name[TOPIC_RULE_HITS], sizeof(handle->cfg.topic_name[TOPIC_RULE_HITS]), "");
    MESA_load_profile_string_def(profile, "kafka", "proxy_event_topic", handle->cfg.topic_name[TOPIC_PROXY_EVENT], sizeof(handle->cfg.topic_name[TOPIC_PROXY_EVENT]), "");
    MESA_load_profile_string_def(profile, "kafka", "file_stream_topic", handle->cfg.topic_name[TOPIC_FILE_STREAM], sizeof(handle->cfg.topic_name[TOPIC_FILE_STREAM]), "");
    MESA_load_profile_string_def(profile, "kafka", "exch_cert_topic", handle->cfg.topic_name[TOPIC_EXCH_CERT], sizeof(handle->cfg.topic_name[TOPIC_EXCH_CERT]), "");

    if (strlen(handle->cfg.brokerlist) == 0)
    {
        TFE_LOG_ERROR(g_default_logger, "KAFKA: brokerlist is empty");
        goto error_out;
    }

    for (int i = 0; i < MAX_TOPIC_NUM; i++)
    {
        if (strlen(handle->cfg.topic_name[i]) == 0)
        {
            TFE_LOG_ERROR(g_default_logger, "KAFKA: topic_name[%d] is empty", i);
            goto error_out;
        }
    }

    for (int i = 0; i < MAX_TOPIC_NUM; i++)
    {
        handle->pppt[i] = per_producer_per_topic_new(handle->cfg.brokerlist, handle->cfg.sasl_username, handle->cfg.sasl_passwd, handle->cfg.topic_name[i]);
        if (!handle->pppt[i])
        {
            goto error_out;
        }
    }

    return handle;

error_out:
    kafka_destroy(handle);
    return NULL;
}

void kafka_destroy(struct kafka *handle)
{
    if (handle)
    {
        for (int i = 0; i < MAX_TOPIC_NUM; i++)
        {
            per_producer_per_topic_free(handle->pppt[i]);
            handle->pppt[i] = NULL;
        }

        free(handle);
        handle = NULL;
    }
}

int kafka_send2(struct kafka *handle, enum topic_idx idx, const char *data, int len)
{
    if (handle && handle->pppt[idx] && handle->pppt[idx]->topic)
    {
        if(rd_kafka_produce(handle->pppt[idx]->topic, RD_KAFKA_PARTITION_UA, RD_KAFKA_MSG_F_COPY, (void *)data, len, NULL, 0, NULL) == -1)
        {
            TFE_LOG_ERROR(g_default_logger, "KAFKA: failed to produce message with topic[%d], %s", idx, rd_kafka_err2str(rd_kafka_last_error()));
            return -1;
        }
    }
    return 0;
}

int kafka_send(struct kafka *handle, enum topic_idx idx, const char *data, int len)
{
    if (!handle)
    {
        TFE_LOG_ERROR(g_default_logger, "KAFKA: handle is NULL");
        return -1;
    }

    if (idx < 0 || idx >= MAX_TOPIC_NUM)
    {
        TFE_LOG_ERROR(g_default_logger, "KAFKA: invalid topic index: %d", idx);
        return -1;
    }

    if (handle->pppt[idx])
    {
        if (rd_kafka_produce(handle->pppt[idx]->topic, RD_KAFKA_PARTITION_UA, RD_KAFKA_MSG_F_COPY, (void *)data, len, NULL, 0, NULL) == -1)
        {
            TFE_LOG_ERROR(g_default_logger, "KAFKA: failed to produce message with topic [%d], %s", idx, rd_kafka_err2str(rd_kafka_last_error()));
            return -1;
        }
        else
        {
            TFE_LOG_DEBUG(g_default_logger, "KAFKA: success to produce message with topic [%d], %s", idx, data);
            return 0;
        }
    }
    else
    {
        TFE_LOG_ERROR(g_default_logger, "KAFKA: topic %d not initialized", idx);
        return -1;
    }
}