Skip to content

Commit 948a34a

Browse files
committed
Implement a cache daemon that can move the cache into place quickly on demand
- a long running background process we'll deploy as a daemonset on k8s nodes - moves the cache into place via hardlinking the golden copy from a shared location - operates using the host filesystem, and thus has pretty priviledged access
1 parent 6eb8008 commit 948a34a

24 files changed

+980
-37
lines changed

Contributing.md

+4
Original file line numberDiff line numberDiff line change
@@ -182,3 +182,7 @@ We also need to build the server docker image and push it to Gadget's container
182182
```bash
183183
make upload-container-image version=0.0.x
184184
```
185+
186+
### Getting PASETO tokens locally
187+
188+
You can sign PASETO tokens locally with this handy online tool: https://token.dev/paseto/. Ensure you use the V2 algorithm in the public mode, and copy the PASTEO public and private key from the `development` folder.

Dockerfile

+5-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ RUN go mod download
2727

2828
# copy everything else and build the project
2929
COPY . ./
30-
RUN make release/server_linux_$TARGETARCH
30+
RUN make release/server_linux_$TARGETARCH release/cached_linux_$TARGETARCH
3131

3232
FROM buildpack-deps:bullseye AS build-release-stage
3333
ARG TARGETARCH
@@ -48,11 +48,14 @@ RUN mkdir -p /home/main/secrets
4848
VOLUME /home/main/secrets/tls
4949
VOLUME /home/main/secrets/paseto
5050

51+
COPY --from=build-stage /app/release/cached_linux_${TARGETARCH} cached
5152
COPY --from=build-stage /app/release/server_linux_${TARGETARCH} server
53+
5254
COPY migrations migrations
5355
COPY entrypoint.sh entrypoint.sh
5456

55-
# smoke test -- ensure the server command can run
57+
# smoke test -- ensure the commands can run
5658
RUN ./server --help
59+
RUN ./cached --help
5760

5861
ENTRYPOINT ["./entrypoint.sh"]

Makefile

+21-8
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@ DB_USER ?= postgres
88
DB_PASS ?= password
99
DB_URI := postgres://$(DB_USER):$(DB_PASS)@$(DB_HOST):5432/dl
1010

11-
GRPC_PORT ?= 5051
1211
GRPC_HOST ?= localhost
12+
GRPC_PORT ?= 5051
13+
GRPC_CACHED_PORT ?= 5053
1314

14-
DEV_TOKEN_ADMIN ?= v2.public.eyJzdWIiOiJhZG1pbiIsImlhdCI6IjIwMjEtMTAtMTVUMTE6MjA6MDAuMDM0WiJ9WtEey8KfQQRy21xoHq1C5KQatEevk8RxS47k4bRfMwVCPHumZmVuk6ADcfDHTmSnMtEGfFXdxnYOhRP6Clb_Dw
15-
DEV_TOKEN_PROJECT_1 ?= v2.public.eyJzdWIiOiIxIiwiaWF0IjoiMjAyMS0xMC0xNVQxMToyMDowMC4wMzVaIn2MQ14RfIGpoEycCuvRu9J3CZp6PppUXf5l5w8uKKydN3C31z6f6GgOEPNcnwODqBnX7Pjarpz4i2uzWEqLgQYD
15+
DEV_TOKEN_ADMIN ?= v2.public.eyJzdWIiOiJhZG1pbiJ9yt40HNkcyOUtDeFa_WPS6vi0WiE4zWngDGJLh17TuYvssTudCbOdQEkVDRD-mSNTXLgSRDXUkO-AaEr4ZLO4BQ
16+
DEV_TOKEN_PROJECT_1 ?= v2.public.eyJzdWIiOiIxIn2jV7FOdEXafKDtAnVyDgI4fmIbqU7C1iuhKiL0lDnG1Z5-j6_ObNDd75sZvLZ159-X98_mP4qvwzui0w8pjt8F
17+
DEV_SHARED_READER_TOKEN ?= v2.public.eyJzdWIiOiJzaGFyZWQtcmVhZGVyIn1CxWdB02s9el0Wt7qReARZ-7JtIb4Zj3D4Oiji1yXHqj0orkpbcVlswVUiekECJC16d1NrHwD2FWSwRORZn8gK
1618

1719
PKG_GO_FILES := $(shell find pkg/ -type f -name '*.go')
1820
INTERNAL_GO_FILES := $(shell find internal/ -type f -name '*.go')
@@ -63,7 +65,7 @@ development/server.key:
6365

6466
development/server.crt: development/server.key
6567

66-
build: internal/pb/fs.pb.go internal/pb/fs_grpc.pb.go bin/server bin/client development/server.crt
68+
build: internal/pb/fs.pb.go internal/pb/fs_grpc.pb.go internal/pb/cache.pb.go internal/pb/cache_grpc.pb.go bin/server bin/client bin/cached development/server.crt
6769

6870
lint:
6971
golangci-lint run
@@ -86,8 +88,9 @@ release/migrations.tar.gz: migrations/*
8688
tar -zcf $@ migrations
8789

8890
release: build
89-
release: release/server_linux_amd64 release/server_macos_amd64 release/server_macos_arm64
90-
release: release/client_linux_amd64 release/client_macos_amd64 release/client_macos_arm64
91+
release: release/server_linux_amd64 release/server_macos_amd64 release/server_macos_arm64 release/server_linux_arm64
92+
release: release/client_linux_amd64 release/client_macos_amd64 release/client_macos_arm64 release/client_linux_arm64
93+
release: release/cached_linux_amd64 release/cached_macos_amd64 release/cached_macos_arm64 release/cached_linux_arm64
9194
release: release/migrations.tar.gz
9295

9396
test: export DB_URI = postgres://$(DB_USER):$(DB_PASS)@$(DB_HOST):5432/dl_tests
@@ -121,6 +124,11 @@ server-profile: export DL_ENV=dev
121124
server-profile: internal/pb/fs.pb.go internal/pb/fs_grpc.pb.go
122125
go run cmd/server/main.go --dburi $(DB_URI) --port $(GRPC_PORT) --profile cpu.prof --log-level info
123126

127+
cached: export DL_ENV=dev
128+
cached: export DL_TOKEN=$(DEV_SHARED_READER_TOKEN)
129+
cached: internal/pb/cache.pb.go internal/pb/cache_grpc.pb.go
130+
go run cmd/cached/main.go --upstream-host $(GRPC_HOST) --upstream-port $(GRPC_PORT) --port $(GRPC_CACHED_PORT) --staging-path tmp/cache-stage
131+
124132
client-update: export DL_TOKEN=$(DEV_TOKEN_PROJECT_1)
125133
client-update: export DL_SKIP_SSL_VERIFICATION=1
126134
client-update:
@@ -169,6 +177,11 @@ client-getcache: export DL_SKIP_SSL_VERIFICATION=1
169177
client-getcache:
170178
go run cmd/client/main.go getcache --host $(GRPC_HOST) --path input/cache
171179

180+
client-getcache-from-daemon: export DL_TOKEN=$(DEV_TOKEN_ADMIN)
181+
client-getcache-from-daemon: export DL_SKIP_SSL_VERIFICATION=1
182+
client-getcache-from-daemon:
183+
mkdir -p tmp/pods/test-pod/volumes/example && go run cmd/client/main.go getcache-from-daemon --host $(GRPC_HOST) --port $(GRPC_CACHED_PORT) input/cache
184+
172185
client-gc-contents: export DL_TOKEN=$(DEV_TOKEN_ADMIN)
173186
client-gc-contents: export DL_SKIP_SSL_VERIFICATION=1
174187
client-gc-contents:
@@ -242,8 +255,8 @@ else
242255
cd js && npm install
243256
endif
244257

245-
js/src/pb: $(PROTO_FILES)
246-
cd js && mkdir -p ./src/pb && npx protoc --experimental_allow_proto3_optional --ts_out ./src/pb --ts_opt long_type_bigint,ts_nocheck,eslint_disable,add_pb_suffix --proto_path ../internal/pb/ ../$^
258+
js/src/pb: internal/pb/fs.proto
259+
cd js && mkdir -p ./src/pb && npx protoc --experimental_allow_proto3_optional --ts_out ./src/pb --ts_opt long_type_bigint,ts_nocheck,eslint_disable,add_pb_suffix --proto_path ../internal/pb/ ../internal/pb/fs.proto
247260

248261
js/dist: js/node_modules js/src/pb
249262
cd js && npm run build

cmd/cached/main.go

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package main
2+
3+
import "github.com/gadget-inc/dateilager/pkg/cli"
4+
5+
func main() {
6+
cli.CacheDaemonExecute()
7+
}

development/paseto.key

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
-----BEGIN PRIVATE KEY-----
2+
MC4CAQAwBQYDK2VwBCIEILTL+0PfTOIQcn2VPkpxMwf6Gbt9n4UEFDjZ4RuUKjd0
3+
-----END PRIVATE KEY-----

development/paseto.pub

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
-----BEGIN PUBLIC KEY-----
2-
MCowBQYDK2VwAyEASKQkA/AxlNCdOHTnp5McesmQ+y756VTtGz8Xrt1G0fs=
3-
-----END PUBLIC KEY-----
2+
MCowBQYDK2VwAyEAHrnbu7wEfAP9cGBOAHHwmH4Wsot1ciXBHwBBXQ4gsaI=
3+
-----END PUBLIC KEY-----

flake.nix

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@
5454

5555
postgresql = pkgs.postgresql_14;
5656

57+
glibcLocales = pkgs.glibcLocales;
5758
## DateiLager outputs
5859

5960
dateilager = callPackage ./. {
@@ -72,6 +73,7 @@
7273
flake.packages.postgresql
7374
flake.packages.dev
7475
flake.packages.clean
76+
flake.packages.glibcLocales
7577
git
7678
protobuf
7779
protoc-gen-go

internal/key/key.go

+14
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package key
22

33
import (
4+
"time"
5+
46
"github.com/gadget-inc/dateilager/pkg/stringutil"
57
"go.opentelemetry.io/otel/attribute"
68
"go.uber.org/zap"
@@ -36,7 +38,9 @@ const (
3638
Worker = IntKey("dl.worker")
3739
WorkerCount = IntKey("dl.worker_count")
3840
Ignores = StringSliceKey("dl.ignores")
41+
DurationMS = DurationKey("dl.duration_ms")
3942
CloneToProject = Int64Key("dl.clone_to_project")
43+
CachePath = StringKey("dl.cache_path")
4044
)
4145

4246
var (
@@ -148,3 +152,13 @@ func (isk Int64SliceKey) Field(value []int64) zap.Field {
148152
func (isk Int64SliceKey) Attribute(value []int64) attribute.KeyValue {
149153
return attribute.Int64Slice(string(isk), value)
150154
}
155+
156+
type DurationKey string
157+
158+
func (dk DurationKey) Field(value time.Duration) zap.Field {
159+
return zap.Duration(string(dk), value)
160+
}
161+
162+
func (dk DurationKey) Attribute(value time.Duration) attribute.KeyValue {
163+
return attribute.Float64(string(dk), float64(value.Milliseconds()))
164+
}

internal/pb/cache.pb.go

+216
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)