Search in sources :

Example 1 with DistributedProgramLiveInfo

use of co.cask.cdap.proto.DistributedProgramLiveInfo in project cdap by caskdata.

the class ProgramClient method getLiveInfo.

/**
   * Gets the live information of a program. In distributed CDAP,
   * this will contain IDs of the YARN applications that are running the program.
   *
   * @param program the program
   * @return {@link ProgramLiveInfo} of the program
   * @throws IOException if a network error occurred
   * @throws ProgramNotFoundException if the program with the specified name could not be found
   * @throws UnauthenticatedException if the request is not authorized successfully in the gateway server
   */
public DistributedProgramLiveInfo getLiveInfo(ProgramId program) throws IOException, ProgramNotFoundException, UnauthenticatedException, UnauthorizedException {
    String path = String.format("apps/%s/%s/%s/live-info", program.getApplication(), program.getType().getCategoryName(), program.getProgram());
    URL url = config.resolveNamespacedURLV3(program.getNamespaceId(), path);
    HttpResponse response = restClient.execute(HttpMethod.GET, url, config.getAccessToken(), HttpURLConnection.HTTP_NOT_FOUND);
    if (response.getResponseCode() == HttpURLConnection.HTTP_NOT_FOUND) {
        throw new ProgramNotFoundException(program);
    }
    return ObjectResponse.fromJsonBody(response, DistributedProgramLiveInfo.class).getResponseObject();
}
Also used : DistributedProgramLiveInfo(co.cask.cdap.proto.DistributedProgramLiveInfo) HttpResponse(co.cask.common.http.HttpResponse) ProgramNotFoundException(co.cask.cdap.common.ProgramNotFoundException) URL(java.net.URL)

Example 2 with DistributedProgramLiveInfo

use of co.cask.cdap.proto.DistributedProgramLiveInfo 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)

Example 3 with DistributedProgramLiveInfo

use of co.cask.cdap.proto.DistributedProgramLiveInfo in project cdap by caskdata.

the class GetProgramLiveInfoCommand method perform.

@Override
public void perform(Arguments arguments, PrintStream output) throws Exception {
    String[] programIdParts = arguments.get(elementType.getArgumentName().toString()).split("\\.");
    if (programIdParts.length < 2) {
        throw new CommandInputError(this);
    }
    String appId = programIdParts[0];
    String programName = programIdParts[1];
    ProgramId program = cliConfig.getCurrentNamespace().app(appId).program(elementType.getProgramType(), programName);
    DistributedProgramLiveInfo liveInfo = programClient.getLiveInfo(program);
    if (liveInfo == null) {
        output.println("No live info found");
        return;
    }
    Table table = Table.builder().setHeader("app", "type", "id", "runtime", "yarn app id").setRows(ImmutableList.of(liveInfo), new RowMaker<DistributedProgramLiveInfo>() {

        @Override
        public List<?> makeRow(DistributedProgramLiveInfo object) {
            return Lists.newArrayList(object.getApp(), object.getType(), object.getName(), object.getRuntime(), object.getYarnAppId());
        }
    }).build();
    cliConfig.getTableRenderer().render(cliConfig, output, table);
    if (liveInfo.getContainers() != null) {
        Table containersTable = Table.builder().setHeader("containers", "instance", "host", "container", "memory", "virtual cores", "debug port").setRows(liveInfo.getContainers(), new RowMaker<Containers.ContainerInfo>() {

            @Override
            public List<?> makeRow(Containers.ContainerInfo object) {
                return Lists.newArrayList("", object.getInstance(), object.getHost(), object.getContainer(), object.getMemory(), object.getVirtualCores(), object.getDebugPort());
            }
        }).build();
        cliConfig.getTableRenderer().render(cliConfig, output, containersTable);
    }
}
Also used : CommandInputError(co.cask.cdap.cli.exception.CommandInputError) Table(co.cask.cdap.cli.util.table.Table) DistributedProgramLiveInfo(co.cask.cdap.proto.DistributedProgramLiveInfo) RowMaker(co.cask.cdap.cli.util.RowMaker) Containers(co.cask.cdap.proto.Containers) ProgramId(co.cask.cdap.proto.id.ProgramId)

Aggregations

DistributedProgramLiveInfo (co.cask.cdap.proto.DistributedProgramLiveInfo)3 Containers (co.cask.cdap.proto.Containers)2 CommandInputError (co.cask.cdap.cli.exception.CommandInputError)1 RowMaker (co.cask.cdap.cli.util.RowMaker)1 Table (co.cask.cdap.cli.util.table.Table)1 ProgramNotFoundException (co.cask.cdap.common.ProgramNotFoundException)1 ContainerInfo (co.cask.cdap.proto.Containers.ContainerInfo)1 NotRunningProgramLiveInfo (co.cask.cdap.proto.NotRunningProgramLiveInfo)1 ProgramId (co.cask.cdap.proto.id.ProgramId)1 HttpResponse (co.cask.common.http.HttpResponse)1 ImmutableMap (com.google.common.collect.ImmutableMap)1 URL (java.net.URL)1 Collection (java.util.Collection)1 Map (java.util.Map)1 ResourceReport (org.apache.twill.api.ResourceReport)1 TwillController (org.apache.twill.api.TwillController)1 TwillRunResources (org.apache.twill.api.TwillRunResources)1