Skip to content

Commit

Permalink
Run an ephemeral fdbserver in CI tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nktpro authored Feb 5, 2025
1 parent cfc690f commit 088f8f4
Show file tree
Hide file tree
Showing 6 changed files with 158 additions and 47 deletions.
10 changes: 8 additions & 2 deletions .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,20 @@ defaults:
jobs:
build:
name: Build
runs-on: [ self-hosted, nix, chopsticks, amd64-linux, medium ]
runs-on: [ self-hosted, nix, amd64-linux, medium ]
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Set environment variables
run: |
XDG_CACHE_HOME=${XDG_CACHE_HOME:-"$HOME/.cache"}
echo "PROTOC_CACHE=${XDG_CACHE_HOME}/protoc_cache" >> "${GITHUB_ENV}"
echo "COURSIER_CACHE=${XDG_CACHE_HOME}/coursier" >> "${GITHUB_ENV}"
echo "SBT_OPTS=-Dsbt.global.base=${XDG_CACHE_HOME}/sbt -Dsbt.ivy.home=${XDG_CACHE_HOME}/ivy -Xmx4g -Xss6m -XX:-UseContainerSupport" >> "${GITHUB_ENV}"
echo "FDB_CLUSTER_FILE=$PWD/.fdb/cluster.file" >> "${GITHUB_ENV}"
- name: Check code quality
run: |
Expand All @@ -37,7 +40,10 @@ jobs:
# trap : TERM INT; sleep infinity & wait

- name: Test
run: sbt --client test
run: |
FDBSERVER_PID=$(./cli.sh start_ephemeral_fdb_server) || exit $?
trap "kill -15 $FDBSERVER_PID" EXIT
sbt --client test
- name: Publish
if: github.ref == 'refs/heads/master'
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ metals.sbt
/.dev-sdks
/.env
/result
/.fdb
104 changes: 104 additions & 0 deletions cli.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
#!/usr/bin/env bash
set -euo pipefail

export FDB_CLUSTER_FILE="${FDB_CLUSTER_FILE:-"$(dirname "$(realpath "$0")")/.fdb/cluster.file"}"

start_ephemeral_fdb_server() {
local background=${1:-"background"}
local data_dir pid

# shellcheck disable=SC2317
cleanup() {
local exit_code=$?
if [[ -n "${pid:-}" ]] && kill -0 "$pid" 2>/dev/null; then
echo "Stopping fdbserver (PID: $pid)" >&2
kill -15 "$pid" || true
fi
if [[ -n "${data_dir:-}" ]] && [[ -d "$data_dir" ]]; then
rm -rf "$data_dir"
fi
exit "$exit_code"
}
trap cleanup EXIT

if [[ -z "${FDB_CLUSTER_FILE:-}" ]]; then
echo "Error: FDB_CLUSTER_FILE environment variable not set" >&2
exit 1
fi

data_dir="$(mktemp -d)" || {
echo "Error: Failed to create temporary directory" >&2
exit 1
}
chmod 700 "$data_dir"

mkdir -p "$data_dir/data" "$data_dir/trace" || {
echo "Error: Failed to create data directories" >&2
exit 1
}
chmod 700 "$data_dir/data" "$data_dir/trace"

local fdb_port=4500
while { echo >/dev/tcp/127.0.0.1/"$fdb_port"; } 2>/dev/null; do
echo "Port $fdb_port is in use, trying another port..." >&2
fdb_port=$((fdb_port + 1))
done

local cluster_dir
cluster_dir="$(dirname "$FDB_CLUSTER_FILE")"
mkdir -p "$cluster_dir" || {
echo "Error: Failed to create cluster file directory" >&2
exit 1
}

local connection_string="t17x3130g3ju1xwxnnwaal6e029grtel:o7q2o6qe@127.0.0.1:$fdb_port"
echo "Generated connection string: $connection_string" >&2
echo "$connection_string" >"$FDB_CLUSTER_FILE"
chmod 600 "$FDB_CLUSTER_FILE"

local fdbserver_path
fdbserver_path="$(command -v fdbserver)"
local -a cmd=(
"$fdbserver_path"
--cluster_file "$FDB_CLUSTER_FILE"
--datadir "$data_dir/data"
--logdir "$data_dir/trace"
--listen_address "127.0.0.1:$fdb_port"
--public_address "127.0.0.1:$fdb_port"
)

if [[ "$(uname -s)" =~ [dD]arwin ]]; then
echo "macOS detected, requesting sudo access..." >&2
if ! sudo -v; then
echo "Error: Failed to validate sudo access" >&2
exit 1
fi
sudo "${cmd[@]}" >&2 &
else
"${cmd[@]}" >&2 &
fi

pid=$!
echo "FDB server process pid=$pid" >&2

if ! timeout 15 fdbcli --exec "status" >&2; then
echo "Timed out after 15s checking for FDB status" >&2
exit 1
fi

echo "Configuring the cluster.." >&2
if ! fdbcli --exec "configure new single ssd-2" >&2; then
echo "Error: Failed to configure cluster" >&2
exit 1
fi

echo "FDB server started successfully (PID: $pid)" >&2

if [[ "$background" == "background" ]]; then
echo "$pid"
else
wait "$pid"
fi
}

"$@"
61 changes: 30 additions & 31 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 14 additions & 13 deletions flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -5,28 +5,29 @@
hotPot.url = "github:shopstic/nix-hot-pot";
nixpkgs.follows = "hotPot/nixpkgs";
flakeUtils.follows = "hotPot/flakeUtils";
fdb.follows = "hotPot/fdbPkg";
fdbPkg.follows = "hotPot/fdbPkg";
};

outputs = { self, nixpkgs, flakeUtils, hotPot, fdb }:
outputs = { self, nixpkgs, flakeUtils, hotPot, fdbPkg }:
flakeUtils.lib.eachDefaultSystem
(system:
let
pkgs = import nixpkgs {
inherit system;
overlays = [
(final: prev: {
maven = prev.maven.override {
jdk = prev.jdk11;
};
})
];
# overlays = [
# (final: prev: {
# maven = prev.maven.override {
# jdk = prev.jdk11;
# };
# })
# ];
};
hotPotPkgs = hotPot.packages.${system};
hotPotLib = hotPot.lib.${system};
chopsticksSystem = if system == "aarch64-linux" then "x86_64-linux" else system;
chopsticksPkgs = import nixpkgs { system = chopsticksSystem; };

fdbLib = fdb.packages.${system}.fdb_7.lib;
fdb = fdbPkg.packages.${system}.fdb_7;
fdbLib = fdb.lib;
jdkArgs = [
"--set DYLD_LIBRARY_PATH ${fdbLib}"
"--set LD_LIBRARY_PATH ${fdbLib}"
Expand All @@ -36,7 +37,7 @@
# "--set FDB_NETWORK_OPTION_TRACE_ENABLE /Users/nktpro/Downloads/fdb7"
''--set JDK_JAVA_OPTIONS "-DFDB_LIBRARY_PATH_FDB_JAVA=${fdbLib}/libfdb_java.${if pkgs.stdenv.isDarwin then "jnilib" else "so"}"''
];
jdk = pkgs.callPackage hotPot.lib.wrapJdk {
jdk = pkgs.callPackage hotPotLib.wrapJdk {
jdk = pkgs.jdk11;
args = pkgs.lib.concatStringsSep " " (jdkArgs ++ [''--run "if [[ -f ./.env ]]; then source ./.env; fi"'']);
};
Expand Down Expand Up @@ -86,7 +87,7 @@
'';
buildInputs = builtins.attrValues
{
inherit jdk sbt;
inherit fdb jdk sbt;
inherit (pkgs)
jq
parallel
Expand Down
2 changes: 1 addition & 1 deletion project/Dependencies.scala
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ object Dependencies {
)

val lmdbDeps = Seq(
"org.lmdbjava" % "lmdbjava" % "0.8.3"
"org.lmdbjava" % "lmdbjava" % "0.9.0"
)

val fdbDeps = Seq(
Expand Down

0 comments on commit 088f8f4

Please sign in to comment.