Java Code Examples for org.apache.hadoop.hbase.regionserver.HRegion#getScanner()

The following examples show how to use org.apache.hadoop.hbase.regionserver.HRegion#getScanner() . 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: MetaDataEndpointImpl.java    From phoenix with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
private PTable buildDeletedTable(byte[] key, ImmutableBytesPtr cacheKey, HRegion region, long clientTimeStamp) throws IOException {
    if (clientTimeStamp == HConstants.LATEST_TIMESTAMP) {
        return null;
    }
    
    Scan scan = newTableRowsScan(key, clientTimeStamp, HConstants.LATEST_TIMESTAMP);
    scan.setFilter(new FirstKeyOnlyFilter());
    scan.setRaw(true);
    RegionScanner scanner = region.getScanner(scan);
    List<KeyValue> results = Lists.<KeyValue>newArrayList();
    scanner.next(results);
    // HBase ignores the time range on a raw scan (HBASE-7362)
    if (!results.isEmpty() && results.get(0).getTimestamp() > clientTimeStamp) {
        KeyValue kv = results.get(0);
        if (kv.isDelete()) {
            Map<ImmutableBytesPtr,PTable> metaDataCache = GlobalCache.getInstance(this.getEnvironment()).getMetaDataCache();
            PTable table = newDeletedTableMarker(kv.getTimestamp());
            metaDataCache.put(cacheKey, table);
            return table;
        }
    }
    return null;
}
 
Example 2
Source File: MetaDataEndpointImpl.java    From phoenix with Apache License 2.0 6 votes vote down vote up
private PTable buildDeletedTable(byte[] key, ImmutableBytesPtr cacheKey, HRegion region,
    long clientTimeStamp) throws IOException {
    if (clientTimeStamp == HConstants.LATEST_TIMESTAMP) {
        return null;
    }

    Scan scan = MetaDataUtil.newTableRowsScan(key, clientTimeStamp, HConstants.LATEST_TIMESTAMP);
    scan.setFilter(new FirstKeyOnlyFilter());
    scan.setRaw(true);
    List<Cell> results = Lists.<Cell> newArrayList();
    try (RegionScanner scanner = region.getScanner(scan);) {
      scanner.next(results);
    }
    // HBase ignores the time range on a raw scan (HBASE-7362)
    if (!results.isEmpty() && results.get(0).getTimestamp() > clientTimeStamp) {
        Cell kv = results.get(0);
        if (kv.getTypeByte() == Type.Delete.getCode()) {
            Cache<ImmutableBytesPtr, PTable> metaDataCache =
                    GlobalCache.getInstance(this.env).getMetaDataCache();
            PTable table = newDeletedTableMarker(kv.getTimestamp());
            metaDataCache.put(cacheKey, table);
            return table;
        }
    }
    return null;
}
 
Example 3
Source File: TransactionProcessorTest.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("StatementWithEmptyBody")
private void scanAndAssert(HRegion region, List<Cell> expected, Scan scan) throws Exception {
  try (RegionScanner regionScanner = region.getScanner(scan)) {
    List<Cell> results = Lists.newArrayList();
    while (regionScanner.next(results)) { }
    assertEquals(expected, results);
  }
}
 
Example 4
Source File: MetaDataEndpointImpl.java    From phoenix with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
private PTable buildTable(byte[] key, ImmutableBytesPtr cacheKey, HRegion region, long clientTimeStamp) throws IOException, SQLException {
    Scan scan = newTableRowsScan(key, MIN_TABLE_TIMESTAMP, clientTimeStamp);
    RegionScanner scanner = region.getScanner(scan);
    Map<ImmutableBytesPtr,PTable> metaDataCache = GlobalCache.getInstance(this.getEnvironment()).getMetaDataCache();
    try {
        PTable oldTable = metaDataCache.get(cacheKey);
        long tableTimeStamp = oldTable == null ? MIN_TABLE_TIMESTAMP-1 : oldTable.getTimeStamp();
        PTable newTable;
        newTable = getTable(scanner, clientTimeStamp, tableTimeStamp);
        if (newTable == null) {
            return null;
        }
        if (oldTable == null || tableTimeStamp < newTable.getTimeStamp()) {
            if (logger.isDebugEnabled()) {
                logger.debug("Caching table " + Bytes.toStringBinary(cacheKey.get(), cacheKey.getOffset(), cacheKey.getLength()) + " at seqNum " + newTable.getSequenceNumber() + " with newer timestamp " + newTable.getTimeStamp() + " versus " + tableTimeStamp);
            }
            oldTable = metaDataCache.put(cacheKey, newTable);
            if (logger.isDebugEnabled()) {
                if (oldTable == null) {
                    logger.debug("No previously cached table " + Bytes.toStringBinary(cacheKey.get(), cacheKey.getOffset(), cacheKey.getLength()));
                } else {
                    logger.debug("Previously cached table " + Bytes.toStringBinary(cacheKey.get(), cacheKey.getOffset(), cacheKey.getLength()) + " was at seqNum " + oldTable.getSequenceNumber() + " with timestamp " + oldTable.getTimeStamp());
                }
            }
        }
        return newTable;
    } finally {
        scanner.close();
    }
}
 
Example 5
Source File: TestRowProcessorEndpoint.java    From hbase with Apache License 2.0 5 votes vote down vote up
public static void doScan(HRegion region, Scan scan, List<Cell> result) throws IOException {
  InternalScanner scanner = null;
  try {
    scan.setIsolationLevel(IsolationLevel.READ_UNCOMMITTED);
    scanner = region.getScanner(scan);
    result.clear();
    scanner.next(result);
  } finally {
    if (scanner != null) {
      scanner.close();
    }
  }
}
 
Example 6
Source File: TransactionProcessorTest.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("StatementWithEmptyBody")
private void scanAndAssert(HRegion region, List<Cell> expected, Scan scan) throws Exception {
  try (RegionScanner regionScanner = region.getScanner(scan)) {
    List<Cell> results = Lists.newArrayList();
    while (regionScanner.next(results)) { }
    assertEquals(expected, results);
  }
}
 
Example 7
Source File: TransactionProcessorTest.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("StatementWithEmptyBody")
private void scanAndAssert(HRegion region, List<Cell> expected, Scan scan) throws Exception {
  try (RegionScanner regionScanner = region.getScanner(scan)) {
    List<Cell> results = Lists.newArrayList();
    while (regionScanner.next(results)) { }
    assertEquals(expected, results);
  }
}
 
Example 8
Source File: MetaDataEndpointImpl.java    From phoenix with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
/**
 * @param tableName parent table's name
 * @return true if there exist a table that use this table as their base table.
 * TODO: should we pass a timestamp here?
 */
private boolean hasViews(HRegion region, byte[] tenantId, PTable table) throws IOException {
    byte[] schemaName = table.getSchemaName().getBytes();
    byte[] tableName = table.getTableName().getBytes();
    Scan scan = new Scan();
    // If the table is multi-tenant, we need to check across all tenant_ids,
    // so we can't constrain the row key. Otherwise, any views would have
    // the same tenantId.
    if (!table.isMultiTenant()) {
        byte[] startRow = ByteUtil.concat(tenantId, QueryConstants.SEPARATOR_BYTE_ARRAY);
        byte[] stopRow = ByteUtil.nextKey(startRow);
        scan.setStartRow(startRow);
        scan.setStopRow(stopRow);
    }
    SingleColumnValueFilter filter1 = new SingleColumnValueFilter(TABLE_FAMILY_BYTES, BASE_SCHEMA_NAME_BYTES, EQUAL, schemaName);
    filter1.setFilterIfMissing(schemaName.length > 0);
    SingleColumnValueFilter filter2 = new SingleColumnValueFilter(TABLE_FAMILY_BYTES, BASE_TABLE_NAME_BYTES, EQUAL, tableName);
    filter2.setFilterIfMissing(true);
    BinaryComparator comparator = new BinaryComparator(ByteUtil.concat(tenantId, QueryConstants.SEPARATOR_BYTE_ARRAY, schemaName, QueryConstants.SEPARATOR_BYTE_ARRAY, tableName));
    RowFilter filter3 = new RowFilter(CompareOp.NOT_EQUAL,comparator);
    Filter filter = new FilterList(filter1,filter2,filter3);
    scan.setFilter(filter);
    RegionScanner scanner = region.getScanner(scan);
    try {
        List<KeyValue> results = newArrayList();
        scanner.next(results);
        return results.size() > 0;
    }
    finally {
        scanner.close();
    }
}
 
Example 9
Source File: TransactionProcessorTest.java    From phoenix-tephra with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("StatementWithEmptyBody")
private void scanAndAssert(HRegion region, List<Cell> expected, Scan scan) throws Exception {
  try (RegionScanner regionScanner = region.getScanner(scan)) {
    List<Cell> results = Lists.newArrayList();
    while (regionScanner.next(results)) { }
    assertEquals(expected, results);
  }
}
 
Example 10
Source File: TestMultipleColumnPrefixFilter.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Test
public void testMultipleColumnPrefixFilterWithColumnPrefixFilter() throws IOException {
  String family = "Family";
  TableDescriptorBuilder tableDescriptorBuilder =
    TableDescriptorBuilder.newBuilder(TableName.valueOf(name.getMethodName()));
  ColumnFamilyDescriptor columnFamilyDescriptor =
    ColumnFamilyDescriptorBuilder.newBuilder(Bytes.toBytes(family)).build();
  tableDescriptorBuilder.setColumnFamily(columnFamilyDescriptor);
  TableDescriptor tableDescriptor = tableDescriptorBuilder.build();
  RegionInfo info = RegionInfoBuilder.newBuilder(tableDescriptor.getTableName()).build();
  HRegion region = HBaseTestingUtility.createRegionAndWAL(info, TEST_UTIL.
    getDataTestDir(), TEST_UTIL.getConfiguration(), tableDescriptor);

  List<String> rows = generateRandomWords(100, "row");
  List<String> columns = generateRandomWords(10000, "column");
  long maxTimestamp = 2;

  String valueString = "ValueString";

  for (String row: rows) {
    Put p = new Put(Bytes.toBytes(row));
    p.setDurability(Durability.SKIP_WAL);
    for (String column: columns) {
      for (long timestamp = 1; timestamp <= maxTimestamp; timestamp++) {
        KeyValue kv = KeyValueTestUtil.create(row, family, column, timestamp,
            valueString);
        p.add(kv);
      }
    }
    region.put(p);
  }

  MultipleColumnPrefixFilter multiplePrefixFilter;
  Scan scan1 = new Scan();
  scan1.readAllVersions();
  byte [][] filter_prefix = new byte [1][];
  filter_prefix[0] = new byte [] {'p'};

  multiplePrefixFilter = new MultipleColumnPrefixFilter(filter_prefix);
  scan1.setFilter(multiplePrefixFilter);
  List<Cell> results1 = new ArrayList<>();
  InternalScanner scanner1 = region.getScanner(scan1);
  while (scanner1.next(results1))
    ;

  ColumnPrefixFilter singlePrefixFilter;
  Scan scan2 = new Scan();
  scan2.readAllVersions();
  singlePrefixFilter = new ColumnPrefixFilter(Bytes.toBytes("p"));

  scan2.setFilter(singlePrefixFilter);
  List<Cell> results2 = new ArrayList<>();
  InternalScanner scanner2 = region.getScanner(scan1);
  while (scanner2.next(results2))
    ;

  assertEquals(results1.size(), results2.size());

  HBaseTestingUtility.closeRegionAndWAL(region);
}
 
Example 11
Source File: TransactionProcessorTest.java    From phoenix-tephra with Apache License 2.0 4 votes vote down vote up
@Test
public void testDataJanitorRegionScanner() throws Exception {
  String tableName = "TestRegionScanner";
  byte[] familyBytes = Bytes.toBytes("f");
  byte[] columnBytes = Bytes.toBytes("c");
  HRegion region = createRegion(tableName, familyBytes, TimeUnit.HOURS.toMillis(3));
  try {
    region.initialize();
    TransactionStateCache cache = new TransactionStateCacheSupplier(conf).get();
    LOG.info("Coprocessor is using transaction state: " + waitForTransactionState(cache));

    for (int i = 1; i <= 8; i++) {
      for (int k = 1; k <= i; k++) {
        Put p = new Put(Bytes.toBytes(i));
        p.addColumn(familyBytes, columnBytes, V[k], Bytes.toBytes(V[k]));
        region.put(p);
      }
    }

    List<Cell> results = Lists.newArrayList();

    // force a flush to clear the data
    // during flush, the coprocessor should drop all KeyValues with timestamps in the invalid set

    LOG.info("Flushing region " + region.getRegionInfo().getRegionNameAsString());
    FlushResultImpl flushResult = region.flushcache(true, false, new FlushLifeCycleTracker() { });
    Assert.assertTrue("Unexpected flush result: " + flushResult, flushResult.isFlushSucceeded());

    // now a normal scan should only return the valid rows
    // do not use a filter here to test that cleanup works on flush
    Scan scan = new Scan();
    scan.setMaxVersions(10);
    RegionScanner regionScanner = region.getScanner(scan);

    // first returned value should be "4" with version "4"
    results.clear();
    assertTrue(regionScanner.next(results));
    assertKeyValueMatches(results, 4, new long[]{V[4]});

    results.clear();
    assertTrue(regionScanner.next(results));
    assertKeyValueMatches(results, 5, new long[] {V[4]});

    results.clear();
    assertTrue(regionScanner.next(results));
    assertKeyValueMatches(results, 6, new long[]{V[6], V[4]});

    results.clear();
    assertTrue(regionScanner.next(results));
    assertKeyValueMatches(results, 7, new long[]{V[6], V[4]});

    results.clear();
    assertFalse(regionScanner.next(results));
    assertKeyValueMatches(results, 8, new long[] {V[8], V[6], V[4]});
  } finally {
    region.close();
  }
}
 
Example 12
Source File: TransactionProcessorTest.java    From phoenix-tephra with Apache License 2.0 4 votes vote down vote up
@Test
public void testDeleteFiltering() throws Exception {
  String tableName = "TestDeleteFiltering";
  byte[] familyBytes = Bytes.toBytes("f");
  byte[] columnBytes = Bytes.toBytes("c");
  HRegion region = createRegion(tableName, familyBytes, 0);
  try {
    region.initialize();
    TransactionStateCache cache = new TransactionStateCacheSupplier(conf).get();
    LOG.info("Coprocessor is using transaction state: " + waitForTransactionState(cache));

    byte[] row = Bytes.toBytes(1);
    for (int i = 4; i < V.length; i++) {
      Put p = new Put(row);
      p.addColumn(familyBytes, columnBytes, V[i], Bytes.toBytes(V[i]));
      region.put(p);
    }

    // delete from the third entry back
    // take that cell's timestamp + 1 to simulate a delete in a new tx
    long deleteTs = V[5] + 1;
    Delete d = new Delete(row, deleteTs);
    LOG.info("Issuing delete at timestamp " + deleteTs);
    // row deletes are not yet supported (TransactionAwareHTable normally handles this)
    d.addColumns(familyBytes, columnBytes);
    region.delete(d);

    List<Cell> results = Lists.newArrayList();

    // force a flush to clear the data
    // during flush, we should drop the deleted version, but not the others
    LOG.info("Flushing region " + region.getRegionInfo().getRegionNameAsString());
    region.flushcache(true, false, new FlushLifeCycleTracker() { });

    // now a normal scan should return row with versions at: V[8], V[6].
    // V[7] is invalid and V[5] and prior are deleted.
    Scan scan = new Scan();
    scan.setMaxVersions(10);
    RegionScanner regionScanner = region.getScanner(scan);
    // should be only one row
    assertFalse(regionScanner.next(results));
    assertKeyValueMatches(results, 1,
        new long[]{V[8], V[6], deleteTs},
        new byte[][]{Bytes.toBytes(V[8]), Bytes.toBytes(V[6]), new byte[0]});
  } finally {
    region.close();
  }
}
 
Example 13
Source File: TestIntraRowPagination.java    From hbase with Apache License 2.0 4 votes vote down vote up
/**
 * Test from client side for scan with maxResultPerCF set
 */
@Test
public void testScanLimitAndOffset() throws Exception {
  //byte [] TABLE = HTestConst.DEFAULT_TABLE_BYTES;
  byte [][] ROWS = HTestConst.makeNAscii(HTestConst.DEFAULT_ROW_BYTES, 2);
  byte [][] FAMILIES = HTestConst.makeNAscii(HTestConst.DEFAULT_CF_BYTES, 3);
  byte [][] QUALIFIERS = HTestConst.makeNAscii(HTestConst.DEFAULT_QUALIFIER_BYTES, 10);

  TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor =
    new TableDescriptorBuilder.ModifyableTableDescriptor(
      TableName.valueOf(HTestConst.DEFAULT_TABLE_BYTES));

  RegionInfo info = RegionInfoBuilder.newBuilder(HTestConst.DEFAULT_TABLE).build();
  for (byte[] family : FAMILIES) {
    ColumnFamilyDescriptor familyDescriptor =
      new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(family);
    tableDescriptor.setColumnFamily(familyDescriptor);
  }
  HRegion region = HBaseTestingUtility.createRegionAndWAL(info, TEST_UTIL.getDataTestDir(),
      TEST_UTIL.getConfiguration(), tableDescriptor);
  try {
    Put put;
    Scan scan;
    Result result;
    boolean toLog = true;

    List<Cell> kvListExp = new ArrayList<>();

    int storeOffset = 1;
    int storeLimit = 3;
    for (int r = 0; r < ROWS.length; r++) {
      put = new Put(ROWS[r]);
      for (int c = 0; c < FAMILIES.length; c++) {
        for (int q = 0; q < QUALIFIERS.length; q++) {
          KeyValue kv = new KeyValue(ROWS[r], FAMILIES[c], QUALIFIERS[q], 1,
              HTestConst.DEFAULT_VALUE_BYTES);
          put.add(kv);
          if (storeOffset <= q && q < storeOffset + storeLimit) {
            kvListExp.add(kv);
          }
        }
      }
      region.put(put);
    }

    scan = new Scan();
    scan.setRowOffsetPerColumnFamily(storeOffset);
    scan.setMaxResultsPerColumnFamily(storeLimit);
    RegionScanner scanner = region.getScanner(scan);
    List<Cell> kvListScan = new ArrayList<>();
    List<Cell> results = new ArrayList<>();
    while (scanner.next(results) || !results.isEmpty()) {
      kvListScan.addAll(results);
      results.clear();
    }
    result = Result.create(kvListScan);
    TestScannersFromClientSide.verifyResult(result, kvListExp, toLog,
        "Testing scan with storeOffset and storeLimit");
  } finally {
    HBaseTestingUtility.closeRegionAndWAL(region);
  }
}
 
Example 14
Source File: TestScannerSelectionUsingKeyRange.java    From hbase with Apache License 2.0 4 votes vote down vote up
@Test
public void testScannerSelection() throws IOException {
  Configuration conf = TEST_UTIL.getConfiguration();
  conf.setInt("hbase.hstore.compactionThreshold", 10000);
  ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor familyDescriptor =
    new ColumnFamilyDescriptorBuilder.ModifyableColumnFamilyDescriptor(FAMILY_BYTES)
      .setBlockCacheEnabled(true)
      .setBloomFilterType(bloomType);
  TableDescriptorBuilder.ModifyableTableDescriptor tableDescriptor =
    new TableDescriptorBuilder.ModifyableTableDescriptor(TABLE);

  tableDescriptor.setColumnFamily(familyDescriptor);
  RegionInfo info = RegionInfoBuilder.newBuilder(TABLE).build();
  HRegion region = HBaseTestingUtility.createRegionAndWAL(info, TEST_UTIL.getDataTestDir(), conf,
    tableDescriptor);

  for (int iFile = 0; iFile < NUM_FILES; ++iFile) {
    for (int iRow = 0; iRow < NUM_ROWS; ++iRow) {
      Put put = new Put(Bytes.toBytes("row" + iRow));
      for (int iCol = 0; iCol < NUM_COLS_PER_ROW; ++iCol) {
        put.addColumn(FAMILY_BYTES, Bytes.toBytes("col" + iCol),
                Bytes.toBytes("value" + iFile + "_" + iRow + "_" + iCol));
      }
      region.put(put);
    }
    region.flush(true);
  }

  Scan scan = new Scan().withStartRow(Bytes.toBytes("aaa")).withStopRow(Bytes.toBytes("aaz"));
  BlockCache cache = BlockCacheFactory.createBlockCache(conf);
  InternalScanner scanner = region.getScanner(scan);
  List<Cell> results = new ArrayList<>();
  while (scanner.next(results)) {
  }
  scanner.close();
  assertEquals(0, results.size());
  if (cache instanceof LruBlockCache) {
    Set<String> accessedFiles = ((LruBlockCache)cache).getCachedFileNamesForTest();
    assertEquals(expectedCount, accessedFiles.size());
  }
  HBaseTestingUtility.closeRegionAndWAL(region);
}
 
Example 15
Source File: TransactionProcessorTest.java    From phoenix-tephra with Apache License 2.0 4 votes vote down vote up
@Test
public void testDeleteFiltering() throws Exception {
  String tableName = "TestDeleteFiltering";
  byte[] familyBytes = Bytes.toBytes("f");
  byte[] columnBytes = Bytes.toBytes("c");
  HRegion region = createRegion(tableName, familyBytes, 0);
  try {
    region.initialize();
    TransactionStateCache cache = new TransactionStateCacheSupplier(conf).get();
    LOG.info("Coprocessor is using transaction state: " + waitForTransactionState(cache));

    byte[] row = Bytes.toBytes(1);
    for (int i = 4; i < V.length; i++) {
      Put p = new Put(row);
      p.add(familyBytes, columnBytes, V[i], Bytes.toBytes(V[i]));
      region.put(p);
    }

    // delete from the third entry back
    // take that cell's timestamp + 1 to simulate a delete in a new tx
    long deleteTs = V[5] + 1;
    Delete d = new Delete(row, deleteTs);
    LOG.info("Issuing delete at timestamp " + deleteTs);
    // row deletes are not yet supported (TransactionAwareHTable normally handles this)
    d.deleteColumns(familyBytes, columnBytes);
    region.delete(d);

    List<Cell> results = Lists.newArrayList();

    // force a flush to clear the data
    // during flush, we should drop the deleted version, but not the others
    LOG.info("Flushing region " + region.getRegionNameAsString());
    region.flushcache();

    // now a normal scan should return row with versions at: V[8], V[6].
    // V[7] is invalid and V[5] and prior are deleted.
    Scan scan = new Scan();
    scan.setMaxVersions(10);
    RegionScanner regionScanner = region.getScanner(scan);
    // should be only one row
    assertFalse(regionScanner.next(results));
    assertKeyValueMatches(results, 1,
                          new long[]{V[8], V[6], deleteTs},
                          new byte[][]{Bytes.toBytes(V[8]), Bytes.toBytes(V[6]), new byte[0]});
  } finally {
    region.close();
  }
}
 
Example 16
Source File: IIEndpoint.java    From Kylin with Apache License 2.0 4 votes vote down vote up
@Override
public void getRows(RpcController controller, IIProtos.IIRequest request, RpcCallback<IIProtos.IIResponse> done) {

    CoprocessorRowType type;
    CoprocessorProjector projector;
    EndpointAggregators aggregators;
    CoprocessorFilter filter;

    type = CoprocessorRowType.deserialize(request.getType().toByteArray());
    projector = CoprocessorProjector.deserialize(request.getProjector().toByteArray());
    aggregators = EndpointAggregators.deserialize(request.getAggregator().toByteArray());
    filter = CoprocessorFilter.deserialize(request.getFilter().toByteArray());

    TableRecordInfoDigest tableRecordInfoDigest = aggregators.getTableRecordInfoDigest();

    IIProtos.IIResponse response = null;
    RegionScanner innerScanner = null;
    HRegion region = null;
    try {
        region = env.getRegion();
        innerScanner = region.getScanner(buildScan());
        region.startRegionOperation();

        synchronized (innerScanner) {
            IIKeyValueCodec codec = new IIKeyValueCodec(tableRecordInfoDigest);
            //TODO pass projector to codec to skip loading columns
            Iterable<Slice> slices = codec.decodeKeyValue(new HbaseServerKVIterator(innerScanner));

            if (aggregators.isEmpty()) {
                response = getNonAggregatedResponse(slices, filter, type);
            } else {
                response = getAggregatedResponse(slices, filter, type, projector, aggregators);
            }
        }
    } catch (IOException ioe) {
        System.out.println(ioe.toString());
        ResponseConverter.setControllerException(controller, ioe);
    } finally {
        IOUtils.closeQuietly(innerScanner);
        if (region != null) {
            try {
                region.closeRegionOperation();
            } catch (IOException e) {
                e.printStackTrace();
                throw new RuntimeException(e);
            }
        }
    }

    done.run(response);
}
 
Example 17
Source File: TransactionProcessorTest.java    From phoenix-tephra with Apache License 2.0 4 votes vote down vote up
@Test
public void testDeleteFiltering() throws Exception {
  String tableName = "TestDeleteFiltering";
  byte[] familyBytes = Bytes.toBytes("f");
  byte[] columnBytes = Bytes.toBytes("c");
  HRegion region = createRegion(tableName, familyBytes, 0);
  try {
    region.initialize();
    TransactionStateCache cache = new TransactionStateCacheSupplier(conf).get();
    LOG.info("Coprocessor is using transaction state: " + waitForTransactionState(cache));

    byte[] row = Bytes.toBytes(1);
    for (int i = 4; i < V.length; i++) {
      Put p = new Put(row);
      p.add(familyBytes, columnBytes, V[i], Bytes.toBytes(V[i]));
      region.put(p);
    }

    // delete from the third entry back
    // take that cell's timestamp + 1 to simulate a delete in a new tx
    long deleteTs = V[5] + 1;
    Delete d = new Delete(row, deleteTs);
    LOG.info("Issuing delete at timestamp " + deleteTs);
    // row deletes are not yet supported (TransactionAwareHTable normally handles this)
    d.deleteColumns(familyBytes, columnBytes, deleteTs);
    region.delete(d);

    List<Cell> results = Lists.newArrayList();

    // force a flush to clear the data
    // during flush, we should drop the deleted version, but not the others
    LOG.info("Flushing region " + region.getRegionNameAsString());
    region.flushcache();

    // now a normal scan should return row with versions at: V[8], V[6].
    // V[7] is invalid and V[5] and prior are deleted.
    Scan scan = new Scan();
    scan.setMaxVersions(10);
    RegionScanner regionScanner = region.getScanner(scan);
    // should be only one row
    assertFalse(regionScanner.next(results));
    assertKeyValueMatches(results, 1,
                          new long[]{V[8], V[6], deleteTs},
                          new byte[][]{Bytes.toBytes(V[8]), Bytes.toBytes(V[6]), new byte[0]});
  } finally {
    region.close();
  }
}
 
Example 18
Source File: TransactionProcessorTest.java    From phoenix-tephra with Apache License 2.0 4 votes vote down vote up
@Test
public void testDeleteFiltering() throws Exception {
  String tableName = "TestDeleteFiltering";
  byte[] familyBytes = Bytes.toBytes("f");
  byte[] columnBytes = Bytes.toBytes("c");
  HRegion region = createRegion(tableName, familyBytes, 0);
  try {
    region.initialize();
    TransactionStateCache cache = new TransactionStateCacheSupplier(conf).get();
    LOG.info("Coprocessor is using transaction state: " + waitForTransactionState(cache));

    byte[] row = Bytes.toBytes(1);
    for (int i = 4; i < V.length; i++) {
      Put p = new Put(row);
      p.add(familyBytes, columnBytes, V[i], Bytes.toBytes(V[i]));
      region.put(p);
    }

    // delete from the third entry back
    // take that cell's timestamp + 1 to simulate a delete in a new tx
    long deleteTs = V[5] + 1;
    Delete d = new Delete(row, deleteTs);
    LOG.info("Issuing delete at timestamp " + deleteTs);
    // row deletes are not yet supported (TransactionAwareHTable normally handles this)
    d.deleteColumns(familyBytes, columnBytes);
    region.delete(d);

    List<Cell> results = Lists.newArrayList();

    // force a flush to clear the data
    // during flush, we should drop the deleted version, but not the others
    LOG.info("Flushing region " + region.getRegionNameAsString());
    region.flushcache();

    // now a normal scan should return row with versions at: V[8], V[6].
    // V[7] is invalid and V[5] and prior are deleted.
    Scan scan = new Scan();
    scan.setMaxVersions(10);
    RegionScanner regionScanner = region.getScanner(scan);
    // should be only one row
    assertFalse(regionScanner.next(results));
    assertKeyValueMatches(results, 1,
                          new long[]{V[8], V[6], deleteTs},
                          new byte[][]{Bytes.toBytes(V[8]), Bytes.toBytes(V[6]), new byte[0]});
  } finally {
    region.close();
  }
}
 
Example 19
Source File: TransactionProcessorTest.java    From phoenix-tephra with Apache License 2.0 4 votes vote down vote up
@Test
public void testDataJanitorRegionScanner() throws Exception {
  String tableName = "TestRegionScanner";
  byte[] familyBytes = Bytes.toBytes("f");
  byte[] columnBytes = Bytes.toBytes("c");
  HRegion region = createRegion(tableName, familyBytes, TimeUnit.HOURS.toMillis(3));
  try {
    region.initialize();
    TransactionStateCache cache = new TransactionStateCacheSupplier(conf).get();
    LOG.info("Coprocessor is using transaction state: " + waitForTransactionState(cache));

    for (int i = 1; i <= 8; i++) {
      for (int k = 1; k <= i; k++) {
        Put p = new Put(Bytes.toBytes(i));
        p.add(familyBytes, columnBytes, V[k], Bytes.toBytes(V[k]));
        region.put(p);
      }
    }

    List<Cell> results = Lists.newArrayList();

    // force a flush to clear the data
    // during flush, the coprocessor should drop all KeyValues with timestamps in the invalid set
    LOG.info("Flushing region " + region.getRegionNameAsString());
    region.flushcache(); // in 0.96, there is no indication of success

    // now a normal scan should only return the valid rows - testing that cleanup works on flush
    Scan scan = new Scan();
    scan.setMaxVersions(10);
    RegionScanner regionScanner = region.getScanner(scan);

    // first returned value should be "4" with version "4"
    results.clear();
    assertTrue(regionScanner.next(results));
    assertKeyValueMatches(results, 4, new long[] {V[4]});

    results.clear();
    assertTrue(regionScanner.next(results));
    assertKeyValueMatches(results, 5, new long[] {V[4]});

    results.clear();
    assertTrue(regionScanner.next(results));
    assertKeyValueMatches(results, 6, new long[] {V[6], V[4]});

    results.clear();
    assertTrue(regionScanner.next(results));
    assertKeyValueMatches(results, 7, new long[] {V[6], V[4]});

    results.clear();
    assertFalse(regionScanner.next(results));
    assertKeyValueMatches(results, 8, new long[] {V[8], V[6], V[4]});
  } finally {
    region.close();
  }
}
 
Example 20
Source File: TransactionProcessorTest.java    From phoenix-tephra with Apache License 2.0 4 votes vote down vote up
@Test
public void testDataJanitorRegionScanner() throws Exception {
  String tableName = "TestRegionScanner";
  byte[] familyBytes = Bytes.toBytes("f");
  byte[] columnBytes = Bytes.toBytes("c");
  HRegion region = createRegion(tableName, familyBytes, TimeUnit.HOURS.toMillis(3));
  try {
    region.initialize();
    TransactionStateCache cache = new TransactionStateCacheSupplier(conf).get();
    LOG.info("Coprocessor is using transaction state: " + waitForTransactionState(cache));

    for (int i = 1; i <= 8; i++) {
      for (int k = 1; k <= i; k++) {
        Put p = new Put(Bytes.toBytes(i));
        p.add(familyBytes, columnBytes, V[k], Bytes.toBytes(V[k]));
        region.put(p);
      }
    }

    List<Cell> results = Lists.newArrayList();

    // force a flush to clear the data
    // during flush, the coprocessor should drop all KeyValues with timestamps in the invalid set

    LOG.info("Flushing region " + region.getRegionInfo().getRegionNameAsString());
    Region.FlushResult flushResult = region.flushcache(true, false);
    Assert.assertTrue("Unexpected flush result: " + flushResult, flushResult.isFlushSucceeded());

    // now a normal scan should only return the valid rows
    // do not use a filter here to test that cleanup works on flush
    Scan scan = new Scan();
    scan.setMaxVersions(10);
    RegionScanner regionScanner = region.getScanner(scan);

    // first returned value should be "4" with version "4"
    results.clear();
    assertTrue(regionScanner.next(results));
    assertKeyValueMatches(results, 4, new long[]{V[4]});

    results.clear();
    assertTrue(regionScanner.next(results));
    assertKeyValueMatches(results, 5, new long[] {V[4]});

    results.clear();
    assertTrue(regionScanner.next(results));
    assertKeyValueMatches(results, 6, new long[]{V[6], V[4]});

    results.clear();
    assertTrue(regionScanner.next(results));
    assertKeyValueMatches(results, 7, new long[]{V[6], V[4]});

    results.clear();
    assertFalse(regionScanner.next(results));
    assertKeyValueMatches(results, 8, new long[] {V[8], V[6], V[4]});
  } finally {
    region.close();
  }
}