Search in sources :

Example 1 with TwillController

use of org.apache.twill.api.TwillController in project cdap by caskdata.

the class DistributedProgramRuntimeService method list.

@Override
public synchronized Map<RunId, RuntimeInfo> list(ProgramType type) {
    Map<RunId, RuntimeInfo> result = Maps.newHashMap();
    result.putAll(super.list(type));
    // Table holds the Twill RunId and TwillController associated with the program matching the input type
    Table<ProgramId, RunId, TwillController> twillProgramInfo = HashBasedTable.create();
    // Goes through all live application and fill the twillProgramInfo table
    for (TwillRunner.LiveInfo liveInfo : twillRunner.lookupLive()) {
        String appName = liveInfo.getApplicationName();
        ProgramId programId = TwillAppNames.fromTwillAppName(appName, false);
        if (programId == null) {
            continue;
        }
        if (!type.equals(programId.getType())) {
            continue;
        }
        for (TwillController controller : liveInfo.getControllers()) {
            RunId twillRunId = controller.getRunId();
            if (isTwillRunIdCached(twillRunId)) {
                continue;
            }
            twillProgramInfo.put(programId, twillRunId, controller);
        }
    }
    if (twillProgramInfo.isEmpty()) {
        return ImmutableMap.copyOf(result);
    }
    final Set<RunId> twillRunIds = twillProgramInfo.columnKeySet();
    Collection<RunRecordMeta> activeRunRecords = store.getRuns(ProgramRunStatus.RUNNING, new Predicate<RunRecordMeta>() {

        @Override
        public boolean apply(RunRecordMeta record) {
            return record.getTwillRunId() != null && twillRunIds.contains(org.apache.twill.internal.RunIds.fromString(record.getTwillRunId()));
        }
    }).values();
    for (RunRecordMeta record : activeRunRecords) {
        String twillRunId = record.getTwillRunId();
        if (twillRunId == null) {
            // This is unexpected. Just log and ignore the run record
            LOG.warn("No twill runId for in run record {}.", record);
            continue;
        }
        RunId twillRunIdFromRecord = org.apache.twill.internal.RunIds.fromString(twillRunId);
        // Get the CDAP RunId from RunRecord
        RunId runId = RunIds.fromString(record.getPid());
        // Get the Program and TwillController for the current twillRunId
        Map<ProgramId, TwillController> mapForTwillId = twillProgramInfo.columnMap().get(twillRunIdFromRecord);
        Map.Entry<ProgramId, TwillController> entry = mapForTwillId.entrySet().iterator().next();
        // Create RuntimeInfo for the current Twill RunId
        RuntimeInfo runtimeInfo = createRuntimeInfo(entry.getKey(), entry.getValue(), runId);
        if (runtimeInfo != null) {
            result.put(runId, runtimeInfo);
            updateRuntimeInfo(type, runId, runtimeInfo);
        } else {
            LOG.warn("Unable to find program {} {}", type, entry.getKey());
        }
    }
    return ImmutableMap.copyOf(result);
}
Also used : SimpleRuntimeInfo(co.cask.cdap.internal.app.runtime.service.SimpleRuntimeInfo) TwillRunner(org.apache.twill.api.TwillRunner) RunRecordMeta(co.cask.cdap.internal.app.store.RunRecordMeta) ProgramId(co.cask.cdap.proto.id.ProgramId) Predicate(com.google.common.base.Predicate) TwillController(org.apache.twill.api.TwillController) RunId(org.apache.twill.api.RunId) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 2 with TwillController

use of org.apache.twill.api.TwillController in project cdap by caskdata.

the class DistributedProgramRuntimeService method lookup.

@Override
public synchronized RuntimeInfo lookup(ProgramId programId, final RunId runId) {
    RuntimeInfo runtimeInfo = super.lookup(programId, runId);
    if (runtimeInfo != null) {
        return runtimeInfo;
    }
    // Goes through all live application and fill the twillProgramInfo table
    for (TwillRunner.LiveInfo liveInfo : twillRunner.lookupLive()) {
        String appName = liveInfo.getApplicationName();
        ProgramId id = TwillAppNames.fromTwillAppName(appName, false);
        if (id == null) {
            continue;
        }
        if (!id.equals(programId)) {
            continue;
        }
        // Program matched
        RunRecordMeta record = store.getRun(programId, runId.getId());
        if (record == null) {
            return null;
        }
        if (record.getTwillRunId() == null) {
            LOG.warn("Twill RunId does not exist for the program {}, runId {}", programId, runId.getId());
            return null;
        }
        RunId twillRunIdFromRecord = org.apache.twill.internal.RunIds.fromString(record.getTwillRunId());
        for (TwillController controller : liveInfo.getControllers()) {
            RunId twillRunId = controller.getRunId();
            if (!twillRunId.equals(twillRunIdFromRecord)) {
                continue;
            }
            runtimeInfo = createRuntimeInfo(programId, controller, runId);
            if (runtimeInfo != null) {
                updateRuntimeInfo(programId.getType(), runId, runtimeInfo);
            } else {
                LOG.warn("Unable to find program for runId {}", runId);
            }
            return runtimeInfo;
        }
    }
    return null;
}
Also used : TwillController(org.apache.twill.api.TwillController) SimpleRuntimeInfo(co.cask.cdap.internal.app.runtime.service.SimpleRuntimeInfo) TwillRunner(org.apache.twill.api.TwillRunner) RunRecordMeta(co.cask.cdap.internal.app.store.RunRecordMeta) ProgramId(co.cask.cdap.proto.id.ProgramId) RunId(org.apache.twill.api.RunId)

Example 3 with TwillController

use of org.apache.twill.api.TwillController in project cdap by caskdata.

the class AbstractDistributedMasterServiceManager method getInstances.

@Override
public int getInstances() {
    Iterable<TwillController> twillControllerList = twillRunnerService.lookup(Constants.Service.MASTER_SERVICES);
    int instances = 0;
    if (twillControllerList != null) {
        for (TwillController twillController : twillControllerList) {
            if (twillController.getResourceReport() != null) {
                instances = twillController.getResourceReport().getRunnableResources(serviceName).size();
            }
        }
    }
    return instances;
}
Also used : TwillController(org.apache.twill.api.TwillController)

Example 4 with TwillController

use of org.apache.twill.api.TwillController in project cdap by caskdata.

the class AbstractDistributedMasterServiceManager method getLiveInfo.

@Override
public SystemServiceLiveInfo getLiveInfo() {
    SystemServiceLiveInfo.Builder builder = SystemServiceLiveInfo.builder();
    Iterable<TwillController> twillControllerList = twillRunnerService.lookup(Constants.Service.MASTER_SERVICES);
    if (twillControllerList == null) {
        return builder.build();
    }
    for (TwillController twillController : twillControllerList) {
        if (twillController.getResourceReport() == null) {
            continue;
        }
        ResourceReport resourceReport = twillController.getResourceReport();
        Collection<TwillRunResources> runResources = resourceReport.getResources().get(serviceName);
        for (TwillRunResources resources : runResources) {
            Containers.ContainerInfo containerInfo = new Containers.ContainerInfo(Containers.ContainerType.SYSTEM_SERVICE, serviceName, resources.getInstanceId(), resources.getContainerId(), resources.getHost(), resources.getMemoryMB(), resources.getVirtualCores(), resources.getDebugPort());
            builder.addContainer(containerInfo);
        }
    }
    return builder.build();
}
Also used : TwillController(org.apache.twill.api.TwillController) SystemServiceLiveInfo(co.cask.cdap.proto.SystemServiceLiveInfo) Containers(co.cask.cdap.proto.Containers) ResourceReport(org.apache.twill.api.ResourceReport) TwillRunResources(org.apache.twill.api.TwillRunResources)

Example 5 with TwillController

use of org.apache.twill.api.TwillController in project cdap by caskdata.

the class DistributedProgramRuntimeService method getLiveInfo.

@Override
public ProgramLiveInfo getLiveInfo(ProgramId program) {
    String twillAppName = TwillAppNames.toTwillAppName(program);
    Iterator<TwillController> controllers = twillRunner.lookup(twillAppName).iterator();
    // this will return an empty Json if there is no live instance
    if (controllers.hasNext()) {
        TwillController controller = controllers.next();
        if (controllers.hasNext()) {
            LOG.warn("Expected at most one live instance of Twill app {} but found at least two.", twillAppName);
        }
        ResourceReport report = controller.getResourceReport();
        if (report != null) {
            DistributedProgramLiveInfo liveInfo = new DistributedProgramLiveInfo(program, report.getApplicationId());
            // if program type is flow then the container type is flowlet.
            Containers.ContainerType containerType = ProgramType.FLOW.equals(program.getType()) ? FLOWLET : Containers.ContainerType.valueOf(program.getType().name());
            for (Map.Entry<String, Collection<TwillRunResources>> entry : report.getResources().entrySet()) {
                for (TwillRunResources resources : entry.getValue()) {
                    liveInfo.addContainer(new ContainerInfo(containerType, entry.getKey(), resources.getInstanceId(), resources.getContainerId(), resources.getHost(), resources.getMemoryMB(), resources.getVirtualCores(), resources.getDebugPort()));
                }
            }
            // Add a list of announced services and their discoverables to the liveInfo.
            liveInfo.addServices(report.getServices());
            return liveInfo;
        }
    }
    return new NotRunningProgramLiveInfo(program);
}
Also used : Containers(co.cask.cdap.proto.Containers) TwillController(org.apache.twill.api.TwillController) NotRunningProgramLiveInfo(co.cask.cdap.proto.NotRunningProgramLiveInfo) DistributedProgramLiveInfo(co.cask.cdap.proto.DistributedProgramLiveInfo) ContainerInfo(co.cask.cdap.proto.Containers.ContainerInfo) Collection(java.util.Collection) ResourceReport(org.apache.twill.api.ResourceReport) TwillRunResources(org.apache.twill.api.TwillRunResources) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap)

Aggregations

TwillController (org.apache.twill.api.TwillController)6 Map (java.util.Map)3 SimpleRuntimeInfo (co.cask.cdap.internal.app.runtime.service.SimpleRuntimeInfo)2 RunRecordMeta (co.cask.cdap.internal.app.store.RunRecordMeta)2 Containers (co.cask.cdap.proto.Containers)2 ProgramId (co.cask.cdap.proto.id.ProgramId)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 ResourceReport (org.apache.twill.api.ResourceReport)2 RunId (org.apache.twill.api.RunId)2 TwillRunResources (org.apache.twill.api.TwillRunResources)2 TwillRunner (org.apache.twill.api.TwillRunner)2 ProgramDescriptor (co.cask.cdap.app.program.ProgramDescriptor)1 ProgramController (co.cask.cdap.app.runtime.ProgramController)1 ProgramOptions (co.cask.cdap.app.runtime.ProgramOptions)1 MainClassLoader (co.cask.cdap.common.app.MainClassLoader)1 CConfiguration (co.cask.cdap.common.conf.CConfiguration)1 CombineClassLoader (co.cask.cdap.common.lang.CombineClassLoader)1 LoggerLogHandler (co.cask.cdap.common.logging.LoggerLogHandler)1 HBaseDDLExecutorFactory (co.cask.cdap.data2.util.hbase.HBaseDDLExecutorFactory)1 SimpleProgramOptions (co.cask.cdap.internal.app.runtime.SimpleProgramOptions)1