Search in sources :

Example 16 with RegionStates

use of org.apache.hadoop.hbase.master.RegionStates in project hbase by apache.

the class MergeTableRegionsProcedure method prepareMergeRegion.

/**
   * Prepare merge and do some check
   * @param env MasterProcedureEnv
   * @throws IOException
   */
private void prepareMergeRegion(final MasterProcedureEnv env) throws IOException {
    // Note: the following logic assumes that we only have 2 regions to merge.  In the future,
    // if we want to extend to more than 2 regions, the code needs to modify a little bit.
    //
    CatalogJanitor catalogJanitor = env.getMasterServices().getCatalogJanitor();
    boolean regionAHasMergeQualifier = !catalogJanitor.cleanMergeQualifier(regionsToMerge[0]);
    if (regionAHasMergeQualifier || !catalogJanitor.cleanMergeQualifier(regionsToMerge[1])) {
        String msg = "Skip merging regions " + getRegionsToMergeListFullNameString() + ", because region " + (regionAHasMergeQualifier ? regionsToMerge[0].getEncodedName() : regionsToMerge[1].getEncodedName()) + " has merge qualifier";
        LOG.warn(msg);
        throw new MergeRegionException(msg);
    }
    RegionStates regionStates = getAssignmentManager(env).getRegionStates();
    RegionState regionStateA = regionStates.getRegionState(regionsToMerge[0].getEncodedName());
    RegionState regionStateB = regionStates.getRegionState(regionsToMerge[1].getEncodedName());
    if (regionStateA == null || regionStateB == null) {
        throw new UnknownRegionException(regionStateA == null ? regionsToMerge[0].getEncodedName() : regionsToMerge[1].getEncodedName());
    }
    if (!regionStateA.isOpened() || !regionStateB.isOpened()) {
        throw new MergeRegionException("Unable to merge regions not online " + regionStateA + ", " + regionStateB);
    }
}
Also used : RegionState(org.apache.hadoop.hbase.master.RegionState) RegionStates(org.apache.hadoop.hbase.master.RegionStates) CatalogJanitor(org.apache.hadoop.hbase.master.CatalogJanitor) UnknownRegionException(org.apache.hadoop.hbase.UnknownRegionException) MergeRegionException(org.apache.hadoop.hbase.exceptions.MergeRegionException)

Example 17 with RegionStates

use of org.apache.hadoop.hbase.master.RegionStates in project hbase by apache.

the class HBaseTestingUtility method waitUntilAllRegionsAssigned.

/**
   * Wait until all regions for a table in hbase:meta have a non-empty
   * info:server, or until timeout.  This means all regions have been deployed,
   * master has been informed and updated hbase:meta with the regions deployed
   * server.
   * @param tableName the table name
   * @param timeout timeout, in milliseconds
   * @throws IOException
   */
public void waitUntilAllRegionsAssigned(final TableName tableName, final long timeout) throws IOException {
    final Table meta = getConnection().getTable(TableName.META_TABLE_NAME);
    try {
        long l = waitFor(timeout, 200, true, new ExplainingPredicate<IOException>() {

            @Override
            public String explainFailure() throws IOException {
                return explainTableAvailability(tableName);
            }

            @Override
            public boolean evaluate() throws IOException {
                boolean allRegionsAssigned = true;
                Scan scan = new Scan();
                scan.addFamily(HConstants.CATALOG_FAMILY);
                ResultScanner s = meta.getScanner(scan);
                try {
                    Result r;
                    while ((r = s.next()) != null) {
                        byte[] b = r.getValue(HConstants.CATALOG_FAMILY, HConstants.REGIONINFO_QUALIFIER);
                        HRegionInfo info = HRegionInfo.parseFromOrNull(b);
                        if (info != null && info.getTable().equals(tableName)) {
                            b = r.getValue(HConstants.CATALOG_FAMILY, HConstants.SERVER_QUALIFIER);
                            allRegionsAssigned &= (b != null);
                        }
                    }
                } finally {
                    s.close();
                }
                return allRegionsAssigned;
            }
        });
    } finally {
        meta.close();
    }
    // check from the master state if we are using a mini cluster
    if (!getHBaseClusterInterface().isDistributedCluster()) {
        // So, all regions are in the meta table but make sure master knows of the assignments before
        // returing -- sometimes this can lag.
        HMaster master = getHBaseCluster().getMaster();
        final RegionStates states = master.getAssignmentManager().getRegionStates();
        waitFor(timeout, 200, new ExplainingPredicate<IOException>() {

            @Override
            public String explainFailure() throws IOException {
                return explainTableAvailability(tableName);
            }

            @Override
            public boolean evaluate() throws IOException {
                List<HRegionInfo> hris = states.getRegionsOfTable(tableName);
                return hris != null && !hris.isEmpty();
            }
        });
    }
}
Also used : HTable(org.apache.hadoop.hbase.client.HTable) Table(org.apache.hadoop.hbase.client.Table) ResultScanner(org.apache.hadoop.hbase.client.ResultScanner) IOException(java.io.IOException) Result(org.apache.hadoop.hbase.client.Result) RegionStates(org.apache.hadoop.hbase.master.RegionStates) HMaster(org.apache.hadoop.hbase.master.HMaster) Scan(org.apache.hadoop.hbase.client.Scan) ArrayList(java.util.ArrayList) List(java.util.List)

Example 18 with RegionStates

use of org.apache.hadoop.hbase.master.RegionStates in project hbase by apache.

the class TestNamespaceAuditor method testRegionMerge.

@Test
public void testRegionMerge() throws Exception {
    String nsp1 = prefix + "_regiontest";
    NamespaceDescriptor nspDesc = NamespaceDescriptor.create(nsp1).addConfiguration(TableNamespaceManager.KEY_MAX_REGIONS, "3").addConfiguration(TableNamespaceManager.KEY_MAX_TABLES, "2").build();
    ADMIN.createNamespace(nspDesc);
    final TableName tableTwo = TableName.valueOf(nsp1 + TableName.NAMESPACE_DELIM + "table2");
    byte[] columnFamily = Bytes.toBytes("info");
    HTableDescriptor tableDescOne = new HTableDescriptor(tableTwo);
    tableDescOne.addFamily(new HColumnDescriptor(columnFamily));
    final int initialRegions = 3;
    ADMIN.createTable(tableDescOne, Bytes.toBytes("1"), Bytes.toBytes("2000"), initialRegions);
    Connection connection = ConnectionFactory.createConnection(UTIL.getConfiguration());
    try (Table table = connection.getTable(tableTwo)) {
        UTIL.loadNumericRows(table, Bytes.toBytes("info"), 1000, 1999);
    }
    ADMIN.flush(tableTwo);
    List<HRegionInfo> hris = ADMIN.getTableRegions(tableTwo);
    Collections.sort(hris);
    // merge the two regions
    final Set<String> encodedRegionNamesToMerge = Sets.newHashSet(hris.get(0).getEncodedName(), hris.get(1).getEncodedName());
    ADMIN.mergeRegionsAsync(hris.get(0).getEncodedNameAsBytes(), hris.get(1).getEncodedNameAsBytes(), false);
    UTIL.waitFor(10000, 100, new Waiter.ExplainingPredicate<Exception>() {

        @Override
        public boolean evaluate() throws Exception {
            RegionStates regionStates = UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager().getRegionStates();
            for (HRegionInfo hri : ADMIN.getTableRegions(tableTwo)) {
                if (encodedRegionNamesToMerge.contains(hri.getEncodedName())) {
                    return false;
                }
                if (!regionStates.isRegionInState(hri, RegionState.State.OPEN)) {
                    return false;
                }
            }
            return true;
        }

        @Override
        public String explainFailure() throws Exception {
            RegionStates regionStates = UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager().getRegionStates();
            for (HRegionInfo hri : ADMIN.getTableRegions(tableTwo)) {
                if (encodedRegionNamesToMerge.contains(hri.getEncodedName())) {
                    return hri + " which is expected to be merged is still online";
                }
                if (!regionStates.isRegionInState(hri, RegionState.State.OPEN)) {
                    return hri + " is still in not opened";
                }
            }
            return "Unknown";
        }
    });
    hris = ADMIN.getTableRegions(tableTwo);
    assertEquals(initialRegions - 1, hris.size());
    Collections.sort(hris);
    final HRegionInfo hriToSplit = hris.get(1);
    ADMIN.split(tableTwo, Bytes.toBytes("500"));
    UTIL.waitFor(10000, 100, new Waiter.ExplainingPredicate<Exception>() {

        @Override
        public boolean evaluate() throws Exception {
            RegionStates regionStates = UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager().getRegionStates();
            for (HRegionInfo hri : ADMIN.getTableRegions(tableTwo)) {
                if (hri.getEncodedName().equals(hriToSplit.getEncodedName())) {
                    return false;
                }
                if (!regionStates.isRegionInState(hri, RegionState.State.OPEN)) {
                    return false;
                }
            }
            return true;
        }

        @Override
        public String explainFailure() throws Exception {
            RegionStates regionStates = UTIL.getMiniHBaseCluster().getMaster().getAssignmentManager().getRegionStates();
            for (HRegionInfo hri : ADMIN.getTableRegions(tableTwo)) {
                if (hri.getEncodedName().equals(hriToSplit.getEncodedName())) {
                    return hriToSplit + " which is expected to be split is still online";
                }
                if (!regionStates.isRegionInState(hri, RegionState.State.OPEN)) {
                    return hri + " is still in not opened";
                }
            }
            return "Unknown";
        }
    });
    hris = ADMIN.getTableRegions(tableTwo);
    assertEquals(initialRegions, hris.size());
    Collections.sort(hris);
    // fail region merge through Coprocessor hook
    MiniHBaseCluster cluster = UTIL.getHBaseCluster();
    MasterCoprocessorHost cpHost = cluster.getMaster().getMasterCoprocessorHost();
    Coprocessor coprocessor = cpHost.findCoprocessor(CPMasterObserver.class.getName());
    CPMasterObserver masterObserver = (CPMasterObserver) coprocessor;
    masterObserver.failMerge(true);
    masterObserver.triggered = false;
    ADMIN.mergeRegionsAsync(hris.get(1).getEncodedNameAsBytes(), hris.get(2).getEncodedNameAsBytes(), false);
    masterObserver.waitUtilTriggered();
    hris = ADMIN.getTableRegions(tableTwo);
    assertEquals(initialRegions, hris.size());
    Collections.sort(hris);
    // verify that we cannot split
    HRegionInfo hriToSplit2 = hris.get(1);
    ADMIN.split(tableTwo, TableInputFormatBase.getSplitKey(hriToSplit2.getStartKey(), hriToSplit2.getEndKey(), true));
    Thread.sleep(2000);
    assertEquals(initialRegions, ADMIN.getTableRegions(tableTwo).size());
}
Also used : MasterCoprocessorHost(org.apache.hadoop.hbase.master.MasterCoprocessorHost) Table(org.apache.hadoop.hbase.client.Table) HColumnDescriptor(org.apache.hadoop.hbase.HColumnDescriptor) Connection(org.apache.hadoop.hbase.client.Connection) MiniHBaseCluster(org.apache.hadoop.hbase.MiniHBaseCluster) RestoreSnapshotException(org.apache.hadoop.hbase.snapshot.RestoreSnapshotException) DoNotRetryIOException(org.apache.hadoop.hbase.DoNotRetryIOException) KeeperException(org.apache.zookeeper.KeeperException) IOException(java.io.IOException) QuotaExceededException(org.apache.hadoop.hbase.quotas.QuotaExceededException) HTableDescriptor(org.apache.hadoop.hbase.HTableDescriptor) HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) TableName(org.apache.hadoop.hbase.TableName) RegionStates(org.apache.hadoop.hbase.master.RegionStates) Coprocessor(org.apache.hadoop.hbase.Coprocessor) NamespaceDescriptor(org.apache.hadoop.hbase.NamespaceDescriptor) Waiter(org.apache.hadoop.hbase.Waiter) Test(org.junit.Test)

Example 19 with RegionStates

use of org.apache.hadoop.hbase.master.RegionStates in project hbase by apache.

the class EnableTableProcedure method regionsToAssignWithServerName.

/**
   * @param regionsInMeta
   * @return List of regions neither in transition nor assigned.
   * @throws IOException
   */
private static Map<HRegionInfo, ServerName> regionsToAssignWithServerName(final MasterProcedureEnv env, final List<Pair<HRegionInfo, ServerName>> regionsInMeta) throws IOException {
    Map<HRegionInfo, ServerName> regionsToAssign = new HashMap<>(regionsInMeta.size());
    RegionStates regionStates = env.getMasterServices().getAssignmentManager().getRegionStates();
    for (Pair<HRegionInfo, ServerName> regionLocation : regionsInMeta) {
        HRegionInfo hri = regionLocation.getFirst();
        ServerName sn = regionLocation.getSecond();
        if (regionStates.isRegionOffline(hri)) {
            regionsToAssign.put(hri, sn);
        } else {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Skipping assign for the region " + hri + " during enable table " + hri.getTable() + " because its already in tranition or assigned.");
            }
        }
    }
    return regionsToAssign;
}
Also used : HRegionInfo(org.apache.hadoop.hbase.HRegionInfo) RegionStates(org.apache.hadoop.hbase.master.RegionStates) HashMap(java.util.HashMap) ServerName(org.apache.hadoop.hbase.ServerName)

Aggregations

RegionStates (org.apache.hadoop.hbase.master.RegionStates)19 HRegionInfo (org.apache.hadoop.hbase.HRegionInfo)13 TableName (org.apache.hadoop.hbase.TableName)8 Test (org.junit.Test)8 IOException (java.io.IOException)7 Table (org.apache.hadoop.hbase.client.Table)7 ServerName (org.apache.hadoop.hbase.ServerName)6 AssignmentManager (org.apache.hadoop.hbase.master.AssignmentManager)6 InterruptedIOException (java.io.InterruptedIOException)5 HMaster (org.apache.hadoop.hbase.master.HMaster)4 ArrayList (java.util.ArrayList)3 DoNotRetryIOException (org.apache.hadoop.hbase.DoNotRetryIOException)3 MiniHBaseCluster (org.apache.hadoop.hbase.MiniHBaseCluster)3 RegionState (org.apache.hadoop.hbase.master.RegionState)3 HRegionLocation (org.apache.hadoop.hbase.HRegionLocation)2 UnknownRegionException (org.apache.hadoop.hbase.UnknownRegionException)2 Result (org.apache.hadoop.hbase.client.Result)2 MergeRegionException (org.apache.hadoop.hbase.exceptions.MergeRegionException)2 HashMap (java.util.HashMap)1 List (java.util.List)1