Skip to content

Commit 024153c

Browse files
committed
feat kafka: move schemas to separate files and embed them
Tests: протестировано локально и в CI commit_hash:575e46371b331535dfb93baa955633f14ce9d3f7
1 parent 3ccd299 commit 024153c

File tree

13 files changed

+312
-271
lines changed

13 files changed

+312
-271
lines changed

.mapping.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2604,6 +2604,7 @@
26042604
"kafka/include/userver/kafka/rebalance_types.hpp":"taxi/uservices/userver/kafka/include/userver/kafka/rebalance_types.hpp",
26052605
"kafka/library.yaml":"taxi/uservices/userver/kafka/library.yaml",
26062606
"kafka/src/kafka/consumer_component.cpp":"taxi/uservices/userver/kafka/src/kafka/consumer_component.cpp",
2607+
"kafka/src/kafka/consumer_component.yaml":"taxi/uservices/userver/kafka/src/kafka/consumer_component.yaml",
26072608
"kafka/src/kafka/consumer_scope.cpp":"taxi/uservices/userver/kafka/src/kafka/consumer_scope.cpp",
26082609
"kafka/src/kafka/exceptions.cpp":"taxi/uservices/userver/kafka/src/kafka/exceptions.cpp",
26092610
"kafka/src/kafka/headers.cpp":"taxi/uservices/userver/kafka/src/kafka/headers.cpp",
@@ -2628,6 +2629,7 @@
26282629
"kafka/src/kafka/impl/stats.cpp":"taxi/uservices/userver/kafka/src/kafka/impl/stats.cpp",
26292630
"kafka/src/kafka/producer.cpp":"taxi/uservices/userver/kafka/src/kafka/producer.cpp",
26302631
"kafka/src/kafka/producer_component.cpp":"taxi/uservices/userver/kafka/src/kafka/producer_component.cpp",
2632+
"kafka/src/kafka/producer_component.yaml":"taxi/uservices/userver/kafka/src/kafka/producer_component.yaml",
26312633
"kafka/tests/configuration_test.cpp":"taxi/uservices/userver/kafka/tests/configuration_test.cpp",
26322634
"kafka/tests/consumer_kafkatest.cpp":"taxi/uservices/userver/kafka/tests/consumer_kafkatest.cpp",
26332635
"kafka/tests/headers_test.cpp":"taxi/uservices/userver/kafka/tests/headers_test.cpp",

cmake/UserverEmbedFile.cmake

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ include_guard(GLOBAL)
33
function(userver_embed_file TARGET)
44
set(OPTIONS)
55
set(ONE_VALUE_ARGS NAME FILEPATH HPP_FILENAME)
6-
set(MULTI_VALUE_ARGS SQL_FILES)
76
cmake_parse_arguments(ARG "${OPTIONS}" "${ONE_VALUE_ARGS}" "${MULTI_VALUE_ARGS}" ${ARGN})
7+
if(NOT ARG_NAME)
8+
set(ARG_NAME "${ARG_FILEPATH}")
9+
endif()
810
if(NOT ARG_HPP_FILENAME)
911
get_filename_component(ARG_HPP_FILENAME "${ARG_FILEPATH}" NAME)
1012
endif()
@@ -21,11 +23,12 @@ function(userver_embed_file TARGET)
2123
_userver_codegen_register_files("${CMAKE_CURRENT_BINARY_DIR}/embedded/embedded.cpp")
2224

2325
get_property(USERVER_CMAKE_DIR GLOBAL PROPERTY userver_cmake_dir)
26+
string(MAKE_C_IDENTIFIER "${ARG_NAME}" NAME_C_ESCAPED)
2427
add_custom_command(
2528
OUTPUT ${CONFIG_HPP}
2629
DEPENDS ${USERVER_CMAKE_DIR}/embedded_config.cmake ${ARG_FILEPATH}
2730
COMMAND ${CMAKE_COMMAND} -DSOURCE_DIR=${CMAKE_CURRENT_SOURCE_DIR} -DFILEPATH=${ARG_FILEPATH}
28-
-DOUTPUT=${CONFIG_HPP} -DNAME=${ARG_NAME} -P ${USERVER_CMAKE_DIR}/embedded_config.cmake ${CODEGEN}
31+
-DOUTPUT=${CONFIG_HPP} -DNAME_C_ESCAPED=${NAME_C_ESCAPED} -DNAME=${ARG_NAME} -P ${USERVER_CMAKE_DIR}/embedded_config.cmake ${CODEGEN}
2932
)
3033
_userver_codegen_register_files("${CONFIG_HPP}")
3134
add_library(${TARGET} STATIC ${CONFIG_HPP} ${CMAKE_CURRENT_BINARY_DIR}/embedded/embedded.cpp)

cmake/UserverModule.cmake

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ function(userver_module MODULE)
2424
UBENCH_DATABASES
2525
UBENCH_ENV
2626
DEPENDS
27+
EMBED_FILES
2728
)
2829
cmake_parse_arguments(ARG "${OPTIONS}" "${ONE_VALUE_ARGS}" "${MULTI_VALUE_ARGS}" ${ARGN})
2930
if(ARG_UNPARSED_ARGUMENTS)
@@ -149,4 +150,10 @@ function(userver_module MODULE)
149150
DESTINATION "${CMAKE_INSTALL_INCLUDEDIR}/.."
150151
)
151152
endif()
153+
154+
foreach(FILE ${ARG_EMBED_FILES})
155+
string(MAKE_C_IDENTIFIER "userver-${MODULE}-embed_${FILE}" EMBED_TARGET_NAME)
156+
userver_embed_file(${EMBED_TARGET_NAME} FILEPATH "${FILE}" HPP_FILENAME "${FILE}")
157+
target_link_libraries(userver-${MODULE} PRIVATE "$<BUILD_INTERFACE:${EMBED_TARGET_NAME}>")
158+
endforeach()
152159
endfunction()

cmake/embedded_config.cmake

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,32 +19,32 @@ set(TEMPLATE
1919
__asm__(
2020
#if defined(__APPLE__)
2121
\".const_data\\n\"
22-
\".global _@NAME@_start\\n\"
23-
\".global _@NAME@_end\\n\"
24-
\".global _@NAME@_size\\n\"
22+
\".global _@NAME_C_ESCAPED@_start\\n\"
23+
\".global _@NAME_C_ESCAPED@_end\\n\"
24+
\".global _@NAME_C_ESCAPED@_size\\n\"
2525
#else
2626
\".section .rodata\\n\"
2727
#endif
2828
\".balign 16\\n\"
29-
APPLE_PREFIX \"@NAME@_begin:\\n\"
29+
APPLE_PREFIX \"@NAME_C_ESCAPED@_begin:\\n\"
3030
R\"(
3131
.incbin \"${FILEPATH}\"
3232
)\"
3333
\".balign 1\\n\"
34-
APPLE_PREFIX \"@NAME@_end:\\n\"
34+
APPLE_PREFIX \"@NAME_C_ESCAPED@_end:\\n\"
3535
\".byte 0\\n\"
36-
APPLE_PREFIX \"@NAME@_size:\\n\"
37-
\".int \" APPLE_PREFIX \"@NAME@_end - \" APPLE_PREFIX \"@NAME@_begin\\n\"
36+
APPLE_PREFIX \"@NAME_C_ESCAPED@_size:\\n\"
37+
\".int \" APPLE_PREFIX \"@NAME_C_ESCAPED@_end - \" APPLE_PREFIX \"@NAME_C_ESCAPED@_begin\\n\"
3838
\".previous\\n\"
3939
);
4040
41-
extern \"C\" const char @NAME@_begin[];
42-
extern \"C\" const char @NAME@_end;
43-
extern \"C\" const int @NAME@_size;
41+
extern \"C\" const char @NAME_C_ESCAPED@_begin[];
42+
extern \"C\" const char @NAME_C_ESCAPED@_end;
43+
extern \"C\" const int @NAME_C_ESCAPED@_size;
4444
4545
46-
__attribute__((constructor)) void @NAME@_call() {
47-
${NAMESPACE}::utils::RegisterResource(\"@NAME@\", std::string_view{@NAME@_begin, static_cast<size_t>(@NAME@_size)});
46+
inline __attribute__((constructor)) void @NAME_C_ESCAPED@_call() {
47+
${NAMESPACE}::utils::RegisterResource(\"@NAME@\", std::string_view{@NAME_C_ESCAPED@_begin, static_cast<size_t>(@NAME_C_ESCAPED@_size)});
4848
}
4949
"
5050
)

kafka/CMakeLists.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ userver_module(
1313
DBTEST_DIRS "${CMAKE_CURRENT_SOURCE_DIR}/tests"
1414
DBTEST_LINK_LIBRARIES userver::kafka-utest
1515
DEPENDS core
16+
EMBED_FILES
17+
src/kafka/consumer_component.yaml
18+
src/kafka/producer_component.yaml
1619
)
1720

1821
target_compile_options(${PROJECT_NAME} PRIVATE "-Wno-ignored-qualifiers")

kafka/src/kafka/consumer_component.cpp

Lines changed: 5 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@
1010
#include <userver/storages/secdist/component.hpp>
1111
#include <userver/yaml_config/merge_schemas.hpp>
1212

13+
#ifndef ARCADIA_ROOT
14+
#include "generated/src/kafka/consumer_component.yaml.hpp" // Y_IGNORE
15+
#endif
16+
1317
USERVER_NAMESPACE_BEGIN
1418

1519
namespace kafka {
@@ -61,150 +65,7 @@ ConsumerComponent::~ConsumerComponent() { statistics_holder_.Unregister(); }
6165
ConsumerScope ConsumerComponent::GetConsumer() { return consumer_->MakeConsumerScope(); }
6266

6367
yaml_config::Schema ConsumerComponent::GetStaticConfigSchema() {
64-
return yaml_config::MergeSchemas<components::ComponentBase>(R"(
65-
type: object
66-
description: Kafka consumer component
67-
additionalProperties: false
68-
properties:
69-
group_id:
70-
type: string
71-
description: |
72-
consumer group id.
73-
Topic partition evenly distributed
74-
between consumers with the same `group_id`
75-
client_id:
76-
type: string
77-
description: |
78-
Client identifier.
79-
May be an arbitrary string.
80-
Optional, but you should set this property on each instance
81-
because it enables you to more easily correlate requests on the broker
82-
with the client instance which made it,
83-
which can be helpful in debugging and troubleshooting scenarios.
84-
defaultDescription: userver
85-
topics:
86-
type: array
87-
description: list of topics consumer subscribes
88-
items:
89-
type: string
90-
description: topic name
91-
max_batch_size:
92-
type: integer
93-
description: maximum number of messages consumer waits for new messages before calling a callback
94-
defaultDescription: 1
95-
poll_timeout:
96-
type: string
97-
description: maximum amount of time consumer waits for new messages before calling a callback
98-
defaultDescription: 1s
99-
message_key_log_format:
100-
type: string
101-
enum:
102-
- plaintext
103-
- hex
104-
description: |
105-
Specifies the logging format for the message key.
106-
- 'plaintext': logs the message key as-is.
107-
- 'hex': logs the message key in hexadecimal format.
108-
defaultDescription: plaintext
109-
max_callback_duration:
110-
type: string
111-
description: |
112-
duration user callback must fit not to be kicked from the consumer group.
113-
The duration must fit in [1ms; 86400000ms]
114-
defaultDescription: 5m
115-
restart_after_failure_delay:
116-
type: string
117-
description: backoff consumer waits until restart after user-callback exception.
118-
defaultDescription: 10s
119-
auto_offset_reset:
120-
type: string
121-
description: |
122-
action to take when there is no
123-
initial offset in offset store
124-
or the desired offset is out of range:
125-
`smallest`, `earliest`, `beginning` - automatically reset
126-
the offset to the smallest offset
127-
`largest`, `latest`, `end` - automatically reset
128-
the offset to the largest offset,
129-
`error` - trigger an error (ERR__AUTO_OFFSET_RESET).
130-
Note: the policy applies iff there are not committed
131-
offsets in topic
132-
enum:
133-
- smallest
134-
- earliest
135-
- beginning
136-
- largest
137-
- latest
138-
- end
139-
- error
140-
env_pod_name:
141-
type: string
142-
description: |
143-
if defined and `group_id` value contains
144-
`{pod_name}` substring, the substring
145-
is replaced with the value of the environment
146-
variable `env_pod_name`
147-
defaultDescription: none
148-
security_protocol:
149-
type: string
150-
description: protocol used to communicate with brokers
151-
enum:
152-
- PLAINTEXT
153-
- SASL_SSL
154-
- SASL_PLAINTEXT
155-
- SSL
156-
sasl_mechanisms:
157-
type: string
158-
description: |
159-
SASL mechanism to use for authentication.
160-
Must be set if `security_protocol` equals `SASL_SSL`
161-
defaultDescription: none
162-
enum:
163-
- PLAIN
164-
- SCRAM-SHA-512
165-
ssl_ca_location:
166-
type: string
167-
description: |
168-
file or directory path to CA certificate(s) for verifying the broker's key.
169-
Must be set if `security_protocol` equals `SASL_SSL` or `SSL`.
170-
If set to `probe`, CA certificates are probed from the default certificates paths
171-
defaultDescription: none
172-
topic_metadata_refresh_interval:
173-
type: string
174-
description: |
175-
period of time at which
176-
topic and broker metadata is refreshed
177-
in order to discover any new brokers,
178-
topics, partitions or partition leader changes
179-
defaultDescription: 5m
180-
metadata_max_age:
181-
type: string
182-
description: |
183-
metadata cache max age.
184-
Recommended value is 3 times `topic_metadata_refresh_interval`
185-
defaultDescription: 15m
186-
rd_kafka_custom_options:
187-
type: object
188-
description: |
189-
a map of arbitrary `librdkafka` library configuration options.
190-
Full list of options is available by link: https://github.com/confluentinc/librdkafka/blob/master/CONFIGURATION.md.
191-
Note: This options is not guaranteed to be supported in userver-kafka, use it at your own risk
192-
properties: {}
193-
additionalProperties:
194-
type: string
195-
description: librdkafka option value
196-
defaultDescription: '{}'
197-
debug_info_log_level:
198-
type: string
199-
description: |
200-
log level for everything debug information
201-
defaultDescription: debug
202-
operation_log_level:
203-
type: string
204-
description: |
205-
log level for infos about ordinary actions
206-
defaultDescription: debug
207-
)");
68+
return yaml_config::MergeSchemasFromResource<components::ComponentBase>("src/kafka/consumer_component.yaml");
20869
}
20970

21071
} // namespace kafka

0 commit comments

Comments
 (0)