Skip to content

Commit b61e730

Browse files
committed
MINIFICPP-2669 - Reduce controller service api
1 parent ef3dd56 commit b61e730

File tree

118 files changed

+997
-1056
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+997
-1056
lines changed

controller/MiNiFiController.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@
3030
#include "c2/ControllerSocketProtocol.h"
3131
#include "controllers/SSLContextService.h"
3232
#include "core/ConfigurationFactory.h"
33-
#include "minifi-cpp/core/controller/ControllerService.h"
3433
#include "core/extension/ExtensionManager.h"
3534
#include "properties/Configure.h"
3635
#include "range/v3/algorithm/contains.hpp"
@@ -42,8 +41,7 @@ std::shared_ptr<minifi::controllers::SSLContextServiceInterface> getSSLContextSe
4241
std::shared_ptr<minifi::controllers::SSLContextServiceInterface> secure_context;
4342
std::string secure_str;
4443
if (configuration->get(minifi::Configure::nifi_remote_input_secure, secure_str) && minifi::utils::string::toBool(secure_str).value_or(false)) {
45-
secure_context = std::make_shared<minifi::controllers::SSLContextService>("ControllerSocketProtocolSSL", configuration);
46-
secure_context->onEnable();
44+
secure_context = minifi::controllers::SSLContextService::createAndEnable("ControllerSocketProtocolSSL", configuration);
4745
}
4846

4947
return secure_context;

controller/tests/ControllerTests.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -228,8 +228,7 @@ class ControllerTestFixture {
228228
configuration_->set(minifi::Configure::nifi_security_client_private_key, (minifi::utils::file::FileUtils::get_executable_dir() / "resources" / "minifi-cpp-flow.key").string());
229229
configuration_->set(minifi::Configure::nifi_security_client_pass_phrase, "abcdefgh");
230230
configuration_->set(minifi::Configure::nifi_security_client_ca_certificate, (minifi::utils::file::FileUtils::get_executable_dir() / "resources" / "root-ca.pem").string());
231-
ssl_context_service_ = std::make_shared<controllers::SSLContextService>("SSLContextService", configuration_);
232-
ssl_context_service_->onEnable();
231+
ssl_context_service_ = controllers::SSLContextService::createAndEnable("SSLContextService", configuration_);
233232
controller_socket_data_.host = "localhost";
234233
controller_socket_data_.port = 9997;
235234
}

core-framework/include/core/Resource.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
#include "utils/OptionalUtils.h"
3131
#include "utils/Macro.h"
3232
#include "core/ProcessorFactoryImpl.h"
33+
#include "core/controller/ControllerServiceFactoryImpl.h"
3334
#include "core/ObjectFactory.h"
3435

3536
namespace org::apache::nifi::minifi::core {
@@ -61,6 +62,11 @@ class StaticClassType {
6162
auto factory = std::unique_ptr<ProcessorFactory>(new ProcessorFactoryImpl<Class>(module_name));
6263
getClassLoader().registerClass(construction_name, std::move(factory));
6364
}
65+
} else if constexpr (Type == ResourceType::ControllerService) {
66+
for (const auto& construction_name : construction_names_) {
67+
auto factory = std::unique_ptr<controller::ControllerServiceFactory>(new controller::ControllerServiceFactoryImpl<Class>(module_name));
68+
getClassLoader().registerClass(construction_name, std::move(factory));
69+
}
6470
} else {
6571
for (const auto& construction_name : construction_names_) {
6672
auto factory = std::unique_ptr<ObjectFactory>(new DefaultObjectFactory<Class>(module_name));

core-framework/include/core/controller/ControllerService.h

Lines changed: 0 additions & 121 deletions
This file was deleted.
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
/**
2+
*
3+
* Licensed to the Apache Software Foundation (ASF) under one or more
4+
* contributor license agreements. See the NOTICE file distributed with
5+
* this work for additional information regarding copyright ownership.
6+
* The ASF licenses this file to You under the Apache License, Version 2.0
7+
* (the "License"); you may not use this file except in compliance with
8+
* the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing, software
13+
* distributed under the License is distributed on an "AS IS" BASIS,
14+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
* See the License for the specific language governing permissions and
16+
* limitations under the License.
17+
*/
18+
#pragma once
19+
20+
#include <memory>
21+
#include <string>
22+
#include <utility>
23+
#include <vector>
24+
25+
#include "minifi-cpp/properties/Configure.h"
26+
#include "core/Core.h"
27+
#include "core/ConfigurableComponentImpl.h"
28+
#include "core/Connectable.h"
29+
#include "minifi-cpp/core/controller/ControllerServiceApi.h"
30+
#include "minifi-cpp/core/ControllerServiceApiDefinition.h"
31+
#include "minifi-cpp/core/controller/ControllerServiceMetadata.h"
32+
33+
namespace org::apache::nifi::minifi::core::controller {
34+
35+
/**
36+
* Controller Service base class that contains some pure virtual methods.
37+
*
38+
* Design: OnEnable is executed when the controller service is being enabled.
39+
* Note that keeping state here must be protected in this function.
40+
*/
41+
class ControllerServiceBase : public virtual ControllerServiceApi {
42+
public:
43+
explicit ControllerServiceBase(ControllerServiceMetadata metadata)
44+
: name_(std::move(metadata.name)),
45+
uuid_(metadata.uuid),
46+
logger_(std::move(metadata.logger)) {}
47+
48+
virtual void initialize() {}
49+
50+
void initialize(ControllerServiceDescriptor& descriptor) final {
51+
gsl_Expects(!descriptor_);
52+
descriptor_ = &descriptor;
53+
auto guard = gsl::finally([&] {descriptor_ = nullptr;});
54+
initialize();
55+
}
56+
57+
void setSupportedProperties(std::span<const PropertyReference> properties) {
58+
gsl_Expects(descriptor_);
59+
descriptor_->setSupportedProperties(properties);
60+
}
61+
62+
~ControllerServiceBase() override {}
63+
64+
virtual void onEnable() {}
65+
66+
/**
67+
* Function is called when Controller Services are enabled and being run
68+
*/
69+
void onEnable(ControllerServiceContext& context, const std::shared_ptr<Configure>& configuration, const std::vector<std::shared_ptr<ControllerServiceInterface>>& linked_services) final {
70+
configuration_ = configuration;
71+
linked_services_ = linked_services;
72+
gsl_Expects(!context_);
73+
context_ = &context;
74+
auto guard = gsl::finally([&] {context_ = nullptr;});
75+
onEnable();
76+
}
77+
78+
[[nodiscard]] nonstd::expected<std::string, std::error_code> getProperty(std::string_view name) const {
79+
gsl_Expects(context_);
80+
return context_->getProperty(name);
81+
}
82+
83+
[[nodiscard]] nonstd::expected<std::vector<std::string>, std::error_code> getAllPropertyValues(std::string_view name) const {
84+
gsl_Expects(context_);
85+
return context_->getAllPropertyValues(name);
86+
}
87+
88+
/**
89+
* Function is called when Controller Services are disabled
90+
*/
91+
void notifyStop() override {}
92+
93+
std::string getName() const {
94+
return name_;
95+
}
96+
97+
utils::Identifier getUUID() const {
98+
return uuid_;
99+
}
100+
101+
102+
static constexpr auto ImplementsApis = std::array<ControllerServiceApiDefinition, 0>{};
103+
104+
protected:
105+
std::string name_;
106+
utils::Identifier uuid_;
107+
std::vector<std::shared_ptr<controller::ControllerServiceInterface> > linked_services_;
108+
std::shared_ptr<Configure> configuration_;
109+
ControllerServiceDescriptor* descriptor_{nullptr};
110+
ControllerServiceContext* context_{nullptr};
111+
112+
std::shared_ptr<core::logging::Logger> logger_;
113+
};
114+
115+
} // namespace org::apache::nifi::minifi::core::controller
Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/**
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
#pragma once
19+
20+
#include <string>
21+
#include <memory>
22+
#include <utility>
23+
#include "core/ClassName.h"
24+
#include "minifi-cpp/core/controller/ControllerServiceFactory.h"
25+
26+
namespace org::apache::nifi::minifi::core::controller {
27+
28+
template<class T>
29+
class ControllerServiceFactoryImpl : public ControllerServiceFactory {
30+
public:
31+
ControllerServiceFactoryImpl()
32+
: class_name_(core::className<T>()) {
33+
}
34+
35+
explicit ControllerServiceFactoryImpl(std::string group_name)
36+
: group_name_(std::move(group_name)),
37+
class_name_(core::className<T>()) {
38+
}
39+
40+
std::string getGroupName() const override {
41+
return group_name_;
42+
}
43+
44+
std::unique_ptr<ControllerServiceApi> create(ControllerServiceMetadata metadata) override {
45+
return std::make_unique<T>(metadata);
46+
}
47+
48+
std::string getClassName() const override {
49+
return std::string{class_name_};
50+
}
51+
52+
protected:
53+
std::string group_name_;
54+
std::string_view class_name_;
55+
};
56+
57+
} // namespace org::apache::nifi::minifi::core::controller

core-framework/include/utils/ThreadPool.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@
3838
#include "Monitors.h"
3939
#include "core/expect.h"
4040
#include "minifi-cpp/controllers/ThreadManagementService.h"
41-
#include "minifi-cpp/core/controller/ControllerServiceLookup.h"
4241
#include "minifi-cpp/core/logging/Logger.h"
4342

4443
namespace org::apache::nifi::minifi::utils {
@@ -137,8 +136,10 @@ class WorkerThread {
137136
*/
138137
class ThreadPool {
139138
public:
139+
using ControllerServiceProvider = std::function<std::shared_ptr<core::controller::ControllerServiceInterface>(std::string_view)>;
140+
140141
ThreadPool(int max_worker_threads = 2,
141-
core::controller::ControllerServiceLookup* controller_service_provider = nullptr, std::string name = "NamelessPool");
142+
ControllerServiceProvider controller_service_provider = nullptr, std::string name = "NamelessPool");
142143

143144
ThreadPool(const ThreadPool &other) = delete;
144145
ThreadPool& operator=(const ThreadPool &other) = delete;
@@ -231,7 +232,7 @@ class ThreadPool {
231232
start();
232233
}
233234

234-
void setControllerServiceProvider(core::controller::ControllerServiceLookup* controller_service_provider) {
235+
void setControllerServiceProvider(ControllerServiceProvider controller_service_provider) {
235236
std::lock_guard<std::recursive_mutex> lock(manager_mutex_);
236237
bool was_running = running_;
237238
if (was_running) {
@@ -272,7 +273,7 @@ class ThreadPool {
272273
std::thread manager_thread_;
273274
std::thread delayed_scheduler_thread_;
274275
std::atomic<bool> running_;
275-
core::controller::ControllerServiceLookup* controller_service_provider_;
276+
ControllerServiceProvider controller_service_provider_;
276277
std::shared_ptr<controllers::ThreadManagementService> thread_manager_;
277278
ConcurrentQueue<std::shared_ptr<WorkerThread>> deceased_thread_queue_;
278279
ConditionConcurrentQueue<Worker> worker_queue_;

0 commit comments

Comments
 (0)