Search in sources :

Example 91 with ServerName

use of org.apache.hadoop.hbase.ServerName in project hbase by apache.

the class BaseLoadBalancer method roundRobinAssignment.

/**
   * Round robin a list of regions to a list of servers
   */
private void roundRobinAssignment(Cluster cluster, List<HRegionInfo> regions, List<HRegionInfo> unassignedRegions, List<ServerName> servers, Map<ServerName, List<HRegionInfo>> assignments) {
    int numServers = servers.size();
    int numRegions = regions.size();
    int max = (int) Math.ceil((float) numRegions / numServers);
    int serverIdx = 0;
    if (numServers > 1) {
        serverIdx = RANDOM.nextInt(numServers);
    }
    int regionIdx = 0;
    for (int j = 0; j < numServers; j++) {
        ServerName server = servers.get((j + serverIdx) % numServers);
        List<HRegionInfo> serverRegions = new ArrayList<>(max);
        for (int i = regionIdx; i < numRegions; i += numServers) {
            HRegionInfo region = regions.get(i % numRegions);
            if (cluster.wouldLowerAvailability(region, server)) {
                unassignedRegions.add(region);
            } else {
                serverRegions.add(region);
                cluster.doAssignRegion(region, server);
            }
        }
        assignments.put(server, serverRegions);
        regionIdx++;
    }
}
Also used : HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) ServerName(org.apache.hadoop.hbase.ServerName) ArrayList(java.util.ArrayList)

Example 92 with ServerName

use of org.apache.hadoop.hbase.ServerName in project hbase by apache.

the class RegionStates method getTableRSRegionMap.

private Map<TableName, Map<ServerName, List<HRegionInfo>>> getTableRSRegionMap(Boolean bytable) {
    Map<TableName, Map<ServerName, List<HRegionInfo>>> result = new HashMap<>();
    for (Map.Entry<ServerName, Set<HRegionInfo>> e : serverHoldings.entrySet()) {
        for (HRegionInfo hri : e.getValue()) {
            if (hri.isMetaRegion())
                continue;
            TableName tablename = bytable ? hri.getTable() : TableName.valueOf(HConstants.ENSEMBLE_TABLE_NAME);
            Map<ServerName, List<HRegionInfo>> svrToRegions = result.get(tablename);
            if (svrToRegions == null) {
                svrToRegions = new HashMap<>(serverHoldings.size());
                result.put(tablename, svrToRegions);
            }
            List<HRegionInfo> regions = svrToRegions.get(e.getKey());
            if (regions == null) {
                regions = new ArrayList<>();
                svrToRegions.put(e.getKey(), regions);
            }
            regions.add(hri);
        }
    }
    return result;
}
Also used : SortedSet(java.util.SortedSet) TreeSet(java.util.TreeSet) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) TableName(org.apache.hadoop.hbase.TableName) ServerName(org.apache.hadoop.hbase.ServerName) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) TreeMap(java.util.TreeMap)

Example 93 with ServerName

use of org.apache.hadoop.hbase.ServerName in project hbase by apache.

the class RegionStates method getRegionInfo.

/**
   * Get the HRegionInfo from cache, if not there, from the hbase:meta table.
   * Be careful. Does RPC. Do not hold a lock or synchronize when you call this method.
   * @param  regionName
   * @return HRegionInfo for the region
   */
@SuppressWarnings("deprecation")
protected HRegionInfo getRegionInfo(final byte[] regionName) {
    String encodedName = HRegionInfo.encodeRegionName(regionName);
    RegionState regionState = getRegionState(encodedName);
    if (regionState != null) {
        return regionState.getRegion();
    }
    try {
        Pair<HRegionInfo, ServerName> p = MetaTableAccessor.getRegion(server.getConnection(), regionName);
        HRegionInfo hri = p == null ? null : p.getFirst();
        if (hri != null) {
            createRegionState(hri);
        }
        return hri;
    } catch (IOException e) {
        server.abort("Aborting because error occurred while reading " + Bytes.toStringBinary(regionName) + " from hbase:meta", e);
        return null;
    }
}
Also used : HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) ServerName(org.apache.hadoop.hbase.ServerName) IOException(java.io.IOException)

Example 94 with ServerName

use of org.apache.hadoop.hbase.ServerName in project hbase by apache.

the class RSRpcServices method closeRegion.

/**
   * Close a region on the region server.
   *
   * @param controller the RPC controller
   * @param request the request
   * @throws ServiceException
   */
@Override
@QosPriority(priority = HConstants.ADMIN_QOS)
public CloseRegionResponse closeRegion(final RpcController controller, final CloseRegionRequest request) throws ServiceException {
    final ServerName sn = (request.hasDestinationServer() ? ProtobufUtil.toServerName(request.getDestinationServer()) : null);
    try {
        checkOpen();
        if (request.hasServerStartCode()) {
            // check that we are the same server that this RPC is intended for.
            long serverStartCode = request.getServerStartCode();
            if (regionServer.serverName.getStartcode() != serverStartCode) {
                throw new ServiceException(new DoNotRetryIOException("This RPC was intended for a " + "different server with startCode: " + serverStartCode + ", this server is: " + regionServer.serverName));
            }
        }
        final String encodedRegionName = ProtobufUtil.getRegionEncodedName(request.getRegion());
        requestCount.increment();
        if (sn == null) {
            LOG.info("Close " + encodedRegionName + " without moving");
        } else {
            LOG.info("Close " + encodedRegionName + ", moving to " + sn);
        }
        boolean closed = regionServer.closeRegion(encodedRegionName, false, sn);
        CloseRegionResponse.Builder builder = CloseRegionResponse.newBuilder().setClosed(closed);
        return builder.build();
    } catch (IOException ie) {
        throw new ServiceException(ie);
    }
}
Also used : ServiceException(org.apache.hadoop.hbase.shaded.com.google.protobuf.ServiceException) DoNotRetryIOException(org.apache.hadoop.hbase.DoNotRetryIOException) ServerName(org.apache.hadoop.hbase.ServerName) ByteString(org.apache.hadoop.hbase.shaded.com.google.protobuf.ByteString) CloseRegionResponse(org.apache.hadoop.hbase.shaded.protobuf.generated.AdminProtos.CloseRegionResponse) InterruptedIOException(java.io.InterruptedIOException) IOException(java.io.IOException) DoNotRetryIOException(org.apache.hadoop.hbase.DoNotRetryIOException) HBaseIOException(org.apache.hadoop.hbase.HBaseIOException) QosPriority(org.apache.hadoop.hbase.ipc.QosPriority)

Example 95 with ServerName

use of org.apache.hadoop.hbase.ServerName in project hbase by apache.

the class HBaseAdmin method getRegion.

/**
   * @param regionName Name of a region.
   * @return a pair of HRegionInfo and ServerName if <code>regionName</code> is
   *  a verified region name (we call {@link
   *  MetaTableAccessor#getRegionLocation(Connection, byte[])}
   *  else null.
   * Throw IllegalArgumentException if <code>regionName</code> is null.
   * @throws IOException
   */
Pair<HRegionInfo, ServerName> getRegion(final byte[] regionName) throws IOException {
    if (regionName == null) {
        throw new IllegalArgumentException("Pass a table name or region name");
    }
    Pair<HRegionInfo, ServerName> pair = MetaTableAccessor.getRegion(connection, regionName);
    if (pair == null) {
        final AtomicReference<Pair<HRegionInfo, ServerName>> result = new AtomicReference<>(null);
        final String encodedName = Bytes.toString(regionName);
        MetaTableAccessor.Visitor visitor = new MetaTableAccessor.Visitor() {

            @Override
            public boolean visit(Result data) throws IOException {
                HRegionInfo info = MetaTableAccessor.getHRegionInfo(data);
                if (info == null) {
                    LOG.warn("No serialized HRegionInfo in " + data);
                    return true;
                }
                RegionLocations rl = MetaTableAccessor.getRegionLocations(data);
                boolean matched = false;
                ServerName sn = null;
                if (rl != null) {
                    for (HRegionLocation h : rl.getRegionLocations()) {
                        if (h != null && encodedName.equals(h.getRegionInfo().getEncodedName())) {
                            sn = h.getServerName();
                            info = h.getRegionInfo();
                            matched = true;
                        }
                    }
                }
                if (!matched)
                    return true;
                result.set(new Pair<>(info, sn));
                // found the region, stop
                return false;
            }
        };
        MetaTableAccessor.fullScanRegions(connection, visitor);
        pair = result.get();
    }
    return pair;
}
Also used : RegionLocations(org.apache.hadoop.hbase.RegionLocations) MetaTableAccessor(org.apache.hadoop.hbase.MetaTableAccessor) AtomicReference(java.util.concurrent.atomic.AtomicReference) HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) HRegionLocation(org.apache.hadoop.hbase.HRegionLocation) ServerName(org.apache.hadoop.hbase.ServerName) Pair(org.apache.hadoop.hbase.util.Pair) NameStringPair(org.apache.hadoop.hbase.shaded.protobuf.generated.HBaseProtos.NameStringPair)

Aggregations

ServerName (org.apache.hadoop.hbase.ServerName)426 HRegionInfo (org.apache.hadoop.hbase.HRegionInfo)202 Test (org.junit.Test)163 ArrayList (java.util.ArrayList)97 TableName (org.apache.hadoop.hbase.TableName)89 IOException (java.io.IOException)87 HashMap (java.util.HashMap)81 List (java.util.List)72 Map (java.util.Map)54 HRegionLocation (org.apache.hadoop.hbase.HRegionLocation)45 HTableDescriptor (org.apache.hadoop.hbase.HTableDescriptor)34 Table (org.apache.hadoop.hbase.client.Table)33 HashSet (java.util.HashSet)32 TreeMap (java.util.TreeMap)31 HColumnDescriptor (org.apache.hadoop.hbase.HColumnDescriptor)29 Configuration (org.apache.hadoop.conf.Configuration)26 HRegionServer (org.apache.hadoop.hbase.regionserver.HRegionServer)26 Pair (org.apache.hadoop.hbase.util.Pair)24 KeeperException (org.apache.zookeeper.KeeperException)23 InterruptedIOException (java.io.InterruptedIOException)22