Search in sources :

Example 1 with InstantiatorFactory

use of co.cask.cdap.common.lang.InstantiatorFactory in project cdap by caskdata.

the class MapReduceProgramRunner method run.

@Override
public ProgramController run(final Program program, ProgramOptions options) {
    // Extract and verify parameters
    ApplicationSpecification appSpec = program.getApplicationSpecification();
    Preconditions.checkNotNull(appSpec, "Missing application specification.");
    ProgramType processorType = program.getType();
    Preconditions.checkNotNull(processorType, "Missing processor type.");
    Preconditions.checkArgument(processorType == ProgramType.MAPREDUCE, "Only MAPREDUCE process type is supported.");
    MapReduceSpecification spec = appSpec.getMapReduce().get(program.getName());
    Preconditions.checkNotNull(spec, "Missing MapReduceSpecification for %s", program.getName());
    Arguments arguments = options.getArguments();
    RunId runId = ProgramRunners.getRunId(options);
    WorkflowProgramInfo workflowInfo = WorkflowProgramInfo.create(arguments);
    DatasetFramework programDatasetFramework = workflowInfo == null ? datasetFramework : NameMappedDatasetFramework.createFromWorkflowProgramInfo(datasetFramework, workflowInfo, appSpec);
    // Setup dataset framework context, if required
    if (programDatasetFramework instanceof ProgramContextAware) {
        ProgramId programId = program.getId();
        ((ProgramContextAware) programDatasetFramework).setContext(new BasicProgramContext(programId.run(runId)));
    }
    MapReduce mapReduce;
    try {
        mapReduce = new InstantiatorFactory(false).get(TypeToken.of(program.<MapReduce>getMainClass())).create();
    } catch (Exception e) {
        LOG.error("Failed to instantiate MapReduce class for {}", spec.getClassName(), e);
        throw Throwables.propagate(e);
    }
    // List of all Closeable resources that needs to be cleanup
    List<Closeable> closeables = new ArrayList<>();
    try {
        PluginInstantiator pluginInstantiator = createPluginInstantiator(options, program.getClassLoader());
        if (pluginInstantiator != null) {
            closeables.add(pluginInstantiator);
        }
        final BasicMapReduceContext context = new BasicMapReduceContext(program, options, cConf, spec, workflowInfo, discoveryServiceClient, metricsCollectionService, txSystemClient, programDatasetFramework, streamAdmin, getPluginArchive(options), pluginInstantiator, secureStore, secureStoreManager, messagingService);
        Reflections.visit(mapReduce, mapReduce.getClass(), new PropertyFieldSetter(context.getSpecification().getProperties()), new MetricsFieldSetter(context.getMetrics()), new DataSetFieldSetter(context));
        // note: this sets logging context on the thread level
        LoggingContextAccessor.setLoggingContext(context.getLoggingContext());
        // Set the job queue to hConf if it is provided
        Configuration hConf = new Configuration(this.hConf);
        String schedulerQueue = options.getArguments().getOption(Constants.AppFabric.APP_SCHEDULER_QUEUE);
        if (schedulerQueue != null && !schedulerQueue.isEmpty()) {
            hConf.set(JobContext.QUEUE_NAME, schedulerQueue);
        }
        Service mapReduceRuntimeService = new MapReduceRuntimeService(injector, cConf, hConf, mapReduce, spec, context, program.getJarLocation(), locationFactory, streamAdmin, txSystemClient, authorizationEnforcer, authenticationContext);
        mapReduceRuntimeService.addListener(createRuntimeServiceListener(program.getId(), runId, closeables, arguments, options.getUserArguments()), Threads.SAME_THREAD_EXECUTOR);
        final ProgramController controller = new MapReduceProgramController(mapReduceRuntimeService, context);
        LOG.debug("Starting MapReduce Job: {}", context);
        // be running the job, but the data directory will be owned by cdap.
        if (MapReduceTaskContextProvider.isLocal(hConf) || UserGroupInformation.isSecurityEnabled()) {
            mapReduceRuntimeService.start();
        } else {
            ProgramRunners.startAsUser(cConf.get(Constants.CFG_HDFS_USER), mapReduceRuntimeService);
        }
        return controller;
    } catch (Exception e) {
        closeAllQuietly(closeables);
        throw Throwables.propagate(e);
    }
}
Also used : ApplicationSpecification(co.cask.cdap.api.app.ApplicationSpecification) CConfiguration(co.cask.cdap.common.conf.CConfiguration) Configuration(org.apache.hadoop.conf.Configuration) Closeable(java.io.Closeable) ArrayList(java.util.ArrayList) MapReduce(co.cask.cdap.api.mapreduce.MapReduce) NameMappedDatasetFramework(co.cask.cdap.internal.app.runtime.workflow.NameMappedDatasetFramework) DatasetFramework(co.cask.cdap.data2.dataset2.DatasetFramework) InstantiatorFactory(co.cask.cdap.common.lang.InstantiatorFactory) MetricsFieldSetter(co.cask.cdap.internal.app.runtime.MetricsFieldSetter) ProgramType(co.cask.cdap.proto.ProgramType) RunId(org.apache.twill.api.RunId) ProgramController(co.cask.cdap.app.runtime.ProgramController) MapReduceSpecification(co.cask.cdap.api.mapreduce.MapReduceSpecification) Arguments(co.cask.cdap.app.runtime.Arguments) MessagingService(co.cask.cdap.messaging.MessagingService) MetricsCollectionService(co.cask.cdap.api.metrics.MetricsCollectionService) Service(com.google.common.util.concurrent.Service) ProgramId(co.cask.cdap.proto.id.ProgramId) BasicProgramContext(co.cask.cdap.internal.app.runtime.BasicProgramContext) DataSetFieldSetter(co.cask.cdap.internal.app.runtime.DataSetFieldSetter) PropertyFieldSetter(co.cask.cdap.common.lang.PropertyFieldSetter) WorkflowProgramInfo(co.cask.cdap.internal.app.runtime.workflow.WorkflowProgramInfo) PluginInstantiator(co.cask.cdap.internal.app.runtime.plugin.PluginInstantiator) ProgramContextAware(co.cask.cdap.data.ProgramContextAware)

Example 2 with InstantiatorFactory

use of co.cask.cdap.common.lang.InstantiatorFactory in project cdap by caskdata.

the class WorkflowDriver method initializeWorkflow.

@SuppressWarnings("unchecked")
private Workflow initializeWorkflow() throws Exception {
    Class<?> clz = Class.forName(workflowSpec.getClassName(), true, program.getClassLoader());
    if (!Workflow.class.isAssignableFrom(clz)) {
        throw new IllegalStateException(String.format("%s is not Workflow.", clz));
    }
    Class<? extends Workflow> workflowClass = (Class<? extends Workflow>) clz;
    final Workflow workflow = new InstantiatorFactory(false).get(TypeToken.of(workflowClass)).create();
    // set metrics
    Reflections.visit(workflow, workflow.getClass(), new MetricsFieldSetter(basicWorkflowContext.getMetrics()));
    if (!(workflow instanceof ProgramLifecycle)) {
        return workflow;
    }
    final TransactionControl txControl = Transactions.getTransactionControl(TransactionControl.IMPLICIT, Workflow.class, workflow, "initialize", WorkflowContext.class);
    basicWorkflowToken.setCurrentNode(workflowSpec.getName());
    basicWorkflowContext.setState(new ProgramState(ProgramStatus.INITIALIZING, null));
    basicWorkflowContext.initializeProgram((ProgramLifecycle) workflow, basicWorkflowContext, txControl, false);
    runtimeStore.updateWorkflowToken(workflowRunId, basicWorkflowToken);
    return workflow;
}
Also used : InstantiatorFactory(co.cask.cdap.common.lang.InstantiatorFactory) MetricsFieldSetter(co.cask.cdap.internal.app.runtime.MetricsFieldSetter) ProgramLifecycle(co.cask.cdap.api.ProgramLifecycle) TransactionControl(co.cask.cdap.api.annotation.TransactionControl) Workflow(co.cask.cdap.api.workflow.Workflow) ProgramState(co.cask.cdap.api.ProgramState)

Example 3 with InstantiatorFactory

use of co.cask.cdap.common.lang.InstantiatorFactory in project cdap by caskdata.

the class WorkerDriver method startUp.

@Override
protected void startUp() throws Exception {
    LoggingContextAccessor.setLoggingContext(context.getLoggingContext());
    // Instantiate worker instance
    Class<?> workerClass = program.getClassLoader().loadClass(spec.getClassName());
    @SuppressWarnings("unchecked") TypeToken<Worker> workerType = (TypeToken<Worker>) TypeToken.of(workerClass);
    worker = new InstantiatorFactory(false).get(workerType).create();
    // Fields injection
    Reflections.visit(worker, workerType.getType(), new MetricsFieldSetter(context.getMetrics()), new PropertyFieldSetter(spec.getProperties()));
    LOG.debug("Starting Worker Program {}", program.getId());
    // Initialize worker
    TransactionControl txControl = Transactions.getTransactionControl(TransactionControl.EXPLICIT, Worker.class, worker, "initialize", WorkerContext.class);
    try {
        context.initializeProgram(worker, context, txControl, false);
    } catch (LinkageError e) {
        // of the user program is missing dependencies (CDAP-2543)
        throw new Exception(e.getMessage(), e);
    }
}
Also used : InstantiatorFactory(co.cask.cdap.common.lang.InstantiatorFactory) PropertyFieldSetter(co.cask.cdap.common.lang.PropertyFieldSetter) MetricsFieldSetter(co.cask.cdap.internal.app.runtime.MetricsFieldSetter) TypeToken(com.google.common.reflect.TypeToken) TransactionControl(co.cask.cdap.api.annotation.TransactionControl) Worker(co.cask.cdap.api.worker.Worker)

Example 4 with InstantiatorFactory

use of co.cask.cdap.common.lang.InstantiatorFactory in project cdap by caskdata.

the class ServiceHttpServer method createHandlerDelegatorContexts.

private List<HandlerDelegatorContext> createHandlerDelegatorContexts(Program program, ServiceSpecification spec, BasicHttpServiceContextFactory contextFactory) {
    // Constructs all handler delegator. It is for bridging ServiceHttpHandler and HttpHandler (in netty-http).
    List<HandlerDelegatorContext> delegatorContexts = Lists.newArrayList();
    InstantiatorFactory instantiatorFactory = new InstantiatorFactory(false);
    for (Map.Entry<String, HttpServiceHandlerSpecification> entry : spec.getHandlers().entrySet()) {
        try {
            Class<?> handlerClass = program.getClassLoader().loadClass(entry.getValue().getClassName());
            @SuppressWarnings("unchecked") TypeToken<HttpServiceHandler> type = TypeToken.of((Class<HttpServiceHandler>) handlerClass);
            delegatorContexts.add(new HandlerDelegatorContext(type, instantiatorFactory, entry.getValue(), contextFactory));
        } catch (Exception e) {
            LOG.error("Could not initialize HTTP Service");
            throw Throwables.propagate(e);
        }
    }
    return delegatorContexts;
}
Also used : InstantiatorFactory(co.cask.cdap.common.lang.InstantiatorFactory) HttpServiceHandler(co.cask.cdap.api.service.http.HttpServiceHandler) HttpServiceHandlerSpecification(co.cask.cdap.api.service.http.HttpServiceHandlerSpecification) Map(java.util.Map)

Example 5 with InstantiatorFactory

use of co.cask.cdap.common.lang.InstantiatorFactory in project cdap by caskdata.

the class ExploreCustomClassLoaderTwillRunnable method initialize.

@Override
public void initialize(TwillContext context) {
    super.initialize(context);
    if (getClass().getClassLoader() instanceof URLClassLoader) {
        LOG.debug("Using ResourcesClassLoader to load config files bundled with application.");
        customClassLoader = new ResourcesClassLoader(((URLClassLoader) getClass().getClassLoader()).getURLs(), getClass().getClassLoader());
    } else {
        LOG.warn("Classloader is not URLCLassLoader, config files bundled with application might not be loaded.");
        customClassLoader = getClass().getClassLoader();
    }
    ClassLoader previousClassLoader = ClassLoaders.setContextClassLoader(customClassLoader);
    try {
        @SuppressWarnings("unchecked") Class<? extends TwillRunnable> twillRunnableClass = (Class<? extends TwillRunnable>) customClassLoader.loadClass(CLASS_NAME);
        twillRunnable = new InstantiatorFactory(false).get(TypeToken.of(twillRunnableClass)).create();
        twillRunnable.initialize(context);
    } catch (Exception e) {
        throw Throwables.propagate(e);
    } finally {
        ClassLoaders.setContextClassLoader(previousClassLoader);
    }
}
Also used : InstantiatorFactory(co.cask.cdap.common.lang.InstantiatorFactory) URLClassLoader(java.net.URLClassLoader) TwillRunnable(org.apache.twill.api.TwillRunnable) AbstractTwillRunnable(org.apache.twill.api.AbstractTwillRunnable) URLClassLoader(java.net.URLClassLoader)

Aggregations

InstantiatorFactory (co.cask.cdap.common.lang.InstantiatorFactory)8 MetricsFieldSetter (co.cask.cdap.internal.app.runtime.MetricsFieldSetter)4 ApplicationSpecification (co.cask.cdap.api.app.ApplicationSpecification)3 MetricsCollectionService (co.cask.cdap.api.metrics.MetricsCollectionService)3 PropertyFieldSetter (co.cask.cdap.common.lang.PropertyFieldSetter)3 ProgramContextAware (co.cask.cdap.data.ProgramContextAware)3 BasicProgramContext (co.cask.cdap.internal.app.runtime.BasicProgramContext)3 MessagingService (co.cask.cdap.messaging.MessagingService)3 ProgramType (co.cask.cdap.proto.ProgramType)3 ProgramId (co.cask.cdap.proto.id.ProgramId)3 Service (com.google.common.util.concurrent.Service)3 RunId (org.apache.twill.api.RunId)3 ProgramState (co.cask.cdap.api.ProgramState)2 TransactionControl (co.cask.cdap.api.annotation.TransactionControl)2 Arguments (co.cask.cdap.app.runtime.Arguments)2 ProgramController (co.cask.cdap.app.runtime.ProgramController)2 CConfiguration (co.cask.cdap.common.conf.CConfiguration)2 DatasetFramework (co.cask.cdap.data2.dataset2.DatasetFramework)2 DataSetFieldSetter (co.cask.cdap.internal.app.runtime.DataSetFieldSetter)2 PluginInstantiator (co.cask.cdap.internal.app.runtime.plugin.PluginInstantiator)2