Example usage for java.net InetSocketAddress getHostString

List of usage examples for java.net InetSocketAddress getHostString

Introduction

In this page you can find the example usage for java.net InetSocketAddress getHostString.

Prototype

public final String getHostString() 

Source Link

Document

Returns the hostname, or the String form of the address if it doesn't have a hostname (it was created using a literal).

Usage

From source file:org.ireland.jnetty.http.HttpServletRequestImpl.java

@Override
// OK/* ww w . j  av  a  2s .  c o m*/
public String getRemoteHost() {
    InetSocketAddress remote = _remote;
    if (remote == null)
        remote = socketChannel.remoteAddress();
    return remote == null ? "" : remote.getHostString();
}

From source file:org.ireland.jnetty.http.HttpServletRequestImpl.java

@Override
// OK//from  ww  w. j  av  a 2 s  . c  om
public String getLocalAddr() {
    InetSocketAddress local = socketChannel.localAddress();
    if (local == null)
        return "";
    InetAddress address = local.getAddress();
    if (address == null)
        return local.getHostString();
    return address.getHostAddress();
}

From source file:org.ireland.jnetty.http.HttpServletRequestImpl.java

@Override
// OK/*from w w  w  .  j a v a 2s .  co  m*/
public String getRemoteAddr() {
    InetSocketAddress remote = _remote;
    if (remote == null)
        remote = socketChannel.remoteAddress();

    if (remote == null)
        return "";

    InetAddress address = remote.getAddress();
    if (address == null)
        return remote.getHostString();

    return address.getHostAddress();
}

From source file:org.apache.slider.server.appmaster.SliderAppMaster.java

/**
 * Create and run the cluster.//from  w  w  w. j av  a 2  s.c om
 * @param clustername cluster name
 * @return exit code
 * @throws Throwable on a failure
 */
private int createAndRunCluster(String clustername) throws Throwable {

    //load the cluster description from the cd argument
    String sliderClusterDir = serviceArgs.getSliderClusterURI();
    URI sliderClusterURI = new URI(sliderClusterDir);
    Path clusterDirPath = new Path(sliderClusterURI);
    log.info("Application defined at {}", sliderClusterURI);
    SliderFileSystem fs = getClusterFS();

    // build up information about the running application -this
    // will be passed down to the cluster status
    MapOperations appInformation = new MapOperations();

    AggregateConf instanceDefinition = InstanceIO.loadInstanceDefinitionUnresolved(fs, clusterDirPath);
    instanceDefinition.setName(clustername);

    log.info("Deploying cluster {}:", instanceDefinition);

    // and resolve it
    AggregateConf resolvedInstance = new AggregateConf(instanceDefinition);
    resolvedInstance.resolve();

    stateForProviders.setApplicationName(clustername);

    Configuration serviceConf = getConfig();

    // extend AM configuration with component resource
    MapOperations amConfiguration = resolvedInstance.getAppConfOperations().getComponent(COMPONENT_AM);
    // and patch configuration with prefix
    if (amConfiguration != null) {
        Map<String, String> sliderAppConfKeys = amConfiguration.prefixedWith("slider.");
        for (Map.Entry<String, String> entry : sliderAppConfKeys.entrySet()) {
            String k = entry.getKey();
            String v = entry.getValue();
            boolean exists = serviceConf.get(k) != null;
            log.info("{} {} to {}", (exists ? "Overwriting" : "Setting"), k, v);
            serviceConf.set(k, v);
        }
    }

    securityConfiguration = new SecurityConfiguration(serviceConf, resolvedInstance, clustername);
    // obtain security state
    securityEnabled = securityConfiguration.isSecurityEnabled();
    // set the global security flag for the instance definition
    instanceDefinition.getAppConfOperations().set(KEY_SECURITY_ENABLED, securityEnabled);

    // triggers resolution and snapshotting for agent
    appState.setInitialInstanceDefinition(instanceDefinition);

    File confDir = getLocalConfDir();
    if (!confDir.exists() || !confDir.isDirectory()) {
        log.info("Conf dir {} does not exist.", confDir);
        File parentFile = confDir.getParentFile();
        log.info("Parent dir {}:\n{}", parentFile, SliderUtils.listDir(parentFile));
    }

    //get our provider
    MapOperations globalInternalOptions = getGlobalInternalOptions();
    String providerType = globalInternalOptions.getMandatoryOption(InternalKeys.INTERNAL_PROVIDER_NAME);
    log.info("Cluster provider type is {}", providerType);
    SliderProviderFactory factory = SliderProviderFactory.createSliderProviderFactory(providerType);
    providerService = factory.createServerProvider();
    // init the provider BUT DO NOT START IT YET
    initAndAddService(providerService);
    providerRMOperationHandler = new ProviderNotifyingOperationHandler(providerService);

    // create a slider AM provider
    sliderAMProvider = new SliderAMProviderService();
    initAndAddService(sliderAMProvider);

    InetSocketAddress rmSchedulerAddress = SliderUtils.getRmSchedulerAddress(serviceConf);
    log.info("RM is at {}", rmSchedulerAddress);
    yarnRPC = YarnRPC.create(serviceConf);

    // set up the YARN client. This may require patching in the RM client-API address if it
    // is (somehow) unset server-side.    String clientRMaddr = serviceConf.get(YarnConfiguration.RM_ADDRESS);
    InetSocketAddress clientRpcAddress = SliderUtils.getRmAddress(serviceConf);
    if (!SliderUtils.isAddressDefined(clientRpcAddress)) {
        // client addr is being unset. We can lift it from the other RM APIs
        log.warn("Yarn RM address was unbound; attempting to fix up");
        serviceConf.set(YarnConfiguration.RM_ADDRESS,
                String.format("%s:%d", rmSchedulerAddress.getHostString(), clientRpcAddress.getPort()));
    }

    /*
     * Extract the container ID. This is then
     * turned into an (incomplete) container
     */
    appMasterContainerID = ConverterUtils.toContainerId(
            SliderUtils.mandatoryEnvVariable(ApplicationConstants.Environment.CONTAINER_ID.name()));
    appAttemptID = appMasterContainerID.getApplicationAttemptId();

    ApplicationId appid = appAttemptID.getApplicationId();
    log.info("AM for ID {}", appid.getId());

    appInformation.put(StatusKeys.INFO_AM_CONTAINER_ID, appMasterContainerID.toString());
    appInformation.put(StatusKeys.INFO_AM_APP_ID, appid.toString());
    appInformation.put(StatusKeys.INFO_AM_ATTEMPT_ID, appAttemptID.toString());

    Map<String, String> envVars;
    List<Container> liveContainers;

    /*
     * It is critical this section is synchronized, to stop async AM events
     * arriving while registering a restarting AM.
     */
    synchronized (appState) {
        int heartbeatInterval = HEARTBEAT_INTERVAL;

        // add the RM client -this brings the callbacks in
        asyncRMClient = AMRMClientAsync.createAMRMClientAsync(heartbeatInterval, this);
        addService(asyncRMClient);
        //now bring it up
        deployChildService(asyncRMClient);

        // nmclient relays callbacks back to this class
        nmClientAsync = new NMClientAsyncImpl("nmclient", this);
        deployChildService(nmClientAsync);

        // set up secret manager
        secretManager = new ClientToAMTokenSecretManager(appAttemptID, null);

        if (securityEnabled) {
            // fix up the ACLs if they are not set
            String acls = serviceConf.get(KEY_PROTOCOL_ACL);
            if (acls == null) {
                getConfig().set(KEY_PROTOCOL_ACL, "*");
            }
        }

        certificateManager = new CertificateManager();

        //bring up the Slider RPC service
        buildPortScanner(instanceDefinition);
        startSliderRPCServer(instanceDefinition);

        rpcServiceAddress = rpcService.getConnectAddress();
        appMasterHostname = rpcServiceAddress.getAddress().getCanonicalHostName();
        appMasterRpcPort = rpcServiceAddress.getPort();
        appMasterTrackingUrl = null;
        log.info("AM Server is listening at {}:{}", appMasterHostname, appMasterRpcPort);
        appInformation.put(StatusKeys.INFO_AM_HOSTNAME, appMasterHostname);
        appInformation.set(StatusKeys.INFO_AM_RPC_PORT, appMasterRpcPort);

        log.info("Starting Yarn registry");
        registryOperations = startRegistryOperationsService();
        log.info(registryOperations.toString());

        //build the role map
        List<ProviderRole> providerRoles = new ArrayList<>(providerService.getRoles());
        providerRoles.addAll(SliderAMClientProvider.ROLES);

        // Start up the WebApp and track the URL for it
        MapOperations component = instanceDefinition.getAppConfOperations()
                .getComponent(SliderKeys.COMPONENT_AM);
        certificateManager.initialize(component, appMasterHostname, appMasterContainerID.toString(),
                clustername);
        certificateManager.setPassphrase(instanceDefinition.getPassphrase());

        if (component.getOptionBool(AgentKeys.KEY_AGENT_TWO_WAY_SSL_ENABLED, false)) {
            uploadServerCertForLocalization(clustername, fs);
        }

        // Web service endpoints: initialize
        WebAppApiImpl webAppApi = new WebAppApiImpl(stateForProviders, providerService, certificateManager,
                registryOperations, metricsAndMonitoring, actionQueues, this, contentCache);
        initAMFilterOptions(serviceConf);

        // start the agent web app
        startAgentWebApp(appInformation, serviceConf, webAppApi);
        int webAppPort = deployWebApplication(webAppApi);

        String scheme = WebAppUtils.HTTP_PREFIX;
        appMasterTrackingUrl = scheme + appMasterHostname + ":" + webAppPort;

        appInformation.put(StatusKeys.INFO_AM_WEB_URL, appMasterTrackingUrl + "/");
        appInformation.set(StatusKeys.INFO_AM_WEB_PORT, webAppPort);

        // *****************************************************
        // Register self with ResourceManager
        // This will start heartbeating to the RM
        // address = SliderUtils.getRmSchedulerAddress(asyncRMClient.getConfig());
        // *****************************************************
        log.info("Connecting to RM at {}; AM tracking URL={}", appMasterRpcPort, appMasterTrackingUrl);
        amRegistrationData = asyncRMClient.registerApplicationMaster(appMasterHostname, appMasterRpcPort,
                appMasterTrackingUrl);
        maximumResourceCapability = amRegistrationData.getMaximumResourceCapability();

        int minMemory = serviceConf.getInt(RM_SCHEDULER_MINIMUM_ALLOCATION_MB,
                DEFAULT_RM_SCHEDULER_MINIMUM_ALLOCATION_MB);
        // validate scheduler vcores allocation setting
        int minCores = serviceConf.getInt(RM_SCHEDULER_MINIMUM_ALLOCATION_VCORES,
                DEFAULT_RM_SCHEDULER_MINIMUM_ALLOCATION_VCORES);
        int maxMemory = maximumResourceCapability.getMemory();
        int maxCores = maximumResourceCapability.getVirtualCores();
        appState.setContainerLimits(minMemory, maxMemory, minCores, maxCores);

        // build the handler for RM request/release operations; this uses
        // the max value as part of its lookup
        rmOperationHandler = new AsyncRMOperationHandler(asyncRMClient, maximumResourceCapability);

        // set the RM-defined maximum cluster values
        appInformation.put(ResourceKeys.YARN_CORES, Integer.toString(maxCores));
        appInformation.put(ResourceKeys.YARN_MEMORY, Integer.toString(maxMemory));

        processAMCredentials(securityConfiguration);

        if (securityEnabled) {
            secretManager.setMasterKey(amRegistrationData.getClientToAMTokenMasterKey().array());
            applicationACLs = amRegistrationData.getApplicationACLs();

            //tell the server what the ACLs are
            rpcService.getServer().refreshServiceAcl(serviceConf, new SliderAMPolicyProvider());
            if (securityConfiguration.isKeytabProvided()) {
                // perform keytab based login to establish kerberos authenticated
                // principal.  Can do so now since AM registration with RM above required
                // tokens associated to principal
                String principal = securityConfiguration.getPrincipal();
                File localKeytabFile = securityConfiguration.getKeytabFile(instanceDefinition);
                // Now log in...
                login(principal, localKeytabFile);
                // obtain new FS reference that should be kerberos based and different
                // than the previously cached reference
                fs = new SliderFileSystem(serviceConf);
            }
        }

        // YARN client.
        // Important: this is only valid at startup, and must be executed within
        // the right UGI context. Use with care.
        SliderYarnClientImpl yarnClient = null;
        List<NodeReport> nodeReports;
        try {
            yarnClient = new SliderYarnClientImpl();
            yarnClient.init(getConfig());
            yarnClient.start();
            nodeReports = getNodeReports(yarnClient);
            log.info("Yarn node report count: {}", nodeReports.size());
            // look up the application itself -this is needed to get the proxied
            // URL of the AM, for registering endpoints.
            // this call must be made after the AM has registered itself, obviously
            ApplicationAttemptReport report = getApplicationAttemptReport(yarnClient);
            appMasterProxiedUrl = report.getTrackingUrl();
            if (SliderUtils.isUnset(appMasterProxiedUrl)) {
                log.warn("Proxied URL is not set in application report");
                appMasterProxiedUrl = appMasterTrackingUrl;
            }
        } finally {
            // at this point yarnClient is no longer needed.
            // stop it immediately
            ServiceOperations.stop(yarnClient);
            yarnClient = null;
        }

        // extract container list

        liveContainers = amRegistrationData.getContainersFromPreviousAttempts();

        //now validate the installation
        Configuration providerConf = providerService.loadProviderConfigurationInformation(confDir);

        providerService.initializeApplicationConfiguration(instanceDefinition, fs);

        providerService.validateApplicationConfiguration(instanceDefinition, confDir, securityEnabled);

        //determine the location for the role history data
        Path historyDir = new Path(clusterDirPath, HISTORY_DIR_NAME);

        //build the instance
        AppStateBindingInfo binding = new AppStateBindingInfo();
        binding.instanceDefinition = instanceDefinition;
        binding.serviceConfig = serviceConf;
        binding.publishedProviderConf = providerConf;
        binding.roles = providerRoles;
        binding.fs = fs.getFileSystem();
        binding.historyPath = historyDir;
        binding.liveContainers = liveContainers;
        binding.applicationInfo = appInformation;
        binding.releaseSelector = providerService.createContainerReleaseSelector();
        binding.nodeReports = nodeReports;
        appState.buildInstance(binding);

        providerService.rebuildContainerDetails(liveContainers, instanceDefinition.getName(),
                appState.getRolePriorityMap());

        // add the AM to the list of nodes in the cluster

        appState.buildAppMasterNode(appMasterContainerID, appMasterHostname, webAppPort,
                appMasterHostname + ":" + webAppPort);

        // build up environment variables that the AM wants set in every container
        // irrespective of provider and role.
        envVars = new HashMap<>();
        if (hadoop_user_name != null) {
            envVars.put(HADOOP_USER_NAME, hadoop_user_name);
        }
        String debug_kerberos = System.getenv(HADOOP_JAAS_DEBUG);
        if (debug_kerberos != null) {
            envVars.put(HADOOP_JAAS_DEBUG, debug_kerberos);
        }
    }
    String rolesTmpSubdir = appMasterContainerID.toString() + "/roles";

    String amTmpDir = globalInternalOptions.getMandatoryOption(InternalKeys.INTERNAL_AM_TMP_DIR);

    Path tmpDirPath = new Path(amTmpDir);
    Path launcherTmpDirPath = new Path(tmpDirPath, rolesTmpSubdir);
    fs.getFileSystem().mkdirs(launcherTmpDirPath);

    //launcher service
    launchService = new RoleLaunchService(actionQueues, providerService, fs, new Path(getGeneratedConfDir()),
            envVars, launcherTmpDirPath);

    deployChildService(launchService);

    appState.noteAMLaunched();

    //Give the provider access to the state, and AM
    providerService.bind(stateForProviders, actionQueues, liveContainers);
    sliderAMProvider.bind(stateForProviders, actionQueues, liveContainers);

    // chaos monkey
    maybeStartMonkey();

    // setup token renewal and expiry handling for long lived apps
    //    if (!securityConfiguration.isKeytabProvided() &&
    //        SliderUtils.isHadoopClusterSecure(getConfig())) {
    //      fsDelegationTokenManager = new FsDelegationTokenManager(actionQueues);
    //      fsDelegationTokenManager.acquireDelegationToken(getConfig());
    //    }

    // if not a secure cluster, extract the username -it will be
    // propagated to workers
    if (!UserGroupInformation.isSecurityEnabled()) {
        hadoop_user_name = System.getenv(HADOOP_USER_NAME);
        log.info(HADOOP_USER_NAME + "='{}'", hadoop_user_name);
    }
    service_user_name = RegistryUtils.currentUser();
    log.info("Registry service username ={}", service_user_name);

    // declare the cluster initialized
    log.info("Application Master Initialization Completed");
    initCompleted.set(true);

    scheduleFailureWindowResets(instanceDefinition.getResources());
    scheduleEscalation(instanceDefinition.getInternal());

    try {
        // schedule YARN Registry registration
        queue(new ActionRegisterServiceInstance(clustername, appid));

        // log the YARN and web UIs
        log.info("RM Webapp address {}", serviceConf.get(YarnConfiguration.RM_WEBAPP_ADDRESS));
        log.info("Slider webapp address {} proxied at {}", appMasterTrackingUrl, appMasterProxiedUrl);

        // Start the Slider AM provider
        sliderAMProvider.start();

        // launch the real provider; this is expected to trigger a callback that
        // starts the node review process
        launchProviderService(instanceDefinition, confDir);

        // start handling any scheduled events

        startQueueProcessing();

        //now block waiting to be told to exit the process
        waitForAMCompletionSignal();
    } catch (Exception e) {
        log.error("Exception : {}", e, e);
        // call the AM stop command as if it had been queued (but without
        // going via the queue, which may not have started
        onAMStop(new ActionStopSlider(e));
    }
    //shutdown time
    return finish();
}