Skip to content

Commit

Permalink
PUB-225: Java 11 migration
Browse files Browse the repository at this point in the history
  • Loading branch information
basilisk487 committed Apr 4, 2020
1 parent 32cedb3 commit 508c72f
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 55 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
The [Wavefront Proxy](https://docs.wavefront.com/proxies.html) is a light-weight Java application that you send your metrics, histograms, and trace data to. It handles batching and transmission of your data to the Wavefront service in a secure, fast, and reliable manner.

## Requirements
* Java >= 1.8
* Java 11 or higher
* Maven

## Overview
Expand Down
2 changes: 1 addition & 1 deletion pkg/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Wavefront Proxy Packaging
Tools
-----
* Install [docker](https://www.docker.com/).
* Install Java 8+/Maven.
* Install Java 11+/Maven.

Methodology
-----------
Expand Down
10 changes: 5 additions & 5 deletions pkg/after-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ spool_dir="/var/spool/wavefront-proxy"
wavefront_dir="/opt/wavefront"
conf_dir="/etc/wavefront"
log_dir="/var/log/wavefront"
jre_dir="$wavefront_dir/$service_name/proxy-jre"
jre_dir="$wavefront_dir/$service_name/proxy-jre-11"

# Set up wavefront user.
if ! groupmod $group &> /dev/null; then
Expand Down Expand Up @@ -46,23 +46,23 @@ chown $user:$group $conf_dir/$service_name

if [[ ! -f $conf_dir/$service_name/wavefront.conf ]]; then
if [[ -f $wavefront_dir/$service_name/conf/wavefront.conf ]]; then
echo "Copying $conf_dir/$service_name/wavefront.conf from $wavefront_dir/$service_name/conf/wavefront.conf"
echo "Copying $conf_dir/$service_name/wavefront.conf from $wavefront_dir/$service_name/conf/wavefront.conf" >&2
cp $wavefront_dir/$service_name/conf/wavefront.conf $conf_dir/$service_name/wavefront.conf
else
echo "Creating $conf_dir/$service_name/wavefront.conf from template"
echo "Creating $conf_dir/$service_name/wavefront.conf from default template" >&2
cp $conf_dir/$service_name/wavefront.conf.default $conf_dir/$service_name/wavefront.conf
fi
else
echo "$conf_dir/$service_name/wavefront.conf already exists"
fi

if [[ ! -f $conf_dir/$service_name/preprocessor_rules.yaml ]]; then
echo "Creating $conf_dir/$service_name/preprocessor_rules.yaml from template"
echo "Creating $conf_dir/$service_name/preprocessor_rules.yaml from default template" >&2
cp $conf_dir/$service_name/preprocessor_rules.yaml.default $conf_dir/$service_name/preprocessor_rules.yaml
fi

if [[ ! -f $conf_dir/$service_name/log4j2.xml ]]; then
echo "Creating $conf_dir/$service_name/log4j2.xml from template"
echo "Creating $conf_dir/$service_name/log4j2.xml from default template" >&2
cp $conf_dir/$service_name/log4j2.xml.default $conf_dir/$service_name/log4j2.xml
fi

Expand Down
8 changes: 5 additions & 3 deletions pkg/before-remove.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

service_name="wavefront-proxy"
wavefront_dir="/opt/wavefront"
jre_dir="$wavefront_dir/$service_name/proxy-jre"
jre_dir="$wavefront_dir/$service_name/proxy-jre-11"

#service wavefront-proxy stop || true

Expand All @@ -15,8 +15,10 @@ jre_dir="$wavefront_dir/$service_name/proxy-jre"
# - https://www.debian.org/doc/debian-policy/ch-maintainerscripts.html
if [[ "$1" == "0" ]] || [[ "$1" == "remove" ]] || [[ "$1" == "purge" ]]; then
service wavefront-proxy stop || true
echo "Removing installed JRE from $jre_dir"
rm -rf $jre_dir
if [ -d $jre_dir ]; then
[ "$(ls -A $jre_dir)" ] && echo "Removing installed JRE from $jre_dir" >&2
rm -rf $jre_dir
fi
fi

exit 0
37 changes: 24 additions & 13 deletions pkg/etc/init.d/wavefront-proxy
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ user="wavefront"
wavefront_dir="/opt/wavefront"
proxy_dir=${PROXY_DIR:-$wavefront_dir/wavefront-proxy}
config_dir=${CONFIG_DIR:-/etc/wavefront/wavefront-proxy}
proxy_jre_dir="$proxy_dir/proxy-jre"
proxy_jre_dir="$proxy_dir/proxy-jre-11"
export JAVA_HOME=${PROXY_JAVA_HOME:-$proxy_jre_dir}
conf_file=$CONF_FILE
if [[ -z $conf_file ]]; then
Expand All @@ -53,8 +53,8 @@ fi
daemon_log_file=${DAEMON_LOG_FILE:-/var/log/wavefront/wavefront-daemon.log}
err_file="/var/log/wavefront/wavefront-error.log"
pid_file=${PID_FILE:-/var/run/$service_name.pid}
agent_jar=${AGENT_JAR:-$proxy_dir/bin/wavefront-push-agent.jar}
class="com.wavefront.agent.PushAgentDaemon"
proxy_jar=${AGENT_JAR:-$proxy_dir/bin/wavefront-proxy.jar}
class="com.wavefront.agent.WavefrontProxyService"
app_args=${APP_ARGS:--f $conf_file}

# If JAVA_ARGS is not set, try to detect memory size and set heap to 8GB if machine has more than 8GB.
Expand Down Expand Up @@ -88,23 +88,23 @@ fi
download_jre() {
[[ -d $proxy_jre_dir ]] || mkdir -p $proxy_jre_dir

echo "Checking $conf_file for HTTP proxy settings"
echo "Checking $conf_file for HTTP proxy settings" >&2
proxy_host=$(grep "^\s*proxyHost=" $conf_file | cut -d'=' -f2)
proxy_port=$(grep "^\s*proxyPort=" $conf_file | cut -d'=' -f2)
proxy_user=$(grep "^\s*proxyUser=" $conf_file | cut -d'=' -f2)
proxy_password=$(grep "^\s*proxyPassword=" $conf_file | cut -d'=' -f2)

if [[ -n $proxy_host && -n $proxy_port ]]; then
echo "Using HTTP proxy $proxy_host:$proxy_port"
echo "Using HTTP proxy $proxy_host:$proxy_port" >&2
proxy_args="--proxy $proxy_host:$proxy_port"
if [[ -n $proxy_user && -n $proxy_password ]]; then
echo "Authenticating as $proxy_user"
echo "Authenticating as $proxy_user" >&2
proxy_args+=" --proxy-user $proxy_user:$proxy_password"
fi
else
echo "No HTTP proxy configuration detected - attempting direct download"
echo "No HTTP proxy configuration detected - attempting direct download" >&2
fi
curl -L --silent -o /tmp/jre.tar.gz $proxy_args https://s3-us-west-2.amazonaws.com/wavefront-misc/proxy-jre.tgz || true
curl -L --silent -o /tmp/jre.tar.gz $proxy_args https://s3-us-west-2.amazonaws.com/wavefront-misc/proxy-jre-11.0.6-linux_x64.tar.gz || true
tar -xf /tmp/jre.tar.gz --strip 1 -C $proxy_jre_dir || true
rm /tmp/jre.tar.gz || true
}
Expand All @@ -115,8 +115,19 @@ download_jre() {
# from wavefront.conf, if any, and try to download the JRE again. Ideally we should go back to bundling JRE
# and not having to worry about accessibility of external resources.
if [[ -z "$PROXY_JAVA_HOME" && ! -r $proxy_jre_dir/bin/java ]]; then
echo "JRE not found - trying to download and install"
download_jre
if type -p java; then
JAVA_VERSION=$(java -version 2>&1 | head -1 | cut -d'"' -f2 | sed '/^1\./s///' | cut -d'.' -f1)
if [[ $JAVA_VERSION == "11" ]]; then
JAVA_HOME=$($(dirname $(dirname $(readlink -f $(which javac)))))
echo "Using Java 11 detected in $JAVA_HOME" >&2
else
echo "Java 11 not found - trying to download and install" >&2
download_jre
fi
else
echo "Java 11 not found - trying to download and install" >&2
download_jre
fi
fi

jsvc=$proxy_dir/bin/jsvc
Expand All @@ -128,15 +139,15 @@ jsvc_exec()
> $err_file
fi

cd "$(dirname "$agent_jar")"
cd "$(dirname "$proxy_jar")"

set +e
# We want word splitting below, as we're building up a command line.
# shellcheck disable=SC2086
$jsvc \
-user $user \
-home $JAVA_HOME \
-cp $agent_jar \
-cp $proxy_jar \
$java_args \
-Xss2049k \
-XX:OnOutOfMemoryError="kill -1 %p" \
Expand All @@ -150,7 +161,7 @@ jsvc_exec()
$class \
$app_args &> $daemon_log_file
if [[ $? -ne 0 ]]; then
echo "There was a problem, see $err_file and $daemon_log_file"
echo "There was a problem, see $err_file and $daemon_log_file" >&2
fi
}

Expand Down
2 changes: 1 addition & 1 deletion pkg/stage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,4 @@ ln -s ../commons-daemon/src/native/unix/jsvc jsvc

echo "Stage the agent jar..."
cd $PROG_DIR
cp $PUSH_AGENT_JAR $PROXY_DIR/bin/wavefront-push-agent.jar
cp $PUSH_AGENT_JAR $PROXY_DIR/bin/wavefront-proxy.jar
8 changes: 4 additions & 4 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
</modules>
<packaging>pom</packaging>

<name>Wavefront All-in-One</name>
<description>Top-level Wavefront Public Java POM</description>
<name>Wavefront Proxy</name>
<description>Wavefront Proxy Project</description>
<url>http://www.wavefront.com</url>
<licenses>
<license>
Expand Down Expand Up @@ -53,12 +53,12 @@
<junit.version>4.12</junit.version>
<truth.version>0.29</truth.version>

<java.version>1.8</java.version>
<java.version>11</java.version>
<log4j.version>2.12.1</log4j.version>
<jackson.version>2.9.10</jackson.version>
<jackson-databind.version>2.9.10.3</jackson-databind.version>
<netty.version>4.1.45.Final</netty.version>
<java-lib.version>2020-02.2</java-lib.version>
<java-lib.version>2020-03.4</java-lib.version>

<doclint>none</doclint>
</properties>
Expand Down
2 changes: 1 addition & 1 deletion proxy/docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ RUN apt-get install -y sudo
RUN apt-get install -y gnupg2
RUN apt-get install -y debian-archive-keyring
RUN apt-get install -y apt-transport-https
RUN apt-get install -y openjdk-8-jdk
RUN apt-get install -y openjdk-11-jdk

# Download wavefront proxy (latest release). Merely extract the debian, don't want to try running startup scripts.
RUN echo "deb https://packagecloud.io/wavefront/proxy/ubuntu/ bionic main" > /etc/apt/sources.list.d/wavefront_proxy.list
Expand Down
7 changes: 3 additions & 4 deletions proxy/docker/Dockerfile-rhel
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@
FROM registry.access.redhat.com/ubi7
USER root


# This script may automatically configure wavefront without prompting, based on
# these variables:
# WAVEFRONT_URL (required)
Expand All @@ -12,17 +11,17 @@ USER root
# WAVEFRONT_PROXY_ARGS (default is none)
# JAVA_ARGS (default is none)

RUN yum-config-manager --enable rhel-7-server-optional-rpms
RUN yum update --disableplugin=subscription-manager -y && rm -rf /var/cache/yum

RUN yum install -y sudo
RUN yum install -y curl
RUN yum install -y hostname
RUN yum install -y java-1.8.0-openjdk-devel.x86_64
RUN yum install -y java-11-openjdk-devel

# Download wavefront proxy (latest release). Merely extract the debian, don't want to try running startup scripts.
# Download wavefront proxy (latest release). Merely extract the package, don't want to try running startup scripts.
RUN curl -s https://packagecloud.io/install/repositories/wavefront/proxy/script.rpm.sh | sudo bash
RUN yum -y update

RUN yum -y -q install wavefront-proxy

# Configure agent
Expand Down
2 changes: 1 addition & 1 deletion proxy/docker/run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ java \
$jvm_container_opts $JAVA_ARGS \
-Djava.util.logging.manager=org.apache.logging.log4j.jul.LogManager \
-Dlog4j.configurationFile=/etc/wavefront/wavefront-proxy/log4j2.xml \
-jar /opt/wavefront/wavefront-proxy/bin/wavefront-push-agent.jar \
-jar /opt/wavefront/wavefront-proxy/bin/wavefront-proxy.jar \
-h $WAVEFRONT_URL \
-t $WAVEFRONT_TOKEN \
--hostname ${WAVEFRONT_HOSTNAME:-$(hostname)} \
Expand Down
2 changes: 1 addition & 1 deletion proxy/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
</parent>

<properties>
<java.version>1.8</java.version>
<java.version>11</java.version>
</properties>

<artifactId>proxy</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/**
* @author Mori Bellamy (mori@wavefront.com)
*/
public class PushAgentDaemon implements Daemon {
public class WavefrontProxyService implements Daemon {

private PushAgent agent;
private DaemonContext daemonContext;
Expand Down
52 changes: 33 additions & 19 deletions proxy/src/main/java/com/wavefront/common/DelegatingLogger.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
package com.wavefront.common;

import java.security.AccessController;
import java.security.PrivilegedAction;
import java.util.Optional;
import java.util.function.Predicate;
import java.util.logging.Level;
import java.util.logging.LogRecord;
import java.util.logging.Logger;
Expand Down Expand Up @@ -37,28 +41,38 @@ public void log(LogRecord logRecord) {
delegate.log(logRecord);
}

/**
* This is a JDK8-specific implementation. TODO: switch to StackWalker after migrating to JDK9+
*/
private void inferCaller(LogRecord logRecord) {
StackTraceElement[] stackTraceElements = Thread.currentThread().getStackTrace();
boolean lookingForLogger = true;
for (StackTraceElement frame : stackTraceElements) {
String cname = frame.getClassName();
Optional<StackWalker.StackFrame> frame = new CallerFinder().get();
frame.ifPresent(f -> {
logRecord.setSourceClassName(f.getClassName());
logRecord.setSourceMethodName(f.getMethodName());
});
}

static final class CallerFinder implements Predicate<StackWalker.StackFrame> {
private static final StackWalker WALKER;
static {
final PrivilegedAction<StackWalker> action =
() -> StackWalker.getInstance(StackWalker.Option.RETAIN_CLASS_REFERENCE);
WALKER = AccessController.doPrivileged(action);
}

Optional<StackWalker.StackFrame> get() {
return WALKER.walk(s -> s.filter(this).findFirst());
}

private boolean lookingForLogger = true;

@Override
public boolean test(StackWalker.StackFrame t) {
final String cname = t.getClassName();
if (lookingForLogger) {
// Skip all frames until we have found the first logger frame.
if (cname.endsWith("Logger")) {
lookingForLogger = false;
}
} else {
if (!cname.endsWith("Logger") && !cname.startsWith("java.lang.reflect.") &&
!cname.startsWith("sun.reflect.")) {
// We've found the relevant frame.
logRecord.setSourceClassName(cname);
logRecord.setSourceMethodName(frame.getMethodName());
return;
}
// the log record could be created for a platform logger
lookingForLogger = !(cname.endsWith("Logger") ||
cname.startsWith("sun.util.logging.PlatformLogger"));
return false;
}
return !cname.endsWith("Logger");
}
}
}

0 comments on commit 508c72f

Please sign in to comment.