Search in sources :

Example 41 with ArtifactRange

use of co.cask.cdap.api.artifact.ArtifactRange in project cdap by caskdata.

the class ArtifactStore method deleteMeta.

private void deleteMeta(Table table, Id.Artifact artifactId, byte[] oldData) throws IOException {
    // delete old artifact data
    ArtifactCell artifactCell = new ArtifactCell(artifactId);
    table.delete(artifactCell.rowkey, artifactCell.column);
    // delete old plugins
    final ArtifactData oldMeta = GSON.fromJson(Bytes.toString(oldData), ArtifactData.class);
    byte[] artifactColumn = new ArtifactColumn(artifactId).getColumn();
    for (PluginClass pluginClass : oldMeta.meta.getClasses().getPlugins()) {
        // delete metadata for each artifact this plugin extends
        for (ArtifactRange artifactRange : oldMeta.meta.getUsableBy()) {
            // p:{namespace}:{type}:{name}
            PluginKey pluginKey = new PluginKey(artifactRange.getNamespace(), artifactRange.getName(), pluginClass.getType(), pluginClass.getName());
            table.delete(pluginKey.getRowKey(), artifactColumn);
        }
    }
    // delete old appclass metadata
    for (ApplicationClass appClass : oldMeta.meta.getClasses().getApps()) {
        AppClassKey appClassKey = new AppClassKey(artifactId.getNamespace().toEntityId(), appClass.getClassName());
        table.delete(appClassKey.getRowKey(), artifactColumn);
    }
    try {
        new EntityImpersonator(artifactId.toEntityId(), impersonator).impersonate(new Callable<Void>() {

            @Override
            public Void call() throws Exception {
                Locations.getLocationFromAbsolutePath(locationFactory, oldMeta.getLocationPath()).delete();
                return null;
            }
        });
    } catch (IOException ioe) {
        throw ioe;
    } catch (Exception e) {
        // this should not happen
        throw Throwables.propagate(e);
    }
}
Also used : EntityImpersonator(co.cask.cdap.security.impersonation.EntityImpersonator) ArtifactRange(co.cask.cdap.api.artifact.ArtifactRange) ApplicationClass(co.cask.cdap.api.artifact.ApplicationClass) IOException(java.io.IOException) TransactionFailureException(org.apache.tephra.TransactionFailureException) ArtifactNotFoundException(co.cask.cdap.common.ArtifactNotFoundException) ArtifactAlreadyExistsException(co.cask.cdap.common.ArtifactAlreadyExistsException) TransactionConflictException(org.apache.tephra.TransactionConflictException) PluginNotExistsException(co.cask.cdap.internal.app.runtime.plugin.PluginNotExistsException) ArtifactRangeNotFoundException(co.cask.cdap.common.ArtifactRangeNotFoundException) DatasetManagementException(co.cask.cdap.api.dataset.DatasetManagementException) IOException(java.io.IOException) PluginClass(co.cask.cdap.api.plugin.PluginClass)

Example 42 with ArtifactRange

use of co.cask.cdap.api.artifact.ArtifactRange in project cdap by caskdata.

the class ArtifactStoreTest method testSamePluginDifferentArtifacts.

@Test
public void testSamePluginDifferentArtifacts() throws Exception {
    ArtifactRange parentArtifacts = new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "parent", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0"));
    // add one artifact with a couple plugins
    Id.Artifact artifact1 = Id.Artifact.from(Id.Namespace.DEFAULT, "plugins1", "1.0.0");
    Set<PluginClass> plugins = ImmutableSet.of(new PluginClass("atype", "plugin1", "", "c.c.c.plugin1", "cfg", ImmutableMap.<String, PluginPropertyField>of()), new PluginClass("atype", "plugin2", "", "c.c.c.plugin2", "cfg", ImmutableMap.<String, PluginPropertyField>of()));
    ArtifactMeta meta1 = new ArtifactMeta(ArtifactClasses.builder().addPlugins(plugins).build(), ImmutableSet.of(parentArtifacts));
    writeArtifact(artifact1, meta1, "something");
    ArtifactDescriptor artifact1Info = artifactStore.getArtifact(artifact1).getDescriptor();
    // add a different artifact with the same plugins
    Id.Artifact artifact2 = Id.Artifact.from(Id.Namespace.DEFAULT, "plugins2", "1.0.0");
    ArtifactMeta meta2 = new ArtifactMeta(ArtifactClasses.builder().addPlugins(plugins).build(), ImmutableSet.of(parentArtifacts));
    writeArtifact(artifact2, meta2, "something");
    ArtifactDescriptor artifact2Info = artifactStore.getArtifact(artifact2).getDescriptor();
    Id.Artifact parentArtifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "1.0.0");
    writeArtifact(parentArtifactId, new ArtifactMeta(ArtifactClasses.builder().build()), "content");
    Map<ArtifactDescriptor, Set<PluginClass>> expected = Maps.newHashMap();
    expected.put(artifact1Info, plugins);
    expected.put(artifact2Info, plugins);
    Map<ArtifactDescriptor, Set<PluginClass>> actual = artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentArtifactId);
    Assert.assertEquals(expected, actual);
}
Also used : ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ArtifactRange(co.cask.cdap.api.artifact.ArtifactRange) Id(co.cask.cdap.proto.Id) ArtifactId(co.cask.cdap.proto.id.ArtifactId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) PluginClass(co.cask.cdap.api.plugin.PluginClass) PluginPropertyField(co.cask.cdap.api.plugin.PluginPropertyField) Test(org.junit.Test)

Example 43 with ArtifactRange

use of co.cask.cdap.api.artifact.ArtifactRange in project cdap by caskdata.

the class ArtifactStoreTest method testPluginParentInclusiveExclusiveVersions.

@Test
public void testPluginParentInclusiveExclusiveVersions() throws Exception {
    // write artifacts that extend:
    // parent-[1.0.0,1.0.0] -- only visible by parent-1.0.0
    Id.Artifact id1 = Id.Artifact.from(Id.Namespace.DEFAULT, "plugins", "0.0.1");
    Set<ArtifactRange> parentArtifacts = ImmutableSet.of(new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "parent", new ArtifactVersion("1.0.0"), true, new ArtifactVersion("1.0.0"), true));
    List<PluginClass> plugins = ImmutableList.of(new PluginClass("typeA", "plugin1", "", "c.c.c.plugin1", "cfg", ImmutableMap.<String, PluginPropertyField>of()));
    ArtifactMeta meta = new ArtifactMeta(ArtifactClasses.builder().addPlugins(plugins).build(), parentArtifacts);
    writeArtifact(id1, meta, "some contents");
    // parent-[2.0.0,2.0.1) -- only visible by parent-2.0.0
    Id.Artifact id2 = Id.Artifact.from(Id.Namespace.DEFAULT, "plugins", "0.0.2");
    parentArtifacts = ImmutableSet.of(new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "parent", new ArtifactVersion("2.0.0"), true, new ArtifactVersion("2.0.1"), false));
    plugins = ImmutableList.of(new PluginClass("typeA", "plugin2", "", "c.c.c.plugin2", "cfg", ImmutableMap.<String, PluginPropertyField>of()));
    meta = new ArtifactMeta(ArtifactClasses.builder().addPlugins(plugins).build(), parentArtifacts);
    writeArtifact(id2, meta, "some contents");
    // parent-(3.0.0,3.0.1] -- only visible by parent-3.0.1
    Id.Artifact id3 = Id.Artifact.from(Id.Namespace.DEFAULT, "plugins", "0.0.3");
    parentArtifacts = ImmutableSet.of(new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "parent", new ArtifactVersion("3.0.0"), false, new ArtifactVersion("3.0.1"), true));
    plugins = ImmutableList.of(new PluginClass("typeA", "plugin3", "", "c.c.c.plugin3", "cfg", ImmutableMap.<String, PluginPropertyField>of()));
    meta = new ArtifactMeta(ArtifactClasses.builder().addPlugins(plugins).build(), parentArtifacts);
    writeArtifact(id3, meta, "some contents");
    // parent-(4.0.0,4.0.2) -- only visible by parent-4.0.1
    Id.Artifact id4 = Id.Artifact.from(Id.Namespace.DEFAULT, "plugins", "0.0.4");
    parentArtifacts = ImmutableSet.of(new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "parent", new ArtifactVersion("4.0.0"), false, new ArtifactVersion("4.0.2"), false));
    plugins = ImmutableList.of(new PluginClass("typeA", "plugin4", "", "c.c.c.plugin4", "cfg", ImmutableMap.<String, PluginPropertyField>of()));
    meta = new ArtifactMeta(ArtifactClasses.builder().addPlugins(plugins).build(), parentArtifacts);
    writeArtifact(id4, meta, "some contents");
    ArtifactMeta parentMeta = new ArtifactMeta(ArtifactClasses.builder().build());
    // check parent-1.0.0 has plugin1 but parent-0.0.9 does not and 1.0.1 does not
    Id.Artifact parentId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "0.0.9");
    writeArtifact(parentId, parentMeta, "content");
    Assert.assertTrue(artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentId).isEmpty());
    parentId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "1.0.1");
    writeArtifact(parentId, parentMeta, "content");
    Assert.assertTrue(artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentId).isEmpty());
    parentId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "1.0.0");
    writeArtifact(parentId, parentMeta, "content");
    Assert.assertEquals(1, artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentId).size());
    // check parent-2.0.0 has plugin2 but parent-1.9.9 does not and 2.0.1 does not
    parentId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "1.9.9");
    writeArtifact(parentId, parentMeta, "content");
    Assert.assertTrue(artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentId).isEmpty());
    parentId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "2.0.1");
    writeArtifact(parentId, parentMeta, "content");
    Assert.assertTrue(artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentId).isEmpty());
    parentId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "2.0.0");
    writeArtifact(parentId, parentMeta, "content");
    Assert.assertEquals(1, artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentId).size());
    // check parent-3.0.1 has plugin3 but parent-3.0.0 does not and 3.0.2 does not
    parentId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "3.0.0");
    writeArtifact(parentId, parentMeta, "content");
    Assert.assertTrue(artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentId).isEmpty());
    parentId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "3.0.2");
    writeArtifact(parentId, parentMeta, "content");
    Assert.assertTrue(artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentId).isEmpty());
    parentId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "3.0.1");
    writeArtifact(parentId, parentMeta, "content");
    Assert.assertEquals(1, artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentId).size());
    // check parent-4.0.1 has plugin4 but parent-4.0.0 does not and 4.0.2 does not
    parentId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "4.0.0");
    writeArtifact(parentId, parentMeta, "content");
    Assert.assertTrue(artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentId).isEmpty());
    parentId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "4.0.2");
    writeArtifact(parentId, parentMeta, "content");
    Assert.assertTrue(artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentId).isEmpty());
    parentId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "4.0.1");
    writeArtifact(parentId, parentMeta, "content");
    Assert.assertEquals(1, artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentId).size());
}
Also used : ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) ArtifactRange(co.cask.cdap.api.artifact.ArtifactRange) Id(co.cask.cdap.proto.Id) ArtifactId(co.cask.cdap.proto.id.ArtifactId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) PluginClass(co.cask.cdap.api.plugin.PluginClass) PluginPropertyField(co.cask.cdap.api.plugin.PluginPropertyField) Test(org.junit.Test)

Example 44 with ArtifactRange

use of co.cask.cdap.api.artifact.ArtifactRange in project cdap by caskdata.

the class ArtifactStoreTest method testSnapshotMutability.

@Test
public void testSnapshotMutability() 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");
    ArtifactRange parentArtifacts = new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "parent", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0"));
    // write the snapshot once
    PluginClass plugin1 = new PluginClass("atype", "plugin1", "", "c.c.c.plugin1", "cfg", ImmutableMap.<String, PluginPropertyField>of());
    PluginClass plugin2 = new PluginClass("atype", "plugin2", "", "c.c.c.plugin2", "cfg", ImmutableMap.<String, PluginPropertyField>of());
    Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "myplugins", "1.0.0-SNAPSHOT");
    ArtifactMeta artifactMeta = new ArtifactMeta(ArtifactClasses.builder().addPlugins(plugin1, plugin2).build(), ImmutableSet.of(parentArtifacts));
    writeArtifact(artifactId, artifactMeta, "abc123");
    // update meta and jar contents
    artifactMeta = new ArtifactMeta(ArtifactClasses.builder().addPlugin(plugin2).build(), ImmutableSet.of(parentArtifacts));
    writeArtifact(artifactId, artifactMeta, "xyz321");
    // check the metadata and contents got updated
    ArtifactDetail detail = artifactStore.getArtifact(artifactId);
    assertEqual(artifactId, artifactMeta, "xyz321", detail);
    // check that plugin1 was deleted and plugin2 remains
    Assert.assertEquals(ImmutableMap.of(detail.getDescriptor(), plugin2), artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentArtifactId, plugin2.getType(), plugin2.getName(), null, Integer.MAX_VALUE, ArtifactSortOrder.UNORDERED));
    try {
        artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentArtifactId, plugin1.getType(), plugin1.getName(), null, Integer.MAX_VALUE, ArtifactSortOrder.UNORDERED);
        Assert.fail();
    } catch (PluginNotExistsException e) {
    // expected
    }
}
Also used : PluginNotExistsException(co.cask.cdap.internal.app.runtime.plugin.PluginNotExistsException) ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) ArtifactRange(co.cask.cdap.api.artifact.ArtifactRange) Id(co.cask.cdap.proto.Id) ArtifactId(co.cask.cdap.proto.id.ArtifactId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) PluginClass(co.cask.cdap.api.plugin.PluginClass) Test(org.junit.Test)

Example 45 with ArtifactRange

use of co.cask.cdap.api.artifact.ArtifactRange in project cdap by caskdata.

the class ArtifactStoreTest method testPluginNamespaceIsolation.

@Test
public void testPluginNamespaceIsolation() throws Exception {
    // write some system artifact
    Id.Artifact systemAppArtifact = Id.Artifact.from(Id.Namespace.SYSTEM, "app", "1.0.0");
    ArtifactMeta systemAppMeta = new ArtifactMeta(ArtifactClasses.builder().addApp(new ApplicationClass("co.cask.class", "desc", null)).build());
    writeArtifact(systemAppArtifact, systemAppMeta, "app contents");
    Set<ArtifactRange> usableBy = ImmutableSet.of(new ArtifactRange(systemAppArtifact.getNamespace().getId(), systemAppArtifact.getName(), systemAppArtifact.getVersion(), true, systemAppArtifact.getVersion(), true));
    PluginClass plugin = new PluginClass("atype", "plugin1", "", "c.c.c.plugin1", "cfg", ImmutableMap.<String, PluginPropertyField>of());
    // write a plugins artifact in namespace1
    NamespaceId namespace1 = Ids.namespace("ns1");
    Id.Artifact artifact1 = Id.Artifact.from(namespace1.toId(), "plugins1", "1.0.0");
    ArtifactMeta meta1 = new ArtifactMeta(ArtifactClasses.builder().addPlugin(plugin).build(), usableBy);
    String contents1 = "plugin1 contents";
    writeArtifact(artifact1, meta1, contents1);
    // write a plugins artifact in namespace2
    NamespaceId namespace2 = Ids.namespace("ns2");
    Id.Artifact artifact2 = Id.Artifact.from(namespace2.toId(), "plugins2", "1.0.0");
    ArtifactMeta meta2 = new ArtifactMeta(ArtifactClasses.builder().addPlugin(plugin).build(), usableBy);
    String contents2 = "plugin2 contents";
    writeArtifact(artifact2, meta2, contents2);
    try {
        // this should only get plugins from artifact1
        SortedMap<ArtifactDescriptor, Set<PluginClass>> plugins = artifactStore.getPluginClasses(namespace1, systemAppArtifact);
        Assert.assertEquals(1, plugins.size());
        ArtifactDescriptor artifactDescriptor = plugins.firstKey();
        Assert.assertEquals(artifact1.toArtifactId(), artifactDescriptor.getArtifactId());
        assertContentsEqual(contents1, artifactDescriptor.getLocation());
        // this should only get plugins from artifact2
        plugins = artifactStore.getPluginClasses(namespace2, systemAppArtifact);
        Assert.assertEquals(1, plugins.size());
        artifactDescriptor = plugins.firstKey();
        Assert.assertEquals(artifact2.toArtifactId(), artifactDescriptor.getArtifactId());
        assertContentsEqual(contents2, artifactDescriptor.getLocation());
    } finally {
        artifactStore.clear(namespace1);
        artifactStore.clear(namespace2);
        artifactStore.clear(NamespaceId.SYSTEM);
    }
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ArtifactRange(co.cask.cdap.api.artifact.ArtifactRange) ApplicationClass(co.cask.cdap.api.artifact.ApplicationClass) Id(co.cask.cdap.proto.Id) ArtifactId(co.cask.cdap.proto.id.ArtifactId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) NamespaceId(co.cask.cdap.proto.id.NamespaceId) PluginClass(co.cask.cdap.api.plugin.PluginClass) Test(org.junit.Test)

Aggregations

ArtifactRange (co.cask.cdap.api.artifact.ArtifactRange)46 ArtifactVersion (co.cask.cdap.api.artifact.ArtifactVersion)33 Test (org.junit.Test)31 NamespaceId (co.cask.cdap.proto.id.NamespaceId)28 Id (co.cask.cdap.proto.Id)24 PluginClass (co.cask.cdap.api.plugin.PluginClass)17 PluginPropertyField (co.cask.cdap.api.plugin.PluginPropertyField)13 ArtifactId (co.cask.cdap.proto.id.ArtifactId)13 File (java.io.File)11 Manifest (java.util.jar.Manifest)11 HashSet (java.util.HashSet)9 ArtifactNotFoundException (co.cask.cdap.common.ArtifactNotFoundException)8 ArtifactId (co.cask.cdap.api.artifact.ArtifactId)7 ImmutableSet (com.google.common.collect.ImmutableSet)7 Set (java.util.Set)7 ArtifactSummary (co.cask.cdap.api.artifact.ArtifactSummary)6 ApplicationClass (co.cask.cdap.api.artifact.ApplicationClass)5 PluginNotExistsException (co.cask.cdap.internal.app.runtime.plugin.PluginNotExistsException)5 ArtifactInfo (co.cask.cdap.api.artifact.ArtifactInfo)4 IOException (java.io.IOException)4