throws IOException { ConnectionImplementation c = Mockito.mock(ConnectionImplementation.class); Mockito.when(c.getConfiguration()).thenReturn(conf); Mockito.doNothing().when(c).close(); Mockito.when(c.getRegionLocation((TableName) Mockito.any(), (byte[]) Mockito.any(), Mockito.anyBoolean())). thenReturn(loc); Mockito.when(c.locateRegion((TableName) Mockito.any(), (byte[]) Mockito.any())). thenReturn(loc); Mockito.when(c.locateRegion((TableName) Mockito.any(), (byte[]) Mockito.any(), Mockito.anyBoolean(), Mockito.anyBoolean(), Mockito.anyInt())) .thenReturn(new RegionLocations(loc)); if (admin != null) { Mockito.when(c.getAdmin(Mockito.any())). thenReturn(admin); Mockito.when(c.getClient(Mockito.any())). thenReturn(client); Mockito.when(c.getNonceGenerator()).thenReturn(ng); Mockito.when(c.getAsyncProcess()).thenReturn( new AsyncProcess(c, conf, RpcRetryingCallerFactory.instantiate(conf), RpcControllerFactory.instantiate(conf))); Mockito.when(c.getNewRpcRetryingCallerFactory(conf)).thenReturn( RpcRetryingCallerFactory.instantiate(conf, RetryingCallerInterceptorFactory.NO_OP_INTERCEPTOR, null)); Mockito.when(c.getRpcControllerFactory()).thenReturn(Mockito.mock(RpcControllerFactory.class));
@BeforeClass public static void beforeClass() { ConnectionImplementation mocked = Mockito.mock(ConnectionImplementation.class); Mockito.when(mocked.toString()).thenReturn("mocked-connection"); Mockito.when(mocked.getCurrentBatchPool()).thenReturn(BATCH_POOL); METRICS = new MetricsConnection(mocked); }
@Override public MasterKeepAliveConnection getMaster() throws IOException { if (this.localHostClient instanceof MasterService.BlockingInterface) { return new ShortCircuitMasterConnection( (MasterService.BlockingInterface) this.localHostClient); } return super.getMaster(); } }
RegionLocations locations = null; if (useCache) { locations = getCachedLocation(tableName, metaCacheKey); if (locations != null && locations.getRegionLocation(replicaId) != null) { return locations; locations = getCachedLocation(tableName, metaCacheKey); if (locations != null && locations.getRegionLocation(replicaId) != null) { return locations; locations = get(this.registry.getMetaRegionLocation()); if (locations != null) { cacheLocation(tableName, locations);
Mockito.when(mockConnection.getConfiguration()).thenReturn(conf); Mockito.when(mockConnection.getMaster()).thenReturn(mockMaster); Mockito.when(controllerFactory.newController()).thenReturn( Mockito.mock(HBaseRpcController.class)); Mockito.when(mockConnection.getRpcRetryingCallerFactory()).thenReturn(callerFactory); Mockito.when(mockConnection.getRpcControllerFactory()).thenReturn(controllerFactory);
RegionLocations locations = getCachedLocation(tableName, row); if (locations != null && locations.getRegionLocation(replicaId) != null) { return locations; RegionLocations locations = getCachedLocation(tableName, row); if (locations != null && locations.getRegionLocation(replicaId) != null) { return locations; try { if (useCache) {// re-check cache after get lock RegionLocations locations = getCachedLocation(tableName, row); if (locations != null && locations.getRegionLocation(replicaId) != null) { return locations; relocateRegion(TableName.META_TABLE_NAME, HConstants.EMPTY_START_ROW, RegionInfo.DEFAULT_REPLICA_ID); try (ReversedClientScanner rcs = new ReversedClientScanner(conf, s, TableName.META_TABLE_NAME, this, rpcCallerFactory, rpcControllerFactory, getMetaLookupPool(), metaReplicaCallTimeoutScanInMicroSecond)) { boolean tableNotFound = true; for (;;) { if (isDeadServer(serverName)) { throw new RegionServerStoppedException( "hbase:meta says the region " + regionInfo.getRegionNameAsString() + cacheLocation(tableName, locations); return locations;
final RegionLocations oldLocations = getCachedLocation(tableName, rowkey); HRegionLocation oldLocation = null; if (oldLocations != null) { updateCachedLocation( regionInfo, source, rme.getServerName(), rme.getLocationSeqNum()); return;
(ConnectionImplementation) ConnectionFactory.createConnection(conf); try { Table table = conn.getTable(TABLE_NAME); byte[] row = Bytes.toBytes("row1"); assertNull(conn.getCachedLocation(TABLE_NAME, row)); } else if (success) { assertNotNull(conn.getCachedLocation(TABLE_NAME, row)); conn.close();
private void checkClosed() throws DoNotRetryIOException { if (this.closed) { throw new DoNotRetryIOException(toString() + " closed"); } }
/** * Get a Mocked {@link ClusterConnection} that goes with the passed <code>conf</code> * configuration instance. Minimally the mock will return * <code>conf</conf> when {@link ClusterConnection#getConfiguration()} is invoked. * Be sure to shutdown the connection when done by calling * {@link Connection#close()} else it will stick around; this is probably not what you want. * @param conf configuration * @return ClusterConnection object for <code>conf</code> * @throws ZooKeeperConnectionException */ public static ClusterConnection getMockedConnection(final Configuration conf) throws ZooKeeperConnectionException { ConnectionImplementation connection = Mockito.mock(ConnectionImplementation.class); Mockito.when(connection.getConfiguration()).thenReturn(conf); Mockito.when(connection.getRpcControllerFactory()).thenReturn( Mockito.mock(RpcControllerFactory.class)); // we need a real retrying caller RpcRetryingCallerFactory callerFactory = new RpcRetryingCallerFactory(conf); Mockito.when(connection.getRpcRetryingCallerFactory()).thenReturn(callerFactory); return connection; }
(ConnectionImplementation) ConnectionFactory.createConnection(conf); try { Table table = conn.getTable(TABLE_NAME); byte[] row = Bytes.toBytes("row1"); MetricsConnection metrics = conn.getConnectionMetrics(); long preGetRegionClears = metrics.metaCacheNumClearRegion.getCount(); long preGetServerClears = metrics.metaCacheNumClearServer.getCount(); assertEquals(preGetServerClears, postGetServerClears); } finally { conn.close();
final ExecutorService pool) { this.connection = Preconditions.checkNotNull(connection, "connection is null"); this.configuration = connection.getConfiguration(); this.connConfiguration = connection.getConnectionConfiguration(); if (pool == null) { this.pool = getDefaultExecutor(this.configuration); this.rpcCallerFactory = connection.getNewRpcRetryingCallerFactory(configuration); } else { this.rpcCallerFactory = rpcCallerFactory;
assertNotNull(conn.getCachedLocation(TABLE_NAME, ROW)); HRegionLocation loc = conn.getCachedLocation(TABLE_NAME, ROW).getRegionLocation(); final int nextPort = loc.getPort() + 1; conn.updateCachedLocation(loc.getRegionInfo(), loc.getServerName(), ServerName.valueOf("127.0.0.1", nextPort, HConstants.LATEST_TIMESTAMP), HConstants.LATEST_TIMESTAMP); Assert.assertEquals(conn.getCachedLocation(TABLE_NAME, ROW) .getRegionLocation().getPort(), nextPort); conn.clearRegionCache(TABLE_NAME, ROW.clone()); RegionLocations rl = conn.getCachedLocation(TABLE_NAME, ROW); assertNull("What is this location?? " + rl, rl); conn.clearRegionCache(TABLE_NAME); Assert.assertEquals(0, conn.getNumberOfCachedRegionLocations(TABLE_NAME)); Put put2 = new Put(ROW); put2.addColumn(FAM_NAM, ROW, ROW); table.put(put2); assertNotNull(conn.getCachedLocation(TABLE_NAME, ROW)); assertNotNull(conn.getCachedLocation(TableName.valueOf(TABLE_NAME.getName()), ROW.clone())); HRegionLocation toMove = conn.getCachedLocation(TABLE_NAME, ROW).getRegionLocation(); byte[] regionName = toMove.getRegionInfo().getRegionName(); byte[] encodedRegionNameBytes = toMove.getRegionInfo().getEncodedNameAsBytes(); conn.getCachedLocation(TABLE_NAME, ROW).getRegionLocation() .getPort() == destServerName.getPort());
@Override protected Ratio getRatio() { ThreadPoolExecutor batchPool = (ThreadPoolExecutor) conn.getCurrentBatchPool(); if (batchPool == null) { return Ratio.of(0, 0); } return Ratio.of(batchPool.getActiveCount(), batchPool.getMaximumPoolSize()); } });
@Test public void testMultiMetrics() throws Exception { Configuration conf = new Configuration(TEST_UTIL.getConfiguration()); conf.set(MetricsConnection.CLIENT_SIDE_METRICS_ENABLED_KEY, "true"); ConnectionImplementation conn = (ConnectionImplementation) ConnectionFactory.createConnection(conf); try { BufferedMutator mutator = conn.getBufferedMutator(TABLE_NAME); byte[][] keys = {Bytes.toBytes("aaa"), Bytes.toBytes("mmm"), Bytes.toBytes("zzz")}; for (byte[] key : keys) { Put p = new Put(key); p.addColumn(FAMILY, QUALIFIER, Bytes.toBytes(10)); mutator.mutate(p); } mutator.flush(); mutator.close(); MetricsConnection metrics = conn.getConnectionMetrics(); assertEquals(1, metrics.multiTracker.reqHist.getCount()); assertEquals(3, metrics.numActionsPerServerHist.getSnapshot().getMean(), 1e-15); assertEquals(1, metrics.numActionsPerServerHist.getCount()); } finally { conn.close(); } } }
/** * Close the connection for good. On the off chance that someone is unable to close * the connection, perhaps because it bailed out prematurely, the method * below will ensure that this instance is cleaned up. * Caveat: The JVM may take an unknown amount of time to call finalize on an * unreachable object, so our hope is that every consumer cleans up after * itself, like any good citizen. */ @Override protected void finalize() throws Throwable { super.finalize(); close(); }
@Test public void testMetaLookupThreadPoolCreated() throws Exception { final TableName tableName = TableName.valueOf(name.getMethodName()); byte[][] FAMILIES = new byte[][] { Bytes.toBytes("foo") }; if (TEST_UTIL.getAdmin().tableExists(tableName)) { TEST_UTIL.getAdmin().disableTable(tableName); TEST_UTIL.getAdmin().deleteTable(tableName); } try (Table htable = TEST_UTIL.createTable(tableName, FAMILIES)) { byte[] row = "test".getBytes(); ConnectionImplementation c = ((ConnectionImplementation) TEST_UTIL.getConnection()); // check that metalookup pool would get created c.relocateRegion(tableName, row); ExecutorService ex = c.getCurrentMetaLookupPool(); assert(ex != null); } }
@Test(expected = UnknownHostException.class) public void testGetClientBadHostname() throws Exception { // verify that we can get an instance with the cluster hostname ServerName rs = testUtil.getHBaseCluster().getRegionServer(0).getServerName(); try { conn.getClient(rs); } catch (UnknownHostException uhe) { fail("Obtaining client to the cluster regionserver should have succeeded"); } // test that we fail to get a client to an unresolvable hostname, which // means it won't be cached ServerName badHost = ServerName.valueOf("unknownhost.invalid:" + HConstants.DEFAULT_REGIONSERVER_PORT, System.currentTimeMillis()); conn.getAdmin(badHost); fail("Obtaining client to unresolvable hostname should have failed"); } }
conn.clearRegionCache(TABLE_NAME3); Assert.assertEquals(0, conn.getNumberOfCachedRegionLocations(TABLE_NAME3)); HRegionLocation toMove = conn.getCachedLocation(TABLE_NAME3, ROW_X).getRegionLocation(); byte[] regionName = toMove.getRegionInfo().getRegionName(); byte[] encodedRegionNameBytes = toMove.getRegionInfo().getEncodedNameAsBytes(); conn.getCachedLocation(TABLE_NAME3, ROW_X).getRegionLocation() .getPort() == destServerName.getPort());