@Override protected HBaseServiceHandler createHandler(Configuration conf, UserProvider userProvider) throws IOException { return new ThriftHBaseServiceHandler(conf, userProvider); }
@Override public int openScanner(ByteBuffer table, TScan scan) throws TIOError, TException { Table htable = getTable(table); ResultScanner resultScanner = null; try { resultScanner = htable.getScanner(scanFromThrift(scan)); } catch (IOException e) { throw getTIOError(e); } finally { closeTable(htable); } return addScanner(resultScanner); }
@Override public List<TResult> getScannerRows(int scannerId, int numRows) throws TIOError, TIllegalArgument, TException { ResultScanner scanner = getScanner(scannerId); if (scanner == null) { TIllegalArgument ex = new TIllegalArgument(); ex.setMessage("Invalid scanner Id"); throw ex; } try { connectionCache.updateConnectionAccessTime(); return resultsFromHBase(scanner.next(numRows)); } catch (IOException e) { throw getTIOError(e); } }
private void checkReadOnlyMode() throws TIOError { if (isReadOnly()) { throw getTIOError(ioe); } }
@Override public void deleteSingle(ByteBuffer table, TDelete deleteSingle) throws TIOError, TException { checkReadOnlyMode(); Table htable = getTable(table); try { htable.delete(deleteFromThrift(deleteSingle)); } catch (IOException e) { throw getTIOError(e); } finally { closeTable(htable); } }
@Override public boolean exists(ByteBuffer table, TGet get) throws TIOError, TException { Table htable = getTable(table); try { return htable.exists(getFromThrift(get)); } catch (IOException e) { throw getTIOError(e); } finally { closeTable(htable); } }
ThriftHBaseServiceHandler handler = new ThriftHBaseServiceHandler(conf, UserProvider.instantiate(conf)); for (int i = 0; i < numTrials; i++) { TPut put = new TPut(wrap(Bytes.toBytes("testScan" + i)), columnValues); handler.put(table, put); int scanId = handler.openScanner(table, scan); for (int i = 0; i < numTrials; i++) { List<TResult> results = handler.getScannerRows(scanId, 1); assertArrayEquals(Bytes.toBytes("testScan" + i), results.get(0).getRow()); Thread.sleep(trialPause);
@Test public void testAppend() throws Exception { ThriftHBaseServiceHandler handler = createHandler(); byte[] rowName = Bytes.toBytes("testAppend"); ByteBuffer table = wrap(tableAname); byte[] v1 = Bytes.toBytes("42"); byte[] v2 = Bytes.toBytes("23"); List<TColumnValue> columnValues = new ArrayList<>(1); columnValues.add(new TColumnValue(wrap(familyAname), wrap(qualifierAname), wrap(v1))); TPut put = new TPut(wrap(rowName), columnValues); put.setColumnValues(columnValues); handler.put(table, put); List<TColumnValue> appendColumns = new ArrayList<>(1); appendColumns.add(new TColumnValue(wrap(familyAname), wrap(qualifierAname), wrap(v2))); TAppend append = new TAppend(wrap(rowName), appendColumns); handler.append(table, append); TGet get = new TGet(wrap(rowName)); TResult result = handler.get(table, get); assertArrayEquals(rowName, result.getRow()); assertEquals(1, result.getColumnValuesSize()); TColumnValue columnValue = result.getColumnValues().get(0); assertArrayEquals(Bytes.add(v1, v2), columnValue.getValue()); }
TProtocolFactory protocolFactory = getTProtocolFactory(compact); final ThriftHBaseServiceHandler hbaseHandler = new ThriftHBaseServiceHandler(conf, userProvider); THBaseService.Iface handler = ThriftHBaseServiceHandler.newInstance(hbaseHandler, metrics); final THBaseService.Processor p = new THBaseService.Processor(handler); conf.setBoolean("hbase.regionserver.thrift.compact", compact);
TProtocolFactory protocolFactory = getTProtocolFactory(compact); THBaseService.Iface handler = ThriftHBaseServiceHandler.newInstance(conf, metrics); THBaseService.Processor processor = new THBaseService.Processor(handler); conf.setBoolean("hbase.regionserver.thrift.compact", compact);
private void closeTable(Table table) throws TIOError { try { table.close(); } catch (IOException e) { throw getTIOError(e); } }
@Override public void closeScanner(int scannerId) throws TIOError, TIllegalArgument, TException { LOG.debug("scannerClose: id=" + scannerId); ResultScanner scanner = getScanner(scannerId); if (scanner == null) { String message = "scanner ID is invalid"; LOG.warn(message); TIllegalArgument ex = new TIllegalArgument(); ex.setMessage("Invalid scanner Id"); throw ex; } scanner.close(); removeScanner(scannerId); }
handler.put(table, putB); assertFalse(handler.checkAndDelete(table, wrap(rowName), wrap(familyAname), wrap(qualifierAname), wrap(valueAname), delete)); TResult result = handler.get(table, get); assertArrayEquals(rowName, result.getRow()); assertTColumnValuesEqual(columnValuesB, result.getColumnValues()); handler.put(table, putA); assertTrue(handler.checkAndDelete(table, wrap(rowName), wrap(familyAname), wrap(qualifierAname), wrap(valueAname), delete)); result = handler.get(table, get); assertFalse(result.isSetRow()); assertEquals(0, result.getColumnValuesSize());
@Override public boolean checkAndMutate(ByteBuffer table, ByteBuffer row, ByteBuffer family, ByteBuffer qualifier, TCompareOp compareOp, ByteBuffer value, TRowMutations rowMutations) throws TIOError, TException { checkReadOnlyMode(); try (final Table htable = getTable(table)) { return htable.checkAndMutate(byteBufferToByteArray(row), byteBufferToByteArray(family)) .qualifier(byteBufferToByteArray(qualifier)) .ifMatches(compareOpFromThrift(compareOp), byteBufferToByteArray(value)) .thenMutate(rowMutationsFromThrift(rowMutations)); } catch (IOException e) { throw getTIOError(e); } }
@Test public void testCheckAndDeleteWithReadOnly() throws Exception { ThriftHBaseServiceHandler handler = createHandler(); byte[] rowName = Bytes.toBytes("testCheckAndDelete"); ByteBuffer table = wrap(tableAname); TDelete delete = new TDelete(wrap(rowName)); boolean exceptionCaught = false; try { handler.checkAndDelete(table, wrap(rowName), wrap(familyAname), wrap(qualifierAname), wrap(valueAname), delete); } catch (TIOError e) { exceptionCaught = true; assertTrue(e.getCause() instanceof DoNotRetryIOException); assertEquals("Thrift Server is in Read-only mode.", e.getMessage()); } finally { assertTrue(exceptionCaught); } }
@Test public void testAppendWithReadOnly() throws Exception { ThriftHBaseServiceHandler handler = createHandler(); byte[] rowName = Bytes.toBytes("testAppend"); ByteBuffer table = wrap(tableAname); byte[] v1 = Bytes.toBytes("42"); List<TColumnValue> appendColumns = new ArrayList<>(1); appendColumns.add(new TColumnValue(wrap(familyAname), wrap(qualifierAname), wrap(v1))); TAppend append = new TAppend(wrap(rowName), appendColumns); boolean exceptionCaught = false; try { handler.append(table, append); } catch (TIOError e) { exceptionCaught = true; assertTrue(e.getCause() instanceof DoNotRetryIOException); assertEquals("Thrift Server is in Read-only mode.", e.getMessage()); } finally { assertTrue(exceptionCaught); } }
@Override public void closeScanner(int scannerId) throws TIOError, TIllegalArgument, TException { if (removeScanner(scannerId) == null) { TIllegalArgument ex = new TIllegalArgument(); ex.setMessage("Invalid scanner Id"); throw ex; } }
@Override public void put(ByteBuffer table, TPut put) throws TIOError, TException { checkReadOnlyMode(); Table htable = getTable(table); try { htable.put(putFromThrift(put)); } catch (IOException e) { throw getTIOError(e); } finally { closeTable(htable); } }
@Override public TResult get(ByteBuffer table, TGet get) throws TIOError, TException { Table htable = getTable(table); try { return resultFromHBase(htable.get(getFromThrift(get))); } catch (IOException e) { throw getTIOError(e); } finally { closeTable(htable); } }
ThriftHBaseServiceHandler handler = new ThriftHBaseServiceHandler(conf, UserProvider.instantiate(conf)); for (int i = 0; i < numTrials; i++) { TPut put = new TPut(wrap(Bytes.toBytes("testScan" + i)), columnValues); handler.put(table, put); int scanId = handler.openScanner(table, scan); for (int i = 0; i < numTrials; i++) { List<TResult> results = handler.getScannerRows(scanId, 1); assertArrayEquals(Bytes.toBytes("testScan" + i), results.get(0).getRow()); Thread.sleep(trialPause);