Search in sources :

Example 26 with PluginClass

use of co.cask.cdap.api.plugin.PluginClass in project cdap by caskdata.

the class DropNullTransform method getPluginClass.

private static PluginClass getPluginClass() {
    Map<String, PluginPropertyField> properties = new HashMap<>();
    properties.put("field", new PluginPropertyField("field", "", "string", true, false));
    return new PluginClass(Transform.PLUGIN_TYPE, "DropField", "", DropNullTransform.class.getName(), "config", properties);
}
Also used : HashMap(java.util.HashMap) PluginClass(co.cask.cdap.api.plugin.PluginClass) PluginPropertyField(co.cask.cdap.api.plugin.PluginPropertyField)

Example 27 with PluginClass

use of co.cask.cdap.api.plugin.PluginClass in project cdap by caskdata.

the class ArtifactInspectorTest method inspectAppsAndPlugins.

@Test
public void inspectAppsAndPlugins() throws Exception {
    Manifest manifest = new Manifest();
    manifest.getMainAttributes().put(ManifestFields.EXPORT_PACKAGE, InspectionApp.class.getPackage().getName());
    File appFile = createJar(InspectionApp.class, new File(TMP_FOLDER.newFolder(), "InspectionApp-1.0.0.jar"), manifest);
    Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "InspectionApp", "1.0.0");
    Location artifactLocation = Locations.toLocation(appFile);
    try (CloseableClassLoader artifactClassLoader = classLoaderFactory.createClassLoader(ImmutableList.of(artifactLocation).iterator(), new EntityImpersonator(artifactId.toEntityId(), new DefaultImpersonator(CConfiguration.create(), null)))) {
        ArtifactClasses classes = artifactInspector.inspectArtifact(artifactId, appFile, artifactClassLoader);
        // check app classes
        Set<ApplicationClass> expectedApps = ImmutableSet.of(new ApplicationClass(InspectionApp.class.getName(), "", new ReflectionSchemaGenerator(false).generate(InspectionApp.AConfig.class)));
        Assert.assertEquals(expectedApps, classes.getApps());
        // check plugin classes
        PluginClass expectedPlugin = new PluginClass(InspectionApp.PLUGIN_TYPE, InspectionApp.PLUGIN_NAME, InspectionApp.PLUGIN_DESCRIPTION, InspectionApp.AppPlugin.class.getName(), "pluginConf", ImmutableMap.of("y", new PluginPropertyField("y", "", "double", true, true), "isSomething", new PluginPropertyField("isSomething", "", "boolean", true, false)));
        Assert.assertEquals(ImmutableSet.of(expectedPlugin), classes.getPlugins());
    }
}
Also used : EntityImpersonator(co.cask.cdap.security.impersonation.EntityImpersonator) ApplicationClass(co.cask.cdap.api.artifact.ApplicationClass) CloseableClassLoader(co.cask.cdap.api.artifact.CloseableClassLoader) Manifest(java.util.jar.Manifest) ReflectionSchemaGenerator(co.cask.cdap.internal.io.ReflectionSchemaGenerator) DefaultImpersonator(co.cask.cdap.security.impersonation.DefaultImpersonator) PluginPropertyField(co.cask.cdap.api.plugin.PluginPropertyField) ArtifactClasses(co.cask.cdap.api.artifact.ArtifactClasses) Id(co.cask.cdap.common.id.Id) PluginClass(co.cask.cdap.api.plugin.PluginClass) File(java.io.File) Location(org.apache.twill.filesystem.Location) InspectionApp(co.cask.cdap.internal.app.runtime.artifact.app.inspection.InspectionApp) Test(org.junit.Test)

Example 28 with PluginClass

use of co.cask.cdap.api.plugin.PluginClass in project cdap by caskdata.

the class ArtifactStoreTest method testGetPluginsByParentArtifactRanges.

@Test
public void testGetPluginsByParentArtifactRanges() throws Exception {
    ArtifactRange parentArtifacts1 = new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "parent1", new ArtifactVersion("1.0.0"), new ArtifactVersion("5.0.0"));
    // we have 2 plugins of type A and 2 plugins of type B
    PluginClass pluginA1 = new PluginClass("A", "p1", "desc", "c.p1", "cfg", ImmutableMap.of("threshold", new PluginPropertyField("thresh", "description", "double", true, false), "retry", new PluginPropertyField("retries", "description", "int", false, false)));
    PluginClass pluginA2 = new PluginClass("A", "p2", "desc", "c.p2", "conf", ImmutableMap.of("stream", new PluginPropertyField("stream", "description", "string", true, false)));
    // add artifacts
    // not interested in artifact contents for this test, using some dummy value
    String contents = "0";
    // write parent artifacts
    List<String> parentArtifactsVersions = ImmutableList.of("1.0.0", "1.2.1", "2.0.0", "3.0.0", "4.0.0");
    for (String artifactVersion : parentArtifactsVersions) {
        Id.Artifact parentArtifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent1", artifactVersion);
        ArtifactMeta parentMeta = new ArtifactMeta(ArtifactClasses.builder().build());
        writeArtifact(parentArtifactId, parentMeta, contents);
    }
    // artifact artifactX-1.0.0 contains plugin A1
    Id.Artifact artifactXv100 = Id.Artifact.from(Id.Namespace.DEFAULT, "artifactX", "1.0.0");
    ArtifactMeta metaXv100 = new ArtifactMeta(ArtifactClasses.builder().addPlugin(pluginA1).build(), ImmutableSet.of(parentArtifacts1));
    writeArtifact(artifactXv100, metaXv100, contents);
    ArtifactDescriptor artifactXv100Info = artifactStore.getArtifact(artifactXv100).getDescriptor();
    // artifact artifactX-1.1.0 contains plugin A1
    Id.Artifact artifactXv110 = Id.Artifact.from(Id.Namespace.DEFAULT, "artifactX", "1.1.0");
    ArtifactMeta metaXv110 = new ArtifactMeta(ArtifactClasses.builder().addPlugin(pluginA1).build(), ImmutableSet.of(parentArtifacts1));
    writeArtifact(artifactXv110, metaXv110, contents);
    ArtifactDescriptor artifactXv110Info = artifactStore.getArtifact(artifactXv110).getDescriptor();
    // artifact artifactX-2.0.0 contains plugins A1 and A2
    Id.Artifact artifactXv200 = Id.Artifact.from(Id.Namespace.DEFAULT, "artifactX", "2.0.0");
    ArtifactMeta metaXv200 = new ArtifactMeta(ArtifactClasses.builder().addPlugins(pluginA1, pluginA2).build(), ImmutableSet.of(parentArtifacts1));
    writeArtifact(artifactXv200, metaXv200, contents);
    ArtifactDescriptor artifactXv200Info = artifactStore.getArtifact(artifactXv200).getDescriptor();
    ArtifactRange parentArtifactsrange1 = new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "parent1", new ArtifactVersion("3.0.0"), new ArtifactVersion("5.0.0"));
    // artifact artifactZ-2.0.0 contains plugins A1, A2
    Id.Artifact artifactZv200 = Id.Artifact.from(Id.Namespace.DEFAULT, "artifactZ", "2.0.0");
    ArtifactMeta metaZv200 = new ArtifactMeta(ArtifactClasses.builder().addPlugins(pluginA1, pluginA2).build(), ImmutableSet.of(parentArtifactsrange1));
    writeArtifact(artifactZv200, metaZv200, contents);
    ArtifactDescriptor artifactZv200Info = artifactStore.getArtifact(artifactZv200).getDescriptor();
    // artifact written with this range should not come up as their parent range is out of the parent artifact range.
    ArtifactRange parentArtifactsOutOfRange1 = new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "parent1", new ArtifactVersion("5.0.0"), new ArtifactVersion("8.0.0"));
    // artifact artifactZ-2.0.0 contains plugins A1, A2, B1, and B2
    Id.Artifact artifactZv300 = Id.Artifact.from(Id.Namespace.DEFAULT, "artifactZ", "3.0.0");
    ArtifactMeta metaZv300 = new ArtifactMeta(ArtifactClasses.builder().addPlugins(pluginA1, pluginA2).build(), ImmutableSet.of(parentArtifactsOutOfRange1));
    writeArtifact(artifactZv300, metaZv300, contents);
    Map<ArtifactDescriptor, PluginClass> expectedMap = Maps.newHashMap();
    expectedMap.put(artifactXv100Info, pluginA1);
    expectedMap.put(artifactXv110Info, pluginA1);
    expectedMap.put(artifactXv200Info, pluginA1);
    expectedMap.put(artifactZv200Info, pluginA1);
    Assert.assertEquals(expectedMap, artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentArtifacts1, "A", "p1", null, Integer.MAX_VALUE, ArtifactSortOrder.UNORDERED));
    // test limited number
    Assert.assertEquals(ImmutableMap.of(artifactXv100Info, pluginA1), artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentArtifacts1, "A", "p1", null, 1, ArtifactSortOrder.UNORDERED));
    // test DESC order
    Assert.assertEquals(expectedMap, new TreeMap<>(artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentArtifacts1, "A", "p1", null, Integer.MAX_VALUE, ArtifactSortOrder.DESC)).descendingMap());
    ArtifactRange parentArtifactsSub1 = new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "parent1", new ArtifactVersion("1.1.0"), new ArtifactVersion("2.0.0"));
    expectedMap = Maps.newHashMap();
    expectedMap.put(artifactXv100Info, pluginA1);
    expectedMap.put(artifactXv110Info, pluginA1);
    expectedMap.put(artifactXv200Info, pluginA1);
    // artifactZv200Info wont be here, as the parent range 3.0.0-5.0.0 for artifactZv200 plugin
    // wont match the 1.2.1 parent artifact version
    Assert.assertEquals(expectedMap, artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentArtifactsSub1, "A", "p1", null, Integer.MAX_VALUE, ArtifactSortOrder.UNORDERED));
    expectedMap = Maps.newHashMap();
    expectedMap.put(artifactXv200Info, pluginA2);
    expectedMap.put(artifactZv200Info, pluginA2);
    Assert.assertEquals(expectedMap, artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentArtifacts1, "A", "p2", null, Integer.MAX_VALUE, ArtifactSortOrder.UNORDERED));
    ArtifactRange parentArtifactsSub2 = new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "parent1", new ArtifactVersion("5.0.0"), new ArtifactVersion("10.0.0"));
    try {
        artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentArtifactsSub2, "A", "p1", null, Integer.MAX_VALUE, ArtifactSortOrder.UNORDERED);
        Assert.fail("Get plugin class for invalid range should not retrun result");
    } catch (ArtifactNotFoundException e) {
    // no-op
    }
}
Also used : ArtifactRange(co.cask.cdap.api.artifact.ArtifactRange) TreeMap(java.util.TreeMap) PluginPropertyField(co.cask.cdap.api.plugin.PluginPropertyField) ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) ArtifactId(co.cask.cdap.proto.id.ArtifactId) Id(co.cask.cdap.common.id.Id) NamespaceId(co.cask.cdap.proto.id.NamespaceId) PluginClass(co.cask.cdap.api.plugin.PluginClass) ArtifactNotFoundException(co.cask.cdap.common.ArtifactNotFoundException) Test(org.junit.Test)

Example 29 with PluginClass

use of co.cask.cdap.api.plugin.PluginClass in project cdap by caskdata.

the class ArtifactStoreTest method testConcurrentSnapshotWrite.

@Category(SlowTests.class)
@Test
public void testConcurrentSnapshotWrite() throws Exception {
    // write parent
    Id.Artifact parentArtifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "1.0.0");
    ArtifactMeta parentMeta = new ArtifactMeta(ArtifactClasses.builder().build());
    writeArtifact(parentArtifactId, parentMeta, "content");
    final ArtifactRange parentArtifacts = new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "parent", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0"));
    // start up a bunch of threads that will try and write the same artifact at the same time
    // only one of them should be able to write it
    int numThreads = 20;
    final Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "abc", "1.0.0-SNAPSHOT");
    // use a barrier so they all try and write at the same time
    final CyclicBarrier barrier = new CyclicBarrier(numThreads);
    final CountDownLatch latch = new CountDownLatch(numThreads);
    ExecutorService executorService = Executors.newFixedThreadPool(numThreads);
    for (int i = 0; i < numThreads; i++) {
        final String writer = String.valueOf(i);
        executorService.execute(new Runnable() {

            @Override
            public void run() {
                try {
                    barrier.await();
                    ArtifactMeta meta = new ArtifactMeta(ArtifactClasses.builder().addPlugin(new PluginClass("plugin-type", "plugin" + writer, "", "classname", "cfg", ImmutableMap.<String, PluginPropertyField>of())).build(), ImmutableSet.of(parentArtifacts));
                    writeArtifact(artifactId, meta, writer);
                } catch (InterruptedException | BrokenBarrierException | ArtifactAlreadyExistsException | IOException e) {
                    // something went wrong, fail the test
                    throw new RuntimeException(e);
                } catch (WriteConflictException e) {
                // these are ok though unexpected (means couldn't write after a bunch of retries too)
                } finally {
                    latch.countDown();
                }
            }
        });
    }
    // wait for all writers to finish
    latch.await();
    // figure out which was the last writer by reading our data. all the writers should have been able to write,
    // and they should have all overwritten each other in a consistent manner
    ArtifactDetail detail = artifactStore.getArtifact(artifactId);
    // figure out the winning writer from the plugin name, which is 'plugin<writer>'
    String pluginName = detail.getMeta().getClasses().getPlugins().iterator().next().getName();
    String winnerWriter = pluginName.substring("plugin".length());
    ArtifactMeta expectedMeta = new ArtifactMeta(ArtifactClasses.builder().addPlugin(new PluginClass("plugin-type", "plugin" + winnerWriter, "", "classname", "cfg", ImmutableMap.<String, PluginPropertyField>of())).build(), ImmutableSet.of(parentArtifacts));
    assertEqual(artifactId, expectedMeta, winnerWriter, detail);
    // check only 1 plugin remains and that its the correct one
    Map<ArtifactDescriptor, Set<PluginClass>> pluginMap = artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentArtifactId, "plugin-type");
    Map<ArtifactDescriptor, Set<PluginClass>> expected = Maps.newHashMap();
    expected.put(detail.getDescriptor(), ImmutableSet.<PluginClass>of(new PluginClass("plugin-type", "plugin" + winnerWriter, "", "classname", "cfg", ImmutableMap.<String, PluginPropertyField>of())));
    Assert.assertEquals(expected, pluginMap);
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ArtifactRange(co.cask.cdap.api.artifact.ArtifactRange) CountDownLatch(java.util.concurrent.CountDownLatch) PluginPropertyField(co.cask.cdap.api.plugin.PluginPropertyField) CyclicBarrier(java.util.concurrent.CyclicBarrier) ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) ExecutorService(java.util.concurrent.ExecutorService) ArtifactId(co.cask.cdap.proto.id.ArtifactId) Id(co.cask.cdap.common.id.Id) NamespaceId(co.cask.cdap.proto.id.NamespaceId) PluginClass(co.cask.cdap.api.plugin.PluginClass) Category(org.junit.experimental.categories.Category) Test(org.junit.Test)

Example 30 with PluginClass

use of co.cask.cdap.api.plugin.PluginClass in project cdap by caskdata.

the class ArtifactStoreTest method testPluginParentVersions.

// this test tests that when an artifact specifies a range of artifact versions it extends,
// those versions are honored
@Test
public void testPluginParentVersions() throws Exception {
    // write an artifact that extends parent-[1.0.0, 2.0.0)
    Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "plugins", "0.1.0");
    Set<ArtifactRange> parentArtifacts = ImmutableSet.of(new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "parent", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0")));
    Set<PluginClass> plugins = ImmutableSet.of(new PluginClass("atype", "plugin1", "", "c.c.c.plugin1", "cfg", ImmutableMap.<String, PluginPropertyField>of()));
    ArtifactMeta meta = new ArtifactMeta(ArtifactClasses.builder().addPlugins(plugins).build(), parentArtifacts);
    writeArtifact(artifactId, meta, "some contents");
    ArtifactDescriptor artifactInfo = artifactStore.getArtifact(artifactId).getDescriptor();
    // check ids that are out of range. They should not return anything
    List<Id.Artifact> badIds = Lists.newArrayList(// ids that are too low
    Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "0.9.9"), Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "1.0.0-SNAPSHOT"), // ids that are too high
    Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "2.0.0"));
    ArtifactMeta emptyMeta = new ArtifactMeta(ArtifactClasses.builder().build());
    for (Id.Artifact badId : badIds) {
        // write the parent artifact to make sure we don't get ArtifactNotFound exceptions with later calls
        // we're testing range filtering, not the absence of the parent artifact
        writeArtifact(badId, emptyMeta, "content");
        Assert.assertTrue(artifactStore.getPluginClasses(NamespaceId.DEFAULT, badId).isEmpty());
        Assert.assertTrue(artifactStore.getPluginClasses(NamespaceId.DEFAULT, badId, "atype").isEmpty());
        try {
            artifactStore.getPluginClasses(NamespaceId.DEFAULT, badId, "atype", "plugin1", null, Integer.MAX_VALUE, ArtifactSortOrder.UNORDERED);
            Assert.fail();
        } catch (PluginNotExistsException e) {
        // expected
        }
    }
    // check ids that are in range return what we expect
    List<Id.Artifact> goodIds = Lists.newArrayList(// ids that are too low
    Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "1.0.0"), // ids that are too high
    Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "1.9.9"), Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "1.99.999"), Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "2.0.0-SNAPSHOT"));
    Map<ArtifactDescriptor, Set<PluginClass>> expectedPluginsMapList = ImmutableMap.of(artifactInfo, plugins);
    Map<ArtifactDescriptor, PluginClass> expectedPluginsMap = ImmutableMap.of(artifactInfo, plugins.iterator().next());
    for (Id.Artifact goodId : goodIds) {
        // make sure parent actually exists
        writeArtifact(goodId, emptyMeta, "content");
        Assert.assertEquals(expectedPluginsMapList, artifactStore.getPluginClasses(NamespaceId.DEFAULT, goodId));
        Assert.assertEquals(expectedPluginsMapList, artifactStore.getPluginClasses(NamespaceId.DEFAULT, goodId, "atype"));
        Assert.assertEquals(expectedPluginsMap, artifactStore.getPluginClasses(NamespaceId.DEFAULT, goodId, "atype", "plugin1", null, Integer.MAX_VALUE, ArtifactSortOrder.UNORDERED));
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ArtifactRange(co.cask.cdap.api.artifact.ArtifactRange) PluginPropertyField(co.cask.cdap.api.plugin.PluginPropertyField) PluginNotExistsException(co.cask.cdap.internal.app.runtime.plugin.PluginNotExistsException) ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) ArtifactId(co.cask.cdap.proto.id.ArtifactId) Id(co.cask.cdap.common.id.Id) NamespaceId(co.cask.cdap.proto.id.NamespaceId) PluginClass(co.cask.cdap.api.plugin.PluginClass) Test(org.junit.Test)

Aggregations

PluginClass (co.cask.cdap.api.plugin.PluginClass)76 PluginPropertyField (co.cask.cdap.api.plugin.PluginPropertyField)46 HashMap (java.util.HashMap)37 Test (org.junit.Test)24 ArtifactId (co.cask.cdap.proto.id.ArtifactId)22 NamespaceId (co.cask.cdap.proto.id.NamespaceId)21 ArtifactRange (co.cask.cdap.api.artifact.ArtifactRange)19 ArtifactVersion (co.cask.cdap.api.artifact.ArtifactVersion)17 Id (co.cask.cdap.common.id.Id)17 Set (java.util.Set)14 ImmutableSet (com.google.common.collect.ImmutableSet)13 PluginNotExistsException (co.cask.cdap.internal.app.runtime.plugin.PluginNotExistsException)11 Map (java.util.Map)11 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)10 SortedMap (java.util.SortedMap)10 ArtifactId (co.cask.cdap.api.artifact.ArtifactId)9 File (java.io.File)9 ApplicationClass (co.cask.cdap.api.artifact.ApplicationClass)8 HashSet (java.util.HashSet)8 ImmutableMap (com.google.common.collect.ImmutableMap)7