Search in sources :

Example 11 with Requirements

use of io.cdap.cdap.api.plugin.Requirements in project cdap by cdapio.

the class ProvisioningService method getUnfulfilledRequirements.

/**
 * Checks if the given provisioner fulfills all the requirements of a program.
 *
 * @param provisionerFulfilledRequirements {@link Requirements} containing requirements fulfilled by the provisioner
 * @param requirements a {@link Set} of {@link PluginRequirement}
 *
 * @return {@link Set} {@link PluginRequirement} which consists of plugin identifier and the requirements which
 * are not meet
 */
@VisibleForTesting
Set<PluginRequirement> getUnfulfilledRequirements(Capabilities provisionerFulfilledRequirements, Set<PluginRequirement> requirements) {
    // create a map of plugin name to unfulfilled requirement if there are any
    Set<PluginRequirement> unfulfilledRequirements = new HashSet<>();
    Set<String> capabilities = provisionerFulfilledRequirements.getDatasetTypes().stream().map(String::toLowerCase).collect(Collectors.toSet());
    for (PluginRequirement pluginRequirement : requirements) {
        Requirements requisites = pluginRequirement.getRequirements();
        Sets.SetView<String> unfulfilledRequirement = Sets.difference(requisites.getDatasetTypes(), capabilities);
        if (!unfulfilledRequirement.isEmpty()) {
            unfulfilledRequirements.add(new PluginRequirement(pluginRequirement.getName(), pluginRequirement.getType(), new Requirements(unfulfilledRequirement.immutableCopy())));
        }
    }
    return unfulfilledRequirements;
}
Also used : Sets(com.google.common.collect.Sets) PluginRequirement(io.cdap.cdap.internal.pipeline.PluginRequirement) Requirements(io.cdap.cdap.api.plugin.Requirements) HashSet(java.util.HashSet) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 12 with Requirements

use of io.cdap.cdap.api.plugin.Requirements in project cdap by cdapio.

the class ApplicationClassCodec method deserialize.

@Override
public ApplicationClass deserialize(JsonElement json, Type typeOfT, JsonDeserializationContext context) throws JsonParseException {
    JsonObject jsonObject = json.getAsJsonObject();
    Schema configSchema = getConfigSchema(jsonObject, context);
    Requirements requirements = getRequirements(jsonObject, context);
    return new ApplicationClass(getStringValue(jsonObject, CLASS_NAME), getStringValue(jsonObject, DESCRIPTION), configSchema, requirements);
}
Also used : Schema(io.cdap.cdap.api.data.schema.Schema) JsonObject(com.google.gson.JsonObject) ApplicationClass(io.cdap.cdap.api.artifact.ApplicationClass) Requirements(io.cdap.cdap.api.plugin.Requirements)

Example 13 with Requirements

use of io.cdap.cdap.api.plugin.Requirements in project cdap by cdapio.

the class ArtifactStoreTest method testExcludedPlugins.

@Test
public void testExcludedPlugins() throws Exception {
    ArtifactRange parentArtifacts = new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "parent", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0"));
    // does not have any requirement
    PluginClass includedPlugin1 = PluginClass.builder().setName("includedPlugin1").setType("A").setDescription("desc").setClassName("c.p2").setConfigFieldName("conf").setProperties(ImmutableMap.of("stream", new PluginPropertyField("stream", "description", "string", true, false))).build();
    PluginClass excludedPlugin1 = PluginClass.builder().setName("excludedPlugin1").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))).setRequirements(new Requirements(ImmutableSet.of(Table.TYPE))).build();
    PluginClass excludedPlugin2 = PluginClass.builder().setName("excludedPlugin2").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))).setRequirements(new Requirements(ImmutableSet.of(KeyValueTable.TYPE))).build();
    PluginClass excludedPlugin3 = PluginClass.builder().setName("excludedPlugin3").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))).setRequirements(new Requirements(ImmutableSet.of(Table.TYPE, KeyValueTable.TYPE))).build();
    PluginClass excludedPlugin4 = PluginClass.builder().setName("excludedPlugin4").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))).setRequirements(new Requirements(ImmutableSet.of(Table.TYPE, KeyValueTable.TYPE, Cube.TYPE))).build();
    PluginClass excludedPlugin5 = PluginClass.builder().setName("excludedPlugin5").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))).setRequirements(new Requirements(ImmutableSet.of(Table.TYPE, Cube.TYPE))).build();
    PluginClass includedPlugin2 = PluginClass.builder().setName("includedPlugin2").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))).setRequirements(new Requirements(ImmutableSet.of("noTransactionNeeded"))).build();
    PluginClass includedPlugin3 = PluginClass.builder().setName("includedPlugin3").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))).setRequirements(new Requirements(ImmutableSet.of("noTransactionNeeded", "tpfs"))).build();
    Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "ArtifactWithTransactionalPlugins", "1.0.0");
    ArtifactMeta artifactMeta = new ArtifactMeta(ArtifactClasses.builder().addPlugins(includedPlugin1, includedPlugin2, includedPlugin3, excludedPlugin1, excludedPlugin2, excludedPlugin3, excludedPlugin4, excludedPlugin5).build(), ImmutableSet.of(parentArtifacts));
    writeArtifact(artifactId, artifactMeta, "no-content");
    ArtifactDescriptor artifactInfo = artifactStore.getArtifact(artifactId).getDescriptor();
    // plugins which have transaction or spark as requirement should be excluded from plugins listed for the artifact
    Map<ArtifactDescriptor, Set<PluginClass>> actual = artifactStore.getPluginClasses(NamespaceId.DEFAULT, artifactId);
    Set<PluginClass> expectedPlugins = ImmutableSet.of(includedPlugin1, includedPlugin2, includedPlugin3);
    Assert.assertEquals(ImmutableMap.of(artifactInfo, expectedPlugins), actual);
    // excluded plugins should also not be in the plugins listed for the namespace
    List<ArtifactDetail> actualArtifacts = artifactStore.getArtifacts(NamespaceId.DEFAULT);
    Assert.assertEquals(1, actualArtifacts.size());
    Assert.assertEquals(expectedPlugins, actualArtifacts.get(0).getMeta().getClasses().getPlugins());
    // excluded plugins should also not be in the plugins listed for the artifact id
    ArtifactDetail actualArtifact = artifactStore.getArtifact(artifactId);
    Assert.assertEquals(expectedPlugins, actualArtifact.getMeta().getClasses().getPlugins());
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) PluginPropertyField(io.cdap.cdap.api.plugin.PluginPropertyField) Requirements(io.cdap.cdap.api.plugin.Requirements) ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Id(io.cdap.cdap.common.id.Id) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) PluginClass(io.cdap.cdap.api.plugin.PluginClass) Test(org.junit.Test)

Example 14 with Requirements

use of io.cdap.cdap.api.plugin.Requirements in project cdap by caskdata.

the class ArtifactStoreTest method testExcludedPlugins.

@Test
public void testExcludedPlugins() throws Exception {
    ArtifactRange parentArtifacts = new ArtifactRange(NamespaceId.DEFAULT.getNamespace(), "parent", new ArtifactVersion("1.0.0"), new ArtifactVersion("2.0.0"));
    // does not have any requirement
    PluginClass includedPlugin1 = PluginClass.builder().setName("includedPlugin1").setType("A").setDescription("desc").setClassName("c.p2").setConfigFieldName("conf").setProperties(ImmutableMap.of("stream", new PluginPropertyField("stream", "description", "string", true, false))).build();
    PluginClass excludedPlugin1 = PluginClass.builder().setName("excludedPlugin1").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))).setRequirements(new Requirements(ImmutableSet.of(Table.TYPE))).build();
    PluginClass excludedPlugin2 = PluginClass.builder().setName("excludedPlugin2").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))).setRequirements(new Requirements(ImmutableSet.of(KeyValueTable.TYPE))).build();
    PluginClass excludedPlugin3 = PluginClass.builder().setName("excludedPlugin3").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))).setRequirements(new Requirements(ImmutableSet.of(Table.TYPE, KeyValueTable.TYPE))).build();
    PluginClass excludedPlugin4 = PluginClass.builder().setName("excludedPlugin4").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))).setRequirements(new Requirements(ImmutableSet.of(Table.TYPE, KeyValueTable.TYPE, Cube.TYPE))).build();
    PluginClass excludedPlugin5 = PluginClass.builder().setName("excludedPlugin5").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))).setRequirements(new Requirements(ImmutableSet.of(Table.TYPE, Cube.TYPE))).build();
    PluginClass includedPlugin2 = PluginClass.builder().setName("includedPlugin2").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))).setRequirements(new Requirements(ImmutableSet.of("noTransactionNeeded"))).build();
    PluginClass includedPlugin3 = PluginClass.builder().setName("includedPlugin3").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))).setRequirements(new Requirements(ImmutableSet.of("noTransactionNeeded", "tpfs"))).build();
    Id.Artifact artifactId = Id.Artifact.from(Id.Namespace.DEFAULT, "ArtifactWithTransactionalPlugins", "1.0.0");
    ArtifactMeta artifactMeta = new ArtifactMeta(ArtifactClasses.builder().addPlugins(includedPlugin1, includedPlugin2, includedPlugin3, excludedPlugin1, excludedPlugin2, excludedPlugin3, excludedPlugin4, excludedPlugin5).build(), ImmutableSet.of(parentArtifacts));
    writeArtifact(artifactId, artifactMeta, "no-content");
    ArtifactDescriptor artifactInfo = artifactStore.getArtifact(artifactId).getDescriptor();
    // plugins which have transaction or spark as requirement should be excluded from plugins listed for the artifact
    Map<ArtifactDescriptor, Set<PluginClass>> actual = artifactStore.getPluginClasses(NamespaceId.DEFAULT, artifactId);
    Set<PluginClass> expectedPlugins = ImmutableSet.of(includedPlugin1, includedPlugin2, includedPlugin3);
    Assert.assertEquals(ImmutableMap.of(artifactInfo, expectedPlugins), actual);
    // excluded plugins should also not be in the plugins listed for the namespace
    List<ArtifactDetail> actualArtifacts = artifactStore.getArtifacts(NamespaceId.DEFAULT);
    Assert.assertEquals(1, actualArtifacts.size());
    Assert.assertEquals(expectedPlugins, actualArtifacts.get(0).getMeta().getClasses().getPlugins());
    // excluded plugins should also not be in the plugins listed for the artifact id
    ArtifactDetail actualArtifact = artifactStore.getArtifact(artifactId);
    Assert.assertEquals(expectedPlugins, actualArtifact.getMeta().getClasses().getPlugins());
}
Also used : ImmutableSet(com.google.common.collect.ImmutableSet) Set(java.util.Set) ArtifactRange(io.cdap.cdap.api.artifact.ArtifactRange) PluginPropertyField(io.cdap.cdap.api.plugin.PluginPropertyField) Requirements(io.cdap.cdap.api.plugin.Requirements) ArtifactVersion(io.cdap.cdap.api.artifact.ArtifactVersion) NamespaceId(io.cdap.cdap.proto.id.NamespaceId) Id(io.cdap.cdap.common.id.Id) ArtifactId(io.cdap.cdap.proto.id.ArtifactId) PluginClass(io.cdap.cdap.api.plugin.PluginClass) Test(org.junit.Test)

Example 15 with Requirements

use of io.cdap.cdap.api.plugin.Requirements in project cdap by caskdata.

the class RequirementsCodecTest method testCodec.

@Test
public void testCodec() throws IOException {
    String test1 = getData("Requirements_Pre_6_3.json");
    Gson gsonWithoutCodec = new Gson();
    Requirements requirements = gsonWithoutCodec.fromJson(test1, Requirements.class);
    // Will be null without codec
    Assert.assertNull(requirements.getCapabilities());
    Gson gson = new GsonBuilder().registerTypeAdapter(Requirements.class, new RequirementsCodec()).create();
    requirements = gson.fromJson(test1, Requirements.class);
    // not null with codec
    Assert.assertNotNull(requirements.getCapabilities());
    Assert.assertEquals(Arrays.stream(new String[] { "d1", "d2" }).collect(Collectors.toSet()), requirements.getDatasetTypes());
    String test2 = getData("Requirements_6_3.json");
    requirements = gson.fromJson(test2, Requirements.class);
    // Verify serialize and deserialize works as expected
    Assert.assertEquals(Arrays.stream(new String[] { "c1", "c2" }).collect(Collectors.toSet()), requirements.getCapabilities());
    Assert.assertEquals(Arrays.stream(new String[] { "d1", "d2" }).collect(Collectors.toSet()), requirements.getDatasetTypes());
    Assert.assertEquals(requirements, gson.fromJson(gson.toJson(requirements, Requirements.class), Requirements.class));
    String test3 = getData("PluginClass_Pre_6_3.json");
    PluginClass pluginClass = gson.fromJson(test3, PluginClass.class);
    Assert.assertNotNull(pluginClass.getRequirements().getCapabilities());
}
Also used : GsonBuilder(com.google.gson.GsonBuilder) Gson(com.google.gson.Gson) PluginClass(io.cdap.cdap.api.plugin.PluginClass) Requirements(io.cdap.cdap.api.plugin.Requirements) Test(org.junit.Test)

Aggregations

Requirements (io.cdap.cdap.api.plugin.Requirements)22 Test (org.junit.Test)18 ApplicationClass (io.cdap.cdap.api.artifact.ApplicationClass)8 PluginRequirement (io.cdap.cdap.internal.pipeline.PluginRequirement)8 PluginClass (io.cdap.cdap.api.plugin.PluginClass)6 ImmutableSet (com.google.common.collect.ImmutableSet)4 Gson (com.google.gson.Gson)4 GsonBuilder (com.google.gson.GsonBuilder)4 PluginPropertyField (io.cdap.cdap.api.plugin.PluginPropertyField)4 Id (io.cdap.cdap.common.id.Id)4 InspectionApp (io.cdap.cdap.internal.app.runtime.artifact.app.inspection.InspectionApp)4 ArtifactId (io.cdap.cdap.proto.id.ArtifactId)4 Capabilities (io.cdap.cdap.runtime.spi.provisioner.Capabilities)4 Set (java.util.Set)4 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 Sets (com.google.common.collect.Sets)2 JsonObject (com.google.gson.JsonObject)2 CapabilityAppWithWorkflow (io.cdap.cdap.CapabilityAppWithWorkflow)2 ArtifactClasses (io.cdap.cdap.api.artifact.ArtifactClasses)2 ArtifactRange (io.cdap.cdap.api.artifact.ArtifactRange)2