use of com.sequenceiq.cloudbreak.cloud.yarn.client.YarnClient in project cloudbreak by hortonworks.
the class YarnResourceConnector method check.
@Override
public List<CloudResourceStatus> check(AuthenticatedContext authenticatedContext, List<CloudResource> resources) {
YarnClient yarnClient = yarnClientUtil.createYarnClient(authenticatedContext);
List<CloudResourceStatus> result = new ArrayList<>();
for (CloudResource resource : resources) {
switch(resource.getType()) {
case YARN_APPLICATION:
LOGGER.info("Checking Yarn application status of: {}", resource.getName());
try {
ApplicationDetailRequest applicationDetailRequest = new ApplicationDetailRequest();
applicationDetailRequest.setName(resource.getName());
ResponseContext responseContext = yarnClient.getApplicationDetail(applicationDetailRequest);
if (responseContext.getStatusCode() == YarnResourceConstants.HTTP_SUCCESS) {
ApplicationDetailResponse applicationDetailResponse = (ApplicationDetailResponse) responseContext.getResponseObject();
result.add(new CloudResourceStatus(resource, YarnApplicationStatus.mapResourceStatus(applicationDetailResponse.getState())));
} else if (responseContext.getStatusCode() == YarnResourceConstants.HTTP_NOT_FOUND) {
result.add(new CloudResourceStatus(resource, ResourceStatus.DELETED, "Yarn application has been killed."));
} else if (responseContext.getResponseError() != null) {
throw new CloudConnectorException(String.format("Yarn Application status check failed: HttpStatusCode: %d, Error: %s", responseContext.getStatusCode(), responseContext.getResponseError().getDiagnostics()));
} else {
throw new CloudConnectorException(String.format("Yarn Application status check failed: Invalid HttpStatusCode: %d", responseContext.getStatusCode()));
}
} catch (MalformedURLException | RuntimeException e) {
throw new CloudConnectorException(String.format("Invalid resource exception: %s", e.getMessage()), e);
}
break;
default:
throw new CloudConnectorException(String.format("Invalid resource type: %s", resource.getType()));
}
}
return result;
}
use of com.sequenceiq.cloudbreak.cloud.yarn.client.YarnClient in project cloudbreak by hortonworks.
the class YarnResourceConnector method terminate.
@Override
public List<CloudResourceStatus> terminate(AuthenticatedContext authenticatedContext, CloudStack stack, List<CloudResource> cloudResources) {
for (CloudResource resource : cloudResources) {
switch(resource.getType()) {
case YARN_APPLICATION:
YarnClient yarnClient = yarnClientUtil.createYarnClient(authenticatedContext);
String yarnApplicationName = resource.getName();
String stackName = authenticatedContext.getCloudContext().getName();
LOGGER.info("Terminate stack: {}", stackName);
try {
DeleteApplicationRequest deleteApplicationRequest = new DeleteApplicationRequest();
deleteApplicationRequest.setName(yarnApplicationName);
yarnClient.deleteApplication(deleteApplicationRequest);
LOGGER.info("Yarn Applicatin has been deleted");
} catch (MalformedURLException | YarnClientException e) {
throw new CloudConnectorException("Stack cannot be deleted", e);
}
break;
default:
throw new CloudConnectorException(String.format("Invalid resource type: %s", resource.getType()));
}
}
return check(authenticatedContext, cloudResources);
}
use of com.sequenceiq.cloudbreak.cloud.yarn.client.YarnClient in project cloudbreak by hortonworks.
the class YarnResourceConnector method launch.
@Override
public List<CloudResourceStatus> launch(AuthenticatedContext authenticatedContext, CloudStack stack, PersistenceNotifier persistenceNotifier, AdjustmentType adjustmentType, Long threshold) throws Exception {
CreateApplicationRequest createApplicationRequest = new CreateApplicationRequest();
createApplicationRequest.setName(createApplicationName(authenticatedContext));
createApplicationRequest.setQueue(stack.getParameters().getOrDefault(YarnConstants.YARN_QUEUE_PARAMETER, defaultQueue));
String lifeTimeStr = stack.getParameters().get(YarnConstants.YARN_LIFETIME_PARAMETER);
createApplicationRequest.setLifetime(lifeTimeStr != null ? Integer.parseInt(lifeTimeStr) : defaultLifeTime);
Artifact artifact = new Artifact();
artifact.setId(stack.getImage().getImageName());
artifact.setType("DOCKER");
List<YarnComponent> components = new ArrayList<>();
for (Group group : stack.getGroups()) {
YarnComponent component = new YarnComponent();
component.setName(group.getName());
component.setNumberOfContainers(group.getInstancesSize());
String userData = stack.getImage().getUserDataByType(group.getType());
component.setLaunchCommand(String.format("/bootstrap/start-systemd '%s' '%s' '%s'", Base64.getEncoder().encodeToString(userData.getBytes()), stack.getLoginUserName(), stack.getPublicKey()));
component.setArtifact(artifact);
component.setDependencies(new ArrayList<>());
InstanceTemplate instanceTemplate = group.getReferenceInstanceConfiguration().getTemplate();
Resource resource = new Resource();
resource.setCpus(instanceTemplate.getParameter(PlatformParametersConsts.CUSTOM_INSTANCETYPE_CPUS, Integer.class));
resource.setMemory(instanceTemplate.getParameter(PlatformParametersConsts.CUSTOM_INSTANCETYPE_MEMORY, Integer.class));
component.setResource(resource);
component.setRunPrivilegedContainer(true);
Configuration configuration = new Configuration();
Map<String, String> propsMap = Maps.newHashMap();
propsMap.put("conf.cb-conf.per.component", "true");
propsMap.put("site.cb-conf.userData", '\'' + Base64.getEncoder().encodeToString(userData.getBytes()) + '\'');
propsMap.put("site.cb-conf.sshUser", '\'' + stack.getLoginUserName() + '\'');
propsMap.put("site.cb-conf.groupname", '\'' + group.getName() + '\'');
propsMap.put("site.cb-conf.sshPubKey", '\'' + stack.getPublicKey() + '\'');
configuration.setProperties(propsMap);
ConfigFile configFileProps = new ConfigFile();
configFileProps.setType(ConfigFileType.PROPERTIES.name());
configFileProps.setSrcFile("cb-conf");
configFileProps.setDestFile("/etc/cloudbreak-config.props");
configuration.setFiles(Collections.singletonList(configFileProps));
component.setConfiguration(configuration);
components.add(component);
}
createApplicationRequest.setComponents(components);
CloudResource yarnApplication = new Builder().type(YARN_APPLICATION).name(createApplicationRequest.getName()).build();
persistenceNotifier.notifyAllocation(yarnApplication, authenticatedContext.getCloudContext());
YarnClient yarnClient = yarnClientUtil.createYarnClient(authenticatedContext);
ResponseContext responseContext = yarnClient.createApplication(createApplicationRequest);
if (responseContext.getResponseError() != null) {
ApplicationErrorResponse applicationErrorResponse = responseContext.getResponseError();
throw new CloudConnectorException(String.format("Yarn Application creation error: HTTP Return: %d Error: %s", responseContext.getStatusCode(), applicationErrorResponse.getDiagnostics()));
}
return check(authenticatedContext, Collections.singletonList(yarnApplication));
}
use of com.sequenceiq.cloudbreak.cloud.yarn.client.YarnClient in project cloudbreak by hortonworks.
the class YarnMetadataCollector method collect.
@Override
public List<CloudVmMetaDataStatus> collect(AuthenticatedContext authenticatedContext, List<CloudResource> resources, List<CloudInstance> vms) {
try {
YarnClient yarnClient = yarnClientUtil.createYarnClient(authenticatedContext);
CloudResource yarnApplication = getYarnApplcationResource(resources);
ApplicationDetailRequest applicationDetailRequest = new ApplicationDetailRequest();
applicationDetailRequest.setName(yarnApplication.getName());
ResponseContext responseContext = yarnClient.getApplicationDetail(applicationDetailRequest);
if (responseContext.getStatusCode() == YarnResourceConstants.HTTP_SUCCESS) {
ApplicationDetailResponse applicationDetailResponse = (ApplicationDetailResponse) responseContext.getResponseObject();
ListMultimap<String, CloudInstance> groupInstancesByInstanceGroup = groupInstancesByInstanceGroup(vms);
ListMultimap<String, Container> groupContainersByInstanceGroup = groupContainersByInstanceGroup(applicationDetailResponse.getContainers());
List<CloudVmMetaDataStatus> cloudVmMetaDataStatuses = Lists.newArrayList();
for (String groupName : groupContainersByInstanceGroup.keySet()) {
List<CloudInstance> groupInstances = groupInstancesByInstanceGroup.get(groupName);
List<Container> groupContainers = groupContainersByInstanceGroup.get(groupName);
Map<String, CloudInstance> mapByInstanceId = mapByInstanceId(groupInstances);
Queue<CloudInstance> untrackedInstances = untrackedInstances(groupInstances);
for (Container container : groupContainers) {
String containerId = container.getId();
CloudInstance cloudInstance = mapByInstanceId.get(containerId);
if (cloudInstance == null) {
if (!untrackedInstances.isEmpty()) {
cloudInstance = untrackedInstances.remove();
cloudInstance = new CloudInstance(containerId, cloudInstance.getTemplate(), cloudInstance.getAuthentication());
}
}
if (cloudInstance != null) {
String ipAddress = container.getIp();
CloudInstanceMetaData md = new CloudInstanceMetaData(ipAddress, ipAddress);
CloudVmInstanceStatus cloudVmInstanceStatus = new CloudVmInstanceStatus(cloudInstance, InstanceStatus.CREATED);
CloudVmMetaDataStatus cloudVmMetaDataStatus = new CloudVmMetaDataStatus(cloudVmInstanceStatus, md);
cloudVmMetaDataStatuses.add(cloudVmMetaDataStatus);
}
}
}
return cloudVmMetaDataStatuses;
} else {
ApplicationErrorResponse errorResponse = responseContext.getResponseError();
throw new CloudConnectorException(String.format("Failed to get yarn application details: HTTP Return: %d Error: %s", responseContext.getStatusCode(), errorResponse == null ? "unknown" : errorResponse.getDiagnostics()));
}
} catch (MalformedURLException ex) {
throw new CloudConnectorException("Failed to get yarn application details", ex);
}
}
Aggregations