Skip to content

Commit

Permalink
[#150][#178] feat(all): Add launching process and scripts (#191)
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?

Through `graviton.sh` script launching GravtionServer.

#### Command
```
./bin/graviton.sh [--config <conf-dir>] {start|stop|restart|status}
```

#### Directory struct
```
├── bin
│   ├── command.sh
│   └── graviton.sh
├── conf
│   ├── graviton.conf.template
│   ├── graviton-env.sh.template
│   └── log4j.properties.template
└── distribution/package
    ├── bin/
    ├── catalogs/catalog-hive
    ├── conf/
    ├── lib/
    ├── graviton-${version}-bin.tar
    └── graviton-${version}-bin.tar.sha256
```

#### Scripts context
+ bin/command.sh: Command functions
+ bin/graviton.sh: Launching scripts
+ conf/graviton.conf.template: All configuration templates for Gravtion
+ conf/gravtion-env.sh.template: Environment variables, etc., JAVA_HOME,
GRAVITON_HOME etc.,
+ conf/log4j.properties.template: log4j configuration for Gravtion
Server.

#### Assemble Distribution
1. `./gradlew clean build`
2. `./gradlew compileDistribution`

+ Copy `${project_root_dir}/bin/` directory to
`${project_root_dir}/distribution/package/bin/`.
+ Copy `${project_root_dir}/conf/` directory to
`${project_root_dir}/distribution/package/conf/` and remove `.template`
file name suffix.
+ Copy `${project_root_dir}/${sub_module}/lib` to
`${project_root_dir}/distribution/package/lib/`
+ Copy `${project_root_dir}/catalog-hive}/lib` to
`${project_root_dir}/distribution/package/catalogs/catalog-hive/lib`

3. `./gradlew assembleDistribution` 

+ Compress `${project_root_dir}/distribution/package` generate
`graviton-${version}-bin.tar` and `graviton-${version}-bin.tar.sha256`

4. `./distribution/package/bin/graviton.sh start|stop|status`

### Why are the changes needed?

Through script launching `Graviton Server`.

Fix: #178 

### Does this PR introduce _any_ user-facing change?

N/A

### How was this patch tested?

Test on PullRequst submission
  • Loading branch information
xunliu authored Aug 11, 2023
1 parent b595f60 commit b97e382
Show file tree
Hide file tree
Showing 14 changed files with 589 additions and 23 deletions.
43 changes: 43 additions & 0 deletions .github/workflows/integration.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: integration

# Controls when the workflow will run
on:
# Triggers the workflow on push or pull request events but only for the "main" branch
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

concurrency:
group: ${{ github.worklfow }}-${{ github.event.pull_request.number || github.ref }}
cancel-in-progress: ${{ github.event_name == 'pull_requests' }}

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
# This workflow contains a single job called "build"
integration_test:
# The type of runner that the job will run on
runs-on: ubuntu-latest

# Steps represent a sequence of tasks that will be executed as part of the job
steps:
# Checks-out your repository under $GITHUB_WORKSPACE, so your job can access it
- uses: actions/checkout@v3

- uses: actions/setup-java@v3
with:
java-version: '8'
distribution: 'temurin'

- name: Setup Gradle
uses: gradle/gradle-build-action@v2
with:
gradle-version: '8.1.1'

- name: Show gradle version
run: gradle --version

- name: Package Graviton
run: |
gradle build
gradle compileDistribution
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,6 @@ gen
*.iml
out/**
*.iws

distribution
server/src/main/resources/project.properties
82 changes: 82 additions & 0 deletions bin/common.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
#!/bin/bash
#
# Copyright 2023 Datastrato.
# This software is licensed under the Apache License version 2.
#

if [ -L "${BASH_SOURCE-$0}" ]; then
FWDIR=$(dirname "$(readlink "${BASH_SOURCE-$0}")")
else
FWDIR=$(dirname "${BASH_SOURCE-$0}")
fi

echo ${GRAVITON_HOME}

if [[ -z "${GRAVITON_HOME}" ]]; then
export GRAVITON_HOME="$(cd "${FWDIR}/.." || exit; pwd)"
fi

if [[ -z "${GRAVITON_CONF_DIR}" ]]; then
export GRAVITON_CONF_DIR="${GRAVITON_HOME}/conf"
fi

if [[ -z "${GRAVITON_LOG_DIR}" ]]; then
export GRAVITON_LOG_DIR="${GRAVITON_HOME}/logs"
fi

if [[ -f "${GRAVITON_CONF_DIR}/graviton-env.sh" ]]; then
. "${GRAVITON_CONF_DIR}/graviton-env.sh"
fi

GRAVITON_CLASSPATH+=":${GRAVITON_CONF_DIR}"

function check_java_version() {
if [[ -n "${JAVA_HOME+x}" ]]; then
JAVA="$JAVA_HOME/bin/java"
fi
java_ver_output=$("${JAVA:-java}" -version 2>&1)
jvmver=$(echo "$java_ver_output" | grep '[openjdk|java] version' | awk -F'"' 'NR==1 {print $2}' | cut -d\- -f1)
JVM_VERSION=$(echo "$jvmver"|sed -e 's|^\([0-9][0-9]*\)\..*$|\1|')
if [ "$JVM_VERSION" = "1" ]; then
JVM_VERSION=$(echo "$jvmver"|sed -e 's|^1\.\([0-9][0-9]*\)\..*$|\1|')
fi

# JDK 8u151 version fixed a number of security vulnerabilities and issues to improve system stability and security.
# https://www.oracle.com/java/technologies/javase/8u151-relnotes.html
if [ "$JVM_VERSION" -lt 8 ] || { [ "$JVM_VERSION" -eq 8 ] && [ "${jvmver#*_}" -lt 151 ]; } ; then
echo "Graviton requires either Java 8 update 151 or newer"
exit 1;
fi
}

function addEachJarInDir(){
if [[ -d "${1}" ]]; then
for jar in "${1}"/*.jar ; do
GRAVITON_CLASSPATH="${jar}:${GRAVITON_CLASSPATH}"
done
fi
}

function addEachJarInDirRecursive(){
if [[ -d "${1}" ]]; then
for jar in "${1}"/**/*.jar ; do
GRAVITON_CLASSPATH="${jar}:${GRAVITON_CLASSPATH}"
done
fi
}

function addJarInDir(){
if [[ -d "${1}" ]]; then
GRAVITON_CLASSPATH="${1}/*:${GRAVITON_CLASSPATH}"
fi
}

if [[ -z "${GRAVITON_MEM}" ]]; then
export GRAVITON_MEM="-Xmx1024m"
fi

if [[ -n "${JAVA_HOME}" ]]; then
export JAVA_RUNNER="${JAVA_HOME}/bin/java"
else
export JAVA_RUNNER=java
fi
152 changes: 152 additions & 0 deletions bin/graviton.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
#!/bin/bash
#
# Copyright 2023 Datastrato.
# This software is licensed under the Apache License version 2.
#
#set -ex
USAGE="-e Usage: bin/graviton.sh [--config <conf-dir>]\n\t
{start|stop|restart|status}"

if [[ "$1" == "--config" ]]; then
shift
conf_dir="$1"
if [[ ! -d "${conf_dir}" ]]; then
echo "ERROR : ${conf_dir} is not a directory"
echo ${USAGE}
exit 1
else
export GRAVITON_CONF_DIR="${conf_dir}"
fi
shift
fi

bin="$(dirname "${BASH_SOURCE-$0}")"
bin="$(cd "${bin}">/dev/null; pwd)"

. "${bin}/common.sh"

check_java_version

function check_process_status() {
local pid=$(found_graviton_server_pid)

if [[ -z "${pid}" ]]; then
echo "Graviton Server is not running"
else
echo "Graviton Server is running[PID:$pid]"
fi
}

function found_graviton_server_pid() {
process_name='GravitonServer';
RUNNING_PIDS=$(ps x | grep ${process_name} | grep -v grep | awk '{print $1}');

if [[ -z "${RUNNING_PIDS}" ]]; then
return
fi

if ! kill -0 ${RUNNING_PIDS} > /dev/null 2>&1; then
echo "Graviton Server running but process is dead"
fi

echo "${RUNNING_PIDS}"
}

function wait_for_graviton_server_to_die() {
timeout=10
timeoutTime=$(date "+%s")
let "timeoutTime+=$timeout"
currentTime=$(date "+%s")
forceKill=1

while [[ $currentTime -lt $timeoutTime ]]; do
local pid=$(found_graviton_server_pid)
if [[ -z "${pid}" ]]; then
forceKill=0
break
fi

$(kill ${pid} > /dev/null 2> /dev/null)
if kill -0 ${pid} > /dev/null 2>&1; then
sleep 3
else
forceKill=0
break
fi
currentTime=$(date "+%s")
done

if [[ forceKill -ne 0 ]]; then
$(kill -9 ${pid} > /dev/null 2> /dev/null)
fi
}

function start() {
local pid=$(found_graviton_server_pid)

if [[ ! -z "${pid}" ]]; then
if kill -0 ${pid} >/dev/null 2>&1; then
echo "Graviton Server is already running"
return 0;
fi
fi

if [[ ! -d "${GRAVITON_LOG_DIR}" ]]; then
echo "Log dir doesn't exist, create ${GRAVITON_LOG_DIR}"
mkdir -p "${GRAVITON_LOG_DIR}"
fi

nohup ${JAVA_RUNNER} ${JAVA_OPTS} ${GRAVITON_DEBUG_OPTS} -cp ${GRAVITON_CLASSPATH} ${GRAVITON_SERVER_NAME} >> "${GRAVITON_OUTFILE}" 2>&1 &

pid=$!
if [[ -z "${pid}" ]]; then
echo "Graviton Server start error!"
return 1;
else
echo "Graviton Server start success!"
fi

sleep 2
check_process_status
}

function stop() {
local pid

pid=$(found_graviton_server_pid)

if [[ -z "${pid}" ]]; then
echo "Graviton Server is not running"
else
wait_for_graviton_server_to_die
echo "Graviton Server stop"
fi
}

HOSTNAME=$(hostname)
GRAVITON_OUTFILE="${GRAVITON_LOG_DIR}/graviton-${HOSTNAME}.out"
GRAVITON_SERVER_NAME=com.datastrato.graviton.server.GravitonServer

JAVA_OPTS+=" -Dfile.encoding=UTF-8"
JAVA_OPTS+=" -Dlog4j2.configurationFile=file://${GRAVITON_CONF_DIR}/log4j2.properties"
JAVA_OPTS+=" -Dgraviton.log.path=${GRAVITON_LOG_DIR} ${GRAVITON_MEM}"

addJarInDir "${GRAVITON_HOME}/lib"

case "${1}" in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
check_process_status
;;
*)
echo ${USAGE}
esac
Loading

0 comments on commit b97e382

Please sign in to comment.