Search in sources :

Example 1 with DatasetInstantiationException

use of io.cdap.cdap.api.data.DatasetInstantiationException in project cdap by caskdata.

the class MapReduceProgramRunnerTest method testWordCount.

@Test
public void testWordCount() throws Exception {
    // deploy to namespace default by default
    final ApplicationWithPrograms app = deployApp(AppWithMapReduce.class);
    final String inputPath = createInput();
    final java.io.File outputDir = new java.io.File(TEMP_FOLDER.newFolder(), "output");
    try {
        datasetCache.getDataset("someOtherNameSpace", "jobConfig");
        Assert.fail("getDataset() should throw an exception when accessing a non-existing dataset.");
    } catch (DatasetInstantiationException e) {
    // expected
    }
    // Should work if explicitly specify the default namespace
    final KeyValueTable jobConfigTable = datasetCache.getDataset(NamespaceId.DEFAULT.getNamespace(), "jobConfig");
    // write config into dataset
    Transactions.createTransactionExecutor(txExecutorFactory, jobConfigTable).execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() {
            jobConfigTable.write(Bytes.toBytes("inputPath"), Bytes.toBytes(inputPath));
            jobConfigTable.write(Bytes.toBytes("outputPath"), Bytes.toBytes(outputDir.getPath()));
        }
    });
    runProgram(app, AppWithMapReduce.ClassicWordCount.class, false, true);
    Assert.assertEquals("true", System.getProperty("partitioner.initialize"));
    Assert.assertEquals("true", System.getProperty("partitioner.destroy"));
    Assert.assertEquals("true", System.getProperty("partitioner.set.conf"));
    Assert.assertEquals("true", System.getProperty("comparator.initialize"));
    Assert.assertEquals("true", System.getProperty("comparator.destroy"));
    Assert.assertEquals("true", System.getProperty("comparator.set.conf"));
    File[] outputFiles = outputDir.listFiles(new FilenameFilter() {

        @Override
        public boolean accept(File dir, String name) {
            return name.startsWith("part-r-") && !name.endsWith(".crc");
        }
    });
    Assert.assertNotNull("no output files found", outputFiles);
    int lines = 0;
    for (File file : outputFiles) {
        lines += Files.readLines(file, Charsets.UTF_8).size();
    }
    // dummy check that output file is not empty
    Assert.assertTrue(lines > 0);
}
Also used : TransactionExecutor(org.apache.tephra.TransactionExecutor) File(java.io.File) FilenameFilter(java.io.FilenameFilter) ApplicationWithPrograms(io.cdap.cdap.internal.app.deploy.pipeline.ApplicationWithPrograms) KeyValueTable(io.cdap.cdap.api.dataset.lib.KeyValueTable) File(java.io.File) DatasetInstantiationException(io.cdap.cdap.api.data.DatasetInstantiationException) Test(org.junit.Test)

Example 2 with DatasetInstantiationException

use of io.cdap.cdap.api.data.DatasetInstantiationException in project cdap by caskdata.

the class MapReduceProgramRunnerTest method testMapreduceWithObjectStore.

@Test
public void testMapreduceWithObjectStore() throws Exception {
    // Deploy apps to another namespace and test cross-namespace access meanwhile
    final ApplicationWithPrograms app = deployApp(Id.Namespace.fromEntityId(new NamespaceId("someOtherNameSpace")), AppWithMapReduceUsingObjectStore.class);
    final ObjectStore<String> input = datasetCache.getDataset("someOtherNameSpace", "keys");
    // Get dataset from a non existing namespace
    try {
        datasetCache.getDataset("nonExistingNameSpace", "keys");
        Assert.fail("getDataset() should throw an exception when accessing dataset from a non-existing namespace.");
    } catch (DatasetInstantiationException e) {
    // expected
    }
    final String testString = "persisted data";
    // Populate some input
    Transactions.createTransactionExecutor(txExecutorFactory, (TransactionAware) input).execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() {
            input.write(Bytes.toBytes(testString), testString);
            input.write(Bytes.toBytes("distributed systems"), "distributed systems");
        }
    });
    runProgram(app, AppWithMapReduceUsingObjectStore.ComputeCounts.class, false, true);
    final KeyValueTable output = datasetCache.getDataset("someOtherNameSpace", "count");
    // read output and verify result
    Transactions.createTransactionExecutor(txExecutorFactory, output).execute(new TransactionExecutor.Subroutine() {

        @Override
        public void apply() {
            byte[] val = output.read(Bytes.toBytes(testString));
            Assert.assertTrue(val != null);
            Assert.assertEquals(Bytes.toString(val), Integer.toString(testString.length()));
            val = output.read(Bytes.toBytes("distributed systems"));
            Assert.assertTrue(val != null);
            Assert.assertEquals(Bytes.toString(val), "19");
        }
    });
}
Also used : ApplicationWithPrograms(io.cdap.cdap.internal.app.deploy.pipeline.ApplicationWithPrograms) TransactionAware(org.apache.tephra.TransactionAware) KeyValueTable(io.cdap.cdap.api.dataset.lib.KeyValueTable) TransactionExecutor(org.apache.tephra.TransactionExecutor) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) DatasetInstantiationException(io.cdap.cdap.api.data.DatasetInstantiationException) Test(org.junit.Test)

Example 3 with DatasetInstantiationException

use of io.cdap.cdap.api.data.DatasetInstantiationException in project cdap by caskdata.

the class SingleThreadDatasetCache method getDataset.

@Override
public <T extends Dataset> T getDataset(DatasetCacheKey key, boolean bypass) throws DatasetInstantiationException {
    Dataset dataset;
    try {
        if (bypass) {
            dataset = createDatasetInstance(key, true);
        } else {
            try {
                dataset = datasetCache.get(new AccessAwareDatasetCacheKey(key));
            } catch (ExecutionException | UncheckedExecutionException e) {
                throw e.getCause();
            }
        }
    } catch (DatasetInstantiationException | ServiceUnavailableException e) {
        throw e;
    } catch (Throwable t) {
        throw new DatasetInstantiationException(String.format("Could not instantiate dataset '%s:%s'", key.getNamespace(), key.getName()), t);
    }
    // make sure the dataset exists and is of the right type
    if (dataset == null) {
        throw new DatasetInstantiationException(String.format("Dataset '%s' does not exist", key.getName()));
    }
    T typedDataset;
    try {
        @SuppressWarnings("unchecked") T t = (T) dataset;
        typedDataset = t;
    } catch (Throwable t) {
        // must be ClassCastException
        throw new DatasetInstantiationException(String.format("Could not cast dataset '%s' to requested type. Actual type is %s.", key.getName(), dataset.getClass().getName()), t);
    }
    // any transaction aware that is not in the active tx-awares is added to the current tx context (if there is one).
    if (!bypass && dataset instanceof TransactionAware) {
        TransactionAware txAware = (TransactionAware) dataset;
        TransactionAware existing = activeTxAwares.get(key);
        if (existing == null) {
            activeTxAwares.put(key, txAware);
            if (txContext != null) {
                txContext.addTransactionAware(txAware);
            }
        } else if (existing != dataset) {
            // this better be the same dataset, otherwise the cache did not work
            throw new IllegalStateException(String.format("Unexpected state: Cache returned %s for %s, which is different from the " + "active transaction aware %s for the same key. This should never happen.", dataset, key, existing));
        }
    }
    return typedDataset;
}
Also used : UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) Dataset(io.cdap.cdap.api.dataset.Dataset) MeteredDataset(io.cdap.cdap.api.dataset.metrics.MeteredDataset) ServiceUnavailableException(io.cdap.cdap.common.ServiceUnavailableException) TransactionAware(org.apache.tephra.TransactionAware) UncheckedExecutionException(com.google.common.util.concurrent.UncheckedExecutionException) ExecutionException(java.util.concurrent.ExecutionException) DatasetInstantiationException(io.cdap.cdap.api.data.DatasetInstantiationException)

Example 4 with DatasetInstantiationException

use of io.cdap.cdap.api.data.DatasetInstantiationException in project cdap by caskdata.

the class NoSqlStructuredTableContext method getTable.

@Override
public StructuredTable getTable(StructuredTableId tableId) throws StructuredTableInstantiationException, TableNotFoundException {
    try {
        StructuredTableSchema schema = tableAdmin.getSchema(tableId);
        Map<String, String> arguments = new HashMap<>();
        if (schema.getIndexes().isEmpty()) {
            // No indexes on the table
            arguments.put(IndexedTable.INDEX_COLUMNS_CONF_KEY, "");
            arguments.put(IndexedTable.DYNAMIC_INDEXING_PREFIX, "");
        } else {
            arguments.put(IndexedTable.INDEX_COLUMNS_CONF_KEY, Joiner.on(",").join(schema.getIndexes()));
            arguments.put(IndexedTable.DYNAMIC_INDEXING_PREFIX, tableId.getName());
        }
        StructuredTable table = new NoSqlStructuredTable(datasetContext.getDataset(NoSqlStructuredTableAdmin.ENTITY_TABLE_NAME, arguments), schema);
        return new MetricStructuredTable(tableId, table, metricsCollector, emitTimeMetrics);
    } catch (DatasetInstantiationException e) {
        throw new StructuredTableInstantiationException(tableId, String.format("Error instantiating table %s", tableId), e);
    }
}
Also used : StructuredTableSchema(io.cdap.cdap.spi.data.table.StructuredTableSchema) StructuredTableInstantiationException(io.cdap.cdap.spi.data.StructuredTableInstantiationException) HashMap(java.util.HashMap) MetricStructuredTable(io.cdap.cdap.spi.data.common.MetricStructuredTable) StructuredTable(io.cdap.cdap.spi.data.StructuredTable) DatasetInstantiationException(io.cdap.cdap.api.data.DatasetInstantiationException) MetricStructuredTable(io.cdap.cdap.spi.data.common.MetricStructuredTable)

Example 5 with DatasetInstantiationException

use of io.cdap.cdap.api.data.DatasetInstantiationException in project cdap by caskdata.

the class EntityTableDatasetContext method getDataset.

@Override
public <T extends Dataset> T getDataset(String name, Map<String, String> arguments) {
    // this is the only method that gets called on this dataset context (see NoSqlStructuredTableContext)
    Dataset entityTable = entityTables.get(arguments);
    if (entityTable == null) {
        try {
            entityTable = datasetAccesor.getTableDataset(name, arguments);
            txContext.addTransactionAware((TransactionAware) entityTable);
            entityTables.put(arguments, entityTable);
        } catch (IOException e) {
            throw new DatasetInstantiationException("Cannot instantiate entity table", e);
        }
    }
    // noinspection unchecked
    return (T) entityTable;
}
Also used : Dataset(io.cdap.cdap.api.dataset.Dataset) IOException(java.io.IOException) DatasetInstantiationException(io.cdap.cdap.api.data.DatasetInstantiationException)

Aggregations

DatasetInstantiationException (io.cdap.cdap.api.data.DatasetInstantiationException)5 Dataset (io.cdap.cdap.api.dataset.Dataset)2 KeyValueTable (io.cdap.cdap.api.dataset.lib.KeyValueTable)2 ApplicationWithPrograms (io.cdap.cdap.internal.app.deploy.pipeline.ApplicationWithPrograms)2 TransactionAware (org.apache.tephra.TransactionAware)2 TransactionExecutor (org.apache.tephra.TransactionExecutor)2 Test (org.junit.Test)2 UncheckedExecutionException (com.google.common.util.concurrent.UncheckedExecutionException)1 MeteredDataset (io.cdap.cdap.api.dataset.metrics.MeteredDataset)1 ServiceUnavailableException (io.cdap.cdap.common.ServiceUnavailableException)1 NamespaceId (io.cdap.cdap.proto.id.NamespaceId)1 StructuredTable (io.cdap.cdap.spi.data.StructuredTable)1 StructuredTableInstantiationException (io.cdap.cdap.spi.data.StructuredTableInstantiationException)1 MetricStructuredTable (io.cdap.cdap.spi.data.common.MetricStructuredTable)1 StructuredTableSchema (io.cdap.cdap.spi.data.table.StructuredTableSchema)1 File (java.io.File)1 FilenameFilter (java.io.FilenameFilter)1 IOException (java.io.IOException)1 HashMap (java.util.HashMap)1 ExecutionException (java.util.concurrent.ExecutionException)1