private HRegionInfo createRandomRegion(final String name) { HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(name)); String [] families = new String [] {"info", "anchor"}; for (int i = 0; i < families.length; i++) { htd.addFamily(new HColumnDescriptor(families[i])); } return new HRegionInfo(htd.getTableName(), HConstants.EMPTY_START_ROW, HConstants.EMPTY_END_ROW); }
@Test public void testLastRegionCompare() { HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(name.getMethodName())); HRegionInfo hrip = new HRegionInfo( tableDesc.getTableName(), Bytes.toBytes("a"), new byte[0]); HRegionInfo hric = new HRegionInfo( tableDesc.getTableName(), Bytes.toBytes("a"), Bytes.toBytes("b")); assertTrue(hrip.compareTo(hric) > 0); }
@Test public void testTableDescriptor() throws Exception { final String name = "testTableDescriptor"; HTableDescriptor htd = createTableDescriptor(name); byte [] mb = htd.toByteArray(); HTableDescriptor deserializedHtd = HTableDescriptor.parseFrom(mb); assertEquals(htd.getTableName(), deserializedHtd.getTableName()); }
@Test public void test() throws IOException { HBaseAdmin hBaseAdmin = mock(HBaseAdmin.class); HTableDescriptor[] hds = new HTableDescriptor[2]; HTableDescriptor d1 = mock(HTableDescriptor.class); HTableDescriptor d2 = mock(HTableDescriptor.class); hds[0] = d1; hds[1] = d2; when(d1.getValue("KYLIN_HOST")).thenReturn("../examples/test_metadata/"); when(d2.getValue("KYLIN_HOST")).thenReturn("../examples/test_metadata/"); when(d1.getTableName()).thenReturn(TableName.valueOf("KYLIN_J9TE08D9IA")); String toBeDel = "to-be-del"; when(d2.getTableName()).thenReturn(TableName.valueOf(toBeDel)); when(hBaseAdmin.listTables("KYLIN_.*")).thenReturn(hds); when(hBaseAdmin.tableExists(toBeDel)).thenReturn(true); when(hBaseAdmin.isTableEnabled(toBeDel)).thenReturn(false); StorageCleanJobHbaseUtil.cleanUnusedHBaseTables(hBaseAdmin, true, 100000); ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); verify(hBaseAdmin).deleteTable(captor.capture()); assertEquals(Lists.newArrayList(toBeDel), captor.getAllValues()); } }
@Before public void setUp() throws Exception { UTIL = HBaseTestingUtility.createLocalHTU(); conf = UTIL.getConfiguration(); conf.set(MobConstants.MOB_FILE_CACHE_SIZE_KEY, TEST_CACHE_SIZE); HTableDescriptor htd = UTIL.createTableDescriptor("testMobFileCache"); HColumnDescriptor hcd1 = new HColumnDescriptor(FAMILY1); hcd1.setMobEnabled(true); hcd1.setMobThreshold(0); HColumnDescriptor hcd2 = new HColumnDescriptor(FAMILY2); hcd2.setMobEnabled(true); hcd2.setMobThreshold(0); HColumnDescriptor hcd3 = new HColumnDescriptor(FAMILY3); hcd3.setMobEnabled(true); hcd3.setMobThreshold(0); htd.addFamily(hcd1); htd.addFamily(hcd2); htd.addFamily(hcd3); RegionInfo regionInfo = RegionInfoBuilder.newBuilder(htd.getTableName()).build(); mobFileCache = new MobFileCache(conf); region = HBaseTestingUtility .createRegionAndWAL(regionInfo, UTIL.getDataTestDir(), conf, htd, mobFileCache); }
/** * Set the number of Region replicas. */ public static void setReplicas(Admin admin, TableName table, int replicaCount) throws IOException, InterruptedException { admin.disableTable(table); HTableDescriptor desc = new HTableDescriptor(admin.getTableDescriptor(table)); desc.setRegionReplication(replicaCount); admin.modifyTable(desc.getTableName(), desc); admin.enableTable(table); }
private HRegion initHRegion(byte [] tableName, String callingMethod, byte[] ... families) throws IOException { HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName)); for(byte [] family : families) { htd.addFamily(new HColumnDescriptor(family)); } HRegionInfo info = new HRegionInfo(htd.getTableName(), null, null, false); return HBTU.createLocalHRegion(info, htd); }
@Test public void testRandomKeying() throws Exception { // Verify we have store file(s) with a random key final List<Path> initialPaths = findStorefilePaths(htd.getTableName()); assertTrue(initialPaths.size() > 0); for (Path path: initialPaths) { assertNotNull("Store file " + path + " is not encrypted", extractHFileKey(path)); } }
@Test public void testWithRegions() throws IOException, ServiceException { HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(name.getMethodName())); List<RegionInfo> regions = Lists.newArrayList( RegionInfoBuilder.newBuilder(htd.getTableName()) .setStartKey(Bytes.toBytes("a")) .setEndKey(Bytes.toBytes("d")) .build(), RegionInfoBuilder.newBuilder(htd.getTableName()) .setStartKey(Bytes.toBytes("d")) .setEndKey(Bytes.toBytes("z")) .build() ); Mockito.doReturn(ResponseConverter.buildGetOnlineRegionResponse( regions)).when(rpcServices).getOnlineRegion((RpcController)Mockito.any(), (GetOnlineRegionRequest)Mockito.any()); new RSStatusTmpl().render(new StringWriter(), rs); } }
@Test public void testListTables() throws IOException, InterruptedException { final TableName tableName1 = TableName.valueOf(name.getMethodName() + "1"); final TableName tableName2 = TableName.valueOf(name.getMethodName() + "2"); final TableName tableName3 = TableName.valueOf(name.getMethodName() + "3"); TableName [] tables = new TableName[] { tableName1, tableName2, tableName3 }; for (int i = 0; i < tables.length; i++) { TEST_UTIL.createTable(tables[i], FAMILY); } Admin admin = TEST_UTIL.getAdmin(); HTableDescriptor[] ts = admin.listTables(); HashSet<HTableDescriptor> result = new HashSet<HTableDescriptor>(ts.length); Collections.addAll(result, ts); int size = result.size(); assertTrue(size >= tables.length); for (int i = 0; i < tables.length && i < size; i++) { boolean found = false; for (int j = 0; j < ts.length; j++) { if (ts[j].getTableName().equals(tables[i])) { found = true; break; } } assertTrue("Not found: " + tables[i], found); } }
@Test public void createTableInDefaultNamespace() throws Exception { HTableDescriptor desc = new HTableDescriptor(TableName.valueOf(name.getMethodName())); HColumnDescriptor colDesc = new HColumnDescriptor("cf1"); desc.addFamily(colDesc); admin.createTable(desc); assertTrue(admin.listTables().length == 1); admin.disableTable(desc.getTableName()); admin.deleteTable(desc.getTableName()); }
/** * Tests the case where if there is some data in the primary region, reopening the region replicas * (enable/disable table, etc) makes the region replicas readable. * @throws IOException */ @Test public void testSecondaryRegionWithNonEmptyRegion() throws IOException { // Create a new table with region replication and load some data // than disable and enable the table again and verify the data from secondary try (Connection connection = ConnectionFactory.createConnection(HTU.getConfiguration()); Table table = connection.getTable(htd.getTableName())) { HTU.loadNumericRows(table, fam, 0, 1000); HTU.getAdmin().disableTable(htd.getTableName()); HTU.getAdmin().enableTable(htd.getTableName()); HTU.verifyNumericRows(table, fam, 0, 1000, 1); } }
@Test public void testContainsRange() { HTableDescriptor tableDesc = new HTableDescriptor(TableName.valueOf(name.getMethodName())); HRegionInfo hri = new HRegionInfo( tableDesc.getTableName(), Bytes.toBytes("a"), Bytes.toBytes("g")); // Single row range at start of region assertTrue(hri.containsRange(Bytes.toBytes("a"), Bytes.toBytes("a"))); // Fully contained range assertTrue(hri.containsRange(Bytes.toBytes("b"), Bytes.toBytes("c"))); // Range overlapping start of region assertTrue(hri.containsRange(Bytes.toBytes("a"), Bytes.toBytes("c"))); // Fully contained single-row range assertTrue(hri.containsRange(Bytes.toBytes("c"), Bytes.toBytes("c"))); // Range that overlaps end key and hence doesn't fit assertFalse(hri.containsRange(Bytes.toBytes("a"), Bytes.toBytes("g"))); // Single row range on end key assertFalse(hri.containsRange(Bytes.toBytes("g"), Bytes.toBytes("g"))); // Single row range entirely outside assertFalse(hri.containsRange(Bytes.toBytes("z"), Bytes.toBytes("z"))); // Degenerate range try { hri.containsRange(Bytes.toBytes("z"), Bytes.toBytes("a")); fail("Invalid range did not throw IAE"); } catch (IllegalArgumentException iae) { } }
@Test public void testLotsOfRegionReplicas() throws IOException { int numRegions = NB_SERVERS * 20; int regionReplication = 10; String tableName = htd.getTableName().getNameAsString() + "2"; htd = HTU.createTableDescriptor(tableName); htd.setRegionReplication(regionReplication); Table table = connection.getTable(htd.getTableName())) { HTU.deleteTableIfAny(TableName.valueOf(tableName));
private void initHRegion (byte [] tableName, String callingMethod, int [] maxVersions, byte[] ... families) throws IOException { HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(tableName)); int i=0; for(byte [] family : families) { HColumnDescriptor hcd = new HColumnDescriptor(family); hcd.setMaxVersions(maxVersions != null ? maxVersions[i++] : 1); htd.addFamily(hcd); } HRegionInfo info = new HRegionInfo(htd.getTableName(), null, null, false); region = TEST_UTIL.createLocalHRegion(info, htd); }
/** * Tests the case where a newly created table with region replicas and no data, the secondary * region replicas are available to read immediately. */ @Test public void testSecondaryRegionWithEmptyRegion() throws IOException { // Create a new table with region replication, don't put any data. Test that the secondary // region replica is available to read. try (Connection connection = ConnectionFactory.createConnection(HTU.getConfiguration()); Table table = connection.getTable(htd.getTableName())) { Get get = new Get(row); get.setConsistency(Consistency.TIMELINE); get.setReplicaId(1); table.get(get); // this should not block } }
@SuppressWarnings("SelfComparison") @Test public void testComparator() { final TableName tableName = TableName.valueOf(name.getMethodName()); byte[] empty = new byte[0]; HRegionInfo older = new HRegionInfo(tableName, empty, empty, false, 0L); assertEquals(0, newer.compareTo(newer)); HRegionInfo a = new HRegionInfo(TableName.valueOf("a"), null, null); HRegionInfo b = new HRegionInfo(TableName.valueOf("b"), null, null); assertNotEquals(0, a.compareTo(b)); HTableDescriptor t = new HTableDescriptor(TableName.valueOf("t")); byte [] midway = Bytes.toBytes("midway"); a = new HRegionInfo(t.getTableName(), null, midway); b = new HRegionInfo(t.getTableName(), midway, null); assertTrue(a.compareTo(b) < 0); assertTrue(b.compareTo(a) > 0); assertEquals(a, a); assertEquals(0, a.compareTo(a)); a = new HRegionInfo(t.getTableName(), Bytes.toBytes("a"), Bytes.toBytes("d")); b = new HRegionInfo(t.getTableName(), Bytes.toBytes("e"), Bytes.toBytes("g")); assertTrue(a.compareTo(b) < 0); a = new HRegionInfo(t.getTableName(), Bytes.toBytes("aaaa"), Bytes.toBytes("dddd")); b = new HRegionInfo(t.getTableName(), Bytes.toBytes("e"), Bytes.toBytes("g")); assertTrue(a.compareTo(b) < 0); a = new HRegionInfo(t.getTableName(), Bytes.toBytes("aaaa"), Bytes.toBytes("dddd")); b = new HRegionInfo(t.getTableName(), Bytes.toBytes("aaaa"), Bytes.toBytes("eeee")); assertTrue(a.compareTo(b) < 0);
new TestGetThread(TEST_UTIL.getConnection().getTable(hdt.getTableName())); TestGetThread tg2 = new TestGetThread(TEST_UTIL.getConnection().getTable(hdt.getTableName())); TestGetThread tg3 = new TestGetThread(TEST_UTIL.getConnection().getTable(hdt.getTableName())); TestGetThread tg4 = new TestGetThread(TEST_UTIL.getConnection().getTable(hdt.getTableName())); TestGetThread tg5 = new TestGetThread(TEST_UTIL.getConnection().getTable(hdt.getTableName())); tg1.start(); tg2.start(); new TestPutThread(TEST_UTIL.getConnection().getTable(hdt.getTableName())); TestPutThread tp2 = new TestPutThread(TEST_UTIL.getConnection().getTable(hdt.getTableName())); TestPutThread tp3 = new TestPutThread(TEST_UTIL.getConnection().getTable(hdt.getTableName())); TestPutThread tp4 = new TestPutThread(TEST_UTIL.getConnection().getTable(hdt.getTableName())); TestPutThread tp5 = new TestPutThread(TEST_UTIL.getConnection().getTable(hdt.getTableName())); tp1.start(); tp2.start();
@Test public void createTableInSystemNamespace() throws Exception { final TableName tableName = TableName.valueOf("hbase:" + name.getMethodName()); HTableDescriptor desc = new HTableDescriptor(tableName); HColumnDescriptor colDesc = new HColumnDescriptor("cf1"); desc.addFamily(colDesc); admin.createTable(desc); assertEquals(0, admin.listTables().length); assertTrue(admin.tableExists(tableName)); admin.disableTable(desc.getTableName()); admin.deleteTable(desc.getTableName()); }
@Test public void testCreateDeleteTable() throws IOException { // Create table then get the single region for our new table. HTableDescriptor hdt = HTU.createTableDescriptor("testCreateDeleteTable"); hdt.setRegionReplication(NB_SERVERS); hdt.addCoprocessor(SlowMeCopro.class.getName()); Table table = HTU.createTable(hdt, new byte[][]{f}, null); Put p = new Put(row); p.addColumn(f, row, row); table.put(p); Get g = new Get(row); Result r = table.get(g); Assert.assertFalse(r.isStale()); try { // But if we ask for stale we will get it SlowMeCopro.cdl.set(new CountDownLatch(1)); g = new Get(row); g.setConsistency(Consistency.TIMELINE); r = table.get(g); Assert.assertTrue(r.isStale()); SlowMeCopro.cdl.get().countDown(); } finally { SlowMeCopro.cdl.get().countDown(); SlowMeCopro.sleepTime.set(0); } HTU.getAdmin().disableTable(hdt.getTableName()); HTU.deleteTable(hdt.getTableName()); }