Search in sources :

Example 1 with Region

use of org.apache.hadoop.hbase.regionserver.Region in project hbase by apache.

the class ColumnAggregationEndpointWithErrors method sum.

@Override
public void sum(RpcController controller, ColumnAggregationWithErrorsSumRequest request, RpcCallback<ColumnAggregationWithErrorsSumResponse> done) {
    // aggregate at each region
    Scan scan = new Scan();
    // Family is required in pb. Qualifier is not.
    byte[] family = request.getFamily().toByteArray();
    byte[] qualifier = request.hasQualifier() ? request.getQualifier().toByteArray() : null;
    if (request.hasQualifier()) {
        scan.addColumn(family, qualifier);
    } else {
        scan.addFamily(family);
    }
    int sumResult = 0;
    InternalScanner scanner = null;
    try {
        Region region = this.env.getRegion();
        // throw an exception for requests to the last region in the table, to test error handling
        if (Bytes.equals(region.getRegionInfo().getEndKey(), HConstants.EMPTY_END_ROW)) {
            throw new DoNotRetryIOException("An expected exception");
        }
        scanner = region.getScanner(scan);
        List<Cell> curVals = new ArrayList<>();
        boolean hasMore = false;
        do {
            curVals.clear();
            hasMore = scanner.next(curVals);
            for (Cell kv : curVals) {
                if (CellUtil.matchingQualifier(kv, qualifier)) {
                    sumResult += Bytes.toInt(kv.getValueArray(), kv.getValueOffset());
                }
            }
        } while (hasMore);
    } catch (IOException e) {
        CoprocessorRpcUtils.setControllerException(controller, e);
        // Set result to -1 to indicate error.
        sumResult = -1;
        LOG.info("Setting sum result to -1 to indicate error", e);
    } finally {
        if (scanner != null) {
            try {
                scanner.close();
            } catch (IOException e) {
                CoprocessorRpcUtils.setControllerException(controller, e);
                sumResult = -1;
                LOG.info("Setting sum result to -1 to indicate error", e);
            }
        }
    }
    done.run(ColumnAggregationWithErrorsSumResponse.newBuilder().setSum(sumResult).build());
}
Also used : InternalScanner(org.apache.hadoop.hbase.regionserver.InternalScanner) DoNotRetryIOException(org.apache.hadoop.hbase.DoNotRetryIOException) ArrayList(java.util.ArrayList) Region(org.apache.hadoop.hbase.regionserver.Region) Scan(org.apache.hadoop.hbase.client.Scan) DoNotRetryIOException(org.apache.hadoop.hbase.DoNotRetryIOException) IOException(java.io.IOException) Cell(org.apache.hadoop.hbase.Cell)

Example 2 with Region

use of org.apache.hadoop.hbase.regionserver.Region in project hbase by apache.

the class TestClassLoading method testClassLoadingFromLocalFS.

@Test
public // HBASE-3516: Test CP Class loading from local file system
void testClassLoadingFromLocalFS() throws Exception {
    File jarFile = buildCoprocessorJar(cpName3);
    // create a table that references the jar
    HTableDescriptor htd = new HTableDescriptor(TableName.valueOf(cpName3));
    htd.addFamily(new HColumnDescriptor("test"));
    htd.setValue("COPROCESSOR$1", getLocalPath(jarFile) + "|" + cpName3 + "|" + Coprocessor.PRIORITY_USER);
    Admin admin = TEST_UTIL.getAdmin();
    admin.createTable(htd);
    waitForTable(htd.getTableName());
    // verify that the coprocessor was loaded
    boolean found = false;
    MiniHBaseCluster hbase = TEST_UTIL.getHBaseCluster();
    for (Region region : hbase.getRegionServer(0).getOnlineRegionsLocalContext()) {
        if (region.getRegionInfo().getRegionNameAsString().startsWith(cpName3)) {
            found = (region.getCoprocessorHost().findCoprocessor(cpName3) != null);
        }
    }
    assertTrue("Class " + cpName3 + " was missing on a region", found);
}
Also used : Region(org.apache.hadoop.hbase.regionserver.Region) Admin(org.apache.hadoop.hbase.client.Admin)

Example 3 with Region

use of org.apache.hadoop.hbase.regionserver.Region in project hbase by apache.

the class TestClassLoading method loadingClassFromLibDirInJar.

void loadingClassFromLibDirInJar(String libPrefix) throws Exception {
    FileSystem fs = cluster.getFileSystem();
    File innerJarFile1 = buildCoprocessorJar(cpName1);
    File innerJarFile2 = buildCoprocessorJar(cpName2);
    File outerJarFile = new File(TEST_UTIL.getDataTestDir().toString(), "outer.jar");
    ClassLoaderTestHelper.addJarFilesToJar(outerJarFile, libPrefix, innerJarFile1, innerJarFile2);
    // copy the jars into dfs
    fs.copyFromLocalFile(new Path(outerJarFile.getPath()), new Path(fs.getUri().toString() + Path.SEPARATOR));
    String jarFileOnHDFS = fs.getUri().toString() + Path.SEPARATOR + outerJarFile.getName();
    assertTrue("Copy jar file to HDFS failed.", fs.exists(new Path(jarFileOnHDFS)));
    LOG.info("Copied jar file to HDFS: " + jarFileOnHDFS);
    // create a table that references the coprocessors
    HTableDescriptor htd = new HTableDescriptor(tableName);
    htd.addFamily(new HColumnDescriptor("test"));
    // without configuration values
    htd.setValue("COPROCESSOR$1", jarFileOnHDFS.toString() + "|" + cpName1 + "|" + Coprocessor.PRIORITY_USER);
    // with configuration values
    htd.setValue("COPROCESSOR$2", jarFileOnHDFS.toString() + "|" + cpName2 + "|" + Coprocessor.PRIORITY_USER + "|k1=v1,k2=v2,k3=v3");
    Admin admin = TEST_UTIL.getAdmin();
    if (admin.tableExists(tableName)) {
        if (admin.isTableEnabled(tableName)) {
            admin.disableTable(tableName);
        }
        admin.deleteTable(tableName);
    }
    admin.createTable(htd);
    waitForTable(htd.getTableName());
    // verify that the coprocessors were loaded
    boolean found1 = false, found2 = false, found2_k1 = false, found2_k2 = false, found2_k3 = false;
    MiniHBaseCluster hbase = TEST_UTIL.getHBaseCluster();
    for (Region region : hbase.getRegionServer(0).getOnlineRegionsLocalContext()) {
        if (region.getRegionInfo().getRegionNameAsString().startsWith(tableName.getNameAsString())) {
            CoprocessorEnvironment env;
            env = region.getCoprocessorHost().findCoprocessorEnvironment(cpName1);
            if (env != null) {
                found1 = true;
            }
            env = region.getCoprocessorHost().findCoprocessorEnvironment(cpName2);
            if (env != null) {
                found2 = true;
                Configuration conf = env.getConfiguration();
                found2_k1 = conf.get("k1") != null;
                found2_k2 = conf.get("k2") != null;
                found2_k3 = conf.get("k3") != null;
            }
        }
    }
    assertTrue("Class " + cpName1 + " was missing on a region", found1);
    assertTrue("Class " + cpName2 + " was missing on a region", found2);
    assertTrue("Configuration key 'k1' was missing on a region", found2_k1);
    assertTrue("Configuration key 'k2' was missing on a region", found2_k2);
    assertTrue("Configuration key 'k3' was missing on a region", found2_k3);
}
Also used : Path(org.apache.hadoop.fs.Path) Configuration(org.apache.hadoop.conf.Configuration) FileSystem(org.apache.hadoop.fs.FileSystem) Region(org.apache.hadoop.hbase.regionserver.Region) Admin(org.apache.hadoop.hbase.client.Admin)

Example 4 with Region

use of org.apache.hadoop.hbase.regionserver.Region in project hbase by apache.

the class MiniHBaseCluster method getServerHoldingRegion.

@Override
public ServerName getServerHoldingRegion(final TableName tn, byte[] regionName) throws IOException {
    // Assume there is only one master thread which is the active master.
    // If there are multiple master threads, the backup master threads
    // should hold some regions. Please refer to #countServedRegions
    // to see how we find out all regions.
    HMaster master = getMaster();
    Region region = master.getOnlineRegion(regionName);
    if (region != null) {
        return master.getServerName();
    }
    int index = getServerWith(regionName);
    if (index < 0) {
        return null;
    }
    return getRegionServer(index).getServerName();
}
Also used : HMaster(org.apache.hadoop.hbase.master.HMaster) HRegion(org.apache.hadoop.hbase.regionserver.HRegion) Region(org.apache.hadoop.hbase.regionserver.Region)

Example 5 with Region

use of org.apache.hadoop.hbase.regionserver.Region in project hbase by apache.

the class MiniHBaseCluster method getServerWith.

/**
   * Get the location of the specified region
   * @param regionName Name of the region in bytes
   * @return Index into List of {@link MiniHBaseCluster#getRegionServerThreads()}
   * of HRS carrying hbase:meta. Returns -1 if none found.
   */
public int getServerWith(byte[] regionName) {
    int index = -1;
    int count = 0;
    for (JVMClusterUtil.RegionServerThread rst : getRegionServerThreads()) {
        HRegionServer hrs = rst.getRegionServer();
        if (!hrs.isStopped()) {
            Region region = hrs.getOnlineRegion(regionName);
            if (region != null) {
                index = count;
                break;
            }
        }
        count++;
    }
    return index;
}
Also used : JVMClusterUtil(org.apache.hadoop.hbase.util.JVMClusterUtil) RegionServerThread(org.apache.hadoop.hbase.util.JVMClusterUtil.RegionServerThread) HRegion(org.apache.hadoop.hbase.regionserver.HRegion) Region(org.apache.hadoop.hbase.regionserver.Region) HRegionServer(org.apache.hadoop.hbase.regionserver.HRegionServer)

Aggregations

Region (org.apache.hadoop.hbase.regionserver.Region)155 Test (org.junit.Test)63 HRegion (org.apache.hadoop.hbase.regionserver.HRegion)38 ArrayList (java.util.ArrayList)34 Configuration (org.apache.hadoop.conf.Configuration)33 HRegionServer (org.apache.hadoop.hbase.regionserver.HRegionServer)33 TableName (org.apache.hadoop.hbase.TableName)26 Put (org.apache.hadoop.hbase.client.Put)26 Scan (org.apache.hadoop.hbase.client.Scan)24 IOException (java.io.IOException)22 ImmutableBytesPtr (org.apache.phoenix.hbase.index.util.ImmutableBytesPtr)22 Cell (org.apache.hadoop.hbase.Cell)19 HColumnDescriptor (org.apache.hadoop.hbase.HColumnDescriptor)19 CountDownLatch (java.util.concurrent.CountDownLatch)17 HRegionInfo (org.apache.hadoop.hbase.HRegionInfo)17 HTableDescriptor (org.apache.hadoop.hbase.HTableDescriptor)17 BlockCache (org.apache.hadoop.hbase.io.hfile.BlockCache)15 RegionScanner (org.apache.hadoop.hbase.regionserver.RegionScanner)15 Store (org.apache.hadoop.hbase.regionserver.Store)15 Mutation (org.apache.hadoop.hbase.client.Mutation)14