Skip to content
This repository was archived by the owner on Dec 12, 2023. It is now read-only.

Commit e3d90e3

Browse files
committed
Fixed tracing bug
The copying operation is now run as a separate script so that the emitted traces actually close.
1 parent 48d230e commit e3d90e3

File tree

7 files changed

+111
-105
lines changed

7 files changed

+111
-105
lines changed

.dockerignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
*
22
!docker-build-resources/etc
33
!docker-build-resources/opentelemetry-shell/library
4-
!docker-build-resources/sync.sh
4+
!docker-build-resources/copy-database.sh
5+
!docker-build-resources/watch.sh

Dockerfile

+2-1
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ ENV LITEFS_INTERNAL_DATA_DIRECTORY=/var/lib/litefs
2727

2828
WORKDIR /
2929
COPY docker-build-resources/opentelemetry-shell /opentelemetry-shell
30-
COPY docker-build-resources/sync.sh /sync.sh
30+
COPY docker-build-resources/watch.sh /watch.sh
31+
COPY docker-build-resources/copy-database.sh /copy-database.sh
3132

3233
ENTRYPOINT litefs mount
+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
#!/bin/bash
2+
3+
# This script copies a synced database to the host and emits an Otel signal.
4+
5+
set -e
6+
set +v
7+
set +x
8+
9+
# OpenTelemetry setup
10+
export OTEL_EXPORTER_OTEL_ENDPOINT="http://host.docker.internal:4318"
11+
export OTEL_SH_LIB_PATH="opentelemetry-shell/library"
12+
# shellcheck disable=SC2034
13+
service_version=$OTEL_SERVICE_VERSION
14+
. opentelemetry-shell/library/otel_traces.sh
15+
16+
Normal='\033[0m'
17+
Red='\033[0;31m'
18+
Green='\033[0;32m'
19+
Yellow='\033[0;33m'
20+
21+
name="$1"
22+
path="$LITEFS_DB_DIRECTORY/$name"
23+
24+
echo "Attempting to copy $path..."
25+
26+
if test -f "$path"; then
27+
sourcePath="$LITEFS_DB_DIRECTORY/$name"
28+
destPath="/host-data/$(date +"%s")-$name"
29+
30+
stat "$sourcePath"
31+
32+
if test -s "$sourcePath"; then
33+
# NOTE: We execute the restore in the container since it is more performant than restoring on a host volume.
34+
echo -e "${Yellow}Copying $sourcePath to $destPath...${Normal}"
35+
36+
# Create an Otel span
37+
generatedAt=$(sqlite3 "$sourcePath" "SELECT generated_at FROM metadata;")
38+
schemaVersion=$(sqlite3 "$sourcePath" "SELECT schema_version FROM metadata;")
39+
# shellcheck disable=SC2034
40+
span_name="Copy domain data to host"
41+
# shellcheck disable=SC2034
42+
custom_resource_attributes=(
43+
"domain_data_generated_at:$generatedAt"
44+
"domain_data_schema_version:$schemaVersion"
45+
)
46+
linkedTraceId=$(sqlite3 "$sourcePath" "SELECT linked_trace_id FROM metadata;")
47+
linkedTraceState=$(sqlite3 "$sourcePath" "SELECT linked_trace_state FROM metadata;")
48+
linkedSpanId=$(sqlite3 "$sourcePath" "SELECT linked_span_id FROM metadata;")
49+
echo "[Linked Span Data] linkedTraceId: $linkedTraceId, linkedTraceState: $linkedTraceState, linkedSpanId: $linkedSpanId"
50+
# shellcheck disable=SC2034
51+
linked_span=("$linkedTraceId" "$linkedSpanId" "$linkedTraceState")
52+
otel_trace_start_parent_span cp "$sourcePath" "$destPath"
53+
54+
echo -e "${Green}Successfully copied ${name} to ${destPath}$Normal"
55+
sqlite3 "$destPath" ".mode table" "SELECT * FROM metadata;"
56+
sqlite3 "$destPath" "SELECT COUNT(*) || ' departments' FROM departments;"
57+
sqlite3 "$destPath" "SELECT COUNT(*) || ' tax rates' FROM tax_rates;"
58+
sqlite3 "$destPath" "SELECT COUNT(*) || ' products' FROM products;"
59+
sqlite3 "$destPath" "SELECT COUNT(*) || ' product barcodes' FROM product_barcodes;"
60+
sqlite3 "$destPath" "SELECT COUNT(*) || ' promotions' FROM promotions;"
61+
sqlite3 "$destPath" "SELECT COUNT(*) || ' offers' FROM offers;"
62+
sqlite3 "$destPath" "SELECT COUNT(*) || ' offer benefits' FROM offer_benefits;"
63+
sqlite3 "$destPath" "SELECT COUNT(*) || ' offer conditions' FROM offer_conditions;"
64+
sqlite3 "$destPath" "SELECT COUNT(*) || ' product ranges' FROM product_ranges;"
65+
sqlite3 "$destPath" ".mode table" "ANALYZE; SELECT * FROM sqlite_stat1;"
66+
67+
echo -e "${Yellow}Deleting all but the latest ${FILE_RETENTION_COUNT} files from /host-data$Normal"
68+
tailNum=$(( FILE_RETENTION_COUNT + 1 ))
69+
cd /host-data
70+
# Source: https://stackoverflow.com/a/34862475/592820
71+
ls -tp | grep -v '/$' | tail -n +${tailNum} | xargs -I {} rm -- {}
72+
else
73+
echo -e "$Red$sourcePath has not been fully replicated from LiteFS Cloud$Normal"
74+
fi
75+
else
76+
echo -e "$Red$path does not yet exist$Normal"
77+
fi

docker-build-resources/etc/litefs.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,4 @@ lease:
1919
# and false on the replicas.
2020
candidate: $IS_PRIMARY
2121

22-
exec: "bash sync.sh"
22+
exec: "bash watch.sh"

docker-build-resources/sync.sh

-101
This file was deleted.

docker-build-resources/watch.sh

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
#!/bin/bash
2+
3+
# This script watches the LiteFS directory for changes to trigger
4+
# copying the synced database to the host machine.
5+
6+
set -e
7+
set +v
8+
set +x
9+
10+
Normal='\033[0m'
11+
Underline='\033[4m'
12+
Red='\033[0;31m'
13+
14+
echo "Started watch.sh"
15+
16+
if [ -z "$LITEFS_CLOUD_TOKEN" ]
17+
then
18+
echo -e "${Red}${Underline}The LITEFS_CLOUD_TOKEN environment variable is not set! Make sure it is present in user defaults, and Bash can access them.$Normal"
19+
fi
20+
21+
# Perform an initial copy attempt
22+
bash copy-database.sh "$DOMAIN_DB_NAME"
23+
24+
# NOTE: We watch the internal data directory because the DB directory uses FUSE,
25+
# and cannot be easily monitored with fswatch.
26+
watched_path="$LITEFS_INTERNAL_DATA_DIRECTORY/dbs/$DOMAIN_DB_NAME/database"
27+
echo "Running fswatch for $watched_path"
28+
fswatch -or "$watched_path" | while read f; do echo "Change detected in $f files" && bash copy-database.sh "$DOMAIN_DB_NAME"; done

voripos-domain-sync.sh

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ set +x
1010
# otherwise, the script will fail. This is not an issue on POS machines where Docker authentication is not configured.
1111
export PATH="/usr/local/bin:/opt/homebrew/bin:$PATH"
1212

13-
export VORIPOS_DOMAIN_SYNC_VERSION=0.4.1
13+
export VORIPOS_DOMAIN_SYNC_VERSION=0.4.2
1414

1515
# NOTE: Bash must be given Full Disk Access in order to read the user defaults.
1616
# See https://www.kith.org/jed/2022/02/15/launchctl-scheduling-shell-scripts-on-macos-and-full-disk-access/.

0 commit comments

Comments
 (0)