Skip to content

Commit

Permalink
Refactoring Gradle project structure; introducing raigad-discovery-pl…
Browse files Browse the repository at this point in the history
…ugin Zip artifact
  • Loading branch information
gndcshv committed Jun 28, 2016
1 parent 18b616e commit 05cc062
Show file tree
Hide file tree
Showing 17 changed files with 721 additions and 45 deletions.
12 changes: 4 additions & 8 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
buildscript {
repositories { jcenter() }
dependencies {
classpath 'com.netflix.nebula:gradle-extra-configurations-plugin:2.2.0'
}
plugins {
id 'nebula.netflixoss' version '3.3.0'
}

plugins {
id 'nebula.netflixoss' version '2.2.9'
allprojects {
apply plugin: 'netflix.nebula'
}

// Establish version and status
Expand All @@ -19,7 +16,6 @@ apply plugin: 'project-report'
subprojects {
apply plugin: 'nebula.netflixoss'
apply plugin: 'java'
apply plugin: 'nebula.provided-base'

group = 'com.netflix.raigad'

Expand Down
Binary file modified gradle/wrapper/gradle-wrapper.jar
Binary file not shown.
4 changes: 2 additions & 2 deletions gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#Fri May 01 12:00:47 PDT 2015
#Thu Jun 09 18:03:08 UTC 2016
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-2.2.1-all.zip
distributionUrl=http\://artifacts.netflix.com/gradle-netflix-local/netflix/nebula-gradle/3.10.1/nebula-gradle-3.10.1-all.zip
61 changes: 34 additions & 27 deletions gradlew
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,37 @@
##
##############################################################################

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS=""
# Netflix: Attempt to use $WORKSPACE/.gradle if we're not told otherwise
if [ "x$GRADLE_USER_HOME" == "x" ]; then
if [ "x$WORKSPACE" != "x" ]; then
export GRADLE_USER_HOME="$WORKSPACE/.gradle"
fi
fi

# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`"/$link"
fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >/dev/null
APP_HOME="`pwd -P`"
cd "$SAVED" >/dev/null

APP_NAME="Gradle"
APP_BASE_NAME=`basename "$0"`

# Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
DEFAULT_JVM_OPTS=""

# Use the maximum available, or set MAX_FD != -1 to use that value.
MAX_FD="maximum"

Expand All @@ -30,6 +55,7 @@ die ( ) {
cygwin=false
msys=false
darwin=false
nonstop=false
case "`uname`" in
CYGWIN* )
cygwin=true
Expand All @@ -40,31 +66,11 @@ case "`uname`" in
MINGW* )
msys=true
;;
NONSTOP* )
nonstop=true
;;
esac

# For Cygwin, ensure paths are in UNIX format before anything is touched.
if $cygwin ; then
[ -n "$JAVA_HOME" ] && JAVA_HOME=`cygpath --unix "$JAVA_HOME"`
fi

# Attempt to set APP_HOME
# Resolve links: $0 may be a link
PRG="$0"
# Need this for relative symlinks.
while [ -h "$PRG" ] ; do
ls=`ls -ld "$PRG"`
link=`expr "$ls" : '.*-> \(.*\)$'`
if expr "$link" : '/.*' > /dev/null; then
PRG="$link"
else
PRG=`dirname "$PRG"`"/$link"
fi
done
SAVED="`pwd`"
cd "`dirname \"$PRG\"`/" >&-
APP_HOME="`pwd -P`"
cd "$SAVED" >&-

CLASSPATH=$APP_HOME/gradle/wrapper/gradle-wrapper.jar

# Determine the Java command to use to start the JVM.
Expand All @@ -90,7 +96,7 @@ location of your Java installation."
fi

# Increase the maximum file descriptors if we can.
if [ "$cygwin" = "false" -a "$darwin" = "false" ] ; then
if [ "$cygwin" = "false" -a "$darwin" = "false" -a "$nonstop" = "false" ] ; then
MAX_FD_LIMIT=`ulimit -H -n`
if [ $? -eq 0 ] ; then
if [ "$MAX_FD" = "maximum" -o "$MAX_FD" = "max" ] ; then
Expand All @@ -114,6 +120,7 @@ fi
if $cygwin ; then
APP_HOME=`cygpath --path --mixed "$APP_HOME"`
CLASSPATH=`cygpath --path --mixed "$CLASSPATH"`
JAVACMD=`cygpath --unix "$JAVACMD"`

# We build the pattern for arguments to be converted via cygpath
ROOTDIRSRAW=`find -L / -maxdepth 1 -mindepth 1 -type d 2>/dev/null`
Expand Down Expand Up @@ -161,4 +168,4 @@ function splitJvmOpts() {
eval splitJvmOpts $DEFAULT_JVM_OPTS $JAVA_OPTS $GRADLE_OPTS
JVM_OPTS[${#JVM_OPTS[*]}]="-Dorg.gradle.appname=$APP_BASE_NAME"

exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain "$@"
exec "$JAVACMD" "${JVM_OPTS[@]}" -classpath "$CLASSPATH" org.gradle.wrapper.GradleWrapperMain $NEBULA_FLAGS "$@"
17 changes: 12 additions & 5 deletions gradlew.bat
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,21 @@
@rem Set local scope for the variables with windows NT shell
if "%OS%"=="Windows_NT" setlocal

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=

set DIRNAME=%~dp0
if "%DIRNAME%" == "" set DIRNAME=.
set APP_BASE_NAME=%~n0
set APP_HOME=%DIRNAME%

@rem Netflix: Attempt to use $WORKSPACE/.gradle if we're not told otherwise
if "%GRADLE_USER_HOME%"=="" (
if NOT "%WORKSPACE%"=="" (
set GRADLE_USER_HOME=%WORKSPACE%\.gradle
)
)

@rem Add default JVM options here. You can also use JAVA_OPTS and GRADLE_OPTS to pass JVM options to this script.
set DEFAULT_JVM_OPTS=

@rem Find java.exe
if defined JAVA_HOME goto findJavaFromJavaHome

Expand Down Expand Up @@ -46,7 +53,7 @@ echo location of your Java installation.
goto fail

:init
@rem Get command-line arguments, handling Windowz variants
@rem Get command-line arguments, handling Windows variants

if not "%OS%" == "Windows_NT" goto win9xME_args
if "%@eval[2+2]" == "4" goto 4NT_args
Expand All @@ -72,7 +79,7 @@ set CMD_LINE_ARGS=%$
set CLASSPATH=%APP_HOME%\gradle\wrapper\gradle-wrapper.jar

@rem Execute Gradle
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %CMD_LINE_ARGS%
"%JAVA_EXE%" %DEFAULT_JVM_OPTS% %JAVA_OPTS% %GRADLE_OPTS% "-Dorg.gradle.appname=%APP_BASE_NAME%" -classpath "%CLASSPATH%" org.gradle.wrapper.GradleWrapperMain %NEBULA_FLAGS% %CMD_LINE_ARGS%

:end
@rem End local scope for the variables with windows NT shell
Expand Down
16 changes: 16 additions & 0 deletions raigad-discovery-plugin/build.gradle
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
apply plugin: 'osgi'
apply plugin: 'project-report'
apply plugin: 'java'

task createZip(type: Zip) {
from 'src/main/resources/plugin-metadata'
from jar.archivePath
}

publishing {
publications {
nebulaIvy(IvyPublication) {
artifact createZip
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
/**
* Copyright 2016 Netflix, Inc.
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 com.netflix.raigad.discovery;

import org.elasticsearch.cluster.ClusterName;
import org.elasticsearch.cluster.ClusterService;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.discovery.DiscoverySettings;
import org.elasticsearch.discovery.zen.ZenDiscovery;
import org.elasticsearch.discovery.zen.elect.ElectMasterService;
import org.elasticsearch.discovery.zen.ping.ZenPingService;
import org.elasticsearch.node.settings.NodeSettingsService;
import org.elasticsearch.threadpool.ThreadPool;
import org.elasticsearch.transport.TransportService;

public class RaigadDiscovery extends ZenDiscovery {
public static final String DISCOVERY_TYPE = "raigad";
public static final String DISCOVERY_TYPE_LOCATION = "discovery.type";
public static final String DISCOVERY_DESCRIPTION = "Raigad custom discovery plugin";

public static final String GET_NODES_ISLAND_URL = "http://127.0.0.1:8080/Raigad/REST/v1/esconfig/get_nodes";
public static final String GET_NODES_TRIBE_URL_PREFIX = "http://127.0.0.1:8080/Raigad/REST/v1/esconfig/get_tribe_nodes/";

@Inject
public RaigadDiscovery(Settings settings,
ClusterName clusterName,
ThreadPool threadPool,
TransportService transportService,
ClusterService clusterService,
NodeSettingsService nodeSettingsService,
ZenPingService pingService,
ElectMasterService electMasterService,
DiscoverySettings discoverySettings) {
super(settings, clusterName, threadPool, transportService, clusterService,
nodeSettingsService, pingService, electMasterService, discoverySettings);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
/**
* Copyright 2016 Netflix, Inc.
* <p>
* 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
* <p>
* http://www.apache.org/licenses/LICENSE-2.0
* <p>
* 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 com.netflix.raigad.discovery;

import com.netflix.raigad.discovery.utils.DataFetcher;
import com.netflix.raigad.discovery.utils.ElasticsearchUtil;
import org.elasticsearch.Version;
import org.elasticsearch.cluster.node.DiscoveryNode;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.inject.Inject;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.transport.TransportAddress;
import org.elasticsearch.discovery.zen.ping.unicast.UnicastHostsProvider;
import org.elasticsearch.discovery.zen.ping.unicast.UnicastZenPing;
import org.elasticsearch.transport.TransportService;

import java.util.ArrayList;
import java.util.List;

public class RaigadDiscoveryUnicastHostsProvider extends AbstractComponent implements UnicastHostsProvider {
private final TransportService transportService;
private final Version version;
private final Settings settings;

@Inject
public RaigadDiscoveryUnicastHostsProvider(Settings settings, TransportService transportService, Version version) {
super(settings);

this.transportService = transportService;
this.version = version;
this.settings = settings;
}

@Override
public List<DiscoveryNode> buildDynamicNodes() {
List<DiscoveryNode> discoveryNodes = new ArrayList<>();

try {
//Extract tribe ID from name field of settings and query accordingly
String strNodes;

if (isCurrentNodeTribe(settings)) {
String nodeName = settings.get("name");
//TODO: Check for null node name
String tribeId = nodeName.substring(nodeName.indexOf("/") + 1);
logger.debug("Tribe node name: {}, tribe ID: {}", nodeName, tribeId);
strNodes = DataFetcher.fetchData(RaigadDiscovery.GET_NODES_TRIBE_URL_PREFIX + tribeId, logger);
}
else {
strNodes = DataFetcher.fetchData(RaigadDiscovery.GET_NODES_ISLAND_URL, logger);
}

List<RaigadInstance> instances = ElasticsearchUtil.getRaigadInstancesFromJsonString(strNodes, logger);
for (RaigadInstance instance : instances) {
try {
TransportAddress[] addresses =
transportService.addressesFromString(instance.getHostIP(), UnicastZenPing.LIMIT_FOREIGN_PORTS_COUNT);

// We only limit to 1 port, makes no sense to ping 100 ports
for (int i = 0; (i < addresses.length && i < UnicastZenPing.LIMIT_FOREIGN_PORTS_COUNT); i ++) {
logger.info("Adding instance [{}] (address {}, transport address {})",
instance.getId(), instance.getHostIP(), addresses[i]);
discoveryNodes.add(new DiscoveryNode(instance.getId(), addresses[i], version.minimumCompatibilityVersion()));
}
}
catch (Exception e) {
logger.warn("Failed to add instance {} (address {})", e, instance.getId(), instance.getHostIP());
}
}
}
catch (Exception e) {
logger.error("Exception while trying to build dynamic discovery nodes", e);
throw new RuntimeException(e);
}

logger.info("Using the following dynamic discovery nodes: {}", discoveryNodes);
return discoveryNodes;
}

private boolean isCurrentNodeTribe(Settings settings) {
if (settings == null) {
return false;
}

String tribeName = settings.get("name");
if (tribeName == null || tribeName.isEmpty()) {
return false;
}

return tribeName.contains("/t");
}
}
Loading

0 comments on commit 05cc062

Please sign in to comment.