private void testVerifyMetaRegionLocationWithException(Exception ex) throws IOException, InterruptedException, KeeperException, ServiceException { // Mock an ClientProtocol. final ClientProtos.ClientService.BlockingInterface implementation = Mockito.mock(ClientProtos.ClientService.BlockingInterface.class); ClusterConnection connection = mockConnection(null, implementation); // If a 'get' is called on mocked interface, throw connection refused. Mockito.when(implementation.get((RpcController) Mockito.any(), (GetRequest) Mockito.any())). thenThrow(new ServiceException(ex)); long timeout = UTIL.getConfiguration(). getLong("hbase.catalog.verification.timeout", 1000); MetaTableLocator.setMetaLocation(this.watcher, SN, RegionState.State.OPENING); assertFalse(new MetaTableLocator().verifyMetaRegionLocation( connection, watcher, timeout)); MetaTableLocator.setMetaLocation(this.watcher, SN, RegionState.State.OPEN); assertFalse(new MetaTableLocator().verifyMetaRegionLocation( connection, watcher, timeout)); }
/** * Create a new protocol buffer GetRequest to get a row, all columns in a family. * If there is no such row, return the closest row before it. * * @param regionName the name of the region to get * @param row the row to get * @param family the column family to get * should return the immediate row before * @return a protocol buffer GetReuqest */ public static GetRequest buildGetRowOrBeforeRequest( final byte[] regionName, final byte[] row, final byte[] family) { GetRequest.Builder builder = GetRequest.newBuilder(); RegionSpecifier region = buildRegionSpecifier( RegionSpecifierType.REGION_NAME, regionName); builder.setRegion(region); Column.Builder columnBuilder = Column.newBuilder(); columnBuilder.setFamily(ByteStringer.wrap(family)); ClientProtos.Get.Builder getBuilder = ClientProtos.Get.newBuilder(); getBuilder.setRow(ByteStringer.wrap(row)); getBuilder.addColumn(columnBuilder.build()); getBuilder.setClosestRowBefore(true); builder.setGet(getBuilder.build()); return builder.build(); }
/** * A helper to get a row of the closet one before using client protocol. * * @param client * @param regionName * @param row * @param family * @return the row or the closestRowBefore if it doesn't exist * @throws IOException * @deprecated since 0.99 - use reversed scanner instead. */ @Deprecated public static Result getRowOrBefore(final ClientService.BlockingInterface client, final byte[] regionName, final byte[] row, final byte[] family) throws IOException { GetRequest request = RequestConverter.buildGetRowOrBeforeRequest( regionName, row, family); try { GetResponse response = client.get(null, request); if (!response.hasResult()) return null; return toResult(response.getResult()); } catch (ServiceException se) { throw getRemoteException(se); } }
static GetResponse doMetaGetResponse(final SortedMap<byte [], Pair<HRegionInfo, ServerName>> meta, final GetRequest request) { ClientProtos.Result.Builder resultBuilder = ClientProtos.Result.newBuilder(); ByteString row = request.getGet().getRow(); Pair<HRegionInfo, ServerName> p = meta.get(row.toByteArray()); if (p == null) { if (request.getGet().getClosestRowBefore()) { byte [] bytes = row.toByteArray(); SortedMap<byte [], Pair<HRegionInfo, ServerName>> head = bytes != null? meta.headMap(bytes): meta; p = head == null? null: head.get(head.lastKey()); } } if (p != null) { resultBuilder.addCell(getRegionInfo(row, p.getFirst())); resultBuilder.addCell(getServer(row, p.getSecond())); } resultBuilder.addCell(getStartCode(row)); GetResponse.Builder builder = GetResponse.newBuilder(); builder.setResult(resultBuilder.build()); return builder.build(); }
private void testVerifyMetaRegionLocationWithException(Exception ex) throws IOException, InterruptedException, KeeperException, ServiceException { // Mock an ClientProtocol. final ClientProtos.ClientService.BlockingInterface implementation = Mockito.mock(ClientProtos.ClientService.BlockingInterface.class); HConnection connection = mockConnection(null, implementation); // If a 'get' is called on mocked interface, throw connection refused. Mockito.when(implementation.get((RpcController) Mockito.any(), (GetRequest) Mockito.any())). thenThrow(new ServiceException(ex)); // Now start up the catalogtracker with our doctored Connection. final CatalogTracker ct = constructAndStartCatalogTracker(connection); MetaRegionTracker.setMetaLocation(this.watcher, SN); long timeout = UTIL.getConfiguration(). getLong("hbase.catalog.verification.timeout", 1000); Assert.assertFalse(ct.verifyMetaRegionLocation(timeout)); }
/** * Create a new protocol buffer GetRequest to get a row, all columns in a family. * If there is no such row, return the closest row before it. * * @param regionName the name of the region to get * @param row the row to get * @param family the column family to get * should return the immediate row before * @return a protocol buffer GetReuqest */ public static GetRequest buildGetRowOrBeforeRequest( final byte[] regionName, final byte[] row, final byte[] family) { GetRequest.Builder builder = GetRequest.newBuilder(); RegionSpecifier region = buildRegionSpecifier( RegionSpecifierType.REGION_NAME, regionName); builder.setRegion(region); Column.Builder columnBuilder = Column.newBuilder(); columnBuilder.setFamily(HBaseZeroCopyByteString.wrap(family)); ClientProtos.Get.Builder getBuilder = ClientProtos.Get.newBuilder(); getBuilder.setRow(HBaseZeroCopyByteString.wrap(row)); getBuilder.addColumn(columnBuilder.build()); getBuilder.setClosestRowBefore(true); builder.setGet(getBuilder.build()); return builder.build(); }
/** * Create a new protocol buffer GetRequest to get a row, all columns in a family. * If there is no such row, return the closest row before it. * * @param regionName the name of the region to get * @param row the row to get * @param family the column family to get * should return the immediate row before * @return a protocol buffer GetReuqest */ public static GetRequest buildGetRowOrBeforeRequest( final byte[] regionName, final byte[] row, final byte[] family) { GetRequest.Builder builder = GetRequest.newBuilder(); RegionSpecifier region = buildRegionSpecifier( RegionSpecifierType.REGION_NAME, regionName); builder.setRegion(region); Column.Builder columnBuilder = Column.newBuilder(); columnBuilder.setFamily(ZeroCopyLiteralByteString.wrap(family)); ClientProtos.Get.Builder getBuilder = ClientProtos.Get.newBuilder(); getBuilder.setRow(ZeroCopyLiteralByteString.wrap(row)); getBuilder.addColumn(columnBuilder.build()); getBuilder.setClosestRowBefore(true); builder.setGet(getBuilder.build()); return builder.build(); }
/** * Create a new protocol buffer GetRequest to get a row, all columns in a family. * If there is no such row, return the closest row before it. * * @param regionName the name of the region to get * @param row the row to get * @param family the column family to get * should return the immediate row before * @return a protocol buffer GetReuqest */ public static GetRequest buildGetRowOrBeforeRequest( final byte[] regionName, final byte[] row, final byte[] family) { GetRequest.Builder builder = GetRequest.newBuilder(); RegionSpecifier region = buildRegionSpecifier( RegionSpecifierType.REGION_NAME, regionName); builder.setClosestRowBefore(true); builder.setRegion(region); Column.Builder columnBuilder = Column.newBuilder(); columnBuilder.setFamily(ByteString.copyFrom(family)); ClientProtos.Get.Builder getBuilder = ClientProtos.Get.newBuilder(); getBuilder.setRow(ByteString.copyFrom(row)); getBuilder.addColumn(columnBuilder.build()); builder.setGet(getBuilder.build()); return builder.build(); }
/** * {@inheritDoc} */ @Override public boolean exists(final Get get) throws IOException { return new ServerCallable<Boolean>(connection, tableName, get.getRow(), operationTimeout) { public Boolean call() throws IOException { try { GetRequest request = RequestConverter.buildGetRequest( location.getRegionInfo().getRegionName(), get, true); GetResponse response = server.get(null, request); return response.getExists(); } catch (ServiceException se) { throw ProtobufUtil.getRemoteException(se); } } }.withRetries(); }
private void testVerifyMetaRegionLocationWithException(Exception ex) throws IOException, InterruptedException, KeeperException, ServiceException { // Mock an ClientProtocol. final ClientProtocol implementation = Mockito.mock(ClientProtocol.class); HConnection connection = mockConnection(null, implementation); try { // If a 'get' is called on mocked interface, throw connection refused. Mockito.when(implementation.get((RpcController) Mockito.any(), (GetRequest) Mockito.any())). thenThrow(new ServiceException(ex)); // Now start up the catalogtracker with our doctored Connection. final CatalogTracker ct = constructAndStartCatalogTracker(connection); try { RootRegionTracker.setRootLocation(this.watcher, SN); long timeout = UTIL.getConfiguration(). getLong("hbase.catalog.verification.timeout", 1000); Assert.assertFalse(ct.verifyMetaRegionLocation(timeout)); } finally { // Clean out root location or later tests will be confused... they // presume start fresh in zk. RootRegionTracker.deleteRootLocation(this.watcher); } } finally { // Clear out our doctored connection or could mess up subsequent tests. HConnectionManager.deleteConnection(UTIL.getConfiguration(), true); } }
/** * Test normal operations */ @Test public void testMetaLookup() throws IOException, InterruptedException, ServiceException, KeeperException { final ClientProtos.ClientService.BlockingInterface client = Mockito.mock(ClientProtos.ClientService.BlockingInterface.class); Mockito.when(client.get((RpcController)Mockito.any(), (GetRequest)Mockito.any())). thenReturn(GetResponse.newBuilder().build()); final MetaTableLocator mtl = new MetaTableLocator(); assertNull(mtl.getMetaRegionLocation(this.watcher)); for (RegionState.State state : RegionState.State.values()) { if (state.equals(RegionState.State.OPEN)) continue; MetaTableLocator.setMetaLocation(this.watcher, SN, state); assertNull(mtl.getMetaRegionLocation(this.watcher)); assertEquals(state, MetaTableLocator.getMetaRegionState(this.watcher).getState()); } MetaTableLocator.setMetaLocation(this.watcher, SN, RegionState.State.OPEN); assertEquals(mtl.getMetaRegionLocation(this.watcher), SN); assertEquals(RegionState.State.OPEN, MetaTableLocator.getMetaRegionState(this.watcher).getState()); mtl.deleteMetaLocation(this.watcher); assertNull(MetaTableLocator.getMetaRegionState(this.watcher).getServerName()); assertEquals(MetaTableLocator.getMetaRegionState(this.watcher).getState(), RegionState.State.OFFLINE); assertNull(mtl.getMetaRegionLocation(this.watcher)); }
/** * Test interruptable while blocking wait on meta. * @throws IOException * @throws ServiceException * @throws InterruptedException */ @Test public void testInterruptWaitOnMeta() throws IOException, InterruptedException, ServiceException { final ClientProtos.ClientService.BlockingInterface client = Mockito.mock(ClientProtos.ClientService.BlockingInterface.class); Mockito.when(client.get((RpcController)Mockito.any(), (GetRequest)Mockito.any())). thenReturn(GetResponse.newBuilder().build()); final MetaTableLocator mtl = new MetaTableLocator(); ServerName meta = new MetaTableLocator().getMetaRegionLocation(this.watcher); assertNull(meta); Thread t = new Thread() { @Override public void run() { try { mtl.waitMetaRegionLocation(watcher); } catch (InterruptedException e) { throw new RuntimeException("Interrupted", e); } } }; t.start(); while (!t.isAlive()) Threads.sleep(1); Threads.sleep(1); assertTrue(t.isAlive()); mtl.stop(); // Join the thread... should exit shortly. t.join(); }
@Override public GetResponse get(RpcController controller, GetRequest request) throws ServiceException { byte[] regionName = request.getRegion().getValue().toByteArray(); Map<byte [], Result> m = this.gets.get(regionName); GetResponse.Builder builder = GetResponse.newBuilder(); if (m != null) { byte[] row = request.getGet().getRow().toByteArray(); builder.setResult(ProtobufUtil.toResult(m.get(row))); } return builder.build(); }
@Test public void testQosFunctionForMeta() throws IOException { priority = regionServer.rpcServices.getPriority(); RequestHeader.Builder headerBuilder = RequestHeader.newBuilder(); //create a rpc request that has references to hbase:meta region and also //uses one of the known argument classes (known argument classes are //listed in HRegionServer.QosFunctionImpl.knownArgumentClasses) headerBuilder.setMethodName("foo"); GetRequest.Builder getRequestBuilder = GetRequest.newBuilder(); RegionSpecifier.Builder regionSpecifierBuilder = RegionSpecifier.newBuilder(); regionSpecifierBuilder.setType(RegionSpecifierType.REGION_NAME); ByteString name = ByteStringer.wrap(HRegionInfo.FIRST_META_REGIONINFO.getRegionName()); regionSpecifierBuilder.setValue(name); RegionSpecifier regionSpecifier = regionSpecifierBuilder.build(); getRequestBuilder.setRegion(regionSpecifier); Get.Builder getBuilder = Get.newBuilder(); getBuilder.setRow(ByteStringer.wrap("somerow".getBytes())); getRequestBuilder.setGet(getBuilder.build()); GetRequest getRequest = getRequestBuilder.build(); RequestHeader header = headerBuilder.build(); HRegion mockRegion = Mockito.mock(HRegion.class); HRegionServer mockRS = Mockito.mock(HRegionServer.class); RSRpcServices mockRpc = Mockito.mock(RSRpcServices.class); Mockito.when(mockRS.getRSRpcServices()).thenReturn(mockRpc); HRegionInfo mockRegionInfo = Mockito.mock(HRegionInfo.class); Mockito.when(mockRpc.getRegion((RegionSpecifier) Mockito.any())).thenReturn(mockRegion); Mockito.when(mockRegion.getRegionInfo()).thenReturn(mockRegionInfo); Mockito.when(mockRegionInfo.isSystemTable()).thenReturn(true); // Presume type. ((AnnotationReadingPriorityFunction)priority).setRegionServer(mockRS); assertEquals(HConstants.SYSTEMTABLE_QOS, priority.getPriority(header, getRequest, User.createUserForTesting(regionServer.conf, "someuser", new String[]{"somegroup"}))); }
/** * Create a protocol buffer GetRequest for a client Get * * @param regionName the name of the region to get * @param get the client Get * @return a protocol buffer GetRequest */ public static GetRequest buildGetRequest(final byte[] regionName, final Get get) throws IOException { GetRequest.Builder builder = GetRequest.newBuilder(); RegionSpecifier region = buildRegionSpecifier( RegionSpecifierType.REGION_NAME, regionName); builder.setRegion(region); builder.setGet(ProtobufUtil.toGet(get)); return builder.build(); }
RpcTimeoutConnection(Configuration conf, boolean managed, ExecutorService pool, User user) throws IOException { super(conf, managed); // Mock up my stub so an exists call -- which turns into a get -- throws an exception this.stub = Mockito.mock(ClientService.BlockingInterface.class); try { Mockito.when(stub.get((RpcController)Mockito.any(), (ClientProtos.GetRequest)Mockito.any())). thenThrow(new ServiceException(new RegionServerStoppedException("From Mockito"))); } catch (ServiceException e) { throw new IOException(e); } }
@Override public GetResponse get(RpcController controller, GetRequest request) throws ServiceException { boolean metaRegion = isMetaRegion(request.getRegion().getValue().toByteArray(), request.getRegion().getType()); if (!metaRegion) { return doGetResponse(request); } return doMetaGetResponse(meta, request); }
private GetResponse doGetResponse(GetRequest request) { ClientProtos.Result.Builder resultBuilder = ClientProtos.Result.newBuilder(); ByteString row = request.getGet().getRow(); resultBuilder.addCell(getStartCode(row)); GetResponse.Builder builder = GetResponse.newBuilder(); builder.setResult(resultBuilder.build()); return builder.build(); }
@Test public void testQosFunctionForMeta() throws IOException { priority = regionServer.rpcServices.getPriority(); RequestHeader.Builder headerBuilder = RequestHeader.newBuilder(); //create a rpc request that has references to hbase:meta region and also //uses one of the known argument classes (known argument classes are //listed in HRegionServer.QosFunctionImpl.knownArgumentClasses) headerBuilder.setMethodName("foo"); GetRequest.Builder getRequestBuilder = GetRequest.newBuilder(); RegionSpecifier.Builder regionSpecifierBuilder = RegionSpecifier.newBuilder(); regionSpecifierBuilder.setType(RegionSpecifierType.REGION_NAME); ByteString name = ByteStringer.wrap(HRegionInfo.FIRST_META_REGIONINFO.getRegionName()); regionSpecifierBuilder.setValue(name); RegionSpecifier regionSpecifier = regionSpecifierBuilder.build(); getRequestBuilder.setRegion(regionSpecifier); Get.Builder getBuilder = Get.newBuilder(); getBuilder.setRow(ByteStringer.wrap("somerow".getBytes())); getRequestBuilder.setGet(getBuilder.build()); GetRequest getRequest = getRequestBuilder.build(); RequestHeader header = headerBuilder.build(); HRegion mockRegion = Mockito.mock(HRegion.class); HRegionServer mockRS = Mockito.mock(HRegionServer.class); RSRpcServices mockRpc = Mockito.mock(RSRpcServices.class); Mockito.when(mockRS.getRSRpcServices()).thenReturn(mockRpc); HRegionInfo mockRegionInfo = Mockito.mock(HRegionInfo.class); Mockito.when(mockRpc.getRegion((RegionSpecifier)Mockito.any())).thenReturn(mockRegion); Mockito.when(mockRegion.getRegionInfo()).thenReturn(mockRegionInfo); Mockito.when(mockRegionInfo.isSystemTable()).thenReturn(true); // Presume type. ((AnnotationReadingPriorityFunction)priority).setRegionServer(mockRS); assertEquals(HConstants.SYSTEMTABLE_QOS, priority.getPriority(header, getRequest)); }
/** * Test interruptable while blocking wait on meta. * @throws IOException * @throws ServiceException * @throws InterruptedException */ @Test public void testInterruptWaitOnMeta() throws IOException, InterruptedException, ServiceException { final ClientProtos.ClientService.BlockingInterface client = Mockito.mock(ClientProtos.ClientService.BlockingInterface.class); HConnection connection = mockConnection(null, client); Mockito.when(client.get((RpcController)Mockito.any(), (GetRequest)Mockito.any())). thenReturn(GetResponse.newBuilder().build()); final CatalogTracker ct = constructAndStartCatalogTracker(connection); ServerName meta = ct.getMetaLocation(); Assert.assertNull(meta); Thread t = new Thread() { @Override public void run() { try { ct.waitForMeta(); } catch (InterruptedException e) { throw new RuntimeException("Interrupted", e); } } }; t.start(); while (!t.isAlive()) Threads.sleep(1); Threads.sleep(1); assertTrue(t.isAlive()); ct.stop(); // Join the thread... should exit shortly. t.join(); }
@Test public void testQosFunctionForMeta() throws IOException { priority = regionServer.getPriority(); RequestHeader.Builder headerBuilder = RequestHeader.newBuilder(); //create a rpc request that has references to hbase:meta region and also //uses one of the known argument classes (known argument classes are //listed in HRegionServer.QosFunctionImpl.knownArgumentClasses) headerBuilder.setMethodName("foo"); GetRequest.Builder getRequestBuilder = GetRequest.newBuilder(); RegionSpecifier.Builder regionSpecifierBuilder = RegionSpecifier.newBuilder(); regionSpecifierBuilder.setType(RegionSpecifierType.REGION_NAME); ByteString name = HBaseZeroCopyByteString.wrap(HRegionInfo.FIRST_META_REGIONINFO.getRegionName()); regionSpecifierBuilder.setValue(name); RegionSpecifier regionSpecifier = regionSpecifierBuilder.build(); getRequestBuilder.setRegion(regionSpecifier); Get.Builder getBuilder = Get.newBuilder(); getBuilder.setRow(HBaseZeroCopyByteString.wrap("somerow".getBytes())); getRequestBuilder.setGet(getBuilder.build()); GetRequest getRequest = getRequestBuilder.build(); RequestHeader header = headerBuilder.build(); HRegion mockRegion = Mockito.mock(HRegion.class); HRegionServer mockRS = Mockito.mock(HRegionServer.class); HRegionInfo mockRegionInfo = Mockito.mock(HRegionInfo.class); Mockito.when(mockRS.getRegion((RegionSpecifier)Mockito.any())).thenReturn(mockRegion); Mockito.when(mockRegion.getRegionInfo()).thenReturn(mockRegionInfo); Mockito.when(mockRegionInfo.isMetaTable()).thenReturn(true); // Presume type. ((AnnotationReadingPriorityFunction)priority).setRegionServer(mockRS); assertEquals(HConstants.HIGH_QOS, priority.getPriority(header, getRequest)); }