mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-04 18:17:16 +01:00
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.
212 lines
6.8 KiB
Go
212 lines
6.8 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"
|
|
)
|
|
|
|
// miniVectorWorkerPath is weed-worker's resolved path, empty when mini does
|
|
// not start it.
|
|
var miniVectorWorkerPath string
|
|
|
|
// lookMiniBinary finds a companion binary named by its default name next to
|
|
// weed first, then on PATH; any other value is used as given.
|
|
func lookMiniBinary(binary, defaultName string) (string, error) {
|
|
if binary == defaultName {
|
|
if executable, err := os.Executable(); err == nil {
|
|
if resolved, err := exec.LookPath(filepath.Join(filepath.Dir(executable), 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
|
|
}
|
|
|
|
// prepareMiniVectorWorker finds weed-worker before the S3 gateway starts. 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 prepareMiniVectorWorker() {
|
|
if !*miniVector || !*miniVectorWorker {
|
|
return
|
|
}
|
|
binary, err := lookMiniBinary(*miniVectorWorkerBinary, "weed-worker")
|
|
if err != nil {
|
|
glog.Warningf("%s is unavailable, so vector and metadata indexes will not be built: install weed-worker or set -vector.worker.binary: %v", *miniVectorWorkerBinary, 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.
|
|
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)
|
|
}
|
|
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, err := lookMiniBinary(*miniVectorBinary, "weed-vector")
|
|
if err != nil {
|
|
return fmt.Errorf("%s is unavailable: install the vector component or set -vector.binary: %w", *miniVectorBinary, err)
|
|
}
|
|
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
|
|
}
|