PageRenderTime 57ms CodeModel.GetById 27ms RepoModel.GetById 0ms app.codeStats 0ms

/bookkeeper-server/src/test/java/org/apache/bookkeeper/client/TestFencing.java

https://gitlab.com/math4youbyusgroupillinois/bookkeeper
Java | 398 lines | 247 code | 62 blank | 89 comment | 16 complexity | 2db9d6a838c8206cb538c4e6ecab88ab MD5 | raw file
Possible License(s): Apache-2.0
  1. package org.apache.bookkeeper.client;
  2. /*
  3. *
  4. * Licensed to the Apache Software Foundation (ASF) under one
  5. * or more contributor license agreements. See the NOTICE file
  6. * distributed with this work for additional information
  7. * regarding copyright ownership. The ASF licenses this file
  8. * to you under the Apache License, Version 2.0 (the
  9. * "License"); you may not use this file except in compliance
  10. * with the License. You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing,
  15. * software distributed under the License is distributed on an
  16. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  17. * KIND, either express or implied. See the License for the
  18. * specific language governing permissions and limitations
  19. * under the License.
  20. *
  21. */
  22. import org.junit.*;
  23. import java.net.InetSocketAddress;
  24. import java.util.Enumeration;
  25. import java.util.concurrent.CyclicBarrier;
  26. import java.util.concurrent.CountDownLatch;
  27. import org.apache.bookkeeper.conf.ClientConfiguration;
  28. import org.apache.bookkeeper.client.LedgerHandle;
  29. import org.apache.bookkeeper.client.LedgerEntry;
  30. import org.apache.bookkeeper.client.BookKeeper;
  31. import org.apache.bookkeeper.client.BookKeeperAdmin;
  32. import org.apache.bookkeeper.client.BKException;
  33. import org.apache.bookkeeper.client.BookKeeper.DigestType;
  34. import org.apache.bookkeeper.test.BaseTestCase;
  35. import org.slf4j.Logger;
  36. import org.slf4j.LoggerFactory;
  37. /**
  38. * This unit test tests ledger fencing;
  39. *
  40. */
  41. public class TestFencing extends BaseTestCase {
  42. static Logger LOG = LoggerFactory.getLogger(TestFencing.class);
  43. DigestType digestType;
  44. public TestFencing(DigestType digestType) {
  45. super(10);
  46. this.digestType = digestType;
  47. }
  48. /**
  49. * Basic fencing test. Create ledger, write to it,
  50. * open ledger, write again (should fail).
  51. */
  52. @Test
  53. public void testBasicFencing() throws Exception {
  54. /*
  55. * Create ledger.
  56. */
  57. LedgerHandle writelh = null;
  58. writelh = bkc.createLedger(digestType, "password".getBytes());
  59. String tmp = "BookKeeper is cool!";
  60. for (int i = 0; i < 10; i++) {
  61. writelh.addEntry(tmp.getBytes());
  62. }
  63. /*
  64. * Try to open ledger.
  65. */
  66. LedgerHandle readlh = bkc.openLedger(writelh.getId(), digestType, "password".getBytes());
  67. // should have triggered recovery and fencing
  68. try {
  69. writelh.addEntry(tmp.getBytes());
  70. LOG.error("Should have thrown an exception");
  71. fail("Should have thrown an exception when trying to write");
  72. } catch (BKException.BKLedgerFencedException e) {
  73. // correct behaviour
  74. }
  75. /*
  76. * Check if has recovered properly.
  77. */
  78. assertTrue("Has not recovered correctly: " + readlh.getLastAddConfirmed()
  79. + " original " + writelh.getLastAddConfirmed(),
  80. readlh.getLastAddConfirmed() == writelh.getLastAddConfirmed());
  81. }
  82. private static int threadCount = 0;
  83. class LedgerOpenThread extends Thread {
  84. private final long ledgerId;
  85. private long lastConfirmedEntry = 0;
  86. private final DigestType digestType;
  87. private final CyclicBarrier barrier;
  88. LedgerOpenThread (DigestType digestType, long ledgerId, CyclicBarrier barrier)
  89. throws Exception {
  90. super("TestFencing-LedgerOpenThread-" + threadCount++);
  91. this.ledgerId = ledgerId;
  92. this.digestType = digestType;
  93. this.barrier = barrier;
  94. }
  95. @Override
  96. public void run() {
  97. LedgerHandle lh = null;
  98. BookKeeper bk = null;
  99. try {
  100. barrier.await();
  101. while(true) {
  102. try {
  103. bk = new BookKeeper(new ClientConfiguration(baseClientConf), bkc.getZkHandle());
  104. lh = bk.openLedger(ledgerId,
  105. digestType, "".getBytes());
  106. lastConfirmedEntry = lh.getLastAddConfirmed();
  107. lh.close();
  108. break;
  109. } catch (BKException.BKMetadataVersionException zke) {
  110. LOG.info("Contention with someone else recovering");
  111. } catch (BKException.BKLedgerRecoveryException bkre) {
  112. LOG.info("Contention with someone else recovering");
  113. } finally {
  114. if (lh != null) {
  115. lh.close();
  116. }
  117. if (bk != null) {
  118. bk.close();
  119. bk = null;
  120. }
  121. }
  122. }
  123. } catch (Exception e) {
  124. // just exit, test should spot bad last add confirmed
  125. LOG.error("Exception occurred ", e);
  126. }
  127. LOG.info("Thread exiting, lastConfirmedEntry = " + lastConfirmedEntry);
  128. }
  129. long getLastConfirmedEntry() {
  130. return lastConfirmedEntry;
  131. }
  132. }
  133. /**
  134. * Try to open a ledger many times in parallel.
  135. * All opens should result in a ledger with an equals number of
  136. * entries.
  137. */
  138. @Test
  139. public void testManyOpenParallel() throws Exception {
  140. /*
  141. * Create ledger.
  142. */
  143. final LedgerHandle writelh = bkc.createLedger(digestType, "".getBytes());
  144. final int numRecovery = 10;
  145. final String tmp = "BookKeeper is cool!";
  146. final CountDownLatch latch = new CountDownLatch(numRecovery);
  147. Thread writethread = new Thread() {
  148. public void run() {
  149. try {
  150. while (true) {
  151. writelh.addEntry(tmp.getBytes());
  152. latch.countDown();
  153. }
  154. } catch (Exception e) {
  155. LOG.info("Exception adding entry", e);
  156. }
  157. }
  158. };
  159. writethread.start();
  160. CyclicBarrier barrier = new CyclicBarrier(numRecovery+1);
  161. LedgerOpenThread threads[] = new LedgerOpenThread[numRecovery];
  162. for (int i = 0; i < numRecovery; i++) {
  163. threads[i] = new LedgerOpenThread(digestType, writelh.getId(), barrier);
  164. threads[i].start();
  165. }
  166. latch.await();
  167. barrier.await(); // should trigger threads to go
  168. writethread.join();
  169. long lastConfirmed = writelh.getLastAddConfirmed();
  170. for (int i = 0; i < numRecovery; i++) {
  171. threads[i].join();
  172. assertTrue("Added confirmed is incorrect",
  173. lastConfirmed <= threads[i].getLastConfirmedEntry());
  174. }
  175. }
  176. /**
  177. * Test that opening a ledger in norecovery mode
  178. * doesn't fence off a ledger
  179. */
  180. @Test
  181. public void testNoRecoveryOpen() throws Exception {
  182. /*
  183. * Create ledger.
  184. */
  185. LedgerHandle writelh = null;
  186. writelh = bkc.createLedger(digestType, "".getBytes());
  187. String tmp = "BookKeeper is cool!";
  188. final int numEntries = 10;
  189. for (int i = 0; i < numEntries; i++) {
  190. writelh.addEntry(tmp.getBytes());
  191. }
  192. /*
  193. * Try to open ledger.
  194. */
  195. LedgerHandle readlh = bkc.openLedgerNoRecovery(writelh.getId(),
  196. digestType, "".getBytes());
  197. // should not have triggered recovery and fencing
  198. long numReadable = readlh.getLastAddConfirmed();
  199. LOG.error("numRead " + numReadable);
  200. Enumeration<LedgerEntry> entries = readlh.readEntries(1, numReadable);
  201. writelh.addEntry(tmp.getBytes());
  202. try {
  203. readlh.readEntries(numReadable+1, numReadable+1);
  204. fail("Shouldn't have been able to read this far");
  205. } catch (BKException.BKReadException e) {
  206. // all is good
  207. }
  208. writelh.addEntry(tmp.getBytes());
  209. long numReadable2 = readlh.getLastAddConfirmed();
  210. assertEquals("Number of readable entries hasn't changed", numReadable2, numReadable);
  211. readlh.close();
  212. writelh.addEntry(tmp.getBytes());
  213. writelh.close();
  214. }
  215. /**
  216. * create a ledger and write entries.
  217. * kill a bookie in the ensemble. Recover.
  218. * Fence the ledger. Kill another bookie. Recover.
  219. */
  220. @Test
  221. public void testFencingInteractionWithBookieRecovery() throws Exception {
  222. System.setProperty("digestType", digestType.toString());
  223. System.setProperty("passwd", "testPasswd");
  224. BookKeeperAdmin admin = new BookKeeperAdmin(zkUtil.getZooKeeperConnectString());
  225. LedgerHandle writelh = bkc.createLedger(digestType, "testPasswd".getBytes());
  226. String tmp = "Foobar";
  227. final int numEntries = 10;
  228. for (int i = 0; i < numEntries; i++) {
  229. writelh.addEntry(tmp.getBytes());
  230. }
  231. InetSocketAddress bookieToKill
  232. = writelh.getLedgerMetadata().getEnsemble(numEntries).get(0);
  233. killBookie(bookieToKill);
  234. // write entries to change ensemble
  235. for (int i = 0; i < numEntries; i++) {
  236. writelh.addEntry(tmp.getBytes());
  237. }
  238. admin.recoverBookieData(bookieToKill, null);
  239. for (int i = 0; i < numEntries; i++) {
  240. writelh.addEntry(tmp.getBytes());
  241. }
  242. LedgerHandle readlh = bkc.openLedger(writelh.getId(),
  243. digestType, "testPasswd".getBytes());
  244. try {
  245. writelh.addEntry(tmp.getBytes());
  246. LOG.error("Should have thrown an exception");
  247. fail("Should have thrown an exception when trying to write");
  248. } catch (BKException.BKLedgerFencedException e) {
  249. // correct behaviour
  250. }
  251. readlh.close();
  252. writelh.close();
  253. }
  254. /**
  255. * create a ledger and write entries.
  256. * Fence the ledger. Kill a bookie. Recover.
  257. * Ensure that recover doesn't reallow adding
  258. */
  259. @Test
  260. public void testFencingInteractionWithBookieRecovery2() throws Exception {
  261. System.setProperty("digestType", digestType.toString());
  262. System.setProperty("passwd", "testPasswd");
  263. BookKeeperAdmin admin = new BookKeeperAdmin(zkUtil.getZooKeeperConnectString());
  264. LedgerHandle writelh = bkc.createLedger(digestType, "testPasswd".getBytes());
  265. String tmp = "Foobar";
  266. final int numEntries = 10;
  267. for (int i = 0; i < numEntries; i++) {
  268. writelh.addEntry(tmp.getBytes());
  269. }
  270. LedgerHandle readlh = bkc.openLedger(writelh.getId(),
  271. digestType, "testPasswd".getBytes());
  272. // should be fenced by now
  273. InetSocketAddress bookieToKill
  274. = writelh.getLedgerMetadata().getEnsemble(numEntries).get(0);
  275. killBookie(bookieToKill);
  276. admin.recoverBookieData(bookieToKill, null);
  277. try {
  278. writelh.addEntry(tmp.getBytes());
  279. LOG.error("Should have thrown an exception");
  280. fail("Should have thrown an exception when trying to write");
  281. } catch (BKException.BKLedgerFencedException e) {
  282. // correct behaviour
  283. }
  284. readlh.close();
  285. writelh.close();
  286. }
  287. /**
  288. * Test that fencing doesn't work with a bad password
  289. */
  290. @Test
  291. public void testFencingBadPassword() throws Exception {
  292. /*
  293. * Create ledger.
  294. */
  295. LedgerHandle writelh = null;
  296. writelh = bkc.createLedger(digestType, "password1".getBytes());
  297. String tmp = "BookKeeper is cool!";
  298. for (int i = 0; i < 10; i++) {
  299. writelh.addEntry(tmp.getBytes());
  300. }
  301. /*
  302. * Try to open ledger.
  303. */
  304. try {
  305. LedgerHandle readlh = bkc.openLedger(writelh.getId(), digestType, "badPassword".getBytes());
  306. fail("Should not have been able to open with a bad password");
  307. } catch (BKException.BKUnauthorizedAccessException uue) {
  308. // correct behaviour
  309. }
  310. // should have triggered recovery and fencing
  311. writelh.addEntry(tmp.getBytes());
  312. }
  313. @Test
  314. public void testFencingAndRestartBookies() throws Exception {
  315. LedgerHandle writelh = null;
  316. writelh = bkc.createLedger(digestType, "password".getBytes());
  317. String tmp = "BookKeeper is cool!";
  318. for (int i = 0; i < 10; i++) {
  319. writelh.addEntry(tmp.getBytes());
  320. }
  321. /*
  322. * Try to open ledger.
  323. */
  324. LedgerHandle readlh = bkc.openLedger(writelh.getId(), digestType,
  325. "password".getBytes());
  326. restartBookies();
  327. try {
  328. writelh.addEntry(tmp.getBytes());
  329. LOG.error("Should have thrown an exception");
  330. fail("Should have thrown an exception when trying to write");
  331. } catch (BKException.BKLedgerFencedException e) {
  332. // correct behaviour
  333. }
  334. readlh.close();
  335. }
  336. }