forked from confluentinc/librdkafka
-
Notifications
You must be signed in to change notification settings - Fork 0
/
incremental_alter_configs.c
348 lines (284 loc) · 12.3 KB
/
incremental_alter_configs.c
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
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
/*
* librdkafka - Apache Kafka C library
*
* Copyright (c) 2023, Confluent Inc.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
/**
* IncrementalAlterConfigs usage example.
*/
#include <stdio.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <stdarg.h>
#ifdef _WIN32
#include "../win32/wingetopt.h"
#else
#include <getopt.h>
#endif
/* Typical include path would be <librdkafka/rdkafka.h>, but this program
* is builtin from within the librdkafka source tree and thus differs. */
#include "rdkafka.h"
const char *argv0;
static rd_kafka_queue_t *queue; /** Admin result queue.
* This is a global so we can
* yield in stop() */
static volatile sig_atomic_t run = 1;
/**
* @brief Signal termination of program
*/
static void stop(int sig) {
if (!run) {
fprintf(stderr, "%% Forced termination\n");
exit(2);
}
run = 0;
rd_kafka_queue_yield(queue);
}
static void usage(const char *reason, ...) {
fprintf(stderr,
"Incremental alter config usage examples\n"
"\n"
"Usage: %s <options> <res_type1> <res_name1> <alter_op_type1> "
"<config_name1> <config_value1> ...\n"
"\n"
"Options:\n"
" -b <brokers> Bootstrap server list to connect to.\n"
" -X <prop=val> Set librdkafka configuration property.\n"
" See CONFIGURATION.md for full list.\n"
" -d <dbg,..> Enable librdkafka debugging (%s).\n"
"\n",
argv0, rd_kafka_get_debug_contexts());
if (reason) {
va_list ap;
char reasonbuf[512];
va_start(ap, reason);
vsnprintf(reasonbuf, sizeof(reasonbuf), reason, ap);
va_end(ap);
fprintf(stderr, "ERROR: %s\n", reasonbuf);
}
exit(reason ? 1 : 0);
}
#define fatal(...) \
do { \
fprintf(stderr, "ERROR: "); \
fprintf(stderr, __VA_ARGS__); \
fprintf(stderr, "\n"); \
exit(2); \
} while (0)
/**
* @brief Set config property. Exit on failure.
*/
static void conf_set(rd_kafka_conf_t *conf, const char *name, const char *val) {
char errstr[512];
if (rd_kafka_conf_set(conf, name, val, errstr, sizeof(errstr)) !=
RD_KAFKA_CONF_OK)
fatal("Failed to set %s=%s: %s", name, val, errstr);
}
static void print_alter_configs_result(
FILE *fp,
const rd_kafka_IncrementalAlterConfigs_result_t *result,
const char *prefix) {
size_t i;
size_t config_cnt;
const rd_kafka_ConfigResource_t **configs =
rd_kafka_IncrementalAlterConfigs_result_resources(result,
&config_cnt);
for (i = 0; i < config_cnt; i++) {
const rd_kafka_ConfigResource_t *config = configs[i];
const char *resname = rd_kafka_ConfigResource_name(config);
rd_kafka_ResourceType_t restype =
rd_kafka_ConfigResource_type(config);
rd_kafka_resp_err_t err = rd_kafka_ConfigResource_error(config);
fprintf(fp, "%sResource type: %s name: %s error: %s: %s\n",
prefix, rd_kafka_ResourceType_name(restype), resname,
rd_kafka_err2str(err),
rd_kafka_ConfigResource_error_string(config));
}
}
/**
* @brief Call rd_kafka_IncrementalAlterConfigs() with a list of
* configs to alter.
*/
static void
cmd_incremental_alter_configs(rd_kafka_conf_t *conf, int argc, char **argv) {
rd_kafka_t *rk;
char errstr[512];
rd_kafka_AdminOptions_t *options;
rd_kafka_event_t *event = NULL;
rd_kafka_error_t *error;
int retval = 0;
const char *prefix = " ";
int i = 0;
int resources = 0;
int config_cnt;
rd_kafka_ResourceType_t prev_restype = RD_KAFKA_RESOURCE_UNKNOWN;
char *prev_resname = NULL;
rd_kafka_ConfigResource_t **configs;
if (argc % 5 != 0) {
usage("Invalid number of arguments: %d", argc);
}
config_cnt = argc / 5;
configs = calloc(config_cnt, sizeof(*configs));
for (i = 0; i < config_cnt; i++) {
char *restype_s = argv[i * 5];
char *resname = argv[i * 5 + 1];
char *alter_op_type_s = argv[i * 5 + 2];
char *config_name = argv[i * 5 + 3];
char *config_value = argv[i * 5 + 4];
rd_kafka_ConfigResource_t *config;
rd_kafka_AlterConfigOpType_t op_type;
rd_kafka_ResourceType_t restype =
!strcmp(restype_s, "TOPIC")
? RD_KAFKA_RESOURCE_TOPIC
: !strcmp(restype_s, "BROKER")
? RD_KAFKA_RESOURCE_BROKER
: RD_KAFKA_RESOURCE_UNKNOWN;
if (restype == RD_KAFKA_RESOURCE_UNKNOWN) {
usage("Invalid resource type: %s", restype_s);
}
/* It's not necessary, but cleaner and more efficient to group
* incremental alterations for the same ConfigResource.*/
if (restype != prev_restype || strcmp(resname, prev_resname)) {
configs[resources++] =
rd_kafka_ConfigResource_new(restype, resname);
}
config = configs[resources - 1];
prev_restype = restype;
prev_resname = resname;
if (!strcmp(alter_op_type_s, "SET")) {
op_type = RD_KAFKA_ALTER_CONFIG_OP_TYPE_SET;
} else if (!strcmp(alter_op_type_s, "APPEND")) {
op_type = RD_KAFKA_ALTER_CONFIG_OP_TYPE_APPEND;
} else if (!strcmp(alter_op_type_s, "SUBTRACT")) {
op_type = RD_KAFKA_ALTER_CONFIG_OP_TYPE_SUBTRACT;
} else if (!strcmp(alter_op_type_s, "DELETE")) {
op_type = RD_KAFKA_ALTER_CONFIG_OP_TYPE_DELETE;
} else {
usage("Invalid alter config operation: %s",
alter_op_type_s);
}
error = rd_kafka_ConfigResource_add_incremental_config(
config, config_name, op_type, config_value);
if (error) {
usage(
"Error setting incremental config alteration %s"
" at index %d: %s",
alter_op_type_s, i, rd_kafka_error_string(error));
}
}
/*
* Create consumer instance
* NOTE: rd_kafka_new() takes ownership of the conf object
* and the application must not reference it again after
* this call.
*/
rk = rd_kafka_new(RD_KAFKA_CONSUMER, conf, errstr, sizeof(errstr));
if (!rk)
fatal("Failed to create new consumer: %s", errstr);
/*
* Incremental alter configs
*/
queue = rd_kafka_queue_new(rk);
/* Signal handler for clean shutdown */
signal(SIGINT, stop);
options = rd_kafka_AdminOptions_new(
rk, RD_KAFKA_ADMIN_OP_INCREMENTALALTERCONFIGS);
if (rd_kafka_AdminOptions_set_request_timeout(
options, 10 * 1000 /* 10s */, errstr, sizeof(errstr))) {
fprintf(stderr, "%% Failed to set timeout: %s\n", errstr);
goto exit;
}
rd_kafka_IncrementalAlterConfigs(rk, configs, resources, options,
queue);
rd_kafka_ConfigResource_destroy_array(configs, resources);
free(configs);
/* Wait for results */
event = rd_kafka_queue_poll(queue, -1 /* indefinitely but limited by
* the request timeout set
* above (10s) */);
if (!event) {
/* User hit Ctrl-C,
* see yield call in stop() signal handler */
fprintf(stderr, "%% Cancelled by user\n");
} else if (rd_kafka_event_error(event)) {
rd_kafka_resp_err_t err = rd_kafka_event_error(event);
/* IncrementalAlterConfigs request failed */
fprintf(stderr, "%% IncrementalAlterConfigs failed: %s: %s\n",
rd_kafka_err2str(err),
rd_kafka_event_error_string(event));
goto exit;
} else {
/* IncrementalAlterConfigs request succeeded, but individual
* configs may have errors. */
const rd_kafka_IncrementalAlterConfigs_result_t *result =
rd_kafka_event_IncrementalAlterConfigs_result(event);
printf("IncrementalAlterConfigs results:\n");
print_alter_configs_result(stdout, result, prefix);
}
exit:
if (event)
rd_kafka_event_destroy(event);
rd_kafka_AdminOptions_destroy(options);
rd_kafka_queue_destroy(queue);
/* Destroy the client instance */
rd_kafka_destroy(rk);
exit(retval);
}
int main(int argc, char **argv) {
rd_kafka_conf_t *conf; /**< Client configuration object */
int opt;
argv0 = argv[0];
/*
* Create Kafka client configuration place-holder
*/
conf = rd_kafka_conf_new();
/*
* Parse common options
*/
while ((opt = getopt(argc, argv, "b:X:d:")) != -1) {
switch (opt) {
case 'b':
conf_set(conf, "bootstrap.servers", optarg);
break;
case 'X': {
char *name = optarg, *val;
if (!(val = strchr(name, '=')))
fatal("-X expects a name=value argument");
*val = '\0';
val++;
conf_set(conf, name, val);
break;
}
case 'd':
conf_set(conf, "debug", optarg);
break;
default:
usage("Unknown option %c", (char)opt);
}
}
cmd_incremental_alter_configs(conf, argc - optind, &argv[optind]);
return 0;
}