Search in sources :

Example 1 with MeteredDataset

use of io.cdap.cdap.api.dataset.metrics.MeteredDataset in project cdap by caskdata.

the class SingleThreadDatasetCache method createDatasetInstance.

/**
 * Creates a new instance of a dataset based on the given information.
 */
private Dataset createDatasetInstance(DatasetCacheKey key, boolean recordLineage) {
    DatasetId datasetId = new DatasetId(key.getNamespace(), key.getName());
    Dataset dataset = instantiator.getDataset(datasetId, key.getArguments(), key.getAccessType());
    if (dataset instanceof MeteredDataset && metricsContext != null) {
        ((MeteredDataset) dataset).setMetricsCollector(metricsContext.childContext(Constants.Metrics.Tag.DATASET, key.getName()));
    }
    if (recordLineage) {
        instantiator.writeLineage(datasetId, key.getAccessType());
    }
    return dataset;
}
Also used : Dataset(io.cdap.cdap.api.dataset.Dataset) MeteredDataset(io.cdap.cdap.api.dataset.metrics.MeteredDataset) MeteredDataset(io.cdap.cdap.api.dataset.metrics.MeteredDataset) DatasetId(io.cdap.cdap.proto.id.DatasetId)

Example 2 with MeteredDataset

use of io.cdap.cdap.api.dataset.metrics.MeteredDataset in project cdap by caskdata.

the class TableTest method testMetrics.

private void testMetrics(boolean readless) throws Exception {
    final String tableName = "survive";
    DatasetProperties props = TableProperties.builder().setReadlessIncrementSupport(readless).build();
    DatasetAdmin admin = getTableAdmin(CONTEXT1, tableName, props);
    admin.create();
    try (Table table = getTable(CONTEXT1, tableName, props)) {
        final Map<String, Long> metrics = Maps.newHashMap();
        ((MeteredDataset) table).setMetricsCollector(new MetricsCollector() {

            @Override
            public void increment(String metricName, long value) {
                Long old = metrics.get(metricName);
                metrics.put(metricName, old == null ? value : old + value);
            }

            @Override
            public void gauge(String metricName, long value) {
                metrics.put(metricName, value);
            }
        });
        // Note that we don't need to finish tx for metrics to be reported
        Transaction tx0 = txClient.startShort();
        ((TransactionAware) table).startTx(tx0);
        int writes = 0;
        int reads = 0;
        table.put(new Put(R1, C1, V1));
        verifyDatasetMetrics(metrics, ++writes, reads);
        table.compareAndSwap(R1, C1, V1, V2);
        verifyDatasetMetrics(metrics, ++writes, ++reads);
        // note: will not write anything as expected value will not match
        table.compareAndSwap(R1, C1, V1, V2);
        verifyDatasetMetrics(metrics, writes, ++reads);
        table.increment(new Increment(R2, C2, 1L));
        if (readless) {
            verifyDatasetMetrics(metrics, ++writes, reads);
        } else {
            verifyDatasetMetrics(metrics, ++writes, ++reads);
        }
        table.incrementAndGet(new Increment(R2, C2, 1L));
        verifyDatasetMetrics(metrics, ++writes, ++reads);
        table.get(new Get(R1, C1, V1));
        verifyDatasetMetrics(metrics, writes, ++reads);
        Scanner scanner = table.scan(new Scan(null, null));
        while (scanner.next() != null) {
            verifyDatasetMetrics(metrics, writes, ++reads);
        }
        table.delete(new Delete(R1, C1, V1));
        verifyDatasetMetrics(metrics, ++writes, reads);
    } finally {
        // drop table
        admin.drop();
    }
}
Also used : MetricsCollector(io.cdap.cdap.api.metrics.MetricsCollector) Delete(io.cdap.cdap.api.dataset.table.Delete) Scanner(io.cdap.cdap.api.dataset.table.Scanner) Table(io.cdap.cdap.api.dataset.table.Table) HBaseTable(io.cdap.cdap.data2.dataset2.lib.table.hbase.HBaseTable) DatasetProperties(io.cdap.cdap.api.dataset.DatasetProperties) DatasetAdmin(io.cdap.cdap.api.dataset.DatasetAdmin) Put(io.cdap.cdap.api.dataset.table.Put) Transaction(org.apache.tephra.Transaction) TransactionAware(org.apache.tephra.TransactionAware) Increment(io.cdap.cdap.api.dataset.table.Increment) Get(io.cdap.cdap.api.dataset.table.Get) MeteredDataset(io.cdap.cdap.api.dataset.metrics.MeteredDataset) Scan(io.cdap.cdap.api.dataset.table.Scan)

Aggregations

MeteredDataset (io.cdap.cdap.api.dataset.metrics.MeteredDataset)2 Dataset (io.cdap.cdap.api.dataset.Dataset)1 DatasetAdmin (io.cdap.cdap.api.dataset.DatasetAdmin)1 DatasetProperties (io.cdap.cdap.api.dataset.DatasetProperties)1 Delete (io.cdap.cdap.api.dataset.table.Delete)1 Get (io.cdap.cdap.api.dataset.table.Get)1 Increment (io.cdap.cdap.api.dataset.table.Increment)1 Put (io.cdap.cdap.api.dataset.table.Put)1 Scan (io.cdap.cdap.api.dataset.table.Scan)1 Scanner (io.cdap.cdap.api.dataset.table.Scanner)1 Table (io.cdap.cdap.api.dataset.table.Table)1 MetricsCollector (io.cdap.cdap.api.metrics.MetricsCollector)1 HBaseTable (io.cdap.cdap.data2.dataset2.lib.table.hbase.HBaseTable)1 DatasetId (io.cdap.cdap.proto.id.DatasetId)1 Transaction (org.apache.tephra.Transaction)1 TransactionAware (org.apache.tephra.TransactionAware)1