Search in sources :

Example 1 with ImmutablePair

use of co.cask.cdap.common.utils.ImmutablePair in project cdap by caskdata.

the class MetadataStoreDataset method listKV.

// returns mapping of all that match the given keySet provided they pass the combinedFilter predicate
public <T> Map<MDSKey, T> listKV(Set<MDSKey> keySet, Type typeOfT, int limit, @Nullable Predicate<KeyValue<T>> combinedFilter) {
    // Sort fuzzy keys
    List<MDSKey> sortedKeys = Lists.newArrayList(keySet);
    Collections.sort(sortedKeys);
    // Scan using fuzzy filter
    byte[] startKey = sortedKeys.get(0).getKey();
    byte[] stopKey = Bytes.stopKeyForPrefix(sortedKeys.get(sortedKeys.size() - 1).getKey());
    List<ImmutablePair<byte[], byte[]>> fuzzyKeys = new ArrayList<>();
    for (MDSKey key : sortedKeys) {
        fuzzyKeys.add(getFuzzyKeyFor(key));
    }
    Scan scan = new Scan(startKey, stopKey, new FuzzyRowFilter(fuzzyKeys));
    return listCombinedFilterKV(scan, typeOfT, limit, combinedFilter);
}
Also used : ImmutablePair(co.cask.cdap.common.utils.ImmutablePair) ArrayList(java.util.ArrayList) Scan(co.cask.cdap.api.dataset.table.Scan)

Example 2 with ImmutablePair

use of co.cask.cdap.common.utils.ImmutablePair in project cdap by caskdata.

the class HBaseTable method setFilterIfNeeded.

private void setFilterIfNeeded(ScanBuilder scan, @Nullable Filter filter) {
    if (filter == null) {
        return;
    }
    if (filter instanceof FuzzyRowFilter) {
        FuzzyRowFilter fuzzyRowFilter = (FuzzyRowFilter) filter;
        List<Pair<byte[], byte[]>> fuzzyPairs = Lists.newArrayListWithExpectedSize(fuzzyRowFilter.getFuzzyKeysData().size());
        for (ImmutablePair<byte[], byte[]> pair : fuzzyRowFilter.getFuzzyKeysData()) {
            fuzzyPairs.add(Pair.newPair(pair.getFirst(), pair.getSecond()));
        }
        scan.setFilter(new org.apache.hadoop.hbase.filter.FuzzyRowFilter(fuzzyPairs));
    } else {
        throw new IllegalArgumentException("Unsupported filter: " + filter);
    }
}
Also used : FuzzyRowFilter(co.cask.cdap.data2.dataset2.lib.table.FuzzyRowFilter) ImmutablePair(co.cask.cdap.common.utils.ImmutablePair) Pair(org.apache.hadoop.hbase.util.Pair)

Example 3 with ImmutablePair

use of co.cask.cdap.common.utils.ImmutablePair in project cdap by caskdata.

the class ObjectStoreDatasetTest method testPairStore.

@Test
public void testPairStore() throws Exception {
    DatasetId pairs = DatasetFrameworkTestUtil.NAMESPACE_ID.dataset("pairs");
    createObjectStoreInstance(pairs, new TypeToken<ImmutablePair<Integer, String>>() {
    }.getType());
    final ObjectStoreDataset<ImmutablePair<Integer, String>> pairStore = dsFrameworkUtil.getInstance(pairs);
    TransactionExecutor txnl = dsFrameworkUtil.newInMemoryTransactionExecutor(pairStore);
    final ImmutablePair<Integer, String> pair = new ImmutablePair<>(1, "second");
    txnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            pairStore.write(a, pair);
        }
    });
    txnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            ImmutablePair<Integer, String> result = pairStore.read(a);
            Assert.assertEquals(pair, result);
        }
    });
    txnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            deleteAndVerify(pairStore, a);
        }
    });
    dsFrameworkUtil.deleteInstance(pairs);
}
Also used : ImmutablePair(co.cask.cdap.common.utils.ImmutablePair) TypeToken(com.google.common.reflect.TypeToken) TransactionExecutor(org.apache.tephra.TransactionExecutor) TransactionFailureException(org.apache.tephra.TransactionFailureException) NoSuchElementException(java.util.NoSuchElementException) DatasetId(co.cask.cdap.proto.id.DatasetId) Test(org.junit.Test)

Example 4 with ImmutablePair

use of co.cask.cdap.common.utils.ImmutablePair in project cdap by caskdata.

the class ObjectStoreDatasetTest method testInstantiateWrongClass.

@Test
public void testInstantiateWrongClass() throws Exception {
    DatasetId pairs = DatasetFrameworkTestUtil.NAMESPACE_ID.dataset("pairs");
    createObjectStoreInstance(pairs, new TypeToken<ImmutablePair<Integer, String>>() {
    }.getType());
    // note: due to type erasure, this succeeds
    final ObjectStoreDataset<Custom> store = dsFrameworkUtil.getInstance(pairs);
    TransactionExecutor storeTxnl = dsFrameworkUtil.newTransactionExecutor(store);
    // but now it must fail with incompatible type
    try {
        storeTxnl.execute(new TransactionExecutor.Subroutine() {

            @Override
            public void apply() throws Exception {
                Custom custom = new Custom(42, Lists.newArrayList("one", "two"));
                store.write(a, custom);
            }
        });
        Assert.fail("write should have failed with incompatible type");
    } catch (TransactionFailureException e) {
    // expected
    }
    // write a correct object to the pair store
    final ObjectStoreDataset<ImmutablePair<Integer, String>> pairStore = dsFrameworkUtil.getInstance(pairs);
    TransactionExecutor pairStoreTxnl = dsFrameworkUtil.newTransactionExecutor(pairStore);
    final ImmutablePair<Integer, String> pair = new ImmutablePair<>(1, "second");
    pairStoreTxnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            // should succeed
            pairStore.write(a, pair);
        }
    });
    pairStoreTxnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            ImmutablePair<Integer, String> actualPair = pairStore.read(a);
            Assert.assertEquals(pair, actualPair);
        }
    });
    // now try to read that as a custom object, should fail with class cast
    try {
        storeTxnl.execute(new TransactionExecutor.Subroutine() {

            @Override
            public void apply() throws Exception {
                Custom custom = store.read(a);
                Preconditions.checkNotNull(custom);
            }
        });
        Assert.fail("write should have failed with class cast exception");
    } catch (TransactionFailureException e) {
    // expected
    }
    pairStoreTxnl.execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() throws Exception {
            deleteAndVerify(pairStore, a);
        }
    });
    dsFrameworkUtil.deleteInstance(pairs);
}
Also used : TransactionExecutor(org.apache.tephra.TransactionExecutor) TransactionFailureException(org.apache.tephra.TransactionFailureException) NoSuchElementException(java.util.NoSuchElementException) DatasetId(co.cask.cdap.proto.id.DatasetId) TransactionFailureException(org.apache.tephra.TransactionFailureException) ImmutablePair(co.cask.cdap.common.utils.ImmutablePair) TypeToken(com.google.common.reflect.TypeToken) Test(org.junit.Test)

Example 5 with ImmutablePair

use of co.cask.cdap.common.utils.ImmutablePair in project cdap by caskdata.

the class HBaseQueueDebugger method scanQueue.

private void scanQueue(TransactionExecutor txExecutor, HBaseConsumerStateStore stateStore, QueueName queueName, QueueBarrier start, @Nullable QueueBarrier end, final QueueStatistics outStats) throws Exception {
    final byte[] queueRowPrefix = QueueEntryRow.getQueueRowPrefix(queueName);
    ConsumerGroupConfig groupConfig = start.getGroupConfig();
    printProgress("Got consumer group config: %s\n", groupConfig);
    HBaseQueueAdmin admin = queueClientFactory.getQueueAdmin();
    TableId tableId = admin.getDataTableId(queueName, QueueConstants.QueueType.SHARDED_QUEUE);
    HTable hTable = queueClientFactory.createHTable(tableId);
    printProgress("Looking at HBase table: %s\n", Bytes.toString(hTable.getTableName()));
    final byte[] stateColumnName = Bytes.add(QueueEntryRow.STATE_COLUMN_PREFIX, Bytes.toBytes(groupConfig.getGroupId()));
    int distributorBuckets = queueClientFactory.getDistributorBuckets(hTable.getTableDescriptor());
    ShardedHBaseQueueStrategy queueStrategy = new ShardedHBaseQueueStrategy(tableUtil, distributorBuckets);
    ScanBuilder scan = tableUtil.buildScan();
    scan.setStartRow(start.getStartRow());
    if (end != null) {
        scan.setStopRow(end.getStartRow());
    } else {
        scan.setStopRow(QueueEntryRow.getQueueEntryRowKey(queueName, Long.MAX_VALUE, Integer.MAX_VALUE));
    }
    // Needs to include meta column for row that doesn't have state yet.
    scan.addColumn(QueueEntryRow.COLUMN_FAMILY, QueueEntryRow.META_COLUMN);
    scan.addColumn(QueueEntryRow.COLUMN_FAMILY, stateColumnName);
    // Don't do block cache for debug tool. We don't want old blocks get cached
    scan.setCacheBlocks(false);
    scan.setMaxVersions(1);
    printProgress("Scanning section with scan: %s\n", scan.toString());
    List<Integer> instanceIds = Lists.newArrayList();
    if (groupConfig.getDequeueStrategy() == DequeueStrategy.FIFO) {
        instanceIds.add(0);
    } else {
        for (int instanceId = 0; instanceId < groupConfig.getGroupSize(); instanceId++) {
            instanceIds.add(instanceId);
        }
    }
    final int rowsCache = Integer.parseInt(System.getProperty(PROP_ROWS_CACHE, "100000"));
    for (final int instanceId : instanceIds) {
        printProgress("Processing instance %d", instanceId);
        ConsumerConfig consConfig = new ConsumerConfig(groupConfig, instanceId);
        final QueueScanner scanner = queueStrategy.createScanner(consConfig, hTable, scan.build(), rowsCache);
        try {
            txExecutor.execute(new TransactionExecutor.Procedure<HBaseConsumerStateStore>() {

                @Override
                public void apply(HBaseConsumerStateStore input) throws Exception {
                    ImmutablePair<byte[], Map<byte[], byte[]>> result;
                    while ((result = scanner.next()) != null) {
                        byte[] rowKey = result.getFirst();
                        Map<byte[], byte[]> columns = result.getSecond();
                        visitRow(outStats, input.getTransaction(), rowKey, columns.get(stateColumnName), queueRowPrefix.length);
                        if (showProgress() && outStats.getTotal() % rowsCache == 0) {
                            System.out.printf("\rProcessing instance %d: %s", instanceId, outStats.getReport(showTxTimestampOnly()));
                        }
                    }
                }
            }, stateStore);
        } catch (TransactionFailureException e) {
            // Ignore transaction not in progress exception as it's caused by short TX timeout on commit
            if (!(Throwables.getRootCause(e) instanceof TransactionNotInProgressException)) {
                throw Throwables.propagate(e);
            }
        }
        printProgress("\rProcessing instance %d: %s\n", instanceId, outStats.getReport(showTxTimestampOnly()));
    }
}
Also used : TableId(co.cask.cdap.data2.util.TableId) ShardedHBaseQueueStrategy(co.cask.cdap.data2.transaction.queue.hbase.ShardedHBaseQueueStrategy) ScanBuilder(co.cask.cdap.data2.util.hbase.ScanBuilder) TransactionExecutor(org.apache.tephra.TransactionExecutor) TransactionNotInProgressException(org.apache.tephra.TransactionNotInProgressException) HTable(org.apache.hadoop.hbase.client.HTable) TransactionNotInProgressException(org.apache.tephra.TransactionNotInProgressException) TransactionFailureException(org.apache.tephra.TransactionFailureException) NotFoundException(co.cask.cdap.common.NotFoundException) HBaseConsumerStateStore(co.cask.cdap.data2.transaction.queue.hbase.HBaseConsumerStateStore) TransactionFailureException(org.apache.tephra.TransactionFailureException) ImmutablePair(co.cask.cdap.common.utils.ImmutablePair) HBaseQueueAdmin(co.cask.cdap.data2.transaction.queue.hbase.HBaseQueueAdmin) ConsumerConfig(co.cask.cdap.data2.queue.ConsumerConfig) QueueScanner(co.cask.cdap.data2.transaction.queue.QueueScanner) ConsumerGroupConfig(co.cask.cdap.data2.queue.ConsumerGroupConfig) Map(java.util.Map)

Aggregations

ImmutablePair (co.cask.cdap.common.utils.ImmutablePair)12 FuzzyRowFilter (co.cask.cdap.data2.dataset2.lib.table.FuzzyRowFilter)3 ArrayList (java.util.ArrayList)3 TransactionExecutor (org.apache.tephra.TransactionExecutor)3 TransactionFailureException (org.apache.tephra.TransactionFailureException)3 Test (org.junit.Test)3 Scan (co.cask.cdap.api.dataset.table.Scan)2 ConfigModule (co.cask.cdap.common.guice.ConfigModule)2 DiscoveryRuntimeModule (co.cask.cdap.common.guice.DiscoveryRuntimeModule)2 IOModule (co.cask.cdap.common.guice.IOModule)2 DatasetId (co.cask.cdap.proto.id.DatasetId)2 TypeToken (com.google.common.reflect.TypeToken)2 Injector (com.google.inject.Injector)2 HashSet (java.util.HashSet)2 Map (java.util.Map)2 NoSuchElementException (java.util.NoSuchElementException)2 DimensionValue (co.cask.cdap.api.dataset.lib.cube.DimensionValue)1 Row (co.cask.cdap.api.dataset.table.Row)1 Scanner (co.cask.cdap.api.dataset.table.Scanner)1 PluginClass (co.cask.cdap.api.plugin.PluginClass)1