mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2026-12-06 19:17:28 +01:00
-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.
220 lines
7.1 KiB
Go
220 lines
7.1 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 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
|
|
}
|
|
|
|
// 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() {
|
|
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
|
|
if !*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 := 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
|
|
}
|