hunyadi-dev commented on a change in pull request #940: URL: https://github.com/apache/nifi-minifi-cpp/pull/940#discussion_r538356594
########## File path: extensions/librdkafka/ConsumeKafka.h ########## @@ -0,0 +1,197 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#pragma once + +#include <string> +#include <utility> +#include <vector> +#include <memory> + +#include "core/Processor.h" +#include "core/logging/LoggerConfiguration.h" +#include "rdkafka.h" +#include "rdkafka_utils.h" +#include "KafkaConnection.h" + +namespace org { +namespace apache { +namespace nifi { +namespace minifi { +namespace core { + +// The upper limit for Max Poll Time is 4 seconds. This is because Watchdog would potentially start +// reporting issues with the processor health otherwise +class ConsumeKafkaMaxPollTimeValidator : public TimePeriodValidator { + public: + ConsumeKafkaMaxPollTimeValidator(const std::string &name) // NOLINT + : TimePeriodValidator(name) { + } + ~ConsumeKafkaMaxPollTimeValidator() override = default; + + ValidationResult validate(const std::string& subject, const std::string& input) const override { + uint64_t value; + TimeUnit timeUnit; + uint64_t value_as_ms; + return ValidationResult::Builder::createBuilder().withSubject(subject).withInput(input).isValid( + core::TimePeriodValue::StringToTime(input, value, timeUnit) && + org::apache::nifi::minifi::core::Property::ConvertTimeUnitToMS(value, timeUnit, value_as_ms) && + 0 < value_as_ms && value_as_ms <= 4000).build(); + } +}; + +} // namespace core +namespace processors { + +class ConsumeKafka : public core::Processor { + public: + static constexpr char const* ProcessorName = "ConsumeKafka"; + + // Supported Properties + static core::Property KafkaBrokers; + static core::Property SecurityProtocol; + static core::Property TopicNames; + static core::Property TopicNameFormat; + static core::Property HonorTransactions; + static core::Property GroupID; + static core::Property OffsetReset; + static core::Property KeyAttributeEncoding; + static core::Property MessageDemarcator; + static core::Property MessageHeaderEncoding; + static core::Property HeadersToAddAsAttributes; + static core::Property DuplicateHeaderHandling; + static core::Property MaxPollRecords; + static core::Property MaxPollTime; + static core::Property SessionTimeout; + + // Supported Relationships + static const core::Relationship Success; + + // Security Protocol allowable values + static constexpr char const* SECURITY_PROTOCOL_PLAINTEXT = "PLAINTEXT"; + static constexpr char const* SECURITY_PROTOCOL_SSL = "SSL"; + static constexpr char const* SECURITY_PROTOCOL_SASL_PLAINTEXT = "SASL_PLAINTEXT"; + static constexpr char const* SECURITY_PROTOCOL_SASL_SSL = "SASL_SSL"; + + // Topic Name Format allowable values + static constexpr char const* TOPIC_FORMAT_NAMES = "Names"; + static constexpr char const* TOPIC_FORMAT_PATTERNS = "Patterns"; + + // Offset Reset allowable values + static constexpr char const* OFFSET_RESET_EARLIEST = "earliest"; + static constexpr char const* OFFSET_RESET_LATEST = "latest"; + static constexpr char const* OFFSET_RESET_NONE = "none"; + + // Key Attribute Encoding allowable values + static constexpr char const* KEY_ATTR_ENCODING_UTF_8 = "UTF-8"; + static constexpr char const* KEY_ATTR_ENCODING_HEX = "Hex"; + + // Message Header Encoding allowable values + static constexpr char const* MSG_HEADER_ENCODING_UTF_8 = "UTF-8"; + static constexpr char const* MSG_HEADER_ENCODING_HEX = "Hex"; + + // Duplicate Header Handling allowable values + static constexpr char const* MSG_HEADER_KEEP_FIRST = "Keep First"; + static constexpr char const* MSG_HEADER_KEEP_LATEST = "Keep Latest"; + static constexpr char const* MSG_HEADER_COMMA_SEPARATED_MERGE = "Comma-separated Merge"; + + // Flowfile attributes written + static constexpr char const* KAFKA_COUNT_ATTR = "kafka.count"; // Always 1 until we start supporting merging from batches + static constexpr char const* KAFKA_MESSAGE_KEY_ATTR = "kafka.key"; + static constexpr char const* KAFKA_OFFSET_ATTR = "kafka.offset"; + static constexpr char const* KAFKA_PARTITION_ATTR = "kafka.partition"; + static constexpr char const* KAFKA_TOPIC_ATTR = "kafka.topic"; + + static constexpr const std::size_t DEFAULT_MAX_POLL_RECORDS{ 10000 }; + static constexpr char const* DEFAULT_MAX_POLL_TIME = "4 seconds"; + static constexpr const std::size_t METADATA_COMMUNICATIONS_TIMEOUT_MS{ 60000 }; + + explicit ConsumeKafka(std::string name, utils::Identifier uuid = utils::Identifier()) : + Processor(name, uuid), + logger_(logging::LoggerFactory<ConsumeKafka>::getLogger()) {} + + virtual ~ConsumeKafka() = default; + + public: + bool supportsDynamicProperties() override { + return true; + } + /** + * Function that's executed when the processor is scheduled. + * @param context process context. + * @param sessionFactory process session factory that is used when creating + * ProcessSession objects. + */ + void onSchedule(core::ProcessContext* context, core::ProcessSessionFactory* /* sessionFactory */) override; + /** + * Execution trigger for the RetryFlowFile Processor + * @param context processor context + * @param session processor session reference. + */ + void onTrigger(core::ProcessContext* context, core::ProcessSession* session) override; + + // Initialize, overwrite by NiFi RetryFlowFile + void initialize() override; + + private: + void createTopicPartitionList(); + void extend_config_from_dynamic_properties(const core::ProcessContext* context); + void configure_new_connection(const core::ProcessContext* context); + std::string extract_message(const rd_kafka_message_t* rkmessage); + std::vector<std::unique_ptr<rd_kafka_message_t, utils::rd_kafka_message_deleter>> poll_kafka_messages(); + utils::KafkaEncoding key_attr_encoding_attr_to_enum(); + utils::KafkaEncoding message_header_encoding_attr_to_enum(); + std::string resolve_duplicate_headers(const std::vector<std::string>& matching_headers); + std::vector<std::string> get_matching_headers(const rd_kafka_message_t* message, const std::string& header_name); + std::vector<std::pair<std::string, std::string>> get_flowfile_attributes_from_message_header(const rd_kafka_message_t* message); + std::vector<std::shared_ptr<FlowFileRecord>> transform_messages_into_flowfiles( + const std::vector<std::unique_ptr<rd_kafka_message_t, utils::rd_kafka_message_deleter>>& messages, core::ProcessSession* session); Review comment: Yes, updated accordingly. ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: [email protected]
