@Test public void testPutNoCF() throws IOException { final byte[] BAD_FAM = Bytes.toBytes("BAD_CF"); final byte[] VAL = Bytes.toBytes(100); Table table = TEST_UTIL.createTable(Bytes.toBytes("testPutNoCF"), FAMILY); boolean caughtNSCFE = false; try { Put p = new Put(ROW); p.add(BAD_FAM, QUALIFIER, VAL); table.put(p); } catch (RetriesExhaustedWithDetailsException e) { caughtNSCFE = e.getCause(0) instanceof NoSuchColumnFamilyException; } assertTrue("Should throw NoSuchColumnFamilyException", caughtNSCFE); }
@Test public void testPutNoCF() throws IOException { final byte[] BAD_FAM = Bytes.toBytes("BAD_CF"); final byte[] VAL = Bytes.toBytes(100); HTable table = TEST_UTIL.createTable(Bytes.toBytes("testPutNoCF"), new byte[][]{FAMILY}); boolean caughtNSCFE = false; try { Put p = new Put(ROW); p.add(BAD_FAM, QUALIFIER, VAL); table.put(p); } catch (RetriesExhaustedWithDetailsException e) { caughtNSCFE = e.getCause(0) instanceof NoSuchColumnFamilyException; } assertTrue("Should throw NoSuchColumnFamilyException", caughtNSCFE); }
public static void handleHBaseException( Throwable t, Iterator<Record> records, ErrorRecordHandler errorRecordHandler ) throws StageException { Throwable cause = t; // Drill down to root cause while((cause instanceof UncheckedExecutionException || cause instanceof UndeclaredThrowableException || cause instanceof ExecutionException) && cause.getCause() != null) { cause = cause.getCause(); } // Column is null or No such Column Family exception if(cause instanceof NullPointerException || cause instanceof NoSuchColumnFamilyException) { while(records.hasNext()) { Record record = records.next(); errorRecordHandler.onError(new OnRecordErrorException(record, Errors.HBASE_37, cause)); } } else { LOG.error(Errors.HBASE_36.getMessage(), cause.toString(), cause); throw new StageException(Errors.HBASE_36, cause.toString(), cause); } }
public static void handleHBaseException( RetriesExhaustedWithDetailsException rex, Record record, Map<String, Record> rowKeyToRecord, ErrorRecordHandler errorRecordHandler ) throws StageException { for (int i = 0; i < rex.getNumExceptions(); i++) { if (rex.getCause(i) instanceof NoSuchColumnFamilyException) { Row r = rex.getRow(i); Record errorRecord = record != null ? record : rowKeyToRecord.get(Bytes.toString(r.getRow())); OnRecordErrorException exception = new OnRecordErrorException(errorRecord, Errors.HBASE_10, getErrorDescription(rex.getCause(i), r, rex.getHostnamePort(i))); errorRecordHandler.onError(exception); } else { // If at least 1 non NoSuchColumnFamilyException exception, // consider as stage exception throw new StageException(Errors.HBASE_02, rex); } } }
@Test public void testPutNoCF() throws IOException { final byte[] BAD_FAM = Bytes.toBytes("BAD_CF"); final byte[] VAL = Bytes.toBytes(100); Table table = TEST_UTIL.createTable(TableName.valueOf(name.getMethodName()), FAMILY); boolean caughtNSCFE = false; try { Put p = new Put(ROW); p.addColumn(BAD_FAM, QUALIFIER, VAL); table.put(p); } catch (Exception e) { caughtNSCFE = e instanceof NoSuchColumnFamilyException; } assertTrue("Should throw NoSuchColumnFamilyException", caughtNSCFE); }
public static Map<String, Integer> classifyExs(List<Throwable> ths) { Map<String, Integer> cls = new HashMap<String, Integer>(); for (Throwable t : ths) { if (t == null) continue; String name = ""; if (t instanceof NoSuchColumnFamilyException) { name = t.getMessage(); } else { name = t.getClass().getSimpleName(); } Integer i = cls.get(name); if (i == null) { i = 0; } i += 1; cls.put(name, i); } return cls; }
@Override public boolean processPutStatus(MutationStatus operationStatus) throws IOException{ OperationStatus opStat = ((HMutationStatus)operationStatus).unwrapDelegate(); switch (opStat.getOperationStatusCode()) { case NOT_RUN: throw new IOException("Could not acquire Lock"); case BAD_FAMILY: throw new NoSuchColumnFamilyException(opStat.getExceptionMsg()); case SANITY_CHECK_FAILURE: throw new IOException("Sanity Check failure:" + opStat.getExceptionMsg()); case FAILURE: throw new IOException(opStat.getExceptionMsg()); default: return true; } }
@Test @Category(KnownGap.class) public void testAtomicPut() throws Exception { Table table = getConnection().getTable(TABLE_NAME); byte[] rowKey = Bytes.toBytes("testrow-" + RandomStringUtils.randomAlphanumeric(8)); byte[] goodQual = Bytes.toBytes("testQualifier-" + RandomStringUtils.randomAlphanumeric(8)); byte[] goodValue = Bytes.toBytes("testValue-" + RandomStringUtils.randomAlphanumeric(8)); byte[] badQual = Bytes.toBytes("testQualifier-" + RandomStringUtils.randomAlphanumeric(8)); byte[] badValue = Bytes.toBytes("testValue-" + RandomStringUtils.randomAlphanumeric(8)); byte[] badfamily = Bytes.toBytes("badcolumnfamily-" + RandomStringUtils.randomAlphanumeric(8)); Put put = new Put(rowKey); put.addColumn(COLUMN_FAMILY, goodQual, goodValue); put.addColumn(badfamily, badQual, badValue); RetriesExhaustedWithDetailsException thrownException = null; try { table.put(put); } catch (RetriesExhaustedWithDetailsException e) { thrownException = e; } Assert.assertNotNull("Exception should have been thrown", thrownException); Assert.assertEquals("Expecting one exception", 1, thrownException.getNumExceptions()); Assert.assertArrayEquals("Row key", rowKey, thrownException.getRow(0).getRow()); Assert.assertTrue("Cause: NoSuchColumnFamilyException", thrownException.getCause(0) instanceof NoSuchColumnFamilyException); Get get = new Get(rowKey); Result result = table.get(get); Assert.assertEquals("Atomic behavior means there should be nothing here", 0, result.size()); table.close(); }
@Test public void testScanWrongColumnFamily() throws Exception { try { doScan(createScan().addFamily(Bytes.toBytes("WrongColumnFamily"))); } catch (Exception e) { assertTrue(e instanceof NoSuchColumnFamilyException || e.getCause() instanceof NoSuchColumnFamilyException); } }
@Test public void testCheckAndMutate() throws Throwable { try (Table table = createTable()) { // put one row putOneRow(table); // get row back and assert the values getOneRowAndAssertAllExist(table); // put the same row again with C column deleted RowMutations rm = makeRowMutationsWithColumnCDeleted(); boolean res = table.checkAndMutate(ROWKEY, FAMILY).qualifier(Bytes.toBytes("A")) .ifEquals(Bytes.toBytes("a")).thenMutate(rm); assertTrue(res); // get row back and assert the values getOneRowAndAssertAllButCExist(table); //Test that we get a region level exception try { rm = getBogusRowMutations(); table.checkAndMutate(ROWKEY, FAMILY).qualifier(Bytes.toBytes("A")) .ifEquals(Bytes.toBytes("a")).thenMutate(rm); fail("Expected NoSuchColumnFamilyException"); } catch (RetriesExhaustedWithDetailsException e) { try { throw e.getCause(0); } catch (NoSuchColumnFamilyException e1) { // expected } } } }
@Test public void testCheckAndMutateWithBuilder() throws Throwable { try (Table table = createTable()) { // put one row putOneRow(table); // get row back and assert the values getOneRowAndAssertAllExist(table); // put the same row again with C column deleted RowMutations rm = makeRowMutationsWithColumnCDeleted(); boolean res = table.checkAndMutate(ROWKEY, FAMILY).qualifier(Bytes.toBytes("A")) .ifEquals(Bytes.toBytes("a")).thenMutate(rm); assertTrue(res); // get row back and assert the values getOneRowAndAssertAllButCExist(table); //Test that we get a region level exception try { rm = getBogusRowMutations(); table.checkAndMutate(ROWKEY, FAMILY).qualifier(Bytes.toBytes("A")) .ifEquals(Bytes.toBytes("a")).thenMutate(rm); fail("Expected NoSuchColumnFamilyException"); } catch (RetriesExhaustedWithDetailsException e) { try { throw e.getCause(0); } catch (NoSuchColumnFamilyException e1) { // expected } } } }
@Test public void testRestoreSchemaChange() throws Exception { byte[] TEST_FAMILY2 = Bytes.toBytes("cf2"); HTable table = new HTable(TEST_UTIL.getConfiguration(), tableName); // Add one column family and put some data in it admin.disableTable(tableName); admin.addColumn(tableName, new HColumnDescriptor(TEST_FAMILY2)); admin.enableTable(tableName); assertEquals(2, table.getTableDescriptor().getFamilies().size()); HTableDescriptor htd = admin.getTableDescriptor(tableName); assertEquals(2, htd.getFamilies().size()); SnapshotTestingUtils.loadData(TEST_UTIL, tableName, 500, TEST_FAMILY2); long snapshot2Rows = snapshot1Rows + 500; assertEquals(snapshot2Rows, TEST_UTIL.countRows(table)); assertEquals(500, TEST_UTIL.countRows(table, TEST_FAMILY2)); Set<String> fsFamilies = getFamiliesFromFS(tableName); assertEquals(2, fsFamilies.size()); // Take a snapshot admin.disableTable(tableName); admin.snapshot(snapshotName2, tableName); // Restore the snapshot (without the cf) admin.restoreSnapshot(snapshotName0); admin.enableTable(tableName); assertEquals(1, table.getTableDescriptor().getFamilies().size()); try { TEST_UTIL.countRows(table, TEST_FAMILY2); fail("family '" + Bytes.toString(TEST_FAMILY2) + "' should not exists"); } catch (NoSuchColumnFamilyException e) { // expected } assertEquals(snapshot0Rows, TEST_UTIL.countRows(table)); htd = admin.getTableDescriptor(tableName); assertEquals(1, htd.getFamilies().size()); fsFamilies = getFamiliesFromFS(tableName); assertEquals(1, fsFamilies.size()); // Restore back the snapshot (with the cf) admin.disableTable(tableName); admin.restoreSnapshot(snapshotName2); admin.enableTable(tableName); htd = admin.getTableDescriptor(tableName); assertEquals(2, htd.getFamilies().size()); assertEquals(2, table.getTableDescriptor().getFamilies().size()); assertEquals(500, TEST_UTIL.countRows(table, TEST_FAMILY2)); assertEquals(snapshot2Rows, TEST_UTIL.countRows(table)); fsFamilies = getFamiliesFromFS(tableName); assertEquals(2, fsFamilies.size()); table.close(); }
@Test public void testRowMutation() throws Exception { LOG.info("Starting testRowMutation"); final TableName TABLENAME = TableName.valueOf("testRowMutation"); Table t = TEST_UTIL.createTable(TABLENAME, FAMILY); byte [][] QUALIFIERS = new byte [][] { Bytes.toBytes("a"), Bytes.toBytes("b") }; RowMutations arm = new RowMutations(ROW); Put p = new Put(ROW); p.add(FAMILY, QUALIFIERS[0], VALUE); arm.add(p); t.mutateRow(arm); Get g = new Get(ROW); Result r = t.get(g); assertEquals(0, Bytes.compareTo(VALUE, r.getValue(FAMILY, QUALIFIERS[0]))); arm = new RowMutations(ROW); p = new Put(ROW); p.add(FAMILY, QUALIFIERS[1], VALUE); arm.add(p); Delete d = new Delete(ROW); d.deleteColumns(FAMILY, QUALIFIERS[0]); arm.add(d); // TODO: Trying mutateRow again. The batch was failing with a one try only. t.mutateRow(arm); r = t.get(g); assertEquals(0, Bytes.compareTo(VALUE, r.getValue(FAMILY, QUALIFIERS[1]))); assertNull(r.getValue(FAMILY, QUALIFIERS[0])); //Test that we get a region level exception try { arm = new RowMutations(ROW); p = new Put(ROW); p.add(new byte[]{'b', 'o', 'g', 'u', 's'}, QUALIFIERS[0], VALUE); arm.add(p); t.mutateRow(arm); fail("Expected NoSuchColumnFamilyException"); } catch(NoSuchColumnFamilyException e) { } }
@Test public void testCheckAndMutate() throws Exception { final TableName tableName = TableName.valueOf("TestPutWithDelete"); final byte[] rowKey = Bytes.toBytes("12345"); final byte[] family = Bytes.toBytes("cf"); HTable table = TEST_UTIL.createTable(tableName, family); TEST_UTIL.waitTableAvailable(tableName.getName(), 5000); try { // put one row Put put = new Put(rowKey); put.add(family, Bytes.toBytes("A"), Bytes.toBytes("a")); put.add(family, Bytes.toBytes("B"), Bytes.toBytes("b")); put.add(family, Bytes.toBytes("C"), Bytes.toBytes("c")); table.put(put); // get row back and assert the values Get get = new Get(rowKey); Result result = table.get(get); assertTrue("Column A value should be a", Bytes.toString(result.getValue(family, Bytes.toBytes("A"))).equals("a")); assertTrue("Column B value should be b", Bytes.toString(result.getValue(family, Bytes.toBytes("B"))).equals("b")); assertTrue("Column C value should be c", Bytes.toString(result.getValue(family, Bytes.toBytes("C"))).equals("c")); // put the same row again with C column deleted RowMutations rm = new RowMutations(rowKey); put = new Put(rowKey); put.add(family, Bytes.toBytes("A"), Bytes.toBytes("a")); put.add(family, Bytes.toBytes("B"), Bytes.toBytes("b")); rm.add(put); Delete del = new Delete(rowKey); del.deleteColumn(family, Bytes.toBytes("C")); rm.add(del); boolean res = table.checkAndMutate(rowKey, family, Bytes.toBytes("A"), CompareFilter.CompareOp.EQUAL, Bytes.toBytes("a"), rm); assertTrue(res); // get row back and assert the values get = new Get(rowKey); result = table.get(get); assertTrue("Column A value should be a", Bytes.toString(result.getValue(family, Bytes.toBytes("A"))).equals("a")); assertTrue("Column B value should be b", Bytes.toString(result.getValue(family, Bytes.toBytes("B"))).equals("b")); assertTrue("Column C should not exist", result.getValue(family, Bytes.toBytes("C")) == null); //Test that we get a region level exception try { Put p = new Put(rowKey); p.add(new byte[]{'b', 'o', 'g', 'u', 's'}, new byte[]{'A'}, new byte[0]); rm = new RowMutations(rowKey); rm.add(p); table.checkAndMutate(rowKey, family, Bytes.toBytes("A"), CompareFilter.CompareOp.EQUAL, Bytes.toBytes("a"), rm); fail("Expected NoSuchColumnFamilyException"); } catch(NoSuchColumnFamilyException e) { } } finally { table.close(); } }
protected Response processException(Throwable exp) { Throwable curr = exp; if(accessDeniedClazz != null) { //some access denied exceptions are buried while (curr != null) { if(accessDeniedClazz.isAssignableFrom(curr.getClass())) { throw new WebApplicationException( Response.status(Response.Status.FORBIDDEN) .type(MIMETYPE_TEXT).entity("Forbidden" + CRLF + StringUtils.stringifyException(exp) + CRLF) .build()); } curr = curr.getCause(); } } //TableNotFound may also be buried one level deep if (exp instanceof TableNotFoundException || exp.getCause() instanceof TableNotFoundException) { throw new WebApplicationException( Response.status(Response.Status.NOT_FOUND) .type(MIMETYPE_TEXT).entity("Not found" + CRLF + StringUtils.stringifyException(exp) + CRLF) .build()); } if (exp instanceof NoSuchColumnFamilyException){ throw new WebApplicationException( Response.status(Response.Status.NOT_FOUND) .type(MIMETYPE_TEXT).entity("Not found" + CRLF + StringUtils.stringifyException(exp) + CRLF) .build()); } if (exp instanceof RuntimeException) { throw new WebApplicationException( Response.status(Response.Status.BAD_REQUEST) .type(MIMETYPE_TEXT).entity("Bad request" + CRLF + StringUtils.stringifyException(exp) + CRLF) .build()); } if (exp instanceof RetriesExhaustedWithDetailsException) { RetriesExhaustedWithDetailsException retryException = (RetriesExhaustedWithDetailsException) exp; processException(retryException.getCause(0)); } throw new WebApplicationException( Response.status(Response.Status.SERVICE_UNAVAILABLE) .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF + StringUtils.stringifyException(exp) + CRLF) .build()); }
@Test public void testRestoreSchemaChange() throws Exception { byte[] TEST_FAMILY2 = Bytes.toBytes("cf2"); HTable table = new HTable(TEST_UTIL.getConfiguration(), tableName); // Add one column family and put some data in it admin.disableTable(tableName); admin.addColumn(tableName, new HColumnDescriptor(TEST_FAMILY2)); admin.enableTable(tableName); assertEquals(2, table.getTableDescriptor().getFamilies().size()); HTableDescriptor htd = admin.getTableDescriptor(tableName); assertEquals(2, htd.getFamilies().size()); SnapshotTestingUtils.loadData(TEST_UTIL, table, 500, TEST_FAMILY2); long snapshot2Rows = snapshot1Rows + 500; assertEquals(snapshot2Rows, TEST_UTIL.countRows(table)); assertEquals(500, TEST_UTIL.countRows(table, TEST_FAMILY2)); Set<String> fsFamilies = getFamiliesFromFS(tableName); assertEquals(2, fsFamilies.size()); table.close(); // Take a snapshot admin.disableTable(tableName); admin.snapshot(snapshotName2, tableName); // Restore the snapshot (without the cf) admin.restoreSnapshot(snapshotName0); assertEquals(1, table.getTableDescriptor().getFamilies().size()); admin.enableTable(tableName); try { TEST_UTIL.countRows(table, TEST_FAMILY2); fail("family '" + Bytes.toString(TEST_FAMILY2) + "' should not exists"); } catch (NoSuchColumnFamilyException e) { // expected } assertEquals(snapshot0Rows, TEST_UTIL.countRows(table)); htd = admin.getTableDescriptor(tableName); assertEquals(1, htd.getFamilies().size()); fsFamilies = getFamiliesFromFS(tableName); assertEquals(1, fsFamilies.size()); table.close(); // Restore back the snapshot (with the cf) admin.disableTable(tableName); admin.restoreSnapshot(snapshotName2); admin.enableTable(tableName); htd = admin.getTableDescriptor(tableName); assertEquals(2, htd.getFamilies().size()); assertEquals(2, table.getTableDescriptor().getFamilies().size()); assertEquals(500, TEST_UTIL.countRows(table, TEST_FAMILY2)); assertEquals(snapshot2Rows, TEST_UTIL.countRows(table)); fsFamilies = getFamiliesFromFS(tableName); assertEquals(2, fsFamilies.size()); table.close(); }
/** * Requirement 8.2 - Batch throws an exception if any of the calls failed. Any successful * mutations that did not throw an exception will succeed. * * Requirement 8.7 - Server errors should populate those return elements in a batch() call with * corresponding Throwables. */ @Test @Category(KnownGap.class) public void testBatchWithException() throws IOException, InterruptedException { // Initialize data Table table = getConnection().getTable(TABLE_NAME); byte[][] rowKeys = dataHelper.randomData("testrow-", 5); byte[][] quals = dataHelper.randomData("qual-", 5); byte[][] values = dataHelper.randomData("value-", 5); Put put0 = new Put(rowKeys[0]).addColumn(COLUMN_FAMILY, quals[0], values[0]); Put put1 = new Put(rowKeys[1]).addColumn(Bytes.toBytes("NO SUCH FAMILY"), quals[1], values[1]); Put put2 = new Put(rowKeys[2]).addColumn(COLUMN_FAMILY, quals[2], values[2]); Put put3 = new Put(rowKeys[3]).addColumn(Bytes.toBytes("NO SUCH FAMILY"), quals[3], values[3]); Put put4 = new Put(rowKeys[4]).addColumn(COLUMN_FAMILY, quals[4], values[4]); List<Row> batch = new ArrayList<Row>(5); Object[] results = new Object[5]; batch.add(put0); batch.add(put1); // This one is bad batch.add(put2); batch.add(put3); // So's this one batch.add(put4); RetriesExhaustedWithDetailsException exception = null; try { table.batch(batch, results); } catch (RetriesExhaustedWithDetailsException e) { exception = e; } Assert.assertNotNull("Exception should have been thrown", exception); Assert.assertEquals("There should have been two exceptions", 2, exception.getNumExceptions()); Assert.assertTrue("Cause should be NoSuchColumnFamilyException", exception.getCause(0) instanceof NoSuchColumnFamilyException); Assert.assertArrayEquals("Row key should be #1", rowKeys[1], exception.getRow(0).getRow()); Assert.assertTrue("Cause should be NoSuchColumnFamilyException", exception.getCause(1) instanceof NoSuchColumnFamilyException); Assert.assertArrayEquals("Row key should be #3", rowKeys[3], exception.getRow(1).getRow()); Assert.assertTrue("#0 should be a Result", results[0] instanceof Result); Assert.assertTrue("#1 should be the exception cause", results[1] instanceof NoSuchColumnFamilyException); Assert.assertTrue("#2 should be a Result", results[2] instanceof Result); Assert.assertTrue("#3 should be the exception cause", results[3] instanceof NoSuchColumnFamilyException); Assert.assertTrue("#4 should be a Result", results[4] instanceof Result); // Check values. The good puts should have worked. List<Get> gets = new ArrayList<Get>(5); for (int i = 0; i < 5; ++i) { gets.add(new Get(rowKeys[i])); } Result[] getResults = table.get(gets); Assert.assertArrayEquals("Row #0 should have value #0", values[0], CellUtil.cloneValue(getResults[0].getColumnLatestCell(COLUMN_FAMILY, quals[0]))); Assert.assertTrue("Row #1 should be empty", getResults[1].isEmpty()); Assert.assertArrayEquals("Row #2 should have value #2", values[2], CellUtil.cloneValue(getResults[2].getColumnLatestCell(COLUMN_FAMILY, quals[2]))); Assert.assertTrue("Row #3 should be empty", getResults[3].isEmpty()); Assert.assertArrayEquals("Row #4 should have value #4", values[4], CellUtil.cloneValue(getResults[4].getColumnLatestCell(COLUMN_FAMILY, quals[4]))); table.close(); }
protected Response processException(Throwable exp) { Throwable curr = exp; if(accessDeniedClazz != null) { //some access denied exceptions are buried while (curr != null) { if(accessDeniedClazz.isAssignableFrom(curr.getClass())) { throw new SecurityException("Unauthorized" + CRLF + StringUtils.stringifyException(exp) + CRLF); } curr = curr.getCause(); } } //TableNotFound may also be buried one level deep if (exp instanceof TableNotFoundException || exp.getCause() instanceof TableNotFoundException) { throw new WebApplicationException( Response.status(Response.Status.NOT_FOUND) .type(MIMETYPE_TEXT).entity("Not found" + CRLF + StringUtils.stringifyException(exp) + CRLF) .build()); } if (exp instanceof NoSuchColumnFamilyException){ throw new WebApplicationException( Response.status(Response.Status.NOT_FOUND) .type(MIMETYPE_TEXT).entity("Not found" + CRLF + StringUtils.stringifyException(exp) + CRLF) .build()); } if (exp instanceof RuntimeException) { throw new WebApplicationException( Response.status(Response.Status.BAD_REQUEST) .type(MIMETYPE_TEXT).entity("Bad request" + CRLF + StringUtils.stringifyException(exp) + CRLF) .build()); } if (exp instanceof RetriesExhaustedWithDetailsException) { RetriesExhaustedWithDetailsException retryException = (RetriesExhaustedWithDetailsException) exp; processException(retryException.getCause(0)); } throw new WebApplicationException( Response.status(Response.Status.SERVICE_UNAVAILABLE) .type(MIMETYPE_TEXT).entity("Unavailable" + CRLF + StringUtils.stringifyException(exp) + CRLF) .build()); }
@Test public void testRestoreSchemaChange() throws Exception { byte[] TEST_FAMILY2 = Bytes.toBytes("cf2"); HTable table = new HTable(TEST_UTIL.getConfiguration(), tableName); // Add one column family and put some data in it admin.disableTable(tableName); admin.addColumn(tableName, new HColumnDescriptor(TEST_FAMILY2)); admin.enableTable(tableName); assertEquals(2, table.getTableDescriptor().getFamilies().size()); HTableDescriptor htd = admin.getTableDescriptor(tableName); assertEquals(2, htd.getFamilies().size()); SnapshotTestingUtils.loadData(TEST_UTIL, table, 500, TEST_FAMILY2); long snapshot2Rows = snapshot1Rows + 500; assertEquals(snapshot2Rows, TEST_UTIL.countRows(table)); assertEquals(500, TEST_UTIL.countRows(table, TEST_FAMILY2)); Set<String> fsFamilies = getFamiliesFromFS(tableName); assertEquals(2, fsFamilies.size()); table.close(); // Take a snapshot admin.disableTable(tableName); admin.snapshot(snapshotName2, tableName); // Restore the snapshot (without the cf) admin.restoreSnapshot(snapshotName0); admin.enableTable(tableName); assertEquals(1, table.getTableDescriptor().getFamilies().size()); try { TEST_UTIL.countRows(table, TEST_FAMILY2); fail("family '" + Bytes.toString(TEST_FAMILY2) + "' should not exists"); } catch (NoSuchColumnFamilyException e) { // expected } assertEquals(snapshot0Rows, TEST_UTIL.countRows(table)); htd = admin.getTableDescriptor(tableName); assertEquals(1, htd.getFamilies().size()); fsFamilies = getFamiliesFromFS(tableName); assertEquals(1, fsFamilies.size()); table.close(); // Restore back the snapshot (with the cf) admin.disableTable(tableName); admin.restoreSnapshot(snapshotName2); admin.enableTable(tableName); htd = admin.getTableDescriptor(tableName); assertEquals(2, htd.getFamilies().size()); assertEquals(2, table.getTableDescriptor().getFamilies().size()); assertEquals(500, TEST_UTIL.countRows(table, TEST_FAMILY2)); assertEquals(snapshot2Rows, TEST_UTIL.countRows(table)); fsFamilies = getFamiliesFromFS(tableName); assertEquals(2, fsFamilies.size()); table.close(); }
@Test public void testRestoreSchemaChange() throws Exception { Table table = TEST_UTIL.getConnection().getTable(tableName); // Add one column family and put some data in it admin.disableTable(tableName); admin.addColumnFamily(tableName, getTestRestoreSchemaChangeHCD()); admin.enableTable(tableName); assertEquals(2, table.getTableDescriptor().getFamilies().size()); HTableDescriptor htd = admin.getTableDescriptor(tableName); assertEquals(2, htd.getFamilies().size()); SnapshotTestingUtils.loadData(TEST_UTIL, tableName, 500, TEST_FAMILY2); long snapshot2Rows = snapshot1Rows + 500L; assertEquals(snapshot2Rows, countRows(table)); assertEquals(500, countRows(table, TEST_FAMILY2)); Set<String> fsFamilies = getFamiliesFromFS(tableName); assertEquals(2, fsFamilies.size()); // Take a snapshot admin.disableTable(tableName); admin.snapshot(snapshotName2, tableName); // Restore the snapshot (without the cf) admin.restoreSnapshot(snapshotName0); admin.enableTable(tableName); assertEquals(1, table.getTableDescriptor().getFamilies().size()); try { countRows(table, TEST_FAMILY2); fail("family '" + Bytes.toString(TEST_FAMILY2) + "' should not exists"); } catch (NoSuchColumnFamilyException e) { // expected } assertEquals(snapshot0Rows, countRows(table)); htd = admin.getTableDescriptor(tableName); assertEquals(1, htd.getFamilies().size()); fsFamilies = getFamiliesFromFS(tableName); assertEquals(1, fsFamilies.size()); // Restore back the snapshot (with the cf) admin.disableTable(tableName); admin.restoreSnapshot(snapshotName2); admin.enableTable(tableName); htd = admin.getTableDescriptor(tableName); assertEquals(2, htd.getFamilies().size()); assertEquals(2, table.getTableDescriptor().getFamilies().size()); assertEquals(500, countRows(table, TEST_FAMILY2)); assertEquals(snapshot2Rows, countRows(table)); fsFamilies = getFamiliesFromFS(tableName); assertEquals(2, fsFamilies.size()); table.close(); }
@Test public void testRowMutation() throws Exception { LOG.info("Starting testRowMutation"); final TableName tableName = TableName.valueOf(name.getMethodName()); Table t = TEST_UTIL.createTable(tableName, FAMILY); byte [][] QUALIFIERS = new byte [][] { Bytes.toBytes("a"), Bytes.toBytes("b") }; RowMutations arm = new RowMutations(ROW); Put p = new Put(ROW); p.addColumn(FAMILY, QUALIFIERS[0], VALUE); arm.add(p); t.mutateRow(arm); Get g = new Get(ROW); Result r = t.get(g); assertEquals(0, Bytes.compareTo(VALUE, r.getValue(FAMILY, QUALIFIERS[0]))); arm = new RowMutations(ROW); p = new Put(ROW); p.addColumn(FAMILY, QUALIFIERS[1], VALUE); arm.add(p); Delete d = new Delete(ROW); d.addColumns(FAMILY, QUALIFIERS[0]); arm.add(d); // TODO: Trying mutateRow again. The batch was failing with a one try only. t.mutateRow(arm); r = t.get(g); assertEquals(0, Bytes.compareTo(VALUE, r.getValue(FAMILY, QUALIFIERS[1]))); assertNull(r.getValue(FAMILY, QUALIFIERS[0])); //Test that we get a region level exception try { arm = new RowMutations(ROW); p = new Put(ROW); p.addColumn(new byte[]{'b', 'o', 'g', 'u', 's'}, QUALIFIERS[0], VALUE); arm.add(p); t.mutateRow(arm); fail("Expected NoSuchColumnFamilyException"); } catch(RetriesExhaustedWithDetailsException e) { for(Throwable rootCause: e.getCauses()){ if(rootCause instanceof NoSuchColumnFamilyException){ return; } } throw e; } }
public RowResultGenerator(final String tableName, final RowSpec rowspec, final Filter filter) throws IllegalArgumentException, IOException { HTablePool pool = RESTServlet.getInstance().getTablePool(); HTableInterface table = pool.getTable(tableName); try { Get get = new Get(rowspec.getRow()); if (rowspec.hasColumns()) { for (byte[] col: rowspec.getColumns()) { byte[][] split = KeyValue.parseColumn(col); if (split.length == 2 && split[1].length != 0) { get.addColumn(split[0], split[1]); } else { get.addFamily(split[0]); } } } else { // rowspec does not explicitly specify columns, return them all for (HColumnDescriptor family: table.getTableDescriptor().getFamilies()) { get.addFamily(family.getName()); } } get.setTimeRange(rowspec.getStartTime(), rowspec.getEndTime()); get.setMaxVersions(rowspec.getMaxVersions()); if (filter != null) { get.setFilter(filter); } Result result = table.get(get); if (result != null && !result.isEmpty()) { valuesI = result.list().iterator(); } } catch (NoSuchColumnFamilyException e) { // Warn here because Stargate will return 404 in the case if multiple // column families were specified but one did not exist -- currently // HBase will fail the whole Get. // Specifying multiple columns in a URI should be uncommon usage but // help to avoid confusion by leaving a record of what happened here in // the log. LOG.warn(StringUtils.stringifyException(e)); } finally { pool.putTable(table); } }
@Test public void testRestoreSchemaChange() throws IOException { byte[] TEST_FAMILY2 = Bytes.toBytes("cf2"); HTable table = new HTable(TEST_UTIL.getConfiguration(), tableName); // Add one column family and put some data in it admin.disableTable(tableName); admin.addColumn(tableName, new HColumnDescriptor(TEST_FAMILY2)); admin.enableTable(tableName); assertEquals(2, table.getTableDescriptor().getFamilies().size()); HTableDescriptor htd = admin.getTableDescriptor(tableName); assertEquals(2, htd.getFamilies().size()); loadData(table, 500, TEST_FAMILY2); long snapshot2Rows = snapshot1Rows + 500; assertEquals(snapshot2Rows, TEST_UTIL.countRows(table)); assertEquals(500, TEST_UTIL.countRows(table, TEST_FAMILY2)); Set<String> fsFamilies = getFamiliesFromFS(tableName); assertEquals(2, fsFamilies.size()); table.close(); // Take a snapshot admin.disableTable(tableName); admin.snapshot(snapshotName2, tableName); // Restore the snapshot (without the cf) admin.restoreSnapshot(snapshotName0); assertEquals(1, table.getTableDescriptor().getFamilies().size()); admin.enableTable(tableName); try { TEST_UTIL.countRows(table, TEST_FAMILY2); fail("family '" + Bytes.toString(TEST_FAMILY2) + "' should not exists"); } catch (NoSuchColumnFamilyException e) { // expected } assertEquals(snapshot0Rows, TEST_UTIL.countRows(table)); htd = admin.getTableDescriptor(tableName); assertEquals(1, htd.getFamilies().size()); fsFamilies = getFamiliesFromFS(tableName); assertEquals(1, fsFamilies.size()); table.close(); // Restore back the snapshot (with the cf) admin.disableTable(tableName); admin.restoreSnapshot(snapshotName2); admin.enableTable(tableName); htd = admin.getTableDescriptor(tableName); assertEquals(2, htd.getFamilies().size()); assertEquals(2, table.getTableDescriptor().getFamilies().size()); assertEquals(500, TEST_UTIL.countRows(table, TEST_FAMILY2)); assertEquals(snapshot2Rows, TEST_UTIL.countRows(table)); fsFamilies = getFamiliesFromFS(tableName); assertEquals(2, fsFamilies.size()); table.close(); }