You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by be...@apache.org on 2011/06/05 07:45:46 UTC

svn commit: r1131893 - in /incubator/mesos/trunk/src: configurator.cpp configurator.hpp local.cpp master_main.cpp option.hpp slave_main.cpp tests/test_configurator.cpp

Author: benh
Date: Sun Jun  5 05:45:45 2011
New Revision: 1131893

URL: http://svn.apache.org/viewvc?rev=1131893&view=rev
Log:
Updated nexus-local to use new config framework. Also replaced
BadOptionValueException with ConfigurationException to make error
reporting slightly simpler.

Modified:
    incubator/mesos/trunk/src/configurator.cpp
    incubator/mesos/trunk/src/configurator.hpp
    incubator/mesos/trunk/src/local.cpp
    incubator/mesos/trunk/src/master_main.cpp
    incubator/mesos/trunk/src/option.hpp
    incubator/mesos/trunk/src/slave_main.cpp
    incubator/mesos/trunk/src/tests/test_configurator.cpp

Modified: incubator/mesos/trunk/src/configurator.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/configurator.cpp?rev=1131893&r1=1131892&r2=1131893&view=diff
==============================================================================
--- incubator/mesos/trunk/src/configurator.cpp (original)
+++ incubator/mesos/trunk/src/configurator.cpp Sun Jun  5 05:45:45 2011
@@ -24,8 +24,10 @@ const char* Configurator::ENV_VAR_PREFIX
 void Configurator::validate()
 {
   foreachpair (const string& key, const Option& opt, options) {
-    if (params.contains(key) && opt.validator && !opt.validator->isValid(params[key])) {
-      throw BadOptionValueException(params[key].c_str());
+    if (params.contains(key) && opt.validator &&
+        !opt.validator->isValid(params[key])) {
+      string msg = "Invalid value for '" + key + "' option: " + params[key];
+      throw ConfigurationException(msg.c_str());
     }
   }
 }

Modified: incubator/mesos/trunk/src/configurator.hpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/configurator.hpp?rev=1131893&r1=1131892&r2=1131893&view=diff
==============================================================================
--- incubator/mesos/trunk/src/configurator.hpp (original)
+++ incubator/mesos/trunk/src/configurator.hpp Sun Jun  5 05:45:45 2011
@@ -191,8 +191,7 @@ public:
 
   /**
    * Validates the values of all keys that it has a default option for.
-   * @throws BadOptionValueException with the key of the parameter 
-   * that has the wrong type.
+   * @throws ConfigurationError if a key has the wrong type.
    **/ 
   void validate();
 

Modified: incubator/mesos/trunk/src/local.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/local.cpp?rev=1131893&r1=1131892&r2=1131893&view=diff
==============================================================================
--- incubator/mesos/trunk/src/local.cpp (original)
+++ incubator/mesos/trunk/src/local.cpp Sun Jun  5 05:45:45 2011
@@ -3,6 +3,8 @@
 #include <iostream>
 #include <string>
 
+#include "configurator.hpp"
+#include "logging.hpp"
 #include "nexus_local.hpp"
 
 using std::cerr;
@@ -10,59 +12,57 @@ using std::endl;
 using std::string;
 
 using namespace nexus::internal;
+using nexus::internal::master::Master;
+using nexus::internal::slave::Slave;
+
+
+void usage(const char* programName, const Configurator& conf)
+{
+  cerr << "Usage: " << programName
+       << " [--port=PORT] [--slaves=NUM] [--cpus=NUM] [--mem=NUM] [...]" << endl
+       << endl
+       << "Supported options:" << endl
+       << conf.getUsage();
+}
 
 
 int main (int argc, char **argv)
 {
+  Configurator conf;
+  conf.addOption<int>("port", 'p', "Port to listen on", 50010);
+  conf.addOption<int>("slaves", 's', "Number of slaves", 1);
+  conf.addOption<int32_t>("cpus", 'c', "CPU cores for tasks per slave", 1);
+  conf.addOption<int64_t>("mem", 'm', "Memory for tasks per slave, in bytes\n",
+                          1 * Gigabyte);
+  Logging::registerOptions(&conf);
+  Master::registerOptions(&conf);
+  Slave::registerOptions(&conf);
+
   if (argc == 2 && string("--help") == argv[1]) {
-    cerr << "Usage: " << argv[0]
-         << " [--port PORT] [--slaves NUM] [--cpus NUM] [--mem NUM] [--quiet]"
-         << endl;
+    usage(argv[0], conf);
     exit(1);
   }
 
-  int slaves = 1;
-  int32_t cpus = 1;
-  int64_t mem = 1073741824;
-  bool quiet = false;
-
-  option options[] = {
-    {"slaves", required_argument, 0, 's'},
-    {"cpus", required_argument, 0, 'c'},
-    {"mem", required_argument, 0, 'm'},
-    {"port", required_argument, 0, 'p'},
-    {"quiet", no_argument, 0, 'q'},
-  };
-
-  int opt;
-  int index;
-  while ((opt = getopt_long(argc, argv, "s:c:m:p:q", options, &index)) != -1) {
-    switch (opt) {
-      case 's':
-        slaves = atoi(optarg);
-        break;
-      case 'c':
-        cpus = atoi(optarg);
-        break;
-      case 'm':
-        mem = atoll(optarg);
-        break;
-      case 'p':
-        setenv("LIBPROCESS_PORT", optarg, true);
-        break;
-      case 'q':
-        quiet = true;
-        break;
-      case '?':
-        // Error parsing options; getopt prints an error message, so just exit
-        exit(1);
-        break;
-      default:
-        break;
-    }
+  Params params;
+  try {
+    conf.load(argc, argv, true);
+    params = conf.getParams();
+  } catch (ConfigurationException& e) {
+    cerr << "Configuration error: " << e.what() << endl;
+    exit(1);
   }
 
-  const PID &master = local::launch(slaves, cpus, mem, true, quiet);
+  Logging::init(argv[0], params);
+
+  if (params.contains("port"))
+    setenv("LIBPROCESS_PORT", params["port"].c_str(), 1);
+
+  int slaves = params.get<int>("slaves", 1);
+  int32_t cpus = params.get<int32_t>("cpus", 1);
+  int64_t mem = params.get<int64_t>("mem", 1 * Gigabyte);
+  bool quiet = Logging::isQuiet(params);
+
+  const PID &master = local::launch(slaves, cpus, mem, false, quiet);
 
   Process::wait(master);
 

Modified: incubator/mesos/trunk/src/master_main.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/master_main.cpp?rev=1131893&r1=1131892&r2=1131893&view=diff
==============================================================================
--- incubator/mesos/trunk/src/master_main.cpp (original)
+++ incubator/mesos/trunk/src/master_main.cpp Sun Jun  5 05:45:45 2011
@@ -47,9 +47,6 @@ int main(int argc, char **argv)
   try {
     conf.load(argc, argv, true);
     params = conf.getParams();
-  } catch (BadOptionValueException& e) {
-    cerr << "Invalid value for '" << e.what() << "' option" << endl;
-    exit(1);
   } catch (ConfigurationException& e) {
     cerr << "Configuration error: " << e.what() << endl;
     exit(1);

Modified: incubator/mesos/trunk/src/option.hpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/option.hpp?rev=1131893&r1=1131892&r2=1131893&view=diff
==============================================================================
--- incubator/mesos/trunk/src/option.hpp (original)
+++ incubator/mesos/trunk/src/option.hpp Sun Jun  5 05:45:45 2011
@@ -31,6 +31,7 @@ public:
   virtual ValidatorBase* clone() const = 0;
 };
 
+
 /**
  * Validator that checks if a string can be cast to its templated type.
  **/
@@ -62,16 +63,6 @@ public:
 
 };
 
-/**
- * Exception type thrown if the the value of an Option 
- * doesn't match the default value type.
- */
-struct BadOptionValueException : std::exception
-{
-  const char* message;
-  BadOptionValueException(const char* msg): message(msg) {}
-  const char* what() const throw () { return message; }
-};
 
 /**
  * Registered option with help string and default value

Modified: incubator/mesos/trunk/src/slave_main.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/slave_main.cpp?rev=1131893&r1=1131892&r2=1131893&view=diff
==============================================================================
--- incubator/mesos/trunk/src/slave_main.cpp (original)
+++ incubator/mesos/trunk/src/slave_main.cpp Sun Jun  5 05:45:45 2011
@@ -52,9 +52,6 @@ int main(int argc, char **argv)
   try {
     conf.load(argc, argv, true);
     params = conf.getParams();
-  } catch (BadOptionValueException& e) {
-    cerr << "Invalid value for '" << e.what() << "' option" << endl;
-    exit(1);
   } catch (ConfigurationException& e) {
     cerr << "Configuration error: " << e.what() << endl;
     exit(1);

Modified: incubator/mesos/trunk/src/tests/test_configurator.cpp
URL: http://svn.apache.org/viewvc/incubator/mesos/trunk/src/tests/test_configurator.cpp?rev=1131893&r1=1131892&r2=1131893&view=diff
==============================================================================
--- incubator/mesos/trunk/src/tests/test_configurator.cpp (original)
+++ incubator/mesos/trunk/src/tests/test_configurator.cpp Sun Jun  5 05:45:45 2011
@@ -51,7 +51,7 @@ TEST(ConfiguratorTest, DefaultOptions)
     } );
 
   conf.addOption<int>("excp", "Exception tester.", 50);
-  EXPECT_THROW(conf.validate(), BadOptionValueException);
+  EXPECT_THROW(conf.validate(), ConfigurationException);
   conf.getParams()["excp"] = "27";
   EXPECT_NO_THROW(conf.validate());