Search in sources :

Example 1 with CoveredDeleteScanner

use of org.apache.phoenix.hbase.index.scanner.ScannerBuilder.CoveredDeleteScanner in project phoenix by apache.

the class LocalTableStateTest method testCorrectRollback.

/**
 * Test that we correctly rollback the state of keyvalue
 * @throws Exception
 */
@Test
@SuppressWarnings("unchecked")
public void testCorrectRollback() throws Exception {
    Put m = new Put(row);
    m.add(fam, qual, ts, val);
    // setup mocks
    RegionCoprocessorEnvironment env = Mockito.mock(RegionCoprocessorEnvironment.class);
    Region region = Mockito.mock(Region.class);
    Mockito.when(env.getRegion()).thenReturn(region);
    RegionScanner scanner = Mockito.mock(RegionScanner.class);
    Mockito.when(region.getScanner(Mockito.any(Scan.class))).thenReturn(scanner);
    final byte[] stored = Bytes.toBytes("stored-value");
    final KeyValue storedKv = new KeyValue(row, fam, qual, ts, Type.Put, stored);
    storedKv.setSequenceId(2);
    Mockito.when(scanner.next(Mockito.any(List.class))).thenAnswer(new Answer<Boolean>() {

        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            List<KeyValue> list = (List<KeyValue>) invocation.getArguments()[0];
            list.add(storedKv);
            return false;
        }
    });
    LocalHBaseState state = new LocalTable(env);
    LocalTableState table = new LocalTableState(state, m);
    // add the kvs from the mutation
    KeyValue kv = KeyValueUtil.ensureKeyValue(m.get(fam, qual).get(0));
    kv.setSequenceId(0);
    table.addPendingUpdates(kv);
    // setup the lookup
    ColumnReference col = new ColumnReference(fam, qual);
    table.setCurrentTimestamp(ts);
    // check that the value is there
    Pair<CoveredDeleteScanner, IndexUpdate> p = table.getIndexedColumnsTableState(Arrays.asList(col), false, false, indexMetaData);
    Scanner s = p.getFirst();
    assertEquals("Didn't get the pending mutation's value first", kv, s.next());
    // rollback that value
    table.rollback(Arrays.asList(kv));
    p = table.getIndexedColumnsTableState(Arrays.asList(col), false, false, indexMetaData);
    s = p.getFirst();
    assertEquals("Didn't correctly rollback the row - still found it!", null, s.next());
    Mockito.verify(env, Mockito.times(1)).getRegion();
    Mockito.verify(region, Mockito.times(1)).getScanner(Mockito.any(Scan.class));
}
Also used : LocalTable(org.apache.phoenix.hbase.index.covered.data.LocalTable) RegionScanner(org.apache.hadoop.hbase.regionserver.RegionScanner) CoveredDeleteScanner(org.apache.phoenix.hbase.index.scanner.ScannerBuilder.CoveredDeleteScanner) Scanner(org.apache.phoenix.hbase.index.scanner.Scanner) KeyValue(org.apache.hadoop.hbase.KeyValue) CoveredDeleteScanner(org.apache.phoenix.hbase.index.scanner.ScannerBuilder.CoveredDeleteScanner) Put(org.apache.hadoop.hbase.client.Put) RegionCoprocessorEnvironment(org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment) LocalHBaseState(org.apache.phoenix.hbase.index.covered.data.LocalHBaseState) RegionScanner(org.apache.hadoop.hbase.regionserver.RegionScanner) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Region(org.apache.hadoop.hbase.regionserver.Region) Scan(org.apache.hadoop.hbase.client.Scan) List(java.util.List) ColumnReference(org.apache.phoenix.hbase.index.covered.update.ColumnReference) Test(org.junit.Test)

Example 2 with CoveredDeleteScanner

use of org.apache.phoenix.hbase.index.scanner.ScannerBuilder.CoveredDeleteScanner in project phoenix by apache.

the class LocalTableStateTest method testCorrectOrderingWithLazyLoadingColumns.

@SuppressWarnings("unchecked")
@Test
public void testCorrectOrderingWithLazyLoadingColumns() throws Exception {
    Put m = new Put(row);
    m.add(fam, qual, ts, val);
    // setup mocks
    Configuration conf = new Configuration(false);
    RegionCoprocessorEnvironment env = Mockito.mock(RegionCoprocessorEnvironment.class);
    Mockito.when(env.getConfiguration()).thenReturn(conf);
    Region region = Mockito.mock(Region.class);
    Mockito.when(env.getRegion()).thenReturn(region);
    RegionScanner scanner = Mockito.mock(RegionScanner.class);
    Mockito.when(region.getScanner(Mockito.any(Scan.class))).thenReturn(scanner);
    final byte[] stored = Bytes.toBytes("stored-value");
    Mockito.when(scanner.next(Mockito.any(List.class))).thenAnswer(new Answer<Boolean>() {

        @Override
        public Boolean answer(InvocationOnMock invocation) throws Throwable {
            List<KeyValue> list = (List<KeyValue>) invocation.getArguments()[0];
            KeyValue kv = new KeyValue(row, fam, qual, ts, Type.Put, stored);
            kv.setSequenceId(0);
            list.add(kv);
            return false;
        }
    });
    LocalHBaseState state = new LocalTable(env);
    LocalTableState table = new LocalTableState(state, m);
    // add the kvs from the mutation
    table.addPendingUpdates(KeyValueUtil.ensureKeyValues(m.get(fam, qual)));
    // setup the lookup
    ColumnReference col = new ColumnReference(fam, qual);
    table.setCurrentTimestamp(ts);
    // check that our value still shows up first on scan, even though this is a lazy load
    Pair<CoveredDeleteScanner, IndexUpdate> p = table.getIndexedColumnsTableState(Arrays.asList(col), false, false, indexMetaData);
    Scanner s = p.getFirst();
    assertEquals("Didn't get the pending mutation's value first", m.get(fam, qual).get(0), s.next());
}
Also used : LocalTable(org.apache.phoenix.hbase.index.covered.data.LocalTable) RegionScanner(org.apache.hadoop.hbase.regionserver.RegionScanner) CoveredDeleteScanner(org.apache.phoenix.hbase.index.scanner.ScannerBuilder.CoveredDeleteScanner) Scanner(org.apache.phoenix.hbase.index.scanner.Scanner) KeyValue(org.apache.hadoop.hbase.KeyValue) Configuration(org.apache.hadoop.conf.Configuration) CoveredDeleteScanner(org.apache.phoenix.hbase.index.scanner.ScannerBuilder.CoveredDeleteScanner) Put(org.apache.hadoop.hbase.client.Put) RegionCoprocessorEnvironment(org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment) LocalHBaseState(org.apache.phoenix.hbase.index.covered.data.LocalHBaseState) RegionScanner(org.apache.hadoop.hbase.regionserver.RegionScanner) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Region(org.apache.hadoop.hbase.regionserver.Region) Scan(org.apache.hadoop.hbase.client.Scan) List(java.util.List) ColumnReference(org.apache.phoenix.hbase.index.covered.update.ColumnReference) Test(org.junit.Test)

Example 3 with CoveredDeleteScanner

use of org.apache.phoenix.hbase.index.scanner.ScannerBuilder.CoveredDeleteScanner in project phoenix by apache.

the class CoveredColumnIndexCodec method getIndexUpdateForGroup.

/**
 * @param group
 * @param state
 * @return the update that should be made to the table
 */
private IndexUpdate getIndexUpdateForGroup(ColumnGroup group, TableState state, IndexMetaData indexMetaData) {
    List<CoveredColumn> refs = group.getColumns();
    try {
        Pair<CoveredDeleteScanner, IndexUpdate> stateInfo = ((LocalTableState) state).getIndexedColumnsTableState(refs, false, false, indexMetaData);
        Scanner kvs = stateInfo.getFirst();
        Pair<Integer, List<ColumnEntry>> columns = getNextEntries(refs, kvs, state.getCurrentRowKey());
        // make sure we close the scanner
        kvs.close();
        if (columns.getFirst().intValue() == 0) {
            return stateInfo.getSecond();
        }
        // have all the column entries, so just turn it into a Delete for the row
        // convert the entries to the needed values
        byte[] rowKey = composeRowKey(state.getCurrentRowKey(), columns.getFirst(), columns.getSecond());
        Put p = new Put(rowKey, state.getCurrentTimestamp());
        // add the columns to the put
        addColumnsToPut(p, columns.getSecond());
        // update the index info
        IndexUpdate update = stateInfo.getSecond();
        update.setTable(Bytes.toBytes(group.getTable()));
        update.setUpdate(p);
        return update;
    } catch (IOException e) {
        throw new RuntimeException("Unexpected exception when getting state for columns: " + refs);
    }
}
Also used : CoveredDeleteScanner(org.apache.phoenix.hbase.index.scanner.ScannerBuilder.CoveredDeleteScanner) Scanner(org.apache.phoenix.hbase.index.scanner.Scanner) CoveredDeleteScanner(org.apache.phoenix.hbase.index.scanner.ScannerBuilder.CoveredDeleteScanner) IOException(java.io.IOException) Put(org.apache.hadoop.hbase.client.Put) ArrayList(java.util.ArrayList) List(java.util.List)

Example 4 with CoveredDeleteScanner

use of org.apache.phoenix.hbase.index.scanner.ScannerBuilder.CoveredDeleteScanner in project phoenix by apache.

the class CoveredColumnIndexCodec method getDeleteForGroup.

/**
 * Get all the deletes necessary for a group of columns - logically, the cleanup the index table for a given index.
 *
 * @param group
 *            index information
 * @return the cleanup for the given index, or <tt>null</tt> if no cleanup is necessary
 */
private IndexUpdate getDeleteForGroup(ColumnGroup group, TableState state, IndexMetaData indexMetaData) {
    List<CoveredColumn> refs = group.getColumns();
    try {
        Pair<CoveredDeleteScanner, IndexUpdate> kvs = ((LocalTableState) state).getIndexedColumnsTableState(refs, false, false, indexMetaData);
        Pair<Integer, List<ColumnEntry>> columns = getNextEntries(refs, kvs.getFirst(), state.getCurrentRowKey());
        // make sure we close the scanner reference
        kvs.getFirst().close();
        // no change, just return the passed update
        if (columns.getFirst() == 0) {
            return kvs.getSecond();
        }
        // have all the column entries, so just turn it into a Delete for the row
        // convert the entries to the needed values
        byte[] rowKey = composeRowKey(state.getCurrentRowKey(), columns.getFirst(), columns.getSecond());
        Delete d = new Delete(rowKey);
        d.setTimestamp(state.getCurrentTimestamp());
        IndexUpdate update = kvs.getSecond();
        update.setUpdate(d);
        update.setTable(Bytes.toBytes(group.getTable()));
        return update;
    } catch (IOException e) {
        throw new RuntimeException("Unexpected exception when getting state for columns: " + refs);
    }
}
Also used : Delete(org.apache.hadoop.hbase.client.Delete) CoveredDeleteScanner(org.apache.phoenix.hbase.index.scanner.ScannerBuilder.CoveredDeleteScanner) IOException(java.io.IOException) ArrayList(java.util.ArrayList) List(java.util.List)

Example 5 with CoveredDeleteScanner

use of org.apache.phoenix.hbase.index.scanner.ScannerBuilder.CoveredDeleteScanner in project phoenix by apache.

the class LocalTableState method getIndexUpdateState.

@Override
public Pair<ValueGetter, IndexUpdate> getIndexUpdateState(Collection<? extends ColumnReference> indexedColumns, boolean ignoreNewerMutations, boolean isStateForDeletes, IndexMetaData indexMetaData) throws IOException {
    Pair<CoveredDeleteScanner, IndexUpdate> pair = getIndexedColumnsTableState(indexedColumns, ignoreNewerMutations, isStateForDeletes, indexMetaData);
    ValueGetter valueGetter = IndexManagementUtil.createGetterFromScanner(pair.getFirst(), getCurrentRowKey());
    return new Pair<ValueGetter, IndexUpdate>(valueGetter, pair.getSecond());
}
Also used : CoveredDeleteScanner(org.apache.phoenix.hbase.index.scanner.ScannerBuilder.CoveredDeleteScanner) ValueGetter(org.apache.phoenix.hbase.index.ValueGetter) Pair(org.apache.hadoop.hbase.util.Pair)

Aggregations

CoveredDeleteScanner (org.apache.phoenix.hbase.index.scanner.ScannerBuilder.CoveredDeleteScanner)8 List (java.util.List)5 Put (org.apache.hadoop.hbase.client.Put)5 ColumnReference (org.apache.phoenix.hbase.index.covered.update.ColumnReference)5 Scanner (org.apache.phoenix.hbase.index.scanner.Scanner)5 Scan (org.apache.hadoop.hbase.client.Scan)4 RegionCoprocessorEnvironment (org.apache.hadoop.hbase.coprocessor.RegionCoprocessorEnvironment)4 Region (org.apache.hadoop.hbase.regionserver.Region)4 RegionScanner (org.apache.hadoop.hbase.regionserver.RegionScanner)4 LocalHBaseState (org.apache.phoenix.hbase.index.covered.data.LocalHBaseState)4 LocalTable (org.apache.phoenix.hbase.index.covered.data.LocalTable)4 Test (org.junit.Test)4 KeyValue (org.apache.hadoop.hbase.KeyValue)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Configuration (org.apache.hadoop.conf.Configuration)2 Pair (org.apache.hadoop.hbase.util.Pair)2 Delete (org.apache.hadoop.hbase.client.Delete)1 Mutation (org.apache.hadoop.hbase.client.Mutation)1