Search in sources :

Example 11 with MetaTableLocator

use of org.apache.hadoop.hbase.zookeeper.MetaTableLocator in project hbase by apache.

the class RegionMover method getServerNameForRegion.

/**
   * Get servername that is up in hbase:meta hosting the given region. this is hostname + port +
   * startcode comma-delimited. Can return null
   * @param admin
   * @param region
   * @return regionServer hosting the given region
   * @throws IOException
   */
private String getServerNameForRegion(Admin admin, HRegionInfo region) throws IOException {
    String server = null;
    if (!admin.isTableEnabled(region.getTable())) {
        return null;
    }
    if (region.isMetaRegion()) {
        ZooKeeperWatcher zkw = new ZooKeeperWatcher(admin.getConfiguration(), "region_mover", null);
        MetaTableLocator locator = new MetaTableLocator();
        int maxWaitInSeconds = admin.getConfiguration().getInt(MOVE_WAIT_MAX_KEY, DEFAULT_MOVE_WAIT_MAX);
        try {
            server = locator.waitMetaRegionLocation(zkw, maxWaitInSeconds * 1000).toString() + ",";
        } catch (InterruptedException e) {
            LOG.error("Interrupted while waiting for location of Meta", e);
        } finally {
            if (zkw != null) {
                zkw.close();
            }
        }
    } else {
        Table table = admin.getConnection().getTable(TableName.META_TABLE_NAME);
        try {
            Get get = new Get(region.getRegionName());
            get.addColumn(HConstants.CATALOG_FAMILY, HConstants.SERVER_QUALIFIER);
            get.addColumn(HConstants.CATALOG_FAMILY, HConstants.STARTCODE_QUALIFIER);
            Result result = table.get(get);
            if (result != null) {
                byte[] servername = result.getValue(HConstants.CATALOG_FAMILY, HConstants.SERVER_QUALIFIER);
                byte[] startcode = result.getValue(HConstants.CATALOG_FAMILY, HConstants.STARTCODE_QUALIFIER);
                if (servername != null) {
                    server = Bytes.toString(servername).replaceFirst(":", ",") + "," + Bytes.toLong(startcode);
                }
            }
        } catch (IOException e) {
            LOG.error("Could not get Server Name for region:" + region.getEncodedName(), e);
            throw e;
        } finally {
            table.close();
        }
    }
    return server;
}
Also used : MetaTableLocator(org.apache.hadoop.hbase.zookeeper.MetaTableLocator) Table(org.apache.hadoop.hbase.client.Table) ZooKeeperWatcher(org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher) Get(org.apache.hadoop.hbase.client.Get) IOException(java.io.IOException) Result(org.apache.hadoop.hbase.client.Result)

Example 12 with MetaTableLocator

use of org.apache.hadoop.hbase.zookeeper.MetaTableLocator in project hbase by apache.

the class HBaseAdmin method getCompactionState.

/**
   * {@inheritDoc}
   */
@Override
public CompactionState getCompactionState(final TableName tableName, CompactType compactType) throws IOException {
    AdminProtos.GetRegionInfoResponse.CompactionState state = AdminProtos.GetRegionInfoResponse.CompactionState.NONE;
    checkTableExists(tableName);
    // TODO: There is no timeout on this controller. Set one!
    final HBaseRpcController rpcController = rpcControllerFactory.newController();
    switch(compactType) {
        case MOB:
            final AdminProtos.AdminService.BlockingInterface masterAdmin = this.connection.getAdmin(getMasterAddress());
            Callable<AdminProtos.GetRegionInfoResponse.CompactionState> callable = new Callable<AdminProtos.GetRegionInfoResponse.CompactionState>() {

                @Override
                public AdminProtos.GetRegionInfoResponse.CompactionState call() throws Exception {
                    HRegionInfo info = getMobRegionInfo(tableName);
                    GetRegionInfoRequest request = RequestConverter.buildGetRegionInfoRequest(info.getRegionName(), true);
                    GetRegionInfoResponse response = masterAdmin.getRegionInfo(rpcController, request);
                    return response.getCompactionState();
                }
            };
            state = ProtobufUtil.call(callable);
            break;
        case NORMAL:
        default:
            ZooKeeperWatcher zookeeper = null;
            try {
                List<Pair<HRegionInfo, ServerName>> pairs;
                if (TableName.META_TABLE_NAME.equals(tableName)) {
                    zookeeper = new ZooKeeperWatcher(conf, ZK_IDENTIFIER_PREFIX + connection.toString(), new ThrowableAbortable());
                    pairs = new MetaTableLocator().getMetaRegionsAndLocations(zookeeper);
                } else {
                    pairs = MetaTableAccessor.getTableRegionsAndLocations(connection, tableName);
                }
                for (Pair<HRegionInfo, ServerName> pair : pairs) {
                    if (pair.getFirst().isOffline())
                        continue;
                    if (pair.getSecond() == null)
                        continue;
                    final ServerName sn = pair.getSecond();
                    final byte[] regionName = pair.getFirst().getRegionName();
                    final AdminService.BlockingInterface snAdmin = this.connection.getAdmin(sn);
                    try {
                        Callable<GetRegionInfoResponse> regionInfoCallable = new Callable<GetRegionInfoResponse>() {

                            @Override
                            public GetRegionInfoResponse call() throws Exception {
                                GetRegionInfoRequest request = RequestConverter.buildGetRegionInfoRequest(regionName, true);
                                return snAdmin.getRegionInfo(rpcController, request);
                            }
                        };
                        GetRegionInfoResponse response = ProtobufUtil.call(regionInfoCallable);
                        switch(response.getCompactionState()) {
                            case MAJOR_AND_MINOR:
                                return CompactionState.MAJOR_AND_MINOR;
                            case MAJOR:
                                if (state == AdminProtos.GetRegionInfoResponse.CompactionState.MINOR) {
                                    return CompactionState.MAJOR_AND_MINOR;
                                }
                                state = AdminProtos.GetRegionInfoResponse.CompactionState.MAJOR;
                                break;
                            case MINOR:
                                if (state == AdminProtos.GetRegionInfoResponse.CompactionState.MAJOR) {
                                    return CompactionState.MAJOR_AND_MINOR;
                                }
                                state = AdminProtos.GetRegionInfoResponse.CompactionState.MINOR;
                                break;
                            case NONE:
                            // nothing, continue
                            default:
                        }
                    } catch (NotServingRegionException e) {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("Trying to get compaction state of " + pair.getFirst() + ": " + StringUtils.stringifyException(e));
                        }
                    } catch (RemoteException e) {
                        if (e.getMessage().indexOf(NotServingRegionException.class.getName()) >= 0) {
                            if (LOG.isDebugEnabled()) {
                                LOG.debug("Trying to get compaction state of " + pair.getFirst() + ": " + StringUtils.stringifyException(e));
                            }
                        } else {
                            throw e;
                        }
                    }
                }
            } finally {
                if (zookeeper != null) {
                    zookeeper.close();
                }
            }
            break;
    }
    if (state != null) {
        return ProtobufUtil.createCompactionState(state);
    }
    return null;
}
Also used : AdminService(org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.AdminService) NotServingRegionException(org.apache.hadoop.hbase.NotServingRegionException) Callable(java.util.concurrent.Callable) HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) HBaseRpcController(org.apache.hadoop.hbase.ipc.HBaseRpcController) MetaTableLocator(org.apache.hadoop.hbase.zookeeper.MetaTableLocator) GetRegionInfoRequest(org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.GetRegionInfoRequest) GetRegionInfoResponse(org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.GetRegionInfoResponse) ZooKeeperWatcher(org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher) AdminProtos(org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos) ServerName(org.apache.hadoop.hbase.ServerName) RemoteException(org.apache.hadoop.ipc.RemoteException) Pair(org.apache.hadoop.hbase.util.Pair) NameStringPair(org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.NameStringPair)

Example 13 with MetaTableLocator

use of org.apache.hadoop.hbase.zookeeper.MetaTableLocator in project hbase by apache.

the class HBaseAdmin method getTableRegions.

@Override
public List<HRegionInfo> getTableRegions(final TableName tableName) throws IOException {
    ZooKeeperWatcher zookeeper = new ZooKeeperWatcher(conf, ZK_IDENTIFIER_PREFIX + connection.toString(), new ThrowableAbortable());
    List<HRegionInfo> regions = null;
    try {
        if (TableName.META_TABLE_NAME.equals(tableName)) {
            regions = new MetaTableLocator().getMetaRegions(zookeeper);
        } else {
            regions = MetaTableAccessor.getTableRegions(connection, tableName, true);
        }
    } finally {
        zookeeper.close();
    }
    return regions;
}
Also used : HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) MetaTableLocator(org.apache.hadoop.hbase.zookeeper.MetaTableLocator) ZooKeeperWatcher(org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher)

Example 14 with MetaTableLocator

use of org.apache.hadoop.hbase.zookeeper.MetaTableLocator in project hbase by apache.

the class HBaseAdmin method compact.

/**
   * Compact a table.
   * Asynchronous operation.
   *
   * @param tableName table or region to compact
   * @param columnFamily column family within a table or region
   * @param major True if we are to do a major compaction.
   * @param compactType {@link org.apache.hadoop.hbase.client.CompactType}
   * @throws IOException if a remote or network exception occurs
   */
private void compact(final TableName tableName, final byte[] columnFamily, final boolean major, CompactType compactType) throws IOException {
    switch(compactType) {
        case MOB:
            ServerName master = getMasterAddress();
            compact(master, getMobRegionInfo(tableName), major, columnFamily);
            break;
        case NORMAL:
        default:
            ZooKeeperWatcher zookeeper = null;
            try {
                checkTableExists(tableName);
                zookeeper = new ZooKeeperWatcher(conf, ZK_IDENTIFIER_PREFIX + connection.toString(), new ThrowableAbortable());
                List<Pair<HRegionInfo, ServerName>> pairs;
                if (TableName.META_TABLE_NAME.equals(tableName)) {
                    pairs = new MetaTableLocator().getMetaRegionsAndLocations(zookeeper);
                } else {
                    pairs = MetaTableAccessor.getTableRegionsAndLocations(connection, tableName);
                }
                for (Pair<HRegionInfo, ServerName> pair : pairs) {
                    if (pair.getFirst().isOffline())
                        continue;
                    if (pair.getSecond() == null)
                        continue;
                    try {
                        compact(pair.getSecond(), pair.getFirst(), major, columnFamily);
                    } catch (NotServingRegionException e) {
                        if (LOG.isDebugEnabled()) {
                            LOG.debug("Trying to" + (major ? " major" : "") + " compact " + pair.getFirst() + ": " + StringUtils.stringifyException(e));
                        }
                    }
                }
            } finally {
                if (zookeeper != null) {
                    zookeeper.close();
                }
            }
            break;
    }
}
Also used : HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) MetaTableLocator(org.apache.hadoop.hbase.zookeeper.MetaTableLocator) ZooKeeperWatcher(org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher) NotServingRegionException(org.apache.hadoop.hbase.NotServingRegionException) ServerName(org.apache.hadoop.hbase.ServerName) Pair(org.apache.hadoop.hbase.util.Pair) NameStringPair(org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.NameStringPair)

Example 15 with MetaTableLocator

use of org.apache.hadoop.hbase.zookeeper.MetaTableLocator in project hbase by apache.

the class TestHBaseFsckTwoRS method testFixAssignmentsWhenMETAinTransition.

@Test(timeout = 180000)
public void testFixAssignmentsWhenMETAinTransition() throws Exception {
    MiniHBaseCluster cluster = TEST_UTIL.getHBaseCluster();
    admin.closeRegion(cluster.getServerHoldingMeta(), HRegionInfo.FIRST_META_REGIONINFO);
    regionStates.regionOffline(HRegionInfo.FIRST_META_REGIONINFO);
    new MetaTableLocator().deleteMetaLocation(cluster.getMaster().getZooKeeper());
    assertFalse(regionStates.isRegionOnline(HRegionInfo.FIRST_META_REGIONINFO));
    HBaseFsck hbck = doFsck(conf, true);
    assertErrors(hbck, new HBaseFsck.ErrorReporter.ERROR_CODE[] { HBaseFsck.ErrorReporter.ERROR_CODE.UNKNOWN, HBaseFsck.ErrorReporter.ERROR_CODE.NO_META_REGION, HBaseFsck.ErrorReporter.ERROR_CODE.NULL_META_REGION });
    assertNoErrors(doFsck(conf, false));
}
Also used : MetaTableLocator(org.apache.hadoop.hbase.zookeeper.MetaTableLocator) Test(org.junit.Test)

Aggregations

MetaTableLocator (org.apache.hadoop.hbase.zookeeper.MetaTableLocator)21 HRegionInfo (org.apache.hadoop.hbase.HRegionInfo)10 ServerName (org.apache.hadoop.hbase.ServerName)8 Pair (org.apache.hadoop.hbase.util.Pair)7 ZooKeeperWatcher (org.apache.hadoop.hbase.zookeeper.ZooKeeperWatcher)7 Test (org.junit.Test)6 IOException (java.io.IOException)3 ClusterConnection (org.apache.hadoop.hbase.client.ClusterConnection)3 NameStringPair (org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.NameStringPair)3 HashSet (java.util.HashSet)2 NotServingRegionException (org.apache.hadoop.hbase.NotServingRegionException)2 ForeignException (org.apache.hadoop.hbase.errorhandling.ForeignException)2 HBaseRpcController (org.apache.hadoop.hbase.ipc.HBaseRpcController)2 ServiceException (org.apache.hadoop.hbase.shaded.com.google.protobuf.ServiceException)2 FileNotFoundException (java.io.FileNotFoundException)1 ConnectException (java.net.ConnectException)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 Callable (java.util.concurrent.Callable)1