You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by fg...@apache.org on 2023/03/09 13:26:34 UTC
[nifi-minifi-cpp] 01/04: MINIFICPP-1869 Metrics-related refactoring
This is an automated email from the ASF dual-hosted git repository.
fgerlits pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/nifi-minifi-cpp.git
commit 314fd81d6b189c93db99ba8bd5db0910c6fd0ced
Author: Gabor Gyimesi <ga...@gmail.com>
AuthorDate: Tue Jan 17 16:12:30 2023 +0100
MINIFICPP-1869 Metrics-related refactoring
Signed-off-by: Ferenc Gerlits <fg...@gmail.com>
This closes #1497
---
controller/Controller.h | 12 +-
extensions/coap/tests/CoapIntegrationBase.h | 7 +-
extensions/http-curl/protocols/RESTSender.cpp | 5 +-
.../http-curl/tests/C2DescribeMetricsTest.cpp | 1 -
extensions/http-curl/tests/C2EmptyMetricTest.cpp | 1 -
extensions/http-curl/tests/C2MetricsTest.cpp | 1 -
extensions/http-curl/tests/C2PauseResumeTest.cpp | 9 +-
.../http-curl/tests/C2SameProcessorMetrics.cpp | 1 -
.../tests/ControllerServiceIntegrationTests.cpp | 6 +-
extensions/http-curl/tests/VerifyInvokeHTTP.h | 5 +-
.../prometheus/PrometheusMetricsPublisher.cpp | 22 +-
extensions/prometheus/PrometheusMetricsPublisher.h | 10 +-
.../tests/PrometheusMetricsPublisherTest.cpp | 11 +-
extensions/python/tests/PythonManifestTests.cpp | 7 +-
libminifi/include/FlowController.h | 108 ++----
libminifi/include/RootProcessGroupWrapper.h | 91 ++++++
libminifi/include/c2/C2Agent.h | 29 +-
.../c2/{C2Client.h => C2MetricsPublisher.h} | 49 +--
.../state/MetricsPublisher.h => c2/C2Utils.h} | 19 +-
libminifi/include/core/Flow.h | 59 ----
libminifi/include/core/FlowConfiguration.h | 2 +-
libminifi/include/core/Processor.h | 2 +-
libminifi/include/core/state/ConnectionStore.h | 6 +-
libminifi/include/core/state/MetricsPublisher.h | 16 +-
...etricsPublisher.h => MetricsPublisherFactory.h} | 15 +-
.../include/core/state/MetricsPublisherStore.h | 55 ++++
.../include/core/state/nodes/FlowInformation.h | 14 +-
libminifi/include/core/state/nodes/MetricsBase.h | 40 +--
libminifi/include/core/state/nodes/QueueMetrics.h | 17 +-
.../include/core/state/nodes/ResponseNodeLoader.h | 48 +--
libminifi/src/FlowController.cpp | 362 +++++++--------------
libminifi/src/RootProcessGroupWrapper.cpp | 188 +++++++++++
libminifi/src/c2/C2Agent.cpp | 99 +++---
.../c2/{C2Client.cpp => C2MetricsPublisher.cpp} | 133 +++-----
.../MetricsPublisher.h => src/c2/C2Utils.cpp} | 23 +-
.../src/core/state/MetricsPublisherFactory.cpp | 45 +++
libminifi/src/core/state/MetricsPublisherStore.cpp | 68 ++++
libminifi/src/core/state/nodes/MetricsBase.cpp | 52 +++
.../src/core/state/nodes/ResponseNodeLoader.cpp | 146 ++++++---
libminifi/test/flow-tests/TestControllerWithFlow.h | 13 +-
libminifi/test/integration/IntegrationBase.h | 14 +-
.../integration/OnScheduleErrorHandlingTests.cpp | 8 +-
.../test/integration/ProvenanceReportingTest.cpp | 4 +-
.../integration/StateTransactionalityTests.cpp | 8 +-
.../test/persistence-tests/PersistenceTests.cpp | 14 +-
libminifi/test/rocksdb-tests/RepoTests.cpp | 3 +-
libminifi/test/unit/ProvenanceTestHelper.h | 12 +-
libminifi/test/unit/ResponseNodeLoaderTests.cpp | 20 +-
minifi_main/MiNiFiMain.cpp | 12 +-
nanofi/include/cxx/C2CallbackAgent.h | 24 +-
nanofi/include/cxx/Instance.h | 2 +-
nanofi/src/cxx/C2CallbackAgent.cpp | 6 +-
52 files changed, 1081 insertions(+), 843 deletions(-)
diff --git a/controller/Controller.h b/controller/Controller.h
index 836357977..e3d2d266b 100644
--- a/controller/Controller.h
+++ b/controller/Controller.h
@@ -29,6 +29,8 @@
#include "FlowController.h"
#include "core/repository/VolatileContentRepository.h"
#include "core/repository/VolatileFlowFileRepository.h"
+#include "core/state/MetricsPublisherFactory.h"
+#include "core/state/MetricsPublisherStore.h"
/**
* Sends a single argument comment
@@ -284,9 +286,10 @@ std::shared_ptr<org::apache::nifi::minifi::core::controller::ControllerService>
const auto stream_factory = org::apache::nifi::minifi::io::StreamFactory::getInstance(configuration);
auto flow_configuration = org::apache::nifi::minifi::core::createFlowConfiguration(
- org::apache::nifi::minifi::core::ConfigurationContext{flow_repo, content_repo, stream_factory, configuration}, nifi_configuration_class_name);
+ org::apache::nifi::minifi::core::ConfigurationContext{flow_repo, content_repo, stream_factory, configuration}, nifi_configuration_class_name);
- const auto controller = std::make_unique<org::apache::nifi::minifi::FlowController>(prov_repo, flow_repo, configuration, std::move(flow_configuration), content_repo);
+ const auto controller = std::make_unique<org::apache::nifi::minifi::FlowController>(
+ prov_repo, flow_repo, configuration, std::move(flow_configuration), content_repo);
controller->load();
auto service = controller->getControllerService(service_name);
if (service)
@@ -349,9 +352,10 @@ void printManifest(const std::shared_ptr<org::apache::nifi::minifi::Configure> &
const auto stream_factory = org::apache::nifi::minifi::io::StreamFactory::getInstance(configuration);
auto flow_configuration = org::apache::nifi::minifi::core::createFlowConfiguration(
- org::apache::nifi::minifi::core::ConfigurationContext{flow_repo, content_repo, stream_factory, configuration}, nifi_configuration_class_name);
+ org::apache::nifi::minifi::core::ConfigurationContext{flow_repo, content_repo, stream_factory, configuration}, nifi_configuration_class_name);
- const auto controller = std::make_unique<org::apache::nifi::minifi::FlowController>(prov_repo, flow_repo, configuration, std::move(flow_configuration), content_repo, "manifest");
+ const auto controller = std::make_unique<org::apache::nifi::minifi::FlowController>(
+ prov_repo, flow_repo, configuration, std::move(flow_configuration), content_repo);
controller->load();
controller->start();
std::this_thread::sleep_for(std::chrono::milliseconds(10000));
diff --git a/extensions/coap/tests/CoapIntegrationBase.h b/extensions/coap/tests/CoapIntegrationBase.h
index 10ef89fd8..593bf4dac 100644
--- a/extensions/coap/tests/CoapIntegrationBase.h
+++ b/extensions/coap/tests/CoapIntegrationBase.h
@@ -64,7 +64,7 @@ class CoapIntegrationBase : public IntegrationBase {
std::shared_ptr<core::ContentRepository> content_repo = std::make_shared<core::repository::VolatileContentRepository>();
content_repo->initialize(configuration);
std::shared_ptr<minifi::io::StreamFactory> stream_factory = minifi::io::StreamFactory::getInstance(configuration);
- auto yaml_ptr = std::make_unique<core::YamlConfiguration>(core::ConfigurationContext{test_repo, content_repo, stream_factory, configuration, test_file_location});
+ auto yaml_ptr = std::make_shared<core::YamlConfiguration>(core::ConfigurationContext{test_repo, content_repo, stream_factory, configuration, test_file_location});
core::YamlConfiguration yaml_config({test_repo, content_repo, stream_factory, configuration, test_file_location});
@@ -72,8 +72,8 @@ class CoapIntegrationBase : public IntegrationBase {
queryRootProcessGroup(pg);
- std::shared_ptr<minifi::FlowController> controller = std::make_shared<minifi::FlowController>(test_repo, test_flow_repo, configuration, std::move(yaml_ptr), content_repo, DEFAULT_ROOT_GROUP_NAME,
- std::make_shared<utils::file::FileSystem>(), []{});
+ auto metrics_publisher_store = std::make_unique<minifi::state::MetricsPublisherStore>(configuration, test_repo, test_flow_repo, yaml_ptr);
+ auto controller = std::make_unique<minifi::FlowController>(test_repo, test_flow_repo, configuration, std::move(yaml_ptr), content_repo, std::move(metrics_publisher_store));
controller->load();
controller->start();
@@ -82,7 +82,6 @@ class CoapIntegrationBase : public IntegrationBase {
shutdownBeforeFlowController();
controller->waitUnload(wait_time_.count());
- controller->stopC2();
cleanup();
}
diff --git a/extensions/http-curl/protocols/RESTSender.cpp b/extensions/http-curl/protocols/RESTSender.cpp
index d5934b817..5eb8c2d8f 100644
--- a/extensions/http-curl/protocols/RESTSender.cpp
+++ b/extensions/http-curl/protocols/RESTSender.cpp
@@ -45,9 +45,8 @@ void RESTSender::initialize(core::controller::ControllerServiceProvider* control
std::string ssl_context_service_str;
configure->get(Configuration::nifi_c2_rest_url, "c2.rest.url", rest_uri_);
configure->get(Configuration::nifi_c2_rest_url_ack, "c2.rest.url.ack", ack_uri_);
- if (configure->get(Configuration::nifi_c2_rest_ssl_context_service, "c2.rest.ssl.context.service", ssl_context_service_str)) {
- auto service = controller->getControllerService(ssl_context_service_str);
- if (nullptr != service) {
+ if (controller && configure->get(Configuration::nifi_c2_rest_ssl_context_service, "c2.rest.ssl.context.service", ssl_context_service_str)) {
+ if (auto service = controller->getControllerService(ssl_context_service_str)) {
ssl_context_service_ = std::static_pointer_cast<minifi::controllers::SSLContextService>(service);
}
}
diff --git a/extensions/http-curl/tests/C2DescribeMetricsTest.cpp b/extensions/http-curl/tests/C2DescribeMetricsTest.cpp
index 0e177d5fd..b5458a3b4 100644
--- a/extensions/http-curl/tests/C2DescribeMetricsTest.cpp
+++ b/extensions/http-curl/tests/C2DescribeMetricsTest.cpp
@@ -41,7 +41,6 @@ class VerifyEmptyC2Metric : public VerifyC2Base {
void testSetup() override {
LogTestController::getInstance().setTrace<c2::C2Agent>();
- LogTestController::getInstance().setTrace<c2::C2Client>();
LogTestController::getInstance().setDebug<c2::RESTSender>();
LogTestController::getInstance().setDebug<FlowController>();
LogTestController::getInstance().setOff<processors::GetTCP>();
diff --git a/extensions/http-curl/tests/C2EmptyMetricTest.cpp b/extensions/http-curl/tests/C2EmptyMetricTest.cpp
index 84e2435ac..6323a4839 100644
--- a/extensions/http-curl/tests/C2EmptyMetricTest.cpp
+++ b/extensions/http-curl/tests/C2EmptyMetricTest.cpp
@@ -43,7 +43,6 @@ class VerifyEmptyC2Metric : public VerifyC2Base {
void testSetup() override {
LogTestController::getInstance().setTrace<minifi::c2::C2Agent>();
- LogTestController::getInstance().setTrace<minifi::c2::C2Client>();
LogTestController::getInstance().setDebug<minifi::c2::RESTSender>();
LogTestController::getInstance().setDebug<minifi::FlowController>();
LogTestController::getInstance().setOff<minifi::processors::GetTCP>();
diff --git a/extensions/http-curl/tests/C2MetricsTest.cpp b/extensions/http-curl/tests/C2MetricsTest.cpp
index 2d717e2e7..0acbfff6c 100644
--- a/extensions/http-curl/tests/C2MetricsTest.cpp
+++ b/extensions/http-curl/tests/C2MetricsTest.cpp
@@ -43,7 +43,6 @@ class VerifyC2Metrics : public VerifyC2Base {
void testSetup() override {
LogTestController::getInstance().setTrace<minifi::c2::C2Agent>();
- LogTestController::getInstance().setTrace<minifi::c2::C2Client>();
LogTestController::getInstance().setDebug<minifi::c2::RESTSender>();
LogTestController::getInstance().setDebug<minifi::FlowController>();
LogTestController::getInstance().setOff<minifi::processors::GetTCP>();
diff --git a/extensions/http-curl/tests/C2PauseResumeTest.cpp b/extensions/http-curl/tests/C2PauseResumeTest.cpp
index 6f0bfbc81..78bd1c54f 100644
--- a/extensions/http-curl/tests/C2PauseResumeTest.cpp
+++ b/extensions/http-curl/tests/C2PauseResumeTest.cpp
@@ -133,12 +133,11 @@ int main(int argc, char **argv) {
std::shared_ptr<core::ContentRepository> content_repo = std::make_shared<core::repository::VolatileContentRepository>();
content_repo->initialize(configuration);
- std::unique_ptr<core::FlowConfiguration> yaml_ptr = std::make_unique<core::YamlConfiguration>(
- core::ConfigurationContext{test_repo, content_repo, stream_factory, configuration, args.test_file});
+ auto yaml_ptr = std::make_shared<core::YamlConfiguration>(core::ConfigurationContext{test_repo, content_repo, stream_factory, configuration, args.test_file});
- std::shared_ptr<minifi::FlowController> controller = std::make_shared<minifi::FlowController>(
- test_repo, test_flow_repo, configuration, std::move(yaml_ptr), content_repo, DEFAULT_ROOT_GROUP_NAME,
- std::make_shared<utils::file::FileSystem>(), []{});
+ auto metrics_publisher_store = std::make_unique<minifi::state::MetricsPublisherStore>(configuration, test_repo, test_flow_repo, yaml_ptr);
+ std::shared_ptr<minifi::FlowController> controller = std::make_shared<minifi::FlowController>(test_repo, test_flow_repo, configuration,
+ std::move(yaml_ptr), content_repo, std::move(metrics_publisher_store));
core::YamlConfiguration yaml_config({test_repo, content_repo, stream_factory, configuration, args.test_file});
diff --git a/extensions/http-curl/tests/C2SameProcessorMetrics.cpp b/extensions/http-curl/tests/C2SameProcessorMetrics.cpp
index c7d1b29aa..a7144582f 100644
--- a/extensions/http-curl/tests/C2SameProcessorMetrics.cpp
+++ b/extensions/http-curl/tests/C2SameProcessorMetrics.cpp
@@ -41,7 +41,6 @@ class VerifyEmptyC2Metric : public VerifyC2Base {
void testSetup() override {
LogTestController::getInstance().setTrace<minifi::c2::C2Agent>();
- LogTestController::getInstance().setTrace<minifi::c2::C2Client>();
LogTestController::getInstance().setDebug<minifi::c2::RESTSender>();
LogTestController::getInstance().setDebug<minifi::FlowController>();
LogTestController::getInstance().setOff<minifi::processors::GetTCP>();
diff --git a/extensions/http-curl/tests/ControllerServiceIntegrationTests.cpp b/extensions/http-curl/tests/ControllerServiceIntegrationTests.cpp
index 9e92c8270..3c8abb228 100644
--- a/extensions/http-curl/tests/ControllerServiceIntegrationTests.cpp
+++ b/extensions/http-curl/tests/ControllerServiceIntegrationTests.cpp
@@ -70,11 +70,7 @@ int main(int argc, char **argv) {
std::unique_ptr<core::FlowConfiguration> yaml_ptr = std::make_unique<core::YamlConfiguration>(
core::ConfigurationContext{test_repo, content_repo, stream_factory, configuration, args.test_file});
- const auto controller = std::make_shared<minifi::FlowController>(test_repo, test_flow_repo, configuration, std::move(yaml_ptr),
- content_repo,
- DEFAULT_ROOT_GROUP_NAME,
- std::make_shared<utils::file::FileSystem>(),
- []{});
+ const auto controller = std::make_shared<minifi::FlowController>(test_repo, test_flow_repo, configuration, std::move(yaml_ptr), content_repo);
disabled = false;
std::shared_ptr<core::controller::ControllerServiceMap> map = std::make_shared<core::controller::ControllerServiceMap>();
diff --git a/extensions/http-curl/tests/VerifyInvokeHTTP.h b/extensions/http-curl/tests/VerifyInvokeHTTP.h
index 31b83e207..2996489cd 100644
--- a/extensions/http-curl/tests/VerifyInvokeHTTP.h
+++ b/extensions/http-curl/tests/VerifyInvokeHTTP.h
@@ -92,7 +92,7 @@ class VerifyInvokeHTTP : public HTTPIntegrationBase {
content_repo->initialize(configuration);
std::shared_ptr<minifi::io::StreamFactory> stream_factory = minifi::io::StreamFactory::getInstance(configuration);
auto yaml_ptr = std::make_unique<core::YamlConfiguration>(core::ConfigurationContext{test_repo, content_repo, stream_factory, configuration, flow_yml_path});
- flowController_ = std::make_unique<minifi::FlowController>(test_repo, test_flow_repo, configuration, std::move(yaml_ptr), content_repo, DEFAULT_ROOT_GROUP_NAME);
+ flowController_ = std::make_unique<minifi::FlowController>(test_repo, test_flow_repo, configuration, std::move(yaml_ptr), content_repo);
flowController_->load();
std::string url = scheme + "://localhost:" + getWebPort() + *path_;
@@ -123,8 +123,7 @@ class VerifyInvokeHTTP : public HTTPIntegrationBase {
}
void stopFlowController() {
- flowController_->unload();
- flowController_->stopC2();
+ flowController_->stop();
cleanup();
}
diff --git a/extensions/prometheus/PrometheusMetricsPublisher.cpp b/extensions/prometheus/PrometheusMetricsPublisher.cpp
index 56c866a4d..80b65033a 100644
--- a/extensions/prometheus/PrometheusMetricsPublisher.cpp
+++ b/extensions/prometheus/PrometheusMetricsPublisher.cpp
@@ -27,21 +27,19 @@
namespace org::apache::nifi::minifi::extensions::prometheus {
PrometheusMetricsPublisher::PrometheusMetricsPublisher(const std::string &name, const utils::Identifier &uuid, std::unique_ptr<MetricsExposer> exposer)
- : CoreComponent(name, uuid),
+ : state::MetricsPublisher(name, uuid),
exposer_(std::move(exposer)) {}
-void PrometheusMetricsPublisher::initialize(const std::shared_ptr<Configure>& configuration, state::response::ResponseNodeLoader& response_node_loader, core::ProcessGroup* root) {
- gsl_Expects(configuration);
- configuration_ = configuration;
- response_node_loader_ = &response_node_loader;
+void PrometheusMetricsPublisher::initialize(const std::shared_ptr<Configure>& configuration, const std::shared_ptr<state::response::ResponseNodeLoader>& response_node_loader) {
+ state::MetricsPublisher::initialize(configuration, response_node_loader);
if (!exposer_) {
exposer_ = std::make_unique<PrometheusExposerWrapper>(readPort());
}
loadAgentIdentifier();
- loadMetricNodes(root);
}
uint32_t PrometheusMetricsPublisher::readPort() {
+ gsl_Expects(configuration_);
if (auto port = configuration_->get(Configuration::nifi_metrics_publisher_prometheus_metrics_publisher_port)) {
return std::stoul(*port);
}
@@ -58,9 +56,9 @@ void PrometheusMetricsPublisher::clearMetricNodes() {
gauge_collections_.clear();
}
-void PrometheusMetricsPublisher::loadMetricNodes(core::ProcessGroup* root) {
+void PrometheusMetricsPublisher::loadMetricNodes() {
std::lock_guard<std::mutex> lock(registered_metrics_mutex_);
- auto nodes = getMetricNodes(root);
+ auto nodes = getMetricNodes();
for (const auto& metric_node : nodes) {
logger_->log_debug("Registering metric node '%s'", metric_node->getName());
@@ -69,12 +67,13 @@ void PrometheusMetricsPublisher::loadMetricNodes(core::ProcessGroup* root) {
}
}
-std::vector<std::shared_ptr<state::response::ResponseNode>> PrometheusMetricsPublisher::getMetricNodes(core::ProcessGroup* root) {
- std::vector<std::shared_ptr<state::response::ResponseNode>> nodes;
+std::vector<state::response::SharedResponseNode> PrometheusMetricsPublisher::getMetricNodes() {
+ gsl_Expects(response_node_loader_ && configuration_);
+ std::vector<state::response::SharedResponseNode> nodes;
if (auto metric_classes_str = configuration_->get(minifi::Configuration::nifi_metrics_publisher_metrics)) {
auto metric_classes = utils::StringUtils::split(*metric_classes_str, ",");
for (const std::string& clazz : metric_classes) {
- auto response_nodes = response_node_loader_->loadResponseNodes(clazz, root);
+ auto response_nodes = response_node_loader_->loadResponseNodes(clazz);
if (response_nodes.empty()) {
logger_->log_warn("Metric class '%s' could not be loaded.", clazz);
continue;
@@ -86,6 +85,7 @@ std::vector<std::shared_ptr<state::response::ResponseNode>> PrometheusMetricsPub
}
void PrometheusMetricsPublisher::loadAgentIdentifier() {
+ gsl_Expects(configuration_);
auto agent_identifier = configuration_->get(Configure::nifi_metrics_publisher_agent_identifier);
if (agent_identifier && !agent_identifier->empty()) {
agent_identifier_ = *agent_identifier;
diff --git a/extensions/prometheus/PrometheusMetricsPublisher.h b/extensions/prometheus/PrometheusMetricsPublisher.h
index 58451b077..131502f4a 100644
--- a/extensions/prometheus/PrometheusMetricsPublisher.h
+++ b/extensions/prometheus/PrometheusMetricsPublisher.h
@@ -31,26 +31,24 @@
namespace org::apache::nifi::minifi::extensions::prometheus {
-class PrometheusMetricsPublisher : public core::CoreComponent, public state::MetricsPublisher {
+class PrometheusMetricsPublisher : public state::MetricsPublisher {
public:
explicit PrometheusMetricsPublisher(const std::string &name, const utils::Identifier &uuid = {}, std::unique_ptr<MetricsExposer> exposer = nullptr);
EXTENSIONAPI static constexpr const char* Description = "HTTP server that exposes MiNiFi metrics for Prometheus to scrape";
- void initialize(const std::shared_ptr<Configure>& configuration, state::response::ResponseNodeLoader& response_node_loader, core::ProcessGroup* root) override;
+ void initialize(const std::shared_ptr<Configure>& configuration, const std::shared_ptr<state::response::ResponseNodeLoader>& response_node_loader) override;
void clearMetricNodes() override;
- void loadMetricNodes(core::ProcessGroup* root) override;
+ void loadMetricNodes() override;
private:
uint32_t readPort();
- std::vector<std::shared_ptr<state::response::ResponseNode>> getMetricNodes(core::ProcessGroup* root);
+ std::vector<state::response::SharedResponseNode> getMetricNodes();
void loadAgentIdentifier();
std::mutex registered_metrics_mutex_;
std::vector<std::shared_ptr<PublishedMetricGaugeCollection>> gauge_collections_;
std::unique_ptr<MetricsExposer> exposer_;
- std::shared_ptr<Configure> configuration_;
- state::response::ResponseNodeLoader* response_node_loader_ = nullptr;
std::string agent_identifier_;
std::shared_ptr<core::logging::Logger> logger_{core::logging::LoggerFactory<PrometheusMetricsPublisher>::getLogger()};
};
diff --git a/extensions/prometheus/tests/PrometheusMetricsPublisherTest.cpp b/extensions/prometheus/tests/PrometheusMetricsPublisherTest.cpp
index 98e8d6afb..6b39468f5 100644
--- a/extensions/prometheus/tests/PrometheusMetricsPublisherTest.cpp
+++ b/extensions/prometheus/tests/PrometheusMetricsPublisherTest.cpp
@@ -52,7 +52,7 @@ class PrometheusPublisherTestFixture {
: configuration_(std::make_shared<Configure>()),
provenance_repo_(core::createRepository("provenancerepository")),
flow_file_repo_(core::createRepository("flowfilerepository")),
- response_node_loader_(configuration_, provenance_repo_, flow_file_repo_, nullptr) {
+ response_node_loader_(std::make_shared<state::response::ResponseNodeLoader>(configuration_, provenance_repo_, flow_file_repo_, nullptr)) {
std::unique_ptr<DummyMetricsExposer> dummy_exposer;
if (user_dummy_exposer) {
dummy_exposer = std::make_unique<DummyMetricsExposer>();
@@ -65,7 +65,7 @@ class PrometheusPublisherTestFixture {
std::shared_ptr<Configure> configuration_;
std::shared_ptr<core::Repository> provenance_repo_;
std::shared_ptr<core::Repository> flow_file_repo_;
- state::response::ResponseNodeLoader response_node_loader_;
+ std::shared_ptr<state::response::ResponseNodeLoader> response_node_loader_;
std::unique_ptr<PrometheusMetricsPublisher> publisher_;
DummyMetricsExposer* exposer_ = nullptr;
};
@@ -81,18 +81,19 @@ class PrometheusPublisherTestFixtureWithDummyExposer : public PrometheusPublishe
};
TEST_CASE_METHOD(PrometheusPublisherTestFixtureWithRealExposer, "Test prometheus empty port", "[prometheusPublisherTest]") {
- REQUIRE_THROWS_WITH(publisher_->initialize(configuration_, response_node_loader_, nullptr), "General Operation: Port not configured for Prometheus metrics publisher!");
+ REQUIRE_THROWS_WITH(publisher_->initialize(configuration_, response_node_loader_), "General Operation: Port not configured for Prometheus metrics publisher!");
}
TEST_CASE_METHOD(PrometheusPublisherTestFixtureWithRealExposer, "Test prometheus invalid port", "[prometheusPublisherTest]") {
configuration_->set(Configure::nifi_metrics_publisher_prometheus_metrics_publisher_port, "invalid");
- REQUIRE_THROWS_AS(publisher_->initialize(configuration_, response_node_loader_, nullptr), std::exception);
+ REQUIRE_THROWS_AS(publisher_->initialize(configuration_, response_node_loader_), std::exception);
}
TEST_CASE_METHOD(PrometheusPublisherTestFixtureWithDummyExposer, "Test adding metrics to exposer", "[prometheusPublisherTest]") {
configuration_->set(Configure::nifi_metrics_publisher_metrics, "QueueMetrics,RepositoryMetrics,DeviceInfoNode,FlowInformation,AgentInformation,InvalidMetrics,GetFileMetrics,GetTCPMetrics");
configuration_->set(Configure::nifi_metrics_publisher_agent_identifier, "AgentId-1");
- publisher_->initialize(configuration_, response_node_loader_, nullptr);
+ publisher_->initialize(configuration_, response_node_loader_);
+ publisher_->loadMetricNodes();
auto stored_metrics = exposer_->getMetrics();
std::vector<std::string> valid_metrics_without_flow = {"QueueMetrics", "RepositoryMetrics", "DeviceInfoNode", "FlowInformation", "AgentInformation"};
REQUIRE(stored_metrics.size() == valid_metrics_without_flow.size());
diff --git a/extensions/python/tests/PythonManifestTests.cpp b/extensions/python/tests/PythonManifestTests.cpp
index 59b3e0e33..2867f125b 100644
--- a/extensions/python/tests/PythonManifestTests.cpp
+++ b/extensions/python/tests/PythonManifestTests.cpp
@@ -24,6 +24,7 @@
#include "Catch.h"
#include "flow-tests/TestControllerWithFlow.h"
#include "EmptyFlow.h"
+#include "c2/C2MetricsPublisher.h"
using minifi::state::response::SerializedResponseNode;
@@ -58,13 +59,15 @@ TEST_CASE("Python processor's description is part of the manifest") {
" proc.addProperty('Prop1', 'A great property', 'banana', True, False)\n";
controller.configuration_->set(minifi::Configuration::nifi_python_processor_dir, python_dir.string());
- controller.configuration_->set(minifi::Configuration::nifi_extension_path, "*minifi-python-script*");
+ controller.configuration_->set(minifi::Configuration::nifi_extension_path, "*minifi-python-script*,*minifi-http-curl*");
core::extension::ExtensionManager::get().initialize(controller.configuration_);
controller.setupFlow();
- auto agent_info = controller.controller_->getAgentManifest();
+ auto c2_metrics_publisher = std::static_pointer_cast<minifi::c2::C2MetricsPublisher>(controller.metrics_publisher_store_->getMetricsPublisher(minifi::c2::C2_METRICS_PUBLISHER).lock());
+
+ auto agent_info = c2_metrics_publisher->getAgentManifest();
auto& manifest = getNode(agent_info.serialized_nodes, "agentManifest");
diff --git a/libminifi/include/FlowController.h b/libminifi/include/FlowController.h
index d412427d3..dc3cf6c3f 100644
--- a/libminifi/include/FlowController.h
+++ b/libminifi/include/FlowController.h
@@ -47,7 +47,7 @@
#include "core/state/nodes/FlowInformation.h"
#include "core/state/nodes/MetricsBase.h"
#include "core/state/UpdateController.h"
-#include "c2/C2Client.h"
+#include "c2/C2Agent.h"
#include "CronDrivenSchedulingAgent.h"
#include "EventDrivenSchedulingAgent.h"
#include "FlowControlProtocol.h"
@@ -58,6 +58,8 @@
#include "utils/file/FileSystem.h"
#include "core/state/nodes/ResponseNodeLoader.h"
#include "core/state/MetricsPublisher.h"
+#include "core/state/MetricsPublisherStore.h"
+#include "RootProcessGroupWrapper.h"
namespace org::apache::nifi::minifi {
@@ -65,24 +67,12 @@ namespace state {
class ProcessorController;
} // namespace state
-#define DEFAULT_ROOT_GROUP_NAME ""
-
-/**
- * Flow Controller class. Generally used by FlowController factory
- * as a singleton.
- */
-class FlowController : public core::controller::ForwardingControllerServiceProvider, public state::StateMonitor, public c2::C2Client {
+class FlowController : public core::controller::ForwardingControllerServiceProvider, public state::StateMonitor {
public:
FlowController(std::shared_ptr<core::Repository> provenance_repo, std::shared_ptr<core::Repository> flow_file_repo,
- std::shared_ptr<Configure> configure, std::unique_ptr<core::FlowConfiguration> flow_configuration,
- std::shared_ptr<core::ContentRepository> content_repo, const std::string& name = DEFAULT_ROOT_GROUP_NAME,
- std::shared_ptr<utils::file::FileSystem> filesystem = std::make_shared<utils::file::FileSystem>(),
- std::function<void()> request_restart = []{});
-
- FlowController(std::shared_ptr<core::Repository> provenance_repo, std::shared_ptr<core::Repository> flow_file_repo,
- std::shared_ptr<Configure> configure, std::unique_ptr<core::FlowConfiguration> flow_configuration,
- std::shared_ptr<core::ContentRepository> content_repo, std::shared_ptr<utils::file::FileSystem> filesystem,
- std::function<void()> request_restart = []{});
+ std::shared_ptr<Configure> configure, std::shared_ptr<core::FlowConfiguration> flow_configuration,
+ std::shared_ptr<core::ContentRepository> content_repo, std::unique_ptr<state::MetricsPublisherStore> metrics_publisher_store = nullptr,
+ std::shared_ptr<utils::file::FileSystem> filesystem = std::make_shared<utils::file::FileSystem>(), std::function<void()> request_restart = []{});
~FlowController() override;
@@ -90,7 +80,12 @@ class FlowController : public core::controller::ForwardingControllerServiceProvi
return this->provenance_repo_;
}
- virtual void load(std::unique_ptr<core::ProcessGroup> root = nullptr, bool reload = false);
+ virtual void load(bool reload = false);
+
+ void load(std::unique_ptr<core::ProcessGroup> root, bool reload = false) {
+ root_wrapper_.setNewRoot(std::move(root));
+ load(reload);
+ }
bool isRunning() const override {
return running_.load() || updating_.load();
@@ -120,19 +115,8 @@ class FlowController : public core::controller::ForwardingControllerServiceProvi
int16_t applyUpdate(const std::string& /*source*/, const std::shared_ptr<state::Update>&) override { return -1; }
// Asynchronous function trigger unloading and wait for a period of time
virtual void waitUnload(uint64_t timeToWaitMs);
- // Unload the current flow, clean the root process group and all its children
- virtual void unload();
void updatePropertyValue(std::string processorName, std::string propertyName, std::string propertyValue) {
- if (root_ != nullptr)
- root_->updatePropertyValue(std::move(processorName), std::move(propertyName), std::move(propertyValue));
- }
-
- void setSerialNumber(std::string number) {
- serial_number_ = std::move(number);
- }
-
- std::string getSerialNumber() {
- return serial_number_;
+ root_wrapper_.updatePropertyValue(std::move(processorName), std::move(propertyName), std::move(propertyValue));
}
// validate and apply passing configuration payload
@@ -142,10 +126,7 @@ class FlowController : public core::controller::ForwardingControllerServiceProvi
bool applyConfiguration(const std::string &source, const std::string &configurePayload, const std::optional<std::string>& flow_id = std::nullopt);
std::string getName() const override {
- if (root_ != nullptr)
- return root_->getName();
- else
- return "";
+ return root_wrapper_.getName();
}
std::string getComponentName() const override {
@@ -153,29 +134,13 @@ class FlowController : public core::controller::ForwardingControllerServiceProvi
}
utils::Identifier getComponentUUID() const override {
- if (!root_) {
- return {};
- }
- return root_->getUUID();
+ return root_wrapper_.getComponentUUID();
}
virtual std::string getVersion() {
- if (root_ != nullptr)
- return std::to_string(root_->getVersion());
- else
- return "0";
+ return root_wrapper_.getVersion();
}
- utils::Identifier getControllerUUID() const override {
- return getUUID();
- }
-
- /**
- * Retrieves the agent manifest to be sent as a response to C2 DESCRIBE manifest
- * @return the agent manifest response node
- */
- state::response::NodeReporter::ReportedNode getAgentManifest() override;
-
uint64_t getUptime() override;
std::vector<BackTrace> getTraces() override;
@@ -190,14 +155,12 @@ class FlowController : public core::controller::ForwardingControllerServiceProvi
*/
std::unique_ptr<core::ProcessGroup> loadInitialFlow();
- void loadMetricsPublisher();
-
- protected:
void loadFlowRepo();
+ std::vector<state::StateController*> getAllComponents();
+ state::StateController* getComponent(const std::string& id_or_name);
+ gsl::not_null<std::unique_ptr<state::ProcessorController>> createController(core::Processor& processor);
+ std::unique_ptr<core::ProcessGroup> updateFromPayload(const std::string& url, const std::string& config_payload, const std::optional<std::string>& flow_id = std::nullopt);
- std::optional<std::chrono::milliseconds> loadShutdownTimeoutFromConfiguration();
-
- private:
template <typename T, typename = typename std::enable_if<std::is_base_of<SchedulingAgent, T>::value>::type>
void conditionalReloadScheduler(std::unique_ptr<T>& scheduler, const bool condition) {
if (condition) {
@@ -205,40 +168,27 @@ class FlowController : public core::controller::ForwardingControllerServiceProvi
}
}
- protected:
std::recursive_mutex mutex_;
-
std::atomic<bool> running_;
std::atomic<bool> updating_;
-
std::atomic<bool> initialized_;
std::unique_ptr<TimerDrivenSchedulingAgent> timer_scheduler_;
std::unique_ptr<EventDrivenSchedulingAgent> event_scheduler_;
std::unique_ptr<CronDrivenSchedulingAgent> cron_scheduler_;
std::unique_ptr<FlowControlProtocol> protocol_;
std::chrono::steady_clock::time_point start_time_;
-
- private:
- std::vector<state::StateController*> getAllComponents();
-
- state::StateController* getComponent(const std::string& id_or_name);
-
- state::StateController* getProcessorController(const std::string& id_or_name,
- const std::function<gsl::not_null<std::unique_ptr<state::ProcessorController>>(core::Processor&)>& controllerFactory);
-
- std::vector<state::StateController*> getAllProcessorControllers(
- const std::function<gsl::not_null<std::unique_ptr<state::ProcessorController>>(core::Processor&)>& controllerFactory);
-
- gsl::not_null<std::unique_ptr<state::ProcessorController>> createController(core::Processor& processor);
-
- std::chrono::milliseconds shutdown_check_interval_{1000};
+ std::shared_ptr<Configure> configuration_;
+ std::shared_ptr<core::Repository> provenance_repo_;
+ std::shared_ptr<core::Repository> flow_file_repo_;
+ std::shared_ptr<core::ContentRepository> content_repo_;
+ std::shared_ptr<core::FlowConfiguration> flow_configuration_;
+ std::unique_ptr<state::MetricsPublisherStore> metrics_publisher_store_;
+ RootProcessGroupWrapper root_wrapper_;
+ std::unique_ptr<c2::C2Agent> c2_agent_{};
std::shared_ptr<core::logging::Logger> logger_ = core::logging::LoggerFactory<FlowController>::getLogger();
- std::string serial_number_;
// Thread pool for schedulers
utils::ThreadPool<utils::TaskRescheduleInfo> thread_pool_;
- std::map<utils::Identifier, std::unique_ptr<state::ProcessorController>> processor_to_controller_;
- std::unique_ptr<state::MetricsPublisher> metrics_publisher_;
};
} // namespace org::apache::nifi::minifi
diff --git a/libminifi/include/RootProcessGroupWrapper.h b/libminifi/include/RootProcessGroupWrapper.h
new file mode 100644
index 000000000..b4ca610bd
--- /dev/null
+++ b/libminifi/include/RootProcessGroupWrapper.h
@@ -0,0 +1,91 @@
+/**
+ * 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 <memory>
+#include <map>
+#include <unordered_map>
+#include <string>
+#include <functional>
+#include <optional>
+#include <utility>
+#include <vector>
+
+#include "core/ProcessGroup.h"
+#include "utils/Id.h"
+#include "core/state/ProcessorController.h"
+#include "core/state/MetricsPublisher.h"
+#include "core/state/MetricsPublisherStore.h"
+#include "utils/gsl.h"
+#include "core/logging/Logger.h"
+
+namespace org::apache::nifi::minifi {
+
+class RootProcessGroupWrapper {
+ public:
+ explicit RootProcessGroupWrapper(std::shared_ptr<Configure> configuration, state::MetricsPublisherStore* metrics_publisher_store = nullptr)
+ : configuration_(std::move(configuration)),
+ metrics_publisher_store_(metrics_publisher_store) {
+ }
+
+ ~RootProcessGroupWrapper() {
+ if (metrics_publisher_store_) {
+ metrics_publisher_store_->clearMetricNodes();
+ }
+ }
+
+ void updatePropertyValue(const std::string& processor_name, const std::string& property_name, const std::string& property_value);
+ std::string getName() const;
+ utils::Identifier getComponentUUID() const;
+ std::string getVersion() const;
+ void setNewRoot(std::unique_ptr<core::ProcessGroup> new_root);
+ void restoreBackup();
+ void clearBackup();
+ void stopProcessing(TimerDrivenSchedulingAgent& timer_scheduler,
+ EventDrivenSchedulingAgent& event_scheduler,
+ CronDrivenSchedulingAgent& cron_scheduler);
+ void drainConnections();
+ bool initialized() const {
+ return root_ != nullptr;
+ }
+
+ void getConnections(std::map<std::string, core::Connectable*>& connectionMap);
+ void getFlowFileContainers(std::map<std::string, core::Connectable*>& containers) const;
+ bool startProcessing(TimerDrivenSchedulingAgent& timer_scheduler,
+ EventDrivenSchedulingAgent& event_scheduler,
+ CronDrivenSchedulingAgent& cron_scheduler);
+ void clearConnection(const std::string &connection);
+
+ state::StateController* getProcessorController(const std::string& id_or_name,
+ const std::function<gsl::not_null<std::unique_ptr<state::ProcessorController>>(core::Processor&)>& controllerFactory);
+
+ std::optional<std::vector<state::StateController*>> getAllProcessorControllers(
+ const std::function<gsl::not_null<std::unique_ptr<state::ProcessorController>>(core::Processor&)>& controllerFactory);
+
+ private:
+ std::optional<std::chrono::milliseconds> loadShutdownTimeoutFromConfiguration();
+
+ std::shared_ptr<Configure> configuration_;
+ std::unique_ptr<core::ProcessGroup> root_;
+ std::unique_ptr<core::ProcessGroup> backup_root_;
+ state::MetricsPublisherStore* metrics_publisher_store_{};
+ std::unordered_map<utils::Identifier, std::unique_ptr<state::ProcessorController>> processor_to_controller_;
+ std::chrono::milliseconds shutdown_check_interval_{1000};
+ std::shared_ptr<core::logging::Logger> logger_ = core::logging::LoggerFactory<RootProcessGroupWrapper>::getLogger();
+};
+
+} // namespace org::apache::nifi::minifi
diff --git a/libminifi/include/c2/C2Agent.h b/libminifi/include/c2/C2Agent.h
index 9ad280cbb..f2c997dce 100644
--- a/libminifi/include/c2/C2Agent.h
+++ b/libminifi/include/c2/C2Agent.h
@@ -42,13 +42,9 @@
#include "utils/MinifiConcurrentQueue.h"
#include "utils/ThreadPool.h"
#include "utils/file/FileSystem.h"
+#include "C2Utils.h"
-namespace org {
-namespace apache {
-namespace nifi {
-namespace minifi {
-
-namespace c2 {
+namespace org::apache::nifi::minifi::c2 {
/**
* Purpose and Justification: C2 agent will be the mechanism that will abstract the protocol to do the work.
@@ -63,12 +59,8 @@ namespace c2 {
*/
class C2Agent : public state::UpdateController {
public:
- static constexpr const char* UPDATE_NAME = "C2UpdatePolicy";
-
- C2Agent(core::controller::ControllerServiceProvider *controller,
- state::Pausable *pause_handler,
- state::StateMonitor* updateSink,
- std::shared_ptr<Configure> configuration,
+ C2Agent(std::shared_ptr<Configure> configuration,
+ std::weak_ptr<state::response::NodeReporter> node_reporter,
std::shared_ptr<utils::file::FileSystem> filesystem,
std::function<void()> request_restart);
@@ -76,8 +68,8 @@ class C2Agent : public state::UpdateController {
delete protocol_.load();
}
+ void initialize(core::controller::ControllerServiceProvider *controller, state::Pausable *pause_handler, state::StateMonitor* update_sink);
void start() override;
-
void stop() override;
/**
@@ -230,7 +222,7 @@ class C2Agent : public state::UpdateController {
std::vector<std::unique_ptr<C2Trigger>> triggers_;
- std::atomic<C2Protocol*> protocol_;
+ std::atomic<C2Protocol*> protocol_{};
std::shared_ptr<core::logging::Logger> logger_ = core::logging::LoggerFactory<C2Agent>::getLogger();
@@ -238,17 +230,14 @@ class C2Agent : public state::UpdateController {
std::vector<utils::Identifier> task_ids_;
- bool manifest_sent_;
+ bool manifest_sent_{false};
const uint64_t C2RESPONSE_POLL_MS = 100;
+ std::weak_ptr<state::response::NodeReporter> node_reporter_;
std::atomic<bool> restart_needed_ = false;
std::function<void()> request_restart_;
};
-} // namespace c2
-} // namespace minifi
-} // namespace nifi
-} // namespace apache
-} // namespace org
+} // namespace org::apache::nifi::minifi::c2
diff --git a/libminifi/include/c2/C2Client.h b/libminifi/include/c2/C2MetricsPublisher.h
similarity index 53%
rename from libminifi/include/c2/C2Client.h
rename to libminifi/include/c2/C2MetricsPublisher.h
index 816474d76..1aa626439 100644
--- a/libminifi/include/c2/C2Client.h
+++ b/libminifi/include/c2/C2MetricsPublisher.h
@@ -24,69 +24,48 @@
#include <optional>
#include <string>
#include <vector>
-#include <unordered_set>
+#include <functional>
#include "c2/C2Agent.h"
#include "core/controller/ControllerServiceProvider.h"
#include "properties/Configure.h"
#include "core/logging/Logger.h"
#include "core/state/nodes/MetricsBase.h"
-#include "core/Repository.h"
-#include "core/ContentRepository.h"
#include "core/ProcessGroup.h"
-#include "core/Flow.h"
+#include "core/Core.h"
#include "utils/file/FileSystem.h"
#include "core/state/nodes/ResponseNodeLoader.h"
#include "utils/Id.h"
+#include "core/state/MetricsPublisher.h"
namespace org::apache::nifi::minifi::c2 {
-class C2Client : public core::Flow, public state::response::NodeReporter {
+class C2MetricsPublisher : public state::response::NodeReporter, public state::MetricsPublisher {
public:
- C2Client(
- std::shared_ptr<Configure> configuration, std::shared_ptr<core::Repository> provenance_repo,
- std::shared_ptr<core::Repository> flow_file_repo, std::shared_ptr<core::ContentRepository> content_repo,
- std::unique_ptr<core::FlowConfiguration> flow_configuration, std::shared_ptr<utils::file::FileSystem> filesystem,
- std::function<void()> request_restart,
- std::shared_ptr<core::logging::Logger> logger = core::logging::LoggerFactory<C2Client>::getLogger());
+ using MetricsPublisher::MetricsPublisher;
+
+ MINIFIAPI static constexpr const char* Description = "Class that provides C2 metrics to the C2Agent";
- void initialize(core::controller::ControllerServiceProvider *controller, state::Pausable *pause_handler, state::StateMonitor* update_sink);
std::optional<state::response::NodeReporter::ReportedNode> getMetricsNode(const std::string& metrics_class) const override;
std::vector<state::response::NodeReporter::ReportedNode> getHeartbeatNodes(bool include_manifest) const override;
+ state::response::NodeReporter::ReportedNode getAgentManifest() override;
- void stopC2();
- void initializeResponseNodes(core::ProcessGroup* root);
- void clearResponseNodes();
-
- protected:
- bool isC2Enabled() const;
- std::optional<std::string> fetchFlow(const std::string& uri) const;
+ void clearMetricNodes() override;
+ void loadMetricNodes() override;
private:
void loadC2ResponseConfiguration(const std::string &prefix);
- std::shared_ptr<state::response::ResponseNode> loadC2ResponseConfiguration(const std::string &prefix, std::shared_ptr<state::response::ResponseNode> prev_node);
- void loadNodeClasses(const std::string& class_definitions, const std::shared_ptr<state::response::ResponseNode>& new_node);
+ state::response::SharedResponseNode loadC2ResponseConfiguration(const std::string &prefix, state::response::SharedResponseNode prev_node);
+ void loadNodeClasses(const std::string& class_definitions, const state::response::SharedResponseNode& new_node);
- protected:
- std::shared_ptr<Configure> configuration_;
- std::shared_ptr<utils::file::FileSystem> filesystem_;
-
- private:
- std::unique_ptr<C2Agent> c2_agent_;
- std::mutex initialization_mutex_;
- bool initialized_ = false;
- std::shared_ptr<core::logging::Logger> logger_;
mutable std::mutex metrics_mutex_;
// Name and response node value of the root response nodes defined in nifi.c2.root.classes and nifi.c2.root.class.definitions
// In case a root class is defined to be a processor metric there can be multiple response nodes if the same processor is defined
// multiple times in the flow
- std::unordered_map<std::string, std::vector<std::shared_ptr<state::response::ResponseNode>>> root_response_nodes_;
+ std::unordered_map<std::string, std::vector<state::response::SharedResponseNode>> root_response_nodes_;
- protected:
- std::atomic<bool> flow_update_{false};
- std::function<void()> request_restart_;
- state::response::ResponseNodeLoader response_node_loader_;
+ std::shared_ptr<core::logging::Logger> logger_ = core::logging::LoggerFactory<C2MetricsPublisher>::getLogger();
};
} // namespace org::apache::nifi::minifi::c2
diff --git a/libminifi/include/core/state/MetricsPublisher.h b/libminifi/include/c2/C2Utils.h
similarity index 65%
copy from libminifi/include/core/state/MetricsPublisher.h
copy to libminifi/include/c2/C2Utils.h
index f7cbc0e7d..7eb7cb04d 100644
--- a/libminifi/include/core/state/MetricsPublisher.h
+++ b/libminifi/include/c2/C2Utils.h
@@ -15,21 +15,20 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
+
#pragma once
+#include <string>
#include <memory>
-#include "nodes/ResponseNodeLoader.h"
#include "properties/Configure.h"
+#include "utils/StringUtils.h"
+
+namespace org::apache::nifi::minifi::c2 {
-namespace org::apache::nifi::minifi::state {
+inline constexpr const char* UPDATE_NAME = "C2UpdatePolicy";
+inline constexpr const char* C2_METRICS_PUBLISHER = "C2MetricsPublisher";
-class MetricsPublisher {
- public:
- virtual void initialize(const std::shared_ptr<Configure>& configuration, response::ResponseNodeLoader& response_node_loader, core::ProcessGroup* root) = 0;
- virtual void clearMetricNodes() = 0;
- virtual void loadMetricNodes(core::ProcessGroup* root) = 0;
- virtual ~MetricsPublisher() = default;
-};
+bool isC2Enabled(const std::shared_ptr<Configure>& configuration);
-} // namespace org::apache::nifi::minifi::state
+} // namespace org::apache::nifi::minifi::c2
diff --git a/libminifi/include/core/Flow.h b/libminifi/include/core/Flow.h
deleted file mode 100644
index fe8e5d56a..000000000
--- a/libminifi/include/core/Flow.h
+++ /dev/null
@@ -1,59 +0,0 @@
-/**
- *
- * 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 <memory>
-#include <utility>
-#include "core/ProcessGroup.h"
-#include "core/Repository.h"
-#include "core/ContentRepository.h"
-#include "core/FlowConfiguration.h"
-#include "utils/Id.h"
-
-namespace org {
-namespace apache {
-namespace nifi {
-namespace minifi {
-namespace core {
-
-class Flow {
- protected:
- Flow(std::shared_ptr<core::Repository> provenance_repo, std::shared_ptr<core::Repository> flow_file_repo,
- std::shared_ptr<core::ContentRepository> content_repo, std::unique_ptr<core::FlowConfiguration> flow_configuration)
- : provenance_repo_(std::move(provenance_repo)),
- flow_file_repo_(std::move(flow_file_repo)),
- content_repo_(std::move(content_repo)),
- flow_configuration_(std::move(flow_configuration)) {}
-
- virtual ~Flow() = default;
-
- virtual utils::Identifier getControllerUUID() const = 0;
-
- std::unique_ptr<core::ProcessGroup> root_;
- std::shared_ptr<core::Repository> provenance_repo_;
- std::shared_ptr<core::Repository> flow_file_repo_;
- std::shared_ptr<core::ContentRepository> content_repo_;
- std::unique_ptr<core::FlowConfiguration> flow_configuration_;
-};
-
-} // namespace core
-} // namespace minifi
-} // namespace nifi
-} // namespace apache
-} // namespace org
diff --git a/libminifi/include/core/FlowConfiguration.h b/libminifi/include/core/FlowConfiguration.h
index be4e75df2..850e04c3c 100644
--- a/libminifi/include/core/FlowConfiguration.h
+++ b/libminifi/include/core/FlowConfiguration.h
@@ -124,7 +124,7 @@ class FlowConfiguration : public CoreComponent {
return nullptr;
}
- std::shared_ptr<core::controller::StandardControllerServiceProvider> &getControllerServiceProvider() {
+ std::shared_ptr<core::controller::StandardControllerServiceProvider> getControllerServiceProvider() const {
return service_provider_;
}
diff --git a/libminifi/include/core/Processor.h b/libminifi/include/core/Processor.h
index b4a72596b..637d13598 100644
--- a/libminifi/include/core/Processor.h
+++ b/libminifi/include/core/Processor.h
@@ -221,7 +221,7 @@ class Processor : public Connectable, public ConfigurableComponent, public state
virtual annotation::Input getInputRequirement() const = 0;
- std::shared_ptr<state::response::ResponseNode> getResponseNodes() override {
+ state::response::SharedResponseNode getResponseNode() override {
return metrics_;
}
diff --git a/libminifi/include/core/state/ConnectionStore.h b/libminifi/include/core/state/ConnectionStore.h
index a5cfa4eff..4037a0433 100644
--- a/libminifi/include/core/state/ConnectionStore.h
+++ b/libminifi/include/core/state/ConnectionStore.h
@@ -28,6 +28,10 @@ namespace org::apache::nifi::minifi::state {
class ConnectionStore {
public:
+ const std::unordered_map<utils::Identifier, minifi::Connection*>& getConnections() {
+ return connections_;
+ }
+
void updateConnection(minifi::Connection* connection) {
if (nullptr != connection) {
connections_[connection->getUUID()] = connection;
@@ -51,8 +55,6 @@ class ConnectionStore {
return metrics;
}
- virtual ~ConnectionStore() = default;
-
protected:
std::unordered_map<utils::Identifier, minifi::Connection*> connections_;
};
diff --git a/libminifi/include/core/state/MetricsPublisher.h b/libminifi/include/core/state/MetricsPublisher.h
index f7cbc0e7d..19e5b565c 100644
--- a/libminifi/include/core/state/MetricsPublisher.h
+++ b/libminifi/include/core/state/MetricsPublisher.h
@@ -21,15 +21,25 @@
#include "nodes/ResponseNodeLoader.h"
#include "properties/Configure.h"
+#include "core/Core.h"
namespace org::apache::nifi::minifi::state {
-class MetricsPublisher {
+class MetricsPublisher : public core::CoreComponent {
public:
- virtual void initialize(const std::shared_ptr<Configure>& configuration, response::ResponseNodeLoader& response_node_loader, core::ProcessGroup* root) = 0;
+ using CoreComponent::CoreComponent;
+ virtual void initialize(const std::shared_ptr<Configure>& configuration, const std::shared_ptr<state::response::ResponseNodeLoader>& response_node_loader) {
+ gsl_Expects(configuration && response_node_loader);
+ configuration_ = configuration;
+ response_node_loader_ = response_node_loader;
+ }
virtual void clearMetricNodes() = 0;
- virtual void loadMetricNodes(core::ProcessGroup* root) = 0;
+ virtual void loadMetricNodes() = 0;
virtual ~MetricsPublisher() = default;
+
+ protected:
+ std::shared_ptr<Configure> configuration_;
+ std::shared_ptr<state::response::ResponseNodeLoader> response_node_loader_;
};
} // namespace org::apache::nifi::minifi::state
diff --git a/libminifi/include/core/state/MetricsPublisher.h b/libminifi/include/core/state/MetricsPublisherFactory.h
similarity index 65%
copy from libminifi/include/core/state/MetricsPublisher.h
copy to libminifi/include/core/state/MetricsPublisherFactory.h
index f7cbc0e7d..e483d36e6 100644
--- a/libminifi/include/core/state/MetricsPublisher.h
+++ b/libminifi/include/core/state/MetricsPublisherFactory.h
@@ -18,18 +18,15 @@
#pragma once
#include <memory>
+#include <string>
-#include "nodes/ResponseNodeLoader.h"
-#include "properties/Configure.h"
+#include "MetricsPublisher.h"
+#include "utils/gsl.h"
namespace org::apache::nifi::minifi::state {
-class MetricsPublisher {
- public:
- virtual void initialize(const std::shared_ptr<Configure>& configuration, response::ResponseNodeLoader& response_node_loader, core::ProcessGroup* root) = 0;
- virtual void clearMetricNodes() = 0;
- virtual void loadMetricNodes(core::ProcessGroup* root) = 0;
- virtual ~MetricsPublisher() = default;
-};
+gsl::not_null<std::unique_ptr<MetricsPublisher>> createMetricsPublisher(const std::string& name, const std::shared_ptr<Configure>& configuration,
+ const std::shared_ptr<state::response::ResponseNodeLoader>& response_node_loader);
+std::unique_ptr<MetricsPublisher> createMetricsPublisher(const std::shared_ptr<Configure>& configuration, const std::shared_ptr<state::response::ResponseNodeLoader>& response_node_loader);
} // namespace org::apache::nifi::minifi::state
diff --git a/libminifi/include/core/state/MetricsPublisherStore.h b/libminifi/include/core/state/MetricsPublisherStore.h
new file mode 100644
index 000000000..90eab8aa1
--- /dev/null
+++ b/libminifi/include/core/state/MetricsPublisherStore.h
@@ -0,0 +1,55 @@
+/**
+ *
+ * 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 <unordered_map>
+#include <string>
+#include <utility>
+#include <memory>
+
+#include "MetricsPublisher.h"
+#include "core/state/nodes/ResponseNodeLoader.h"
+#include "utils/gsl.h"
+#include "core/ProcessGroup.h"
+
+namespace org::apache::nifi::minifi::state {
+
+class MetricsPublisherStore {
+ public:
+ MetricsPublisherStore(std::shared_ptr<Configure> configuration, std::shared_ptr<core::Repository> provenance_repo,
+ std::shared_ptr<core::Repository> flow_file_repo, std::shared_ptr<core::FlowConfiguration> flow_configuration);
+ void initialize(core::controller::ControllerServiceProvider* controller, state::StateMonitor* update_sink);
+ void loadMetricNodes(core::ProcessGroup* root);
+ void clearMetricNodes();
+ std::weak_ptr<state::MetricsPublisher> getMetricsPublisher(const std::string& name) const;
+
+ private:
+ void addMetricsPublisher(const std::string& name, std::shared_ptr<state::MetricsPublisher> publisher) {
+ if (!publisher) {
+ return;
+ }
+
+ metrics_publishers_.emplace(name, gsl::make_not_null(std::move(publisher)));
+ }
+
+ std::shared_ptr<Configure> configuration_;
+ gsl::not_null<std::shared_ptr<response::ResponseNodeLoader>> response_node_loader_;
+ std::unordered_map<std::string, gsl::not_null<std::shared_ptr<state::MetricsPublisher>>> metrics_publishers_;
+};
+
+} // namespace org::apache::nifi::minifi::state
diff --git a/libminifi/include/core/state/nodes/FlowInformation.h b/libminifi/include/core/state/nodes/FlowInformation.h
index 894d938a0..f864f7d21 100644
--- a/libminifi/include/core/state/nodes/FlowInformation.h
+++ b/libminifi/include/core/state/nodes/FlowInformation.h
@@ -125,7 +125,7 @@ class FlowVersion : public DeviceInformation {
std::shared_ptr<FlowIdentifier> identifier;
};
-class FlowMonitor : public StateMonitorNode, public ConnectionStore {
+class FlowMonitor : public StateMonitorNode {
public:
FlowMonitor(std::string name, const utils::Identifier &uuid)
: StateMonitorNode(std::move(name), uuid) {
@@ -139,8 +139,13 @@ class FlowMonitor : public StateMonitorNode, public ConnectionStore {
flow_version_ = std::move(flow_version);
}
+ void updateConnection(minifi::Connection* connection) {
+ connection_store_.updateConnection(connection);
+ }
+
protected:
std::shared_ptr<state::response::FlowVersion> flow_version_;
+ ConnectionStore connection_store_;
};
/**
@@ -178,12 +183,13 @@ class FlowInformation : public FlowMonitor {
serialized.push_back(fv);
serialized.push_back(uri);
- if (!connections_.empty()) {
+ const auto& connections = connection_store_.getConnections();
+ if (!connections.empty()) {
SerializedResponseNode queues;
queues.collapsible = false;
queues.name = "queues";
- for (auto &queue : connections_) {
+ for (const auto& queue : connections) {
SerializedResponseNode repoNode;
repoNode.collapsible = false;
repoNode.name = queue.second->getName();
@@ -248,7 +254,7 @@ class FlowInformation : public FlowMonitor {
}
std::vector<PublishedMetric> calculateMetrics() override {
- std::vector<PublishedMetric> metrics = calculateConnectionMetrics("FlowInformation");
+ std::vector<PublishedMetric> metrics = connection_store_.calculateConnectionMetrics("FlowInformation");
if (nullptr != monitor_) {
monitor_->executeOnAllComponents([&metrics](StateController& component){
diff --git a/libminifi/include/core/state/nodes/MetricsBase.h b/libminifi/include/core/state/nodes/MetricsBase.h
index 062aa57b7..e8f16a627 100644
--- a/libminifi/include/core/state/nodes/MetricsBase.h
+++ b/libminifi/include/core/state/nodes/MetricsBase.h
@@ -31,6 +31,9 @@
namespace org::apache::nifi::minifi::state::response {
+class ResponseNode;
+using SharedResponseNode = gsl::not_null<std::shared_ptr<ResponseNode>>;
+
/**
* Purpose: Defines a metric. Serialization is intended to be thread safe.
*/
@@ -53,18 +56,7 @@ class ResponseNode : public core::Connectable, public PublishedMetricProvider {
~ResponseNode() override = default;
- static std::vector<state::response::SerializedResponseNode> serializeAndMergeResponseNodes(const std::vector<std::shared_ptr<ResponseNode>>& nodes) {
- if (nodes.empty()) {
- return {};
- }
-
- std::vector<state::response::SerializedResponseNode> result;
- for (const auto& node: nodes) {
- auto serialized = node->serialize();
- result.insert(result.end(), serialized.begin(), serialized.end());
- }
- return result;
- }
+ static std::vector<SerializedResponseNode> serializeAndMergeResponseNodes(const std::vector<SharedResponseNode>& nodes);
virtual std::vector<SerializedResponseNode> serialize() = 0;
@@ -118,7 +110,7 @@ class ObjectNode : public ResponseNode {
: ResponseNode(std::move(name), uuid) {
}
- void add_node(const std::shared_ptr<ResponseNode> &node) {
+ void add_node(const SharedResponseNode &node) {
nodes_[node->getName()].push_back(node);
}
@@ -126,30 +118,14 @@ class ObjectNode : public ResponseNode {
return Connectable::getName();
}
- std::vector<SerializedResponseNode> serialize() override {
- std::vector<SerializedResponseNode> serialized;
- for (const auto& [name, nodes] : nodes_) {
- if (nodes.empty()) {
- continue;
- }
- SerializedResponseNode inner_node;
- inner_node.name = nodes[0]->getName();
- for (auto &embed : ResponseNode::serializeAndMergeResponseNodes(nodes)) {
- if (!embed.empty() || embed.keep_empty) {
- inner_node.children.push_back(std::move(embed));
- }
- }
- serialized.push_back(std::move(inner_node));
- }
- return serialized;
- }
+ std::vector<SerializedResponseNode> serialize() override;
bool isEmpty() override {
return nodes_.empty();
}
protected:
- std::unordered_map<std::string, std::vector<std::shared_ptr<ResponseNode>>> nodes_;
+ std::unordered_map<std::string, std::vector<SharedResponseNode>> nodes_;
};
/**
@@ -159,7 +135,7 @@ class ObjectNode : public ResponseNode {
class ResponseNodeSource {
public:
virtual ~ResponseNodeSource() = default;
- virtual std::shared_ptr<ResponseNode> getResponseNodes() = 0;
+ virtual SharedResponseNode getResponseNode() = 0;
};
class NodeReporter {
diff --git a/libminifi/include/core/state/nodes/QueueMetrics.h b/libminifi/include/core/state/nodes/QueueMetrics.h
index 3b6a832d6..c9436ec77 100644
--- a/libminifi/include/core/state/nodes/QueueMetrics.h
+++ b/libminifi/include/core/state/nodes/QueueMetrics.h
@@ -33,10 +33,10 @@ namespace org::apache::nifi::minifi::state::response {
* C2 server.
*
*/
-class QueueMetrics : public ResponseNode, public ConnectionStore {
+class QueueMetrics : public ResponseNode {
public:
QueueMetrics(const std::string &name, const utils::Identifier &uuid)
- : ResponseNode(name, uuid) {
+ : ResponseNode(name, uuid) {
}
QueueMetrics(const std::string &name) // NOLINT
@@ -44,7 +44,7 @@ class QueueMetrics : public ResponseNode, public ConnectionStore {
}
QueueMetrics()
- : ResponseNode("QueueMetrics") {
+ : ResponseNode("QueueMetrics") {
}
MINIFIAPI static constexpr const char* Description = "Metric node that defines queue metric information";
@@ -53,9 +53,13 @@ class QueueMetrics : public ResponseNode, public ConnectionStore {
return "QueueMetrics";
}
+ void updateConnection(minifi::Connection* connection) {
+ connection_store_.updateConnection(connection);
+ }
+
std::vector<SerializedResponseNode> serialize() override {
std::vector<SerializedResponseNode> serialized;
- for (const auto& [_, connection] : connections_) {
+ for (const auto& [_, connection] : connection_store_.getConnections()) {
SerializedResponseNode parent;
parent.name = connection->getName();
SerializedResponseNode datasize;
@@ -85,8 +89,11 @@ class QueueMetrics : public ResponseNode, public ConnectionStore {
}
std::vector<PublishedMetric> calculateMetrics() override {
- return calculateConnectionMetrics("QueueMetrics");
+ return connection_store_.calculateConnectionMetrics("QueueMetrics");
}
+
+ private:
+ ConnectionStore connection_store_;
};
} // namespace org::apache::nifi::minifi::state::response
diff --git a/libminifi/include/core/state/nodes/ResponseNodeLoader.h b/libminifi/include/core/state/nodes/ResponseNodeLoader.h
index b30125673..719d2e9a5 100644
--- a/libminifi/include/core/state/nodes/ResponseNodeLoader.h
+++ b/libminifi/include/core/state/nodes/ResponseNodeLoader.h
@@ -32,41 +32,49 @@
#include "core/FlowConfiguration.h"
#include "utils/gsl.h"
#include "utils/Id.h"
+#include "utils/expected.h"
namespace org::apache::nifi::minifi::state::response {
class ResponseNodeLoader {
public:
ResponseNodeLoader(std::shared_ptr<Configure> configuration, std::shared_ptr<core::Repository> provenance_repo,
- std::shared_ptr<core::Repository> flow_file_repo, core::FlowConfiguration* flow_configuration);
- void initializeComponentMetrics(core::ProcessGroup* root);
+ std::shared_ptr<core::Repository> flow_file_repo, std::shared_ptr<core::FlowConfiguration> flow_configuration);
+
+ void setNewConfigRoot(core::ProcessGroup* root);
+ void clearConfigRoot();
void setControllerServiceProvider(core::controller::ControllerServiceProvider* controller);
void setStateMonitor(state::StateMonitor* update_sink);
- std::vector<std::shared_ptr<ResponseNode>> loadResponseNodes(const std::string& clazz, core::ProcessGroup* root) const;
+ std::vector<SharedResponseNode> loadResponseNodes(const std::string& clazz);
+ state::response::NodeReporter::ReportedNode getAgentManifest() const;
private:
- std::vector<std::shared_ptr<ResponseNode>> getComponentMetricsNodes(const std::string& metrics_class) const;
- std::vector<std::shared_ptr<ResponseNode>> getResponseNodes(const std::string& clazz) const;
- void initializeRepositoryMetrics(const std::shared_ptr<ResponseNode>& response_node) const;
- static void initializeQueueMetrics(const std::shared_ptr<ResponseNode>& response_node, core::ProcessGroup* root);
- void initializeAgentIdentifier(const std::shared_ptr<ResponseNode>& response_node) const;
- void initializeAgentMonitor(const std::shared_ptr<ResponseNode>& response_node) const;
- void initializeAgentNode(const std::shared_ptr<ResponseNode>& response_node) const;
- void initializeAgentStatus(const std::shared_ptr<ResponseNode>& response_node) const;
- void initializeConfigurationChecksums(const std::shared_ptr<ResponseNode>& response_node) const;
- void initializeFlowMonitor(const std::shared_ptr<ResponseNode>& response_node, core::ProcessGroup* root) const;
- std::vector<std::shared_ptr<ResponseNode>> getMatchingComponentMetricsNodes(const std::string& regex_str) const;
+ void initializeComponentMetrics();
+ std::vector<SharedResponseNode> getComponentMetricsNodes(const std::string& metrics_class) const;
+ nonstd::expected<SharedResponseNode, std::string> getSystemMetricsNode(const std::string& clazz);
+ std::vector<SharedResponseNode> getResponseNodes(const std::string& clazz);
+ void initializeRepositoryMetrics(const SharedResponseNode& response_node) const;
+ void initializeQueueMetrics(const SharedResponseNode& response_node) const;
+ void initializeAgentIdentifier(const SharedResponseNode& response_node) const;
+ void initializeAgentMonitor(const SharedResponseNode& response_node) const;
+ void initializeAgentNode(const SharedResponseNode& response_node) const;
+ void initializeAgentStatus(const SharedResponseNode& response_node) const;
+ void initializeConfigurationChecksums(const SharedResponseNode& response_node) const;
+ void initializeFlowMonitor(const SharedResponseNode& response_node) const;
+ std::vector<SharedResponseNode> getMatchingComponentMetricsNodes(const std::string& regex_str) const;
+ mutable std::mutex root_mutex_;
mutable std::mutex component_metrics_mutex_;
- std::unordered_map<std::string, std::vector<std::shared_ptr<ResponseNode>>> component_metrics_;
+ mutable std::mutex system_metrics_mutex_;
+ core::ProcessGroup* root_{};
+ std::unordered_map<std::string, std::vector<SharedResponseNode>> component_metrics_;
+ std::unordered_map<std::string, SharedResponseNode> system_metrics_;
std::shared_ptr<Configure> configuration_;
std::shared_ptr<core::Repository> provenance_repo_;
std::shared_ptr<core::Repository> flow_file_repo_;
- core::FlowConfiguration* flow_configuration_ = nullptr;
- core::controller::ControllerServiceProvider* controller_ = nullptr;
- state::StateMonitor* update_sink_ = nullptr;
- std::mutex callback_mutex_;
- std::map<utils::Identifier, std::function<void(core::ProcessGroup*)>> flow_change_callbacks_;
+ std::shared_ptr<core::FlowConfiguration> flow_configuration_;
+ core::controller::ControllerServiceProvider* controller_{};
+ state::StateMonitor* update_sink_{};
std::shared_ptr<core::logging::Logger> logger_{core::logging::LoggerFactory<ResponseNodeLoader>::getLogger()};
};
diff --git a/libminifi/src/FlowController.cpp b/libminifi/src/FlowController.cpp
index efc69a36b..f8efae4a0 100644
--- a/libminifi/src/FlowController.cpp
+++ b/libminifi/src/FlowController.cpp
@@ -27,9 +27,7 @@
#include <string>
#include "FlowController.h"
-#include "core/state/nodes/AgentInformation.h"
#include "core/state/ProcessorController.h"
-#include "c2/C2Agent.h"
#include "core/ProcessGroup.h"
#include "core/Core.h"
#include "SchedulingAgent.h"
@@ -44,21 +42,26 @@
#include "io/FileStream.h"
#include "core/ClassLoader.h"
#include "core/ThreadedRepository.h"
+#include "c2/C2MetricsPublisher.h"
namespace org::apache::nifi::minifi {
FlowController::FlowController(std::shared_ptr<core::Repository> provenance_repo, std::shared_ptr<core::Repository> flow_file_repo,
- std::shared_ptr<Configure> configure, std::unique_ptr<core::FlowConfiguration> flow_configuration,
- std::shared_ptr<core::ContentRepository> content_repo, const std::string& /*name*/,
+ std::shared_ptr<Configure> configure, std::shared_ptr<core::FlowConfiguration> flow_configuration,
+ std::shared_ptr<core::ContentRepository> content_repo, std::unique_ptr<state::MetricsPublisherStore> metrics_publisher_store,
std::shared_ptr<utils::file::FileSystem> filesystem, std::function<void()> request_restart)
: core::controller::ForwardingControllerServiceProvider(core::getClassName<FlowController>()),
- c2::C2Client(std::move(configure), std::move(provenance_repo), std::move(flow_file_repo),
- std::move(content_repo), std::move(flow_configuration), std::move(filesystem),
- std::move(request_restart), core::logging::LoggerFactory<c2::C2Client>::getLogger()),
running_(false),
updating_(false),
initialized_(false),
- thread_pool_(5, false, nullptr, "Flowcontroller threadpool") {
+ thread_pool_(5, false, nullptr, "Flowcontroller threadpool"),
+ configuration_(std::move(configure)),
+ provenance_repo_(std::move(provenance_repo)),
+ flow_file_repo_(std::move(flow_file_repo)),
+ content_repo_(std::move(content_repo)),
+ flow_configuration_(std::move(flow_configuration)),
+ metrics_publisher_store_(std::move(metrics_publisher_store)),
+ root_wrapper_(configuration_, metrics_publisher_store_.get()) {
if (provenance_repo_ == nullptr)
throw std::runtime_error("Provenance Repo should not be null");
if (flow_file_repo_ == nullptr)
@@ -66,36 +69,29 @@ FlowController::FlowController(std::shared_ptr<core::Repository> provenance_repo
if (configuration_ == nullptr) {
throw std::runtime_error("Must supply a configuration.");
}
- running_ = false;
- initialized_ = false;
+ if (flow_configuration_) {
+ controller_service_provider_impl_ = flow_configuration_->getControllerServiceProvider();
+ }
protocol_ = std::make_unique<FlowControlProtocol>(this, configuration_);
- response_node_loader_.setControllerServiceProvider(this);
- response_node_loader_.setStateMonitor(this);
-}
+ if (metrics_publisher_store_) {
+ metrics_publisher_store_->initialize(this, this);
+ }
-FlowController::FlowController(std::shared_ptr<core::Repository> provenance_repo, std::shared_ptr<core::Repository> flow_file_repo,
- std::shared_ptr<Configure> configure, std::unique_ptr<core::FlowConfiguration> flow_configuration,
- std::shared_ptr<core::ContentRepository> content_repo, std::shared_ptr<utils::file::FileSystem> filesystem,
- std::function<void()> request_restart)
- : FlowController(std::move(provenance_repo), std::move(flow_file_repo), std::move(configure), std::move(flow_configuration),
- std::move(content_repo), DEFAULT_ROOT_GROUP_NAME, std::move(filesystem), std::move(request_restart)) {}
-
-std::optional<std::chrono::milliseconds> FlowController::loadShutdownTimeoutFromConfiguration() {
- std::string shutdown_timeout_str;
- if (configuration_->get(minifi::Configure::nifi_flowcontroller_drain_timeout, shutdown_timeout_str)) {
- const auto time_from_config = core::TimePeriodValue::fromString(shutdown_timeout_str);
- if (time_from_config) {
- return { std::chrono::milliseconds{ time_from_config.value().getMilliseconds() }};
+ if (c2::isC2Enabled(configuration_)) {
+ std::shared_ptr<c2::C2MetricsPublisher> c2_metrics_publisher;
+ if (auto publisher = metrics_publisher_store_->getMetricsPublisher(c2::C2_METRICS_PUBLISHER).lock()) {
+ c2_metrics_publisher = std::dynamic_pointer_cast<c2::C2MetricsPublisher>(publisher);
}
+ c2_agent_ = std::make_unique<c2::C2Agent>(configuration_, c2_metrics_publisher, std::move(filesystem), std::move(request_restart));
}
- return std::nullopt;
}
FlowController::~FlowController() {
+ if (c2_agent_) {
+ c2_agent_->stop();
+ }
stop();
- stopC2();
- unload();
// TODO(adebreceni): are these here on purpose, so they are destroyed first?
protocol_ = nullptr;
flow_file_repo_ = nullptr;
@@ -106,7 +102,7 @@ FlowController::~FlowController() {
bool FlowController::applyConfiguration(const std::string &source, const std::string &configurePayload, const std::optional<std::string>& flow_id) {
std::unique_ptr<core::ProcessGroup> newRoot;
try {
- newRoot = flow_configuration_->updateFromPayload(source, configurePayload, flow_id);
+ newRoot = updateFromPayload(source, configurePayload, flow_id);
} catch (const std::exception& ex) {
logger_->log_error("Invalid configuration payload, type: %s, what: %s", typeid(ex).name(), ex.what());
return false;
@@ -129,22 +125,11 @@ bool FlowController::applyConfiguration(const std::string &source, const std::st
{
std::lock_guard<std::recursive_mutex> flow_lock(mutex_);
stop();
- unload();
- // prepare to accept the new controller service provider from flow_configuration_
- clearControllerServices();
-
- clearResponseNodes();
- if (metrics_publisher_) {
- metrics_publisher_->clearMetricNodes();
- }
- auto prevRoot = std::move(this->root_);
- this->root_ = std::move(newRoot);
- processor_to_controller_.clear();
+ root_wrapper_.setNewRoot(std::move(newRoot));
initialized_ = false;
try {
- load(std::move(root_), true);
- flow_update_ = true;
+ load(true);
started = start() == 0;
} catch (const std::exception& ex) {
logger_->log_error("Caught exception while starting flow, type %s, what: %s", typeid(ex).name(), ex.what());
@@ -153,9 +138,11 @@ bool FlowController::applyConfiguration(const std::string &source, const std::st
}
if (!started) {
logger_->log_error("Failed to start new flow, restarting previous flow");
- load(std::move(prevRoot), true);
- flow_update_ = true;
+ root_wrapper_.restoreBackup();
+ load(true);
start();
+ } else {
+ root_wrapper_.clearBackup();
}
}
@@ -180,22 +167,7 @@ int16_t FlowController::stop() {
if (running_) {
// immediately indicate that we are not running
logger_->log_info("Stop Flow Controller");
- if (this->root_) {
- // stop source processors first
- this->root_->stopProcessing(*timer_scheduler_, *event_scheduler_, *cron_scheduler_, [] (const core::Processor* proc) -> bool {
- return !proc->hasIncomingConnections();
- });
- // we enable C2 to progressively increase the timeout
- // in case it sees that waiting for a little longer could
- // allow the FlowFiles to be processed
- auto shutdown_start = std::chrono::steady_clock::now();
- while ((std::chrono::steady_clock::now() - shutdown_start) < loadShutdownTimeoutFromConfiguration().value_or(std::chrono::milliseconds{0}) &&
- this->root_->getTotalFlowFileCount() != 0) {
- std::this_thread::sleep_for(shutdown_check_interval_);
- }
- // shutdown all other processors as well
- this->root_->stopProcessing(*timer_scheduler_, *event_scheduler_, *cron_scheduler_);
- }
+ root_wrapper_.stopProcessing(*timer_scheduler_, *event_scheduler_, *cron_scheduler_);
// stop after we've attempted to stop the processors.
timer_scheduler_->stop();
event_scheduler_->stop();
@@ -205,14 +177,13 @@ int16_t FlowController::stop() {
* -Stopping the schedulers doesn't actually quit the onTrigger functions of processors
* -They only guarantee that the processors are not scheduled anymore
* -After the threadpool is stopped we can make sure that processors don't need repos and controllers anymore */
- if (this->root_) {
- this->root_->drainConnections();
- }
+ root_wrapper_.drainConnections();
this->flow_file_repo_->stop();
this->provenance_repo_->stop();
this->content_repo_->stop();
// stop the ControllerServices
disableAllControllerServices();
+ initialized_ = false;
running_ = false;
}
return 0;
@@ -233,7 +204,7 @@ void FlowController::waitUnload(const uint64_t timeToWaitMs) {
std::chrono::system_clock::time_point wait_time = std::chrono::system_clock::now() + std::chrono::milliseconds(timeToWaitMs);
// create an asynchronous future.
- std::future<void> unload_task = std::async(std::launch::async, [this]() {unload();});
+ std::future<void> unload_task = std::async(std::launch::async, [this]() {stop();});
if (std::future_status::ready == unload_task.wait_until(wait_time)) {
running_ = false;
@@ -241,18 +212,6 @@ void FlowController::waitUnload(const uint64_t timeToWaitMs) {
}
}
-void FlowController::unload() {
- std::lock_guard<std::recursive_mutex> flow_lock(mutex_);
- if (running_) {
- stop();
- }
- if (initialized_) {
- logger_->log_info("Unload Flow Controller");
- initialized_ = false;
- name_ = "";
- }
-}
-
std::unique_ptr<core::ProcessGroup> FlowController::loadInitialFlow() {
std::unique_ptr<core::ProcessGroup> root = flow_configuration_->getRoot();
if (root) {
@@ -267,14 +226,17 @@ std::unique_ptr<core::ProcessGroup> FlowController::loadInitialFlow() {
// ensure that C2 connection is up and running
// since we don't have access to the flow definition, the C2 communication
// won't be able to use the services defined there, e.g. SSLContextService
- controller_service_provider_impl_ = flow_configuration_->getControllerServiceProvider();
- C2Client::initialize(this, this, this);
- auto opt_source = fetchFlow(*opt_flow_url);
+ if (!c2_agent_) {
+ return nullptr;
+ }
+ c2_agent_->initialize(this, this, this);
+ c2_agent_->start();
+ auto opt_source = c2_agent_->fetchFlow(*opt_flow_url);
if (!opt_source) {
logger_->log_error("Couldn't fetch flow configuration from C2 server");
return nullptr;
}
- root = flow_configuration_->updateFromPayload(*opt_flow_url, *opt_source);
+ root = updateFromPayload(*opt_flow_url, *opt_source);
if (root) {
logger_->log_info("Successfully fetched valid flow configuration");
if (!flow_configuration_->persist(*opt_source)) {
@@ -284,66 +246,57 @@ std::unique_ptr<core::ProcessGroup> FlowController::loadInitialFlow() {
return root;
}
-void FlowController::load(std::unique_ptr<core::ProcessGroup> root, bool reload) {
+void FlowController::load(bool reload) {
std::lock_guard<std::recursive_mutex> flow_lock(mutex_);
if (running_) {
stop();
}
- if (!initialized_) {
- if (reload) {
- io::NetworkPrioritizerFactory::getInstance()->clearPrioritizer();
- }
-
- if (root) {
- logger_->log_info("Load Flow Controller from provided root");
- this->root_ = std::move(root);
- processor_to_controller_.clear();
- } else {
- logger_->log_info("Instantiating new flow");
- this->root_ = loadInitialFlow();
- }
-
- logger_->log_info("Loaded root processor Group");
- logger_->log_info("Initializing timers");
- controller_service_provider_impl_ = flow_configuration_->getControllerServiceProvider();
- response_node_loader_.initializeComponentMetrics(root_.get());
- initializeResponseNodes(root_.get());
- if (metrics_publisher_) {
- metrics_publisher_->loadMetricNodes(root_.get());
- } else {
- loadMetricsPublisher();
- }
+ if (reload) {
+ io::NetworkPrioritizerFactory::getInstance()->clearPrioritizer();
+ }
- if (!thread_pool_.isRunning() || reload) {
- thread_pool_.shutdown();
- thread_pool_.setMaxConcurrentTasks(configuration_->getInt(Configure::nifi_flow_engine_threads, 5));
- thread_pool_.setControllerServiceProvider(this);
- thread_pool_.start();
+ if (!root_wrapper_.initialized()) {
+ logger_->log_info("Instantiating new flow");
+ root_wrapper_.setNewRoot(loadInitialFlow());
+ if (c2_agent_ && !c2_agent_->isControllerRunning()) {
+ // TODO(lordgamez): this initialization configures the C2 sender protocol (e.g. RESTSender) which may contain an SSL Context service from the flow config
+ // for SSL communication. This service may change on flow update and we should take care of the SSL Context Service change in the C2 Agent.
+ c2_agent_->initialize(this, this, this);
+ c2_agent_->start();
}
-
- conditionalReloadScheduler<TimerDrivenSchedulingAgent>(timer_scheduler_, !timer_scheduler_ || reload);
- conditionalReloadScheduler<EventDrivenSchedulingAgent>(event_scheduler_, !event_scheduler_ || reload);
- conditionalReloadScheduler<CronDrivenSchedulingAgent>(cron_scheduler_, !cron_scheduler_ || reload);
-
- logger_->log_info("Loaded controller service provider");
-
- /*
- * Without reset we have to distinguish a fresh restart and a reload, to decide if we have to
- * increment the claims' counter on behalf of the persisted instances.
- * ResourceClaim::getStreamCount is not suitable as multiple persisted instances
- * might have the same claim.
- * e.g. without reset a streamCount of 3 could mean the following:
- * - it was a fresh restart and 3 instances of this claim have already been resurrected -> we must increment
- * - it was a reload and 3 instances have been persisted before the shutdown -> we must not increment
- */
- content_repo_->reset();
- logger_->log_info("Reset content repository");
-
- // Load Flow File from Repo
- loadFlowRepo();
- logger_->log_info("Loaded flow repository");
- initialized_ = true;
}
+
+ logger_->log_info("Loaded root processor Group");
+ logger_->log_info("Initializing timers");
+ if (!thread_pool_.isRunning() || reload) {
+ thread_pool_.shutdown();
+ thread_pool_.setMaxConcurrentTasks(configuration_->getInt(Configure::nifi_flow_engine_threads, 5));
+ thread_pool_.setControllerServiceProvider(this);
+ thread_pool_.start();
+ }
+
+ conditionalReloadScheduler<TimerDrivenSchedulingAgent>(timer_scheduler_, !timer_scheduler_ || reload);
+ conditionalReloadScheduler<EventDrivenSchedulingAgent>(event_scheduler_, !event_scheduler_ || reload);
+ conditionalReloadScheduler<CronDrivenSchedulingAgent>(cron_scheduler_, !cron_scheduler_ || reload);
+
+ logger_->log_info("Loaded controller service provider");
+
+ /*
+ * Without reset we have to distinguish a fresh restart and a reload, to decide if we have to
+ * increment the claims' counter on behalf of the persisted instances.
+ * ResourceClaim::getStreamCount is not suitable as multiple persisted instances
+ * might have the same claim.
+ * e.g. without reset a streamCount of 3 could mean the following:
+ * - it was a fresh restart and 3 instances of this claim have already been resurrected -> we must increment
+ * - it was a reload and 3 instances have been persisted before the shutdown -> we must not increment
+ */
+ content_repo_->reset();
+ logger_->log_info("Reset content repository");
+
+ // Load Flow File from Repo
+ loadFlowRepo();
+ logger_->log_info("Loaded flow repository");
+ initialized_ = true;
}
void FlowController::loadFlowRepo() {
@@ -351,10 +304,8 @@ void FlowController::loadFlowRepo() {
logger_->log_debug("Getting connection map");
std::map<std::string, core::Connectable*> connectionMap;
std::map<std::string, core::Connectable*> containers;
- if (this->root_ != nullptr) {
- this->root_->getConnections(connectionMap);
- this->root_->getFlowFileContainers(containers);
- }
+ root_wrapper_.getConnections(connectionMap);
+ root_wrapper_.getFlowFileContainers(containers);
flow_file_repo_->setConnectionMap(connectionMap);
flow_file_repo_->setContainers(containers);
flow_file_repo_->loadComponent(content_repo_);
@@ -368,31 +319,29 @@ int16_t FlowController::start() {
if (!initialized_) {
logger_->log_error("Can not start Flow Controller because it has not been initialized");
return -1;
- } else {
- if (!running_) {
- logger_->log_info("Starting Flow Controller");
- enableAllControllerServices();
- timer_scheduler_->start();
- event_scheduler_->start();
- cron_scheduler_->start();
-
- if (this->root_ != nullptr) {
- start_time_ = std::chrono::steady_clock::now();
- // watch out, this might immediately start the processors
- // as the thread_pool_ is started in load()
- this->root_->startProcessing(*timer_scheduler_, *event_scheduler_, *cron_scheduler_);
- }
- C2Client::initialize(this, this, this);
- core::logging::LoggerConfiguration::getConfiguration().initializeAlertSinks(this, configuration_);
- running_ = true;
- this->protocol_->start();
- this->content_repo_->start();
- this->provenance_repo_->start();
- this->flow_file_repo_->start();
- logger_->log_info("Started Flow Controller");
+ } else if (!running_) {
+ logger_->log_info("Starting Flow Controller");
+ enableAllControllerServices();
+ timer_scheduler_->start();
+ event_scheduler_->start();
+ cron_scheduler_->start();
+
+ // watch out, this might immediately start the processors
+ // as the thread_pool_ is started in load()
+ if (root_wrapper_.startProcessing(*timer_scheduler_, *event_scheduler_, *cron_scheduler_)) {
+ start_time_ = std::chrono::steady_clock::now();
}
- return 0;
+
+ core::logging::LoggerConfiguration::getConfiguration().initializeAlertSinks(this, configuration_);
+ running_ = true;
+ protocol_->start();
+ content_repo_->start();
+ provenance_repo_->start();
+ flow_file_repo_->start();
+ thread_pool_.start();
+ logger_->log_info("Started Flow Controller");
}
+ return 0;
}
int16_t FlowController::pause() {
@@ -435,35 +384,10 @@ int16_t FlowController::applyUpdate(const std::string &source, const std::string
}
int16_t FlowController::clearConnection(const std::string &connection) {
- if (root_ != nullptr) {
- logger_->log_info("Attempting to clear connection %s", connection);
- std::map<std::string, Connection*> connections;
- root_->getConnections(connections);
- auto conn = connections.find(connection);
- if (conn != connections.end()) {
- logger_->log_info("Clearing connection %s", connection);
- conn->second->drain(true);
- }
- }
+ root_wrapper_.clearConnection(connection);
return -1;
}
-state::response::NodeReporter::ReportedNode FlowController::getAgentManifest() {
- state::response::AgentInformation agentInfo("agentInfo");
- agentInfo.setUpdatePolicyController(std::static_pointer_cast<controllers::UpdatePolicyControllerService>(getControllerService(c2::C2Agent::UPDATE_NAME)).get());
- agentInfo.setAgentIdentificationProvider(configuration_);
- agentInfo.setConfigurationReader([this](const std::string& key){
- return configuration_->getRawValue(key);
- });
- agentInfo.setStateMonitor(this);
- agentInfo.includeAgentStatus(false);
- state::response::NodeReporter::ReportedNode reported_node;
- reported_node.name = agentInfo.getName();
- reported_node.is_array = agentInfo.isArray();
- reported_node.serialized_nodes = agentInfo.serialize();
- return reported_node;
-}
-
void FlowController::executeOnAllComponents(std::function<void(state::StateController&)> func) {
if (updating_) {
return;
@@ -487,8 +411,9 @@ void FlowController::executeOnComponent(const std::string &id_or_name, std::func
}
std::vector<state::StateController*> FlowController::getAllComponents() {
- if (root_) {
- return getAllProcessorControllers([this](core::Processor& p) { return createController(p); });
+ if (auto components = root_wrapper_.getAllProcessorControllers([this](core::Processor& p) { return createController(p); })) {
+ components->push_back(this);
+ return *components;
}
return {this};
@@ -497,8 +422,8 @@ std::vector<state::StateController*> FlowController::getAllComponents() {
state::StateController* FlowController::getComponent(const std::string& id_or_name) {
if (id_or_name == getUUIDStr() || id_or_name == "FlowController") {
return this;
- } else if (root_) {
- return getProcessorController(id_or_name, [this](core::Processor& p) { return createController(p); });
+ } else if (auto controller = root_wrapper_.getProcessorController(id_or_name, [this](core::Processor& p) { return createController(p); })) {
+ return controller;
}
return nullptr;
@@ -550,55 +475,12 @@ std::map<std::string, std::unique_ptr<io::InputStream>> FlowController::getDebug
return debug_info;
}
-std::vector<state::StateController*> FlowController::getAllProcessorControllers(
- const std::function<gsl::not_null<std::unique_ptr<state::ProcessorController>>(core::Processor&)>& controllerFactory) {
- std::vector<state::StateController*> controllerVec{this};
- std::vector<core::Processor*> processorVec;
- root_->getAllProcessors(processorVec);
-
- for (const auto& processor : processorVec) {
- // reference to the existing or newly created controller
- auto& controller = processor_to_controller_[processor->getUUID()];
- if (!controller) {
- controller = controllerFactory(*processor);
- }
- controllerVec.push_back(controller.get());
- }
-
- return controllerVec;
-}
-
-state::StateController* FlowController::getProcessorController(const std::string& id_or_name,
- const std::function<gsl::not_null<std::unique_ptr<state::ProcessorController>>(core::Processor&)>& controllerFactory) {
- return utils::Identifier::parse(id_or_name)
- | utils::flatMap([this](utils::Identifier id) { return utils::optional_from_ptr(root_->findProcessorById(id)); })
- | utils::orElse([this, &id_or_name] { return utils::optional_from_ptr(root_->findProcessorByName(id_or_name)); })
- | utils::map([this, &controllerFactory](gsl::not_null<core::Processor*> proc) -> gsl::not_null<state::ProcessorController*> {
- return utils::optional_from_ptr(processor_to_controller_[proc->getUUID()].get())
- | utils::valueOrElse([this, proc, &controllerFactory] {
- return gsl::make_not_null((processor_to_controller_[proc->getUUID()] = controllerFactory(*proc)).get());
- });
- })
- | utils::valueOrElse([this, &id_or_name]() -> state::ProcessorController* {
- logger_->log_error("Could not get processor controller for requested id/name \"%s\", because the processor was not found", id_or_name);
- return nullptr;
- });
-}
-
-void FlowController::loadMetricsPublisher() {
- if (auto metrics_publisher_class = configuration_->get(minifi::Configure::nifi_metrics_publisher_class)) {
- auto ptr = core::ClassLoader::getDefaultClassLoader().instantiate(*metrics_publisher_class, *metrics_publisher_class);
- if (!ptr) {
- logger_->log_error("Configured metrics publisher class \"%s\" could not be instantiated.", *metrics_publisher_class);
- return;
- }
- metrics_publisher_ = utils::dynamic_unique_cast<state::MetricsPublisher>(std::move(ptr));
- if (!metrics_publisher_) {
- logger_->log_error("Configured metrics publisher class \"%s\" is not a metrics publisher.", *metrics_publisher_class);
- return;
- }
- metrics_publisher_->initialize(configuration_, response_node_loader_, root_.get());
- }
+std::unique_ptr<core::ProcessGroup> FlowController::updateFromPayload(const std::string& url, const std::string& config_payload, const std::optional<std::string>& flow_id) {
+ auto root = flow_configuration_->updateFromPayload(url, config_payload, flow_id);
+ // prepare to accept the new controller service provider from flow_configuration_
+ clearControllerServices();
+ controller_service_provider_impl_ = flow_configuration_->getControllerServiceProvider();
+ return root;
}
} // namespace org::apache::nifi::minifi
diff --git a/libminifi/src/RootProcessGroupWrapper.cpp b/libminifi/src/RootProcessGroupWrapper.cpp
new file mode 100644
index 000000000..ee6f5e84c
--- /dev/null
+++ b/libminifi/src/RootProcessGroupWrapper.cpp
@@ -0,0 +1,188 @@
+/**
+ * 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 "RootProcessGroupWrapper.h"
+
+namespace org::apache::nifi::minifi {
+
+void RootProcessGroupWrapper::updatePropertyValue(const std::string& processor_name, const std::string& property_name, const std::string& property_value) {
+ if (root_ != nullptr) {
+ root_->updatePropertyValue(processor_name, property_name, property_value);
+ }
+}
+
+std::string RootProcessGroupWrapper::getName() const {
+ if (root_ != nullptr) {
+ return root_->getName();
+ }
+ return "";
+}
+
+utils::Identifier RootProcessGroupWrapper::getComponentUUID() const {
+ if (!root_) {
+ return {};
+ }
+ return root_->getUUID();
+}
+
+std::string RootProcessGroupWrapper::getVersion() const {
+ if (root_ != nullptr) {
+ return std::to_string(root_->getVersion());
+ }
+ return "0";
+}
+
+void RootProcessGroupWrapper::setNewRoot(std::unique_ptr<core::ProcessGroup> new_root) {
+ if (!new_root) {
+ logger_->log_error("New flow to be set was empty!");
+ return;
+ }
+
+ if (metrics_publisher_store_) { metrics_publisher_store_->clearMetricNodes(); }
+ backup_root_ = std::move(root_);
+ root_ = std::move(new_root);
+ if (metrics_publisher_store_) { metrics_publisher_store_->loadMetricNodes(root_.get()); }
+}
+
+void RootProcessGroupWrapper::restoreBackup() {
+ if (metrics_publisher_store_) { metrics_publisher_store_->clearMetricNodes(); }
+ root_ = std::move(backup_root_);
+ if (metrics_publisher_store_) { metrics_publisher_store_->loadMetricNodes(root_.get()); }
+}
+
+void RootProcessGroupWrapper::clearBackup() {
+ backup_root_ = nullptr;
+}
+
+void RootProcessGroupWrapper::stopProcessing(TimerDrivenSchedulingAgent& timer_scheduler,
+ EventDrivenSchedulingAgent& event_scheduler,
+ CronDrivenSchedulingAgent& cron_scheduler) {
+ if (!root_) {
+ return;
+ }
+ // stop source processors first
+ root_->stopProcessing(timer_scheduler, event_scheduler, cron_scheduler, [] (const core::Processor* proc) -> bool {
+ return !proc->hasIncomingConnections();
+ });
+ // we enable C2 to progressively increase the timeout
+ // in case it sees that waiting for a little longer could
+ // allow the FlowFiles to be processed
+ auto shutdown_start = std::chrono::steady_clock::now();
+ while ((std::chrono::steady_clock::now() - shutdown_start) < loadShutdownTimeoutFromConfiguration().value_or(std::chrono::milliseconds{0}) &&
+ root_->getTotalFlowFileCount() != 0) {
+ std::this_thread::sleep_for(shutdown_check_interval_);
+ }
+ // shutdown all other processors as well
+ root_->stopProcessing(timer_scheduler, event_scheduler, cron_scheduler);
+}
+
+void RootProcessGroupWrapper::drainConnections() {
+ if (root_) {
+ root_->drainConnections();
+ }
+}
+
+void RootProcessGroupWrapper::getConnections(std::map<std::string, core::Connectable*>& connectionMap) {
+ if (root_) {
+ root_->getConnections(connectionMap);
+ }
+}
+
+void RootProcessGroupWrapper::getFlowFileContainers(std::map<std::string, core::Connectable*>& containers) const {
+ if (root_) {
+ root_->getFlowFileContainers(containers);
+ }
+}
+
+bool RootProcessGroupWrapper::startProcessing(TimerDrivenSchedulingAgent& timer_scheduler,
+ EventDrivenSchedulingAgent& event_scheduler,
+ CronDrivenSchedulingAgent& cron_scheduler) {
+ if (!root_) {
+ return false;
+ }
+
+ root_->startProcessing(timer_scheduler, event_scheduler, cron_scheduler);
+ return true;
+}
+
+void RootProcessGroupWrapper::clearConnection(const std::string &connection) {
+ if (root_ == nullptr) {
+ return;
+ }
+ logger_->log_info("Attempting to clear connection %s", connection);
+ std::map<std::string, Connection*> connections;
+ root_->getConnections(connections);
+ auto conn = connections.find(connection);
+ if (conn != connections.end()) {
+ logger_->log_info("Clearing connection %s", connection);
+ conn->second->drain(true);
+ }
+}
+
+std::optional<std::vector<state::StateController*>> RootProcessGroupWrapper::getAllProcessorControllers(
+ const std::function<gsl::not_null<std::unique_ptr<state::ProcessorController>>(core::Processor&)>& controllerFactory) {
+ if (!root_) {
+ return std::nullopt;
+ }
+ std::vector<state::StateController*> controllerVec;
+ std::vector<core::Processor*> processorVec;
+ root_->getAllProcessors(processorVec);
+
+ for (const auto& processor : processorVec) {
+ // reference to the existing or newly created controller
+ auto& controller = processor_to_controller_[processor->getUUID()];
+ if (!controller) {
+ controller = controllerFactory(*processor);
+ }
+ controllerVec.push_back(controller.get());
+ }
+
+ return controllerVec;
+}
+
+state::StateController* RootProcessGroupWrapper::getProcessorController(const std::string& id_or_name,
+ const std::function<gsl::not_null<std::unique_ptr<state::ProcessorController>>(core::Processor&)>& controllerFactory) {
+ if (!root_) {
+ return nullptr;
+ }
+
+ return utils::Identifier::parse(id_or_name)
+ | utils::flatMap([this](utils::Identifier id) { return utils::optional_from_ptr(root_->findProcessorById(id)); })
+ | utils::orElse([this, &id_or_name] { return utils::optional_from_ptr(root_->findProcessorByName(id_or_name)); })
+ | utils::map([this, &controllerFactory](gsl::not_null<core::Processor*> proc) -> gsl::not_null<state::ProcessorController*> {
+ return utils::optional_from_ptr(processor_to_controller_[proc->getUUID()].get())
+ | utils::valueOrElse([this, proc, &controllerFactory] {
+ return gsl::make_not_null((processor_to_controller_[proc->getUUID()] = controllerFactory(*proc)).get());
+ });
+ })
+ | utils::valueOrElse([this, &id_or_name]() -> state::ProcessorController* {
+ logger_->log_error("Could not get processor controller for requested id/name \"%s\", because the processor was not found", id_or_name);
+ return nullptr;
+ });
+}
+
+std::optional<std::chrono::milliseconds> RootProcessGroupWrapper::loadShutdownTimeoutFromConfiguration() {
+ std::string shutdown_timeout_str;
+ if (configuration_->get(minifi::Configure::nifi_flowcontroller_drain_timeout, shutdown_timeout_str)) {
+ const auto time_from_config = core::TimePeriodValue::fromString(shutdown_timeout_str);
+ if (time_from_config) {
+ return { std::chrono::milliseconds{ time_from_config.value().getMilliseconds() }};
+ }
+ }
+ return std::nullopt;
+}
+
+} // namespace org::apache::nifi::minifi
diff --git a/libminifi/src/c2/C2Agent.cpp b/libminifi/src/c2/C2Agent.cpp
index faa89b971..ee557e1f4 100644
--- a/libminifi/src/c2/C2Agent.cpp
+++ b/libminifi/src/c2/C2Agent.cpp
@@ -44,31 +44,42 @@
#include "utils/StringUtils.h"
#include "io/ArchiveStream.h"
#include "io/StreamPipe.h"
+#include "utils/Id.h"
using namespace std::literals::chrono_literals;
namespace org::apache::nifi::minifi::c2 {
-C2Agent::C2Agent(core::controller::ControllerServiceProvider *controller,
- state::Pausable *pause_handler,
- state::StateMonitor* updateSink,
- std::shared_ptr<Configure> configuration,
+C2Agent::C2Agent(std::shared_ptr<Configure> configuration,
+ std::weak_ptr<state::response::NodeReporter> node_reporter,
std::shared_ptr<utils::file::FileSystem> filesystem,
std::function<void()> request_restart)
: heart_beat_period_(3s),
max_c2_responses(5),
- update_sink_(updateSink),
- update_service_(nullptr),
- controller_(controller),
- pause_handler_(pause_handler),
configuration_(std::move(configuration)),
+ node_reporter_(std::move(node_reporter)),
filesystem_(std::move(filesystem)),
- protocol_(nullptr),
thread_pool_(2, false, nullptr, "C2 threadpool"),
- request_restart_(std::move(request_restart)) {
- manifest_sent_ = false;
+ request_restart_(std::move(request_restart)),
+ last_run_(std::chrono::steady_clock::now()) {
+ if (!configuration_->getAgentClass()) {
+ logger_->log_info("Agent class is not predefined");
+ }
- last_run_ = std::chrono::steady_clock::now();
+ // Set a persistent fallback agent id. This is needed so that the C2 server can identify the same agent after a restart, even if nifi.c2.agent.identifier is not specified.
+ if (auto id = configuration_->get(Configuration::nifi_c2_agent_identifier_fallback)) {
+ configuration_->setFallbackAgentIdentifier(*id);
+ } else {
+ const auto agent_id = utils::IdGenerator::getIdGenerator()->generate().to_string();
+ configuration_->setFallbackAgentIdentifier(agent_id);
+ configuration_->set(Configuration::nifi_c2_agent_identifier_fallback, agent_id, PropertyChangeLifetime::PERSISTENT);
+ }
+}
+
+void C2Agent::initialize(core::controller::ControllerServiceProvider *controller, state::Pausable *pause_handler, state::StateMonitor* update_sink) {
+ controller_ = controller;
+ pause_handler_ = pause_handler;
+ update_sink_ = update_sink;
if (nullptr != controller_) {
update_service_ = std::static_pointer_cast<controllers::UpdatePolicyControllerService>(controller_->getControllerService(UPDATE_NAME));
@@ -102,6 +113,9 @@ void C2Agent::start() {
}
void C2Agent::stop() {
+ if (!controller_running_) {
+ return;
+ }
controller_running_ = false;
for (const auto& id : task_ids_) {
thread_pool_.stopTasks(id.to_string());
@@ -221,25 +235,24 @@ void C2Agent::configure(const std::shared_ptr<Configure> &configure, bool reconf
void C2Agent::performHeartBeat() {
C2Payload payload(Operation::HEARTBEAT);
logger_->log_trace("Performing heartbeat");
- auto reporter = dynamic_cast<state::response::NodeReporter*>(update_sink_);
std::vector<state::response::NodeReporter::ReportedNode> metrics;
- if (reporter) {
+ if (auto node_reporter = node_reporter_.lock()) {
if (!manifest_sent_) {
// include agent manifest for the first heartbeat
- metrics = reporter->getHeartbeatNodes(true);
+ metrics = node_reporter->getHeartbeatNodes(true);
manifest_sent_ = true;
} else {
- metrics = reporter->getHeartbeatNodes(false);
+ metrics = node_reporter->getHeartbeatNodes(false);
}
+ }
- payload.reservePayloads(metrics.size());
- for (const auto& metric : metrics) {
- C2Payload child_metric_payload(Operation::HEARTBEAT);
- child_metric_payload.setLabel(metric.name);
- child_metric_payload.setContainer(metric.is_array);
- serializeMetrics(child_metric_payload, metric.name, metric.serialized_nodes, metric.is_array);
- payload.addPayload(std::move(child_metric_payload));
- }
+ payload.reservePayloads(metrics.size());
+ for (const auto& metric : metrics) {
+ C2Payload child_metric_payload(Operation::HEARTBEAT);
+ child_metric_payload.setLabel(metric.name);
+ child_metric_payload.setContainer(metric.is_array);
+ serializeMetrics(child_metric_payload, metric.name, metric.serialized_nodes, metric.is_array);
+ payload.addPayload(std::move(child_metric_payload));
}
C2Payload response = protocol_.load()->consumePayload(payload);
@@ -492,24 +505,24 @@ void C2Agent::handle_describe(const C2ContentResponse &resp) {
return;
}
- auto reporter = dynamic_cast<state::response::NodeReporter*>(update_sink_);
switch (operand.value()) {
case DescribeOperand::METRICS: {
C2Payload response(Operation::ACKNOWLEDGE, resp.ident, true);
- if (reporter != nullptr) {
- auto iter = resp.operation_arguments.find("metricsClass");
- std::string metricsClass;
- if (iter != resp.operation_arguments.end()) {
- metricsClass = iter->second.to_string();
- }
- auto metricsNode = reporter->getMetricsNode(metricsClass);
- C2Payload metrics(Operation::ACKNOWLEDGE);
- metricsClass.empty() ? metrics.setLabel("metrics") : metrics.setLabel(metricsClass);
- if (metricsNode) {
- serializeMetrics(metrics, metricsNode->name, metricsNode->serialized_nodes, metricsNode->is_array);
- }
- response.addPayload(std::move(metrics));
+ auto iter = resp.operation_arguments.find("metricsClass");
+ std::string metricsClass;
+ if (iter != resp.operation_arguments.end()) {
+ metricsClass = iter->second.to_string();
+ }
+ std::optional<state::response::NodeReporter::ReportedNode> metricsNode;
+ if (auto node_reporter = node_reporter_.lock()) {
+ metricsNode = node_reporter->getMetricsNode(metricsClass);
+ }
+ C2Payload metrics(Operation::ACKNOWLEDGE);
+ metricsClass.empty() ? metrics.setLabel("metrics") : metrics.setLabel(metricsClass);
+ if (metricsNode) {
+ serializeMetrics(metrics, metricsNode->name, metricsNode->serialized_nodes, metricsNode->is_array);
}
+ response.addPayload(std::move(metrics));
enqueue_c2_response(std::move(response));
break;
}
@@ -520,14 +533,14 @@ void C2Agent::handle_describe(const C2ContentResponse &resp) {
}
case DescribeOperand::MANIFEST: {
C2Payload response(prepareConfigurationOptions(resp));
- if (reporter != nullptr) {
- C2Payload agentInfo(Operation::ACKNOWLEDGE, resp.ident, true);
- agentInfo.setLabel("agentInfo");
+ C2Payload agentInfo(Operation::ACKNOWLEDGE, resp.ident, true);
+ agentInfo.setLabel("agentInfo");
- const auto manifest = reporter->getAgentManifest();
+ if (auto node_reporter = node_reporter_.lock()) {
+ auto manifest = node_reporter->getAgentManifest();
serializeMetrics(agentInfo, manifest.name, manifest.serialized_nodes);
- response.addPayload(std::move(agentInfo));
}
+ response.addPayload(std::move(agentInfo));
enqueue_c2_response(std::move(response));
break;
}
diff --git a/libminifi/src/c2/C2Client.cpp b/libminifi/src/c2/C2MetricsPublisher.cpp
similarity index 57%
rename from libminifi/src/c2/C2Client.cpp
rename to libminifi/src/c2/C2MetricsPublisher.cpp
index 26b6c6817..8f71e1f38 100644
--- a/libminifi/src/c2/C2Client.cpp
+++ b/libminifi/src/c2/C2MetricsPublisher.cpp
@@ -19,7 +19,7 @@
#include <filesystem>
#include <memory>
#include <map>
-#include "c2/C2Client.h"
+#include "c2/C2MetricsPublisher.h"
#include "core/state/nodes/MetricsBase.h"
#include "core/state/nodes/QueueMetrics.h"
#include "core/state/nodes/AgentInformation.h"
@@ -35,87 +35,26 @@
#include "utils/file/FileUtils.h"
#include "utils/gsl.h"
#include "utils/StringUtils.h"
+#include "core/Resource.h"
namespace org::apache::nifi::minifi::c2 {
-C2Client::C2Client(
- std::shared_ptr<Configure> configuration, std::shared_ptr<core::Repository> provenance_repo,
- std::shared_ptr<core::Repository> flow_file_repo, std::shared_ptr<core::ContentRepository> content_repo,
- std::unique_ptr<core::FlowConfiguration> flow_configuration, std::shared_ptr<utils::file::FileSystem> filesystem,
- std::function<void()> request_restart, std::shared_ptr<core::logging::Logger> logger)
- : core::Flow(std::move(provenance_repo), std::move(flow_file_repo), std::move(content_repo), std::move(flow_configuration)),
- configuration_(std::move(configuration)),
- filesystem_(std::move(filesystem)),
- logger_(std::move(logger)),
- request_restart_(std::move(request_restart)),
- response_node_loader_(configuration_, provenance_repo_, flow_file_repo_, flow_configuration_.get()) {}
-
-void C2Client::stopC2() {
- if (c2_agent_) {
- c2_agent_->stop();
- }
-}
-
-bool C2Client::isC2Enabled() const {
- std::string c2_enable_str;
- configuration_->get(minifi::Configuration::nifi_c2_enable, "c2.enable", c2_enable_str);
- return utils::StringUtils::toBool(c2_enable_str).value_or(false);
-}
-
-void C2Client::initialize(core::controller::ControllerServiceProvider *controller, state::Pausable *pause_handler, state::StateMonitor* update_sink) {
- if (!isC2Enabled()) {
- return;
- }
-
- if (!configuration_->getAgentClass()) {
- logger_->log_info("Agent class is not predefined");
- }
-
- // Set a persistent fallback agent id. This is needed so that the C2 server can identify the same agent after a restart, even if nifi.c2.agent.identifier is not specified.
- if (auto id = configuration_->get(Configuration::nifi_c2_agent_identifier_fallback)) {
- configuration_->setFallbackAgentIdentifier(*id);
- } else {
- const auto agent_id = getControllerUUID().to_string();
- configuration_->setFallbackAgentIdentifier(agent_id);
- configuration_->set(Configuration::nifi_c2_agent_identifier_fallback, agent_id, PropertyChangeLifetime::PERSISTENT);
- }
-
- std::lock_guard<std::mutex> lock(initialization_mutex_);
- if (initialized_ && !flow_update_) {
- return;
- }
-
- if (!initialized_) {
- initializeResponseNodes(root_.get());
- // C2Agent is initialized once, meaning that a C2-triggered flow/configuration update
- // might not be equal to a fresh restart
- c2_agent_ = std::make_unique<c2::C2Agent>(controller, pause_handler, update_sink, configuration_, filesystem_, request_restart_);
- c2_agent_->start();
- initialized_ = true;
- }
-}
-
-std::optional<std::string> C2Client::fetchFlow(const std::string& uri) const {
- if (!c2_agent_) {
- return {};
- }
- return c2_agent_->fetchFlow(uri);
-}
-
-void C2Client::loadNodeClasses(const std::string& class_definitions, const std::shared_ptr<state::response::ResponseNode>& new_node) {
+void C2MetricsPublisher::loadNodeClasses(const std::string& class_definitions, const state::response::SharedResponseNode& new_node) {
+ gsl_Expects(response_node_loader_);
auto classes = utils::StringUtils::split(class_definitions, ",");
for (const std::string& clazz : classes) {
- auto response_nodes = response_node_loader_.loadResponseNodes(clazz, root_.get());
+ auto response_nodes = response_node_loader_->loadResponseNodes(clazz);
if (response_nodes.empty()) {
continue;
}
for (const auto& response_node : response_nodes) {
- std::static_pointer_cast<state::response::ObjectNode>(new_node)->add_node(response_node);
+ static_cast<state::response::ObjectNode*>(new_node.get())->add_node(response_node);
}
}
}
-void C2Client::loadC2ResponseConfiguration(const std::string &prefix) {
+void C2MetricsPublisher::loadC2ResponseConfiguration(const std::string &prefix) {
+ gsl_Expects(configuration_);
std::string class_definitions;
if (!configuration_->get(prefix, class_definitions)) {
return;
@@ -133,7 +72,7 @@ void C2Client::loadC2ResponseConfiguration(const std::string &prefix) {
if (!configuration_->get(nameOption, name)) {
continue;
}
- std::shared_ptr<state::response::ResponseNode> new_node = std::make_shared<state::response::ObjectNode>(name);
+ state::response::SharedResponseNode new_node = gsl::make_not_null(std::make_shared<state::response::ObjectNode>(name));
if (configuration_->get(classOption, class_definitions)) {
loadNodeClasses(class_definitions, new_node);
} else {
@@ -141,7 +80,7 @@ void C2Client::loadC2ResponseConfiguration(const std::string &prefix) {
loadC2ResponseConfiguration(optionName, new_node);
}
- // We don't need to lock here, we already do it in the initializeResponseNodes member function
+ // We don't need to lock here, we already do it in the loadMetricNodes member function
root_response_nodes_[name].push_back(new_node);
} catch (const std::exception& ex) {
logger_->log_error("Could not create metrics class %s, exception type: %s, what: %s", metricsClass, typeid(ex).name(), ex.what());
@@ -151,7 +90,9 @@ void C2Client::loadC2ResponseConfiguration(const std::string &prefix) {
}
}
-std::shared_ptr<state::response::ResponseNode> C2Client::loadC2ResponseConfiguration(const std::string &prefix, std::shared_ptr<state::response::ResponseNode> prev_node) {
+state::response::SharedResponseNode C2MetricsPublisher::loadC2ResponseConfiguration(const std::string &prefix,
+ state::response::SharedResponseNode prev_node) {
+ gsl_Expects(configuration_);
std::string class_definitions;
if (!configuration_->get(prefix, class_definitions)) {
return prev_node;
@@ -168,25 +109,23 @@ std::shared_ptr<state::response::ResponseNode> C2Client::loadC2ResponseConfigura
if (!configuration_->get(nameOption, name)) {
continue;
}
- std::shared_ptr<state::response::ResponseNode> new_node = std::make_shared<state::response::ObjectNode>(name);
+ state::response::SharedResponseNode new_node = gsl::make_not_null(std::make_shared<state::response::ObjectNode>(name));
if (name.find(',') != std::string::npos) {
std::vector<std::string> sub_classes = utils::StringUtils::split(name, ",");
for (const std::string& subClassStr : classes) {
auto node = loadC2ResponseConfiguration(subClassStr, prev_node);
- if (node != nullptr) {
- std::static_pointer_cast<state::response::ObjectNode>(prev_node)->add_node(node);
- }
+ static_cast<state::response::ObjectNode*>(prev_node.get())->add_node(node);
}
} else {
if (configuration_->get(classOption, class_definitions)) {
loadNodeClasses(class_definitions, new_node);
if (!new_node->isEmpty()) {
- std::static_pointer_cast<state::response::ObjectNode>(prev_node)->add_node(new_node);
+ static_cast<state::response::ObjectNode*>(prev_node.get())->add_node(new_node);
}
} else {
std::string optionName = utils::StringUtils::join_pack(option, ".", name);
auto sub_node = loadC2ResponseConfiguration(optionName, new_node);
- std::static_pointer_cast<state::response::ObjectNode>(prev_node)->add_node(sub_node);
+ static_cast<state::response::ObjectNode*>(prev_node.get())->add_node(sub_node);
}
}
} catch (const std::exception& ex) {
@@ -198,9 +137,10 @@ std::shared_ptr<state::response::ResponseNode> C2Client::loadC2ResponseConfigura
return prev_node;
}
-std::optional<state::response::NodeReporter::ReportedNode> C2Client::getMetricsNode(const std::string& metrics_class) const {
+std::optional<state::response::NodeReporter::ReportedNode> C2MetricsPublisher::getMetricsNode(const std::string& metrics_class) const {
+ gsl_Expects(response_node_loader_);
std::lock_guard<std::mutex> guard{metrics_mutex_};
- const auto createReportedNode = [](const std::vector<std::shared_ptr<state::response::ResponseNode>>& nodes) {
+ const auto createReportedNode = [](const std::vector<state::response::SharedResponseNode>& nodes) {
gsl_Expects(!nodes.empty());
state::response::NodeReporter::ReportedNode reported_node;
reported_node.is_array = nodes[0]->isArray();
@@ -210,7 +150,7 @@ std::optional<state::response::NodeReporter::ReportedNode> C2Client::getMetricsN
};
if (!metrics_class.empty()) {
- auto metrics_nodes = response_node_loader_.loadResponseNodes(metrics_class, root_.get());
+ auto metrics_nodes = response_node_loader_->loadResponseNodes(metrics_class);
if (!metrics_nodes.empty()) {
return createReportedNode(metrics_nodes);
}
@@ -223,7 +163,8 @@ std::optional<state::response::NodeReporter::ReportedNode> C2Client::getMetricsN
return std::nullopt;
}
-std::vector<state::response::NodeReporter::ReportedNode> C2Client::getHeartbeatNodes(bool include_manifest) const {
+std::vector<state::response::NodeReporter::ReportedNode> C2MetricsPublisher::getHeartbeatNodes(bool include_manifest) const {
+ gsl_Expects(configuration_);
std::string fullHb{"true"};
configuration_->get(minifi::Configuration::nifi_c2_full_heartbeat, fullHb);
const bool include = include_manifest || fullHb == "true";
@@ -233,23 +174,22 @@ std::vector<state::response::NodeReporter::ReportedNode> C2Client::getHeartbeatN
reported_nodes.reserve(root_response_nodes_.size());
for (const auto& [name, node_values] : root_response_nodes_) {
for (const auto& node : node_values) {
- auto identifier = std::dynamic_pointer_cast<state::response::AgentIdentifier>(node);
+ auto identifier = dynamic_cast<state::response::AgentIdentifier*>(node.get());
if (identifier) {
identifier->includeAgentManifest(include);
}
- if (node) {
- state::response::NodeReporter::ReportedNode reported_node;
- reported_node.name = node->getName();
- reported_node.is_array = node->isArray();
- reported_node.serialized_nodes = node->serialize();
- reported_nodes.push_back(reported_node);
- }
+ state::response::NodeReporter::ReportedNode reported_node;
+ reported_node.name = node->getName();
+ reported_node.is_array = node->isArray();
+ reported_node.serialized_nodes = node->serialize();
+ reported_nodes.push_back(reported_node);
}
}
return reported_nodes;
}
-void C2Client::initializeResponseNodes(core::ProcessGroup* root) {
+void C2MetricsPublisher::loadMetricNodes() {
+ gsl_Expects(response_node_loader_ && configuration_);
if (!root_response_nodes_.empty()) {
return;
}
@@ -259,7 +199,7 @@ void C2Client::initializeResponseNodes(core::ProcessGroup* root) {
std::vector<std::string> classes = utils::StringUtils::split(class_csv, ",");
for (const std::string& clazz : classes) {
- auto response_nodes = response_node_loader_.loadResponseNodes(clazz, root);
+ auto response_nodes = response_node_loader_->loadResponseNodes(clazz);
if (response_nodes.empty()) {
continue;
}
@@ -273,9 +213,16 @@ void C2Client::initializeResponseNodes(core::ProcessGroup* root) {
loadC2ResponseConfiguration(Configuration::nifi_c2_root_class_definitions);
}
-void C2Client::clearResponseNodes() {
+void C2MetricsPublisher::clearMetricNodes() {
std::lock_guard<std::mutex> guard{metrics_mutex_};
root_response_nodes_.clear();
}
+state::response::NodeReporter::ReportedNode C2MetricsPublisher::getAgentManifest() {
+ gsl_Expects(response_node_loader_);
+ return response_node_loader_->getAgentManifest();
+}
+
+REGISTER_RESOURCE(C2MetricsPublisher, DescriptionOnly);
+
} // namespace org::apache::nifi::minifi::c2
diff --git a/libminifi/include/core/state/MetricsPublisher.h b/libminifi/src/c2/C2Utils.cpp
similarity index 60%
copy from libminifi/include/core/state/MetricsPublisher.h
copy to libminifi/src/c2/C2Utils.cpp
index f7cbc0e7d..1225cc309 100644
--- a/libminifi/include/core/state/MetricsPublisher.h
+++ b/libminifi/src/c2/C2Utils.cpp
@@ -15,21 +15,14 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-#pragma once
+#include "c2/C2Utils.h"
-#include <memory>
+namespace org::apache::nifi::minifi::c2 {
-#include "nodes/ResponseNodeLoader.h"
-#include "properties/Configure.h"
+bool isC2Enabled(const std::shared_ptr<Configure>& configuration) {
+ std::string c2_enable_str;
+ configuration->get(minifi::Configuration::nifi_c2_enable, "c2.enable", c2_enable_str);
+ return utils::StringUtils::toBool(c2_enable_str).value_or(false);
+}
-namespace org::apache::nifi::minifi::state {
-
-class MetricsPublisher {
- public:
- virtual void initialize(const std::shared_ptr<Configure>& configuration, response::ResponseNodeLoader& response_node_loader, core::ProcessGroup* root) = 0;
- virtual void clearMetricNodes() = 0;
- virtual void loadMetricNodes(core::ProcessGroup* root) = 0;
- virtual ~MetricsPublisher() = default;
-};
-
-} // namespace org::apache::nifi::minifi::state
+} // namespace org::apache::nifi::minifi::c2
diff --git a/libminifi/src/core/state/MetricsPublisherFactory.cpp b/libminifi/src/core/state/MetricsPublisherFactory.cpp
new file mode 100644
index 000000000..7453ce67b
--- /dev/null
+++ b/libminifi/src/core/state/MetricsPublisherFactory.cpp
@@ -0,0 +1,45 @@
+/**
+ *
+ * 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 "core/state/MetricsPublisherFactory.h"
+
+namespace org::apache::nifi::minifi::state {
+
+gsl::not_null<std::unique_ptr<MetricsPublisher>> createMetricsPublisher(const std::string& name, const std::shared_ptr<Configure>& configuration,
+ const std::shared_ptr<state::response::ResponseNodeLoader>& response_node_loader) {
+ auto ptr = core::ClassLoader::getDefaultClassLoader().instantiate(name, name);
+ if (!ptr) {
+ throw std::runtime_error("Configured metrics publisher class \"" + name + "\" could not be instantiated.");
+ }
+
+ auto metrics_publisher = utils::dynamic_unique_cast<MetricsPublisher>(std::move(ptr));
+ if (!metrics_publisher) {
+ throw std::runtime_error("Configured metrics publisher class \"" + name + "\" is not a MetricsPublisher.");
+ }
+
+ metrics_publisher->initialize(configuration, response_node_loader);
+ return gsl::make_not_null(std::move(metrics_publisher));
+}
+
+std::unique_ptr<MetricsPublisher> createMetricsPublisher(const std::shared_ptr<Configure>& configuration, const std::shared_ptr<state::response::ResponseNodeLoader>& response_node_loader) {
+ if (auto metrics_publisher_class = configuration->get(minifi::Configure::nifi_metrics_publisher_class)) {
+ return createMetricsPublisher(*metrics_publisher_class, configuration, response_node_loader);
+ }
+ return nullptr;
+}
+
+} // namespace org::apache::nifi::minifi::state
diff --git a/libminifi/src/core/state/MetricsPublisherStore.cpp b/libminifi/src/core/state/MetricsPublisherStore.cpp
new file mode 100644
index 000000000..6b1f2504b
--- /dev/null
+++ b/libminifi/src/core/state/MetricsPublisherStore.cpp
@@ -0,0 +1,68 @@
+/**
+ *
+ * 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 "core/state/MetricsPublisherStore.h"
+
+#include "c2/C2Utils.h"
+#include "core/state/MetricsPublisherFactory.h"
+
+namespace org::apache::nifi::minifi::state {
+
+MetricsPublisherStore::MetricsPublisherStore(std::shared_ptr<Configure> configuration, std::shared_ptr<core::Repository> provenance_repo,
+ std::shared_ptr<core::Repository> flow_file_repo, std::shared_ptr<core::FlowConfiguration> flow_configuration)
+ : configuration_(configuration),
+ response_node_loader_(std::make_shared<response::ResponseNodeLoader>(std::move(configuration), std::move(provenance_repo), std::move(flow_file_repo), std::move(flow_configuration))) {
+}
+
+void MetricsPublisherStore::initialize(core::controller::ControllerServiceProvider* controller, state::StateMonitor* update_sink) {
+ response_node_loader_->setControllerServiceProvider(controller);
+ response_node_loader_->setStateMonitor(update_sink);
+ if (minifi::c2::isC2Enabled(configuration_)) {
+ gsl::not_null<std::shared_ptr<state::MetricsPublisher>> c2_metrics_publisher = minifi::state::createMetricsPublisher(c2::C2_METRICS_PUBLISHER, configuration_, response_node_loader_);
+ addMetricsPublisher(c2::C2_METRICS_PUBLISHER, std::move(c2_metrics_publisher));
+ }
+
+ std::shared_ptr metrics_publisher = minifi::state::createMetricsPublisher(configuration_, response_node_loader_);
+ if (metrics_publisher) {
+ addMetricsPublisher(minifi::Configure::nifi_metrics_publisher_class, std::move(metrics_publisher));
+ }
+
+ loadMetricNodes(nullptr);
+}
+
+void MetricsPublisherStore::loadMetricNodes(core::ProcessGroup* root) {
+ response_node_loader_->setNewConfigRoot(root);
+ for (const auto& [name, publisher]: metrics_publishers_) {
+ publisher->loadMetricNodes();
+ }
+}
+
+void MetricsPublisherStore::clearMetricNodes() {
+ for (const auto& [name, publisher]: metrics_publishers_) {
+ publisher->clearMetricNodes();
+ }
+ response_node_loader_->clearConfigRoot();
+}
+
+std::weak_ptr<state::MetricsPublisher> MetricsPublisherStore::getMetricsPublisher(const std::string& name) const {
+ if (metrics_publishers_.contains(name)) {
+ return metrics_publishers_.at(name);
+ }
+ return {};
+}
+
+} // namespace org::apache::nifi::minifi::state
diff --git a/libminifi/src/core/state/nodes/MetricsBase.cpp b/libminifi/src/core/state/nodes/MetricsBase.cpp
new file mode 100644
index 000000000..950ef16d1
--- /dev/null
+++ b/libminifi/src/core/state/nodes/MetricsBase.cpp
@@ -0,0 +1,52 @@
+/**
+ * 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 "core/state/nodes/MetricsBase.h"
+
+namespace org::apache::nifi::minifi::state::response {
+
+std::vector<SerializedResponseNode> ResponseNode::serializeAndMergeResponseNodes(const std::vector<SharedResponseNode>& nodes) {
+ if (nodes.empty()) {
+ return {};
+ }
+
+ std::vector<SerializedResponseNode> result;
+ for (const auto& node: nodes) {
+ auto serialized = node->serialize();
+ result.insert(result.end(), serialized.begin(), serialized.end());
+ }
+ return result;
+}
+
+std::vector<SerializedResponseNode> ObjectNode::serialize() {
+ std::vector<SerializedResponseNode> serialized;
+ for (const auto& [name, nodes] : nodes_) {
+ if (nodes.empty()) {
+ continue;
+ }
+ SerializedResponseNode inner_node;
+ inner_node.name = nodes[0]->getName();
+ for (auto &embed : ResponseNode::serializeAndMergeResponseNodes(nodes)) {
+ if (!embed.empty() || embed.keep_empty) {
+ inner_node.children.push_back(std::move(embed));
+ }
+ }
+ serialized.push_back(std::move(inner_node));
+ }
+ return serialized;
+}
+
+} // namespace org::apache::nifi::minifi::state::response
diff --git a/libminifi/src/core/state/nodes/ResponseNodeLoader.cpp b/libminifi/src/core/state/nodes/ResponseNodeLoader.cpp
index df2f8720e..aeed88e99 100644
--- a/libminifi/src/core/state/nodes/ResponseNodeLoader.cpp
+++ b/libminifi/src/core/state/nodes/ResponseNodeLoader.cpp
@@ -25,59 +25,103 @@
#include "core/state/nodes/QueueMetrics.h"
#include "core/state/nodes/AgentInformation.h"
#include "core/state/nodes/ConfigurationChecksums.h"
-#include "c2/C2Agent.h"
#include "utils/gsl.h"
#include "utils/RegexUtils.h"
#include "utils/StringUtils.h"
+#include "c2/C2Utils.h"
namespace org::apache::nifi::minifi::state::response {
ResponseNodeLoader::ResponseNodeLoader(std::shared_ptr<Configure> configuration, std::shared_ptr<core::Repository> provenance_repo,
- std::shared_ptr<core::Repository> flow_file_repo, core::FlowConfiguration* flow_configuration)
+ std::shared_ptr<core::Repository> flow_file_repo, std::shared_ptr<core::FlowConfiguration> flow_configuration)
: configuration_(std::move(configuration)),
provenance_repo_(std::move(provenance_repo)),
flow_file_repo_(std::move(flow_file_repo)),
- flow_configuration_(flow_configuration) {
+ flow_configuration_(std::move(flow_configuration)) {
}
-void ResponseNodeLoader::initializeComponentMetrics(core::ProcessGroup* root) {
+void ResponseNodeLoader::clearConfigRoot() {
+ {
+ std::lock_guard<std::mutex> guard(system_metrics_mutex_);
+ system_metrics_.clear();
+ }
{
std::lock_guard<std::mutex> guard(component_metrics_mutex_);
component_metrics_.clear();
}
+ {
+ std::lock_guard<std::mutex> guard(root_mutex_);
+ root_ = nullptr;
+ }
+}
- if (!root) {
- return;
+void ResponseNodeLoader::setNewConfigRoot(core::ProcessGroup* root) {
+ {
+ std::lock_guard<std::mutex> guard(root_mutex_);
+ root_ = root;
}
+ initializeComponentMetrics();
+}
- std::vector<core::Processor*> processors;
- root->getAllProcessors(processors);
- for (const auto processor : processors) {
- auto node_source = dynamic_cast<ResponseNodeSource*>(processor);
- if (node_source == nullptr) {
- continue;
- }
- // we have a metrics source.
- auto metric = node_source->getResponseNodes();
+void ResponseNodeLoader::initializeComponentMetrics() {
+ {
std::lock_guard<std::mutex> guard(component_metrics_mutex_);
+ component_metrics_.clear();
+ }
+
+ std::vector<SharedResponseNode> metrics;
+ {
+ std::lock_guard<std::mutex> guard(root_mutex_);
+ if (!root_) {
+ return;
+ }
+
+ std::vector<core::Processor*> processors;
+ root_->getAllProcessors(processors);
+ for (const auto processor : processors) {
+ auto node_source = dynamic_cast<ResponseNodeSource*>(processor);
+ if (node_source == nullptr) {
+ continue;
+ }
+ metrics.push_back(node_source->getResponseNode());
+ }
+ }
+
+ std::lock_guard<std::mutex> guard(component_metrics_mutex_);
+ for (const auto& metric : metrics) {
component_metrics_[metric->getName()].push_back(metric);
}
}
-std::vector<std::shared_ptr<ResponseNode>> ResponseNodeLoader::getResponseNodes(const std::string& clazz) const {
- std::shared_ptr<core::CoreComponent> ptr = core::ClassLoader::getDefaultClassLoader().instantiate(clazz, clazz);
- if (ptr == nullptr) {
- return getComponentMetricsNodes(clazz);
+nonstd::expected<SharedResponseNode, std::string> ResponseNodeLoader::getSystemMetricsNode(const std::string& clazz) {
+ std::lock_guard<std::mutex> guard(system_metrics_mutex_);
+ if (system_metrics_.contains(clazz)) {
+ return system_metrics_.at(clazz);
}
+
+ std::shared_ptr ptr = core::ClassLoader::getDefaultClassLoader().instantiate(clazz, clazz);
auto response_node = std::dynamic_pointer_cast<ResponseNode>(ptr);
if (!response_node) {
- logger_->log_error("Instantiated class '%s' is not a ResponseNode!", clazz);
+ return nonstd::make_unexpected("Instantiated class '" + clazz + "' is not a ResponseNode!");
+ }
+ system_metrics_.emplace(clazz, gsl::make_not_null(response_node));
+ return system_metrics_.at(clazz);
+}
+
+std::vector<SharedResponseNode> ResponseNodeLoader::getResponseNodes(const std::string& clazz) {
+ auto component_metrics = getComponentMetricsNodes(clazz);
+ if (!component_metrics.empty()) {
+ return component_metrics;
+ }
+ auto response_node = getSystemMetricsNode(clazz);
+ if (!response_node) {
+ logger_->log_error(response_node.error().c_str());
return {};
}
- return {response_node};
+ return {*response_node};
}
-void ResponseNodeLoader::initializeRepositoryMetrics(const std::shared_ptr<ResponseNode>& response_node) const {
+void ResponseNodeLoader::initializeRepositoryMetrics(const SharedResponseNode& response_node) const {
auto repository_metrics = dynamic_cast<RepositoryMetrics*>(response_node.get());
if (repository_metrics != nullptr) {
repository_metrics->addRepository(provenance_repo_);
@@ -85,29 +129,30 @@ void ResponseNodeLoader::initializeRepositoryMetrics(const std::shared_ptr<Respo
}
}
-void ResponseNodeLoader::initializeQueueMetrics(const std::shared_ptr<ResponseNode>& response_node, core::ProcessGroup* root) {
- if (!root) {
+void ResponseNodeLoader::initializeQueueMetrics(const SharedResponseNode& response_node) const {
+ std::lock_guard<std::mutex> guard(root_mutex_);
+ if (!root_) {
return;
}
auto queue_metrics = dynamic_cast<QueueMetrics*>(response_node.get());
if (queue_metrics != nullptr) {
std::map<std::string, Connection*> connections;
- root->getConnections(connections);
+ root_->getConnections(connections);
for (const auto &con : connections) {
queue_metrics->updateConnection(con.second);
}
}
}
-void ResponseNodeLoader::initializeAgentIdentifier(const std::shared_ptr<ResponseNode>& response_node) const {
+void ResponseNodeLoader::initializeAgentIdentifier(const SharedResponseNode& response_node) const {
auto identifier = dynamic_cast<state::response::AgentIdentifier*>(response_node.get());
if (identifier != nullptr) {
identifier->setAgentIdentificationProvider(configuration_);
}
}
-void ResponseNodeLoader::initializeAgentMonitor(const std::shared_ptr<ResponseNode>& response_node) const {
+void ResponseNodeLoader::initializeAgentMonitor(const SharedResponseNode& response_node) const {
auto monitor = dynamic_cast<state::response::AgentMonitor*>(response_node.get());
if (monitor != nullptr) {
monitor->addRepository(provenance_repo_);
@@ -116,10 +161,10 @@ void ResponseNodeLoader::initializeAgentMonitor(const std::shared_ptr<ResponseNo
}
}
-void ResponseNodeLoader::initializeAgentNode(const std::shared_ptr<ResponseNode>& response_node) const {
+void ResponseNodeLoader::initializeAgentNode(const SharedResponseNode& response_node) const {
auto agent_node = dynamic_cast<state::response::AgentNode*>(response_node.get());
if (agent_node != nullptr && controller_ != nullptr) {
- agent_node->setUpdatePolicyController(std::static_pointer_cast<controllers::UpdatePolicyControllerService>(controller_->getControllerService(c2::C2Agent::UPDATE_NAME)).get());
+ agent_node->setUpdatePolicyController(std::static_pointer_cast<controllers::UpdatePolicyControllerService>(controller_->getControllerService(c2::UPDATE_NAME)).get());
}
if (agent_node != nullptr) {
agent_node->setConfigurationReader([this](const std::string& key){
@@ -128,7 +173,7 @@ void ResponseNodeLoader::initializeAgentNode(const std::shared_ptr<ResponseNode>
}
}
-void ResponseNodeLoader::initializeAgentStatus(const std::shared_ptr<ResponseNode>& response_node) const {
+void ResponseNodeLoader::initializeAgentStatus(const SharedResponseNode& response_node) const {
auto agent_status = dynamic_cast<state::response::AgentStatus*>(response_node.get());
if (agent_status != nullptr) {
agent_status->addRepository(provenance_repo_);
@@ -137,7 +182,7 @@ void ResponseNodeLoader::initializeAgentStatus(const std::shared_ptr<ResponseNod
}
}
-void ResponseNodeLoader::initializeConfigurationChecksums(const std::shared_ptr<ResponseNode>& response_node) const {
+void ResponseNodeLoader::initializeConfigurationChecksums(const SharedResponseNode& response_node) const {
auto configuration_checksums = dynamic_cast<state::response::ConfigurationChecksums*>(response_node.get());
if (configuration_checksums) {
configuration_checksums->addChecksumCalculator(configuration_->getChecksumCalculator());
@@ -147,15 +192,16 @@ void ResponseNodeLoader::initializeConfigurationChecksums(const std::shared_ptr<
}
}
-void ResponseNodeLoader::initializeFlowMonitor(const std::shared_ptr<ResponseNode>& response_node, core::ProcessGroup* root) const {
+void ResponseNodeLoader::initializeFlowMonitor(const SharedResponseNode& response_node) const {
auto flowMonitor = dynamic_cast<state::response::FlowMonitor*>(response_node.get());
if (flowMonitor == nullptr) {
return;
}
std::map<std::string, Connection*> connections;
- if (root) {
- root->getConnections(connections);
+ std::lock_guard<std::mutex> guard(root_mutex_);
+ if (root_) {
+ root_->getConnections(connections);
}
for (auto &con : connections) {
@@ -167,7 +213,7 @@ void ResponseNodeLoader::initializeFlowMonitor(const std::shared_ptr<ResponseNod
}
}
-std::vector<std::shared_ptr<ResponseNode>> ResponseNodeLoader::loadResponseNodes(const std::string& clazz, core::ProcessGroup* root) const {
+std::vector<SharedResponseNode> ResponseNodeLoader::loadResponseNodes(const std::string& clazz) {
auto response_nodes = getResponseNodes(clazz);
if (response_nodes.empty()) {
logger_->log_error("No metric defined for %s", clazz);
@@ -176,19 +222,19 @@ std::vector<std::shared_ptr<ResponseNode>> ResponseNodeLoader::loadResponseNodes
for (const auto& response_node : response_nodes) {
initializeRepositoryMetrics(response_node);
- initializeQueueMetrics(response_node, root);
+ initializeQueueMetrics(response_node);
initializeAgentIdentifier(response_node);
initializeAgentMonitor(response_node);
initializeAgentNode(response_node);
initializeAgentStatus(response_node);
initializeConfigurationChecksums(response_node);
- initializeFlowMonitor(response_node, root);
+ initializeFlowMonitor(response_node);
}
return response_nodes;
}
-std::vector<std::shared_ptr<ResponseNode>> ResponseNodeLoader::getMatchingComponentMetricsNodes(const std::string& regex_str) const {
- std::vector<std::shared_ptr<ResponseNode>> result;
+std::vector<SharedResponseNode> ResponseNodeLoader::getMatchingComponentMetricsNodes(const std::string& regex_str) const {
+ std::vector<SharedResponseNode> result;
for (const auto& [metric_name, metrics] : component_metrics_) {
utils::Regex regex(regex_str);
if (utils::regexMatch(metric_name, regex)) {
@@ -198,7 +244,7 @@ std::vector<std::shared_ptr<ResponseNode>> ResponseNodeLoader::getMatchingCompon
return result;
}
-std::vector<std::shared_ptr<ResponseNode>> ResponseNodeLoader::getComponentMetricsNodes(const std::string& metrics_class) const {
+std::vector<SharedResponseNode> ResponseNodeLoader::getComponentMetricsNodes(const std::string& metrics_class) const {
if (metrics_class.empty()) {
return {};
}
@@ -224,5 +270,25 @@ void ResponseNodeLoader::setStateMonitor(state::StateMonitor* update_sink) {
update_sink_ = update_sink;
}
+state::response::NodeReporter::ReportedNode ResponseNodeLoader::getAgentManifest() const {
+ state::response::AgentInformation agentInfo("agentInfo");
+ if (controller_) {
+ agentInfo.setUpdatePolicyController(std::static_pointer_cast<controllers::UpdatePolicyControllerService>(controller_->getControllerService(c2::UPDATE_NAME)).get());
+ }
+ agentInfo.setAgentIdentificationProvider(configuration_);
+ agentInfo.setConfigurationReader([this](const std::string& key){
+ return configuration_->getRawValue(key);
+ });
+ if (update_sink_) {
+ agentInfo.setStateMonitor(update_sink_);
+ }
+ agentInfo.includeAgentStatus(false);
+ state::response::NodeReporter::ReportedNode reported_node;
+ reported_node.name = agentInfo.getName();
+ reported_node.is_array = agentInfo.isArray();
+ reported_node.serialized_nodes = agentInfo.serialize();
+ return reported_node;
+}
+
} // namespace org::apache::nifi::minifi::state::response
diff --git a/libminifi/test/flow-tests/TestControllerWithFlow.h b/libminifi/test/flow-tests/TestControllerWithFlow.h
index 7cd5d3e23..ced53fea9 100644
--- a/libminifi/test/flow-tests/TestControllerWithFlow.h
+++ b/libminifi/test/flow-tests/TestControllerWithFlow.h
@@ -50,6 +50,7 @@ class TestControllerWithFlow: public TestController {
configuration_ = std::make_shared<minifi::Configure>();
configuration_->setHome(home_.string());
configuration_->set(minifi::Configure::nifi_flow_configuration_file, yaml_path_.string());
+ configuration_->set(minifi::Configure::nifi_c2_enable, "true");
if (setup_flow) {
setupFlow();
@@ -64,14 +65,12 @@ class TestControllerWithFlow: public TestController {
REQUIRE(content_repo->initialize(configuration_));
std::shared_ptr<minifi::io::StreamFactory> stream_factory = minifi::io::StreamFactory::getInstance(configuration_);
- auto flow = std::make_unique<core::YamlConfiguration>(core::ConfigurationContext{ff_repo, content_repo, stream_factory, configuration_, yaml_path_.string()});
+ auto flow = std::make_shared<core::YamlConfiguration>(core::ConfigurationContext{ff_repo, content_repo, stream_factory, configuration_, yaml_path_.string()});
auto root = flow->getRoot();
root_ = root.get();
- controller_ = std::make_shared<minifi::FlowController>(
- prov_repo, ff_repo, configuration_,
- std::move(flow),
- content_repo, DEFAULT_ROOT_GROUP_NAME,
- std::make_shared<utils::file::FileSystem>(), []{});
+ auto metrics_publisher_store = std::make_unique<minifi::state::MetricsPublisherStore>(configuration_, prov_repo, ff_repo, flow);
+ metrics_publisher_store_ = metrics_publisher_store.get();
+ controller_ = std::make_shared<minifi::FlowController>(prov_repo, ff_repo, configuration_, std::move(flow), content_repo, std::move(metrics_publisher_store));
controller_->load(std::move(root));
}
@@ -82,7 +81,6 @@ class TestControllerWithFlow: public TestController {
~TestControllerWithFlow() {
if (controller_) {
controller_->stop();
- controller_->unload();
}
LogTestController::getInstance().reset();
}
@@ -92,4 +90,5 @@ class TestControllerWithFlow: public TestController {
std::shared_ptr<minifi::Configure> configuration_;
std::shared_ptr<minifi::FlowController> controller_;
core::ProcessGroup* root_{nullptr};
+ minifi::state::MetricsPublisherStore* metrics_publisher_store_{nullptr};
};
diff --git a/libminifi/test/integration/IntegrationBase.h b/libminifi/test/integration/IntegrationBase.h
index 8fd5a7553..84bfedc7e 100644
--- a/libminifi/test/integration/IntegrationBase.h
+++ b/libminifi/test/integration/IntegrationBase.h
@@ -36,6 +36,8 @@
#include "controllers/SSLContextService.h"
#include "HTTPUtils.h"
#include "utils/FifoExecutor.h"
+#include "core/state/MetricsPublisherFactory.h"
+#include "c2/C2Utils.h"
namespace minifi = org::apache::nifi::minifi;
namespace core = minifi::core;
@@ -113,6 +115,7 @@ class IntegrationBase {
void configureSecurity();
std::shared_ptr<minifi::Configure> configuration;
+ std::unique_ptr<minifi::state::response::ResponseNodeLoader> response_node_loader_;
std::unique_ptr<minifi::FlowController> flowController_;
std::chrono::milliseconds wait_time_;
std::string port, scheme;
@@ -174,7 +177,7 @@ void IntegrationBase::run(const std::optional<std::filesystem::path>& test_file_
filesystem = std::make_shared<utils::file::FileSystem>();
}
- auto flow_config = std::make_unique<core::YamlConfiguration>(core::ConfigurationContext{test_repo, content_repo, stream_factory, configuration, test_file_location, filesystem});
+ auto flow_config = std::make_shared<core::YamlConfiguration>(core::ConfigurationContext{test_repo, content_repo, stream_factory, configuration, test_file_location, filesystem});
auto controller_service_provider = flow_config->getControllerServiceProvider();
char state_dir_name_template[] = "/var/tmp/integrationstate.XXXXXX";
@@ -191,8 +194,10 @@ void IntegrationBase::run(const std::optional<std::filesystem::path>& test_file_
++restart_requested_count_;
running = true;
};
- flowController_ = std::make_unique<minifi::FlowController>(test_repo, test_flow_repo, configuration, std::move(flow_config), content_repo, DEFAULT_ROOT_GROUP_NAME,
- std::make_shared<utils::file::FileSystem>(), request_restart);
+
+ auto metrics_publisher_store = std::make_unique<minifi::state::MetricsPublisherStore>(configuration, test_repo, test_flow_repo, flow_config);
+ flowController_ = std::make_unique<minifi::FlowController>(test_repo, test_flow_repo, configuration,
+ std::move(flow_config), content_repo, std::move(metrics_publisher_store), filesystem, request_restart);
flowController_->load();
updateProperties(*flowController_);
flowController_->start();
@@ -209,8 +214,7 @@ void IntegrationBase::run(const std::optional<std::filesystem::path>& test_file_
// Only stop servers if we're shutting down
shutdownBeforeFlowController();
}
- flowController_->unload();
- flowController_->stopC2();
+ flowController_->stop();
}
cleanup();
diff --git a/libminifi/test/integration/OnScheduleErrorHandlingTests.cpp b/libminifi/test/integration/OnScheduleErrorHandlingTests.cpp
index 78526ce7b..ef55f386e 100644
--- a/libminifi/test/integration/OnScheduleErrorHandlingTests.cpp
+++ b/libminifi/test/integration/OnScheduleErrorHandlingTests.cpp
@@ -78,16 +78,16 @@ class EventDriverScheduleErrorHandlingTests: public IntegrationBase {
void updateProperties(minifi::FlowController& fc) override {
/* This tests depends on a configuration that contains only one KamikazeProcessor named kamikaze
* (See testOnScheduleRetry.yml)
- * In this case there are two components in the flowcontroller: first is the controller itself,
- * second is the processor that the test uses.
+ * In this case there are two components in the flowcontroller: first is the processor that the test uses,
+ * second is the controller itself.
* Added here some assertions to make it clear. In case any of these fail without changing the corresponding yml file,
* that most probably means a breaking change. */
size_t controllerVecIdx = 0;
fc.executeOnAllComponents([&controllerVecIdx](org::apache::nifi::minifi::state::StateController& component){
- if (controllerVecIdx == 0) {
+ if (controllerVecIdx == 1) {
assert(component.getComponentName() == "FlowController");
- } else if (controllerVecIdx == 1) {
+ } else if (controllerVecIdx == 0) {
assert(component.getComponentName() == "kamikaze");
auto process_controller = dynamic_cast<org::apache::nifi::minifi::state::ProcessorController*>(&component);
diff --git a/libminifi/test/integration/ProvenanceReportingTest.cpp b/libminifi/test/integration/ProvenanceReportingTest.cpp
index 8457c5146..91953c41f 100644
--- a/libminifi/test/integration/ProvenanceReportingTest.cpp
+++ b/libminifi/test/integration/ProvenanceReportingTest.cpp
@@ -59,9 +59,7 @@ int main(int argc, char **argv) {
std::unique_ptr<core::FlowConfiguration> yaml_ptr = std::make_unique<core::YamlConfiguration>(
core::ConfigurationContext{ test_repo, content_repo, stream_factory, configuration, test_file_location});
- const auto controller = std::make_shared<minifi::FlowController>(
- test_repo, test_flow_repo, configuration, std::move(yaml_ptr), content_repo, DEFAULT_ROOT_GROUP_NAME,
- std::make_shared<utils::file::FileSystem>(), []{});
+ const auto controller = std::make_shared<minifi::FlowController>(test_repo, test_flow_repo, configuration, std::move(yaml_ptr), content_repo);
core::YamlConfiguration yaml_config({test_repo, content_repo, stream_factory, configuration, test_file_location});
diff --git a/libminifi/test/integration/StateTransactionalityTests.cpp b/libminifi/test/integration/StateTransactionalityTests.cpp
index ff3d86e95..b95683b8c 100644
--- a/libminifi/test/integration/StateTransactionalityTests.cpp
+++ b/libminifi/test/integration/StateTransactionalityTests.cpp
@@ -58,16 +58,16 @@ class StatefulIntegrationTest : public IntegrationBase {
void updateProperties(minifi::FlowController& fc) override {
/* This tests depends on a configuration that contains only one StatefulProcessor named statefulProcessor
* (See TestStateTransactionality.yml)
- * In this case there are two components in the flowcontroller: first is the controller itself,
- * second is the processor that the test uses.
+ * In this case there are two components in the flowcontroller: first is the processor that the test uses,
+ * second is the controller itself.
* Added here some assertions to make it clear. In case any of these fail without changing the corresponding yml file,
* that most probably means a breaking change. */
size_t controllerVecIdx = 0;
fc.executeOnAllComponents([this, &controllerVecIdx](org::apache::nifi::minifi::state::StateController& component){
- if (controllerVecIdx == 0) {
+ if (controllerVecIdx == 1) {
assert(component.getComponentName() == "FlowController");
- } else if (controllerVecIdx == 1) {
+ } else if (controllerVecIdx == 0) {
assert(component.getComponentName() == "statefulProcessor");
// set hooks
const auto processController = dynamic_cast<ProcessorController*>(&component);
diff --git a/libminifi/test/persistence-tests/PersistenceTests.cpp b/libminifi/test/persistence-tests/PersistenceTests.cpp
index 748775fcd..0c841e57b 100644
--- a/libminifi/test/persistence-tests/PersistenceTests.cpp
+++ b/libminifi/test/persistence-tests/PersistenceTests.cpp
@@ -179,8 +179,7 @@ TEST_CASE("Processors Can Store FlowFiles", "[TestP1]") {
content_repo->initialize(config);
auto flowConfig = std::make_unique<core::FlowConfiguration>(core::ConfigurationContext{ff_repository, content_repo, nullptr, config, ""});
- auto flowController = std::make_shared<minifi::FlowController>(
- prov_repo, ff_repository, config, std::move(flowConfig), content_repo, "", std::make_shared<utils::file::FileSystem>(), []{});
+ auto flowController = std::make_shared<minifi::FlowController>(prov_repo, ff_repository, config, std::move(flowConfig), content_repo);
{
TestFlow flow(ff_repository, content_repo, prov_repo, setupMergeProcessor, MergeContent::Merge);
@@ -197,7 +196,7 @@ TEST_CASE("Processors Can Store FlowFiles", "[TestP1]") {
flow.trigger();
ff_repository->stop();
- flowController->unload();
+ flowController->stop();
// check if the processor has taken ownership
std::set<std::shared_ptr<core::FlowFile>> expired;
@@ -224,7 +223,7 @@ TEST_CASE("Processors Can Store FlowFiles", "[TestP1]") {
flow.trigger();
ff_repository->stop();
- flowController->unload();
+ flowController->stop();
std::shared_ptr<org::apache::nifi::minifi::core::FlowFile> file = nullptr;
std::set<std::shared_ptr<core::FlowFile>> expired;
const auto flowFileArrivedInOutput = [&file, &expired, &flow] {
@@ -298,8 +297,7 @@ TEST_CASE("Persisted flowFiles are updated on modification", "[TestP1]") {
content_repo->initialize(config);
auto flowConfig = std::make_unique<core::FlowConfiguration>(core::ConfigurationContext{ff_repository, content_repo, nullptr, config, ""});
- auto flowController = std::make_shared<minifi::FlowController>(
- prov_repo, ff_repository, config, std::move(flowConfig), content_repo, "", std::make_shared<utils::file::FileSystem>(), []{});
+ auto flowController = std::make_shared<minifi::FlowController>(prov_repo, ff_repository, config, std::move(flowConfig), content_repo);
{
TestFlow flow(ff_repository, content_repo, prov_repo, setupContentUpdaterProcessor, {"success", "d"});
@@ -334,7 +332,7 @@ TEST_CASE("Persisted flowFiles are updated on modification", "[TestP1]") {
REQUIRE(LogTestController::getInstance().countOccurrences("Deleting resource") == 1);
ff_repository->stop();
- flowController->unload();
+ flowController->stop();
}
// swap the ProcessGroup and restart the FlowController
@@ -360,7 +358,7 @@ TEST_CASE("Persisted flowFiles are updated on modification", "[TestP1]") {
REQUIRE(file->getResourceClaim()->getFlowFileRecordOwnedCount() == 2);
ff_repository->stop();
- flowController->unload();
+ flowController->stop();
}
}
diff --git a/libminifi/test/rocksdb-tests/RepoTests.cpp b/libminifi/test/rocksdb-tests/RepoTests.cpp
index 7a646b545..19cd38f31 100644
--- a/libminifi/test/rocksdb-tests/RepoTests.cpp
+++ b/libminifi/test/rocksdb-tests/RepoTests.cpp
@@ -281,8 +281,7 @@ TEST_CASE("Test FlowFile Restore", "[TestFFR6]") {
root->addConnection(std::move(input));
auto flowConfig = std::make_unique<core::FlowConfiguration>(core::ConfigurationContext{ff_repository, content_repo, nullptr, config, ""});
- auto flowController = std::make_shared<minifi::FlowController>(
- prov_repo, ff_repository, config, std::move(flowConfig), content_repo, "", std::make_shared<utils::file::FileSystem>(), []{});
+ auto flowController = std::make_shared<minifi::FlowController>(prov_repo, ff_repository, config, std::move(flowConfig), content_repo);
std::string data = "banana";
minifi::io::BufferStream content(data);
diff --git a/libminifi/test/unit/ProvenanceTestHelper.h b/libminifi/test/unit/ProvenanceTestHelper.h
index 6da7d14e1..66e82d18b 100644
--- a/libminifi/test/unit/ProvenanceTestHelper.h
+++ b/libminifi/test/unit/ProvenanceTestHelper.h
@@ -222,24 +222,22 @@ class TestFlowController : public org::apache::nifi::minifi::FlowController {
TestFlowController(std::shared_ptr<org::apache::nifi::minifi::core::Repository> repo, std::shared_ptr<org::apache::nifi::minifi::core::Repository> flow_file_repo,
const std::shared_ptr<org::apache::nifi::minifi::core::ContentRepository>& /*content_repo*/)
:org::apache::nifi::minifi::FlowController(repo, flow_file_repo, std::make_shared<org::apache::nifi::minifi::Configure>(), nullptr,
- std::make_shared<org::apache::nifi::minifi::core::repository::VolatileContentRepository>(), "",
- std::make_shared<org::apache::nifi::minifi::utils::file::FileSystem>(), []{}) {
+ std::make_shared<org::apache::nifi::minifi::core::repository::VolatileContentRepository>()) {
}
~TestFlowController() override = default;
- void load(std::unique_ptr<org::apache::nifi::minifi::core::ProcessGroup> /*root*/ = nullptr, bool /*reload*/ = false) override {
+ void load(bool /*reload*/ = false) override {
}
int16_t start() override {
- running_.store(true);
return 0;
}
int16_t stop() override {
- running_.store(false);
return 0;
}
+
void waitUnload(const uint64_t /*timeToWaitMs*/) override {
stop();
}
@@ -252,10 +250,6 @@ class TestFlowController : public org::apache::nifi::minifi::FlowController {
return -1;
}
- void unload() override {
- stop();
- }
-
bool isRunning() const override {
return true;
}
diff --git a/libminifi/test/unit/ResponseNodeLoaderTests.cpp b/libminifi/test/unit/ResponseNodeLoaderTests.cpp
index 52f9238f1..1c5d41ea1 100644
--- a/libminifi/test/unit/ResponseNodeLoaderTests.cpp
+++ b/libminifi/test/unit/ResponseNodeLoaderTests.cpp
@@ -44,7 +44,7 @@ class ResponseNodeLoaderTestFixture {
addConnection("Connection", "success", uuid1, uuid2);
auto uuid3 = addProcessor<minifi::processors::ReadFromFlowFileTestProcessor>("ReadFromFlowFileTestProcessor");
addConnection("Connection", "success", uuid2, uuid3);
- response_node_loader_.initializeComponentMetrics(root_.get());
+ response_node_loader_.setNewConfigRoot(root_.get());
}
protected:
@@ -73,29 +73,29 @@ class ResponseNodeLoaderTestFixture {
};
TEST_CASE_METHOD(ResponseNodeLoaderTestFixture, "Load non-existent response node", "[responseNodeLoaderTest]") {
- auto nodes = response_node_loader_.loadResponseNodes("NonExistentNode", root_.get());
+ auto nodes = response_node_loader_.loadResponseNodes("NonExistentNode");
REQUIRE(nodes.empty());
}
TEST_CASE_METHOD(ResponseNodeLoaderTestFixture, "Load processor metrics node not part of the flow config", "[responseNodeLoaderTest]") {
- auto nodes = response_node_loader_.loadResponseNodes("TailFileMetrics", root_.get());
+ auto nodes = response_node_loader_.loadResponseNodes("TailFileMetrics");
REQUIRE(nodes.empty());
}
TEST_CASE_METHOD(ResponseNodeLoaderTestFixture, "Load system metrics node", "[responseNodeLoaderTest]") {
- auto nodes = response_node_loader_.loadResponseNodes("QueueMetrics", root_.get());
+ auto nodes = response_node_loader_.loadResponseNodes("QueueMetrics");
REQUIRE(nodes.size() == 1);
REQUIRE(nodes[0]->getName() == "QueueMetrics");
}
TEST_CASE_METHOD(ResponseNodeLoaderTestFixture, "Load processor metrics node part of the flow config", "[responseNodeLoaderTest]") {
- auto nodes = response_node_loader_.loadResponseNodes("ReadFromFlowFileTestProcessorMetrics", root_.get());
+ auto nodes = response_node_loader_.loadResponseNodes("ReadFromFlowFileTestProcessorMetrics");
REQUIRE(nodes.size() == 1);
REQUIRE(nodes[0]->getName() == "ReadFromFlowFileTestProcessorMetrics");
}
TEST_CASE_METHOD(ResponseNodeLoaderTestFixture, "Load multiple processor metrics nodes of the same type in a single flow", "[responseNodeLoaderTest]") {
- auto nodes = response_node_loader_.loadResponseNodes("WriteToFlowFileTestProcessorMetrics", root_.get());
+ auto nodes = response_node_loader_.loadResponseNodes("WriteToFlowFileTestProcessorMetrics");
REQUIRE(nodes.size() == 2);
REQUIRE(nodes[0]->getName() == "WriteToFlowFileTestProcessorMetrics");
REQUIRE(nodes[1]->getName() == "WriteToFlowFileTestProcessorMetrics");
@@ -103,7 +103,7 @@ TEST_CASE_METHOD(ResponseNodeLoaderTestFixture, "Load multiple processor metrics
TEST_CASE_METHOD(ResponseNodeLoaderTestFixture, "Use regex to filter processor metrics", "[responseNodeLoaderTest]") {
SECTION("Load all processor metrics with regex") {
- auto nodes = response_node_loader_.loadResponseNodes("processorMetrics/.*", root_.get());
+ auto nodes = response_node_loader_.loadResponseNodes("processorMetrics/.*");
std::unordered_map<std::string, uint32_t> metric_counts;
REQUIRE(nodes.size() == 3);
for (const auto& node : nodes) {
@@ -114,19 +114,19 @@ TEST_CASE_METHOD(ResponseNodeLoaderTestFixture, "Use regex to filter processor m
}
SECTION("Filter for a single processor") {
- auto nodes = response_node_loader_.loadResponseNodes("processorMetrics/Read.*", root_.get());
+ auto nodes = response_node_loader_.loadResponseNodes("processorMetrics/Read.*");
REQUIRE(nodes.size() == 1);
REQUIRE(nodes[0]->getName() == "ReadFromFlowFileTestProcessorMetrics");
}
SECTION("Full match") {
- auto nodes = response_node_loader_.loadResponseNodes("processorMetrics/ReadFromFlowFileTestProcessorMetrics", root_.get());
+ auto nodes = response_node_loader_.loadResponseNodes("processorMetrics/ReadFromFlowFileTestProcessorMetrics");
REQUIRE(nodes.size() == 1);
REQUIRE(nodes[0]->getName() == "ReadFromFlowFileTestProcessorMetrics");
}
SECTION("No partial match is allowed") {
- auto nodes = response_node_loader_.loadResponseNodes("processorMetrics/Read", root_.get());
+ auto nodes = response_node_loader_.loadResponseNodes("processorMetrics/Read");
REQUIRE(nodes.empty());
}
}
diff --git a/minifi_main/MiNiFiMain.cpp b/minifi_main/MiNiFiMain.cpp
index a3ed6a483..0c1cc4915 100644
--- a/minifi_main/MiNiFiMain.cpp
+++ b/minifi_main/MiNiFiMain.cpp
@@ -66,6 +66,10 @@
#include "AgentDocs.h"
#include "MainHelper.h"
#include "agent/JsonSchema.h"
+#include "core/state/nodes/ResponseNodeLoader.h"
+#include "c2/C2Agent.h"
+#include "core/state/MetricsPublisherFactory.h"
+#include "core/state/MetricsPublisherStore.h"
namespace minifi = org::apache::nifi::minifi;
namespace core = minifi::core;
@@ -369,7 +373,7 @@ int main(int argc, char **argv) {
should_encrypt_flow_config,
utils::crypto::EncryptionProvider::create(minifiHome));
- std::unique_ptr<core::FlowConfiguration> flow_configuration = core::createFlowConfiguration(
+ std::shared_ptr<core::FlowConfiguration> flow_configuration = core::createFlowConfiguration(
core::ConfigurationContext{
.flow_file_repo = flow_repo,
.content_repo = content_repo,
@@ -378,8 +382,10 @@ int main(int argc, char **argv) {
.path = configure->get(minifi::Configure::nifi_flow_configuration_file),
.filesystem = filesystem}, nifi_configuration_class_name);
+ auto metrics_publisher_store = std::make_unique<minifi::state::MetricsPublisherStore>(configure, prov_repo, flow_repo, flow_configuration);
+
const auto controller = std::make_unique<minifi::FlowController>(
- prov_repo, flow_repo, configure, std::move(flow_configuration), content_repo, filesystem, request_restart);
+ prov_repo, flow_repo, configure, std::move(flow_configuration), content_repo, std::move(metrics_publisher_store), filesystem, request_restart);
const bool disk_space_watchdog_enable = (configure->get(minifi::Configure::minifi_disk_space_watchdog_enable) | utils::map([](const std::string& v) { return v == "true"; })).value_or(true);
std::unique_ptr<utils::CallBackTimer> disk_space_watchdog;
@@ -412,7 +418,6 @@ int main(int argc, char **argv) {
disk_space_watchdog = std::make_unique<utils::CallBackTimer>(config.interval, [interval_switch, min_space, repo_paths, logger, &controller]() mutable {
const auto stop = [&] {
controller->stop();
- controller->unload();
};
const auto restart = [&] {
controller->load();
@@ -477,7 +482,6 @@ int main(int argc, char **argv) {
* Trigger unload -- wait stop_wait_time
*/
controller->waitUnload(stop_wait_time);
- controller->stopC2();
flow_repo = nullptr;
prov_repo = nullptr;
} while ([&] {
diff --git a/nanofi/include/cxx/C2CallbackAgent.h b/nanofi/include/cxx/C2CallbackAgent.h
index e2d5853e5..02760277e 100644
--- a/nanofi/include/cxx/C2CallbackAgent.h
+++ b/nanofi/include/cxx/C2CallbackAgent.h
@@ -15,8 +15,7 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
-#ifndef LIBMINIFI_INCLUDE_C2_C2CALLBACKAGENT_H_
-#define LIBMINIFI_INCLUDE_C2_C2CALLBACKAGENT_H_
+#pragma once
#include <utility>
#include <functional>
@@ -31,11 +30,7 @@
#include "c2/C2Protocol.h"
#include "io/validation.h"
-namespace org {
-namespace apache {
-namespace nifi {
-namespace minifi {
-namespace c2 {
+namespace org::apache::nifi::minifi::c2 {
typedef int c2_ag_update_callback(char *);
@@ -46,11 +41,7 @@ typedef int c2_ag_start_callback(char *);
class C2CallbackAgent : public c2::C2Agent {
public:
- explicit C2CallbackAgent(
- core::controller::ControllerServiceProvider* controller,
- state::Pausable* pause_handler,
- state::StateMonitor* updateSink,
- const std::shared_ptr<Configure> &configure);
+ explicit C2CallbackAgent(const std::shared_ptr<Configure> &configure);
~C2CallbackAgent() override = default;
@@ -72,11 +63,4 @@ class C2CallbackAgent : public c2::C2Agent {
std::shared_ptr<core::logging::Logger> logger_ = core::logging::LoggerFactory<C2CallbackAgent>::getLogger();
};
-} /* namesapce c2 */
-} /* namespace minifi */
-} /* namespace nifi */
-} /* namespace apache */
-} /* namespace org */
-
-
-#endif /* LIBMINIFI_INCLUDE_C2_C2CALLBACKAGENT_H_ */
+} // namespace org::apache::nifi::minifi::c2
diff --git a/nanofi/include/cxx/Instance.h b/nanofi/include/cxx/Instance.h
index f9e42bd9a..427eed117 100644
--- a/nanofi/include/cxx/Instance.h
+++ b/nanofi/include/cxx/Instance.h
@@ -101,7 +101,7 @@ class Instance {
configure_->set(minifi::Configuration::nifi_c2_rest_url, server->url);
configure_->set(minifi::Configuration::nifi_c2_rest_url_ack, server->ack_url);
}
- agent_ = std::make_shared<c2::C2CallbackAgent>(nullptr, nullptr, nullptr, configure_);
+ agent_ = std::make_shared<c2::C2CallbackAgent>(configure_);
listener_thread_pool_.start();
registerUpdateListener(agent_, 1000);
agent_->setStopCallback(c1);
diff --git a/nanofi/src/cxx/C2CallbackAgent.cpp b/nanofi/src/cxx/C2CallbackAgent.cpp
index d4af51260..2e59916f2 100644
--- a/nanofi/src/cxx/C2CallbackAgent.cpp
+++ b/nanofi/src/cxx/C2CallbackAgent.cpp
@@ -28,11 +28,11 @@
#include "core/logging/LoggerConfiguration.h"
#include "utils/file/FileUtils.h"
#include "utils/file/FileManager.h"
+#include "c2/C2MetricsPublisher.h"
namespace org::apache::nifi::minifi::c2 {
-C2CallbackAgent::C2CallbackAgent(core::controller::ControllerServiceProvider* controller, state::Pausable* pause_handler, state::StateMonitor* updateSink,
- const std::shared_ptr<Configure> &configuration)
- : C2Agent(controller, pause_handler, updateSink, configuration, std::make_shared<utils::file::FileSystem>(), []{}),
+C2CallbackAgent::C2CallbackAgent(const std::shared_ptr<Configure> &configuration)
+ : C2Agent(configuration, std::weak_ptr<C2MetricsPublisher>(), std::make_shared<utils::file::FileSystem>(), []{}),
stop(nullptr) {
}