TestReconfigServer.cc 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with this
  4. * work for additional information regarding copyright ownership. The ASF
  5. * licenses this file to you under the Apache License, Version 2.0 (the
  6. * "License"); you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  13. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
  14. * License for the specific language governing permissions and limitations under
  15. * the License.
  16. */
  17. #include <algorithm>
  18. #include <cppunit/extensions/HelperMacros.h>
  19. #include "zookeeper.h"
  20. #include "Util.h"
  21. #include "ZooKeeperQuorumServer.h"
  22. class TestReconfigServer : public CPPUNIT_NS::TestFixture {
  23. CPPUNIT_TEST_SUITE(TestReconfigServer);
  24. #ifdef THREADED
  25. CPPUNIT_TEST(testNonIncremental);
  26. CPPUNIT_TEST(testRemoveConnectedFollower);
  27. CPPUNIT_TEST(testRemoveFollower);
  28. #endif
  29. CPPUNIT_TEST_SUITE_END();
  30. public:
  31. TestReconfigServer();
  32. virtual ~TestReconfigServer();
  33. void setUp();
  34. void tearDown();
  35. void testNonIncremental();
  36. void testRemoveConnectedFollower();
  37. void testRemoveFollower();
  38. private:
  39. static const uint32_t NUM_SERVERS;
  40. FILE* logfile_;
  41. std::vector<ZooKeeperQuorumServer*> cluster_;
  42. int32_t getLeader();
  43. std::vector<int32_t> getFollowers();
  44. void parseConfig(char* buf, int len, std::vector<std::string>& servers,
  45. std::string& version);
  46. };
  47. const uint32_t TestReconfigServer::NUM_SERVERS = 3;
  48. TestReconfigServer::
  49. TestReconfigServer() :
  50. logfile_(openlogfile("TestReconfigServer")) {
  51. zoo_set_log_stream(logfile_);
  52. }
  53. TestReconfigServer::
  54. ~TestReconfigServer() {
  55. if (logfile_) {
  56. fflush(logfile_);
  57. fclose(logfile_);
  58. logfile_ = NULL;
  59. }
  60. }
  61. void TestReconfigServer::
  62. setUp() {
  63. cluster_ = ZooKeeperQuorumServer::getCluster(NUM_SERVERS);
  64. // give the cluster some time to start up.
  65. sleep(2);
  66. }
  67. void TestReconfigServer::
  68. tearDown() {
  69. for (int i = 0; i < cluster_.size(); i++) {
  70. delete cluster_[i];
  71. }
  72. cluster_.clear();
  73. }
  74. int32_t TestReconfigServer::
  75. getLeader() {
  76. for (int32_t i = 0; i < cluster_.size(); i++) {
  77. if (cluster_[i]->isLeader()) {
  78. return i;
  79. }
  80. }
  81. return -1;
  82. }
  83. std::vector<int32_t> TestReconfigServer::
  84. getFollowers() {
  85. std::vector<int32_t> followers;
  86. for (int32_t i = 0; i < cluster_.size(); i++) {
  87. if (cluster_[i]->isFollower()) {
  88. followers.push_back(i);
  89. }
  90. }
  91. return followers;
  92. }
  93. void TestReconfigServer::
  94. parseConfig(char* buf, int len, std::vector<std::string>& servers,
  95. std::string& version) {
  96. std::string config(buf, len);
  97. std::stringstream ss(config);
  98. std::string line;
  99. std::string serverPrefix("server.");
  100. std::string versionPrefix("version=");
  101. servers.clear();
  102. while(std::getline(ss, line, '\n')) {
  103. if (line.compare(0, serverPrefix.size(), serverPrefix) == 0) {
  104. servers.push_back(line);
  105. } else if (line.compare(0, versionPrefix.size(), versionPrefix) == 0) {
  106. version = line.substr(versionPrefix.size());
  107. }
  108. }
  109. }
  110. /**
  111. * 1. Connect to the leader.
  112. * 2. Remove a follower using incremental reconfig.
  113. * 3. Add the follower back using incremental reconfig.
  114. */
  115. void TestReconfigServer::
  116. testRemoveFollower() {
  117. std::vector<std::string> servers;
  118. std::string version;
  119. struct Stat stat;
  120. int len = 1024;
  121. char buf[len];
  122. // get config from leader.
  123. int32_t leader = getLeader();
  124. CPPUNIT_ASSERT(leader >= 0);
  125. std::string host = cluster_[leader]->getHostPort();
  126. zhandle_t* zk = zookeeper_init(host.c_str(), NULL, 10000, NULL, NULL, 0);
  127. CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_getconfig(zk, 0, buf, &len, &stat));
  128. // check if all the servers are listed in the config.
  129. parseConfig(buf, len, servers, version);
  130. CPPUNIT_ASSERT_EQUAL(std::string("0"), version);
  131. CPPUNIT_ASSERT_EQUAL(NUM_SERVERS, (uint32_t)(servers.size()));
  132. for (int i = 0; i < cluster_.size(); i++) {
  133. CPPUNIT_ASSERT(std::find(servers.begin(), servers.end(),
  134. cluster_[i]->getServerString()) != servers.end());
  135. }
  136. // remove a follower.
  137. std::vector<int32_t> followers = getFollowers();
  138. len = 1024;
  139. CPPUNIT_ASSERT_EQUAL(NUM_SERVERS - 1,
  140. (uint32_t)(followers.size()));
  141. std::stringstream ss;
  142. ss << followers[0];
  143. int rc = zoo_reconfig(zk, NULL, ss.str().c_str(), NULL, -1, buf, &len,
  144. &stat);
  145. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  146. parseConfig(buf, len, servers, version);
  147. CPPUNIT_ASSERT_EQUAL(std::string("100000002"), version);
  148. CPPUNIT_ASSERT_EQUAL(NUM_SERVERS - 1, (uint32_t)(servers.size()));
  149. for (int i = 0; i < cluster_.size(); i++) {
  150. if (i == followers[0]) {
  151. continue;
  152. }
  153. CPPUNIT_ASSERT(std::find(servers.begin(), servers.end(),
  154. cluster_[i]->getServerString()) != servers.end());
  155. }
  156. // add the follower back.
  157. len = 1024;
  158. std::string serverString = cluster_[followers[0]]->getServerString();
  159. rc = zoo_reconfig(zk, serverString.c_str(), NULL, NULL, -1, buf, &len,
  160. &stat);
  161. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  162. parseConfig(buf, len, servers, version);
  163. CPPUNIT_ASSERT_EQUAL(NUM_SERVERS, (uint32_t)(servers.size()));
  164. for (int i = 0; i < cluster_.size(); i++) {
  165. CPPUNIT_ASSERT(std::find(servers.begin(), servers.end(),
  166. cluster_[i]->getServerString()) != servers.end());
  167. }
  168. zookeeper_close(zk);
  169. }
  170. /**
  171. * 1. Connect to the leader.
  172. * 2. Remove a follower using non-incremental reconfig.
  173. * 3. Add the follower back using non-incremental reconfig.
  174. */
  175. void TestReconfigServer::
  176. testNonIncremental() {
  177. std::vector<std::string> servers;
  178. std::string version;
  179. struct Stat stat;
  180. int len = 1024;
  181. char buf[len];
  182. // get config from leader.
  183. int32_t leader = getLeader();
  184. CPPUNIT_ASSERT(leader >= 0);
  185. std::string host = cluster_[leader]->getHostPort();
  186. zhandle_t* zk = zookeeper_init(host.c_str(), NULL, 10000, NULL, NULL, 0);
  187. CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_getconfig(zk, 0, buf, &len, &stat));
  188. // check if all the servers are listed in the config.
  189. parseConfig(buf, len, servers, version);
  190. CPPUNIT_ASSERT_EQUAL(std::string("0"), version);
  191. CPPUNIT_ASSERT_EQUAL(NUM_SERVERS, (uint32_t)(servers.size()));
  192. for (int i = 0; i < cluster_.size(); i++) {
  193. CPPUNIT_ASSERT(std::find(servers.begin(), servers.end(),
  194. cluster_[i]->getServerString()) != servers.end());
  195. }
  196. // remove a follower.
  197. std::vector<int32_t> followers = getFollowers();
  198. len = 1024;
  199. CPPUNIT_ASSERT_EQUAL(NUM_SERVERS - 1,
  200. (uint32_t)(followers.size()));
  201. std::stringstream ss;
  202. for (int i = 1; i < followers.size(); i++) {
  203. ss << cluster_[followers[i]]->getServerString() << ",";
  204. }
  205. ss << cluster_[leader]->getServerString();
  206. int rc = zoo_reconfig(zk, NULL, NULL, ss.str().c_str(), -1, buf, &len,
  207. &stat);
  208. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  209. parseConfig(buf, len, servers, version);
  210. CPPUNIT_ASSERT_EQUAL(std::string("100000002"), version);
  211. CPPUNIT_ASSERT_EQUAL(NUM_SERVERS - 1, (uint32_t)(servers.size()));
  212. for (int i = 0; i < cluster_.size(); i++) {
  213. if (i == followers[0]) {
  214. continue;
  215. }
  216. CPPUNIT_ASSERT(std::find(servers.begin(), servers.end(),
  217. cluster_[i]->getServerString()) != servers.end());
  218. }
  219. // add the follower back.
  220. len = 1024;
  221. ss.str("");
  222. for (int i = 0; i < cluster_.size(); i++) {
  223. ss << cluster_[i]->getServerString() << ",";
  224. }
  225. rc = zoo_reconfig(zk, NULL, NULL, ss.str().c_str(), -1, buf, &len,
  226. &stat);
  227. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  228. parseConfig(buf, len, servers, version);
  229. CPPUNIT_ASSERT_EQUAL(NUM_SERVERS, (uint32_t)(servers.size()));
  230. for (int i = 0; i < cluster_.size(); i++) {
  231. CPPUNIT_ASSERT(std::find(servers.begin(), servers.end(),
  232. cluster_[i]->getServerString()) != servers.end());
  233. }
  234. zookeeper_close(zk);
  235. }
  236. /**
  237. * 1. Connect to a follower.
  238. * 2. Remove the follower the client is connected to.
  239. */
  240. void TestReconfigServer::
  241. testRemoveConnectedFollower() {
  242. std::vector<std::string> servers;
  243. std::string version;
  244. struct Stat stat;
  245. int len = 1024;
  246. char buf[len];
  247. // connect to a follower.
  248. int32_t leader = getLeader();
  249. std::vector<int32_t> followers = getFollowers();
  250. CPPUNIT_ASSERT(leader >= 0);
  251. CPPUNIT_ASSERT_EQUAL(NUM_SERVERS - 1, (uint32_t)(followers.size()));
  252. std::stringstream ss;
  253. for (int i = 0; i < followers.size(); i++) {
  254. ss << cluster_[followers[i]]->getHostPort() << ",";
  255. }
  256. ss << cluster_[leader]->getHostPort();
  257. std::string hosts = ss.str().c_str();
  258. zoo_deterministic_conn_order(true);
  259. zhandle_t* zk = zookeeper_init(hosts.c_str(), NULL, 10000, NULL, NULL, 0);
  260. std::string connectedHost(zoo_get_current_server(zk));
  261. std::string portString = connectedHost.substr(connectedHost.find(":") + 1);
  262. uint32_t port;
  263. std::istringstream (portString) >> port;
  264. CPPUNIT_ASSERT_EQUAL(cluster_[followers[0]]->getClientPort(), port);
  265. // remove the follower.
  266. len = 1024;
  267. ss.str("");
  268. ss << followers[0];
  269. zoo_reconfig(zk, NULL, ss.str().c_str(), NULL, -1, buf, &len, &stat);
  270. CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_getconfig(zk, 0, buf, &len, &stat));
  271. parseConfig(buf, len, servers, version);
  272. CPPUNIT_ASSERT_EQUAL(NUM_SERVERS - 1, (uint32_t)(servers.size()));
  273. for (int i = 0; i < cluster_.size(); i++) {
  274. if (i == followers[0]) {
  275. continue;
  276. }
  277. CPPUNIT_ASSERT(std::find(servers.begin(), servers.end(),
  278. cluster_[i]->getServerString()) != servers.end());
  279. }
  280. }
  281. CPPUNIT_TEST_SUITE_REGISTRATION(TestReconfigServer);