Search in sources :

Example 1 with DefaultAppConfigurer

use of co.cask.cdap.app.DefaultAppConfigurer in project cdap by caskdata.

the class DistributedProgramRunnerTxTimeoutTest method setup.

@BeforeClass
public static void setup() {
    Application app = new AppWithAllProgramTypes();
    DefaultAppConfigurer configurer = new DefaultAppConfigurer(Id.Namespace.DEFAULT, new Id.Artifact(Id.Namespace.DEFAULT, "artifact", new ArtifactVersion("0.1")), app);
    app.configure(configurer, new ApplicationContext() {

        @Override
        public Config getConfig() {
            return null;
        }
    });
    appSpec = configurer.createSpecification("app", "1.0");
    // System.out.println(new GsonBuilder().setPrettyPrinting().create().toJson(appSpec));
    cConf.setInt(TxConstants.Manager.CFG_TX_MAX_TIMEOUT, 60);
    flowRunner = new DistributedFlowProgramRunner(null, yConf, cConf, null, null, null, null, null);
    serviceRunner = new DistributedServiceProgramRunner(null, yConf, cConf, null, null);
    workerRunner = new DistributedWorkerProgramRunner(null, yConf, cConf, null, null);
    mapreduceRunner = new DistributedMapReduceProgramRunner(null, yConf, cConf, null, null);
    sparkRunner = new DistributedSparkProgramRunner(SparkCompat.SPARK1_2_10, null, yConf, cConf, null, null, null);
    workflowRunner = new DistributedWorkflowProgramRunner(null, yConf, cConf, null, null, null);
}
Also used : DefaultAppConfigurer(co.cask.cdap.app.DefaultAppConfigurer) Config(co.cask.cdap.api.Config) ApplicationContext(co.cask.cdap.api.app.ApplicationContext) ArtifactVersion(co.cask.cdap.api.artifact.ArtifactVersion) ProgramId(co.cask.cdap.proto.id.ProgramId) Id(co.cask.cdap.common.id.Id) Application(co.cask.cdap.api.app.Application) AbstractApplication(co.cask.cdap.api.app.AbstractApplication) DistributedSparkProgramRunner(co.cask.cdap.app.runtime.spark.distributed.DistributedSparkProgramRunner) BeforeClass(org.junit.BeforeClass)

Example 2 with DefaultAppConfigurer

use of co.cask.cdap.app.DefaultAppConfigurer in project cdap by caskdata.

the class InMemoryConfigurator method getSpecJson.

private <T extends Config> String getSpecJson(Application<T> app) throws Exception {
    // This Gson cannot be static since it is used to deserialize user class.
    // Gson will keep a static map to class, hence will leak the classloader
    Gson gson = new GsonBuilder().registerTypeAdapterFactory(new CaseInsensitiveEnumTypeAdapterFactory()).create();
    // Now, we call configure, which returns application specification.
    DefaultAppConfigurer configurer;
    File tempDir = DirUtils.createTempDir(baseUnpackDir);
    try (PluginInstantiator pluginInstantiator = new PluginInstantiator(cConf, app.getClass().getClassLoader(), tempDir)) {
        configurer = new DefaultAppConfigurer(appNamespace, artifactId, app, configString, artifactRepository, pluginInstantiator);
        T appConfig;
        Type configType = Artifacts.getConfigType(app.getClass());
        if (configString.isEmpty()) {
            // noinspection unchecked
            appConfig = ((Class<T>) configType).newInstance();
        } else {
            try {
                appConfig = gson.fromJson(configString, configType);
            } catch (JsonSyntaxException e) {
                throw new IllegalArgumentException("Invalid JSON configuration was provided. Please check the syntax.", e);
            }
        }
        try {
            ClassLoader oldClassLoader = ClassLoaders.setContextClassLoader(new CombineClassLoader(null, app.getClass().getClassLoader(), getClass().getClassLoader()));
            try {
                app.configure(configurer, new DefaultApplicationContext<>(appConfig));
            } finally {
                ClassLoaders.setContextClassLoader(oldClassLoader);
            }
        } catch (Throwable t) {
            Throwable rootCause = Throwables.getRootCause(t);
            if (rootCause instanceof ClassNotFoundException) {
                // Heuristic to provide better error message
                String missingClass = rootCause.getMessage();
                // If the missing class has "spark" in the name, try to see if Spark is available
                if (missingClass.startsWith("org.apache.spark.") || missingClass.startsWith("co.cask.cdap.api.spark.")) {
                    // Try to load the SparkContext class, which should be available if Spark is available in the platform
                    try {
                        artifactClassLoader.loadClass("org.apache.spark.SparkContext");
                    } catch (ClassNotFoundException e) {
                        // Spark is not available, it is most likely caused by missing Spark in the platform
                        throw new IllegalStateException("Missing Spark related class " + missingClass + ". It may be caused by unavailability of Spark. " + "Please verify environment variable " + Constants.SPARK_HOME + " is set correctly", t);
                    }
                    // Spark is available, can be caused by incompatible Spark version
                    throw new InvalidArtifactException("Missing Spark related class " + missingClass + ". Configured to use Spark located at " + System.getenv(Constants.SPARK_HOME) + ", which may be incompatible with the one required by the application", t);
                }
                // then the missing class is most likely due to some missing library in the artifact jar
                throw new InvalidArtifactException("Missing class " + missingClass + ". It may be caused by missing dependency jar(s) in the artifact jar.", t);
            }
            throw t;
        }
    } finally {
        try {
            DirUtils.deleteDirectoryContents(tempDir);
        } catch (IOException e) {
            LOG.warn("Exception raised when deleting directory {}", tempDir, e);
        }
    }
    ApplicationSpecification specification = configurer.createSpecification(applicationName, applicationVersion);
    // TODO: The SchemaGenerator should be injected
    return ApplicationSpecificationAdapter.create(new ReflectionSchemaGenerator()).toJson(specification);
}
Also used : ApplicationSpecification(co.cask.cdap.api.app.ApplicationSpecification) GsonBuilder(com.google.gson.GsonBuilder) DefaultAppConfigurer(co.cask.cdap.app.DefaultAppConfigurer) Gson(com.google.gson.Gson) IOException(java.io.IOException) ReflectionSchemaGenerator(co.cask.cdap.internal.io.ReflectionSchemaGenerator) CombineClassLoader(co.cask.cdap.common.lang.CombineClassLoader) Type(java.lang.reflect.Type) CaseInsensitiveEnumTypeAdapterFactory(co.cask.cdap.common.io.CaseInsensitiveEnumTypeAdapterFactory) JsonSyntaxException(com.google.gson.JsonSyntaxException) CombineClassLoader(co.cask.cdap.common.lang.CombineClassLoader) PluginInstantiator(co.cask.cdap.internal.app.runtime.plugin.PluginInstantiator) File(java.io.File) InvalidArtifactException(co.cask.cdap.common.InvalidArtifactException)

Example 3 with DefaultAppConfigurer

use of co.cask.cdap.app.DefaultAppConfigurer in project cdap by caskdata.

the class Specifications method from.

public static ApplicationSpecification from(Application app) {
    DefaultAppConfigurer appConfigurer = new DefaultAppConfigurer(Id.Namespace.fromEntityId(DefaultId.NAMESPACE), Id.Artifact.fromEntityId(DefaultId.ARTIFACT), app);
    app.configure(appConfigurer, new DefaultApplicationContext());
    return appConfigurer.createSpecification(null);
}
Also used : DefaultApplicationContext(co.cask.cdap.app.DefaultApplicationContext) DefaultAppConfigurer(co.cask.cdap.app.DefaultAppConfigurer)

Aggregations

DefaultAppConfigurer (co.cask.cdap.app.DefaultAppConfigurer)3 Config (co.cask.cdap.api.Config)1 AbstractApplication (co.cask.cdap.api.app.AbstractApplication)1 Application (co.cask.cdap.api.app.Application)1 ApplicationContext (co.cask.cdap.api.app.ApplicationContext)1 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)1 ArtifactVersion (co.cask.cdap.api.artifact.ArtifactVersion)1 DefaultApplicationContext (co.cask.cdap.app.DefaultApplicationContext)1 DistributedSparkProgramRunner (co.cask.cdap.app.runtime.spark.distributed.DistributedSparkProgramRunner)1 InvalidArtifactException (co.cask.cdap.common.InvalidArtifactException)1 Id (co.cask.cdap.common.id.Id)1 CaseInsensitiveEnumTypeAdapterFactory (co.cask.cdap.common.io.CaseInsensitiveEnumTypeAdapterFactory)1 CombineClassLoader (co.cask.cdap.common.lang.CombineClassLoader)1 PluginInstantiator (co.cask.cdap.internal.app.runtime.plugin.PluginInstantiator)1 ReflectionSchemaGenerator (co.cask.cdap.internal.io.ReflectionSchemaGenerator)1 ProgramId (co.cask.cdap.proto.id.ProgramId)1 Gson (com.google.gson.Gson)1 GsonBuilder (com.google.gson.GsonBuilder)1 JsonSyntaxException (com.google.gson.JsonSyntaxException)1 File (java.io.File)1