You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by ka...@apache.org on 2016/04/18 14:22:28 UTC
[08/11] mesos git commit: Exposed zookeeper headers.
Exposed zookeeper headers.
Review: https://reviews.apache.org/r/46160
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/9be895cf
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/9be895cf
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/9be895cf
Branch: refs/heads/master
Commit: 9be895cfff6872a812f2b4c5c7506cb30ff208de
Parents: 9ac2dda
Author: Kapil Arya <ka...@mesosphere.io>
Authored: Tue Apr 12 15:32:23 2016 -0400
Committer: Kapil Arya <ka...@mesosphere.io>
Committed: Sun Apr 17 23:38:26 2016 -0400
----------------------------------------------------------------------
include/mesos/zookeeper/authentication.hpp | 65 ++++
include/mesos/zookeeper/contender.hpp | 81 +++++
include/mesos/zookeeper/detector.hpp | 67 ++++
include/mesos/zookeeper/group.hpp | 351 +++++++++++++++++++++
include/mesos/zookeeper/url.hpp | 122 +++++++
include/mesos/zookeeper/watcher.hpp | 93 ++++++
include/mesos/zookeeper/zookeeper.hpp | 335 ++++++++++++++++++++
src/Makefile.am | 20 +-
src/log/log.hpp | 4 +-
src/log/network.hpp | 4 +-
src/master/contender/contender.cpp | 5 +-
src/master/contender/zookeeper.cpp | 8 +-
src/master/contender/zookeeper.hpp | 6 +-
src/master/detector/detector.cpp | 6 +-
src/master/detector/zookeeper.cpp | 8 +-
src/master/detector/zookeeper.hpp | 6 +-
src/master/main.cpp | 4 +-
src/state/zookeeper.cpp | 8 +-
src/state/zookeeper.hpp | 4 +-
src/tests/cluster.cpp | 4 +-
src/tests/cluster.hpp | 4 +-
src/tests/group_tests.cpp | 6 +-
src/tests/master_contender_detector_tests.cpp | 4 +-
src/tests/zookeeper_test_server.hpp | 2 +-
src/tests/zookeeper_tests.cpp | 10 +-
src/tests/zookeeper_url_tests.cpp | 4 +-
src/zookeeper/authentication.cpp | 2 +-
src/zookeeper/authentication.hpp | 66 ----
src/zookeeper/contender.cpp | 8 +-
src/zookeeper/contender.hpp | 81 -----
src/zookeeper/detector.cpp | 6 +-
src/zookeeper/detector.hpp | 67 ----
src/zookeeper/group.cpp | 8 +-
src/zookeeper/group.hpp | 351 ---------------------
src/zookeeper/url.hpp | 122 -------
src/zookeeper/watcher.hpp | 93 ------
src/zookeeper/zookeeper.cpp | 4 +-
src/zookeeper/zookeeper.hpp | 335 --------------------
38 files changed, 1190 insertions(+), 1184 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/include/mesos/zookeeper/authentication.hpp
----------------------------------------------------------------------
diff --git a/include/mesos/zookeeper/authentication.hpp b/include/mesos/zookeeper/authentication.hpp
new file mode 100644
index 0000000..1c8855a
--- /dev/null
+++ b/include/mesos/zookeeper/authentication.hpp
@@ -0,0 +1,65 @@
+// 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 __ZOOKEEPER_AUTHENTICATION_HPP__
+#define __ZOOKEEPER_AUTHENTICATION_HPP__
+
+#include <zookeeper.h>
+
+#include <string>
+
+#include <glog/logging.h>
+
+namespace zookeeper {
+
+struct Authentication
+{
+ Authentication(
+ const std::string& _scheme,
+ const std::string& _credentials)
+ : scheme(_scheme),
+ credentials(_credentials)
+ {
+ // TODO(benh): Fix output operator below once this changes.
+ CHECK_EQ(scheme, "digest") << "Unsupported authentication scheme";
+ }
+
+ const std::string scheme;
+ const std::string credentials;
+};
+
+
+// An ACL that ensures we're the only authenticated user to mutate our
+// nodes - others are welcome to read.
+extern const ACL_vector EVERYONE_READ_CREATOR_ALL;
+
+// An ACL that allows others to create child nodes and read nodes, but
+// we're the only authenticated user to mutate our nodes.
+extern const ACL_vector EVERYONE_CREATE_AND_READ_CREATOR_ALL;
+
+
+inline std::ostream& operator<<(
+ std::ostream& stream,
+ const Authentication& authentication)
+{
+ // TODO(benh): Fix this once we support more than just 'digest'.
+ CHECK_EQ(authentication.scheme, "digest");
+ return stream << authentication.credentials;
+}
+
+} // namespace zookeeper {
+
+#endif // __ZOOKEEPER_AUTHENTICATION_HPP__
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/include/mesos/zookeeper/contender.hpp
----------------------------------------------------------------------
diff --git a/include/mesos/zookeeper/contender.hpp b/include/mesos/zookeeper/contender.hpp
new file mode 100644
index 0000000..348354a
--- /dev/null
+++ b/include/mesos/zookeeper/contender.hpp
@@ -0,0 +1,81 @@
+// 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 __ZOOKEEPER_CONTENDER_HPP
+#define __ZOOKEEPER_CONTENDER_HPP
+
+#include <string>
+
+#include <mesos/zookeeper/group.hpp>
+
+#include <process/future.hpp>
+
+#include <stout/nothing.hpp>
+#include <stout/option.hpp>
+
+namespace zookeeper {
+
+// Forward declaration.
+class LeaderContenderProcess;
+
+
+// Provides an abstraction for contending to be the leader of a
+// ZooKeeper group.
+// Note that the contender is NOT reusable, which means its methods
+// are supposed to be called once and the client needs to create a
+// new instance to contend again.
+class LeaderContender
+{
+public:
+ // The specified 'group' is expected to outlive the contender. The
+ // specified 'data' is associated with the group membership created
+ // by this contender. 'label' indicates the label for the znode that
+ // stores the 'data'.
+ LeaderContender(Group* group,
+ const std::string& data,
+ const Option<std::string>& label);
+
+ // Note that the contender's membership, if obtained, is scheduled
+ // to be cancelled during destruction.
+ // NOTE: The client should call withdraw() to guarantee that the
+ // membership is cancelled when its returned future is satisfied.
+ virtual ~LeaderContender();
+
+ // Returns a Future<Nothing> once the contender has achieved
+ // candidacy (by obtaining a membership) and a failure otherwise.
+ // The inner Future returns Nothing when the contender is out of
+ // the contest (i.e. its membership is lost) and a failure if it is
+ // unable to watch the membership.
+ // It should be called only once, otherwise a failure is returned.
+ process::Future<process::Future<Nothing> > contend();
+
+ // Returns true if successfully withdrawn from the contest (either
+ // while contending or has already contended and is watching for
+ // membership loss).
+ // A false return value implies that there was no valid group
+ // membership to cancel, which may be a result of a race to cancel
+ // an expired membership or because there is nothing to withdraw.
+ // A failed future is returned if the contender is unable to
+ // withdraw.
+ process::Future<bool> withdraw();
+
+private:
+ LeaderContenderProcess* process;
+};
+
+} // namespace zookeeper {
+
+#endif // __ZOOKEEPER_CONTENDER_HPP
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/include/mesos/zookeeper/detector.hpp
----------------------------------------------------------------------
diff --git a/include/mesos/zookeeper/detector.hpp b/include/mesos/zookeeper/detector.hpp
new file mode 100644
index 0000000..5c45f72
--- /dev/null
+++ b/include/mesos/zookeeper/detector.hpp
@@ -0,0 +1,67 @@
+// 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 __ZOOKEEPER_DETECTOR_HPP__
+#define __ZOOKEEPER_DETECTOR_HPP__
+
+#include <string>
+
+#include <mesos/zookeeper/group.hpp>
+
+#include <stout/result.hpp>
+
+#include <process/future.hpp>
+
+namespace zookeeper {
+
+// Forward declaration.
+class LeaderDetectorProcess;
+
+// Provides an abstraction for detecting the leader of a ZooKeeper
+// group.
+class LeaderDetector
+{
+public:
+ // The specified 'group' is expected to outlive the detector.
+ explicit LeaderDetector(Group* group);
+ virtual ~LeaderDetector();
+
+ // Returns some membership after an election has occurred and a
+ // leader (membership) is elected, or none if an election occurs and
+ // no leader is elected (e.g., all memberships are lost).
+ // A failed future is returned if the detector is unable to detect
+ // the leading master due to a non-retryable error.
+ // Note that the detector transparently tries to recover from
+ // retryable errors until the group session expires, in which case
+ // the Future returns None.
+ // The future is never discarded unless it stays pending when the
+ // detector destructs.
+ //
+ // The 'previous' result (if any) should be passed back if this
+ // method is called repeatedly so the detector only returns when it
+ // gets a different result.
+ //
+ // TODO(xujyan): Use a Stream abstraction instead.
+ process::Future<Option<Group::Membership> > detect(
+ const Option<Group::Membership>& previous = None());
+
+private:
+ LeaderDetectorProcess* process;
+};
+
+} // namespace zookeeper {
+
+#endif // __ZOOKEEPER_DETECTOR_HPP__
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/include/mesos/zookeeper/group.hpp
----------------------------------------------------------------------
diff --git a/include/mesos/zookeeper/group.hpp b/include/mesos/zookeeper/group.hpp
new file mode 100644
index 0000000..d5ffca4
--- /dev/null
+++ b/include/mesos/zookeeper/group.hpp
@@ -0,0 +1,351 @@
+// 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 __ZOOKEEPER_GROUP_HPP__
+#define __ZOOKEEPER_GROUP_HPP__
+
+#include <map>
+#include <set>
+#include <string>
+
+#include <mesos/zookeeper/authentication.hpp>
+#include <mesos/zookeeper/url.hpp>
+
+#include <process/future.hpp>
+#include <process/process.hpp>
+#include <process/timer.hpp>
+
+#include <stout/check.hpp>
+#include <stout/duration.hpp>
+#include <stout/none.hpp>
+#include <stout/option.hpp>
+#include <stout/try.hpp>
+
+// Forward declarations.
+class Watcher;
+class ZooKeeper;
+
+namespace zookeeper {
+
+// Forward declaration.
+class GroupProcess;
+
+// Represents a distributed group managed by ZooKeeper. A group is
+// associated with a specific ZooKeeper path, and members are
+// represented by ephemeral sequential nodes.
+class Group
+{
+public:
+ // Represents a group membership. Note that we order memberships by
+ // membership id (that is, an older membership is ordered before a
+ // younger membership). In addition, we do not use the "cancelled"
+ // future to compare memberships so that two memberships created
+ // from different Group instances will still be considered the same.
+ struct Membership
+ {
+ bool operator==(const Membership& that) const
+ {
+ return sequence == that.sequence;
+ }
+
+ bool operator!=(const Membership& that) const
+ {
+ return sequence != that.sequence;
+ }
+
+ bool operator<(const Membership& that) const
+ {
+ return sequence < that.sequence;
+ }
+
+ bool operator<=(const Membership& that) const
+ {
+ return sequence <= that.sequence;
+ }
+
+ bool operator>(const Membership& that) const
+ {
+ return sequence > that.sequence;
+ }
+
+ bool operator>=(const Membership& that) const
+ {
+ return sequence >= that.sequence;
+ }
+
+ int32_t id() const
+ {
+ return sequence;
+ }
+
+ Option<std::string> label() const
+ {
+ return label_;
+ }
+
+ // Returns a future that is only satisfied once this membership
+ // has been cancelled. In which case, the value of the future is
+ // true if you own this membership and cancelled it by invoking
+ // Group::cancel. Otherwise, the value of the future is false (and
+ // could signify cancellation due to a session expiration or
+ // operator error).
+ process::Future<bool> cancelled() const
+ {
+ return cancelled_;
+ }
+
+ private:
+ friend class GroupProcess; // Creates and manages memberships.
+
+ Membership(int32_t _sequence,
+ const Option<std::string>& _label,
+ const process::Future<bool>& cancelled)
+ : sequence(_sequence), label_(_label), cancelled_(cancelled) {}
+
+ const int32_t sequence;
+ const Option<std::string> label_;
+ process::Future<bool> cancelled_;
+ };
+
+ // Constructs this group using the specified ZooKeeper servers (list
+ // of host:port) with the given session timeout at the specified znode.
+ Group(const std::string& servers,
+ const Duration& sessionTimeout,
+ const std::string& znode,
+ const Option<Authentication>& auth = None());
+ Group(const URL& url,
+ const Duration& sessionTimeout);
+
+ ~Group();
+
+ // Returns the result of trying to join a "group" in ZooKeeper.
+ // If "label" is provided the newly created znode contains "label_"
+ // as the prefix. If join is successful, an "owned" membership will
+ // be returned whose retrievable data will be a copy of the
+ // specified parameter. A membership is not "renewed" in the event
+ // of a ZooKeeper session expiration. Instead, a client should watch
+ // the group memberships and rejoin the group as appropriate.
+ process::Future<Membership> join(
+ const std::string& data,
+ const Option<std::string>& label = None());
+
+ // Returns the result of trying to cancel a membership. Note that
+ // only memberships that are "owned" (see join) can be canceled.
+ process::Future<bool> cancel(const Membership& membership);
+
+ // Returns the result of trying to fetch the data associated with a
+ // group membership.
+ // A None is returned if the specified membership doesn't exist,
+ // e.g., it can be removed before this call can read it content.
+ process::Future<Option<std::string>> data(const Membership& membership);
+
+ // Returns a future that gets set when the group memberships differ
+ // from the "expected" memberships specified.
+ process::Future<std::set<Membership>> watch(
+ const std::set<Membership>& expected = std::set<Membership>());
+
+ // Returns the current ZooKeeper session associated with this group,
+ // or none if no session currently exists.
+ process::Future<Option<int64_t>> session();
+
+ // Made public for testing purposes.
+ GroupProcess* process;
+};
+
+
+class GroupProcess : public process::Process<GroupProcess>
+{
+public:
+ GroupProcess(const std::string& servers,
+ const Duration& sessionTimeout,
+ const std::string& znode,
+ const Option<Authentication>& auth);
+
+ GroupProcess(const URL& url,
+ const Duration& sessionTimeout);
+
+ virtual ~GroupProcess();
+
+ virtual void initialize();
+
+ static const Duration RETRY_INTERVAL;
+
+ // Helper function that returns the basename of the znode of
+ // the membership.
+ static std::string zkBasename(const Group::Membership& membership);
+
+ // Group implementation.
+ process::Future<Group::Membership> join(
+ const std::string& data,
+ const Option<std::string>& label);
+ process::Future<bool> cancel(const Group::Membership& membership);
+ process::Future<Option<std::string>> data(
+ const Group::Membership& membership);
+ process::Future<std::set<Group::Membership>> watch(
+ const std::set<Group::Membership>& expected);
+ process::Future<Option<int64_t>> session();
+
+ // ZooKeeper events.
+ // Note that events from previous sessions are dropped.
+ void connected(int64_t sessionId, bool reconnect);
+ void reconnecting(int64_t sessionId);
+ void expired(int64_t sessionId);
+ void updated(int64_t sessionId, const std::string& path);
+ void created(int64_t sessionId, const std::string& path);
+ void deleted(int64_t sessionId, const std::string& path);
+
+private:
+ void startConnection();
+
+ Result<Group::Membership> doJoin(
+ const std::string& data,
+ const Option<std::string>& label);
+ Result<bool> doCancel(const Group::Membership& membership);
+ Result<Option<std::string>> doData(const Group::Membership& membership);
+
+ // Returns true if authentication is successful, false if the
+ // failure is retryable and Error otherwise.
+ Try<bool> authenticate();
+
+ // Creates the group (which means creating its base path) on ZK.
+ // Returns true if successful, false if the failure is retryable
+ // and Error otherwise.
+ Try<bool> create();
+
+ // Attempts to cache the current set of memberships.
+ // Returns true if successful, false if the failure is retryable
+ // and Error otherwise.
+ Try<bool> cache();
+
+ // Synchronizes pending operations with ZooKeeper and also attempts
+ // to cache the current set of memberships if necessary.
+ // Returns true if successful, false if the failure is retryable
+ // and Error otherwise.
+ Try<bool> sync();
+
+ // Updates any pending watches.
+ void update();
+
+ // Generic retry method. This mechanism is "generic" in the sense
+ // that it is not specific to any particular operation, but rather
+ // attempts to perform all pending operations (including caching
+ // memberships if necessary).
+ void retry(const Duration& duration);
+
+ void timedout(int64_t sessionId);
+
+ // Aborts the group instance and fails all pending operations.
+ // The group then enters an error state and all subsequent
+ // operations will fail as well.
+ void abort(const std::string& message);
+
+ // Potential non-retryable error set by abort().
+ Option<Error> error;
+
+ const std::string servers;
+
+ // The session timeout requested by the client.
+ const Duration sessionTimeout;
+
+ const std::string znode;
+
+ Option<Authentication> auth; // ZooKeeper authentication.
+
+ const ACL_vector acl; // Default ACL to use.
+
+ Watcher* watcher;
+ ZooKeeper* zk;
+
+ // Group connection state.
+ // Normal state transitions:
+ // DISCONNECTED -> CONNECTING -> CONNECTED -> AUTHENTICATED
+ // -> READY.
+ // Reconnection does not change the current state and the state is
+ // only reset to DISCONNECTED after session expiration. Therefore
+ // the client's "progress" in setting up the group is preserved
+ // across reconnections. This means authenticate() and create() are
+ // only successfully executed once in one ZooKeeper session.
+ enum State
+ {
+ DISCONNECTED, // The initial state.
+ CONNECTING, // ZooKeeper connecting.
+ CONNECTED, // ZooKeeper connected but before group setup.
+ AUTHENTICATED, // ZooKeeper connected and authenticated.
+ READY, // ZooKeeper connected, session authenticated and
+ // base path for the group created.
+ } state;
+
+ struct Join
+ {
+ Join(const std::string& _data, const Option<std::string>& _label)
+ : data(_data), label(_label) {}
+ std::string data;
+ const Option<std::string> label;
+ process::Promise<Group::Membership> promise;
+ };
+
+ struct Cancel
+ {
+ explicit Cancel(const Group::Membership& _membership)
+ : membership(_membership) {}
+ Group::Membership membership;
+ process::Promise<bool> promise;
+ };
+
+ struct Data
+ {
+ explicit Data(const Group::Membership& _membership)
+ : membership(_membership) {}
+ Group::Membership membership;
+ process::Promise<Option<std::string>> promise;
+ };
+
+ struct Watch
+ {
+ explicit Watch(const std::set<Group::Membership>& _expected)
+ : expected(_expected) {}
+ std::set<Group::Membership> expected;
+ process::Promise<std::set<Group::Membership>> promise;
+ };
+
+ struct {
+ std::queue<Join*> joins;
+ std::queue<Cancel*> cancels;
+ std::queue<Data*> datas;
+ std::queue<Watch*> watches;
+ } pending;
+
+ // Indicates there is a pending delayed retry.
+ bool retrying;
+
+ // Expected ZooKeeper sequence numbers (either owned/created by this
+ // group instance or not) and the promise we associate with their
+ // "cancellation" (i.e., no longer part of the group).
+ std::map<int32_t, process::Promise<bool>*> owned;
+ std::map<int32_t, process::Promise<bool>*> unowned;
+
+ // Cache of owned + unowned, where 'None' represents an invalid
+ // cache and 'Some' represents a valid cache.
+ Option<std::set<Group::Membership>> memberships;
+
+ // A timer that controls when we should give up on waiting for the
+ // current connection attempt to succeed and try to reconnect.
+ Option<process::Timer> connectTimer;
+};
+
+} // namespace zookeeper {
+
+#endif // __ZOOKEEPER_GROUP_HPP__
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/include/mesos/zookeeper/url.hpp
----------------------------------------------------------------------
diff --git a/include/mesos/zookeeper/url.hpp b/include/mesos/zookeeper/url.hpp
new file mode 100644
index 0000000..b67d32f
--- /dev/null
+++ b/include/mesos/zookeeper/url.hpp
@@ -0,0 +1,122 @@
+// 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 __ZOOKEEPER_URL_HPP__
+#define __ZOOKEEPER_URL_HPP__
+
+#include <string>
+
+#include <mesos/zookeeper/authentication.hpp>
+
+#include <stout/error.hpp>
+#include <stout/option.hpp>
+#include <stout/strings.hpp>
+#include <stout/try.hpp>
+
+namespace zookeeper {
+
+// Describes a ZooKeeper URL of the form:
+//
+// zk://username:password@servers/path
+//
+// Where username:password is for the 'digest' scheme (see ZooKeeper
+// documentation regarding "access controls using ACLs") and servers
+// is of the form:
+//
+// host1:port1,host2:port2,host3:port3
+//
+// Note that in the future we may want to support authentication
+// mechanisms other than 'digest' and have a URL of the following
+// form.
+//
+// zk://scheme:credentials@servers/path
+class URL
+{
+public:
+ static Try<URL> parse(const std::string& url);
+
+ static const char* scheme()
+ {
+ return "zk://";
+ }
+
+ const Option<Authentication> authentication;
+ const std::string servers;
+ const std::string path;
+
+private:
+ URL(const std::string& _servers,
+ const std::string& _path)
+ : servers(_servers),
+ path(_path) {}
+
+ URL(const std::string& credentials,
+ const std::string& _servers,
+ const std::string& _path)
+ : authentication(Authentication("digest", credentials)),
+ servers(_servers),
+ path(_path) {}
+};
+
+
+inline Try<URL> URL::parse(const std::string& url)
+{
+ std::string s = strings::trim(url);
+
+ if (!strings::startsWith(s, URL::scheme())) {
+ return Error("Expecting 'zk://' at the beginning of the URL");
+ }
+ s = s.substr(5);
+
+ // Look for the trailing '/' (if any), that's where the path starts.
+ std::string path;
+ do {
+ size_t index = s.find_last_of('/');
+
+ if (index == std::string::npos) {
+ break;
+ } else {
+ path = s.substr(index) + path;
+ s = s.substr(0, index);
+ }
+ } while (true);
+
+ if (path == "") {
+ path = "/";
+ }
+
+ // Look for the trailing '@' (if any), that's where servers starts.
+ size_t index = s.find_last_of('@');
+
+ if (index != std::string::npos) {
+ return URL(s.substr(0, index), s.substr(index + 1), path);
+ } else {
+ return URL(s, path);
+ }
+}
+
+inline std::ostream& operator<<(std::ostream& stream, const URL& url)
+{
+ stream << URL::scheme();
+ if (url.authentication.isSome()) {
+ stream << url.authentication.get() << "@";
+ }
+ return stream << url.servers << url.path;
+}
+
+} // namespace zookeeper {
+
+#endif // __ZOOKEEPER_URL_HPP__
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/include/mesos/zookeeper/watcher.hpp
----------------------------------------------------------------------
diff --git a/include/mesos/zookeeper/watcher.hpp b/include/mesos/zookeeper/watcher.hpp
new file mode 100644
index 0000000..7be9c06
--- /dev/null
+++ b/include/mesos/zookeeper/watcher.hpp
@@ -0,0 +1,93 @@
+// 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 __ZOOKEEPER_WATCHER_HPP__
+#define __ZOOKEEPER_WATCHER_HPP__
+
+#include <stdint.h>
+
+#include <glog/logging.h>
+
+#include <mesos/zookeeper/zookeeper.hpp>
+
+#include <process/dispatch.hpp>
+
+
+// A watcher which dispatches events to a process. Note that it is
+// only "safe" to reuse an instance across ZooKeeper instances after a
+// session expiration. TODO(benh): Add a 'reset/initialize' to the
+// Watcher so that a single instance can be reused.
+// NOTE: By the time the dispatched events are processed by 'pid',
+// its session ID may have changed! Therefore, we pass the session ID
+// for the event to allow the 'pid' Process to check for staleness.
+template <typename T>
+class ProcessWatcher : public Watcher
+{
+public:
+ explicit ProcessWatcher(const process::PID<T>& _pid)
+ : pid(_pid), reconnect(false) {}
+
+ virtual void process(
+ int type,
+ int state,
+ int64_t sessionId,
+ const std::string& path)
+ {
+ if (type == ZOO_SESSION_EVENT) {
+ if (state == ZOO_CONNECTED_STATE) {
+ // Connected (initial or reconnect).
+ process::dispatch(pid, &T::connected, sessionId, reconnect);
+ // If this watcher gets reused then the next connected
+ // event shouldn't be perceived as a reconnect.
+ reconnect = false;
+ } else if (state == ZOO_CONNECTING_STATE) {
+ // The client library automatically reconnects, taking
+ // into account failed servers in the connection string,
+ // appropriately handling the "herd effect", etc.
+ process::dispatch(pid, &T::reconnecting, sessionId);
+ // TODO(benh): If this watcher gets reused then the next
+ // connected event will be perceived as a reconnect, but it
+ // should not.
+ reconnect = true;
+ } else if (state == ZOO_EXPIRED_SESSION_STATE) {
+ process::dispatch(pid, &T::expired, sessionId);
+ // If this watcher gets reused then the next connected
+ // event shouldn't be perceived as a reconnect.
+ reconnect = false;
+ } else {
+ LOG(FATAL) << "Unhandled ZooKeeper state (" << state << ")"
+ << " for ZOO_SESSION_EVENT";
+ }
+ } else if (type == ZOO_CHILD_EVENT) {
+ process::dispatch(pid, &T::updated, sessionId, path);
+ } else if (type == ZOO_CHANGED_EVENT) {
+ process::dispatch(pid, &T::updated, sessionId, path);
+ } else if (type == ZOO_CREATED_EVENT) {
+ process::dispatch(pid, &T::created, sessionId, path);
+ } else if (type == ZOO_DELETED_EVENT) {
+ process::dispatch(pid, &T::deleted, sessionId, path);
+ } else {
+ LOG(FATAL) << "Unhandled ZooKeeper event (" << type << ")"
+ << " in state (" << state << ")";
+ }
+ }
+
+private:
+ const process::PID<T> pid;
+ bool reconnect;
+};
+
+#endif // __ZOOKEEPER_WATCHER_HPP__
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/include/mesos/zookeeper/zookeeper.hpp
----------------------------------------------------------------------
diff --git a/include/mesos/zookeeper/zookeeper.hpp b/include/mesos/zookeeper/zookeeper.hpp
new file mode 100644
index 0000000..9191df5
--- /dev/null
+++ b/include/mesos/zookeeper/zookeeper.hpp
@@ -0,0 +1,335 @@
+// 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.
+
+/**
+ * ZooKeeper C++ API.
+ *
+ * To provide for varying underlying implementations the pimpl idiom
+ * (also known as the compiler-firewall, bridge pattern, etc) was used
+ * for the ZooKeeper class.
+*/
+#ifndef ZOOKEEPER_HPP
+#define ZOOKEEPER_HPP
+
+#include <stdint.h>
+
+#include <zookeeper.h>
+
+#include <string>
+#include <vector>
+
+#include <stout/duration.hpp>
+
+
+/* Forward declarations of classes we are using. */
+class ZooKeeper;
+class ZooKeeperProcess;
+
+/**
+ * This interface specifies the public interface an event handler
+ * class must implement. A ZooKeeper client will get various events
+ * from the ZooKeeper server it connects to. An application using such
+ * a client handles these events by registering a callback object with
+ * the client. The callback object is expected to be an instance of a
+ * class that implements Watcher interface.
+ *
+ * Note that the watcher is invoked by ZooKeeper from a single thread.
+ * See http://zookeeper.apache.org/doc/trunk/zookeeperProgrammers.html#C+Binding
+ */
+class Watcher
+{
+public:
+ virtual void process(
+ int type,
+ int state,
+ int64_t sessionId,
+ const std::string& path) = 0;
+
+ virtual ~Watcher() {}
+};
+
+
+/*
+ * TODO(benh): Clean up this documentation.
+ *
+ * This is the main class of ZooKeeper client library. To use a
+ * ZooKeeper service, an application must first instantiate an object
+ * of ZooKeeper class. All the iterations will be done by calling the
+ * methods of ZooKeeper class.
+ *
+ * Once a connection to a server is established, a session ID is
+ * assigned to the client. The client will send heart beats to the
+ * server periodically to keep the session valid.
+ *
+ * The application can call ZooKeeper APIs through a client as long as
+ * the session ID of the client remains valid.
+ *
+ * If for some reason, the client fails to send heart beats to the
+ * server for a prolonged period of time (exceeding the sessionTimeout
+ * value, for instance), the server will expire the session, and the
+ * session ID will become invalid. The client object will no longer be
+ * usable. To make ZooKeeper API calls, the application must create a
+ * new client object.
+ *
+ * If the ZooKeeper server the client currently connects to fails or
+ * otherwise does not respond, the client will automatically try to
+ * connect to another server before its session ID expires. If
+ * successful, the application can continue to use the client.
+ *
+ * Some successful ZooKeeper API calls can leave watches on the "data
+ * nodes" in the ZooKeeper server. Other successful ZooKeeper API
+ * calls can trigger those watches. Once a watch is triggered, an
+ * event will be delivered to the client which left the watch at the
+ * first place. Each watch can be triggered only once. Thus, up to one
+ * event will be delivered to a client for every watch it leaves.
+ *
+ * A client needs an object of a class implementing Watcher interface
+ * for processing the events delivered to the client. When a client
+ * drops current connection and re-connects to a server, all the
+ * existing watches are considered as being triggered but the
+ * undelivered events are lost. To emulate this, the client will
+ * generate a special event to tell the event handler a connection has
+ * been dropped. This special event has type EventNone and state
+ * sKeeperStateDisconnected.
+ */
+class ZooKeeper
+{
+public:
+ /**
+ * \brief instantiate new ZooKeeper client.
+ *
+ * The constructor initiates a new session, however session
+ * establishment is asynchronous, meaning that the session should
+ * not be considered established until (and unless) an event of
+ * state ZOO_CONNECTED_STATE is received.
+ * \param servers comma-separated host:port pairs, each corresponding
+ * to a ZooKeeper server. e.g. "127.0.0.1:3000,127.0.0.1:3001"
+ * \param watcher the instance of Watcher that receives event
+ * callbacks. When notifications are triggered the Watcher::process
+ * method will be invoked.
+ */
+ ZooKeeper(const std::string& servers,
+ const Duration& sessionTimeout,
+ Watcher* watcher);
+
+ ~ZooKeeper();
+
+ /**
+ * \brief get the state of the zookeeper connection.
+ *
+ * The return value will be one of the \ref State Consts.
+ */
+ int getState();
+
+ /**
+ * \brief get the current session id.
+ *
+ * The current session id or 0 if no session is established.
+ */
+ int64_t getSessionId();
+
+ /**
+ * \brief get the current session timeout.
+ *
+ * The session timeout requested by the client or the negotiated
+ * session timeout after the session is established with
+ * ZooKeeper. Note that this might differ from the initial
+ * `sessionTimeout` specified when this instance was constructed.
+ */
+ Duration getSessionTimeout() const;
+
+ /**
+ * \brief authenticate synchronously.
+ */
+ int authenticate(const std::string& scheme, const std::string& credentials);
+
+ /**
+ * \brief create a node synchronously.
+ *
+ * This method will create a node in ZooKeeper. A node can only be
+ * created if it does not already exists. The Create Flags affect
+ * the creation of nodes. If ZOO_EPHEMERAL flag is set, the node
+ * will automatically get removed if the client session goes
+ * away. If the ZOO_SEQUENCE flag is set, a unique monotonically
+ * increasing sequence number is appended to the path name.
+ *
+ * \param path The name of the node. Expressed as a file name with
+ * slashes separating ancestors of the node.
+ * \param data The data to be stored in the node.
+ * \param acl The initial ACL of the node. If null, the ACL of the
+ * parent will be used.
+ * \param flags this parameter can be set to 0 for normal create or
+ * an OR of the Create Flags
+ * \param result A string which will be filled with the path of
+ * the new node (this might be different than the supplied path
+ * because of the ZOO_SEQUENCE flag). The path string will always
+ * be null-terminated.
+ * \param recursive if true, attempts to create all intermediate
+ * znodes as required; note that 'flags' and 'data' will only be
+ * applied to the creation of 'basename(path)'.
+ * \return one of the following codes are returned:
+ * ZOK operation completed successfully
+ * ZNONODE the parent node does not exist.
+ * ZNODEEXISTS the node already exists
+ * ZNOAUTH the client does not have permission.
+ * ZNOCHILDRENFOREPHEMERALS cannot create children of ephemeral nodes.
+ * ZBADARGUMENTS - invalid input parameters
+ * ZINVALIDSTATE - state is ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE
+ * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory
+ */
+ int create(
+ const std::string& path,
+ const std::string& data,
+ const ACL_vector& acl,
+ int flags,
+ std::string* result,
+ bool recursive = false);
+
+ /**
+ * \brief delete a node in zookeeper synchronously.
+ *
+ * \param path the name of the node. Expressed as a file name with
+ * slashes separating ancestors of the node.
+ * \param version the expected version of the node. The function
+ * will fail if the actual version of the node does not match the
+ * expected version. If -1 is used the version check will not take
+ * place.
+ * \return one of the following values is returned.
+ * ZOK operation completed successfully
+ * ZNONODE the node does not exist.
+ * ZNOAUTH the client does not have permission.
+ * ZBADVERSION expected version does not match actual version.
+ * ZNOTEMPTY children are present; node cannot be deleted.
+ * ZBADARGUMENTS - invalid input parameters
+ * ZINVALIDSTATE - state is ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE
+ * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory
+ */
+ int remove(const std::string& path, int version);
+
+ /**
+ * \brief checks the existence of a node in zookeeper synchronously.
+ *
+ * \param path the name of the node. Expressed as a file name with
+ * slashes separating ancestors of the node.
+ * \param watch if true, a watch will be set at the server to
+ * notify the client if the node changes. The watch will be set even
+ * if the node does not exist. This allows clients to watch for
+ * nodes to appear.
+ * \param stat the return stat value of the node.
+ * \return return code of the function call.
+ * ZOK operation completed successfully
+ * ZNONODE the node does not exist.
+ * ZNOAUTH the client does not have permission.
+ * ZBADARGUMENTS - invalid input parameters
+ * ZINVALIDSTATE - state is ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE
+ * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory
+ */
+ int exists(const std::string& path, bool watch, Stat* stat);
+
+ /**
+ * \brief gets the data associated with a node synchronously.
+ *
+ * \param path the name of the node. Expressed as a file name with
+ * slashes separating ancestors of the node.
+ * \param watch if nonzero, a watch will be set at the server to
+ * notify the client if the node changes.
+ * \param result the data returned by the server
+ * \param stat if not NULL, will hold the value of stat for the path
+ * on return.
+ * \return return value of the function call.
+ * ZOK operation completed successfully
+ * ZNONODE the node does not exist.
+ * ZNOAUTH the client does not have permission.
+ * ZBADARGUMENTS - invalid input parameters
+ * ZINVALIDSTATE - state is ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE
+ * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory
+ */
+ int get(
+ const std::string& path,
+ bool watch,
+ std::string* result,
+ Stat* stat);
+
+ /**
+ * \brief lists the children of a node synchronously.
+ *
+ * \param path the name of the node. Expressed as a file name with
+ * slashes separating ancestors of the node.
+ * \param watch if true, a watch will be set at the server to notify
+ * the client if the node changes.
+ * \param results return value of children paths.
+ * \return the return code of the function.
+ * ZOK operation completed successfully
+ * ZNONODE the node does not exist.
+ * ZNOAUTH the client does not have permission.
+ * ZBADARGUMENTS - invalid input parameters
+ * ZINVALIDSTATE - state is ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE
+ * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory
+ */
+ int getChildren(
+ const std::string& path,
+ bool watch,
+ std::vector<std::string>* results);
+
+ /**
+ * \brief sets the data associated with a node.
+ *
+ * \param path the name of the node. Expressed as a file name with slashes
+ * separating ancestors of the node.
+ * \param data the data to be written to the node.
+ * \param version the expected version of the node. The function will fail if
+ * the actual version of the node does not match the expected version. If -1 is
+ * used the version check will not take place.
+ * \return the return code for the function call.
+ * ZOK operation completed successfully
+ * ZNONODE the node does not exist.
+ * ZNOAUTH the client does not have permission.
+ * ZBADVERSION expected version does not match actual version.
+ * ZBADARGUMENTS - invalid input parameters
+ * ZINVALIDSTATE - zhandle state is either ZOO_SESSION_EXPIRED_STATE or ZOO_AUTH_FAILED_STATE
+ * ZMARSHALLINGERROR - failed to marshall a request; possibly, out of memory
+ */
+ int set(const std::string& path, const std::string& data, int version);
+
+ /**
+ * \brief return a message describing the return code.
+ *
+ * \return string message corresponding to return code.
+ */
+ std::string message(int code) const;
+
+ /**
+ * \brief returns whether or not the specified return code implies
+ * the operation can be retried "as is" (i.e., without needing to
+ * change anything).
+ *
+ * \return bool indicating whether operation can be retried.
+ */
+ bool retryable(int code);
+
+
+protected:
+ /* Underlying implementation (pimpl idiom). */
+ ZooKeeperProcess* process;
+
+private:
+ /* ZooKeeper instances are not copyable. */
+ ZooKeeper(const ZooKeeper& that);
+ ZooKeeper& operator=(const ZooKeeper& that);
+};
+
+
+#endif /* ZOOKEEPER_HPP */
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/src/Makefile.am
----------------------------------------------------------------------
diff --git a/src/Makefile.am b/src/Makefile.am
index 6b21790..271c7d3 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -579,6 +579,17 @@ v1scheduler_HEADERS = \
nodist_v1scheduler_HEADERS = \
../include/mesos/v1/scheduler/scheduler.pb.h
+zookeeperdir = $(pkgincludedir)/zookeeper
+
+zookeeper_HEADERS = \
+ $(top_srcdir)/include/mesos/zookeeper/authentication.hpp \
+ $(top_srcdir)/include/mesos/zookeeper/contender.hpp \
+ $(top_srcdir)/include/mesos/zookeeper/detector.hpp \
+ $(top_srcdir)/include/mesos/zookeeper/group.hpp \
+ $(top_srcdir)/include/mesos/zookeeper/url.hpp \
+ $(top_srcdir)/include/mesos/zookeeper/watcher.hpp \
+ $(top_srcdir)/include/mesos/zookeeper/zookeeper.hpp
+
# We even use a convenience library for most of Mesos so that we can
# exclude third party libraries so setuptools/distribute can build a
@@ -848,14 +859,7 @@ libmesos_no_3rdparty_la_SOURCES += \
uri/schemes/http.hpp \
usage/usage.hpp \
version/version.hpp \
- watcher/whitelist_watcher.hpp \
- zookeeper/authentication.hpp \
- zookeeper/contender.hpp \
- zookeeper/detector.hpp \
- zookeeper/group.hpp \
- zookeeper/url.hpp \
- zookeeper/watcher.hpp \
- zookeeper/zookeeper.hpp
+ watcher/whitelist_watcher.hpp
MESOS_LINUX_FILES = \
linux/cgroups.cpp \
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/src/log/log.hpp
----------------------------------------------------------------------
diff --git a/src/log/log.hpp b/src/log/log.hpp
index 0f61777..1177117 100644
--- a/src/log/log.hpp
+++ b/src/log/log.hpp
@@ -23,6 +23,8 @@
#include <set>
#include <string>
+#include <mesos/zookeeper/group.hpp>
+
#include <process/future.hpp>
#include <process/owned.hpp>
#include <process/process.hpp>
@@ -33,8 +35,6 @@
#include <stout/none.hpp>
#include <stout/option.hpp>
-#include "zookeeper/group.hpp"
-
namespace mesos {
namespace internal {
namespace log {
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/src/log/network.hpp
----------------------------------------------------------------------
diff --git a/src/log/network.hpp b/src/log/network.hpp
index 6d40027..56c5dbb 100644
--- a/src/log/network.hpp
+++ b/src/log/network.hpp
@@ -24,6 +24,8 @@
#include <set>
#include <string>
+#include <mesos/zookeeper/group.hpp>
+
#include <process/collect.hpp>
#include <process/executor.hpp>
#include <process/protobuf.hpp>
@@ -37,8 +39,6 @@
#include "logging/logging.hpp"
-#include "zookeeper/group.hpp"
-
// Forward declaration.
class NetworkProcess;
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/src/master/contender/contender.cpp
----------------------------------------------------------------------
diff --git a/src/master/contender/contender.cpp b/src/master/contender/contender.cpp
index c412fb9..c11506f 100644
--- a/src/master/contender/contender.cpp
+++ b/src/master/contender/contender.cpp
@@ -18,15 +18,16 @@
#include <mesos/module/contender.hpp>
+#include <mesos/zookeeper/url.hpp>
+
#include <stout/check.hpp>
+#include <stout/os.hpp>
#include "master/contender/standalone.hpp"
#include "master/contender/zookeeper.hpp"
#include "module/manager.hpp"
-#include "zookeeper/url.hpp"
-
using std::string;
namespace mesos {
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/src/master/contender/zookeeper.cpp
----------------------------------------------------------------------
diff --git a/src/master/contender/zookeeper.cpp b/src/master/contender/zookeeper.cpp
index ddc9e00..61766ca 100644
--- a/src/master/contender/zookeeper.cpp
+++ b/src/master/contender/zookeeper.cpp
@@ -18,6 +18,10 @@
#include <mesos/master/contender.hpp>
+#include <mesos/zookeeper/contender.hpp>
+#include <mesos/zookeeper/group.hpp>
+#include <mesos/zookeeper/url.hpp>
+
#include <process/dispatch.hpp>
#include <process/id.hpp>
#include <process/process.hpp>
@@ -26,10 +30,6 @@
#include "master/constants.hpp"
-#include "zookeeper/contender.hpp"
-#include "zookeeper/group.hpp"
-#include "zookeeper/url.hpp"
-
using std::string;
using namespace process;
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/src/master/contender/zookeeper.hpp
----------------------------------------------------------------------
diff --git a/src/master/contender/zookeeper.hpp b/src/master/contender/zookeeper.hpp
index 2e9da74..becb93f 100644
--- a/src/master/contender/zookeeper.hpp
+++ b/src/master/contender/zookeeper.hpp
@@ -21,14 +21,14 @@
#include <mesos/master/contender.hpp>
+#include <mesos/zookeeper/group.hpp>
+#include <mesos/zookeeper/url.hpp>
+
#include <process/future.hpp>
#include <process/owned.hpp>
#include <stout/nothing.hpp>
-#include "zookeeper/group.hpp"
-#include "zookeeper/url.hpp"
-
namespace mesos {
namespace master {
namespace contender {
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/src/master/detector/detector.cpp
----------------------------------------------------------------------
diff --git a/src/master/detector/detector.cpp b/src/master/detector/detector.cpp
index 56621f8..1ebe5af 100644
--- a/src/master/detector/detector.cpp
+++ b/src/master/detector/detector.cpp
@@ -20,9 +20,13 @@
#include <mesos/module/detector.hpp>
+#include <mesos/zookeeper/url.hpp>
+
#include <process/pid.hpp>
#include <process/process.hpp>
+#include <stout/os.hpp>
+
#include "common/protobuf_utils.hpp"
#include "master/detector/standalone.hpp"
@@ -30,8 +34,6 @@
#include "module/manager.hpp"
-#include "zookeeper/url.hpp"
-
using namespace process;
using namespace zookeeper;
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/src/master/detector/zookeeper.cpp
----------------------------------------------------------------------
diff --git a/src/master/detector/zookeeper.cpp b/src/master/detector/zookeeper.cpp
index c829ae6..a737d24 100644
--- a/src/master/detector/zookeeper.cpp
+++ b/src/master/detector/zookeeper.cpp
@@ -21,6 +21,10 @@
#include <mesos/master/detector.hpp>
+#include <mesos/zookeeper/detector.hpp>
+#include <mesos/zookeeper/group.hpp>
+#include <mesos/zookeeper/url.hpp>
+
#include <process/defer.hpp>
#include <process/dispatch.hpp>
#include <process/future.hpp>
@@ -36,10 +40,6 @@
#include "master/constants.hpp"
-#include "zookeeper/detector.hpp"
-#include "zookeeper/group.hpp"
-#include "zookeeper/url.hpp"
-
using namespace process;
using namespace zookeeper;
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/src/master/detector/zookeeper.hpp
----------------------------------------------------------------------
diff --git a/src/master/detector/zookeeper.hpp b/src/master/detector/zookeeper.hpp
index 8365e0f..5b531e0 100644
--- a/src/master/detector/zookeeper.hpp
+++ b/src/master/detector/zookeeper.hpp
@@ -23,14 +23,14 @@
#include <mesos/master/detector.hpp>
+#include <mesos/zookeeper/group.hpp>
+#include <mesos/zookeeper/url.hpp>
+
#include <process/future.hpp>
#include <process/owned.hpp>
#include <stout/option.hpp>
-#include "zookeeper/group.hpp"
-#include "zookeeper/url.hpp"
-
namespace mesos {
namespace master {
namespace detector {
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/src/master/main.cpp
----------------------------------------------------------------------
diff --git a/src/master/main.cpp b/src/master/main.cpp
index ea7f0fc..4cf78e5 100644
--- a/src/master/main.cpp
+++ b/src/master/main.cpp
@@ -33,6 +33,8 @@
#include <mesos/module/anonymous.hpp>
#include <mesos/module/authorizer.hpp>
+#include <mesos/zookeeper/detector.hpp>
+
#include <process/limiter.hpp>
#include <process/owned.hpp>
#include <process/pid.hpp>
@@ -74,8 +76,6 @@
#include "version/version.hpp"
-#include "zookeeper/detector.hpp"
-
using namespace mesos::internal;
using namespace mesos::internal::log;
using namespace mesos::internal::master;
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/src/state/zookeeper.cpp
----------------------------------------------------------------------
diff --git a/src/state/zookeeper.cpp b/src/state/zookeeper.cpp
index 5578fa5..e3e7817 100644
--- a/src/state/zookeeper.cpp
+++ b/src/state/zookeeper.cpp
@@ -23,6 +23,10 @@
#include <string>
#include <vector>
+#include <mesos/zookeeper/authentication.hpp>
+#include <mesos/zookeeper/watcher.hpp>
+#include <mesos/zookeeper/zookeeper.hpp>
+
#include <process/dispatch.hpp>
#include <process/future.hpp>
#include <process/process.hpp>
@@ -44,10 +48,6 @@
#include "state/storage.hpp"
#include "state/zookeeper.hpp"
-#include "zookeeper/authentication.hpp"
-#include "zookeeper/watcher.hpp"
-#include "zookeeper/zookeeper.hpp"
-
using namespace process;
// Note that we don't add 'using std::set' here because we need
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/src/state/zookeeper.hpp
----------------------------------------------------------------------
diff --git a/src/state/zookeeper.hpp b/src/state/zookeeper.hpp
index d8f9df8..290f474 100644
--- a/src/state/zookeeper.hpp
+++ b/src/state/zookeeper.hpp
@@ -20,6 +20,8 @@
#include <set>
#include <string>
+#include <mesos/zookeeper/authentication.hpp>
+
#include <process/future.hpp>
#include <stout/duration.hpp>
@@ -30,8 +32,6 @@
#include "state/storage.hpp"
-#include "zookeeper/authentication.hpp"
-
namespace mesos {
namespace internal {
namespace state {
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/src/tests/cluster.cpp
----------------------------------------------------------------------
diff --git a/src/tests/cluster.cpp b/src/tests/cluster.cpp
index 31d2556..b515e4f 100644
--- a/src/tests/cluster.cpp
+++ b/src/tests/cluster.cpp
@@ -26,6 +26,8 @@
#include <mesos/slave/resource_estimator.hpp>
+#include <mesos/zookeeper/url.hpp>
+
#include <process/clock.hpp>
#include <process/future.hpp>
#include <process/gmock.hpp>
@@ -83,8 +85,6 @@
#include "state/protobuf.hpp"
#include "state/storage.hpp"
-#include "zookeeper/url.hpp"
-
#include "tests/cluster.hpp"
using mesos::master::contender::StandaloneMasterContender;
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/src/tests/cluster.hpp
----------------------------------------------------------------------
diff --git a/src/tests/cluster.hpp b/src/tests/cluster.hpp
index 887342a..81aa080 100644
--- a/src/tests/cluster.hpp
+++ b/src/tests/cluster.hpp
@@ -30,6 +30,8 @@
#include <mesos/slave/resource_estimator.hpp>
+#include <mesos/zookeeper/url.hpp>
+
#include <process/owned.hpp>
#include <process/pid.hpp>
@@ -63,8 +65,6 @@
#include "state/protobuf.hpp"
#include "state/storage.hpp"
-#include "zookeeper/url.hpp"
-
namespace mesos {
namespace internal {
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/src/tests/group_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/group_tests.cpp b/src/tests/group_tests.cpp
index ebf6963..83fca11 100644
--- a/src/tests/group_tests.cpp
+++ b/src/tests/group_tests.cpp
@@ -18,6 +18,9 @@
#include <gmock/gmock.h>
+#include <mesos/zookeeper/authentication.hpp>
+#include <mesos/zookeeper/group.hpp>
+
#include <process/future.hpp>
#include <process/gmock.hpp>
#include <process/gtest.hpp>
@@ -27,9 +30,6 @@
#include "tests/zookeeper.hpp"
-#include "zookeeper/authentication.hpp"
-#include "zookeeper/group.hpp"
-
using zookeeper::Group;
using zookeeper::GroupProcess;
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/src/tests/master_contender_detector_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/master_contender_detector_tests.cpp b/src/tests/master_contender_detector_tests.cpp
index 9ffd07b..ef4786f 100644
--- a/src/tests/master_contender_detector_tests.cpp
+++ b/src/tests/master_contender_detector_tests.cpp
@@ -24,6 +24,8 @@
#include <mesos/executor.hpp>
#include <mesos/scheduler.hpp>
+#include <mesos/zookeeper/contender.hpp>
+
#include <process/clock.hpp>
#include <process/future.hpp>
#include <process/owned.hpp>
@@ -55,8 +57,6 @@
#include "tests/zookeeper.hpp"
#endif
-#include "zookeeper/contender.hpp"
-
using namespace zookeeper;
using mesos::internal::master::Master;
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/src/tests/zookeeper_test_server.hpp
----------------------------------------------------------------------
diff --git a/src/tests/zookeeper_test_server.hpp b/src/tests/zookeeper_test_server.hpp
index 1f1360e..4ca9aca 100644
--- a/src/tests/zookeeper_test_server.hpp
+++ b/src/tests/zookeeper_test_server.hpp
@@ -23,7 +23,7 @@
#include <jvm/org/apache/zookeeper.hpp>
-#include "zookeeper/zookeeper.hpp"
+#include <mesos/zookeeper/zookeeper.hpp>
namespace mesos {
namespace internal {
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/src/tests/zookeeper_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/zookeeper_tests.cpp b/src/tests/zookeeper_tests.cpp
index 6bde10b..0a360c6 100644
--- a/src/tests/zookeeper_tests.cpp
+++ b/src/tests/zookeeper_tests.cpp
@@ -18,6 +18,11 @@
#include <gmock/gmock.h>
+#include <mesos/zookeeper/authentication.hpp>
+#include <mesos/zookeeper/contender.hpp>
+#include <mesos/zookeeper/detector.hpp>
+#include <mesos/zookeeper/group.hpp>
+
#include <process/gmock.hpp>
#include <process/gtest.hpp>
#include <process/owned.hpp>
@@ -27,11 +32,6 @@
#include "master/constants.hpp"
-#include "zookeeper/authentication.hpp"
-#include "zookeeper/contender.hpp"
-#include "zookeeper/detector.hpp"
-#include "zookeeper/group.hpp"
-
#include "tests/zookeeper.hpp"
using namespace process;
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/src/tests/zookeeper_url_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/zookeeper_url_tests.cpp b/src/tests/zookeeper_url_tests.cpp
index f6c5014..2b6345f 100644
--- a/src/tests/zookeeper_url_tests.cpp
+++ b/src/tests/zookeeper_url_tests.cpp
@@ -16,11 +16,11 @@
#include <gtest/gtest.h>
+#include <mesos/zookeeper/url.hpp>
+
#include <stout/gtest.hpp>
#include <stout/try.hpp>
-#include "zookeeper/url.hpp"
-
TEST(ZooKeeperURLTest, URL)
{
Try<zookeeper::URL> url =
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/src/zookeeper/authentication.cpp
----------------------------------------------------------------------
diff --git a/src/zookeeper/authentication.cpp b/src/zookeeper/authentication.cpp
index 62eab1c..0fd99b0 100644
--- a/src/zookeeper/authentication.cpp
+++ b/src/zookeeper/authentication.cpp
@@ -14,7 +14,7 @@
// See the License for the specific language governing permissions and
// limitations under the License
-#include "zookeeper/authentication.hpp"
+#include <mesos/zookeeper/authentication.hpp>
namespace zookeeper {
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/src/zookeeper/authentication.hpp
----------------------------------------------------------------------
diff --git a/src/zookeeper/authentication.hpp b/src/zookeeper/authentication.hpp
deleted file mode 100644
index 40a2333..0000000
--- a/src/zookeeper/authentication.hpp
+++ /dev/null
@@ -1,66 +0,0 @@
-// 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 __ZOOKEEPER_AUTHENTICATION_HPP__
-#define __ZOOKEEPER_AUTHENTICATION_HPP__
-
-#include <zookeeper.h>
-
-#include <string>
-
-#include "logging/logging.hpp"
-
-namespace zookeeper {
-
-struct Authentication
-{
- Authentication(
- const std::string& _scheme,
- const std::string& _credentials)
- : scheme(_scheme),
- credentials(_credentials)
- {
- // TODO(benh): Fix output operator below once this changes.
- CHECK_EQ(scheme, "digest") << "Unsupported authentication scheme";
- }
-
- const std::string scheme;
- const std::string credentials;
-};
-
-
-// An ACL that ensures we're the only authenticated user to mutate our
-// nodes - others are welcome to read.
-extern const ACL_vector EVERYONE_READ_CREATOR_ALL;
-
-// An ACL that allows others to create child nodes and read nodes, but
-// we're the only authenticated user to mutate our nodes.
-extern const ACL_vector EVERYONE_CREATE_AND_READ_CREATOR_ALL;
-
-
-inline std::ostream& operator<<(
- std::ostream& stream,
- const Authentication& authentication)
-{
- // TODO(benh): Fix this once we support more than just 'digest'.
- CHECK_EQ(authentication.scheme, "digest");
- return stream << authentication.credentials;
-}
-
-
-} // namespace zookeeper {
-
-#endif // __ZOOKEEPER_AUTHENTICATION_HPP__
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/src/zookeeper/contender.cpp
----------------------------------------------------------------------
diff --git a/src/zookeeper/contender.cpp b/src/zookeeper/contender.cpp
index 4cda56c..4b1cc65 100644
--- a/src/zookeeper/contender.cpp
+++ b/src/zookeeper/contender.cpp
@@ -17,6 +17,10 @@
#include <set>
#include <string>
+#include <mesos/zookeeper/contender.hpp>
+#include <mesos/zookeeper/detector.hpp>
+#include <mesos/zookeeper/group.hpp>
+
#include <process/defer.hpp>
#include <process/dispatch.hpp>
#include <process/future.hpp>
@@ -27,10 +31,6 @@
#include <stout/option.hpp>
#include <stout/some.hpp>
-#include "zookeeper/contender.hpp"
-#include "zookeeper/detector.hpp"
-#include "zookeeper/group.hpp"
-
using namespace process;
using std::set;
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/src/zookeeper/contender.hpp
----------------------------------------------------------------------
diff --git a/src/zookeeper/contender.hpp b/src/zookeeper/contender.hpp
deleted file mode 100644
index c4bb0bc..0000000
--- a/src/zookeeper/contender.hpp
+++ /dev/null
@@ -1,81 +0,0 @@
-// 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 __ZOOKEEPER_CONTENDER_HPP
-#define __ZOOKEEPER_CONTENDER_HPP
-
-#include <string>
-
-#include <process/future.hpp>
-
-#include <stout/nothing.hpp>
-#include <stout/option.hpp>
-
-#include "zookeeper/group.hpp"
-
-namespace zookeeper {
-
-// Forward declaration.
-class LeaderContenderProcess;
-
-
-// Provides an abstraction for contending to be the leader of a
-// ZooKeeper group.
-// Note that the contender is NOT reusable, which means its methods
-// are supposed to be called once and the client needs to create a
-// new instance to contend again.
-class LeaderContender
-{
-public:
- // The specified 'group' is expected to outlive the contender. The
- // specified 'data' is associated with the group membership created
- // by this contender. 'label' indicates the label for the znode that
- // stores the 'data'.
- LeaderContender(Group* group,
- const std::string& data,
- const Option<std::string>& label);
-
- // Note that the contender's membership, if obtained, is scheduled
- // to be cancelled during destruction.
- // NOTE: The client should call withdraw() to guarantee that the
- // membership is cancelled when its returned future is satisfied.
- virtual ~LeaderContender();
-
- // Returns a Future<Nothing> once the contender has achieved
- // candidacy (by obtaining a membership) and a failure otherwise.
- // The inner Future returns Nothing when the contender is out of
- // the contest (i.e. its membership is lost) and a failure if it is
- // unable to watch the membership.
- // It should be called only once, otherwise a failure is returned.
- process::Future<process::Future<Nothing> > contend();
-
- // Returns true if successfully withdrawn from the contest (either
- // while contending or has already contended and is watching for
- // membership loss).
- // A false return value implies that there was no valid group
- // membership to cancel, which may be a result of a race to cancel
- // an expired membership or because there is nothing to withdraw.
- // A failed future is returned if the contender is unable to
- // withdraw.
- process::Future<bool> withdraw();
-
-private:
- LeaderContenderProcess* process;
-};
-
-} // namespace zookeeper {
-
-#endif // __ZOOKEEPER_CONTENDER_HPP
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/src/zookeeper/detector.cpp
----------------------------------------------------------------------
diff --git a/src/zookeeper/detector.cpp b/src/zookeeper/detector.cpp
index b184deb..f1def68 100644
--- a/src/zookeeper/detector.cpp
+++ b/src/zookeeper/detector.cpp
@@ -16,6 +16,9 @@
#include <set>
+#include <mesos/zookeeper/detector.hpp>
+#include <mesos/zookeeper/group.hpp>
+
#include <process/defer.hpp>
#include <process/dispatch.hpp>
#include <process/future.hpp>
@@ -26,9 +29,6 @@
#include <stout/foreach.hpp>
#include <stout/lambda.hpp>
-#include "zookeeper/detector.hpp"
-#include "zookeeper/group.hpp"
-
using namespace process;
using std::set;
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/src/zookeeper/detector.hpp
----------------------------------------------------------------------
diff --git a/src/zookeeper/detector.hpp b/src/zookeeper/detector.hpp
deleted file mode 100644
index 39a2241..0000000
--- a/src/zookeeper/detector.hpp
+++ /dev/null
@@ -1,67 +0,0 @@
-// 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 __ZOOKEEPER_DETECTOR_HPP__
-#define __ZOOKEEPER_DETECTOR_HPP__
-
-#include <string>
-
-#include <stout/result.hpp>
-
-#include <process/future.hpp>
-
-#include "zookeeper/group.hpp"
-
-namespace zookeeper {
-
-// Forward declaration.
-class LeaderDetectorProcess;
-
-// Provides an abstraction for detecting the leader of a ZooKeeper
-// group.
-class LeaderDetector
-{
-public:
- // The specified 'group' is expected to outlive the detector.
- explicit LeaderDetector(Group* group);
- virtual ~LeaderDetector();
-
- // Returns some membership after an election has occurred and a
- // leader (membership) is elected, or none if an election occurs and
- // no leader is elected (e.g., all memberships are lost).
- // A failed future is returned if the detector is unable to detect
- // the leading master due to a non-retryable error.
- // Note that the detector transparently tries to recover from
- // retryable errors until the group session expires, in which case
- // the Future returns None.
- // The future is never discarded unless it stays pending when the
- // detector destructs.
- //
- // The 'previous' result (if any) should be passed back if this
- // method is called repeatedly so the detector only returns when it
- // gets a different result.
- //
- // TODO(xujyan): Use a Stream abstraction instead.
- process::Future<Option<Group::Membership> > detect(
- const Option<Group::Membership>& previous = None());
-
-private:
- LeaderDetectorProcess* process;
-};
-
-} // namespace zookeeper {
-
-#endif // __ZOOKEEPER_DETECTOR_HPP__
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/src/zookeeper/group.cpp
----------------------------------------------------------------------
diff --git a/src/zookeeper/group.cpp b/src/zookeeper/group.cpp
index ca5e99d..0168089 100644
--- a/src/zookeeper/group.cpp
+++ b/src/zookeeper/group.cpp
@@ -19,6 +19,10 @@
#include <utility>
#include <vector>
+#include <mesos/zookeeper/group.hpp>
+#include <mesos/zookeeper/watcher.hpp>
+#include <mesos/zookeeper/zookeeper.hpp>
+
#include <process/delay.hpp>
#include <process/dispatch.hpp>
#include <process/id.hpp>
@@ -37,10 +41,6 @@
#include "logging/logging.hpp"
-#include "zookeeper/group.hpp"
-#include "zookeeper/watcher.hpp"
-#include "zookeeper/zookeeper.hpp"
-
using namespace process;
using process::wait; // Necessary on some OS's to disambiguate.
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/src/zookeeper/group.hpp
----------------------------------------------------------------------
diff --git a/src/zookeeper/group.hpp b/src/zookeeper/group.hpp
deleted file mode 100644
index 2003b60..0000000
--- a/src/zookeeper/group.hpp
+++ /dev/null
@@ -1,351 +0,0 @@
-// 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 __ZOOKEEPER_GROUP_HPP__
-#define __ZOOKEEPER_GROUP_HPP__
-
-#include <map>
-#include <set>
-#include <string>
-
-#include "process/future.hpp"
-#include "process/timer.hpp"
-#include "process/process.hpp"
-
-#include <stout/check.hpp>
-#include <stout/duration.hpp>
-#include <stout/none.hpp>
-#include <stout/option.hpp>
-#include <stout/try.hpp>
-
-#include "zookeeper/authentication.hpp"
-#include "zookeeper/url.hpp"
-
-// Forward declarations.
-class Watcher;
-class ZooKeeper;
-
-namespace zookeeper {
-
-// Forward declaration.
-class GroupProcess;
-
-// Represents a distributed group managed by ZooKeeper. A group is
-// associated with a specific ZooKeeper path, and members are
-// represented by ephemeral sequential nodes.
-class Group
-{
-public:
- // Represents a group membership. Note that we order memberships by
- // membership id (that is, an older membership is ordered before a
- // younger membership). In addition, we do not use the "cancelled"
- // future to compare memberships so that two memberships created
- // from different Group instances will still be considered the same.
- struct Membership
- {
- bool operator==(const Membership& that) const
- {
- return sequence == that.sequence;
- }
-
- bool operator!=(const Membership& that) const
- {
- return sequence != that.sequence;
- }
-
- bool operator<(const Membership& that) const
- {
- return sequence < that.sequence;
- }
-
- bool operator<=(const Membership& that) const
- {
- return sequence <= that.sequence;
- }
-
- bool operator>(const Membership& that) const
- {
- return sequence > that.sequence;
- }
-
- bool operator>=(const Membership& that) const
- {
- return sequence >= that.sequence;
- }
-
- int32_t id() const
- {
- return sequence;
- }
-
- Option<std::string> label() const
- {
- return label_;
- }
-
- // Returns a future that is only satisfied once this membership
- // has been cancelled. In which case, the value of the future is
- // true if you own this membership and cancelled it by invoking
- // Group::cancel. Otherwise, the value of the future is false (and
- // could signify cancellation due to a session expiration or
- // operator error).
- process::Future<bool> cancelled() const
- {
- return cancelled_;
- }
-
- private:
- friend class GroupProcess; // Creates and manages memberships.
-
- Membership(int32_t _sequence,
- const Option<std::string>& _label,
- const process::Future<bool>& cancelled)
- : sequence(_sequence), label_(_label), cancelled_(cancelled) {}
-
- const int32_t sequence;
- const Option<std::string> label_;
- process::Future<bool> cancelled_;
- };
-
- // Constructs this group using the specified ZooKeeper servers (list
- // of host:port) with the given session timeout at the specified znode.
- Group(const std::string& servers,
- const Duration& sessionTimeout,
- const std::string& znode,
- const Option<Authentication>& auth = None());
- Group(const URL& url,
- const Duration& sessionTimeout);
-
- ~Group();
-
- // Returns the result of trying to join a "group" in ZooKeeper.
- // If "label" is provided the newly created znode contains "label_"
- // as the prefix. If join is successful, an "owned" membership will
- // be returned whose retrievable data will be a copy of the
- // specified parameter. A membership is not "renewed" in the event
- // of a ZooKeeper session expiration. Instead, a client should watch
- // the group memberships and rejoin the group as appropriate.
- process::Future<Membership> join(
- const std::string& data,
- const Option<std::string>& label = None());
-
- // Returns the result of trying to cancel a membership. Note that
- // only memberships that are "owned" (see join) can be canceled.
- process::Future<bool> cancel(const Membership& membership);
-
- // Returns the result of trying to fetch the data associated with a
- // group membership.
- // A None is returned if the specified membership doesn't exist,
- // e.g., it can be removed before this call can read it content.
- process::Future<Option<std::string>> data(const Membership& membership);
-
- // Returns a future that gets set when the group memberships differ
- // from the "expected" memberships specified.
- process::Future<std::set<Membership>> watch(
- const std::set<Membership>& expected = std::set<Membership>());
-
- // Returns the current ZooKeeper session associated with this group,
- // or none if no session currently exists.
- process::Future<Option<int64_t>> session();
-
- // Made public for testing purposes.
- GroupProcess* process;
-};
-
-
-class GroupProcess : public process::Process<GroupProcess>
-{
-public:
- GroupProcess(const std::string& servers,
- const Duration& sessionTimeout,
- const std::string& znode,
- const Option<Authentication>& auth);
-
- GroupProcess(const URL& url,
- const Duration& sessionTimeout);
-
- virtual ~GroupProcess();
-
- virtual void initialize();
-
- static const Duration RETRY_INTERVAL;
-
- // Helper function that returns the basename of the znode of
- // the membership.
- static std::string zkBasename(const Group::Membership& membership);
-
- // Group implementation.
- process::Future<Group::Membership> join(
- const std::string& data,
- const Option<std::string>& label);
- process::Future<bool> cancel(const Group::Membership& membership);
- process::Future<Option<std::string>> data(
- const Group::Membership& membership);
- process::Future<std::set<Group::Membership>> watch(
- const std::set<Group::Membership>& expected);
- process::Future<Option<int64_t>> session();
-
- // ZooKeeper events.
- // Note that events from previous sessions are dropped.
- void connected(int64_t sessionId, bool reconnect);
- void reconnecting(int64_t sessionId);
- void expired(int64_t sessionId);
- void updated(int64_t sessionId, const std::string& path);
- void created(int64_t sessionId, const std::string& path);
- void deleted(int64_t sessionId, const std::string& path);
-
-private:
- void startConnection();
-
- Result<Group::Membership> doJoin(
- const std::string& data,
- const Option<std::string>& label);
- Result<bool> doCancel(const Group::Membership& membership);
- Result<Option<std::string>> doData(const Group::Membership& membership);
-
- // Returns true if authentication is successful, false if the
- // failure is retryable and Error otherwise.
- Try<bool> authenticate();
-
- // Creates the group (which means creating its base path) on ZK.
- // Returns true if successful, false if the failure is retryable
- // and Error otherwise.
- Try<bool> create();
-
- // Attempts to cache the current set of memberships.
- // Returns true if successful, false if the failure is retryable
- // and Error otherwise.
- Try<bool> cache();
-
- // Synchronizes pending operations with ZooKeeper and also attempts
- // to cache the current set of memberships if necessary.
- // Returns true if successful, false if the failure is retryable
- // and Error otherwise.
- Try<bool> sync();
-
- // Updates any pending watches.
- void update();
-
- // Generic retry method. This mechanism is "generic" in the sense
- // that it is not specific to any particular operation, but rather
- // attempts to perform all pending operations (including caching
- // memberships if necessary).
- void retry(const Duration& duration);
-
- void timedout(int64_t sessionId);
-
- // Aborts the group instance and fails all pending operations.
- // The group then enters an error state and all subsequent
- // operations will fail as well.
- void abort(const std::string& message);
-
- // Potential non-retryable error set by abort().
- Option<Error> error;
-
- const std::string servers;
-
- // The session timeout requested by the client.
- const Duration sessionTimeout;
-
- const std::string znode;
-
- Option<Authentication> auth; // ZooKeeper authentication.
-
- const ACL_vector acl; // Default ACL to use.
-
- Watcher* watcher;
- ZooKeeper* zk;
-
- // Group connection state.
- // Normal state transitions:
- // DISCONNECTED -> CONNECTING -> CONNECTED -> AUTHENTICATED
- // -> READY.
- // Reconnection does not change the current state and the state is
- // only reset to DISCONNECTED after session expiration. Therefore
- // the client's "progress" in setting up the group is preserved
- // across reconnections. This means authenticate() and create() are
- // only successfully executed once in one ZooKeeper session.
- enum State
- {
- DISCONNECTED, // The initial state.
- CONNECTING, // ZooKeeper connecting.
- CONNECTED, // ZooKeeper connected but before group setup.
- AUTHENTICATED, // ZooKeeper connected and authenticated.
- READY, // ZooKeeper connected, session authenticated and
- // base path for the group created.
- } state;
-
- struct Join
- {
- Join(const std::string& _data, const Option<std::string>& _label)
- : data(_data), label(_label) {}
- std::string data;
- const Option<std::string> label;
- process::Promise<Group::Membership> promise;
- };
-
- struct Cancel
- {
- explicit Cancel(const Group::Membership& _membership)
- : membership(_membership) {}
- Group::Membership membership;
- process::Promise<bool> promise;
- };
-
- struct Data
- {
- explicit Data(const Group::Membership& _membership)
- : membership(_membership) {}
- Group::Membership membership;
- process::Promise<Option<std::string>> promise;
- };
-
- struct Watch
- {
- explicit Watch(const std::set<Group::Membership>& _expected)
- : expected(_expected) {}
- std::set<Group::Membership> expected;
- process::Promise<std::set<Group::Membership>> promise;
- };
-
- struct {
- std::queue<Join*> joins;
- std::queue<Cancel*> cancels;
- std::queue<Data*> datas;
- std::queue<Watch*> watches;
- } pending;
-
- // Indicates there is a pending delayed retry.
- bool retrying;
-
- // Expected ZooKeeper sequence numbers (either owned/created by this
- // group instance or not) and the promise we associate with their
- // "cancellation" (i.e., no longer part of the group).
- std::map<int32_t, process::Promise<bool>*> owned;
- std::map<int32_t, process::Promise<bool>*> unowned;
-
- // Cache of owned + unowned, where 'None' represents an invalid
- // cache and 'Some' represents a valid cache.
- Option<std::set<Group::Membership>> memberships;
-
- // A timer that controls when we should give up on waiting for the
- // current connection attempt to succeed and try to reconnect.
- Option<process::Timer> connectTimer;
-};
-
-} // namespace zookeeper {
-
-#endif // __ZOOKEEPER_GROUP_HPP__
http://git-wip-us.apache.org/repos/asf/mesos/blob/9be895cf/src/zookeeper/url.hpp
----------------------------------------------------------------------
diff --git a/src/zookeeper/url.hpp b/src/zookeeper/url.hpp
deleted file mode 100644
index 1de7a08..0000000
--- a/src/zookeeper/url.hpp
+++ /dev/null
@@ -1,122 +0,0 @@
-// 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 __ZOOKEEPER_URL_HPP__
-#define __ZOOKEEPER_URL_HPP__
-
-#include <string>
-
-#include <stout/error.hpp>
-#include <stout/option.hpp>
-#include <stout/strings.hpp>
-#include <stout/try.hpp>
-
-#include "zookeeper/authentication.hpp"
-
-namespace zookeeper {
-
-// Describes a ZooKeeper URL of the form:
-//
-// zk://username:password@servers/path
-//
-// Where username:password is for the 'digest' scheme (see ZooKeeper
-// documentation regarding "access controls using ACLs") and servers
-// is of the form:
-//
-// host1:port1,host2:port2,host3:port3
-//
-// Note that in the future we may want to support authentication
-// mechanisms other than 'digest' and have a URL of the following
-// form.
-//
-// zk://scheme:credentials@servers/path
-class URL
-{
-public:
- static Try<URL> parse(const std::string& url);
-
- static const char* scheme()
- {
- return "zk://";
- }
-
- const Option<Authentication> authentication;
- const std::string servers;
- const std::string path;
-
-private:
- URL(const std::string& _servers,
- const std::string& _path)
- : servers(_servers),
- path(_path) {}
-
- URL(const std::string& credentials,
- const std::string& _servers,
- const std::string& _path)
- : authentication(Authentication("digest", credentials)),
- servers(_servers),
- path(_path) {}
-};
-
-
-inline Try<URL> URL::parse(const std::string& url)
-{
- std::string s = strings::trim(url);
-
- if (!strings::startsWith(s, URL::scheme())) {
- return Error("Expecting 'zk://' at the beginning of the URL");
- }
- s = s.substr(5);
-
- // Look for the trailing '/' (if any), that's where the path starts.
- std::string path;
- do {
- size_t index = s.find_last_of('/');
-
- if (index == std::string::npos) {
- break;
- } else {
- path = s.substr(index) + path;
- s = s.substr(0, index);
- }
- } while (true);
-
- if (path == "") {
- path = "/";
- }
-
- // Look for the trailing '@' (if any), that's where servers starts.
- size_t index = s.find_last_of('@');
-
- if (index != std::string::npos) {
- return URL(s.substr(0, index), s.substr(index + 1), path);
- } else {
- return URL(s, path);
- }
-}
-
-inline std::ostream& operator<<(std::ostream& stream, const URL& url)
-{
- stream << URL::scheme();
- if (url.authentication.isSome()) {
- stream << url.authentication.get() << "@";
- }
- return stream << url.servers << url.path;
-}
-
-} // namespace zookeeper {
-
-#endif // __ZOOKEEPER_URL_HPP__