Search in sources :

Example 6 with MetadataMutation

use of io.cdap.cdap.spi.metadata.MetadataMutation in project cdap by cdapio.

the class ProfileMetadataMessageProcessor method removeProfileMetadata.

/**
 * Remove the profile metadata according to the message, currently only meant for application and schedule.
 */
private void removeProfileMetadata(MetadataMessage message) throws IOException {
    EntityId entity = message.getEntityId();
    List<MetadataMutation> deletes = new ArrayList<>();
    // We only care about application and schedules.
    if (entity.getEntityType().equals(EntityType.APPLICATION)) {
        LOG.trace("Removing profile metadata for {}", entity);
        ApplicationId appId = (ApplicationId) message.getEntityId();
        ApplicationSpecification appSpec = message.getPayload(GSON, ApplicationSpecification.class);
        for (ProgramId programId : getAllProfileAllowedPrograms(appSpec, appId)) {
            addProfileMetadataDelete(programId, deletes);
        }
        for (ScheduleId scheduleId : getSchedulesInApp(appId, appSpec.getProgramSchedules())) {
            addProfileMetadataDelete(scheduleId, deletes);
        }
        addPluginMetadataDelete(appId, appSpec, deletes);
    } else if (entity.getEntityType().equals(EntityType.SCHEDULE)) {
        addProfileMetadataDelete((NamespacedEntityId) entity, deletes);
    }
    if (!deletes.isEmpty()) {
        metadataStorage.batch(deletes, MutationOptions.DEFAULT);
    }
}
Also used : NamespacedEntityId(io.cdap.cdap.proto.id.NamespacedEntityId) EntityId(io.cdap.cdap.proto.id.EntityId) ApplicationSpecification(io.cdap.cdap.api.app.ApplicationSpecification) MetadataMutation(io.cdap.cdap.spi.metadata.MetadataMutation) NamespacedEntityId(io.cdap.cdap.proto.id.NamespacedEntityId) ArrayList(java.util.ArrayList) ApplicationId(io.cdap.cdap.proto.id.ApplicationId) ProgramId(io.cdap.cdap.proto.id.ProgramId) ScheduleId(io.cdap.cdap.proto.id.ScheduleId)

Example 7 with MetadataMutation

use of io.cdap.cdap.spi.metadata.MetadataMutation in project cdap by cdapio.

the class ProfileMetadataMessageProcessor method addPluginMetadataDelete.

private void addPluginMetadataDelete(ApplicationId appId, ApplicationSpecification appSpec, List<MetadataMutation> deletes) {
    LOG.trace("Deleting plugin metadata for {}", appSpec.getName());
    String namespace = appId.getNamespace();
    String appKey = String.format("%s:%s", namespace, appSpec.getName());
    Set<ScopedNameOfKind> pluginSet = Collections.singleton(new ScopedNameOfKind(MetadataKind.PROPERTY, MetadataScope.SYSTEM, appKey));
    for (Plugin plugin : appSpec.getPlugins().values()) {
        PluginId pluginId = new PluginId(namespace, plugin);
        deletes.add(new MetadataMutation.Remove(pluginId.toMetadataEntity(), pluginSet));
    }
}
Also used : MetadataMutation(io.cdap.cdap.spi.metadata.MetadataMutation) ScopedNameOfKind(io.cdap.cdap.spi.metadata.ScopedNameOfKind) PluginId(io.cdap.cdap.proto.id.PluginId) Plugin(io.cdap.cdap.api.plugin.Plugin)

Example 8 with MetadataMutation

use of io.cdap.cdap.spi.metadata.MetadataMutation in project cdap by cdapio.

the class DefaultArtifactInspector method inspectPlugins.

/**
 * Inspects the plugin file and extracts plugin classes information.
 */
private void inspectPlugins(ArtifactClasses.Builder builder, File artifactFile, io.cdap.cdap.proto.id.ArtifactId artifactId, PluginInstantiator pluginInstantiator, Set<PluginClass> additionalPlugins, List<MetadataMutation> mutations) throws IOException, InvalidArtifactException {
    ArtifactId artifact = artifactId.toApiArtifactId();
    PluginClassLoader pluginClassLoader = pluginInstantiator.getArtifactClassLoader(artifact);
    inspectAdditionalPlugins(artifact, additionalPlugins, pluginClassLoader);
    // See if there are export packages. Plugins should be in those packages
    Set<String> exportPackages = getExportPackages(artifactFile);
    if (exportPackages.isEmpty()) {
        return;
    }
    try {
        for (Class<?> cls : getPluginClasses(exportPackages, pluginClassLoader)) {
            Plugin pluginAnnotation = cls.getAnnotation(Plugin.class);
            if (pluginAnnotation == null) {
                continue;
            }
            Map<String, PluginPropertyField> pluginProperties = Maps.newHashMap();
            try {
                String configField = getProperties(TypeToken.of(cls), pluginProperties);
                String pluginName = getPluginName(cls);
                PluginId pluginId = new PluginId(artifactId.getNamespace(), artifactId.getArtifact(), artifactId.getVersion(), pluginName, pluginAnnotation.type());
                MetadataMutation mutation = getMetadataMutation(pluginId, cls);
                if (mutation != null) {
                    mutations.add(mutation);
                }
                PluginClass pluginClass = PluginClass.builder().setName(pluginName).setType(pluginAnnotation.type()).setCategory(getPluginCategory(cls)).setClassName(cls.getName()).setConfigFieldName(configField).setProperties(pluginProperties).setRequirements(getArtifactRequirements(cls)).setDescription(getPluginDescription(cls)).build();
                builder.addPlugin(pluginClass);
            } catch (UnsupportedTypeException e) {
                LOG.warn("Plugin configuration type not supported. Plugin ignored. {}", cls, e);
            }
        }
    } catch (Throwable t) {
        throw new InvalidArtifactException(String.format("Class could not be found while inspecting artifact for plugins. " + "Please check dependencies are available, and that the correct parent artifact was specified. " + "Error class: %s, message: %s.", t.getClass(), t.getMessage()), t);
    }
}
Also used : ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) PluginId(io.cdap.cdap.proto.id.PluginId) PluginPropertyField(io.cdap.cdap.api.plugin.PluginPropertyField) MetadataMutation(io.cdap.cdap.spi.metadata.MetadataMutation) UnsupportedTypeException(io.cdap.cdap.api.data.schema.UnsupportedTypeException) PluginClass(io.cdap.cdap.api.plugin.PluginClass) InvalidArtifactException(io.cdap.cdap.common.InvalidArtifactException) PluginClassLoader(io.cdap.cdap.internal.app.runtime.plugin.PluginClassLoader) Plugin(io.cdap.cdap.api.annotation.Plugin)

Example 9 with MetadataMutation

use of io.cdap.cdap.spi.metadata.MetadataMutation in project cdap by caskdata.

the class DefaultArtifactRepository method deleteArtifact.

@Override
public void deleteArtifact(Id.Artifact artifactId) throws Exception {
    ArtifactDetail artifactDetail = artifactStore.getArtifact(artifactId);
    io.cdap.cdap.proto.id.ArtifactId artifact = artifactId.toEntityId();
    // delete the artifact first and then privileges. Not the other way to avoid orphan artifact
    // which does not have any privilege if the artifact delete from store fails. see CDAP-6648
    artifactStore.delete(artifactId);
    List<MetadataMutation> mutations = new ArrayList<>();
    // drop artifact metadata
    mutations.add(new MetadataMutation.Drop(artifact.toMetadataEntity()));
    Set<PluginClass> plugins = artifactDetail.getMeta().getClasses().getPlugins();
    // drop plugin metadata
    plugins.forEach(pluginClass -> {
        PluginId pluginId = new PluginId(artifact.getNamespace(), artifact.getArtifact(), artifact.getVersion(), pluginClass.getName(), pluginClass.getType());
        mutations.add(new MetadataMutation.Drop(pluginId.toMetadataEntity()));
    });
    metadataServiceClient.batch(mutations);
}
Also used : MetadataMutation(io.cdap.cdap.spi.metadata.MetadataMutation) ArrayList(java.util.ArrayList) PluginClass(io.cdap.cdap.api.plugin.PluginClass) PluginId(io.cdap.cdap.proto.id.PluginId)

Example 10 with MetadataMutation

use of io.cdap.cdap.spi.metadata.MetadataMutation in project cdap by caskdata.

the class DefaultArtifactInspector method inspectPlugins.

/**
 * Inspects the plugin file and extracts plugin classes information.
 */
private void inspectPlugins(ArtifactClasses.Builder builder, File artifactFile, io.cdap.cdap.proto.id.ArtifactId artifactId, PluginInstantiator pluginInstantiator, Set<PluginClass> additionalPlugins, List<MetadataMutation> mutations) throws IOException, InvalidArtifactException {
    ArtifactId artifact = artifactId.toApiArtifactId();
    PluginClassLoader pluginClassLoader = pluginInstantiator.getArtifactClassLoader(artifact);
    inspectAdditionalPlugins(artifact, additionalPlugins, pluginClassLoader);
    // See if there are export packages. Plugins should be in those packages
    Set<String> exportPackages = getExportPackages(artifactFile);
    if (exportPackages.isEmpty()) {
        return;
    }
    try {
        for (Class<?> cls : getPluginClasses(exportPackages, pluginClassLoader)) {
            Plugin pluginAnnotation = cls.getAnnotation(Plugin.class);
            if (pluginAnnotation == null) {
                continue;
            }
            Map<String, PluginPropertyField> pluginProperties = Maps.newHashMap();
            try {
                String configField = getProperties(TypeToken.of(cls), pluginProperties);
                String pluginName = getPluginName(cls);
                PluginId pluginId = new PluginId(artifactId.getNamespace(), artifactId.getArtifact(), artifactId.getVersion(), pluginName, pluginAnnotation.type());
                MetadataMutation mutation = getMetadataMutation(pluginId, cls);
                if (mutation != null) {
                    mutations.add(mutation);
                }
                PluginClass pluginClass = PluginClass.builder().setName(pluginName).setType(pluginAnnotation.type()).setCategory(getPluginCategory(cls)).setClassName(cls.getName()).setConfigFieldName(configField).setProperties(pluginProperties).setRequirements(getArtifactRequirements(cls)).setDescription(getPluginDescription(cls)).build();
                builder.addPlugin(pluginClass);
            } catch (UnsupportedTypeException e) {
                LOG.warn("Plugin configuration type not supported. Plugin ignored. {}", cls, e);
            }
        }
    } catch (Throwable t) {
        throw new InvalidArtifactException(String.format("Class could not be found while inspecting artifact for plugins. " + "Please check dependencies are available, and that the correct parent artifact was specified. " + "Error class: %s, message: %s.", t.getClass(), t.getMessage()), t);
    }
}
Also used : ArtifactId(io.cdap.cdap.api.artifact.ArtifactId) PluginId(io.cdap.cdap.proto.id.PluginId) PluginPropertyField(io.cdap.cdap.api.plugin.PluginPropertyField) MetadataMutation(io.cdap.cdap.spi.metadata.MetadataMutation) UnsupportedTypeException(io.cdap.cdap.api.data.schema.UnsupportedTypeException) PluginClass(io.cdap.cdap.api.plugin.PluginClass) InvalidArtifactException(io.cdap.cdap.common.InvalidArtifactException) PluginClassLoader(io.cdap.cdap.internal.app.runtime.plugin.PluginClassLoader) Plugin(io.cdap.cdap.api.annotation.Plugin)

Aggregations

MetadataMutation (io.cdap.cdap.spi.metadata.MetadataMutation)18 ArrayList (java.util.ArrayList)14 PluginId (io.cdap.cdap.proto.id.PluginId)6 ApplicationSpecification (io.cdap.cdap.api.app.ApplicationSpecification)4 MetadataEntity (io.cdap.cdap.api.metadata.MetadataEntity)4 PluginClass (io.cdap.cdap.api.plugin.PluginClass)4 InvalidArtifactException (io.cdap.cdap.common.InvalidArtifactException)4 ApplicationId (io.cdap.cdap.proto.id.ApplicationId)4 IOException (java.io.IOException)4 LinkedHashMap (java.util.LinkedHashMap)4 ImmutableMap (com.google.common.collect.ImmutableMap)2 Plugin (io.cdap.cdap.api.annotation.Plugin)2 ArtifactClasses (io.cdap.cdap.api.artifact.ArtifactClasses)2 ArtifactId (io.cdap.cdap.api.artifact.ArtifactId)2 CloseableClassLoader (io.cdap.cdap.api.artifact.CloseableClassLoader)2 UnsupportedTypeException (io.cdap.cdap.api.data.schema.UnsupportedTypeException)2 Metadata (io.cdap.cdap.api.metadata.Metadata)2 Plugin (io.cdap.cdap.api.plugin.Plugin)2 PluginPropertyField (io.cdap.cdap.api.plugin.PluginPropertyField)2 ClassLoaderFolder (io.cdap.cdap.common.lang.jar.ClassLoaderFolder)2