Search in sources :

Example 21 with KeyValueTable

use of io.cdap.cdap.api.dataset.lib.KeyValueTable in project cdap by caskdata.

the class AuthorizationTest method verifyDummyData.

private void verifyDummyData(NamespaceId namespaceId, String datasetName) throws Exception {
    DataSetManager<KeyValueTable> outTableManager = getDataset(namespaceId.dataset(datasetName));
    KeyValueTable outputTable = outTableManager.get();
    Assert.assertEquals("world", Bytes.toString(outputTable.read("hello")));
}
Also used : KeyValueTable(io.cdap.cdap.api.dataset.lib.KeyValueTable)

Example 22 with KeyValueTable

use of io.cdap.cdap.api.dataset.lib.KeyValueTable in project cdap by caskdata.

the class AuthorizationTest method testCrossNSDatasetAccessFromService.

private void testCrossNSDatasetAccessFromService(ServiceManager serviceManager) throws Exception {
    NamespaceMeta outputDatasetNS = new NamespaceMeta.Builder().setName("outputNS").build();
    NamespaceId outputDatasetNSId = outputDatasetNS.getNamespaceId();
    DatasetId datasetId = outputDatasetNSId.dataset("store");
    Map<EntityId, Set<? extends Permission>> neededPrivileges = ImmutableMap.<EntityId, Set<? extends Permission>>builder().put(outputDatasetNSId, EnumSet.of(StandardPermission.GET, StandardPermission.CREATE, StandardPermission.DELETE)).put(datasetId, EnumSet.of(StandardPermission.CREATE, StandardPermission.GET, StandardPermission.DELETE)).put(outputDatasetNSId.datasetType("keyValueTable"), EnumSet.of(StandardPermission.UPDATE)).build();
    setUpPrivilegeAndRegisterForDeletion(ALICE, neededPrivileges);
    getNamespaceAdmin().create(outputDatasetNS);
    addDatasetInstance(datasetId, "keyValueTable");
    // switch to BOB
    SecurityRequestContext.setUserId(BOB.getName());
    Map<String, String> args = ImmutableMap.of(CrossNsDatasetAccessApp.OUTPUT_DATASET_NS, outputDatasetNS.getNamespaceId().getNamespace(), CrossNsDatasetAccessApp.OUTPUT_DATASET_NAME, "store");
    // Start the service as BOB
    serviceManager.start(args);
    // Call to the service would result in failure due to BOB doesn't have permission on the namespace as set in args
    URL url = new URL(serviceManager.getServiceURL(5, TimeUnit.SECONDS), "write/data");
    HttpResponse response = executeAuthenticated(HttpRequest.put(url));
    Assert.assertEquals(500, response.getResponseCode());
    // This is a hack that works around the fact that we cannot properly catch exceptions in the service handler.
    // TODO: Figure out a way to stop checking error messages.
    Assert.assertTrue("Wrong message " + response.getResponseBodyAsString(), response.getResponseBodyAsString().contains("'" + BOB + "' has insufficient privileges"));
    serviceManager.stop();
    serviceManager.waitForStopped(10, TimeUnit.SECONDS);
    SecurityRequestContext.setUserId(ALICE.getName());
    assertDatasetIsEmpty(outputDatasetNS.getNamespaceId(), "store");
    // Give BOB permission to write to the dataset in another namespace
    grantAndAssertSuccess(datasetId, BOB, EnumSet.of(StandardPermission.GET, StandardPermission.UPDATE));
    // switch back to BOB to run service again
    SecurityRequestContext.setUserId(BOB.getName());
    // Write data in another namespace should be successful now
    serviceManager.start(args);
    for (int i = 0; i < 10; i++) {
        url = new URL(serviceManager.getServiceURL(5, TimeUnit.SECONDS), "write/" + i);
        response = executeAuthenticated(HttpRequest.put(url));
        Assert.assertEquals(200, response.getResponseCode());
    }
    serviceManager.stop();
    serviceManager.waitForStopped(10, TimeUnit.SECONDS);
    // switch back to alice and verify the data its fine now to verify.
    SecurityRequestContext.setUserId(ALICE.getName());
    DataSetManager<KeyValueTable> dataSetManager = getDataset(outputDatasetNS.getNamespaceId().dataset("store"));
    KeyValueTable results = dataSetManager.get();
    for (int i = 0; i < 10; i++) {
        byte[] key = String.valueOf(i).getBytes(Charsets.UTF_8);
        Assert.assertArrayEquals(key, results.read(key));
    }
    getNamespaceAdmin().delete(outputDatasetNS.getNamespaceId());
}
Also used : PartitionedFileSet(io.cdap.cdap.api.dataset.lib.PartitionedFileSet) EnumSet(java.util.EnumSet) Set(java.util.Set) ImmutableSet(com.google.common.collect.ImmutableSet) HashSet(java.util.HashSet) HttpResponse(io.cdap.common.http.HttpResponse) URL(java.net.URL) Constraint(io.cdap.cdap.internal.schedule.constraint.Constraint) DatasetId(io.cdap.cdap.proto.id.DatasetId) EntityId(io.cdap.cdap.proto.id.EntityId) NamespaceMeta(io.cdap.cdap.proto.NamespaceMeta) KeyValueTable(io.cdap.cdap.api.dataset.lib.KeyValueTable) GrantedPermission(io.cdap.cdap.proto.security.GrantedPermission) ApplicationPermission(io.cdap.cdap.proto.security.ApplicationPermission) AccessPermission(io.cdap.cdap.proto.security.AccessPermission) Permission(io.cdap.cdap.proto.security.Permission) StandardPermission(io.cdap.cdap.proto.security.StandardPermission) NamespaceId(io.cdap.cdap.proto.id.NamespaceId)

Example 23 with KeyValueTable

use of io.cdap.cdap.api.dataset.lib.KeyValueTable in project cdap by caskdata.

the class AuthorizationTest method addDummyData.

private void addDummyData(NamespaceId namespaceId, String datasetName) throws Exception {
    DataSetManager<KeyValueTable> tableManager = getDataset(namespaceId.dataset(datasetName));
    KeyValueTable inputTable = tableManager.get();
    inputTable.write("hello", "world");
    tableManager.flush();
}
Also used : KeyValueTable(io.cdap.cdap.api.dataset.lib.KeyValueTable)

Example 24 with KeyValueTable

use of io.cdap.cdap.api.dataset.lib.KeyValueTable in project cdap by caskdata.

the class SparkTest method testClassicSpark.

@Test
public void testClassicSpark() throws Exception {
    ApplicationManager appManager = deploy(TestSparkApp.class);
    for (Class<?> sparkClass : Arrays.asList(TestSparkApp.ClassicSpark.class, TestSparkApp.ScalaClassicSpark.class)) {
        final SparkManager sparkManager = appManager.getSparkManager(sparkClass.getSimpleName());
        sparkManager.startAndWaitForGoodRun(ProgramRunStatus.COMPLETED, 5, TimeUnit.MINUTES);
    }
    KeyValueTable resultTable = this.<KeyValueTable>getDataset("ResultTable").get();
    Assert.assertEquals(1L, Bytes.toLong(resultTable.read(ClassicSparkProgram.class.getName())));
    Assert.assertEquals(1L, Bytes.toLong(resultTable.read(ScalaClassicSparkProgram.class.getName())));
}
Also used : ApplicationManager(io.cdap.cdap.test.ApplicationManager) SparkManager(io.cdap.cdap.test.SparkManager) KeyValueTable(io.cdap.cdap.api.dataset.lib.KeyValueTable) TestSparkApp(io.cdap.cdap.spark.app.TestSparkApp) ScalaClassicSparkProgram(io.cdap.cdap.spark.app.ScalaClassicSparkProgram) ClassicSparkProgram(io.cdap.cdap.spark.app.ClassicSparkProgram) ScalaClassicSparkProgram(io.cdap.cdap.spark.app.ScalaClassicSparkProgram) Test(org.junit.Test)

Example 25 with KeyValueTable

use of io.cdap.cdap.api.dataset.lib.KeyValueTable in project cdap by caskdata.

the class SparkTest method testTransaction.

@Test
public void testTransaction() throws Exception {
    ApplicationManager applicationManager = deploy(TestSparkApp.class);
    // Write some data to a local file
    File inputFile = TEMP_FOLDER.newFile();
    try (PrintWriter writer = new PrintWriter(Files.newBufferedWriter(inputFile.toPath(), StandardCharsets.UTF_8))) {
        writer.println("red fox");
        writer.println("brown fox");
        writer.println("grey fox");
        writer.println("brown bear");
        writer.println("black bear");
    }
    // Run the spark program
    SparkManager sparkManager = applicationManager.getSparkManager(TransactionSpark.class.getSimpleName());
    sparkManager.start(ImmutableMap.of("input.file", inputFile.getAbsolutePath(), "keyvalue.table", "KeyValueTable", "result.all.dataset", "SparkResult", "result.threshold", "2", "result.threshold.dataset", "SparkThresholdResult"));
    // Verify result from dataset before the Spark program terminates
    final DataSetManager<KeyValueTable> resultManager = getDataset("SparkThresholdResult");
    final KeyValueTable resultTable = resultManager.get();
    // Expect the threshold result dataset, with threshold >=2, contains [brown, fox, bear]
    Tasks.waitFor(ImmutableSet.of("brown", "fox", "bear"), () -> {
        // This is to start a new TX
        resultManager.flush();
        LOG.info("Reading from threshold result");
        try (CloseableIterator<KeyValue<byte[], byte[]>> itor = resultTable.scan(null, null)) {
            return ImmutableSet.copyOf(Iterators.transform(itor, input -> {
                String word = Bytes.toString(input.getKey());
                LOG.info("{}, {}", word, Bytes.toInt(input.getValue()));
                return word;
            }));
        }
    }, 3, TimeUnit.MINUTES, 1, TimeUnit.SECONDS);
    sparkManager.stop();
    sparkManager.waitForRun(ProgramRunStatus.KILLED, 60, TimeUnit.SECONDS);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) SparkLogParser(io.cdap.cdap.spark.app.SparkLogParser) Arrays(java.util.Arrays) TypeToken(com.google.gson.reflect.TypeToken) ClassicSparkProgram(io.cdap.cdap.spark.app.ClassicSparkProgram) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) PythonSpark(io.cdap.cdap.spark.app.PythonSpark) URL(java.net.URL) LoggerFactory(org.slf4j.LoggerFactory) Bytes(io.cdap.cdap.api.common.Bytes) KeyValue(io.cdap.cdap.api.dataset.lib.KeyValue) DatasetSQLSpark(io.cdap.cdap.spark.app.DatasetSQLSpark) ScheduleId(io.cdap.cdap.proto.id.ScheduleId) Gson(com.google.gson.Gson) Map(java.util.Map) ClassRule(org.junit.ClassRule) Scope(io.cdap.cdap.api.common.Scope) Application(io.cdap.cdap.api.app.Application) StringLengthUDT(io.cdap.cdap.spark.app.plugin.StringLengthUDT) PrintWriter(java.io.PrintWriter) Tasks(io.cdap.cdap.common.utils.Tasks) ImmutableSet(com.google.common.collect.ImmutableSet) TestSparkApp(io.cdap.cdap.spark.app.TestSparkApp) IdentityHashMap(java.util.IdentityHashMap) ImmutableMap(com.google.common.collect.ImmutableMap) ObjectMappedTable(io.cdap.cdap.api.dataset.lib.ObjectMappedTable) TransactionSpark(io.cdap.cdap.spark.app.TransactionSpark) Reader(java.io.Reader) ProgramRunStatus(io.cdap.cdap.proto.ProgramRunStatus) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) SparkServiceProgram(io.cdap.cdap.spark.app.SparkServiceProgram) List(java.util.List) Person(io.cdap.cdap.spark.app.Person) Stream(java.util.stream.Stream) ApplicationManager(io.cdap.cdap.test.ApplicationManager) ByteStreams(com.google.common.io.ByteStreams) FileSet(io.cdap.cdap.api.dataset.lib.FileSet) DataSetManager(io.cdap.cdap.test.DataSetManager) Constants(io.cdap.cdap.common.conf.Constants) FileSetArguments(io.cdap.cdap.api.dataset.lib.FileSetArguments) DirUtils(io.cdap.cdap.common.utils.DirUtils) SparkAppUsingGetDataset(io.cdap.cdap.spark.app.SparkAppUsingGetDataset) Joiner(com.google.common.base.Joiner) Iterables(com.google.common.collect.Iterables) StringLengthFunc(io.cdap.cdap.spark.app.plugin.StringLengthFunc) BeforeClass(org.junit.BeforeClass) Location(org.apache.twill.filesystem.Location) TestConfiguration(io.cdap.cdap.test.TestConfiguration) PluggableFunc(io.cdap.cdap.spark.app.plugin.PluggableFunc) HashMap(java.util.HashMap) Function(java.util.function.Function) Iterators(com.google.common.collect.Iterators) ArrayList(java.util.ArrayList) OutputStreamWriter(java.io.OutputStreamWriter) KeyValueTable(io.cdap.cdap.api.dataset.lib.KeyValueTable) PrintStream(java.io.PrintStream) SparkManager(io.cdap.cdap.test.SparkManager) Logger(org.slf4j.Logger) Files(java.nio.file.Files) ScalaDynamicSpark(io.cdap.cdap.spark.app.ScalaDynamicSpark) BufferedWriter(java.io.BufferedWriter) RuntimeArguments(io.cdap.cdap.api.common.RuntimeArguments) Test(org.junit.Test) IOException(java.io.IOException) Maps(com.google.common.collect.Maps) CloseableIterator(io.cdap.cdap.api.dataset.lib.CloseableIterator) InputStreamReader(java.io.InputStreamReader) ScalaSparkLogParser(io.cdap.cdap.spark.app.ScalaSparkLogParser) File(java.io.File) ScalaClassicSparkProgram(io.cdap.cdap.spark.app.ScalaClassicSparkProgram) TestFrameworkTestBase(io.cdap.cdap.test.base.TestFrameworkTestBase) TimeUnit(java.util.concurrent.TimeUnit) URLEncoder(java.net.URLEncoder) Ignore(org.junit.Ignore) WorkflowManager(io.cdap.cdap.test.WorkflowManager) Assert(org.junit.Assert) Collections(java.util.Collections) TemporaryFolder(org.junit.rules.TemporaryFolder) InputStream(java.io.InputStream) ApplicationManager(io.cdap.cdap.test.ApplicationManager) SparkManager(io.cdap.cdap.test.SparkManager) KeyValue(io.cdap.cdap.api.dataset.lib.KeyValue) KeyValueTable(io.cdap.cdap.api.dataset.lib.KeyValueTable) File(java.io.File) PrintWriter(java.io.PrintWriter) TransactionSpark(io.cdap.cdap.spark.app.TransactionSpark) Test(org.junit.Test)

Aggregations

KeyValueTable (io.cdap.cdap.api.dataset.lib.KeyValueTable)122 Test (org.junit.Test)65 ApplicationManager (io.cdap.cdap.test.ApplicationManager)59 HashMap (java.util.HashMap)27 SparkManager (io.cdap.cdap.test.SparkManager)26 Table (io.cdap.cdap.api.dataset.table.Table)21 TransactionExecutor (org.apache.tephra.TransactionExecutor)20 WorkflowManager (io.cdap.cdap.test.WorkflowManager)19 FileSet (io.cdap.cdap.api.dataset.lib.FileSet)18 ApplicationWithPrograms (io.cdap.cdap.internal.app.deploy.pipeline.ApplicationWithPrograms)18 KeyValue (io.cdap.cdap.api.dataset.lib.KeyValue)14 ServiceManager (io.cdap.cdap.test.ServiceManager)14 IOException (java.io.IOException)14 ArrayList (java.util.ArrayList)14 Location (org.apache.twill.filesystem.Location)14 ImmutableMap (com.google.common.collect.ImmutableMap)13 StructuredRecord (io.cdap.cdap.api.data.format.StructuredRecord)13 ETLStage (io.cdap.cdap.etl.proto.v2.ETLStage)13 File (java.io.File)13 URL (java.net.URL)12