Package com.cloudera.iterativereduce.yarn

Source Code of com.cloudera.iterativereduce.yarn.ContainerManagerHandler

package com.cloudera.iterativereduce.yarn;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.net.NetUtils;
import org.apache.hadoop.security.UserGroupInformation;
import org.apache.hadoop.yarn.api.ContainerManager;
import org.apache.hadoop.yarn.api.protocolrecords.GetContainerStatusRequest;
import org.apache.hadoop.yarn.api.protocolrecords.GetContainerStatusResponse;
import org.apache.hadoop.yarn.api.protocolrecords.StartContainerRequest;
import org.apache.hadoop.yarn.api.protocolrecords.StartContainerResponse;
import org.apache.hadoop.yarn.api.records.Container;
import org.apache.hadoop.yarn.api.records.ContainerLaunchContext;
import org.apache.hadoop.yarn.api.records.LocalResource;
import org.apache.hadoop.yarn.conf.YarnConfiguration;
import org.apache.hadoop.yarn.exceptions.YarnRemoteException;
import org.apache.hadoop.yarn.ipc.YarnRPC;
import org.apache.hadoop.yarn.util.Records;

public class ContainerManagerHandler {

  private static final Log LOG = LogFactory.getLog(ContainerManagerHandler.class);
 
  private Configuration conf;
  private Container container;
  private ContainerManager containerManager;
 
  public ContainerManagerHandler (Configuration conf, Container container) {
    this.conf = conf;
    this.container = container;
  }
 
  public ContainerManager getContainerManager() {
    if (containerManager != null)
      return containerManager;
   
    YarnConfiguration yarnConf = new YarnConfiguration(conf);
    YarnRPC rpc = YarnRPC.create(yarnConf);
   
    InetSocketAddress cmAddr = NetUtils.createSocketAddr(container.getNodeId()
        .getHost(), container.getNodeId().getPort());
   
    LOG.info("Connecting to container manager at " + cmAddr);
    containerManager = ((ContainerManager) rpc.getProxy(ContainerManager.class,
        cmAddr, conf));
  
    return containerManager;
  }
 
  public StartContainerResponse startContainer(List<String> commands,
      Map<String, LocalResource> localResources, Map<String, String> env) throws IOException {

    if (containerManager == null)
      throw new IllegalStateException(
          "Cannot start a continer before connecting to the container manager!");

    ContainerLaunchContext ctx = Records.newRecord(ContainerLaunchContext.class);
    ctx.setContainerId(container.getId());
    ctx.setResource(container.getResource());
    ctx.setLocalResources(localResources);
    ctx.setCommands(commands);
    ctx.setUser(UserGroupInformation.getCurrentUser().getShortUserName());
    ctx.setEnvironment(env);

    if (LOG.isDebugEnabled()) {
      LOG.debug("Using ContainerLaunchContext with"
          + ", containerId=" + ctx.getContainerId()
          + ", memory=" + ctx.getResource().getMemory()
          + ", localResources=" + ctx.getLocalResources().toString()
          + ", commands=" + ctx.getCommands().toString()
          + ", env=" + ctx.getEnvironment().toString());
    }
   
    StartContainerRequest request = Records.newRecord(StartContainerRequest.class);
    request.setContainerLaunchContext(ctx);
   
    LOG.info("Starting container, containerId=" + container.getId().toString()
        + ", host=" + container.getNodeId().getHost()
        + ", http=" + container.getNodeHttpAddress());
   
    return containerManager.startContainer(request);
  }
 
  public GetContainerStatusResponse getContainerStatus()
      throws YarnRemoteException {

    if (containerManager == null)
      throw new IllegalStateException(
          "Cannot request container status without connecting to container manager!");

    GetContainerStatusRequest request = Records.newRecord(GetContainerStatusRequest.class);
    request.setContainerId(container.getId());
   
    LOG.info("Getting container statusr, containerId=" + container.getId().toString());
    return containerManager.getContainerStatus(request);
  }
}
TOP

Related Classes of com.cloudera.iterativereduce.yarn.ContainerManagerHandler

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.