You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nifi.apache.org by ph...@apache.org on 2017/11/03 16:07:04 UTC

nifi-minifi-cpp git commit: MINIFICPP-273 Simplify configuration of MiNiFi - C++ by allowing default values for scheduling strategy and period.

Repository: nifi-minifi-cpp
Updated Branches:
  refs/heads/master 6527fac36 -> 98882882f


MINIFICPP-273 Simplify configuration of MiNiFi - C++ by allowing default values for scheduling strategy and period.

This closes #162.

Signed-off-by: Marc Parisi <ph...@apache.org>


Project: http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/repo
Commit: http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/commit/98882882
Tree: http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/tree/98882882
Diff: http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/diff/98882882

Branch: refs/heads/master
Commit: 98882882fe588975a560ba2e11f6569f042fd4ff
Parents: 6527fac
Author: Andy I. Christianson <an...@andyic.org>
Authored: Wed Oct 25 16:35:04 2017 -0400
Committer: Marc Parisi <ph...@apache.org>
Committed: Fri Nov 3 12:01:09 2017 -0400

----------------------------------------------------------------------
 libminifi/include/core/yaml/YamlConfiguration.h | 20 +++++++++
 libminifi/src/core/yaml/YamlConfiguration.cpp   | 45 +++++++++++++++++---
 2 files changed, 59 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/98882882/libminifi/include/core/yaml/YamlConfiguration.h
----------------------------------------------------------------------
diff --git a/libminifi/include/core/yaml/YamlConfiguration.h b/libminifi/include/core/yaml/YamlConfiguration.h
index d595183..112725e 100644
--- a/libminifi/include/core/yaml/YamlConfiguration.h
+++ b/libminifi/include/core/yaml/YamlConfiguration.h
@@ -272,6 +272,26 @@ class YamlConfiguration : public FlowConfiguration {
    */
   void checkRequiredField(YAML::Node *yamlNode, const std::string &fieldName, const std::string &yamlSection = "", const std::string &errorMessage = "");
 
+  /**
+   * This is a helper function for getting an optional value, if it exists.
+   * If it does not exist, returns the provided default value.
+   *
+   * @param yamlNode     the YAML node to check
+   * @param fieldName    the optional field key
+   * @param defaultValue the default value to use if field is not set
+   * @param yamlSection  [optional] the top level section of the YAML config
+   *                       for the yamlNode. This is used fpr generating a
+   *                       useful info message for troubleshooting.
+   * @param infoMessage  [optional] the info message string to use if
+   *                       the optional field is missing. If not provided,
+   *                       a default info message will be generated.
+   */
+  YAML::Node getOptionalField(YAML::Node *yamlNode,
+                              const std::string &fieldName,
+                              const YAML::Node &defaultValue,
+                              const std::string &yamlSection = "",
+                              const std::string &infoMessage = "");
+
  protected:
   std::shared_ptr<io::StreamFactory> stream_factory_;
  private:

http://git-wip-us.apache.org/repos/asf/nifi-minifi-cpp/blob/98882882/libminifi/src/core/yaml/YamlConfiguration.cpp
----------------------------------------------------------------------
diff --git a/libminifi/src/core/yaml/YamlConfiguration.cpp b/libminifi/src/core/yaml/YamlConfiguration.cpp
index aab22f7..11b9d04 100644
--- a/libminifi/src/core/yaml/YamlConfiguration.cpp
+++ b/libminifi/src/core/yaml/YamlConfiguration.cpp
@@ -23,6 +23,7 @@
 #include <set>
 #include "core/reporting/SiteToSiteProvenanceReportingTask.h"
 #include "io/validation.h"
+
 namespace org {
 namespace apache {
 namespace nifi {
@@ -118,14 +119,18 @@ void YamlConfiguration::parseProcessorNodeYaml(YAML::Node processorsNode, core::
         }
         processor->setName(procCfg.name);
 
-        checkRequiredField(&procNode, "scheduling strategy",
-        CONFIG_YAML_PROCESSORS_KEY);
-        procCfg.schedulingStrategy = procNode["scheduling strategy"].as<std::string>();
+        auto strategyNode = getOptionalField(&procNode,
+                                             "scheduling strategy",
+                                             YAML::Node("EVENT_DRIVEN"),
+                                             CONFIG_YAML_PROCESSORS_KEY);
+        procCfg.schedulingStrategy = strategyNode.as<std::string>();
         logger_->log_debug("parseProcessorNode: scheduling strategy => [%s]", procCfg.schedulingStrategy);
 
-        checkRequiredField(&procNode, "scheduling period",
-        CONFIG_YAML_PROCESSORS_KEY);
-        procCfg.schedulingPeriod = procNode["scheduling period"].as<std::string>();
+        auto periodNode = getOptionalField(&procNode,
+                                           "scheduling period",
+                                           YAML::Node("1 sec"),
+                                           CONFIG_YAML_PROCESSORS_KEY);
+        procCfg.schedulingPeriod = periodNode.as<std::string>();
         logger_->log_debug("parseProcessorNode: scheduling period => [%s]", procCfg.schedulingPeriod);
 
         if (procNode["max concurrent tasks"]) {
@@ -698,6 +703,34 @@ void YamlConfiguration::checkRequiredField(YAML::Node *yamlNode, const std::stri
   }
 }
 
+YAML::Node YamlConfiguration::getOptionalField(YAML::Node *yamlNode,
+                                               const std::string &fieldName,
+                                               const YAML::Node &defaultValue,
+                                               const std::string &yamlSection,
+                                               const std::string &providedInfoMessage) {
+  std::string infoMessage = providedInfoMessage;
+  auto result = yamlNode->as<YAML::Node>()[fieldName];
+  if (!result) {
+    if (infoMessage.empty()) {
+      // Build a helpful info message for the user to inform them that a default is being used
+      infoMessage =
+          yamlNode->as<YAML::Node>()["name"] ?
+          "Using default value for optional field '" + fieldName + "' in component named '"
+              + yamlNode->as<YAML::Node>()["name"].as<std::string>() + "'" :
+          "Using default value for optional field '" + fieldName + "' ";
+      if (!yamlSection.empty()) {
+        infoMessage += " [in '" + yamlSection + "' section of configuration file]: ";
+      }
+
+      infoMessage += defaultValue.as<std::string>();
+    }
+    logger_->log_info(infoMessage.c_str());
+    result = defaultValue;
+  }
+
+  return result;
+}
+
 } /* namespace core */
 } /* namespace minifi */
 } /* namespace nifi */