Skip to content
Open
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
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ web/node_modules
web/dist
web/assets-source
silo
silo-darwin-arm64
streamapp
*.md
.playwright-cli/
6 changes: 5 additions & 1 deletion .github/actions/ci-tools/go-checks.mk
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,13 @@ lint:
$(MAKE) -f Makefile lint-router-recovery

# The whole tree, so a change that breaks vet in a package it did not touch
# still fails; lint-changed reports only changed lines.
# still fails; lint-changed reports only changed lines. The macOS node build
# is vetted too, since Linux never compiles the darwin installer, and its
# cross-build fails on a new cgo import or a gap in imageutil's node stub.
vet:
go vet ./...
GOOS=darwin GOARCH=arm64 CGO_ENABLED=0 go vet -tags silo_node ./cmd/silo/ ./internal/nodeinstall/ ./internal/nodebuild/ ./internal/imageutil/ ./web/
$(MAKE) -f Makefile build-node-darwin NODE_DARWIN_OUT=/dev/null

# Package assertions belong to the tests lane; these check generated artifacts.
contracts:
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# Binary
/silo
/silo-darwin-arm64
*.exe

# IDE
Expand Down
28 changes: 28 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,32 @@ RUN --network=none go build \
-ldflags "-X github.com/Silo-Server/silo-server/internal/buildinfo.revisionOverride=${BUILD_REVISION} -X github.com/Silo-Server/silo-server/internal/buildinfo.dirtyOverride=${BUILD_DIRTY} -X github.com/Silo-Server/silo-server/internal/buildinfo.buildNumberOverride=${BUILD_NUMBER} -X github.com/Silo-Server/silo-server/internal/buildinfo.builtAtOverride=${BUILD_DATE}" \
-o /silo ./cmd/silo/

# The macOS transcode node (darwin/arm64), cross-compiled on the build host.
# The silo_node tag leaves out libvips and the web app, so it needs no cgo and
# links only system frameworks; the Go linker signs it ad hoc. A Mac copies it
# out of the image so it matches this server (docs/architecture/transcode-nodes.md).
FROM --platform=$BUILDPLATFORM golang:1.27 AS node_darwin
ENV GOPROXY=https://proxy.golang.org,direct
ENV GOPRIVATE=github.com/Silo-Server/*
ENV GONOSUMDB=github.com/Silo-Server/*
WORKDIR /app
COPY go.mod go.sum ./
COPY internal/compat/zishang520-webtransport-go/ internal/compat/zishang520-webtransport-go/
RUN go mod download
COPY internal/ internal/
COPY migrations/ migrations/
COPY contracts/ contracts/
COPY cmd/ cmd/
COPY web/embed.go web/embed_node.go web/
RUN --network=none CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -tags silo_node -trimpath -o /dev/null ./cmd/silo/
ARG BUILD_REVISION
ARG BUILD_DIRTY=false
ARG BUILD_NUMBER
ARG BUILD_DATE
RUN --network=none CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -tags silo_node -trimpath \
Comment thread
Quick104 marked this conversation as resolved.
Resolved
-ldflags "-s -w -X github.com/Silo-Server/silo-server/internal/buildinfo.revisionOverride=${BUILD_REVISION} -X github.com/Silo-Server/silo-server/internal/buildinfo.dirtyOverride=${BUILD_DIRTY} -X github.com/Silo-Server/silo-server/internal/buildinfo.buildNumberOverride=${BUILD_NUMBER} -X github.com/Silo-Server/silo-server/internal/buildinfo.builtAtOverride=${BUILD_DATE}" \
-o /silo-darwin-arm64 ./cmd/silo/

# Stage 3: Runtime
# CI rebuilds this stage without the layer cache (no-cache-filters: runtime in
# trusted-linux-build.yml) so every image picks up Debian security updates; the
Expand Down Expand Up @@ -134,6 +160,8 @@ RUN ln -sf ../lib/node_modules/npm/bin/npm-cli.js /usr/local/bin/npm && \
ENV NODE_OPTIONS=--use-openssl-ca
LABEL io.silo.intel-qsv-runtime="${INTEL_QSV_RUNTIME}"
COPY --from=build /silo /usr/local/bin/silo
# Not run here: a Mac copies it out to run a transcode node with VideoToolbox.
COPY --from=node_darwin /silo-darwin-arm64 /usr/share/silo/nodes/silo-darwin-arm64
EXPOSE 8080 8096 13378
# Silo runs database migrations before its HTTP listener starts, and an
# upgrade on a large library can migrate for many minutes. The start period
Expand Down
8 changes: 8 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,14 @@ frontend:
build: frontend
go build -ldflags "$(GO_LDFLAGS)" -o silo ./cmd/silo/

# Cross-build the macOS transcode node as the image's node_darwin stage does.
# Without cgo, a new cgo import or an imageutil function missing from its node
# stub fails here before the image build does.
.PHONY: build-node-darwin
NODE_DARWIN_OUT ?= silo-darwin-arm64
build-node-darwin: embed-stub
CGO_ENABLED=0 GOOS=darwin GOARCH=arm64 go build -tags silo_node -trimpath -ldflags "-s -w $(GO_LDFLAGS)" -o "$(NODE_DARWIN_OUT)" ./cmd/silo/

# Run frontend dev server (proxies API to localhost:8080)
dev-frontend:
cd web && pnpm run dev
Expand Down
22 changes: 21 additions & 1 deletion cmd/silo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,9 @@ import (
_ "github.com/Silo-Server/silo-server/internal/metadata/nfo"
"github.com/Silo-Server/silo-server/internal/models"
"github.com/Silo-Server/silo-server/internal/nodeauth"
"github.com/Silo-Server/silo-server/internal/nodebuild"
"github.com/Silo-Server/silo-server/internal/nodeconfig"
"github.com/Silo-Server/silo-server/internal/nodeinstall"
"github.com/Silo-Server/silo-server/internal/nodemetrics"
"github.com/Silo-Server/silo-server/internal/nodepool"
"github.com/Silo-Server/silo-server/internal/noderecipe"
Expand Down Expand Up @@ -158,7 +160,9 @@ import (
siloweb "github.com/Silo-Server/silo-server/web"
)

const serverModeTranscode = "transcode"
// nodeBuildOnly is what a node build answers anything but a transcode node
// run or a "silo node" subcommand.
const nodeBuildOnly = "this silo build runs only as a transcode node: install it with `silo node install`, or set MODE=transcode"

// serverModeProxy is MODE for a proxy node.
const serverModeProxy = "proxy"
Expand Down Expand Up @@ -801,6 +805,11 @@ func addRedisBootstrapSettings(redisURL, redisURLVar string, configured map[stri

// main starts the Silo server or a requested maintenance command.
func main() {
// A node build has no image processing or web app, so it runs no
// subcommand but its own; flags still reach the transcode node.
if nodebuild.Node && len(os.Args) > 1 && os.Args[1] != "node" && !strings.HasPrefix(os.Args[1], "-") {
log.Fatal(nodeBuildOnly)
}
var storageAdmission *pglock.NodeAdmission
// The admission session is detached from the pool and lives until process
// exit. Worker Stop methods do not all wait for in-flight writes, so closing
Expand Down Expand Up @@ -872,6 +881,14 @@ func main() {
return
}

if len(os.Args) > 1 && os.Args[1] == "node" {
if err := nodeinstall.Run(os.Args[2:], os.Stdin, os.Stdout); err != nil {
fmt.Fprintf(os.Stderr, "silo node: %v\n", err)
os.Exit(1)
}
return
}

envFile := flag.String("env", ".env", "path to .env bootstrap file")
migrateOnly := flag.Bool("migrate-only", false, "apply database migrations and exit")
migrateStatus := flag.Bool("migrate-status", false, "show database migration status and exit")
Expand Down Expand Up @@ -905,6 +922,9 @@ func main() {
runTranscodeNode()
return
}
if nodebuild.Node {
log.Fatal(nodeBuildOnly)
}

// Step 1: Bootstrap from .env
bc, err := config.LoadBootstrap(*envFile)
Expand Down
31 changes: 8 additions & 23 deletions cmd/silo/node_drain.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package main
import (
"context"
"log/slog"
"math"
"os"
"strconv"
"strings"
Expand All @@ -15,14 +14,6 @@ import (
"github.com/Silo-Server/silo-server/internal/transcodenode"
)

// nodeDrainTimeoutEnv bounds how long a transcode node that received SIGTERM
// keeps serving its sessions before it shuts down. 0 turns the drain off.
const nodeDrainTimeoutEnv = "SILO_NODE_DRAIN_TIMEOUT"

// defaultNodeDrainTimeout lets most episodes and a long tail of movie sessions
// finish, while keeping a rolling upgrade of several nodes bounded.
const defaultNodeDrainTimeout = 10 * time.Minute

// nodeDrainKeyCheckInterval is how often a SIGTERM drain asks whether the
// main server still accepts this node's key. The answer is an atomic read.
const nodeDrainKeyCheckInterval = time.Second
Expand All @@ -44,28 +35,22 @@ type nodeDrain struct {
// timeout, or not at all when it is 0. keyRejected ends the wait early; see
// nodeDrain.keyRejected.
func newTranscodeNodeDrain(srv *transcodenode.Server, keyRejected func() bool) *nodeDrain {
timeout := nodeDrainTimeout(os.Getenv(nodeDrainTimeoutEnv))
timeout := nodeDrainTimeout(os.Getenv(transcodenode.EnvDrainTimeout))
if timeout <= 0 {
return nil
}
return &nodeDrain{timeout: timeout, begin: srv.BeginShutdownDrain, wait: srv.WaitDrained, activeJobs: srv.ActiveJobs, keyRejected: keyRejected}
}

// nodeDrainTimeout parses SILO_NODE_DRAIN_TIMEOUT: a Go duration ("15m") or
// whole seconds ("900"). Empty or invalid means the default.
// nodeDrainTimeout parses SILO_NODE_DRAIN_TIMEOUT
// (transcodenode.ParseDrainTimeout). Invalid means the default.
func nodeDrainTimeout(value string) time.Duration {
value = strings.TrimSpace(value)
if value == "" {
return defaultNodeDrainTimeout
}
if seconds, err := strconv.Atoi(value); err == nil && seconds >= 0 && seconds <= math.MaxInt64/int(time.Second) {
return time.Duration(seconds) * time.Second
}
if timeout, err := time.ParseDuration(value); err == nil && timeout >= 0 {
return timeout
timeout, err := transcodenode.ParseDrainTimeout(value)
if err != nil {
slog.Warn("invalid node drain timeout; using the default", "env", transcodenode.EnvDrainTimeout, "value", strings.TrimSpace(value), "default", transcodenode.DefaultDrainTimeout)
return transcodenode.DefaultDrainTimeout
}
slog.Warn("invalid node drain timeout; using the default", "env", nodeDrainTimeoutEnv, "value", value, "default", defaultNodeDrainTimeout)
return defaultNodeDrainTimeout
return timeout
}

// run drains the node: it stops taking new work and waits until its work is
Expand Down
10 changes: 6 additions & 4 deletions cmd/silo/node_drain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,24 @@ import (
"testing"
"testing/synctest"
"time"

"github.com/Silo-Server/silo-server/internal/transcodenode"
)

func TestNodeDrainTimeout(t *testing.T) {
for _, test := range []struct {
value string
want time.Duration
}{
{"", defaultNodeDrainTimeout},
{"", transcodenode.DefaultDrainTimeout},
{"0", 0},
{"0s", 0},
{"900", 15 * time.Minute},
{"45m", 45 * time.Minute},
{" 2h ", 2 * time.Hour},
{"-5m", defaultNodeDrainTimeout},
{"99999999999", defaultNodeDrainTimeout}, // more seconds than a Duration holds
{"soon", defaultNodeDrainTimeout},
{"-5m", transcodenode.DefaultDrainTimeout},
{"99999999999", transcodenode.DefaultDrainTimeout}, // more seconds than a Duration holds
{"soon", transcodenode.DefaultDrainTimeout},
} {
if got := nodeDrainTimeout(test.value); got != test.want {
t.Errorf("nodeDrainTimeout(%q) = %v, want %v", test.value, got, test.want)
Expand Down
7 changes: 2 additions & 5 deletions cmd/silo/node_keys.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/Silo-Server/silo-server/internal/envutil"
"github.com/Silo-Server/silo-server/internal/nodepool"
"github.com/Silo-Server/silo-server/internal/secret"
"github.com/Silo-Server/silo-server/internal/transcodenode"
)

// startProxyNodeKeyRegistry loads a proxy's copy of the stream node credential
Expand All @@ -38,16 +39,12 @@ func startProxyNodeKeyRegistry(ctx context.Context, pool *pgxpool.Pool, cipher *
return registry
}

// envNodeKey is the transcode node's own key, issued by an administrator. The
// node verifies every request the core signs to it with this key.
const envNodeKey = "SILO_NODE_KEY"

// transcodeNodeKey reads and validates SILO_NODE_KEY, or the file
// SILO_NODE_KEY_FILE names. A transcode node refuses to start without one, or
// with one that is not a well-formed node key: it could verify nothing, so
// every request to it would fail.
func transcodeNodeKey(getenv func(string) string) (string, error) {
key, usedVar, err := envutil.LookupSecret(getenv, envNodeKey)
key, usedVar, err := envutil.LookupSecret(getenv, transcodenode.EnvNodeKey)
if err != nil {
return "", err
}
Expand Down
22 changes: 8 additions & 14 deletions cmd/silo/transcode_node.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"fmt"
"log/slog"
"net"
"net/url"
"os"
"os/signal"
"strings"
Expand Down Expand Up @@ -35,11 +34,6 @@ import (
"github.com/Silo-Server/silo-server/internal/tuning"
)

// envCoreURL is the URL a transcode node reaches the core at. With its own
// key (envNodeKey) it is all the node needs: everything else comes from the
// core.
const envCoreURL = "SILO_CORE_URL"

// transcodeNodeIgnoredEnv are variables a transcode node once read and now
// takes from the core or no longer needs: the database and Redis it read
// settings, recipes and deny markers from, the data cipher's key, and the URL
Expand Down Expand Up @@ -77,7 +71,7 @@ func transcodeNodeMode(envFile string) bool {
if envFile != "" {
_ = godotenv.Load(envFile)
}
return strings.TrimSpace(os.Getenv("MODE")) == serverModeTranscode
return strings.TrimSpace(os.Getenv("MODE")) == transcodenode.Mode
}

// transcodeNodeEnv is what a transcode node reads from its environment.
Expand All @@ -98,13 +92,13 @@ type transcodeNodeEnv struct {
// URL or key, or a key that is not a well-formed node key, is an error: the
// node could not even ask the core for its configuration.
func loadTranscodeNodeEnv(getenv func(string) string) (transcodeNodeEnv, error) {
env := transcodeNodeEnv{coreURL: strings.TrimSpace(getenv(envCoreURL))}
env := transcodeNodeEnv{coreURL: strings.TrimSpace(getenv(transcodenode.EnvCoreURL))}
if env.coreURL == "" {
return transcodeNodeEnv{}, fmt.Errorf("%s is required: the URL this node reaches the main server at", envCoreURL)
return transcodeNodeEnv{}, fmt.Errorf("%s is required: the URL this node reaches the main server at", transcodenode.EnvCoreURL)
}
parsed, err := url.Parse(env.coreURL)
if err != nil || (parsed.Scheme != "http" && parsed.Scheme != "https") || parsed.Host == "" {
return transcodeNodeEnv{}, fmt.Errorf("%s must be an absolute http or https URL", envCoreURL)
parsed, ok := transcodenode.ParseCoreURL(env.coreURL)
if !ok {
return transcodeNodeEnv{}, fmt.Errorf("%s must be an absolute http or https URL", transcodenode.EnvCoreURL)
}
env.insecureCoreURL = parsed.Scheme == "http" && !loopbackHost(parsed.Hostname())
key, err := transcodeNodeKey(getenv)
Expand Down Expand Up @@ -200,7 +194,7 @@ func runTranscodeNode() {

stopLogging := configureTranscodeNodeLogging(appCtx, cfg, remote, client, nodeName)
defer stopLogging()
slog.Info("silo starting", "mode", serverModeTranscode, "listen", cfg.Server.Listen, "log_level", cfg.Server.LogLevel,
slog.Info("silo starting", "mode", transcodenode.Mode, "listen", cfg.Server.Listen, "log_level", cfg.Server.LogLevel,
"node_id", client.NodeID(), "node_name", nodeName)

// Every job's input opens through the core with the grant the job
Expand Down Expand Up @@ -269,7 +263,7 @@ func runTranscodeNode() {
Congestion: tuningCfg.TCPCongestion == tuning.TCPCongestionBBR,
SendBuffer: tuningCfg.TCPSendBuffer,
}
startStandaloneServer(serverModeTranscode, cfg.Server.Listen, srv.Handler(), appCancel, shutdown,
startStandaloneServer(transcodenode.Mode, cfg.Server.Listen, srv.Handler(), appCancel, shutdown,
standaloneServerHooks{drain: newTranscodeNodeDrain(srv, remote.KeyRejected)}, opts)
}

Expand Down
Loading
Loading