/metastore/src/test/org/apache/hadoop/hive/metastore/TestLockRequestBuilder.java

http://github.com/apache/hive · Java · 584 lines · 471 code · 36 blank · 77 comment · 0 complexity · 111f8f930c653e82202ca4a2912da2e9 MD5 · raw file

  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.apache.hadoop.hive.metastore;
  19. import junit.framework.Assert;
  20. import org.apache.hadoop.hive.metastore.api.LockComponent;
  21. import org.apache.hadoop.hive.metastore.api.LockLevel;
  22. import org.apache.hadoop.hive.metastore.api.LockRequest;
  23. import org.apache.hadoop.hive.metastore.api.LockType;
  24. import org.junit.Test;
  25. import java.net.InetAddress;
  26. import java.util.List;
  27. /**
  28. * Tests for LockRequestBuilder.
  29. */
  30. public class TestLockRequestBuilder {
  31. // Test failure if user not set
  32. @Test
  33. public void noUser() {
  34. LockRequestBuilder bldr = new LockRequestBuilder();
  35. LockComponent comp = new LockComponent(LockType.EXCLUSIVE, LockLevel.DB, "mydb");
  36. bldr.addLockComponent(comp);
  37. comp = new LockComponent(LockType.EXCLUSIVE, LockLevel.DB, "mydb");
  38. comp.setTablename("mytable");
  39. bldr.addLockComponent(comp);
  40. boolean caughtException = false;
  41. try {
  42. LockRequest req = bldr.build();
  43. } catch (RuntimeException e) {
  44. Assert.assertEquals("Cannot build a lock without giving a user", e.getMessage());
  45. caughtException = true;
  46. }
  47. Assert.assertTrue(caughtException);
  48. }
  49. // Test that database and table don't coalesce.
  50. @Test
  51. public void testDbTable() throws Exception {
  52. LockRequestBuilder bldr = new LockRequestBuilder();
  53. LockComponent comp = new LockComponent(LockType.EXCLUSIVE, LockLevel.DB, "mydb");
  54. bldr.addLockComponent(comp).setUser("fred");
  55. comp = new LockComponent(LockType.EXCLUSIVE, LockLevel.DB, "mydb");
  56. comp.setTablename("mytable");
  57. bldr.addLockComponent(comp);
  58. LockRequest req = bldr.build();
  59. List<LockComponent> locks = req.getComponent();
  60. Assert.assertEquals(2, locks.size());
  61. Assert.assertEquals("fred", req.getUser());
  62. Assert.assertEquals(InetAddress.getLocalHost().getHostName(), req.getHostname());
  63. }
  64. // Test that database and table don't coalesce.
  65. @Test
  66. public void testTablePartition() {
  67. LockRequestBuilder bldr = new LockRequestBuilder();
  68. LockComponent comp = new LockComponent(LockType.EXCLUSIVE, LockLevel.DB, "mydb");
  69. comp.setTablename("mytable");
  70. bldr.addLockComponent(comp).setUser(null);
  71. comp = new LockComponent(LockType.EXCLUSIVE, LockLevel.DB, "mydb");
  72. comp.setTablename("mytable");
  73. comp.setPartitionname("mypartition");
  74. bldr.addLockComponent(comp);
  75. LockRequest req = bldr.build();
  76. List<LockComponent> locks = req.getComponent();
  77. Assert.assertEquals(2, locks.size());
  78. Assert.assertEquals("unknown", req.getUser());
  79. }
  80. // Test that 2 separate databases don't coalesce.
  81. @Test
  82. public void testTwoSeparateDbs() {
  83. LockRequestBuilder bldr = new LockRequestBuilder();
  84. LockComponent comp = new LockComponent(LockType.EXCLUSIVE, LockLevel.DB, "mydb");
  85. bldr.addLockComponent(comp).setUser("fred");
  86. comp = new LockComponent(LockType.EXCLUSIVE, LockLevel.DB, "yourdb");
  87. bldr.addLockComponent(comp);
  88. LockRequest req = bldr.build();
  89. List<LockComponent> locks = req.getComponent();
  90. Assert.assertEquals(2, locks.size());
  91. }
  92. // Test that 2 exclusive db locks coalesce to one
  93. @Test
  94. public void testExExDb() {
  95. LockRequestBuilder bldr = new LockRequestBuilder();
  96. LockComponent comp = new LockComponent(LockType.EXCLUSIVE, LockLevel.DB, "mydb");
  97. bldr.addLockComponent(comp);
  98. comp = new LockComponent(LockType.EXCLUSIVE, LockLevel.DB, "mydb");
  99. bldr.addLockComponent(comp).setUser("fred");
  100. LockRequest req = bldr.build();
  101. List<LockComponent> locks = req.getComponent();
  102. Assert.assertEquals(1, locks.size());
  103. }
  104. // Test that existing exclusive db with new shared_write coalesces to
  105. // exclusive
  106. @Test
  107. public void testExSWDb() {
  108. LockRequestBuilder bldr = new LockRequestBuilder();
  109. LockComponent comp = new LockComponent(LockType.EXCLUSIVE, LockLevel.DB, "mydb");
  110. bldr.addLockComponent(comp).setUser("fred");
  111. comp = new LockComponent(LockType.SHARED_WRITE, LockLevel.DB, "mydb");
  112. bldr.addLockComponent(comp);
  113. LockRequest req = bldr.build();
  114. List<LockComponent> locks = req.getComponent();
  115. Assert.assertEquals(1, locks.size());
  116. Assert.assertEquals(LockType.EXCLUSIVE, locks.get(0).getType());
  117. }
  118. // Test that existing exclusive db with new shared_read coalesces to
  119. // exclusive
  120. @Test
  121. public void testExSRDb() {
  122. LockRequestBuilder bldr = new LockRequestBuilder();
  123. LockComponent comp = new LockComponent(LockType.EXCLUSIVE, LockLevel.DB, "mydb");
  124. bldr.addLockComponent(comp).setUser("fred");
  125. comp = new LockComponent(LockType.SHARED_READ, LockLevel.DB, "mydb");
  126. bldr.addLockComponent(comp);
  127. LockRequest req = bldr.build();
  128. List<LockComponent> locks = req.getComponent();
  129. Assert.assertEquals(1, locks.size());
  130. Assert.assertEquals(LockType.EXCLUSIVE, locks.get(0).getType());
  131. }
  132. // Test that existing shared_write db with new exclusive coalesces to
  133. // exclusive
  134. @Test
  135. public void testSWExDb() {
  136. LockRequestBuilder bldr = new LockRequestBuilder();
  137. LockComponent comp = new LockComponent(LockType.SHARED_WRITE, LockLevel.DB, "mydb");
  138. bldr.addLockComponent(comp);
  139. comp = new LockComponent(LockType.EXCLUSIVE, LockLevel.DB, "mydb");
  140. bldr.addLockComponent(comp).setUser("fred");
  141. LockRequest req = bldr.build();
  142. List<LockComponent> locks = req.getComponent();
  143. Assert.assertEquals(1, locks.size());
  144. Assert.assertEquals(LockType.EXCLUSIVE, locks.get(0).getType());
  145. }
  146. // Test that existing shared_write db with new shared_write coalesces to
  147. // shared_write
  148. @Test
  149. public void testSWSWDb() {
  150. LockRequestBuilder bldr = new LockRequestBuilder();
  151. LockComponent comp = new LockComponent(LockType.SHARED_WRITE, LockLevel.DB, "mydb");
  152. bldr.addLockComponent(comp);
  153. comp = new LockComponent(LockType.SHARED_WRITE, LockLevel.DB, "mydb");
  154. bldr.addLockComponent(comp).setUser("fred");
  155. LockRequest req = bldr.build();
  156. List<LockComponent> locks = req.getComponent();
  157. Assert.assertEquals(1, locks.size());
  158. Assert.assertEquals(LockType.SHARED_WRITE, locks.get(0).getType());
  159. }
  160. // Test that existing shared_write db with new shared_read coalesces to
  161. // shared_write
  162. @Test
  163. public void testSWSRDb() {
  164. LockRequestBuilder bldr = new LockRequestBuilder();
  165. LockComponent comp = new LockComponent(LockType.SHARED_WRITE, LockLevel.DB, "mydb");
  166. bldr.addLockComponent(comp);
  167. comp = new LockComponent(LockType.SHARED_READ, LockLevel.DB, "mydb");
  168. bldr.addLockComponent(comp).setUser("fred");
  169. LockRequest req = bldr.build();
  170. List<LockComponent> locks = req.getComponent();
  171. Assert.assertEquals(1, locks.size());
  172. Assert.assertEquals(LockType.SHARED_WRITE, locks.get(0).getType());
  173. }
  174. // Test that existing shared_read db with new exclusive coalesces to
  175. // exclusive
  176. @Test
  177. public void testSRExDb() {
  178. LockRequestBuilder bldr = new LockRequestBuilder();
  179. LockComponent comp = new LockComponent(LockType.SHARED_READ, LockLevel.DB, "mydb");
  180. bldr.addLockComponent(comp);
  181. comp = new LockComponent(LockType.EXCLUSIVE, LockLevel.DB, "mydb");
  182. bldr.addLockComponent(comp).setUser("fred");
  183. LockRequest req = bldr.build();
  184. List<LockComponent> locks = req.getComponent();
  185. Assert.assertEquals(1, locks.size());
  186. Assert.assertEquals(LockType.EXCLUSIVE, locks.get(0).getType());
  187. }
  188. // Test that existing shared_read db with new shared_write coalesces to
  189. // shared_write
  190. @Test
  191. public void testSRSWDb() {
  192. LockRequestBuilder bldr = new LockRequestBuilder();
  193. LockComponent comp = new LockComponent(LockType.SHARED_READ, LockLevel.DB, "mydb");
  194. bldr.addLockComponent(comp);
  195. comp = new LockComponent(LockType.SHARED_WRITE, LockLevel.DB, "mydb");
  196. bldr.addLockComponent(comp).setUser("fred");
  197. LockRequest req = bldr.build();
  198. List<LockComponent> locks = req.getComponent();
  199. Assert.assertEquals(1, locks.size());
  200. Assert.assertEquals(LockType.SHARED_WRITE, locks.get(0).getType());
  201. }
  202. // Test that existing shared_read db with new shared_read coalesces to
  203. // shared_read
  204. @Test
  205. public void testSRSRDb() {
  206. LockRequestBuilder bldr = new LockRequestBuilder();
  207. LockComponent comp = new LockComponent(LockType.SHARED_READ, LockLevel.DB, "mydb");
  208. bldr.addLockComponent(comp);
  209. comp = new LockComponent(LockType.SHARED_READ, LockLevel.DB, "mydb");
  210. bldr.addLockComponent(comp).setUser("fred");
  211. LockRequest req = bldr.build();
  212. List<LockComponent> locks = req.getComponent();
  213. Assert.assertEquals(1, locks.size());
  214. Assert.assertEquals(LockType.SHARED_READ, locks.get(0).getType());
  215. }
  216. // Test that 2 separate tables don't coalesce.
  217. @Test
  218. public void testTwoSeparateTables() {
  219. LockRequestBuilder bldr = new LockRequestBuilder();
  220. LockComponent comp = new LockComponent(LockType.EXCLUSIVE, LockLevel.DB, "mydb");
  221. comp.setTablename("mytable");
  222. bldr.addLockComponent(comp);
  223. comp = new LockComponent(LockType.EXCLUSIVE, LockLevel.DB, "mydb");
  224. comp.setTablename("yourtable");
  225. bldr.addLockComponent(comp).setUser("fred");
  226. LockRequest req = bldr.build();
  227. List<LockComponent> locks = req.getComponent();
  228. Assert.assertEquals(2, locks.size());
  229. }
  230. // Test that 2 exclusive table locks coalesce to one
  231. @Test
  232. public void testExExTable() {
  233. LockRequestBuilder bldr = new LockRequestBuilder();
  234. LockComponent comp = new LockComponent(LockType.EXCLUSIVE, LockLevel.DB, "mydb");
  235. comp.setTablename("mytable");
  236. bldr.addLockComponent(comp);
  237. comp = new LockComponent(LockType.EXCLUSIVE, LockLevel.DB, "mydb");
  238. comp.setTablename("mytable");
  239. bldr.addLockComponent(comp).setUser("fred");
  240. LockRequest req = bldr.build();
  241. List<LockComponent> locks = req.getComponent();
  242. Assert.assertEquals(1, locks.size());
  243. }
  244. // Test that existing exclusive table with new shared_write coalesces to
  245. // exclusive
  246. @Test
  247. public void testExSWTable() {
  248. LockRequestBuilder bldr = new LockRequestBuilder();
  249. LockComponent comp = new LockComponent(LockType.EXCLUSIVE, LockLevel.DB, "mydb");
  250. comp.setTablename("mytable");
  251. bldr.addLockComponent(comp);
  252. comp = new LockComponent(LockType.SHARED_WRITE, LockLevel.DB, "mydb");
  253. comp.setTablename("mytable");
  254. bldr.addLockComponent(comp).setUser("fred");
  255. LockRequest req = bldr.build();
  256. List<LockComponent> locks = req.getComponent();
  257. Assert.assertEquals(1, locks.size());
  258. Assert.assertEquals(LockType.EXCLUSIVE, locks.get(0).getType());
  259. }
  260. // Test that existing exclusive table with new shared_read coalesces to
  261. // exclusive
  262. @Test
  263. public void testExSRTable() {
  264. LockRequestBuilder bldr = new LockRequestBuilder();
  265. LockComponent comp = new LockComponent(LockType.EXCLUSIVE, LockLevel.DB, "mydb");
  266. comp.setTablename("mytable");
  267. bldr.addLockComponent(comp);
  268. comp = new LockComponent(LockType.SHARED_READ, LockLevel.DB, "mydb");
  269. comp.setTablename("mytable");
  270. bldr.addLockComponent(comp).setUser("fred");
  271. LockRequest req = bldr.build();
  272. List<LockComponent> locks = req.getComponent();
  273. Assert.assertEquals(1, locks.size());
  274. Assert.assertEquals(LockType.EXCLUSIVE, locks.get(0).getType());
  275. }
  276. // Test that existing shared_write table with new exclusive coalesces to
  277. // exclusive
  278. @Test
  279. public void testSWExTable() {
  280. LockRequestBuilder bldr = new LockRequestBuilder();
  281. LockComponent comp = new LockComponent(LockType.SHARED_WRITE, LockLevel.DB, "mydb");
  282. comp.setTablename("mytable");
  283. bldr.addLockComponent(comp);
  284. comp = new LockComponent(LockType.EXCLUSIVE, LockLevel.DB, "mydb");
  285. comp.setTablename("mytable");
  286. bldr.addLockComponent(comp).setUser("fred");
  287. LockRequest req = bldr.build();
  288. List<LockComponent> locks = req.getComponent();
  289. Assert.assertEquals(1, locks.size());
  290. Assert.assertEquals(LockType.EXCLUSIVE, locks.get(0).getType());
  291. }
  292. // Test that existing shared_write table with new shared_write coalesces to
  293. // shared_write
  294. @Test
  295. public void testSWSWTable() {
  296. LockRequestBuilder bldr = new LockRequestBuilder();
  297. LockComponent comp = new LockComponent(LockType.SHARED_WRITE, LockLevel.DB, "mydb");
  298. comp.setTablename("mytable");
  299. bldr.addLockComponent(comp);
  300. comp = new LockComponent(LockType.SHARED_WRITE, LockLevel.DB, "mydb");
  301. comp.setTablename("mytable");
  302. bldr.addLockComponent(comp).setUser("fred");
  303. LockRequest req = bldr.build();
  304. List<LockComponent> locks = req.getComponent();
  305. Assert.assertEquals(1, locks.size());
  306. Assert.assertEquals(LockType.SHARED_WRITE, locks.get(0).getType());
  307. }
  308. // Test that existing shared_write table with new shared_read coalesces to
  309. // shared_write
  310. @Test
  311. public void testSWSRTable() {
  312. LockRequestBuilder bldr = new LockRequestBuilder();
  313. LockComponent comp = new LockComponent(LockType.SHARED_WRITE, LockLevel.DB, "mydb");
  314. comp.setTablename("mytable");
  315. bldr.addLockComponent(comp);
  316. comp = new LockComponent(LockType.SHARED_READ, LockLevel.DB, "mydb");
  317. comp.setTablename("mytable");
  318. bldr.addLockComponent(comp).setUser("fred");
  319. LockRequest req = bldr.build();
  320. List<LockComponent> locks = req.getComponent();
  321. Assert.assertEquals(1, locks.size());
  322. Assert.assertEquals(LockType.SHARED_WRITE, locks.get(0).getType());
  323. }
  324. // Test that existing shared_read table with new exclusive coalesces to
  325. // exclusive
  326. @Test
  327. public void testSRExTable() {
  328. LockRequestBuilder bldr = new LockRequestBuilder();
  329. LockComponent comp = new LockComponent(LockType.SHARED_READ, LockLevel.DB, "mydb");
  330. comp.setTablename("mytable");
  331. bldr.addLockComponent(comp);
  332. comp = new LockComponent(LockType.EXCLUSIVE, LockLevel.DB, "mydb");
  333. comp.setTablename("mytable");
  334. bldr.addLockComponent(comp).setUser("fred");
  335. LockRequest req = bldr.build();
  336. List<LockComponent> locks = req.getComponent();
  337. Assert.assertEquals(1, locks.size());
  338. Assert.assertEquals(LockType.EXCLUSIVE, locks.get(0).getType());
  339. }
  340. // Test that existing shared_read table with new shared_write coalesces to
  341. // shared_write
  342. @Test
  343. public void testSRSWTable() {
  344. LockRequestBuilder bldr = new LockRequestBuilder();
  345. LockComponent comp = new LockComponent(LockType.SHARED_READ, LockLevel.DB, "mydb");
  346. comp.setTablename("mytable");
  347. bldr.addLockComponent(comp);
  348. comp = new LockComponent(LockType.SHARED_WRITE, LockLevel.DB, "mydb");
  349. comp.setTablename("mytable");
  350. bldr.addLockComponent(comp).setUser("fred");
  351. LockRequest req = bldr.build();
  352. List<LockComponent> locks = req.getComponent();
  353. Assert.assertEquals(1, locks.size());
  354. Assert.assertEquals(LockType.SHARED_WRITE, locks.get(0).getType());
  355. }
  356. // Test that existing shared_read table with new shared_read coalesces to
  357. // shared_read
  358. @Test
  359. public void testSRSRTable() {
  360. LockRequestBuilder bldr = new LockRequestBuilder();
  361. LockComponent comp = new LockComponent(LockType.SHARED_READ, LockLevel.DB, "mydb");
  362. comp.setTablename("mytable");
  363. bldr.addLockComponent(comp);
  364. comp = new LockComponent(LockType.SHARED_READ, LockLevel.DB, "mydb");
  365. comp.setTablename("mytable");
  366. bldr.addLockComponent(comp).setUser("fred");
  367. LockRequest req = bldr.build();
  368. List<LockComponent> locks = req.getComponent();
  369. Assert.assertEquals(1, locks.size());
  370. Assert.assertEquals(LockType.SHARED_READ, locks.get(0).getType());
  371. }
  372. // Test that 2 separate partitions don't coalesce.
  373. @Test
  374. public void testTwoSeparatePartitions() {
  375. LockRequestBuilder bldr = new LockRequestBuilder();
  376. LockComponent comp = new LockComponent(LockType.EXCLUSIVE, LockLevel.DB, "mydb");
  377. comp.setTablename("mytable");
  378. comp.setPartitionname("mypart");
  379. bldr.addLockComponent(comp);
  380. comp = new LockComponent(LockType.EXCLUSIVE, LockLevel.DB, "mydb");
  381. comp.setTablename("mytable");
  382. comp.setPartitionname("yourpart");
  383. bldr.addLockComponent(comp).setUser("fred");
  384. LockRequest req = bldr.build();
  385. List<LockComponent> locks = req.getComponent();
  386. Assert.assertEquals(2, locks.size());
  387. }
  388. // Test that 2 exclusive partition locks coalesce to one
  389. @Test
  390. public void testExExPart() {
  391. LockRequestBuilder bldr = new LockRequestBuilder();
  392. LockComponent comp = new LockComponent(LockType.EXCLUSIVE, LockLevel.DB, "mydb");
  393. comp.setTablename("mytable");
  394. comp.setPartitionname("mypart");
  395. bldr.addLockComponent(comp);
  396. comp = new LockComponent(LockType.EXCLUSIVE, LockLevel.DB, "mydb");
  397. comp.setTablename("mytable");
  398. comp.setPartitionname("mypart");
  399. bldr.addLockComponent(comp).setUser("fred");
  400. LockRequest req = bldr.build();
  401. List<LockComponent> locks = req.getComponent();
  402. Assert.assertEquals(1, locks.size());
  403. }
  404. // Test that existing exclusive partition with new shared_write coalesces to
  405. // exclusive
  406. @Test
  407. public void testExSWPart() {
  408. LockRequestBuilder bldr = new LockRequestBuilder();
  409. LockComponent comp = new LockComponent(LockType.EXCLUSIVE, LockLevel.DB, "mydb");
  410. comp.setTablename("mytable");
  411. comp.setPartitionname("mypart");
  412. bldr.addLockComponent(comp);
  413. comp = new LockComponent(LockType.SHARED_WRITE, LockLevel.DB, "mydb");
  414. comp.setTablename("mytable");
  415. comp.setPartitionname("mypart");
  416. bldr.addLockComponent(comp).setUser("fred");
  417. LockRequest req = bldr.build();
  418. List<LockComponent> locks = req.getComponent();
  419. Assert.assertEquals(1, locks.size());
  420. Assert.assertEquals(LockType.EXCLUSIVE, locks.get(0).getType());
  421. }
  422. // Test that existing exclusive partition with new shared_read coalesces to
  423. // exclusive
  424. @Test
  425. public void testExSRPart() {
  426. LockRequestBuilder bldr = new LockRequestBuilder();
  427. LockComponent comp = new LockComponent(LockType.EXCLUSIVE, LockLevel.DB, "mydb");
  428. comp.setTablename("mytable");
  429. comp.setPartitionname("mypart");
  430. bldr.addLockComponent(comp);
  431. comp = new LockComponent(LockType.SHARED_READ, LockLevel.DB, "mydb");
  432. comp.setTablename("mytable");
  433. comp.setPartitionname("mypart");
  434. bldr.addLockComponent(comp).setUser("fred");
  435. LockRequest req = bldr.build();
  436. List<LockComponent> locks = req.getComponent();
  437. Assert.assertEquals(1, locks.size());
  438. Assert.assertEquals(LockType.EXCLUSIVE, locks.get(0).getType());
  439. }
  440. // Test that existing shared_write partition with new exclusive coalesces to
  441. // exclusive
  442. @Test
  443. public void testSWExPart() {
  444. LockRequestBuilder bldr = new LockRequestBuilder();
  445. LockComponent comp = new LockComponent(LockType.SHARED_WRITE, LockLevel.DB, "mydb");
  446. comp.setTablename("mytable");
  447. comp.setPartitionname("mypart");
  448. bldr.addLockComponent(comp);
  449. comp = new LockComponent(LockType.EXCLUSIVE, LockLevel.DB, "mydb");
  450. comp.setTablename("mytable");
  451. comp.setPartitionname("mypart");
  452. bldr.addLockComponent(comp).setUser("fred");
  453. LockRequest req = bldr.build();
  454. List<LockComponent> locks = req.getComponent();
  455. Assert.assertEquals(1, locks.size());
  456. Assert.assertEquals(LockType.EXCLUSIVE, locks.get(0).getType());
  457. }
  458. // Test that existing shared_write partition with new shared_write coalesces to
  459. // shared_write
  460. @Test
  461. public void testSWSWPart() {
  462. LockRequestBuilder bldr = new LockRequestBuilder();
  463. LockComponent comp = new LockComponent(LockType.SHARED_WRITE, LockLevel.DB, "mydb");
  464. comp.setTablename("mytable");
  465. comp.setPartitionname("mypart");
  466. bldr.addLockComponent(comp);
  467. comp = new LockComponent(LockType.SHARED_WRITE, LockLevel.DB, "mydb");
  468. comp.setTablename("mytable");
  469. comp.setPartitionname("mypart");
  470. bldr.addLockComponent(comp).setUser("fred");
  471. LockRequest req = bldr.build();
  472. List<LockComponent> locks = req.getComponent();
  473. Assert.assertEquals(1, locks.size());
  474. Assert.assertEquals(LockType.SHARED_WRITE, locks.get(0).getType());
  475. }
  476. // Test that existing shared_write partition with new shared_read coalesces to
  477. // shared_write
  478. @Test
  479. public void testSWSRPart() {
  480. LockRequestBuilder bldr = new LockRequestBuilder();
  481. LockComponent comp = new LockComponent(LockType.SHARED_WRITE, LockLevel.DB, "mydb");
  482. comp.setTablename("mytable");
  483. comp.setPartitionname("mypart");
  484. bldr.addLockComponent(comp);
  485. comp = new LockComponent(LockType.SHARED_READ, LockLevel.DB, "mydb");
  486. comp.setTablename("mytable");
  487. comp.setPartitionname("mypart");
  488. bldr.addLockComponent(comp).setUser("fred");
  489. LockRequest req = bldr.build();
  490. List<LockComponent> locks = req.getComponent();
  491. Assert.assertEquals(1, locks.size());
  492. Assert.assertEquals(LockType.SHARED_WRITE, locks.get(0).getType());
  493. }
  494. // Test that existing shared_read partition with new exclusive coalesces to
  495. // exclusive
  496. @Test
  497. public void testSRExPart() {
  498. LockRequestBuilder bldr = new LockRequestBuilder();
  499. LockComponent comp = new LockComponent(LockType.SHARED_READ, LockLevel.DB, "mydb");
  500. comp.setTablename("mytable");
  501. comp.setPartitionname("mypart");
  502. bldr.addLockComponent(comp);
  503. comp = new LockComponent(LockType.EXCLUSIVE, LockLevel.DB, "mydb");
  504. comp.setTablename("mytable");
  505. comp.setPartitionname("mypart");
  506. bldr.addLockComponent(comp).setUser("fred");
  507. LockRequest req = bldr.build();
  508. List<LockComponent> locks = req.getComponent();
  509. Assert.assertEquals(1, locks.size());
  510. Assert.assertEquals(LockType.EXCLUSIVE, locks.get(0).getType());
  511. }
  512. // Test that existing shared_read partition with new shared_write coalesces to
  513. // shared_write
  514. @Test
  515. public void testSRSWPart() {
  516. LockRequestBuilder bldr = new LockRequestBuilder();
  517. LockComponent comp = new LockComponent(LockType.SHARED_READ, LockLevel.DB, "mydb");
  518. comp.setTablename("mytable");
  519. comp.setPartitionname("mypart");
  520. bldr.addLockComponent(comp);
  521. comp = new LockComponent(LockType.SHARED_WRITE, LockLevel.DB, "mydb");
  522. comp.setTablename("mytable");
  523. comp.setPartitionname("mypart");
  524. bldr.addLockComponent(comp).setUser("fred");
  525. LockRequest req = bldr.build();
  526. List<LockComponent> locks = req.getComponent();
  527. Assert.assertEquals(1, locks.size());
  528. Assert.assertEquals(LockType.SHARED_WRITE, locks.get(0).getType());
  529. }
  530. // Test that existing shared_read partition with new shared_read coalesces to
  531. // shared_read
  532. @Test
  533. public void testSRSRPart() {
  534. LockRequestBuilder bldr = new LockRequestBuilder();
  535. LockComponent comp = new LockComponent(LockType.SHARED_READ, LockLevel.DB, "mydb");
  536. comp.setTablename("mytable");
  537. comp.setPartitionname("mypart");
  538. bldr.addLockComponent(comp);
  539. comp = new LockComponent(LockType.SHARED_READ, LockLevel.DB, "mydb");
  540. comp.setTablename("mytable");
  541. comp.setPartitionname("mypart");
  542. bldr.addLockComponent(comp).setUser("fred");
  543. LockRequest req = bldr.build();
  544. List<LockComponent> locks = req.getComponent();
  545. Assert.assertEquals(1, locks.size());
  546. Assert.assertEquals(LockType.SHARED_READ, locks.get(0).getType());
  547. }
  548. }