Files
seaweedfs/weed/command/mini_vector.go
T
Chris LuandClaude Opus 5.5 13d9f3b104 mini: run weed-vector and weed-worker by default, continuing without either if missing (#13207)
* mini: start weed-worker with the vector engine

With -vector, mini now also starts weed-worker, the Lance maintenance
worker that builds vector and metadata indexes, finding it the way it
finds weed-vector: beside weed, then on PATH. -vector.worker=false leaves
it out and -vector.worker.binary names another binary.

The Lance Namespace trusts the worker only by a token derived from
jwt.filer_signing.key. A cluster with that key hands it to the worker
through security.toml as before. Without one, mini makes a key for the
run, gives it to the worker in its environment, and lets the namespace
accept it while no cluster key is set. The filer and the other services
are unaffected, so unauthenticated filer writes keep working.

* mini: run the vector engine by default, without failing when it is missing

-vector now defaults to true, so a plain weed mini starts weed-vector and
weed-worker whenever they are installed. Both binaries are looked up
before ports are reserved; one that is not found is logged and left out,
and mini runs without it. A vector engine that fails to start is logged
the same way instead of shutting mini down. -vector=false still leaves
vector search out.

* mini: clear the vector state a previous in-process run prepared

prepareMiniVector returned early without clearing the paths and key an
earlier run in the same process had set, so a rerun with
-vector.worker=false still started that run's weed-worker.

* mini: pass the [grpc.worker] certificates to weed-worker

weed-worker takes its admin TLS only from --tls-ca, --tls-cert and
--tls-key, not from security.toml, so behind an admin secured with
[grpc.admin] the worker mini started dialed in plaintext and never
connected. Mini now passes the certificates its Go workers dial admin
with, chosen as security.LoadClientTLS chooses them.

* mini: find weed-worker without new flags

Drop -vector.worker and -vector.worker.binary. weed-worker is looked up
beside weed, then beside the weed-vector mini starts (so an explicit
-vector.binary also locates it), then on PATH; -vector=false leaves both
out.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* docker/s3vectors: keep mini from starting a second engine and worker

The demo runs weed-vector and weed-worker in their own containers. With
-vector on by default, mini in an image that ships both binaries would
start another engine and worker beside them.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* mini: look for weed-worker beside a relative -vector.binary

filepath.Join drops the "." of ./weed-vector, so the sibling candidate
became a bare name and LookPath searched PATH instead. Resolve the
directory first.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-12-01 10:58:00 -08:00

242 lines
8.0 KiB
Go

package command
import (
"context"
"crypto/rand"
"encoding/hex"
"fmt"
"net"
"os"
"os/exec"
"path/filepath"
"time"
"github.com/seaweedfs/seaweedfs/weed/glog"
"github.com/seaweedfs/seaweedfs/weed/pb"
"github.com/seaweedfs/seaweedfs/weed/pb/master_pb"
"github.com/seaweedfs/seaweedfs/weed/security"
"github.com/seaweedfs/seaweedfs/weed/util"
)
// miniVectorPath and miniVectorWorkerPath are the resolved binaries, empty
// for a component mini does not start.
var miniVectorPath, miniVectorWorkerPath string
// lookMiniBinary finds a companion binary named by its default name next to
// weed first, then next to each of besides, then on PATH; any other value is
// used as given.
func lookMiniBinary(binary, defaultName string, besides ...string) (string, error) {
if binary == defaultName {
if executable, err := os.Executable(); err == nil {
besides = append([]string{executable}, besides...)
}
for _, path := range besides {
directory, err := filepath.Abs(filepath.Dir(path))
if err != nil {
continue
}
if resolved, err := exec.LookPath(filepath.Join(directory, binary)); err == nil {
return resolved, nil
}
}
}
return exec.LookPath(binary)
}
// miniSecurityToml is the security.toml in -config_dir for a child process,
// unless its own environment variable already names one.
func miniSecurityToml(env string) string {
directory := util.ConfigurationFileDirectory.String()
if directory == "" {
return ""
}
path := filepath.Join(util.ResolvePath(directory), "security.toml")
if _, explicit := os.LookupEnv(env); explicit || !util.FileExists(path) {
return ""
}
return path
}
// prepareMiniVector finds weed-vector and weed-worker before ports are
// reserved and the S3 gateway starts. A component that is not installed is
// logged and left out. The Lance Namespace trusts the worker only by a token
// signed with a key derived from jwt.filer_signing.key, so when the cluster
// has none, the namespace and the worker share a key made for this run.
func prepareMiniVector() {
miniVectorPath, miniVectorWorkerPath, miniS3Options.lanceWorkerKey = "", "", nil
if !*miniVector {
return
}
binary, err := lookMiniBinary(*miniVectorBinary, "weed-vector")
if err != nil {
glog.Warningf("%s is unavailable, starting without vector search: install weed-vector or set -vector.binary: %v", *miniVectorBinary, err)
*miniVector = false
return
}
miniVectorPath = binary
binary, err = lookMiniBinary("weed-worker", "weed-worker", miniVectorPath)
if err != nil {
glog.Warningf("weed-worker is unavailable, so vector and metadata indexes will not be built: install it beside weed or weed-vector, or on PATH: %v", err)
return
}
miniVectorWorkerPath = binary
if util.GetViper().GetString("jwt.filer_signing.key") != "" {
return
}
key := make([]byte, 32)
if _, err := rand.Read(key); err != nil {
glog.Warningf("weed-worker will not be started: generate its signing key: %v", err)
miniVectorWorkerPath = ""
return
}
miniS3Options.lanceWorkerKey = security.SigningKey(hex.EncodeToString(key))
}
// startMiniVectorWorker starts weed-worker against mini's admin, which names
// the filers and the Lance Namespace with every job.
// miniWorkerTLSArgs passes the [grpc.worker] certificates the Go workers dial
// admin with, chosen as security.LoadClientTLS chooses them: weed-worker takes
// its admin TLS only as flags, not from security.toml.
func miniWorkerTLSArgs(config util.Configuration) []string {
cert, key := config.GetString("grpc.worker.client_cert"), config.GetString("grpc.worker.client_key")
if cert == "" || key == "" {
cert, key = config.GetString("grpc.worker.cert"), config.GetString("grpc.worker.key")
}
ca := config.GetString("grpc.ca")
if cert == "" || key == "" || ca == "" {
return nil
}
return []string{"--tls-ca=" + ca, "--tls-cert=" + cert, "--tls-key=" + key}
}
func startMiniVectorWorker() error {
if miniVectorWorkerPath == "" {
return nil
}
admin := pb.NewServerAddress(miniAdminWorkerDialIP(miniAdminOptions.workerGrpcBindIp), *miniAdminOptions.port, *miniAdminOptions.grpcPort)
args := []string{"--admin=" + string(admin)}
if path := miniSecurityToml("WEED_WORKER_SECURITY_TOML"); path != "" {
args = append(args, "--security-toml="+path)
}
args = append(args, miniWorkerTLSArgs(util.GetViper())...)
command := exec.CommandContext(miniClientsCtx(), miniVectorWorkerPath, args...)
command.Stdout, command.Stderr = os.Stdout, os.Stderr
if key := miniS3Options.lanceWorkerKey; len(key) > 0 {
command.Env = append(os.Environ(), "WEED_FILER_SIGNING_KEY="+string(key))
}
if err := command.Start(); err != nil {
return err
}
done := trackMiniClient()
go func() {
defer done()
if err := command.Wait(); err != nil && miniClientsCtx().Err() == nil {
glog.Errorf("weed-worker exited: %v", err)
}
}()
glog.V(0).Infof("weed-worker started against admin %s", admin)
return nil
}
func startMiniVector() error {
binary := miniVectorPath
address := util.JoinHostPort(*miniIp, *miniVectorPort)
bindIP := *miniBindIp
if bindIP == "" {
bindIP = *miniIp
}
if net.ParseIP(bindIP) == nil {
ctx, cancel := context.WithTimeout(miniClientsCtx(), 5*time.Second)
addresses, err := net.DefaultResolver.LookupIPAddr(ctx, bindIP)
cancel()
if err != nil {
return fmt.Errorf("resolve vector bind address %s: %w", bindIP, err)
}
if len(addresses) == 0 {
return fmt.Errorf("vector bind address %s has no IP addresses", bindIP)
}
bindIP = addresses[0].String()
}
args := []string{"--listen=" + util.JoinHostPort(bindIP, *miniVectorPort), "--advertise=" + address, "--masters=" + string(pb.NewServerAddress(*miniIp, *miniMasterOptions.port, *miniMasterOptions.portGrpc))}
if miniFilerOptions.filerGroup != nil && *miniFilerOptions.filerGroup != "" {
args = append(args, "--filer-group="+*miniFilerOptions.filerGroup)
}
if *miniVectorMetricsPort > 0 {
args = append(args, fmt.Sprintf("--metrics-port=%d", *miniVectorMetricsPort), "--metrics-ip="+bindIP)
}
if path := miniSecurityToml("WEED_VECTOR_SECURITY_TOML"); path != "" {
args = append(args, "--security-toml="+path)
}
command := exec.CommandContext(miniClientsCtx(), binary, args...)
command.Stdout, command.Stderr = os.Stdout, os.Stderr
if err := command.Start(); err != nil {
return err
}
done := trackMiniClient()
exited := make(chan error, 1)
go func() {
defer done()
err := command.Wait()
exited <- err
if err != nil && miniClientsCtx().Err() == nil {
glog.Errorf("vector engine exited: %v", err)
}
}()
ctx, cancel := context.WithTimeout(miniClientsCtx(), 30*time.Second)
defer cancel()
ticker := time.NewTicker(100 * time.Millisecond)
defer ticker.Stop()
for {
select {
case err := <-exited:
return fmt.Errorf("vector engine exited before readiness: %v", err)
case <-ctx.Done():
_ = command.Process.Kill()
return ctx.Err()
case <-ticker.C:
conn, err := net.DialTimeout("tcp", miniVectorLocalAddress(bindIP, *miniVectorPort), 100*time.Millisecond)
if err == nil {
_ = conn.Close()
if !miniVectorRegistered(ctx, address) {
continue
}
glog.V(0).Infof("vector engine ready on %s", address)
return nil
}
}
}
}
func miniVectorLocalAddress(bindIP string, port int) string {
if ip := net.ParseIP(bindIP); ip != nil && ip.IsUnspecified() {
if ip.To4() != nil {
bindIP = "127.0.0.1"
} else {
bindIP = "::1"
}
}
return util.JoinHostPort(bindIP, port)
}
func miniVectorRegistered(ctx context.Context, address string) bool {
ctx, cancel := context.WithTimeout(ctx, time.Second)
defer cancel()
master := pb.NewServerAddress(*miniIp, *miniMasterOptions.port, *miniMasterOptions.portGrpc)
ready := false
_ = pb.WithMasterClient(ctx, false, master, security.LoadClientTLS(util.GetViper(), "grpc.client"), false, func(client master_pb.SeaweedClient) error {
response, err := client.ListClusterNodes(ctx, &master_pb.ListClusterNodesRequest{ClientType: "vector", FilerGroup: *miniFilerOptions.filerGroup})
if err != nil {
return err
}
for _, node := range response.ClusterNodes {
if node.Address == address {
ready = true
break
}
}
return nil
})
return ready
}