You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@mesos.apache.org by bm...@apache.org on 2013/12/13 01:08:51 UTC
[3/3] git commit: Fixed the zookeeper client wrappers to use the
negotiated session timeout value as their local reconnect timeout.
Fixed the zookeeper client wrappers to use the negotiated
session timeout value as their local reconnect timeout.
From: Jiang Yan Xu <ya...@jxu.me>
Review: https://reviews.apache.org/r/16111
Project: http://git-wip-us.apache.org/repos/asf/mesos/repo
Commit: http://git-wip-us.apache.org/repos/asf/mesos/commit/6e16cf93
Tree: http://git-wip-us.apache.org/repos/asf/mesos/tree/6e16cf93
Diff: http://git-wip-us.apache.org/repos/asf/mesos/diff/6e16cf93
Branch: refs/heads/master
Commit: 6e16cf939aa9098ba6cc25d4d2ce0bc64d3e6e49
Parents: e7cfdc6
Author: Benjamin Mahler <bm...@twitter.com>
Authored: Thu Dec 12 15:44:47 2013 -0800
Committer: Benjamin Mahler <bm...@twitter.com>
Committed: Thu Dec 12 15:44:47 2013 -0800
----------------------------------------------------------------------
src/jvm/org/apache/zookeeper.hpp | 42 ++++++++++++++++++++++++++++++++
src/state/zookeeper.hpp | 3 +++
src/tests/zookeeper_test_server.cpp | 28 +++++++++++++++++++++
src/tests/zookeeper_test_server.hpp | 6 +++++
src/tests/zookeeper_tests.cpp | 24 ++++++++++++++++++
src/zookeeper/group.cpp | 7 +++++-
src/zookeeper/group.hpp | 3 +++
src/zookeeper/zookeeper.cpp | 12 +++++++--
src/zookeeper/zookeeper.hpp | 8 ++++++
9 files changed, 130 insertions(+), 3 deletions(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/mesos/blob/6e16cf93/src/jvm/org/apache/zookeeper.hpp
----------------------------------------------------------------------
diff --git a/src/jvm/org/apache/zookeeper.hpp b/src/jvm/org/apache/zookeeper.hpp
index dac1456..2b78a25 100644
--- a/src/jvm/org/apache/zookeeper.hpp
+++ b/src/jvm/org/apache/zookeeper.hpp
@@ -80,6 +80,48 @@ public:
constructor, (jobject) txnLogFactory, (jobject) treeBuilder);
}
+ void setMaxSessionTimeout(int max)
+ {
+ static Jvm::Method method = Jvm::get()->findMethod(
+ Jvm::Class::named("org/apache/zookeeper/server/ZooKeeperServer")
+ .method("setMaxSessionTimeout")
+ .parameter(Jvm::get()->intClass)
+ .returns(Jvm::get()->voidClass));
+
+ Jvm::get()->invoke<void>(object, method, max);
+ }
+
+ void setMinSessionTimeout(int min)
+ {
+ static Jvm::Method method = Jvm::get()->findMethod(
+ Jvm::Class::named("org/apache/zookeeper/server/ZooKeeperServer")
+ .method("setMinSessionTimeout")
+ .parameter(Jvm::get()->intClass)
+ .returns(Jvm::get()->voidClass));
+
+ Jvm::get()->invoke<void>(object, method, min);
+ }
+
+ int getMaxSessionTimeout()
+ {
+ static Jvm::Method method = Jvm::get()->findMethod(
+ Jvm::Class::named("org/apache/zookeeper/server/ZooKeeperServer")
+ .method("getMaxSessionTimeout")
+ .returns(Jvm::get()->intClass));
+
+ return Jvm::get()->invoke<int>(object, method);
+ }
+
+ int getMinSessionTimeout()
+ {
+ static Jvm::Method method = Jvm::get()->findMethod(
+ Jvm::Class::named("org/apache/zookeeper/server/ZooKeeperServer")
+ .method("getMinSessionTimeout")
+ .returns(Jvm::get()->intClass));
+
+ return Jvm::get()->invoke<int>(object, method);
+ }
+
int getClientPort()
{
static Jvm::Method method = Jvm::get()->findMethod(
http://git-wip-us.apache.org/repos/asf/mesos/blob/6e16cf93/src/state/zookeeper.hpp
----------------------------------------------------------------------
diff --git a/src/state/zookeeper.hpp b/src/state/zookeeper.hpp
index 90b6607..d1d1fed 100644
--- a/src/state/zookeeper.hpp
+++ b/src/state/zookeeper.hpp
@@ -88,7 +88,10 @@ private:
Result<bool> doExpunge(const Entry& entry);
const std::string servers;
+
+ // The session timeout requested by the client.
const Duration timeout;
+
const std::string znode;
Option<zookeeper::Authentication> auth; // ZooKeeper authentication.
http://git-wip-us.apache.org/repos/asf/mesos/blob/6e16cf93/src/tests/zookeeper_test_server.cpp
----------------------------------------------------------------------
diff --git a/src/tests/zookeeper_test_server.cpp b/src/tests/zookeeper_test_server.cpp
index dc53d6a..62b4e0b 100644
--- a/src/tests/zookeeper_test_server.cpp
+++ b/src/tests/zookeeper_test_server.cpp
@@ -77,6 +77,34 @@ void ZooKeeperTestServer::expireSession(int64_t sessionId)
}
+void ZooKeeperTestServer::setMinSessionTimeout(const Duration& min)
+{
+ // ZooKeeper server uses int representation of milliseconds for
+ // session timeouts.
+ // See http://zookeeper.apache.org/doc/trunk/zookeeperAdmin.html
+ zooKeeperServer->setMinSessionTimeout(static_cast<int>(min.ms()));
+}
+
+
+void ZooKeeperTestServer::setMaxSessionTimeout(const Duration& max)
+{
+ // See the comment for setMinSessionTimeout.
+ zooKeeperServer->setMaxSessionTimeout(static_cast<int>(max.ms()));
+}
+
+
+Duration ZooKeeperTestServer::getMaxSessionTimeout() const
+{
+ return Milliseconds(zooKeeperServer->getMaxSessionTimeout());
+}
+
+
+Duration ZooKeeperTestServer::getMinSessionTimeout() const
+{
+ return Milliseconds(zooKeeperServer->getMinSessionTimeout());
+}
+
+
std::string ZooKeeperTestServer::connectString() const
{
CHECK(port > 0) << "Illegal state, must call startNetwork first!";
http://git-wip-us.apache.org/repos/asf/mesos/blob/6e16cf93/src/tests/zookeeper_test_server.hpp
----------------------------------------------------------------------
diff --git a/src/tests/zookeeper_test_server.hpp b/src/tests/zookeeper_test_server.hpp
index 97a8524..e612621 100644
--- a/src/tests/zookeeper_test_server.hpp
+++ b/src/tests/zookeeper_test_server.hpp
@@ -66,6 +66,12 @@ public:
// clients with the same session id.
void expireSession(int64_t sessionId);
+ // Sets/gets the min/max session timeouts enforced by the server.
+ void setMinSessionTimeout(const Duration& min);
+ void setMaxSessionTimeout(const Duration& max);
+ Duration getMinSessionTimeout() const;
+ Duration getMaxSessionTimeout() const;
+
private:
org::apache::zookeeper::server::ZooKeeperServer* zooKeeperServer;
org::apache::zookeeper::server::NIOServerCnxn::Factory* connectionFactory;
http://git-wip-us.apache.org/repos/asf/mesos/blob/6e16cf93/src/tests/zookeeper_tests.cpp
----------------------------------------------------------------------
diff --git a/src/tests/zookeeper_tests.cpp b/src/tests/zookeeper_tests.cpp
index a5fe9e1..a0660cb 100644
--- a/src/tests/zookeeper_tests.cpp
+++ b/src/tests/zookeeper_tests.cpp
@@ -68,6 +68,30 @@ TEST_F(ZooKeeperTest, Auth)
}
+TEST_F(ZooKeeperTest, SessionTimeoutNegotiation)
+{
+ server->setMinSessionTimeout(Seconds(8));
+ server->setMaxSessionTimeout(Seconds(20));
+ EXPECT_EQ(Seconds(8), server->getMinSessionTimeout());
+ EXPECT_EQ(Seconds(20), server->getMaxSessionTimeout());
+
+ ZooKeeperTest::TestWatcher watcher;
+ ZooKeeper zk1(server->connectString(), Seconds(7), &watcher);
+ watcher.awaitSessionEvent(ZOO_CONNECTED_STATE);
+
+ // The requested timeout is less than server's min value so the
+ // negotiated result is the sever's min value.
+ EXPECT_EQ(Seconds(8), zk1.getSessionTimeout());
+
+ ZooKeeper zk2(server->connectString(), Seconds(22), &watcher);
+ watcher.awaitSessionEvent(ZOO_CONNECTED_STATE);
+
+ // The requested timeout is greater than server's max value so the
+ // negotiated result is the sever's max value.
+ EXPECT_EQ(Seconds(20), zk2.getSessionTimeout());
+}
+
+
TEST_F(ZooKeeperTest, Create)
{
ZooKeeperTest::TestWatcher watcher;
http://git-wip-us.apache.org/repos/asf/mesos/blob/6e16cf93/src/zookeeper/group.cpp
----------------------------------------------------------------------
diff --git a/src/zookeeper/group.cpp b/src/zookeeper/group.cpp
index 5c92c5f..f3ba86f 100644
--- a/src/zookeeper/group.cpp
+++ b/src/zookeeper/group.cpp
@@ -376,7 +376,12 @@ void GroupProcess::reconnecting()
// we haven't reconnected within the session expiration time out.
// The timer can be reset if the connection is restored.
CHECK(timer.isNone());
- timer = delay(timeout, self(), &Self::timedout, zk->getSessionId());
+
+ // Use the negotiated session timeout for the reconnect timer.
+ timer = delay(zk->getSessionTimeout(),
+ self(),
+ &Self::timedout,
+ zk->getSessionId());
}
http://git-wip-us.apache.org/repos/asf/mesos/blob/6e16cf93/src/zookeeper/group.hpp
----------------------------------------------------------------------
diff --git a/src/zookeeper/group.hpp b/src/zookeeper/group.hpp
index facfb1f..088363a 100644
--- a/src/zookeeper/group.hpp
+++ b/src/zookeeper/group.hpp
@@ -206,7 +206,10 @@ private:
void timedout(const int64_t& sessionId);
const std::string servers;
+
+ // The session timeout requested by the client.
const Duration timeout;
+
const std::string znode;
Option<Authentication> auth; // ZooKeeper authentication.
http://git-wip-us.apache.org/repos/asf/mesos/blob/6e16cf93/src/zookeeper/zookeeper.cpp
----------------------------------------------------------------------
diff --git a/src/zookeeper/zookeeper.cpp b/src/zookeeper/zookeeper.cpp
index cc8a7ca..5720f4c 100644
--- a/src/zookeeper/zookeeper.cpp
+++ b/src/zookeeper/zookeeper.cpp
@@ -165,7 +165,6 @@ public:
const Duration& timeout,
Watcher* watcher)
: servers(servers),
- timeout(timeout),
zk(zk),
watcher(watcher)
{
@@ -486,7 +485,6 @@ private:
friend class ZooKeeper;
const string servers; // ZooKeeper host:port pairs.
- const Duration timeout; // ZooKeeper session timeout.
ZooKeeper* zk; // ZooKeeper instance.
zhandle_t* zh; // ZooKeeper connection handle.
@@ -522,6 +520,16 @@ int64_t ZooKeeper::getSessionId()
}
+Duration ZooKeeper::getSessionTimeout() const
+{
+ // ZooKeeper server uses int representation of milliseconds for
+ // session timeouts.
+ // See:
+ // http://zookeeper.apache.org/doc/trunk/zookeeperProgrammers.html
+ return Milliseconds(zoo_recv_timeout(impl->zh));
+}
+
+
int ZooKeeper::authenticate(const string& scheme, const string& credentials)
{
return impl->authenticate(scheme, credentials).get();
http://git-wip-us.apache.org/repos/asf/mesos/blob/6e16cf93/src/zookeeper/zookeeper.hpp
----------------------------------------------------------------------
diff --git a/src/zookeeper/zookeeper.hpp b/src/zookeeper/zookeeper.hpp
index 7243543..1b4e2ed 100644
--- a/src/zookeeper/zookeeper.hpp
+++ b/src/zookeeper/zookeeper.hpp
@@ -141,6 +141,14 @@ public:
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.
+ */
+ Duration getSessionTimeout() const;
+
+ /**
* \brief authenticate synchronously.
*/
int authenticate(const std::string& scheme, const std::string& credentials);