Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion .github/workflows/ci_build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ jobs:

steps:
- uses: actions/checkout@v3

- name: Check startup log handling
run: bash tests/start-out.sh

- name: Set up JDK 11
uses: actions/setup-java@v3
Expand All @@ -37,7 +40,8 @@ jobs:

- name: Upload Binary Package
if: ${{ success() }}
uses: actions/upload-artifact@v3
uses: actions/upload-artifact@v4
with:
name: KnowStreaming-${{ env.VERSION }}.tar.gz
path: km-dist/target/KnowStreaming-${{ env.VERSION }}.tar.gz
if-no-files-found: error
10 changes: 10 additions & 0 deletions Releases_Notes.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,14 @@

## v3.4.2

**Linux 安装包的控制台日志轮转**

- 修复 `bin/startup.sh` 重启时清空 `logs/start.out` 的问题。启动命令和 Java 的标准输出、标准错误继续写入原路径,便于排查跨重启的问题。[#1267](https://github.com/didi/KnowStreaming/issues/1267)
- 安装包新增 `conf/start-out.logrotate`。启用后每 5 分钟检查一次,按天归档,超过 100 MiB 可提前分卷。默认在轮转时清理超过 7 天的归档,最多保留 14 份压缩文件。管理员可按磁盘容量和日志量调整。
- 已部署的实例可以单独安装轮转配置,运行中的 Java 进程无需重启。请按安装目录修改日志路径,并以服务账号启用定时任务。启动脚本的修改在下次启动时生效。[安装包配置说明](km-dist/README.md)

---

## v3.4.0


Expand Down
22 changes: 9 additions & 13 deletions bin/startup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -62,21 +62,17 @@ JAVA_OPT="${JAVA_OPT} --server.max-http-header-size=524288"



if [ ! -d "${BASE_DIR}/logs" ]; then
mkdir ${BASE_DIR}/logs
fi
LOG_DIR="${BASE_DIR}/logs"
START_OUT="${LOG_DIR}/start.out"

echo "$JAVA ${JAVA_OPT}"
mkdir -p "${LOG_DIR}" || error_exit "Cannot create log directory ${LOG_DIR}"

# check the start.out log output file
if [ ! -f "${BASE_DIR}/logs/start.out" ]; then
touch "${BASE_DIR}/logs/start.out"
fi

# start
echo -e "---- 启动脚本 ------\n $JAVA ${JAVA_OPT}" > ${BASE_DIR}/logs/start.out 2>&1 &
echo "$JAVA ${JAVA_OPT}"

# Write the marker before the JVM starts so its first messages follow it.
printf '%s\n' "---- 启动脚本 $(date '+%Y-%m-%d %H:%M:%S') ------" "$JAVA ${JAVA_OPT}" >> "${START_OUT}" \
|| error_exit "Cannot write startup marker to ${START_OUT}"

nohup $JAVA ${JAVA_OPT} >> ${BASE_DIR}/logs/start.out 2>&1 &
nohup "$JAVA" ${JAVA_OPT} >> "${START_OUT}" 2>&1 &

echo "${WEB_SERVER} is starting,you can check the ${BASE_DIR}/logs/start.out"
echo "${WEB_SERVER} is starting,you can check the ${START_OUT}"
2 changes: 2 additions & 0 deletions docs/install_guide/单机部署手册.md
Original file line number Diff line number Diff line change
Expand Up @@ -415,3 +415,5 @@ password: Didi_km_678
cd /data/KnowStreaming/bin/
sh startup.sh
```

启动日志以追加方式写入 `/data/KnowStreaming/logs/start.out`。如需按天或按大小轮转该文件,可使用安装包内的 `conf/start-out.logrotate`,启用步骤见安装包根目录 `README.md` 的“start.out 日志轮转”一节([km-dist/README.md](../../km-dist/README.md))。
27 changes: 26 additions & 1 deletion km-dist/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
- conf:配置
- application.yml:配置文件
- logback-spring.xml:日志配置
- start-out.logrotate:logs/start.out 的 logrotate 轮转配置(可选启用,见下文)


- helm:安装KnowStreaming的Helm相关文件
Expand All @@ -23,4 +24,28 @@
- ks-km.jar:KnowStreaming的Jar包


- LICENSE:开源协议
- LICENSE:开源协议


## start.out 日志轮转(Linux)

`bin/startup.sh` 将 Java 的标准输出和标准错误追加到 `logs/start.out`。每次启动都会写入时间和启动命令。已有实例也可以单独启用以下配置,运行中的 Java 进程无需重启。

1. 确认 Linux 主机已安装 logrotate,并在 `conf/start-out.logrotate` 中填写实际日志文件的绝对路径。下面以安装目录 `/data/KnowStreaming` 为例。
2. 以能读写日志文件和日志目录的服务账号执行以下命令。为这个实例使用单独的状态文件。按 `command -v` 的输出替换示例中的可执行文件路径。

```sh
command -v logrotate
mkdir -p "$HOME/.local/state/knowstreaming" && chmod 700 "$HOME/.local/state/knowstreaming"
/usr/sbin/logrotate -d -s "$HOME/.local/state/knowstreaming/start-out.status" "/data/KnowStreaming/conf/start-out.logrotate"
```

3. 预演通过后,在同一账号的 `crontab -e` 中添加以下一行,并确认该文件只由这一项任务轮转。保存配置文件后需要启用调度才会生效。

```cron
*/5 * * * * /usr/sbin/logrotate -s "$HOME/.local/state/knowstreaming/start-out.status" "/data/KnowStreaming/conf/start-out.logrotate"
```

任务每 5 分钟检查一次,按天归档。检查时超过 `maxsize 100M` 会提前分卷,两次检查之间文件仍可超过阈值。默认压缩归档,在轮转时清理超过 `maxage 7` 天的文件,最多保留 `rotate 14` 份。高日志量时,14 份可能覆盖不足 7 天。以上参数均可按需调整。

`copytruncate` 让原进程继续写同一个文件,仍可用 `tail -f logs/start.out` 跟踪。复制与截短之间可能丢失少量输出。首次处理已有大文件时,请预留足以容纳一份完整副本的空间。
5 changes: 5 additions & 0 deletions km-dist/ReleaseKnowStreaming.xml
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@
<source>README.md</source>
<outputDirectory>.</outputDirectory>
</file>
<file>
<source>conf/start-out.logrotate</source>
<outputDirectory>conf</outputDirectory>
<fileMode>0644</fileMode>
</file>
<file>
<source>../LICENSE</source>
<outputDirectory>.</outputDirectory>
Expand Down
13 changes: 13 additions & 0 deletions km-dist/conf/start-out.logrotate
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Set the absolute path for this installation. See README.md for scheduling.
"/data/KnowStreaming/logs/start.out" {
daily
maxsize 100M
rotate 14
maxage 7
dateext
dateformat -%Y%m%d-%s
compress
copytruncate
missingok
notifempty
}
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
</parent>

<properties>
<revision>3.4.0</revision>
<revision>3.4.2</revision>

<maven.compiler.source>8</maven.compiler.source>
<maven.compiler.target>8</maven.compiler.target>
Expand Down
54 changes: 54 additions & 0 deletions tests/start-out.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#!/usr/bin/env bash
set -eu

root=$(cd "$(dirname "$0")/.." && pwd)
work=$(mktemp -d)
trap 'rm -rf "$work"' EXIT
export JAVA_HOME="$work/jdk"
mkdir -p "$work/bin" "$work/logs" "$JAVA_HOME/bin"
cp "$root/bin/startup.sh" "$work/bin/"
cat > "$JAVA_HOME/bin/java" <<'JAVA'
#!/bin/sh
if [ "$1" = "-version" ]; then
echo 'openjdk version "11.0.0"' >&2
exit 0
fi
echo "stdout $LAUNCH"
echo "stderr $LAUNCH" >&2
touch "$DONE"
JAVA
chmod +x "$JAVA_HOME/bin/java"

log="$work/logs/start.out"
echo 'prior history' > "$log"
for launch in first second; do
LAUNCH="$launch" DONE="$work/$launch.done" JAVA_OPT="-Dtest.launch=$launch" \
bash "$work/bin/startup.sh" > /dev/null
for ((i=0; i<100; i++)); do
[ -e "$work/$launch.done" ] && break
sleep 0.05
done
test -f "$work/$launch.done" || { echo "Mock JVM did not finish: $launch" >&2; exit 1; }
done

# Keep just the launch command and the two streams from each JVM.
awk '/-Dtest[.]launch=first/ {print "start first"}
/-Dtest[.]launch=second/ {print "start second"}
/^(prior history|stdout |stderr )/ {print}' "$log" > "$work/observed"
diff -u - "$work/observed" <<'EXPECTED'
prior history
start first
stdout first
stderr first
start second
stdout second
stderr second
EXPECTED

mv "$log" "$work/saved.log"
mkdir "$log" # An unwritable file target, even when the test runs as root.
if LAUNCH=failed DONE="$work/failed.done" bash "$work/bin/startup.sh" > /dev/null 2>&1; then
echo 'Startup reported success with an unwritable log target' >&2
exit 1
fi
echo 'start.out regression checks passed'
Loading