Copilot commented on code in PR #2268: URL: https://github.com/apache/nifi-minifi-cpp/pull/2268#discussion_r4120707585
########## extensions/opc/src/FetchOPCEvents.cpp: ########## @@ -0,0 +1,225 @@ +/** + * 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. + */ + +#include "FetchOPCEvents.h" + +#include <chrono> +#include <memory> +#include <string> +#include <vector> + +#include "OPCCommon.h" +#include "core/ProcessSession.h" +#include "core/Resource.h" +#include "minifi-cpp/core/ProcessContext.h" +#include "utils/Enum.h" +#include "utils/ProcessorConfigUtils.h" +#include "utils/StringUtils.h" + +namespace org::apache::nifi::minifi::processors { + +namespace { +// How long one iteration of the event thread waits for notifications to arrive from the server. +constexpr UA_UInt32 NOTIFICATION_WAIT_TIME_MS = 200; +} // namespace + +void FetchOPCEvents::initialize() { + setSupportedProperties(Properties); + setSupportedRelationships(Relationships); +} + +void FetchOPCEvents::onSchedule(core::ProcessContext& context, core::ProcessSessionFactory& factory) { + logger_->log_trace("FetchOPCEvents::onSchedule"); + BaseOPCProcessor::onSchedule(context, factory); + + node_id_ = utils::parseProperty(context, NodeID); + id_type_ = utils::parseEnumProperty<opc::OPCNodeIDType>(context, NodeIDType); + namespace_idx_ = gsl::narrow<UA_UInt16>(utils::parseU64Property(context, NameSpaceIndex)); + parseNode(context); + + const auto record_set_writer_name = utils::parseProperty(context, RecordSetWriter); + auto controller_service = context.getControllerService(record_set_writer_name, getUUID()); + if (!record_set_writer_name.empty() && !controller_service) { + throw Exception(PROCESS_SCHEDULE_EXCEPTION, fmt::format("Controller service '{}' not found", record_set_writer_name)); + } + record_set_writer_ = std::dynamic_pointer_cast<core::RecordSetWriter>(controller_service); + if (!record_set_writer_) { + throw Exception(PROCESS_SCHEDULE_EXCEPTION, fmt::format("Failed to obtain RecordSetWriter controller service '{}'", record_set_writer_name)); + } + + const auto max_queue_size = utils::parseOptionalU64Property(context, MaxQueueSize); + max_event_queue_size_ = max_queue_size && *max_queue_size > 0 + ? std::optional<size_t>(gsl::narrow<size_t>(*max_queue_size)) + : std::nullopt; + + const auto minimum_severity = utils::parseOptionalU64Property(context, MinimumSeverity); + event_filter_.minimum_severity = minimum_severity && *minimum_severity > 0 ? minimum_severity : std::nullopt; + + event_filter_.event_type_node_id = utils::parseProperty(context, EventTypeNodeId); + event_filter_.filter_expression = context.getProperty(EventFilterExpression).value_or(""); + + event_filter_.select_fields = utils::string::splitAndTrimRemovingEmpty(utils::parseProperty(context, SelectFields), ","); + if (event_filter_.select_fields.empty() && event_filter_.filter_expression.empty()) { + throw Exception(PROCESS_SCHEDULE_EXCEPTION, + fmt::format("At least one field must be set in '{}', otherwise the events would carry no data", SelectFields.name)); + } + + const auto batch_size = utils::parseOptionalU64Property(context, BatchSize); + batch_size_ = batch_size && *batch_size > 0 ? batch_size : std::nullopt; + + connection_ = opc::Client::createClient(logger_, application_uri_, cert_buffer_, key_buffer_, trust_buffers_, max_event_queue_size_); + if (!connection_) { + throw Exception(PROCESS_SCHEDULE_EXCEPTION, "Failed to create the OPC UA client"); + } + + gsl_Expects(!event_thread_); + event_thread_ = std::make_unique<utils::StoppableThread>([this]() { runEventLoop(); }); Review Comment: The retry backoff is processor-instance state and is never reset when the processor is stopped and scheduled again. After repeated connection failures raise it to 64 seconds, a manual restart or reconfiguration still inherits that delay, so the restarted processor can remain unresponsive for another minute. Reset the backoff before starting each new event thread. ########## extensions/opc/include/OPCCommon.h: ########## @@ -19,9 +19,12 @@ #pragma once #include <array> +#include <chrono> +#include <deque> #include <string> #include <functional> #include <map> +#include <mutex> Review Comment: This header now declares `Event::fields` as a `std::unordered_map`, but it does not include `<unordered_map>`. Relying on another header to provide the declaration makes this public header non-self-contained and can break compilation when the transitive include set changes; add the standard header explicitly. -- 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. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
