Search in sources :

Example 1 with KeyValue

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

the class TestClientScanner method testNoResultsHint.

@Test
@SuppressWarnings("unchecked")
public void testNoResultsHint() throws IOException {
    final Result[] results = new Result[1];
    KeyValue kv1 = new KeyValue("row".getBytes(), "cf".getBytes(), "cq".getBytes(), 1, Type.Maximum);
    results[0] = Result.create(new Cell[] { kv1 });
    RpcRetryingCaller<Result[]> caller = Mockito.mock(RpcRetryingCaller.class);
    Mockito.when(rpcFactory.<Result[]>newCaller()).thenReturn(caller);
    Mockito.when(caller.callWithoutRetries(Mockito.any(RetryingCallable.class), Mockito.anyInt())).thenAnswer(new Answer<Result[]>() {

        private int count = 0;

        @Override
        public Result[] answer(InvocationOnMock invocation) throws Throwable {
            ScannerCallableWithReplicas callable = invocation.getArgumentAt(0, ScannerCallableWithReplicas.class);
            switch(count) {
                case // initialize
                0:
                    count++;
                    callable.currentScannerCallable.setMoreResultsInRegion(MoreResults.UNKNOWN);
                    return results;
                // detect no more results
                case 1:
                case // close
                2:
                    count++;
                    return new Result[0];
                default:
                    throw new RuntimeException("Expected only 2 invocations");
            }
        }
    });
    // Set a much larger cache and buffer size than we'll provide
    scan.setCaching(100);
    scan.setMaxResultSize(1000 * 1000);
    try (MockClientScanner scanner = new MockClientScanner(conf, scan, TableName.valueOf(name.getMethodName()), clusterConn, rpcFactory, controllerFactory, pool, Integer.MAX_VALUE)) {
        scanner.setRpcFinished(true);
        InOrder inOrder = Mockito.inOrder(caller);
        scanner.loadCache();
        // One for fetching the results
        // One for fetching empty results and quit as we do not have moreResults hint.
        inOrder.verify(caller, Mockito.times(2)).callWithoutRetries(Mockito.any(RetryingCallable.class), Mockito.anyInt());
        assertEquals(1, scanner.cache.size());
        Result r = scanner.cache.poll();
        assertNotNull(r);
        CellScanner cs = r.cellScanner();
        assertTrue(cs.advance());
        assertEquals(kv1, cs.current());
        assertFalse(cs.advance());
    }
}
Also used : KeyValue(org.apache.hadoop.hbase.KeyValue) InOrder(org.mockito.InOrder) CellScanner(org.apache.hadoop.hbase.CellScanner) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Cell(org.apache.hadoop.hbase.Cell) Test(org.junit.Test)

Example 2 with KeyValue

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

the class TestClientScanner method testNoMoreResults.

@Test
@SuppressWarnings("unchecked")
public void testNoMoreResults() throws IOException {
    final Result[] results = new Result[1];
    KeyValue kv1 = new KeyValue("row".getBytes(), "cf".getBytes(), "cq".getBytes(), 1, Type.Maximum);
    results[0] = Result.create(new Cell[] { kv1 });
    RpcRetryingCaller<Result[]> caller = Mockito.mock(RpcRetryingCaller.class);
    Mockito.when(rpcFactory.<Result[]>newCaller()).thenReturn(caller);
    Mockito.when(caller.callWithoutRetries(Mockito.any(RetryingCallable.class), Mockito.anyInt())).thenAnswer(new Answer<Result[]>() {

        private int count = 0;

        @Override
        public Result[] answer(InvocationOnMock invocation) throws Throwable {
            ScannerCallableWithReplicas callable = invocation.getArgumentAt(0, ScannerCallableWithReplicas.class);
            switch(count) {
                case // initialize
                0:
                    count++;
                    callable.currentScannerCallable.setMoreResultsInRegion(MoreResults.NO);
                    return results;
                case // close
                1:
                    count++;
                    return null;
                default:
                    throw new RuntimeException("Expected only 2 invocations");
            }
        }
    });
    Mockito.when(rpcFactory.<Result[]>newCaller()).thenReturn(caller);
    // Set a much larger cache and buffer size than we'll provide
    scan.setCaching(100);
    scan.setMaxResultSize(1000 * 1000);
    try (MockClientScanner scanner = new MockClientScanner(conf, scan, TableName.valueOf(name.getMethodName()), clusterConn, rpcFactory, controllerFactory, pool, Integer.MAX_VALUE)) {
        scanner.setRpcFinished(true);
        InOrder inOrder = Mockito.inOrder(caller);
        scanner.loadCache();
        inOrder.verify(caller, Mockito.times(1)).callWithoutRetries(Mockito.any(RetryingCallable.class), Mockito.anyInt());
        assertEquals(1, scanner.cache.size());
        Result r = scanner.cache.poll();
        assertNotNull(r);
        CellScanner cs = r.cellScanner();
        assertTrue(cs.advance());
        assertEquals(kv1, cs.current());
        assertFalse(cs.advance());
    }
}
Also used : KeyValue(org.apache.hadoop.hbase.KeyValue) InOrder(org.mockito.InOrder) CellScanner(org.apache.hadoop.hbase.CellScanner) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Cell(org.apache.hadoop.hbase.Cell) Test(org.junit.Test)

Example 3 with KeyValue

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

the class TestClientScanner method testSizeLimit.

@Test
@SuppressWarnings("unchecked")
public void testSizeLimit() throws IOException {
    final Result[] results = new Result[1];
    KeyValue kv1 = new KeyValue("row".getBytes(), "cf".getBytes(), "cq".getBytes(), 1, Type.Maximum);
    results[0] = Result.create(new Cell[] { kv1 });
    RpcRetryingCaller<Result[]> caller = Mockito.mock(RpcRetryingCaller.class);
    Mockito.when(rpcFactory.<Result[]>newCaller()).thenReturn(caller);
    Mockito.when(caller.callWithoutRetries(Mockito.any(RetryingCallable.class), Mockito.anyInt())).thenAnswer(new Answer<Result[]>() {

        private int count = 0;

        @Override
        public Result[] answer(InvocationOnMock invocation) throws Throwable {
            ScannerCallableWithReplicas callable = invocation.getArgumentAt(0, ScannerCallableWithReplicas.class);
            switch(count) {
                case // initialize
                0:
                    count++;
                    // if we set no here the implementation will trigger a close
                    callable.currentScannerCallable.setMoreResultsInRegion(MoreResults.YES);
                    return results;
                case // close
                1:
                    count++;
                    return null;
                default:
                    throw new RuntimeException("Expected only 2 invocations");
            }
        }
    });
    Mockito.when(rpcFactory.<Result[]>newCaller()).thenReturn(caller);
    // Set a much larger cache
    scan.setCaching(100);
    // The single key-value will exit the loop
    scan.setMaxResultSize(1);
    try (MockClientScanner scanner = new MockClientScanner(conf, scan, TableName.valueOf(name.getMethodName()), clusterConn, rpcFactory, controllerFactory, pool, Integer.MAX_VALUE)) {
        InOrder inOrder = Mockito.inOrder(caller);
        scanner.loadCache();
        inOrder.verify(caller, Mockito.times(1)).callWithoutRetries(Mockito.any(RetryingCallable.class), Mockito.anyInt());
        assertEquals(1, scanner.cache.size());
        Result r = scanner.cache.poll();
        assertNotNull(r);
        CellScanner cs = r.cellScanner();
        assertTrue(cs.advance());
        assertEquals(kv1, cs.current());
        assertFalse(cs.advance());
    }
}
Also used : KeyValue(org.apache.hadoop.hbase.KeyValue) InOrder(org.mockito.InOrder) CellScanner(org.apache.hadoop.hbase.CellScanner) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Cell(org.apache.hadoop.hbase.Cell) Test(org.junit.Test)

Example 4 with KeyValue

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

the class TestScannersFromClientSide method testAsyncScanner.

private void testAsyncScanner(TableName table, int rowNumber, int familyNumber, int qualifierNumber, int caching, Consumer<Boolean> listener) throws Exception {
    assert rowNumber > 0;
    assert familyNumber > 0;
    assert qualifierNumber > 0;
    byte[] row = Bytes.toBytes("r");
    byte[] family = Bytes.toBytes("f");
    byte[] qualifier = Bytes.toBytes("q");
    byte[][] rows = makeNAsciiWithZeroPrefix(row, rowNumber);
    byte[][] families = makeNAsciiWithZeroPrefix(family, familyNumber);
    byte[][] qualifiers = makeNAsciiWithZeroPrefix(qualifier, qualifierNumber);
    Table ht = TEST_UTIL.createTable(table, families);
    boolean toLog = true;
    List<Cell> kvListExp = new ArrayList<>();
    List<Put> puts = new ArrayList<>();
    for (byte[] r : rows) {
        Put put = new Put(r);
        for (byte[] f : families) {
            for (byte[] q : qualifiers) {
                KeyValue kv = new KeyValue(r, f, q, 1, VALUE);
                put.add(kv);
                kvListExp.add(kv);
            }
        }
        puts.add(put);
        if (puts.size() > 1000) {
            ht.put(puts);
            puts.clear();
        }
    }
    if (!puts.isEmpty()) {
        ht.put(puts);
        puts.clear();
    }
    Scan scan = new Scan();
    scan.setAsyncPrefetch(true);
    if (caching > 0) {
        scan.setCaching(caching);
    }
    try (ResultScanner scanner = ht.getScanner(scan)) {
        assertTrue("Not instance of async scanner", scanner instanceof ClientAsyncPrefetchScanner);
        ((ClientAsyncPrefetchScanner) scanner).setPrefetchListener(listener);
        List<Cell> kvListScan = new ArrayList<>();
        Result result;
        boolean first = true;
        int actualRows = 0;
        while ((result = scanner.next()) != null) {
            ++actualRows;
            // waiting for cache. see HBASE-17376
            if (first) {
                TimeUnit.SECONDS.sleep(1);
                first = false;
            }
            for (Cell kv : result.listCells()) {
                kvListScan.add(kv);
            }
        }
        assertEquals(rowNumber, actualRows);
        // These cells may have different rows but it is ok. The Result#getRow
        // isn't used in the verifyResult()
        result = Result.create(kvListScan);
        verifyResult(result, kvListExp, toLog, "Testing async scan");
    }
    TEST_UTIL.deleteTable(table);
}
Also used : KeyValue(org.apache.hadoop.hbase.KeyValue) ArrayList(java.util.ArrayList) Cell(org.apache.hadoop.hbase.Cell)

Example 5 with KeyValue

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

the class SampleRegionWALObserver method preWALWrite.

@Override
public boolean preWALWrite(ObserverContext<? extends WALCoprocessorEnvironment> env, HRegionInfo info, WALKey logKey, WALEdit logEdit) throws IOException {
    boolean bypass = false;
    // check table name matches or not.
    if (!Bytes.equals(info.getTable().toBytes(), this.tableName)) {
        return bypass;
    }
    preWALWriteCalled = true;
    // here we're going to remove one keyvalue from the WALEdit, and add
    // another one to it.
    List<Cell> cells = logEdit.getCells();
    Cell deletedCell = null;
    for (Cell cell : cells) {
        // assume only one kv from the WALEdit matches.
        byte[] family = CellUtil.cloneFamily(cell);
        byte[] qulifier = CellUtil.cloneQualifier(cell);
        if (Arrays.equals(family, ignoredFamily) && Arrays.equals(qulifier, ignoredQualifier)) {
            LOG.debug("Found the KeyValue from WALEdit which should be ignored.");
            deletedCell = cell;
        }
        if (Arrays.equals(family, changedFamily) && Arrays.equals(qulifier, changedQualifier)) {
            LOG.debug("Found the KeyValue from WALEdit which should be changed.");
            cell.getValueArray()[cell.getValueOffset()] += 1;
        }
    }
    if (null != row) {
        cells.add(new KeyValue(row, addedFamily, addedQualifier));
    }
    if (deletedCell != null) {
        LOG.debug("About to delete a KeyValue from WALEdit.");
        cells.remove(deletedCell);
    }
    return bypass;
}
Also used : KeyValue(org.apache.hadoop.hbase.KeyValue) Cell(org.apache.hadoop.hbase.Cell)

Aggregations

KeyValue (org.apache.hadoop.hbase.KeyValue)735 Test (org.junit.Test)421 Cell (org.apache.hadoop.hbase.Cell)256 ArrayList (java.util.ArrayList)216 Put (org.apache.hadoop.hbase.client.Put)112 Scan (org.apache.hadoop.hbase.client.Scan)101 Configuration (org.apache.hadoop.conf.Configuration)87 Result (org.apache.hadoop.hbase.client.Result)83 Path (org.apache.hadoop.fs.Path)76 IOException (java.io.IOException)54 ByteBufferKeyValue (org.apache.hadoop.hbase.ByteBufferKeyValue)53 ArrayBackedTag (org.apache.hadoop.hbase.ArrayBackedTag)48 List (java.util.List)47 Tag (org.apache.hadoop.hbase.Tag)45 ByteBuffer (java.nio.ByteBuffer)43 HBaseConfiguration (org.apache.hadoop.hbase.HBaseConfiguration)42 HFileContextBuilder (org.apache.hadoop.hbase.io.hfile.HFileContextBuilder)39 TreeMap (java.util.TreeMap)38 TableName (org.apache.hadoop.hbase.TableName)37 FileSystem (org.apache.hadoop.fs.FileSystem)35