Skip to content

Commit

Permalink
refactor into module with spi
Browse files Browse the repository at this point in the history
  • Loading branch information
jetoile committed Feb 5, 2016
1 parent d57299c commit 5107cf0
Show file tree
Hide file tree
Showing 125 changed files with 11,080 additions and 652 deletions.
84 changes: 84 additions & 0 deletions hadoop-bootstrap-commons/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?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-bootstrap</artifactId>
<groupId>fr.jetoile.sample</groupId>
<version>1.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>hadoop-bootstrap-commons</artifactId>


<dependencies>
<dependency>
<groupId>commons-configuration</groupId>
<artifactId>commons-configuration</artifactId>
</dependency>

<dependency>
<groupId>commons-collections</groupId>
<artifactId>commons-collections</artifactId>
</dependency>

<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
</dependency>

<dependency>
<groupId>org.apache.hadoop</groupId>
<artifactId>hadoop-common</artifactId>
<exclusions>
<exclusion>
<artifactId>httpclient</artifactId>
<groupId>org.apache.httpcomponents</groupId>
</exclusion>
<exclusion>
<artifactId>servlet-api</artifactId>
<groupId>javax.servlet</groupId>
</exclusion>
<exclusion>
<artifactId>org.eclipse.jetty</artifactId>
<groupId>jetty-util</groupId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
</dependency>

<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</dependency>

<dependency>
<groupId>ch.qos.logback</groupId>
<artifactId>logback-classic</artifactId>
</dependency>

<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>

<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-util</artifactId>
<version>9.2.14.v20151106</version>
</dependency>

<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.1.0</version>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
/*
* Copyright (c) 2011 Khanh Tuong Maudoux <kmx.petals@gmail.com>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
package fr.jetoile.sample;

import com.google.common.collect.Lists;
import fr.jetoile.sample.component.Bootstrap;
import fr.jetoile.sample.exception.NotFoundServiceException;
import org.apache.commons.configuration.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;

public enum HadoopBootstrap {
INSTANCE;

final private static Logger LOGGER = LoggerFactory.getLogger(HadoopBootstrap.class);


private Configuration configuration;
List<Bootstrap> componentsToStart = new ArrayList<>();
List<Bootstrap> componentsToStop = new ArrayList<>();

private ServiceLoader<Bootstrap> commandLoader = ServiceLoader.load(Bootstrap.class);
private Map<String, Bootstrap> commands = new HashMap<>();


HadoopBootstrap() {
commands.clear();
commandLoader.reload();
Iterator<Bootstrap> commandsIterator = commandLoader.iterator();

while (commandsIterator.hasNext()) {
Bootstrap command = commandsIterator.next();
commands.put(command.getName(), command);
}

Arrays.asList(Component.values()).stream().forEach(c -> {
if (commands.containsKey(c.name())) {
componentsToStart.add(commands.get(c.name()));
}
});

componentsToStop = Lists.newArrayList(this.componentsToStart);
Collections.reverse(componentsToStop);

Runtime.getRuntime().addShutdownHook(new Thread() {
public void run() {
LOGGER.info("All services are going to be stopped");
stopAll();
}
});
}

public Bootstrap getService(Component c) throws NotFoundServiceException {
if (commands.containsKey(c.name())) {
return commands.get(c.name());
} else {
throw new NotFoundServiceException("unable to find service " + c.name());
}

}

public void startAll() {
componentsToStart.stream().forEach(c -> {
startService(c);
});
}

public void stopAll() {
componentsToStop.stream().forEach(c -> {
stopService(c);
});
}

public HadoopBootstrap start(Component component) throws NotFoundServiceException {
getService(component).start();
return this;
}

public HadoopBootstrap stop(Component component) throws NotFoundServiceException {
getService(component).stop();
return this;
}

private void startService(Bootstrap c) {
c.start();
}

private void stopService(Bootstrap c) {
c.stop();
}

}
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -33,5 +33,7 @@ public interface Bootstrap {

Configuration getConfiguration();

String getName();


}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package fr.jetoile.sample.exception;

/**
* User: khanh
* To change this template use File | Settings | File Templates.
*/
public class NotFoundServiceException extends Exception {
public NotFoundServiceException(String message) {
super(message);
}
}
File renamed without changes.
44 changes: 44 additions & 0 deletions hadoop-bootstrap-hbase/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?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-bootstrap</artifactId>
<groupId>fr.jetoile.sample</groupId>
<version>1.1-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>

<artifactId>hadoop-bootstrap-hbase</artifactId>


<dependencies>

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

<dependency>
<groupId>fr.jetoile.sample</groupId>
<artifactId>hadoop-bootstrap-zookeeper</artifactId>
</dependency>

<dependency>
<groupId>fr.jetoile.sample</groupId>
<artifactId>hadoop-bootstrap-hdfs</artifactId>
</dependency>

<dependency>
<groupId>com.github.sakserv</groupId>
<artifactId>hadoop-mini-clusters-hbase</artifactId>
<exclusions>
<exclusion>
<artifactId>servlet-api-2.5</artifactId>
<groupId>org.mortbay.jetty</groupId>
</exclusion>
</exclusions>
</dependency>

</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import com.github.sakserv.minicluster.config.ConfigVars;
import com.github.sakserv.minicluster.impl.HbaseLocalCluster;
import com.github.sakserv.minicluster.util.FileUtils;
import fr.jetoile.sample.Component;
import fr.jetoile.sample.HadoopUtils;
import fr.jetoile.sample.exception.BootstrapException;
import org.apache.commons.configuration.Configuration;
Expand All @@ -34,8 +35,8 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public enum HBaseBootstrap implements Bootstrap {
INSTANCE;
public class HBaseBootstrap implements Bootstrap {
final public static String NAME = Component.HBASE.name();

final private Logger LOGGER = LoggerFactory.getLogger(HBaseBootstrap.class);

Expand All @@ -54,7 +55,7 @@ public enum HBaseBootstrap implements Bootstrap {
private String zookeeperConnectionString;


HBaseBootstrap() {
public HBaseBootstrap() {
if (hbaseLocalCluster == null) {
try {
loadConfig();
Expand All @@ -64,6 +65,11 @@ public enum HBaseBootstrap implements Bootstrap {
}
}

@Override
public String getName() {
return NAME;
}

private void init() {

}
Expand Down Expand Up @@ -107,7 +113,7 @@ private void loadConfig() throws BootstrapException {

private void cleanup() {
FileUtils.deleteFolder(rootDirectory);
FileUtils.deleteFolder(rootDirectory.substring(rootDirectory.lastIndexOf("/")+1));
FileUtils.deleteFolder(rootDirectory.substring(rootDirectory.lastIndexOf("/") + 1));

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
fr.jetoile.sample.component.HBaseBootstrap
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@


import com.github.sakserv.minicluster.config.ConfigVars;
import fr.jetoile.sample.Component;
import fr.jetoile.sample.HadoopBootstrap;
import fr.jetoile.sample.exception.BootstrapException;
import org.apache.commons.configuration.Configuration;
import org.apache.commons.configuration.ConfigurationException;
Expand All @@ -22,9 +24,6 @@
public class HBaseBootstrapTest {
static private Logger LOGGER = LoggerFactory.getLogger(HBaseBootstrapTest.class);

static private Bootstrap zookeeper;
static private Bootstrap hbase;
static private Bootstrap hdfs;
static private Configuration configuration;


Expand All @@ -36,16 +35,13 @@ public static void setup() throws Exception {
throw new BootstrapException("bad config", e);
}

zookeeper = ZookeeperBootstrap.INSTANCE.start();
hdfs = HdfsBootstrap.INSTANCE.start();
hbase = HBaseBootstrap.INSTANCE.start();
HadoopBootstrap.INSTANCE.startAll();

}

@AfterClass
public static void tearDown() throws Exception {
hbase.stop();
hdfs.stop();
zookeeper.stop();
HadoopBootstrap.INSTANCE.stopAll();
}


Expand All @@ -56,7 +52,7 @@ public void hBaseShouldStart() throws Exception {
String colFamName = configuration.getString(ConfigVars.HBASE_TEST_COL_FAMILY_NAME_KEY);
String colQualiferName = configuration.getString(ConfigVars.HBASE_TEST_COL_QUALIFIER_NAME_KEY);
Integer numRowsToPut = configuration.getInt(ConfigVars.HBASE_TEST_NUM_ROWS_TO_PUT_KEY);
org.apache.hadoop.conf.Configuration hbaseConfiguration = hbase.getConfiguration();
org.apache.hadoop.conf.Configuration hbaseConfiguration = HadoopBootstrap.INSTANCE.getService(Component.HBASE).getConfiguration();

LOGGER.info("HBASE: Creating table {} with column family {}", tableName, colFamName);
createHbaseTable(tableName, colFamName, hbaseConfiguration);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ solr.collection.name=collection1
solr.cloud.port=8983



# YARN
yarn.num.node.managers=1
yarn.num.local.dirs=1
Expand Down
Loading

0 comments on commit 5107cf0

Please sign in to comment.