Search in sources :

Example 1 with DefaultApplicationUpdateContext

use of io.cdap.cdap.internal.app.DefaultApplicationUpdateContext in project cdap by caskdata.

the class ApplicationLifecycleService method updateApplicationInternal.

/**
 * Updates an application config by applying given update actions. The app should know how to apply these actions
 * to its config.
 */
private void updateApplicationInternal(ApplicationId appId, @Nullable String currentConfigStr, ProgramTerminator programTerminator, ArtifactDetail artifactDetail, List<ApplicationConfigUpdateAction> updateActions, Set<ArtifactScope> allowedArtifactScopes, boolean allowSnapshot, @Nullable KerberosPrincipalId ownerPrincipal, boolean updateSchedules) throws Exception {
    ApplicationClass appClass = Iterables.getFirst(artifactDetail.getMeta().getClasses().getApps(), null);
    if (appClass == null) {
        // This should never happen.
        throw new IllegalStateException(String.format("No application class found in artifact '%s' in namespace '%s'.", artifactDetail.getDescriptor().getArtifactId(), appId.getParent()));
    }
    io.cdap.cdap.proto.id.ArtifactId artifactId = Artifacts.toProtoArtifactId(appId.getParent(), artifactDetail.getDescriptor().getArtifactId());
    EntityImpersonator classLoaderImpersonator = new EntityImpersonator(artifactId, this.impersonator);
    String updatedAppConfig;
    DefaultApplicationUpdateContext updateContext = new DefaultApplicationUpdateContext(appId.getParent(), appId, artifactDetail.getDescriptor().getArtifactId(), artifactRepository, currentConfigStr, updateActions, allowedArtifactScopes, allowSnapshot);
    try (CloseableClassLoader artifactClassLoader = artifactRepository.createArtifactClassLoader(artifactDetail.getDescriptor(), classLoaderImpersonator)) {
        Object appMain = artifactClassLoader.loadClass(appClass.getClassName()).newInstance();
        // Run config update logic for the application to generate updated config.
        if (!(appMain instanceof Application)) {
            throw new IllegalStateException(String.format("Application main class is of invalid type: %s", appMain.getClass().getName()));
        }
        Application app = (Application) appMain;
        Type configType = Artifacts.getConfigType(app.getClass());
        if (!app.isUpdateSupported()) {
            String errorMessage = String.format("Application %s does not support update.", appId);
            throw new UnsupportedOperationException(errorMessage);
        }
        ApplicationUpdateResult<?> updateResult = app.updateConfig(updateContext);
        updatedAppConfig = GSON.toJson(updateResult.getNewConfig(), configType);
    }
    // Deploy application with with potentially new app config and new artifact.
    AppDeploymentInfo deploymentInfo = new AppDeploymentInfo(artifactId, artifactDetail.getDescriptor().getLocation(), appId.getParent(), appClass, appId.getApplication(), appId.getVersion(), updatedAppConfig, ownerPrincipal, updateSchedules, null);
    Manager<AppDeploymentInfo, ApplicationWithPrograms> manager = managerFactory.create(programTerminator);
    // TODO: (CDAP-3258) Manager needs MUCH better error handling.
    ApplicationWithPrograms applicationWithPrograms;
    try {
        applicationWithPrograms = manager.deploy(deploymentInfo).get();
    } catch (ExecutionException e) {
        Throwables.propagateIfPossible(e.getCause(), Exception.class);
        throw Throwables.propagate(e.getCause());
    }
    adminEventPublisher.publishAppCreation(applicationWithPrograms.getApplicationId(), applicationWithPrograms.getSpecification());
}
Also used : DefaultApplicationUpdateContext(io.cdap.cdap.internal.app.DefaultApplicationUpdateContext) EntityImpersonator(io.cdap.cdap.security.impersonation.EntityImpersonator) ApplicationClass(io.cdap.cdap.api.artifact.ApplicationClass) CloseableClassLoader(io.cdap.cdap.api.artifact.CloseableClassLoader) ApplicationNotFoundException(io.cdap.cdap.common.ApplicationNotFoundException) CapabilityNotAvailableException(io.cdap.cdap.internal.capability.CapabilityNotAvailableException) IOException(java.io.IOException) CannotBeDeletedException(io.cdap.cdap.common.CannotBeDeletedException) ExecutionException(java.util.concurrent.ExecutionException) AccessException(io.cdap.cdap.api.security.AccessException) JsonIOException(com.google.gson.JsonIOException) InvalidArtifactException(io.cdap.cdap.common.InvalidArtifactException) ArtifactAlreadyExistsException(io.cdap.cdap.common.ArtifactAlreadyExistsException) NotFoundException(io.cdap.cdap.common.NotFoundException) ArtifactNotFoundException(io.cdap.cdap.common.ArtifactNotFoundException) EntityType(io.cdap.cdap.proto.element.EntityType) Type(java.lang.reflect.Type) ProgramType(io.cdap.cdap.proto.ProgramType) AppDeploymentInfo(io.cdap.cdap.internal.app.deploy.pipeline.AppDeploymentInfo) ApplicationWithPrograms(io.cdap.cdap.internal.app.deploy.pipeline.ApplicationWithPrograms) ExecutionException(java.util.concurrent.ExecutionException) Application(io.cdap.cdap.api.app.Application)

Aggregations

JsonIOException (com.google.gson.JsonIOException)1 Application (io.cdap.cdap.api.app.Application)1 ApplicationClass (io.cdap.cdap.api.artifact.ApplicationClass)1 CloseableClassLoader (io.cdap.cdap.api.artifact.CloseableClassLoader)1 AccessException (io.cdap.cdap.api.security.AccessException)1 ApplicationNotFoundException (io.cdap.cdap.common.ApplicationNotFoundException)1 ArtifactAlreadyExistsException (io.cdap.cdap.common.ArtifactAlreadyExistsException)1 ArtifactNotFoundException (io.cdap.cdap.common.ArtifactNotFoundException)1 CannotBeDeletedException (io.cdap.cdap.common.CannotBeDeletedException)1 InvalidArtifactException (io.cdap.cdap.common.InvalidArtifactException)1 NotFoundException (io.cdap.cdap.common.NotFoundException)1 DefaultApplicationUpdateContext (io.cdap.cdap.internal.app.DefaultApplicationUpdateContext)1 AppDeploymentInfo (io.cdap.cdap.internal.app.deploy.pipeline.AppDeploymentInfo)1 ApplicationWithPrograms (io.cdap.cdap.internal.app.deploy.pipeline.ApplicationWithPrograms)1 CapabilityNotAvailableException (io.cdap.cdap.internal.capability.CapabilityNotAvailableException)1 ProgramType (io.cdap.cdap.proto.ProgramType)1 EntityType (io.cdap.cdap.proto.element.EntityType)1 EntityImpersonator (io.cdap.cdap.security.impersonation.EntityImpersonator)1 IOException (java.io.IOException)1 Type (java.lang.reflect.Type)1