Skip to content

Commit 3c83d84

Browse files
martinzinkszaszm
authored andcommitted
MINIFICPP-2700 Terminate if ContentRepository creation failed
Closes apache#2083 Signed-off-by: Marton Szasz <[email protected]>
1 parent d4dab97 commit 3c83d84

File tree

4 files changed

+31
-43
lines changed

4 files changed

+31
-43
lines changed

extensions/prometheus/tests/PrometheusMetricsPublisherTest.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ class PrometheusPublisherTestFixture {
5252
: configuration_(std::make_shared<ConfigureImpl>()),
5353
provenance_repo_(core::createRepository("provenancerepository")),
5454
flow_file_repo_(core::createRepository("flowfilerepository")),
55-
content_repo_(core::createContentRepository("volatilecontentrepository")),
55+
content_repo_(core::createContentRepository("volatilecontentrepository", "", *core::logging::LoggerFactory<PrometheusPublisherTestFixture>::getLogger())),
5656
response_node_loader_(std::make_shared<state::response::ResponseNodeLoaderImpl>(configuration_,
5757
std::vector<std::shared_ptr<core::RepositoryMetricsSource>>{provenance_repo_, flow_file_repo_, content_repo_}, nullptr)) {
5858
std::unique_ptr<DummyMetricsExposer> dummy_exposer;

libminifi/include/core/RepositoryFactory.h

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -27,20 +27,10 @@
2727

2828
namespace org::apache::nifi::minifi::core {
2929

30-
/**
31-
* Create a context repository
32-
* @param configuration_class_name configuration class name
33-
* @param fail_safe determines whether or not to make the default class if configuration_class_name is invalid
34-
* @param repo_name name of the repository
35-
*/
36-
std::unique_ptr<core::ContentRepository> createContentRepository(const std::string& configuration_class_name, bool fail_safe = false, const std::string& repo_name = "");
30+
std::unique_ptr<core::ContentRepository> createContentRepository(const std::string& configuration_class_name,
31+
const std::string& repo_name,
32+
logging::Logger& logger);
3733

38-
/**
39-
* Create a repository represented by the configuration class name
40-
* @param configuration_class_name configuration class name
41-
* @param fail_safe determines whether or not to make the default class if configuration_class_name is invalid
42-
* @param repo_name name of the repository
43-
*/
4434
std::unique_ptr<core::Repository> createRepository(const std::string& configuration_class_name, const std::string& repo_name = "");
4535

4636
} // namespace org::apache::nifi::minifi::core

libminifi/src/core/RepositoryFactory.cpp

Lines changed: 26 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -23,53 +23,51 @@
2323
#include "core/repository/FileSystemRepository.h"
2424
#include "core/repository/VolatileProvenanceRepository.h"
2525
#include "core/repository/NoOpThreadedRepository.h"
26+
#include "range/v3/algorithm/transform.hpp"
2627

2728
using namespace std::literals::chrono_literals;
2829

2930
namespace org::apache::nifi::minifi::core {
3031

31-
std::unique_ptr<core::ContentRepository> createContentRepository(const std::string& configuration_class_name, bool fail_safe, const std::string& repo_name) {
32+
std::unique_ptr<ContentRepository> createContentRepository(const std::string& configuration_class_name,
33+
const std::string& repo_name,
34+
logging::Logger& logger) {
3235
std::string class_name_lc = configuration_class_name;
33-
std::transform(class_name_lc.begin(), class_name_lc.end(), class_name_lc.begin(), ::tolower);
34-
try {
35-
auto return_obj = core::ClassLoader::getDefaultClassLoader().instantiate<core::ContentRepository>(class_name_lc,
36-
class_name_lc);
37-
if (return_obj) {
38-
return_obj->setName(repo_name);
39-
return return_obj;
40-
}
41-
if (class_name_lc == "volatilecontentrepository") {
42-
return std::make_unique<core::repository::VolatileContentRepository>(repo_name);
43-
} else if (class_name_lc == "filesystemrepository") {
44-
return std::make_unique<core::repository::FileSystemRepository>(repo_name);
45-
}
46-
if (fail_safe) {
47-
return std::make_unique<core::repository::VolatileContentRepository>("fail_safe");
48-
} else {
49-
throw std::runtime_error("Support for the provided configuration class could not be found");
50-
}
51-
} catch (const std::runtime_error&) {
52-
if (fail_safe) {
53-
return std::make_unique<core::repository::VolatileContentRepository>("fail_safe");
54-
}
36+
ranges::transform(class_name_lc, class_name_lc.begin(), ::tolower);
37+
38+
if (auto return_obj = ClassLoader::getDefaultClassLoader().instantiate<ContentRepository>(class_name_lc, class_name_lc)) {
39+
return_obj->setName(repo_name);
40+
return return_obj;
41+
}
42+
43+
if (class_name_lc == "volatilecontentrepository") {
44+
return std::make_unique<repository::VolatileContentRepository>(repo_name);
45+
}
46+
if (class_name_lc == "filesystemrepository") {
47+
return std::make_unique<repository::FileSystemRepository>(repo_name);
5548
}
5649

57-
throw std::runtime_error("Support for the provided configuration class could not be found");
50+
logger.log_critical("Could not create the configured content repository ({})", configuration_class_name);
51+
if (class_name_lc == "databasecontentrepository") {
52+
logger.log_error("To use DatabaseContentRepository MiNiFi needs RocksDB extension, please check the extension path configured in minifi.properties");
53+
}
54+
55+
throw std::runtime_error("Support for the provided configuration class could not be found, check logs for more details");
5856
}
5957

60-
std::unique_ptr<core::Repository> createRepository(const std::string& configuration_class_name, const std::string& repo_name) {
58+
std::unique_ptr<Repository> createRepository(const std::string& configuration_class_name, const std::string& repo_name) {
6159
std::string class_name_lc = configuration_class_name;
6260
std::transform(class_name_lc.begin(), class_name_lc.end(), class_name_lc.begin(), ::tolower);
63-
auto return_obj = core::ClassLoader::getDefaultClassLoader().instantiate<core::ThreadedRepository>(class_name_lc,
64-
class_name_lc);
61+
auto return_obj = ClassLoader::getDefaultClassLoader().instantiate<ThreadedRepository>(class_name_lc, class_name_lc);
6562
if (return_obj) {
6663
return_obj->setName(repo_name);
6764
return return_obj;
6865
}
6966
// if the desired repos don't exist, we can try doing string matches and rely on volatile repositories
7067
if (class_name_lc == "flowfilerepository" || class_name_lc == "volatileflowfilerepository" || class_name_lc == "nooprepository") {
7168
return std::make_unique<repository::NoOpThreadedRepository>(repo_name);
72-
} else if (class_name_lc == "provenancerepository" || class_name_lc == "volatileprovenancerepository") {
69+
}
70+
if (class_name_lc == "provenancerepository" || class_name_lc == "volatileprovenancerepository") {
7371
return instantiate<repository::VolatileProvenanceRepository>(repo_name);
7472
}
7573
return {};

minifi_main/MiNiFiMain.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -352,7 +352,7 @@ int main(int argc, char **argv) {
352352

353353
configure->get(minifi::Configure::nifi_content_repository_class_name, content_repo_class);
354354

355-
std::shared_ptr<core::ContentRepository> content_repo = core::createContentRepository(content_repo_class, true, "content");
355+
std::shared_ptr<core::ContentRepository> content_repo = core::createContentRepository(content_repo_class, "content", *logger);
356356

357357
if (!content_repo->initialize(configure)) {
358358
logger->log_error("Content repository failed to initialize, exiting..");

0 commit comments

Comments
 (0)