use of io.cdap.cdap.api.artifact.ArtifactVersion in project cdap by caskdata.
the class ArtifactStoreTest method testDelete.
@Test
public void testDelete() throws Exception {
// write an artifact with an app
Id.Artifact parentId = Id.Artifact.from(Id.Namespace.DEFAULT, "parent", "1.0.0");
ApplicationClass appClass = new ApplicationClass(InspectionApp.class.getName(), "", new ReflectionSchemaGenerator().generate(InspectionApp.AConfig.class));
ArtifactMeta artifactMeta = new ArtifactMeta(ArtifactClasses.builder().addApp(appClass).build());
writeArtifact(parentId, artifactMeta, "parent contents");
// write a child artifact that extends the parent with some plugins
Id.Artifact childId = Id.Artifact.from(Id.Namespace.DEFAULT, "myplugins", "1.0.0");
Id.Artifact anotherId = Id.Artifact.from(Id.Namespace.SYSTEM, "myplugins", "1.0.0");
List<PluginClass> plugins = ImmutableList.of(PluginClass.builder().setName("plugin1").setType("atype").setDescription("").setClassName("c.c.c.plugin1").setConfigFieldName("cfg").setProperties(ImmutableMap.of()).build(), PluginClass.builder().setName("plugin2").setType("atype").setDescription("").setClassName("c.c.c.plugin2").setConfigFieldName("cfg").setProperties(ImmutableMap.of()).build());
Set<ArtifactRange> parents = ImmutableSet.of(new ArtifactRange(parentId.getNamespace().getId(), parentId.getName(), new ArtifactVersion("0.1.0"), new ArtifactVersion("2.0.0")));
artifactMeta = new ArtifactMeta(ArtifactClasses.builder().addPlugins(plugins).build(), parents);
writeArtifact(childId, artifactMeta, "child contents");
writeArtifact(anotherId, artifactMeta, "child contents");
// check parent has plugins from the child
Assert.assertFalse(artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentId).isEmpty());
// delete the child artifact
artifactStore.delete(childId);
// check that the other artifact is not getting deleted and the plugin classes are not getting deleted
artifactStore.getArtifact(anotherId);
Assert.assertFalse(artifactStore.getPluginClasses(NamespaceId.SYSTEM, parentId).isEmpty());
// shouldn't be able to get artifact detail
try {
artifactStore.getArtifact(childId);
Assert.fail();
} catch (ArtifactNotFoundException e) {
// expected
}
// shouldn't see it in the list
List<ArtifactDetail> artifactList = artifactStore.getArtifacts(parentId.getNamespace().toEntityId());
Assert.assertEquals(1, artifactList.size());
Assert.assertEquals(parentId.getName(), artifactList.get(0).getDescriptor().getArtifactId().getName());
// delete the one in system scope
artifactStore.delete(anotherId);
// shouldn't be able to get artifact detail
try {
artifactStore.getArtifact(anotherId);
Assert.fail();
} catch (ArtifactNotFoundException e) {
// expected
}
Assert.assertTrue(artifactStore.getPluginClasses(NamespaceId.SYSTEM, parentId).isEmpty());
// shouldn't see any more plugins for parent
Assert.assertTrue(artifactStore.getPluginClasses(NamespaceId.DEFAULT, parentId).isEmpty());
// delete parent
artifactStore.delete(parentId);
// nothing should be in the list
Assert.assertTrue(artifactStore.getArtifacts(parentId.getNamespace().toEntityId()).isEmpty());
// shouldn't be able to see app class either
Assert.assertTrue(artifactStore.getApplicationClasses(NamespaceId.DEFAULT, appClass.getClassName()).isEmpty());
}
use of io.cdap.cdap.api.artifact.ArtifactVersion 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(PluginClass.builder().setName("plugin" + writer).setType("plugin-type").setDescription("").setClassName("classname").setConfigFieldName("cfg").setProperties(ImmutableMap.of()).build()).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(PluginClass.builder().setName("plugin" + winnerWriter).setType("plugin-type").setDescription("").setClassName("classname").setConfigFieldName("cfg").setProperties(ImmutableMap.of()).build()).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(PluginClass.builder().setName("plugin" + winnerWriter).setType("plugin-type").setDescription("").setClassName("classname").setConfigFieldName("cfg").setProperties(ImmutableMap.of()).build()));
Assert.assertEquals(expected, pluginMap);
}
use of io.cdap.cdap.api.artifact.ArtifactVersion 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 = PluginClass.builder().setName("p1").setType("A").setDescription("desc").setClassName("c.p1").setConfigFieldName("cfg").setProperties(ImmutableMap.of("threshold", new PluginPropertyField("thresh", "description", "double", true, false), "retry", new PluginPropertyField("retries", "description", "int", false, false))).build();
PluginClass pluginA2 = PluginClass.builder().setName("p2").setType("A").setDescription("desc").setClassName("c.p2").setConfigFieldName("conf").setProperties(ImmutableMap.of("stream", new PluginPropertyField("stream", "description", "string", true, false))).build();
// 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
}
}
use of io.cdap.cdap.api.artifact.ArtifactVersion 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 = PluginClass.builder().setName("plugin1").setType("atype").setDescription("").setClassName("c.c.c.plugin1").setConfigFieldName("cfg").setProperties(ImmutableMap.of()).build();
PluginClass plugin2 = PluginClass.builder().setName("plugin2").setType("atype").setDescription("").setClassName("c.c.c.plugin2").setConfigFieldName("cfg").setProperties(ImmutableMap.of()).build();
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 io.cdap.cdap.api.artifact.ArtifactVersion 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(PluginClass.builder().setName("plugin1").setType("typeA").setDescription("").setClassName("c.c.c.plugin1").setConfigFieldName("cfg").setProperties(ImmutableMap.of()).build());
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(PluginClass.builder().setName("plugin2").setType("typeA").setDescription("").setClassName("c.c.c.plugin2").setConfigFieldName("cfg").setProperties(ImmutableMap.of()).build());
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(PluginClass.builder().setName("plugin3").setType("typeA").setDescription("").setClassName("c.c.c.plugin3").setConfigFieldName("cfg").setProperties(ImmutableMap.of()).build());
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(PluginClass.builder().setName("plugin4").setType("typeA").setDescription("").setClassName("c.c.c.plugin4").setConfigFieldName("cfg").setProperties(ImmutableMap.of()).build());
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());
}
Aggregations