Search in sources :

Example 1 with DistributedProgramLiveInfo

use of io.cdap.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(io.cdap.cdap.proto.DistributedProgramLiveInfo) HttpResponse(io.cdap.common.http.HttpResponse) ProgramNotFoundException(io.cdap.cdap.common.ProgramNotFoundException) URL(java.net.URL)

Example 2 with DistributedProgramLiveInfo

use of io.cdap.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());
            Containers.ContainerType containerType = 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 super.getLiveInfo(program);
}
Also used : TwillController(org.apache.twill.api.TwillController) DistributedProgramLiveInfo(io.cdap.cdap.proto.DistributedProgramLiveInfo) Containers(io.cdap.cdap.proto.Containers) ContainerInfo(io.cdap.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) HashMap(java.util.HashMap)

Example 3 with DistributedProgramLiveInfo

use of io.cdap.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(io.cdap.cdap.cli.exception.CommandInputError) Table(io.cdap.cdap.cli.util.table.Table) DistributedProgramLiveInfo(io.cdap.cdap.proto.DistributedProgramLiveInfo) RowMaker(io.cdap.cdap.cli.util.RowMaker) Containers(io.cdap.cdap.proto.Containers) ProgramId(io.cdap.cdap.proto.id.ProgramId)

Aggregations

DistributedProgramLiveInfo (io.cdap.cdap.proto.DistributedProgramLiveInfo)3 Containers (io.cdap.cdap.proto.Containers)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 CommandInputError (io.cdap.cdap.cli.exception.CommandInputError)1 RowMaker (io.cdap.cdap.cli.util.RowMaker)1 Table (io.cdap.cdap.cli.util.table.Table)1 ProgramNotFoundException (io.cdap.cdap.common.ProgramNotFoundException)1 ContainerInfo (io.cdap.cdap.proto.Containers.ContainerInfo)1 ProgramId (io.cdap.cdap.proto.id.ProgramId)1 HttpResponse (io.cdap.common.http.HttpResponse)1 URL (java.net.URL)1 Collection (java.util.Collection)1 HashMap (java.util.HashMap)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