Skip to content

Commit fa362fa

Browse files
adamdebrecenimartinzink
authored andcommitted
MINIFICPP-2669 - Reduce controller service api
Closes #2065 Signed-off-by: Martin Zink <martinzink@apache.org>
1 parent 2ac1f1b commit fa362fa

File tree

123 files changed

+1020
-1298
lines changed

Some content is hidden

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

123 files changed

+1020
-1298
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
#include "minifi-cpp/agent/agent_version.h"
3536

@@ -62,6 +63,11 @@ class StaticClassType {
6263
auto factory = std::unique_ptr<ProcessorFactory>(new ProcessorFactoryImpl<Class>(module_name));
6364
getClassLoader().registerClass(construction_name, std::move(factory));
6465
}
66+
} else if constexpr (Type == ResourceType::ControllerService) {
67+
for (const auto& construction_name : construction_names_) {
68+
auto factory = std::unique_ptr<controller::ControllerServiceFactory>(new controller::ControllerServiceFactoryImpl<Class>(module_name));
69+
getClassLoader().registerClass(construction_name, std::move(factory));
70+
}
6571
} else {
6672
for (const auto& construction_name : construction_names_) {
6773
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: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
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/controller/ControllerServiceInterface.h"
31+
#include "minifi-cpp/core/ControllerServiceApiDefinition.h"
32+
#include "minifi-cpp/core/controller/ControllerServiceMetadata.h"
33+
34+
namespace org::apache::nifi::minifi::core::controller {
35+
36+
// A base class that helps with controller service development, contains common functionalities.
37+
class ControllerServiceBase : public ControllerServiceApi {
38+
public:
39+
explicit ControllerServiceBase(ControllerServiceMetadata metadata)
40+
: name_(std::move(metadata.name)),
41+
uuid_(metadata.uuid),
42+
logger_(std::move(metadata.logger)) {}
43+
44+
virtual void initialize() {}
45+
46+
void initialize(ControllerServiceDescriptor& descriptor) final {
47+
gsl_Expects(!descriptor_);
48+
descriptor_ = &descriptor;
49+
auto guard = gsl::finally([&] {descriptor_ = nullptr;});
50+
initialize();
51+
}
52+
53+
void setSupportedProperties(std::span<const PropertyReference> properties) {
54+
gsl_Expects(descriptor_);
55+
descriptor_->setSupportedProperties(properties);
56+
}
57+
58+
~ControllerServiceBase() override {}
59+
60+
virtual void onEnable() {}
61+
62+
/**
63+
* Function is called when Controller Services are enabled and being run
64+
*/
65+
void onEnable(ControllerServiceContext& context, const std::shared_ptr<Configure>& configuration, const std::vector<std::shared_ptr<ControllerServiceInterface>>& linked_services) final {
66+
configuration_ = configuration;
67+
linked_services_ = linked_services;
68+
gsl_Expects(!context_);
69+
context_ = &context;
70+
auto guard = gsl::finally([&] {context_ = nullptr;});
71+
onEnable();
72+
}
73+
74+
[[nodiscard]] nonstd::expected<std::string, std::error_code> getProperty(std::string_view name) const {
75+
gsl_Expects(context_);
76+
return context_->getProperty(name);
77+
}
78+
79+
[[nodiscard]] nonstd::expected<std::vector<std::string>, std::error_code> getAllPropertyValues(std::string_view name) const {
80+
gsl_Expects(context_);
81+
return context_->getAllPropertyValues(name);
82+
}
83+
84+
/**
85+
* Function is called when Controller Services are disabled
86+
*/
87+
void notifyStop() override {}
88+
89+
std::string getName() const {
90+
return name_;
91+
}
92+
93+
utils::Identifier getUUID() const {
94+
return uuid_;
95+
}
96+
97+
98+
static constexpr auto ImplementsApis = std::array<ControllerServiceApiDefinition, 0>{};
99+
100+
protected:
101+
std::string name_;
102+
utils::Identifier uuid_;
103+
std::vector<std::shared_ptr<controller::ControllerServiceInterface> > linked_services_;
104+
std::shared_ptr<Configure> configuration_;
105+
// valid during initialize, sink for supported properties
106+
ControllerServiceDescriptor* descriptor_{nullptr};
107+
// valid during onEnable, provides property access
108+
ControllerServiceContext* context_{nullptr};
109+
110+
std::shared_ptr<core::logging::Logger> logger_;
111+
};
112+
113+
} // 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: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@
3737
#include "MinifiConcurrentQueue.h"
3838
#include "Monitors.h"
3939
#include "core/expect.h"
40-
#include "minifi-cpp/controllers/ThreadManagementService.h"
41-
#include "minifi-cpp/core/controller/ControllerServiceLookup.h"
4240
#include "minifi-cpp/core/logging/Logger.h"
4341

4442
namespace org::apache::nifi::minifi::utils {
@@ -137,8 +135,7 @@ class WorkerThread {
137135
*/
138136
class ThreadPool {
139137
public:
140-
ThreadPool(int max_worker_threads = 2,
141-
core::controller::ControllerServiceLookup* controller_service_provider = nullptr, std::string name = "NamelessPool");
138+
explicit ThreadPool(int max_worker_threads = 2, std::string name = "NamelessPool");
142139

143140
ThreadPool(const ThreadPool &other) = delete;
144141
ThreadPool& operator=(const ThreadPool &other) = delete;
@@ -231,20 +228,6 @@ class ThreadPool {
231228
start();
232229
}
233230

234-
void setControllerServiceProvider(core::controller::ControllerServiceLookup* controller_service_provider) {
235-
std::lock_guard<std::recursive_mutex> lock(manager_mutex_);
236-
bool was_running = running_;
237-
if (was_running) {
238-
shutdown();
239-
}
240-
controller_service_provider_ = controller_service_provider;
241-
if (was_running)
242-
start();
243-
}
244-
245-
private:
246-
std::shared_ptr<controllers::ThreadManagementService> createThreadManager() const;
247-
248231
protected:
249232
std::thread createThread(std::function<void()> &&functor) {
250233
return std::thread([ functor ]() mutable {
@@ -272,8 +255,6 @@ class ThreadPool {
272255
std::thread manager_thread_;
273256
std::thread delayed_scheduler_thread_;
274257
std::atomic<bool> running_;
275-
core::controller::ControllerServiceLookup* controller_service_provider_;
276-
std::shared_ptr<controllers::ThreadManagementService> thread_manager_;
277258
ConcurrentQueue<std::shared_ptr<WorkerThread>> deceased_thread_queue_;
278259
ConditionConcurrentQueue<Worker> worker_queue_;
279260
std::priority_queue<Worker, std::vector<Worker>, DelayedTaskComparator> delayed_worker_queue_;

0 commit comments

Comments
 (0)