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);
}
}
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);
}
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());
}
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
}
}
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);
}
}
Aggregations