TestReconfigServer.cc 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  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 <sstream>
  19. #include <vector>
  20. #include <utility>
  21. #include <cppunit/extensions/HelperMacros.h>
  22. #include <unistd.h>
  23. #include "zookeeper.h"
  24. #include "Util.h"
  25. #include "ZooKeeperQuorumServer.h"
  26. #ifdef THREADED
  27. class TestReconfigServer : public CPPUNIT_NS::TestFixture {
  28. CPPUNIT_TEST_SUITE(TestReconfigServer);
  29. CPPUNIT_TEST(testNonIncremental);
  30. CPPUNIT_TEST(testRemoveConnectedFollower);
  31. CPPUNIT_TEST(testRemoveFollower);
  32. CPPUNIT_TEST(testReconfigFailureWithoutAuth);
  33. CPPUNIT_TEST(testReconfigFailureWithoutServerSuperuserPasswordConfigured);
  34. CPPUNIT_TEST_SUITE_END();
  35. public:
  36. TestReconfigServer();
  37. virtual ~TestReconfigServer();
  38. void setUp();
  39. void tearDown();
  40. void testNonIncremental();
  41. void testRemoveConnectedFollower();
  42. void testRemoveFollower();
  43. void testReconfigFailureWithoutAuth();
  44. void testReconfigFailureWithoutServerSuperuserPasswordConfigured();
  45. private:
  46. static const uint32_t NUM_SERVERS;
  47. FILE* logfile_;
  48. std::vector<ZooKeeperQuorumServer*> cluster_;
  49. std::size_t getLeader();
  50. std::vector<std::size_t> getFollowers();
  51. void parseConfig(char* buf, int len, std::vector<std::string>& servers,
  52. std::string& version);
  53. bool waitForConnected(zhandle_t* zh, uint32_t timeout_sec);
  54. zhandle_t* connectFollowers(std::vector<std::size_t> &followers);
  55. };
  56. const uint32_t TestReconfigServer::NUM_SERVERS = 3;
  57. TestReconfigServer::
  58. TestReconfigServer() :
  59. logfile_(openlogfile("TestReconfigServer")) {
  60. zoo_set_log_stream(logfile_);
  61. }
  62. TestReconfigServer::
  63. ~TestReconfigServer() {
  64. if (logfile_) {
  65. fflush(logfile_);
  66. fclose(logfile_);
  67. logfile_ = NULL;
  68. }
  69. }
  70. void TestReconfigServer::
  71. setUp() {
  72. ZooKeeperQuorumServer::tConfigPairs configs;
  73. configs.push_back(std::make_pair("reconfigEnabled", "true"));
  74. cluster_ = ZooKeeperQuorumServer::getCluster(NUM_SERVERS, configs,
  75. "SERVER_JVMFLAGS=-Dzookeeper.DigestAuthenticationProvider.superDigest=super:D/InIHSb7yEEbrWz8b9l71RjZJU="/* password is test */);
  76. }
  77. void TestReconfigServer::
  78. tearDown() {
  79. for (std::size_t i = 0; i < cluster_.size(); i++) {
  80. delete cluster_[i];
  81. }
  82. cluster_.clear();
  83. }
  84. std::size_t TestReconfigServer::
  85. getLeader() {
  86. for (std::size_t i = 0; i < cluster_.size(); i++) {
  87. if (cluster_[i]->isLeader()) {
  88. return i;
  89. }
  90. }
  91. return -1;
  92. }
  93. std::vector<std::size_t> TestReconfigServer::
  94. getFollowers() {
  95. std::vector<std::size_t> followers;
  96. for (std::size_t i = 0; i < cluster_.size(); i++) {
  97. if (cluster_[i]->isFollower()) {
  98. followers.push_back(i);
  99. }
  100. }
  101. return followers;
  102. }
  103. void TestReconfigServer::
  104. parseConfig(char* buf, int len, std::vector<std::string>& servers,
  105. std::string& version) {
  106. std::string config(buf, len);
  107. std::stringstream ss(config);
  108. std::string line;
  109. std::string serverPrefix("server.");
  110. std::string versionPrefix("version=");
  111. servers.clear();
  112. while(std::getline(ss, line, '\n')) {
  113. if (line.compare(0, serverPrefix.size(), serverPrefix) == 0) {
  114. servers.push_back(line);
  115. } else if (line.compare(0, versionPrefix.size(), versionPrefix) == 0) {
  116. version = line.substr(versionPrefix.size());
  117. }
  118. }
  119. }
  120. bool TestReconfigServer::
  121. waitForConnected(zhandle_t* zh, uint32_t timeout_sec) {
  122. for (uint32_t i = 0; i < timeout_sec; i++) {
  123. if (zoo_state(zh) == ZOO_CONNECTED_STATE) {
  124. return true;
  125. }
  126. sleep(1);
  127. }
  128. return false;
  129. }
  130. /**
  131. * 1. Connect to the leader.
  132. * 2. Remove a follower using incremental reconfig.
  133. * 3. Add the follower back using incremental reconfig.
  134. */
  135. void TestReconfigServer::
  136. testRemoveFollower() {
  137. std::vector<std::string> servers;
  138. std::string version;
  139. struct Stat stat;
  140. int len = 1024;
  141. char buf[len];
  142. // get config from leader.
  143. std::size_t leader = getLeader();
  144. CPPUNIT_ASSERT(leader >= 0);
  145. std::string host = cluster_[leader]->getHostPort();
  146. zhandle_t* zk = zookeeper_init(host.c_str(), NULL, 10000, NULL, NULL, 0);
  147. CPPUNIT_ASSERT_EQUAL(true, waitForConnected(zk, 10));
  148. CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_getconfig(zk, 0, buf, &len, &stat));
  149. CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_add_auth(zk, "digest", "super:test", 10, NULL,(void*)ZOK));
  150. // check if all the servers are listed in the config.
  151. parseConfig(buf, len, servers, version);
  152. // initially should be 1<<32, which is 0x100000000. This is the zxid
  153. // of the first NEWLEADER message, used as the initial version
  154. CPPUNIT_ASSERT_EQUAL(std::string("100000000"), version);
  155. CPPUNIT_ASSERT_EQUAL(NUM_SERVERS, (uint32_t)(servers.size()));
  156. for (std::size_t i = 0; i < cluster_.size(); i++) {
  157. CPPUNIT_ASSERT(std::find(servers.begin(), servers.end(),
  158. cluster_[i]->getServerString()) != servers.end());
  159. }
  160. // remove a follower.
  161. std::vector<std::size_t> followers = getFollowers();
  162. len = 1024;
  163. CPPUNIT_ASSERT_EQUAL(NUM_SERVERS - 1,
  164. (uint32_t)(followers.size()));
  165. std::stringstream ss;
  166. ss << followers[0];
  167. int rc = zoo_reconfig(zk, NULL, ss.str().c_str(), NULL, -1, buf, &len,
  168. &stat);
  169. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  170. parseConfig(buf, len, servers, version);
  171. CPPUNIT_ASSERT_EQUAL(std::string("100000002"), version);
  172. CPPUNIT_ASSERT_EQUAL(NUM_SERVERS - 1, (uint32_t)(servers.size()));
  173. for (std::size_t i = 0; i < cluster_.size(); i++) {
  174. if (i == followers[0]) {
  175. continue;
  176. }
  177. CPPUNIT_ASSERT(std::find(servers.begin(), servers.end(),
  178. cluster_[i]->getServerString()) != servers.end());
  179. }
  180. // add the follower back.
  181. len = 1024;
  182. std::string serverString = cluster_[followers[0]]->getServerString();
  183. rc = zoo_reconfig(zk, serverString.c_str(), NULL, NULL, -1, buf, &len,
  184. &stat);
  185. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  186. parseConfig(buf, len, servers, version);
  187. CPPUNIT_ASSERT_EQUAL(NUM_SERVERS, (uint32_t)(servers.size()));
  188. for (std::size_t i = 0; i < cluster_.size(); i++) {
  189. CPPUNIT_ASSERT(std::find(servers.begin(), servers.end(),
  190. cluster_[i]->getServerString()) != servers.end());
  191. }
  192. zookeeper_close(zk);
  193. }
  194. /**
  195. * 1. Connect to the leader.
  196. * 2. Remove a follower using non-incremental reconfig.
  197. * 3. Add the follower back using non-incremental reconfig.
  198. */
  199. void TestReconfigServer::
  200. testNonIncremental() {
  201. std::vector<std::string> servers;
  202. std::string version;
  203. struct Stat stat;
  204. int len = 1024;
  205. char buf[len];
  206. // get config from leader.
  207. std::size_t leader = getLeader();
  208. CPPUNIT_ASSERT(leader >= 0);
  209. std::string host = cluster_[leader]->getHostPort();
  210. zhandle_t* zk = zookeeper_init(host.c_str(), NULL, 10000, NULL, NULL, 0);
  211. CPPUNIT_ASSERT_EQUAL(true, waitForConnected(zk, 10));
  212. CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_getconfig(zk, 0, buf, &len, &stat));
  213. CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_add_auth(zk, "digest", "super:test", 10, NULL,(void*)ZOK));
  214. // check if all the servers are listed in the config.
  215. parseConfig(buf, len, servers, version);
  216. // initially should be 1<<32, which is 0x100000000. This is the zxid
  217. // of the first NEWLEADER message, used as the initial version
  218. CPPUNIT_ASSERT_EQUAL(std::string("100000000"), version);
  219. CPPUNIT_ASSERT_EQUAL(NUM_SERVERS, (uint32_t)(servers.size()));
  220. for (std::size_t i = 0; i < cluster_.size(); i++) {
  221. CPPUNIT_ASSERT(std::find(servers.begin(), servers.end(),
  222. cluster_[i]->getServerString()) != servers.end());
  223. }
  224. // remove a follower.
  225. std::vector<std::size_t> followers = getFollowers();
  226. len = 1024;
  227. CPPUNIT_ASSERT_EQUAL(NUM_SERVERS - 1,
  228. (uint32_t)(followers.size()));
  229. std::stringstream ss;
  230. for (std::size_t i = 1; i < followers.size(); i++) {
  231. ss << cluster_[followers[i]]->getServerString() << ",";
  232. }
  233. ss << cluster_[leader]->getServerString();
  234. int rc = zoo_reconfig(zk, NULL, NULL, ss.str().c_str(), -1, buf, &len,
  235. &stat);
  236. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  237. parseConfig(buf, len, servers, version);
  238. CPPUNIT_ASSERT_EQUAL(std::string("100000002"), version);
  239. CPPUNIT_ASSERT_EQUAL(NUM_SERVERS - 1, (uint32_t)(servers.size()));
  240. for (std::size_t i = 0; i < cluster_.size(); i++) {
  241. if (i == followers[0]) {
  242. continue;
  243. }
  244. CPPUNIT_ASSERT(std::find(servers.begin(), servers.end(),
  245. cluster_[i]->getServerString()) != servers.end());
  246. }
  247. // add the follower back.
  248. len = 1024;
  249. ss.str("");
  250. for (std::size_t i = 0; i < cluster_.size(); i++) {
  251. ss << cluster_[i]->getServerString() << ",";
  252. }
  253. rc = zoo_reconfig(zk, NULL, NULL, ss.str().c_str(), -1, buf, &len,
  254. &stat);
  255. CPPUNIT_ASSERT_EQUAL((int)ZOK, rc);
  256. parseConfig(buf, len, servers, version);
  257. CPPUNIT_ASSERT_EQUAL(NUM_SERVERS, (uint32_t)(servers.size()));
  258. for (std::size_t i = 0; i < cluster_.size(); i++) {
  259. CPPUNIT_ASSERT(std::find(servers.begin(), servers.end(),
  260. cluster_[i]->getServerString()) != servers.end());
  261. }
  262. zookeeper_close(zk);
  263. }
  264. zhandle_t* TestReconfigServer::
  265. connectFollowers(std::vector<std::size_t> &followers) {
  266. std::stringstream ss;
  267. std::size_t leader = getLeader();
  268. CPPUNIT_ASSERT(leader >= 0);
  269. CPPUNIT_ASSERT_EQUAL(NUM_SERVERS - 1, (uint32_t)(followers.size()));
  270. for (std::size_t i = 0; i < followers.size(); i++) {
  271. ss << cluster_[followers[i]]->getHostPort() << ",";
  272. }
  273. ss << cluster_[leader]->getHostPort();
  274. std::string hosts = ss.str().c_str();
  275. zoo_deterministic_conn_order(true);
  276. zhandle_t* zk = zookeeper_init(hosts.c_str(), NULL, 10000, NULL, NULL, 0);
  277. CPPUNIT_ASSERT_EQUAL(true, waitForConnected(zk, 10));
  278. std::string connectedHost(zoo_get_current_server(zk));
  279. std::string portString = connectedHost.substr(connectedHost.find(":") + 1);
  280. uint32_t port;
  281. std::istringstream (portString) >> port;
  282. CPPUNIT_ASSERT_EQUAL(cluster_[followers[0]]->getClientPort(), port);
  283. return zk;
  284. }
  285. /**
  286. * 1. Connect to a follower.
  287. * 2. Remove the follower the client is connected to.
  288. */
  289. void TestReconfigServer::
  290. testRemoveConnectedFollower() {
  291. std::vector<std::string> servers;
  292. std::string version;
  293. struct Stat stat;
  294. int len = 1024;
  295. char buf[len];
  296. // connect to a follower.
  297. std::stringstream ss;
  298. std::vector<std::size_t> followers = getFollowers();
  299. zhandle_t* zk = connectFollowers(followers);
  300. CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_add_auth(zk, "digest", "super:test", 10, NULL,(void*)ZOK));
  301. // remove the follower.
  302. len = 1024;
  303. ss.str("");
  304. ss << followers[0];
  305. zoo_reconfig(zk, NULL, ss.str().c_str(), NULL, -1, buf, &len, &stat);
  306. CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_getconfig(zk, 0, buf, &len, &stat));
  307. parseConfig(buf, len, servers, version);
  308. CPPUNIT_ASSERT_EQUAL(NUM_SERVERS - 1, (uint32_t)(servers.size()));
  309. for (std::size_t i = 0; i < cluster_.size(); i++) {
  310. if (i == followers[0]) {
  311. continue;
  312. }
  313. CPPUNIT_ASSERT(std::find(servers.begin(), servers.end(),
  314. cluster_[i]->getServerString()) != servers.end());
  315. }
  316. zookeeper_close(zk);
  317. }
  318. /**
  319. * ZOOKEEPER-2014: only admin or users who are explicitly granted permission can do reconfig.
  320. */
  321. void TestReconfigServer::
  322. testReconfigFailureWithoutAuth() {
  323. std::vector<std::string> servers;
  324. std::string version;
  325. struct Stat stat;
  326. int len = 1024;
  327. char buf[len];
  328. // connect to a follower.
  329. std::stringstream ss;
  330. std::vector<std::size_t> followers = getFollowers();
  331. zhandle_t* zk = connectFollowers(followers);
  332. // remove the follower.
  333. len = 1024;
  334. ss.str("");
  335. ss << followers[0];
  336. // No auth, should fail.
  337. CPPUNIT_ASSERT_EQUAL((int)ZNOAUTH, zoo_reconfig(zk, NULL, ss.str().c_str(), NULL, -1, buf, &len, &stat));
  338. // Wrong auth, should fail.
  339. CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_add_auth(zk, "digest", "super:wrong", 11, NULL,(void*)ZOK));
  340. CPPUNIT_ASSERT_EQUAL((int)ZNOAUTH, zoo_reconfig(zk, NULL, ss.str().c_str(), NULL, -1, buf, &len, &stat));
  341. // Right auth, should pass.
  342. CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_add_auth(zk, "digest", "super:test", 10, NULL,(void*)ZOK));
  343. CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_reconfig(zk, NULL, ss.str().c_str(), NULL, -1, buf, &len, &stat));
  344. CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_getconfig(zk, 0, buf, &len, &stat));
  345. parseConfig(buf, len, servers, version);
  346. CPPUNIT_ASSERT_EQUAL(NUM_SERVERS - 1, (uint32_t)(servers.size()));
  347. for (std::size_t i = 0; i < cluster_.size(); i++) {
  348. if (i == followers[0]) {
  349. continue;
  350. }
  351. CPPUNIT_ASSERT(std::find(servers.begin(), servers.end(),
  352. cluster_[i]->getServerString()) != servers.end());
  353. }
  354. zookeeper_close(zk);
  355. }
  356. void TestReconfigServer::
  357. testReconfigFailureWithoutServerSuperuserPasswordConfigured() {
  358. std::vector<std::string> servers;
  359. std::string version;
  360. struct Stat stat;
  361. int len = 1024;
  362. char buf[len];
  363. // Create a new quorum with the super user's password not configured.
  364. tearDown();
  365. ZooKeeperQuorumServer::tConfigPairs configs;
  366. configs.push_back(std::make_pair("reconfigEnabled", "true"));
  367. cluster_ = ZooKeeperQuorumServer::getCluster(NUM_SERVERS, configs, "");
  368. // connect to a follower.
  369. std::stringstream ss;
  370. std::vector<std::size_t> followers = getFollowers();
  371. zhandle_t* zk = connectFollowers(followers);
  372. // remove the follower.
  373. len = 1024;
  374. ss.str("");
  375. ss << followers[0];
  376. // All cases should fail as server ensemble was not configured with the super user's password.
  377. CPPUNIT_ASSERT_EQUAL((int)ZNOAUTH, zoo_reconfig(zk, NULL, ss.str().c_str(), NULL, -1, buf, &len, &stat));
  378. CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_add_auth(zk, "digest", "super:", 11, NULL,(void*)ZOK));
  379. CPPUNIT_ASSERT_EQUAL((int)ZNOAUTH, zoo_reconfig(zk, NULL, ss.str().c_str(), NULL, -1, buf, &len, &stat));
  380. CPPUNIT_ASSERT_EQUAL((int)ZOK, zoo_add_auth(zk, "digest", "super:test", 10, NULL,(void*)ZOK));
  381. CPPUNIT_ASSERT_EQUAL((int)ZNOAUTH, zoo_reconfig(zk, NULL, ss.str().c_str(), NULL, -1, buf, &len, &stat));
  382. zookeeper_close(zk);
  383. }
  384. CPPUNIT_TEST_SUITE_REGISTRATION(TestReconfigServer);
  385. #endif