PageRenderTime 91ms CodeModel.GetById 58ms RepoModel.GetById 1ms app.codeStats 0ms

/hbase-server/src/test/java/org/apache/hadoop/hbase/client/TestFromClientSide3.java

https://github.com/jmhsieh/hbase
Java | 259 lines | 169 code | 29 blank | 61 comment | 18 complexity | 420248b3b76084cb78ca38788f50b8a9 MD5 | raw file
  1. /**
  2. * Copyright The Apache Software Foundation
  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, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. package org.apache.hadoop.hbase.client;
  21. import static org.junit.Assert.assertNull;
  22. import static org.junit.Assert.assertTrue;
  23. import java.util.List;
  24. import java.util.Random;
  25. import org.apache.commons.logging.Log;
  26. import org.apache.commons.logging.LogFactory;
  27. import org.apache.hadoop.hbase.HBaseTestingUtility;
  28. import org.apache.hadoop.hbase.HColumnDescriptor;
  29. import org.apache.hadoop.hbase.HRegionLocation;
  30. import org.apache.hadoop.hbase.HTableDescriptor;
  31. import org.apache.hadoop.hbase.LargeTests;
  32. import org.apache.hadoop.hbase.client.AdminProtocol;
  33. import org.apache.hadoop.hbase.protobuf.ProtobufUtil;
  34. import org.apache.hadoop.hbase.util.Bytes;
  35. import org.apache.hadoop.hbase.util.Pair;
  36. import org.junit.After;
  37. import org.junit.AfterClass;
  38. import org.junit.Before;
  39. import org.junit.BeforeClass;
  40. import org.junit.Test;
  41. import org.junit.experimental.categories.Category;
  42. @Category(LargeTests.class)
  43. public class TestFromClientSide3 {
  44. final Log LOG = LogFactory.getLog(getClass());
  45. private final static HBaseTestingUtility TEST_UTIL
  46. = new HBaseTestingUtility();
  47. private static byte[] FAMILY = Bytes.toBytes("testFamily");
  48. private static Random random = new Random();
  49. private static int SLAVES = 3;
  50. /**
  51. * @throws java.lang.Exception
  52. */
  53. @BeforeClass
  54. public static void setUpBeforeClass() throws Exception {
  55. TEST_UTIL.getConfiguration().setBoolean(
  56. "hbase.online.schema.update.enable", true);
  57. TEST_UTIL.startMiniCluster(SLAVES);
  58. }
  59. /**
  60. * @throws java.lang.Exception
  61. */
  62. @AfterClass
  63. public static void tearDownAfterClass() throws Exception {
  64. TEST_UTIL.shutdownMiniCluster();
  65. }
  66. /**
  67. * @throws java.lang.Exception
  68. */
  69. @Before
  70. public void setUp() throws Exception {
  71. // Nothing to do.
  72. }
  73. /**
  74. * @throws java.lang.Exception
  75. */
  76. @After
  77. public void tearDown() throws Exception {
  78. // Nothing to do.
  79. }
  80. private void randomCFPuts(HTable table, byte[] row, byte[] family, int nPuts)
  81. throws Exception {
  82. Put put = new Put(row);
  83. for (int i = 0; i < nPuts; i++) {
  84. byte[] qualifier = Bytes.toBytes(random.nextInt());
  85. byte[] value = Bytes.toBytes(random.nextInt());
  86. put.add(family, qualifier, value);
  87. }
  88. table.put(put);
  89. }
  90. private void performMultiplePutAndFlush(HBaseAdmin admin, HTable table,
  91. byte[] row, byte[] family, int nFlushes, int nPuts) throws Exception {
  92. // connection needed for poll-wait
  93. HConnection conn = HConnectionManager.getConnection(TEST_UTIL
  94. .getConfiguration());
  95. HRegionLocation loc = table.getRegionLocation(row, true);
  96. AdminProtocol server = conn.getAdmin(loc.getHostname(), loc
  97. .getPort());
  98. byte[] regName = loc.getRegionInfo().getRegionName();
  99. for (int i = 0; i < nFlushes; i++) {
  100. randomCFPuts(table, row, family, nPuts);
  101. List<String> sf = ProtobufUtil.getStoreFiles(server, regName, FAMILY);
  102. int sfCount = sf.size();
  103. // TODO: replace this api with a synchronous flush after HBASE-2949
  104. admin.flush(table.getTableName());
  105. // synchronously poll wait for a new storefile to appear (flush happened)
  106. while (ProtobufUtil.getStoreFiles(
  107. server, regName, FAMILY).size() == sfCount) {
  108. Thread.sleep(40);
  109. }
  110. }
  111. }
  112. // override the config settings at the CF level and ensure priority
  113. @Test(timeout = 60000)
  114. public void testAdvancedConfigOverride() throws Exception {
  115. /*
  116. * Overall idea: (1) create 3 store files and issue a compaction. config's
  117. * compaction.min == 3, so should work. (2) Increase the compaction.min
  118. * toggle in the HTD to 5 and modify table. If we use the HTD value instead
  119. * of the default config value, adding 3 files and issuing a compaction
  120. * SHOULD NOT work (3) Decrease the compaction.min toggle in the HCD to 2
  121. * and modify table. The CF schema should override the Table schema and now
  122. * cause a minor compaction.
  123. */
  124. TEST_UTIL.getConfiguration().setInt("hbase.hstore.compaction.min", 3);
  125. String tableName = "testAdvancedConfigOverride";
  126. byte[] TABLE = Bytes.toBytes(tableName);
  127. HTable hTable = TEST_UTIL.createTable(TABLE, FAMILY, 10);
  128. HBaseAdmin admin = new HBaseAdmin(TEST_UTIL.getConfiguration());
  129. HConnection connection = HConnectionManager.getConnection(TEST_UTIL
  130. .getConfiguration());
  131. // Create 3 store files.
  132. byte[] row = Bytes.toBytes(random.nextInt());
  133. performMultiplePutAndFlush(admin, hTable, row, FAMILY, 3, 100);
  134. // Verify we have multiple store files.
  135. HRegionLocation loc = hTable.getRegionLocation(row, true);
  136. byte[] regionName = loc.getRegionInfo().getRegionName();
  137. AdminProtocol server = connection.getAdmin(
  138. loc.getHostname(), loc.getPort());
  139. assertTrue(ProtobufUtil.getStoreFiles(
  140. server, regionName, FAMILY).size() > 1);
  141. // Issue a compaction request
  142. admin.compact(TABLE);
  143. // poll wait for the compactions to happen
  144. for (int i = 0; i < 10 * 1000 / 40; ++i) {
  145. // The number of store files after compaction should be lesser.
  146. loc = hTable.getRegionLocation(row, true);
  147. if (!loc.getRegionInfo().isOffline()) {
  148. regionName = loc.getRegionInfo().getRegionName();
  149. server = connection.getAdmin(loc.getHostname(), loc.getPort());
  150. if (ProtobufUtil.getStoreFiles(
  151. server, regionName, FAMILY).size() <= 1) {
  152. break;
  153. }
  154. }
  155. Thread.sleep(40);
  156. }
  157. // verify the compactions took place and that we didn't just time out
  158. assertTrue(ProtobufUtil.getStoreFiles(
  159. server, regionName, FAMILY).size() <= 1);
  160. // change the compaction.min config option for this table to 5
  161. LOG.info("hbase.hstore.compaction.min should now be 5");
  162. HTableDescriptor htd = new HTableDescriptor(hTable.getTableDescriptor());
  163. htd.setValue("hbase.hstore.compaction.min", String.valueOf(5));
  164. admin.modifyTable(TABLE, htd);
  165. Pair<Integer, Integer> st;
  166. while (null != (st = admin.getAlterStatus(TABLE)) && st.getFirst() > 0) {
  167. LOG.debug(st.getFirst() + " regions left to update");
  168. Thread.sleep(40);
  169. }
  170. LOG.info("alter status finished");
  171. // Create 3 more store files.
  172. performMultiplePutAndFlush(admin, hTable, row, FAMILY, 3, 10);
  173. // Issue a compaction request
  174. admin.compact(TABLE);
  175. // This time, the compaction request should not happen
  176. Thread.sleep(10 * 1000);
  177. loc = hTable.getRegionLocation(row, true);
  178. regionName = loc.getRegionInfo().getRegionName();
  179. server = connection.getAdmin(loc.getHostname(), loc.getPort());
  180. int sfCount = ProtobufUtil.getStoreFiles(
  181. server, regionName, FAMILY).size();
  182. assertTrue(sfCount > 1);
  183. // change an individual CF's config option to 2 & online schema update
  184. LOG.info("hbase.hstore.compaction.min should now be 2");
  185. HColumnDescriptor hcd = new HColumnDescriptor(htd.getFamily(FAMILY));
  186. hcd.setValue("hbase.hstore.compaction.min", String.valueOf(2));
  187. htd.addFamily(hcd);
  188. admin.modifyTable(TABLE, htd);
  189. while (null != (st = admin.getAlterStatus(TABLE)) && st.getFirst() > 0) {
  190. LOG.debug(st.getFirst() + " regions left to update");
  191. Thread.sleep(40);
  192. }
  193. LOG.info("alter status finished");
  194. // Issue a compaction request
  195. admin.compact(TABLE);
  196. // poll wait for the compactions to happen
  197. for (int i = 0; i < 10 * 1000 / 40; ++i) {
  198. loc = hTable.getRegionLocation(row, true);
  199. regionName = loc.getRegionInfo().getRegionName();
  200. try {
  201. server = connection.getAdmin(loc.getHostname(), loc
  202. .getPort());
  203. if (ProtobufUtil.getStoreFiles(
  204. server, regionName, FAMILY).size() < sfCount) {
  205. break;
  206. }
  207. } catch (Exception e) {
  208. LOG.debug("Waiting for region to come online: " + regionName);
  209. }
  210. Thread.sleep(40);
  211. }
  212. // verify the compaction took place and that we didn't just time out
  213. assertTrue(ProtobufUtil.getStoreFiles(
  214. server, regionName, FAMILY).size() < sfCount);
  215. // Finally, ensure that we can remove a custom config value after we made it
  216. LOG.info("Removing CF config value");
  217. LOG.info("hbase.hstore.compaction.min should now be 5");
  218. hcd = new HColumnDescriptor(htd.getFamily(FAMILY));
  219. hcd.setValue("hbase.hstore.compaction.min", null);
  220. htd.addFamily(hcd);
  221. admin.modifyTable(TABLE, htd);
  222. while (null != (st = admin.getAlterStatus(TABLE)) && st.getFirst() > 0) {
  223. LOG.debug(st.getFirst() + " regions left to update");
  224. Thread.sleep(40);
  225. }
  226. LOG.info("alter status finished");
  227. assertNull(hTable.getTableDescriptor().getFamily(FAMILY).getValue(
  228. "hbase.hstore.compaction.min"));
  229. }
  230. }