Added a log tool to start a replica server.

Review: https://reviews.apache.org/r/17755


Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/e15bbe0f
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/e15bbe0f
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/e15bbe0f

Branch: refs/heads/master
Commit: e15bbe0fd9c46109b72e11f5e6eb06a5b67294c5
Parents: 36c7a4d
Author: Jie Yu <[email protected]>
Authored: Fri Feb 14 17:05:26 2014 -0800
Committer: Vinod Kone <[email protected]>
Committed: Fri Feb 14 17:05:26 2014 -0800

----------------------------------------------------------------------
 src/Makefile.am          |   4 +-
 src/log/main.cpp         |   2 +
 src/log/tool/replica.cpp | 150 ++++++++++++++++++++++++++++++++++++++++++
 src/log/tool/replica.hpp |  66 +++++++++++++++++++
 4 files changed, 221 insertions(+), 1 deletion(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/mesos/blob/e15bbe0f/src/Makefile.am
----------------------------------------------------------------------
diff --git a/src/Makefile.am b/src/Makefile.am
index 9a33f21..dc9042a 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -312,7 +312,8 @@ liblog_la_SOURCES =                                         
        \
   log/recover.cpp                                                      \
   log/replica.cpp                                                      \
   log/tool/initialize.cpp                                              \
-  log/tool/read.cpp
+  log/tool/read.cpp                                                    \
+  log/tool/replica.cpp
 liblog_la_SOURCES +=                                                   \
   log/catchup.hpp                                                      \
   log/consensus.hpp                                                    \
@@ -326,6 +327,7 @@ liblog_la_SOURCES +=                                        
                \
   log/tool.hpp                                                         \
   log/tool/initialize.hpp                                              \
   log/tool/read.hpp                                                    \
+  log/tool/replica.hpp                                                 \
   messages/log.hpp                                                     \
   messages/log.proto
 nodist_liblog_la_SOURCES = $(LOG_PROTOS)

http://git-wip-us.apache.org/repos/asf/mesos/blob/e15bbe0f/src/log/main.cpp
----------------------------------------------------------------------
diff --git a/src/log/main.cpp b/src/log/main.cpp
index c37dd6f..2b30fd0 100644
--- a/src/log/main.cpp
+++ b/src/log/main.cpp
@@ -29,6 +29,7 @@
 #include "log/tool.hpp"
 #include "log/tool/initialize.hpp"
 #include "log/tool/read.hpp"
+#include "log/tool/replica.hpp"
 
 using namespace mesos;
 using namespace mesos::internal;
@@ -69,6 +70,7 @@ int main(int argc, char** argv)
   // Register log tools.
   add(Owned<tool::Tool>(new tool::Initialize()));
   add(Owned<tool::Tool>(new tool::Read()));
+  add(Owned<tool::Tool>(new tool::Replica()));
 
   if (argc < 2) {
     usage(argv[0]);

http://git-wip-us.apache.org/repos/asf/mesos/blob/e15bbe0f/src/log/tool/replica.cpp
----------------------------------------------------------------------
diff --git a/src/log/tool/replica.cpp b/src/log/tool/replica.cpp
new file mode 100644
index 0000000..3985fc7
--- /dev/null
+++ b/src/log/tool/replica.cpp
@@ -0,0 +1,150 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include <iostream>
+#include <sstream>
+
+#include <process/future.hpp>
+#include <process/process.hpp>
+
+#include <stout/error.hpp>
+
+#include "log/log.hpp"
+#include "log/tool/initialize.hpp"
+#include "log/tool/replica.hpp"
+
+#include "logging/logging.hpp"
+
+using namespace process;
+
+using std::endl;
+using std::ostringstream;
+using std::string;
+
+namespace mesos {
+namespace internal {
+namespace log {
+namespace tool {
+
+Replica::Flags::Flags()
+{
+  add(&Flags::quorum,
+      "quorum",
+      "Quorum size");
+
+  add(&Flags::path,
+      "path",
+      "Path to the log");
+
+  add(&Flags::servers,
+      "servers",
+      "ZooKeeper servers");
+
+  add(&Flags::znode,
+      "znode",
+      "ZooKeeper znode");
+
+  add(&Flags::initialize,
+      "initialize",
+      "Whether to initialize the log",
+      true);
+
+  add(&Flags::help,
+      "help",
+      "Prints the help message",
+      false);
+}
+
+
+string Replica::usage(const string& argv0) const
+{
+  ostringstream out;
+
+  out << "Usage: " << argv0 << " " << name() << " [OPTIONS]" << endl
+      << endl
+      << "This command is used to start a replica server" << endl
+      << endl
+      << "Supported OPTIONS:" << endl
+      << flags.usage();
+
+  return out.str();
+}
+
+
+Try<Nothing> Replica::execute(int argc, char** argv)
+{
+  // Configure the tool by parsing command line arguments.
+  if (argc > 0 && argv != NULL) {
+    Try<Nothing> load = flags.load(None(), argc, argv);
+    if (load.isError()) {
+      return Error(load.error() + "\n\n" + usage(argv[0]));
+    }
+
+    if (flags.help) {
+      return Error(usage(argv[0]));
+    }
+
+    process::initialize();
+    logging::initialize(argv[0], flags);
+  }
+
+  if (flags.quorum.isNone()) {
+    return Error("Missing flag '--quorum'");
+  }
+
+  if (flags.path.isNone()) {
+    return Error("Missing flag '--path'");
+  }
+
+  if (flags.servers.isNone()) {
+    return Error("Missing flag '--servers'");
+  }
+
+  if (flags.znode.isNone()) {
+    return Error("Missing flag '--znode'");
+  }
+
+  // Initialize the log.
+  if (flags.initialize) {
+    Initialize initialize;
+    initialize.flags.path = flags.path;
+
+    Try<Nothing> execution = initialize.execute();
+    if (execution.isError()) {
+      return Error(execution.error());
+    }
+  }
+
+  // Create the log.
+  Log log(
+      flags.quorum.get(),
+      flags.path.get(),
+      flags.servers.get(),
+      Seconds(10),
+      flags.znode.get());
+
+  // Loop forever.
+  Future<Nothing>().get();
+
+  return Nothing();
+}
+
+} // namespace tool {
+} // namespace log {
+} // namespace internal {
+} // namespace mesos {

http://git-wip-us.apache.org/repos/asf/mesos/blob/e15bbe0f/src/log/tool/replica.hpp
----------------------------------------------------------------------
diff --git a/src/log/tool/replica.hpp b/src/log/tool/replica.hpp
new file mode 100644
index 0000000..b433348
--- /dev/null
+++ b/src/log/tool/replica.hpp
@@ -0,0 +1,66 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __LOG_TOOL_REPLICA_HPP__
+#define __LOG_TOOL_REPLICA_HPP__
+
+#include <stout/flags.hpp>
+#include <stout/option.hpp>
+
+#include "log/tool.hpp"
+
+#include "logging/flags.hpp"
+
+namespace mesos {
+namespace internal {
+namespace log {
+namespace tool {
+
+// Start a replica server.
+class Replica : public Tool
+{
+public:
+  class Flags : public logging::Flags
+  {
+  public:
+    Flags();
+
+    Option<size_t> quorum;
+    Option<std::string> path;
+    Option<std::string> servers;
+    Option<std::string> znode;
+    bool initialize;
+    bool help;
+  };
+
+  virtual std::string name() const { return "replica"; }
+  virtual Try<Nothing> execute(int argc = 0, char** argv = NULL);
+
+  // Users can change the default configuration by setting this flags.
+  Flags flags;
+
+private:
+  std::string usage(const std::string& argv0) const;
+};
+
+} // namespace tool {
+} // namespace log {
+} // namespace internal {
+} // namespace mesos {
+
+#endif // __LOG_TOOL_REPLICA_HPP__

Reply via email to