Skip to content

Commit

Permalink
add yarn
Browse files Browse the repository at this point in the history
  • Loading branch information
jetoile committed Sep 2, 2017
1 parent 5de00ca commit 7b54557
Show file tree
Hide file tree
Showing 16 changed files with 1,006 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
public enum Component {
HDFS("hdfs", "fr.jetoile.hadoopunit.component.HdfsBootstrap", "hdfs.artifact"),
ZOOKEEPER("zookeeper", "fr.jetoile.hadoopunit.component.ZookeeperBootstrap", "zookeeper.artifact"),
YARN("yarn", "fr.jetoile.hadoopunit.component.YarnBootstrap", "yarn.artifact"),
ALLUXIO("alluxio", "fr.jetoile.hadoopunit.component.AlluxioBootstrap", "alluxio.artifact"),
HIVEMETA("hivemeta", "fr.jetoile.hadoopunit.component.HiveMetastoreBootstrap", "hivemeta.artifact"),
HIVESERVER2("hiveserver2", "fr.jetoile.hadoopunit.component.HiveServer2Bootstrap", "hiveserver2.artifact"),
Expand Down
11 changes: 11 additions & 0 deletions hadoop-unit-standalone/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@
<artifactId>hadoop-auth</artifactId>
<groupId>org.apache.hadoop</groupId>
</exclusion>
<exclusion>
<artifactId>hadoop-yarn-api</artifactId>
<groupId>org.apache.hadoop</groupId>
</exclusion>
</exclusions>
</dependency>

Expand Down Expand Up @@ -206,6 +210,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-yarn-api</artifactId>
<version>${hadoop.version}</version>
<scope>test</scope>
</dependency>

</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ elasticsearch.artifact=fr.jetoile.hadoop:hadoop-unit-elasticsearch:2.3-SNAPSHOT
neo4j.artifact=fr.jetoile.hadoop:hadoop-unit-neo4j:2.3-SNAPSHOT
knox.artifact=fr.jetoile.hadoop:hadoop-unit-knox:2.3-SNAPSHOT
redis.artifact=fr.jetoile.hadoop:hadoop-unit-redis:2.3-SNAPSHOT
yarn.artifact=fr.jetoile.hadoop:hadoop-unit-yarn:2.3-SNAPSHOT

maven.central.repo=https://repo.maven.apache.org/maven2/
maven.local.repo=/home/khanh/.m2/repository
Expand Down
5 changes: 3 additions & 2 deletions hadoop-unit-standalone/src/main/resources/hadoop.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
zookeeper=true
hdfs=true
alluxio=true
#alluxio=true
hivemeta=true
hiveserver2=true
#kafka=true
Expand All @@ -12,5 +12,6 @@ hiveserver2=true
#elasticsearch=true
#neo4j=true
#knox=true
redis=true
#redis=true
yarn=true

Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
import com.datastax.driver.core.Session;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.mongodb.*;
import fr.jetoile.hadoopunit.Component;
import fr.jetoile.hadoopunit.HadoopBootstrap;
import fr.jetoile.hadoopunit.HadoopUnitConfig;
import fr.jetoile.hadoopunit.exception.BootstrapException;
import fr.jetoile.hadoopunit.exception.NotFoundServiceException;
import fr.jetoile.hadoopunit.integrationtest.simpleyarnapp.Client;
import fr.jetoile.hadoopunit.test.alluxio.AlluxioUtils;
import fr.jetoile.hadoopunit.test.hdfs.HdfsUtils;
import fr.jetoile.hadoopunit.test.kafka.KafkaConsumerUtils;
Expand Down Expand Up @@ -70,6 +73,9 @@
import java.net.*;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
Expand Down Expand Up @@ -787,5 +793,28 @@ public void neo4jShouldStartWithRealDriver() {
assertEquals("Arthur", results.get(0).get("name").asString());
}

@Test
public void testYarnLocalClusterIntegrationTest() {

String[] args = new String[7];
args[0] = "whoami";
args[1] = "1";
args[2] = getClass().getClassLoader().getResource("simple-yarn-app-1.1.0.jar").toString();
args[3] = configuration.getString(HadoopUnitConfig.YARN_RESOURCE_MANAGER_ADDRESS_KEY);
args[4] = configuration.getString(HadoopUnitConfig.YARN_RESOURCE_MANAGER_HOSTNAME_KEY);
args[5] = configuration.getString(HadoopUnitConfig.YARN_RESOURCE_MANAGER_SCHEDULER_ADDRESS_KEY);
args[6] = configuration.getString(HadoopUnitConfig.YARN_RESOURCE_MANAGER_RESOURCE_TRACKER_ADDRESS_KEY);


try {
Client.main(args);
} catch(Exception e) {
e.printStackTrace();
}


}


}

Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package fr.jetoile.hadoopunit.integrationtest.simpleyarnapp;

import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.fs.FileStatus;
import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.apache.hadoop.yarn.api.ApplicationConstants;
import org.apache.hadoop.yarn.api.ApplicationConstants.Environment;
import org.apache.hadoop.yarn.api.records.*;
import org.apache.hadoop.yarn.client.api.YarnClient;
import org.apache.hadoop.yarn.client.api.YarnClientApplication;
import org.apache.hadoop.yarn.conf.YarnConfiguration;
import org.apache.hadoop.yarn.util.Apps;
import org.apache.hadoop.yarn.util.ConverterUtils;
import org.apache.hadoop.yarn.util.Records;

import java.io.File;
import java.io.IOException;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;


public class Client {

Configuration conf = new YarnConfiguration();

public void run(String[] args) throws Exception {
final String command = args[0];
final int n = Integer.valueOf(args[1]);
final Path jarPath = new Path(args[2]);
final String resourceManagerAddress = args[3];
final String resourceManagerHostname = args[4];
final String resourceManagerSchedulerAddress = args[5];
final String resourceManagerResourceTrackerAddress = args[6];

// Create yarnClient
YarnConfiguration conf = new YarnConfiguration();
conf.set("yarn.resourcemanager.address", resourceManagerAddress);
conf.set("yarn.resourcemanager.hostname", resourceManagerHostname);
conf.set("yarn.resourcemanager.scheduler.address", resourceManagerSchedulerAddress);
conf.set("yarn.resourcemanager.resource-tracker.address", resourceManagerResourceTrackerAddress);
YarnClient yarnClient = YarnClient.createYarnClient();
yarnClient.init(conf);
yarnClient.start();

// Create application via yarnClient
YarnClientApplication app = yarnClient.createApplication();

// Set up the container launch context for the application master
ContainerLaunchContext amContainer =
Records.newRecord(ContainerLaunchContext.class);
amContainer.setCommands(
Collections.singletonList(
"$JAVA_HOME/bin/java" +
" -Xmx256M" +
" com.hortonworks.simpleyarnapp.ApplicationMaster" +
" " + command +
" " + String.valueOf(n) +
" " + resourceManagerAddress +
" " + resourceManagerHostname +
" " + resourceManagerSchedulerAddress +
" " + resourceManagerResourceTrackerAddress +
" 1>" + ApplicationConstants.LOG_DIR_EXPANSION_VAR + "/stdout" +
" 2>" + ApplicationConstants.LOG_DIR_EXPANSION_VAR + "/stderr"
)
);

// Setup jar for ApplicationMaster
LocalResource appMasterJar = Records.newRecord(LocalResource.class);
setupAppMasterJar(jarPath, appMasterJar);
amContainer.setLocalResources(
Collections.singletonMap("simple-yarn-app-1.1.0.jar", appMasterJar));

// Setup CLASSPATH for ApplicationMaster
Map<String, String> appMasterEnv = new HashMap<String, String>();
setupAppMasterEnv(appMasterEnv);
amContainer.setEnvironment(appMasterEnv);

// Set up resource type requirements for ApplicationMaster
Resource capability = Records.newRecord(Resource.class);
capability.setVirtualCores(1);

// Finally, set-up ApplicationSubmissionContext for the application
ApplicationSubmissionContext appContext =
app.getApplicationSubmissionContext();
appContext.setApplicationName("simple-yarn-app"); // application name
appContext.setAMContainerSpec(amContainer);
appContext.setResource(capability);
appContext.setQueue("default"); // queue

// Submit application
ApplicationId appId = appContext.getApplicationId();
System.out.println("Submitting application " + appId);
yarnClient.submitApplication(appContext);

ApplicationReport appReport = yarnClient.getApplicationReport(appId);
YarnApplicationState appState = appReport.getYarnApplicationState();
while (appState != YarnApplicationState.FINISHED &&
appState != YarnApplicationState.KILLED &&
appState != YarnApplicationState.FAILED) {
Thread.sleep(100);
appReport = yarnClient.getApplicationReport(appId);
appState = appReport.getYarnApplicationState();
}

System.out.println(
"Application " + appId + " finished with" +
" state " + appState +
" at " + appReport.getFinishTime());

}

private void setupAppMasterJar(Path jarPath, LocalResource appMasterJar) throws IOException {
FileStatus jarStat = FileSystem.get(conf).getFileStatus(jarPath);
appMasterJar.setResource(ConverterUtils.getYarnUrlFromPath(jarPath));
appMasterJar.setSize(jarStat.getLen());
appMasterJar.setTimestamp(jarStat.getModificationTime());
appMasterJar.setType(LocalResourceType.FILE);
appMasterJar.setVisibility(LocalResourceVisibility.PUBLIC);
}

@SuppressWarnings("deprecation")
private void setupAppMasterEnv(Map<String, String> appMasterEnv) {
for (String c : conf.getStrings(
YarnConfiguration.YARN_APPLICATION_CLASSPATH,
YarnConfiguration.DEFAULT_YARN_APPLICATION_CLASSPATH)) {
Apps.addToEnvironment(appMasterEnv, Environment.CLASSPATH.name(),
c.trim());
}
Apps.addToEnvironment(appMasterEnv,
Environment.CLASSPATH.name(),
Environment.PWD.$() + File.separator + "*");
}

public static void main(String[] args) throws Exception {
Client c = new Client();
c.run(args);
}

}
Binary file not shown.
27 changes: 27 additions & 0 deletions hadoop-unit-yarn/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<parent>
<artifactId>hadoop-unit</artifactId>
<groupId>fr.jetoile.hadoop</groupId>
<version>2.3-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>hadoop-unit-yarn</artifactId>

<dependencies>

<dependency>
<groupId>fr.jetoile.hadoop</groupId>
<artifactId>hadoop-unit-commons-hadoop</artifactId>
</dependency>

<dependency>
<groupId>com.github.sakserv</groupId>
<artifactId>hadoop-mini-clusters-yarn</artifactId>
</dependency>

</dependencies>
</project>
Loading

0 comments on commit 7b54557

Please sign in to comment.