Java Code Examples for org.apache.hadoop.hbase.ServerName#getPort()

The following examples show how to use org.apache.hadoop.hbase.ServerName#getPort() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AbstractRpcClient.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Interrupt the connections to the given ip:port server. This should be called if the server is
 * known as actually dead. This will not prevent current operation to be retried, and, depending
 * on their own behavior, they may retry on the same server. This can be a feature, for example at
 * startup. In any case, they're likely to get connection refused (if the process died) or no
 * route to host: i.e. their next retries should be faster and with a safe exception.
 */
@Override
public void cancelConnections(ServerName sn) {
  synchronized (connections) {
    for (T connection : connections.values()) {
      ConnectionId remoteId = connection.remoteId();
      if (remoteId.address.getPort() == sn.getPort()
          && remoteId.address.getHostName().equals(sn.getHostname())) {
        LOG.info("The server on " + sn.toString() + " is dead - stopping the connection "
            + connection.remoteId);
        connections.removeValue(remoteId, connection);
        connection.shutdown();
        connection.cleanupConnection();
      }
    }
  }
}
 
Example 2
Source File: ConnectionUtils.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Get a unique key for the rpc stub to the given server.
 */
static String getStubKey(String serviceName, ServerName serverName, boolean hostnameCanChange) {
  // Sometimes, servers go down and they come back up with the same hostname but a different
  // IP address. Force a resolution of the rsHostname by trying to instantiate an
  // InetSocketAddress, and this way we will rightfully get a new stubKey.
  // Also, include the hostname in the key so as to take care of those cases where the
  // DNS name is different but IP address remains the same.
  String hostname = serverName.getHostname();
  int port = serverName.getPort();
  if (hostnameCanChange) {
    try {
      InetAddress ip = InetAddress.getByName(hostname);
      return serviceName + "@" + hostname + "-" + ip.getHostAddress() + ":" + port;
    } catch (UnknownHostException e) {
      LOG.warn("Can not resolve " + hostname + ", please check your network", e);
    }
  }
  return serviceName + "@" + hostname + ":" + port;
}
 
Example 3
Source File: ClusterStatusListener.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Check if we know if a server is dead.
 *
 * @param sn the server name to check.
 * @return true if we know for sure that the server is dead, false otherwise.
 */
public boolean isDeadServer(ServerName sn) {
  if (sn.getStartcode() <= 0) {
    return false;
  }

  for (ServerName dead : deadServers) {
    if (dead.getStartcode() >= sn.getStartcode() &&
        dead.getPort() == sn.getPort() &&
        dead.getHostname().equals(sn.getHostname())) {
      return true;
    }
  }

  return false;
}
 
Example 4
Source File: ProtobufUtil.java    From hbase with Apache License 2.0 6 votes vote down vote up
/**
 * Convert a ServerName to a protocol buffer ServerName
 *
 * @param serverName the ServerName to convert
 * @return the converted protocol buffer ServerName
 * @see #toServerName(org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.ServerName)
 */
public static HBaseProtos.ServerName toServerName(final ServerName serverName) {
  if (serverName == null) {
    return null;
  }
  HBaseProtos.ServerName.Builder builder =
    HBaseProtos.ServerName.newBuilder();
  builder.setHostName(serverName.getHostname());
  if (serverName.getPort() >= 0) {
    builder.setPort(serverName.getPort());
  }
  if (serverName.getStartcode() >= 0) {
    builder.setStartCode(serverName.getStartcode());
  }
  return builder.build();
}
 
Example 5
Source File: MCTest.java    From hbase-tools with Apache License 2.0 5 votes vote down vote up
@Test
public void testMC_RS() throws Exception {
    // move each RS has one region
    splitTable("c".getBytes());
    ArrayList<ServerName> serverNameList = getServerNameList();
    assertTrue(serverNameList.size() >= 2);
    ArrayList<HRegionInfo> regionInfoList = getRegionInfoList(tableName);
    assertEquals(2, regionInfoList.size());
    HRegionInfo regionInfo1 = regionInfoList.get(0);
    HRegionInfo regionInfo2 = regionInfoList.get(1);
    ServerName serverName1 = serverNameList.get(0);
    ServerName serverName2 = serverNameList.get(1);
    move(regionInfo1, serverName1);
    move(regionInfo2, serverName2);

    // make 2 + 2 store files
    putData(table, "a".getBytes());
    admin.flush(tableName);
    putData(table, "b".getBytes());
    admin.flush(tableName);
    putData(table, "c".getBytes());
    admin.flush(tableName);
    putData(table, "d".getBytes());
    admin.flush(tableName);
    Thread.sleep(3000);
    assertEquals(2, getRegionLoad(regionInfo1, serverName1).getStorefiles());
    assertEquals(2, getRegionLoad(regionInfo2, serverName2).getStorefiles());

    // run MC
    String regex = serverName1.getHostname() + "," + serverName1.getPort() + ".*";
    String[] argsParam = {"zookeeper", tableName, "--rs=" + regex, "--force-proceed", "--wait", "--test"};
    Args args = new ManagerArgs(argsParam);
    MC command = new MC(admin, args);
    command.run();
    assertRegionName(command);

    // should be 1 store file
    assertEquals(1, getRegionLoad(regionInfo1, serverName1).getStorefiles());
    assertEquals(2, getRegionLoad(regionInfo2, serverName2).getStorefiles());
}
 
Example 6
Source File: TestTableResource.java    From hbase with Apache License 2.0 5 votes vote down vote up
void checkTableInfo(TableInfoModel model) {
  assertEquals(model.getName(), TABLE.getNameAsString());
  Iterator<TableRegionModel> regions = model.getRegions().iterator();
  assertTrue(regions.hasNext());
  while (regions.hasNext()) {
    TableRegionModel region = regions.next();
    boolean found = false;
    LOG.debug("looking for region " + region.getName());
    for (HRegionLocation e: regionMap) {
      RegionInfo hri = e.getRegion();
      // getRegionNameAsString uses Bytes.toStringBinary which escapes some non-printable
      // characters
      String hriRegionName = Bytes.toString(hri.getRegionName());
      String regionName = region.getName();
      LOG.debug("comparing to region " + hriRegionName);
      if (hriRegionName.equals(regionName)) {
        found = true;
        byte[] startKey = hri.getStartKey();
        byte[] endKey = hri.getEndKey();
        ServerName serverName = e.getServerName();
        InetSocketAddress sa =
            new InetSocketAddress(serverName.getHostname(), serverName.getPort());
        String location = sa.getHostName() + ":" +
          Integer.valueOf(sa.getPort());
        assertEquals(hri.getRegionId(), region.getId());
        assertTrue(Bytes.equals(startKey, region.getStartKey()));
        assertTrue(Bytes.equals(endKey, region.getEndKey()));
        assertEquals(location, region.getLocation());
        break;
      }
    }
    assertTrue("Couldn't find region " + region.getName(), found);
  }
}
 
Example 7
Source File: AbstractRpcClient.java    From hbase with Apache License 2.0 5 votes vote down vote up
InetSocketAddress createAddr(ServerName sn) throws UnknownHostException {
  InetSocketAddress addr = new InetSocketAddress(sn.getHostname(), sn.getPort());
  if (addr.isUnresolved()) {
    throw new UnknownHostException("can not resolve " + sn.getServerName());
  }
  return addr;
}
 
Example 8
Source File: ThriftHBaseServiceHandler.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
public TRegionInfo getRegionInfo(ByteBuffer searchRow) throws IOError {
  try {
    byte[] row = getBytes(searchRow);
    Result startRowResult = getReverseScanResult(TableName.META_TABLE_NAME.getName(), row,
        HConstants.CATALOG_FAMILY);

    if (startRowResult == null) {
      throw new IOException("Cannot find row in "+ TableName.META_TABLE_NAME+", row="
          + Bytes.toStringBinary(row));
    }

    // find region start and end keys
    RegionInfo regionInfo = CatalogFamilyFormat.getRegionInfo(startRowResult);
    if (regionInfo == null) {
      throw new IOException("RegionInfo REGIONINFO was null or " +
          " empty in Meta for row="
          + Bytes.toStringBinary(row));
    }
    TRegionInfo region = new TRegionInfo();
    region.setStartKey(regionInfo.getStartKey());
    region.setEndKey(regionInfo.getEndKey());
    region.id = regionInfo.getRegionId();
    region.setName(regionInfo.getRegionName());
    region.version = HREGION_VERSION; // version not used anymore, PB encoding used.

    // find region assignment to server
    ServerName serverName = CatalogFamilyFormat.getServerName(startRowResult, 0);
    if (serverName != null) {
      region.setServerName(Bytes.toBytes(serverName.getHostname()));
      region.port = serverName.getPort();
    }
    return region;
  } catch (IOException e) {
    LOG.warn(e.getMessage(), e);
    throw getIOError(e);
  }
}
 
Example 9
Source File: StartcodeAgnosticServerName.java    From hbase with Apache License 2.0 5 votes vote down vote up
@Override
public int compareTo(ServerName other) {
  int compare = this.getHostname().compareTo(other.getHostname());
  if (compare != 0) return compare;
  compare = this.getPort() - other.getPort();
  if (compare != 0) return compare;
  return 0;
}
 
Example 10
Source File: MCTest.java    From hbase-tools with Apache License 2.0 5 votes vote down vote up
@Test
public void testMC_RS() throws Exception {
    // move each RS has one region
    splitTable("c".getBytes());
    ArrayList<ServerName> serverNameList = getServerNameList();
    assertTrue(serverNameList.size() >= 2);
    ArrayList<HRegionInfo> regionInfoList = getRegionInfoList(tableName);
    assertEquals(2, regionInfoList.size());
    HRegionInfo regionInfo1 = regionInfoList.get(0);
    HRegionInfo regionInfo2 = regionInfoList.get(1);
    ServerName serverName1 = serverNameList.get(0);
    ServerName serverName2 = serverNameList.get(1);
    move(regionInfo1, serverName1);
    move(regionInfo2, serverName2);

    // make 2 + 2 store files
    putData(table, "a".getBytes());
    admin.flush(tableName);
    putData(table, "b".getBytes());
    admin.flush(tableName);
    putData(table, "c".getBytes());
    admin.flush(tableName);
    putData(table, "d".getBytes());
    admin.flush(tableName);
    Thread.sleep(3000);
    assertEquals(2, getRegionLoad(regionInfo1, serverName1).getStorefiles());
    assertEquals(2, getRegionLoad(regionInfo2, serverName2).getStorefiles());

    // run MC
    String regex = serverName1.getHostname() + "," + serverName1.getPort() + ".*";
    String[] argsParam = {"zookeeper", tableName, "--rs=" + regex, "--force-proceed", "--wait", "--test"};
    Args args = new ManagerArgs(argsParam);
    MC command = new MC(admin, args);
    command.run();
    assertRegionName(command);

    // should be 1 store file
    assertEquals(1, getRegionLoad(regionInfo1, serverName1).getStorefiles());
    assertEquals(2, getRegionLoad(regionInfo2, serverName2).getStorefiles());
}
 
Example 11
Source File: MCTest.java    From hbase-tools with Apache License 2.0 5 votes vote down vote up
@Test
public void testMC_RS() throws Exception {
    // move each RS has one region
    splitTable("c".getBytes());
    ArrayList<ServerName> serverNameList = getServerNameList();
    assertTrue(serverNameList.size() >= 2);
    ArrayList<HRegionInfo> regionInfoList = getRegionInfoList(tableName);
    assertEquals(2, regionInfoList.size());
    HRegionInfo regionInfo1 = regionInfoList.get(0);
    HRegionInfo regionInfo2 = regionInfoList.get(1);
    ServerName serverName1 = serverNameList.get(0);
    ServerName serverName2 = serverNameList.get(1);
    move(regionInfo1, serverName1);
    move(regionInfo2, serverName2);

    // make 2 + 2 store files
    putData(table, "a".getBytes());
    admin.flush(tableName);
    putData(table, "b".getBytes());
    admin.flush(tableName);
    putData(table, "c".getBytes());
    admin.flush(tableName);
    putData(table, "d".getBytes());
    admin.flush(tableName);
    Thread.sleep(3000);
    assertEquals(2, getRegionLoad(regionInfo1, serverName1).getStorefiles());
    assertEquals(2, getRegionLoad(regionInfo2, serverName2).getStorefiles());

    // run MC
    String regex = serverName1.getHostname() + "," + serverName1.getPort() + ".*";
    String[] argsParam = {"zookeeper", tableName, "--rs=" + regex, "--force-proceed", "--wait", "--test"};
    Args args = new ManagerArgs(argsParam);
    MC command = new MC(admin, args);
    command.run();
    assertRegionName(command);

    // should be 1 store file
    assertEquals(1, getRegionLoad(regionInfo1, serverName1).getStorefiles());
    assertEquals(2, getRegionLoad(regionInfo2, serverName2).getStorefiles());
}
 
Example 12
Source File: MCTest.java    From hbase-tools with Apache License 2.0 5 votes vote down vote up
@Test
public void testMC_RS() throws Exception {
    // move each RS has one region
    splitTable("c".getBytes());
    ArrayList<ServerName> serverNameList = getServerNameList();
    assertTrue(serverNameList.size() >= 2);
    ArrayList<HRegionInfo> regionInfoList = getRegionInfoList(tableName);
    assertEquals(2, regionInfoList.size());
    HRegionInfo regionInfo1 = regionInfoList.get(0);
    HRegionInfo regionInfo2 = regionInfoList.get(1);
    ServerName serverName1 = serverNameList.get(0);
    ServerName serverName2 = serverNameList.get(1);
    move(regionInfo1, serverName1);
    move(regionInfo2, serverName2);

    // make 2 + 2 store files
    putData(table, "a".getBytes());
    admin.flush(tableName);
    putData(table, "b".getBytes());
    admin.flush(tableName);
    putData(table, "c".getBytes());
    admin.flush(tableName);
    putData(table, "d".getBytes());
    admin.flush(tableName);
    Thread.sleep(3000);
    assertEquals(2, getRegionLoad(regionInfo1, serverName1).getStorefiles());
    assertEquals(2, getRegionLoad(regionInfo2, serverName2).getStorefiles());

    // run MC
    String regex = serverName1.getHostname() + "," + serverName1.getPort() + ".*";
    String[] argsParam = {"zookeeper", tableName, "--rs=" + regex, "--force-proceed", "--wait", "--test"};
    Args args = new ManagerArgs(argsParam);
    MC command = new MC(admin, args);
    command.run();
    assertRegionName(command);

    // should be 1 store file
    assertEquals(1, getRegionLoad(regionInfo1, serverName1).getStorefiles());
    assertEquals(2, getRegionLoad(regionInfo2, serverName2).getStorefiles());
}
 
Example 13
Source File: TestFromClientSide5.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * Tests the non cached version of getRegionLocator by moving a region.
 */
@Test
public void testNonCachedGetRegionLocation() throws Exception {
  // Test Initialization.
  final TableName tableName = name.getTableName();
  byte [] family1 = Bytes.toBytes("f1");
  byte [] family2 = Bytes.toBytes("f2");
  try (Table ignored = TEST_UTIL.createTable(tableName, new byte[][] {family1, family2}, 10);
      Admin admin = TEST_UTIL.getAdmin();
      RegionLocator locator = TEST_UTIL.getConnection().getRegionLocator(tableName)) {
    List<HRegionLocation> allRegionLocations = locator.getAllRegionLocations();
    assertEquals(1, allRegionLocations.size());
    RegionInfo regionInfo = allRegionLocations.get(0).getRegion();
    ServerName addrBefore = allRegionLocations.get(0).getServerName();
    // Verify region location before move.
    HRegionLocation addrCache = locator.getRegionLocation(regionInfo.getStartKey(), false);
    HRegionLocation addrNoCache = locator.getRegionLocation(regionInfo.getStartKey(),  true);

    assertEquals(addrBefore.getPort(), addrCache.getPort());
    assertEquals(addrBefore.getPort(), addrNoCache.getPort());


    // Make sure more than one server.
    if (TEST_UTIL.getMiniHBaseCluster().getLiveRegionServerThreads().size() <= 1) {
      TEST_UTIL.getMiniHBaseCluster().startRegionServer();
      Waiter.waitFor(TEST_UTIL.getConfiguration(), 30000, new Waiter.Predicate<Exception>() {
        @Override public boolean evaluate() throws Exception {
          return TEST_UTIL.getMiniHBaseCluster().getLiveRegionServerThreads().size() > 1;
        }
      });
    }

    ServerName addrAfter = null;
    // Now move the region to a different server.
    for (int i = 0; i < TEST_UTIL.getMiniHBaseCluster().getLiveRegionServerThreads().size();
         i++) {
      HRegionServer regionServer = TEST_UTIL.getHBaseCluster().getRegionServer(i);
      ServerName addr = regionServer.getServerName();
      if (addr.getPort() != addrBefore.getPort()) {
        admin.move(regionInfo.getEncodedNameAsBytes(), addr);
        // Wait for the region to move.
        Thread.sleep(5000);
        addrAfter = addr;
        break;
      }
    }

    // Verify the region was moved.
    addrCache = locator.getRegionLocation(regionInfo.getStartKey(), false);
    addrNoCache = locator.getRegionLocation(regionInfo.getStartKey(), true);
    assertNotNull(addrAfter);
    assertTrue(addrAfter.getPort() != addrCache.getPort());
    assertEquals(addrAfter.getPort(), addrNoCache.getPort());
  }
}
 
Example 14
Source File: TestClientTimeouts.java    From hbase with Apache License 2.0 4 votes vote down vote up
RandomTimeoutBlockingRpcChannel(BlockingRpcClient rpcClient, ServerName sn, User ticket,
    int rpcTimeout) {
  super(rpcClient, new InetSocketAddress(sn.getHostname(), sn.getPort()), ticket, rpcTimeout);
}
 
Example 15
Source File: TestClientTimeouts.java    From hbase with Apache License 2.0 4 votes vote down vote up
RandomTimeoutRpcChannel(AbstractRpcClient<?> rpcClient, ServerName sn, User ticket,
    int rpcTimeout) throws UnknownHostException {
  super(rpcClient, new InetSocketAddress(sn.getHostname(), sn.getPort()), ticket, rpcTimeout);
}
 
Example 16
Source File: RegionMovedException.java    From hbase with Apache License 2.0 4 votes vote down vote up
public RegionMovedException(ServerName serverName, long locationSeqNum) {
  this.hostname = serverName.getHostname();
  this.port = serverName.getPort();
  this.startCode = serverName.getStartcode();
  this.locationSeqNum = locationSeqNum;
}
 
Example 17
Source File: StartcodeAgnosticServerName.java    From hbase with Apache License 2.0 4 votes vote down vote up
public static StartcodeAgnosticServerName valueOf(final ServerName serverName) {
  return new StartcodeAgnosticServerName(serverName.getHostname(), serverName.getPort(),
      serverName.getStartcode());
}
 
Example 18
Source File: MCTest.java    From hbase-tools with Apache License 2.0 4 votes vote down vote up
@Test
public void testMC_LocalityMultipleRSs() throws Exception {
    // fixme Data locality is not correct in HBaseTestingUtility
    if (miniCluster) return;

    // move each RS has one region
    splitTable("c".getBytes());
    ArrayList<ServerName> serverNameList = getServerNameList();
    assertTrue(serverNameList.size() >= 2);
    ArrayList<HRegionInfo> regionInfoList = getRegionInfoList(tableName);
    assertEquals(2, regionInfoList.size());
    HRegionInfo regionInfo1 = regionInfoList.get(0);
    HRegionInfo regionInfo2 = regionInfoList.get(1);
    ServerName serverName1 = serverNameList.get(0);
    ServerName serverName2 = serverNameList.get(1);
    move(regionInfo1, serverName1);
    move(regionInfo2, serverName2);

    // make 2 + 2 store files
    putData(table, "a".getBytes());
    admin.flush(tableName);
    putData(table, "b".getBytes());
    admin.flush(tableName);
    putData(table, "c".getBytes());
    admin.flush(tableName);
    putData(table, "d".getBytes());
    admin.flush(tableName);
    Thread.sleep(3000);
    assertEquals(2, getRegionLoad(regionInfo1, serverName1).getStorefiles());
    assertEquals(2, getRegionLoad(regionInfo2, serverName2).getStorefiles());

    // Data locality of regionInfo1 is 0%, data locality of regionInfo2 is 0%
    move(regionInfo1, serverName2);
    move(regionInfo2, serverName1);
    assertEquals(2, getRegionLoad(regionInfo1, serverName2).getStorefiles());
    assertEquals(2, getRegionLoad(regionInfo2, serverName1).getStorefiles());

    // run MC
    String regex;
    if (miniCluster) regex = serverName1.getHostname() + "," + serverName1.getPort() + ".*";
    else regex = serverName1.getHostname() + ".*";
    String[] argsParam = {"zookeeper", tableName, "--locality=100", "--rs=" + regex,
        "--force-proceed", "--wait", "--test"};
    Args args = new ManagerArgs(argsParam);
    MC command = new MC(admin, args);
    command.run();

    // should be 1 store file
    assertEquals(2, getRegionLoad(regionInfo1, serverName2).getStorefiles());
    assertEquals(1, getRegionLoad(regionInfo2, serverName1).getStorefiles());
}
 
Example 19
Source File: MCTest.java    From hbase-tools with Apache License 2.0 4 votes vote down vote up
@Test
public void testMC_LocalityMultipleRSs() throws Exception {
    // fixme Data locality is not correct in HBaseTestingUtility
    if (miniCluster) return;

    // move each RS has one region
    splitTable("c".getBytes());
    ArrayList<ServerName> serverNameList = getServerNameList();
    assertTrue(serverNameList.size() >= 2);
    ArrayList<HRegionInfo> regionInfoList = getRegionInfoList(tableName);
    assertEquals(2, regionInfoList.size());
    HRegionInfo regionInfo1 = regionInfoList.get(0);
    HRegionInfo regionInfo2 = regionInfoList.get(1);
    ServerName serverName1 = serverNameList.get(0);
    ServerName serverName2 = serverNameList.get(1);
    move(regionInfo1, serverName1);
    move(regionInfo2, serverName2);

    // make 2 + 2 store files
    putData(table, "a".getBytes());
    admin.flush(tableName);
    putData(table, "b".getBytes());
    admin.flush(tableName);
    putData(table, "c".getBytes());
    admin.flush(tableName);
    putData(table, "d".getBytes());
    admin.flush(tableName);
    Thread.sleep(3000);
    assertEquals(2, getRegionLoad(regionInfo1, serverName1).getStorefiles());
    assertEquals(2, getRegionLoad(regionInfo2, serverName2).getStorefiles());

    // Data locality of regionInfo1 is 0%, data locality of regionInfo2 is 0%
    move(regionInfo1, serverName2);
    move(regionInfo2, serverName1);
    assertEquals(2, getRegionLoad(regionInfo1, serverName2).getStorefiles());
    assertEquals(2, getRegionLoad(regionInfo2, serverName1).getStorefiles());

    // run MC
    String regex;
    if (miniCluster) regex = serverName1.getHostname() + "," + serverName1.getPort() + ".*";
    else regex = serverName1.getHostname() + ".*";
    String[] argsParam = {"zookeeper", tableName, "--locality=100", "--rs=" + regex,
        "--force-proceed", "--wait", "--test"};
    Args args = new ManagerArgs(argsParam);
    MC command = new MC(admin, args);
    command.run();

    // should be 1 store file
    assertEquals(2, getRegionLoad(regionInfo1, serverName2).getStorefiles());
    assertEquals(1, getRegionLoad(regionInfo2, serverName1).getStorefiles());
}