2019-12-13 16:02:48 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2023-06-09 06:39:01 +00:00
|
|
|
"context"
|
|
|
|
"encoding/hex"
|
2019-12-13 16:02:48 +00:00
|
|
|
"fmt"
|
2023-08-31 16:19:57 +00:00
|
|
|
"math"
|
2019-12-13 16:02:48 +00:00
|
|
|
"os"
|
2023-02-03 09:46:25 +00:00
|
|
|
"path"
|
2022-07-18 08:52:30 +00:00
|
|
|
"runtime"
|
2020-11-27 15:18:53 +00:00
|
|
|
"sort"
|
2019-12-21 10:26:14 +00:00
|
|
|
"strconv"
|
2019-12-13 16:02:48 +00:00
|
|
|
"strings"
|
|
|
|
"time"
|
|
|
|
|
2023-10-04 11:50:37 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/internal/cache"
|
2023-08-27 15:09:02 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/internal/logs"
|
2023-03-07 14:08:53 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-http-gw/resolver"
|
2023-06-14 08:27:33 +00:00
|
|
|
grpctracing "git.frostfs.info/TrueCloudLab/frostfs-observability/tracing/grpc"
|
2023-06-09 06:39:01 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/pool"
|
|
|
|
treepool "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/pool/tree"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2019-12-13 16:02:48 +00:00
|
|
|
"github.com/spf13/pflag"
|
|
|
|
"github.com/spf13/viper"
|
2021-02-12 12:24:52 +00:00
|
|
|
"github.com/valyala/fasthttp"
|
2022-09-08 14:57:22 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"go.uber.org/zap/zapcore"
|
2023-06-09 06:39:01 +00:00
|
|
|
"google.golang.org/grpc"
|
2019-12-13 16:02:48 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2022-07-19 14:06:32 +00:00
|
|
|
defaultRebalanceTimer = 60 * time.Second
|
2020-02-19 10:47:21 +00:00
|
|
|
defaultRequestTimeout = 15 * time.Second
|
2022-07-19 14:06:32 +00:00
|
|
|
defaultConnectTimeout = 10 * time.Second
|
2022-11-15 15:06:53 +00:00
|
|
|
defaultStreamTimeout = 10 * time.Second
|
2020-02-19 10:47:21 +00:00
|
|
|
|
2022-07-27 06:52:08 +00:00
|
|
|
defaultShutdownTimeout = 15 * time.Second
|
|
|
|
|
2022-07-29 06:34:26 +00:00
|
|
|
defaultPoolErrorThreshold uint32 = 100
|
|
|
|
|
2023-08-31 16:19:57 +00:00
|
|
|
defaultSoftMemoryLimit = math.MaxInt64
|
|
|
|
|
2023-08-25 11:53:59 +00:00
|
|
|
defaultBufferMaxSizeForPut = 1024 * 1024 // 1mb
|
|
|
|
|
2022-11-24 14:49:21 +00:00
|
|
|
cfgServer = "server"
|
|
|
|
cfgTLSEnabled = "tls.enabled"
|
|
|
|
cfgTLSCertFile = "tls.cert_file"
|
|
|
|
cfgTLSKeyFile = "tls.key_file"
|
2020-12-02 09:09:46 +00:00
|
|
|
|
2021-05-13 09:41:25 +00:00
|
|
|
// Web.
|
2021-02-12 12:24:52 +00:00
|
|
|
cfgWebReadBufferSize = "web.read_buffer_size"
|
|
|
|
cfgWebWriteBufferSize = "web.write_buffer_size"
|
|
|
|
cfgWebReadTimeout = "web.read_timeout"
|
|
|
|
cfgWebWriteTimeout = "web.write_timeout"
|
|
|
|
cfgWebStreamRequestBody = "web.stream_request_body"
|
|
|
|
cfgWebMaxRequestBodySize = "web.max_request_body_size"
|
2020-12-02 09:09:46 +00:00
|
|
|
|
2022-07-27 06:52:08 +00:00
|
|
|
// Metrics / Profiler.
|
|
|
|
cfgPrometheusEnabled = "prometheus.enabled"
|
|
|
|
cfgPrometheusAddress = "prometheus.address"
|
|
|
|
cfgPprofEnabled = "pprof.enabled"
|
|
|
|
cfgPprofAddress = "pprof.address"
|
|
|
|
|
2023-03-15 08:07:44 +00:00
|
|
|
// Tracing ...
|
|
|
|
cfgTracingEnabled = "tracing.enabled"
|
|
|
|
cfgTracingExporter = "tracing.exporter"
|
|
|
|
cfgTracingEndpoint = "tracing.endpoint"
|
|
|
|
|
2022-07-29 06:34:26 +00:00
|
|
|
// Pool config.
|
|
|
|
cfgConTimeout = "connect_timeout"
|
2022-11-15 15:06:53 +00:00
|
|
|
cfgStreamTimeout = "stream_timeout"
|
2022-07-29 06:34:26 +00:00
|
|
|
cfgReqTimeout = "request_timeout"
|
|
|
|
cfgRebalance = "rebalance_timer"
|
|
|
|
cfgPoolErrorThreshold = "pool_error_threshold"
|
2020-12-02 09:09:46 +00:00
|
|
|
|
2021-05-13 09:41:25 +00:00
|
|
|
// Logger.
|
2022-03-25 13:06:33 +00:00
|
|
|
cfgLoggerLevel = "logger.level"
|
2020-11-27 15:18:53 +00:00
|
|
|
|
2021-06-22 11:53:59 +00:00
|
|
|
// Wallet.
|
|
|
|
cfgWalletPassphrase = "wallet.passphrase"
|
2022-04-08 09:01:57 +00:00
|
|
|
cfgWalletPath = "wallet.path"
|
|
|
|
cfgWalletAddress = "wallet.address"
|
2021-06-22 11:53:59 +00:00
|
|
|
|
2021-05-13 09:41:25 +00:00
|
|
|
// Uploader Header.
|
2021-01-26 09:40:01 +00:00
|
|
|
cfgUploaderHeaderEnableDefaultTimestamp = "upload_header.use_default_timestamp"
|
2021-01-25 19:36:46 +00:00
|
|
|
|
2021-05-13 09:41:25 +00:00
|
|
|
// Peers.
|
2020-11-27 15:18:53 +00:00
|
|
|
cfgPeers = "peers"
|
|
|
|
|
2022-04-20 09:17:20 +00:00
|
|
|
// NeoGo.
|
|
|
|
cfgRPCEndpoint = "rpc_endpoint"
|
|
|
|
|
|
|
|
// Resolving.
|
|
|
|
cfgResolveOrder = "resolve_order"
|
|
|
|
|
2021-10-29 13:11:34 +00:00
|
|
|
// Zip compression.
|
|
|
|
cfgZipCompression = "zip.compression"
|
|
|
|
|
2023-08-31 16:19:57 +00:00
|
|
|
// Runtime.
|
|
|
|
cfgSoftMemoryLimit = "runtime.soft_memory_limit"
|
|
|
|
|
2023-08-21 13:50:23 +00:00
|
|
|
// Enabling client side object preparing for PUT operations.
|
|
|
|
cfgClientCut = "frostfs.client_cut"
|
2023-08-25 11:53:59 +00:00
|
|
|
// Sets max buffer size for read payload in put operations.
|
|
|
|
cfgBufferMaxSizeForPut = "frostfs.buffer_max_size_for_put"
|
2023-08-21 13:50:23 +00:00
|
|
|
|
2023-10-04 11:50:37 +00:00
|
|
|
// Caching.
|
|
|
|
cfgBucketsCacheLifetime = "cache.buckets.lifetime"
|
|
|
|
cfgBucketsCacheSize = "cache.buckets.size"
|
|
|
|
|
2021-05-13 09:41:25 +00:00
|
|
|
// Command line args.
|
2022-11-24 14:49:21 +00:00
|
|
|
cmdHelp = "help"
|
|
|
|
cmdVersion = "version"
|
|
|
|
cmdPprof = "pprof"
|
|
|
|
cmdMetrics = "metrics"
|
|
|
|
cmdWallet = "wallet"
|
|
|
|
cmdAddress = "address"
|
|
|
|
cmdConfig = "config"
|
2023-02-03 09:46:25 +00:00
|
|
|
cmdConfigDir = "config-dir"
|
2022-11-24 14:49:21 +00:00
|
|
|
cmdListenAddress = "listen_address"
|
2019-12-13 16:02:48 +00:00
|
|
|
)
|
|
|
|
|
2020-11-27 15:18:53 +00:00
|
|
|
var ignore = map[string]struct{}{
|
2022-03-25 13:06:33 +00:00
|
|
|
cfgPeers: {},
|
|
|
|
cmdHelp: {},
|
|
|
|
cmdVersion: {},
|
2020-11-27 15:18:53 +00:00
|
|
|
}
|
|
|
|
|
2019-12-13 16:02:48 +00:00
|
|
|
func settings() *viper.Viper {
|
|
|
|
v := viper.New()
|
|
|
|
v.AutomaticEnv()
|
2020-11-27 15:18:53 +00:00
|
|
|
v.SetEnvPrefix(Prefix)
|
2021-06-28 10:24:46 +00:00
|
|
|
v.AllowEmptyEnv(true)
|
2019-12-13 16:02:48 +00:00
|
|
|
v.SetConfigType("yaml")
|
|
|
|
v.SetEnvKeyReplacer(strings.NewReplacer(".", "_"))
|
|
|
|
|
|
|
|
// flags setup:
|
2019-12-21 10:26:14 +00:00
|
|
|
flags := pflag.NewFlagSet("commandline", pflag.ExitOnError)
|
2020-12-02 08:52:07 +00:00
|
|
|
flags.SetOutput(os.Stdout)
|
2019-12-13 16:02:48 +00:00
|
|
|
flags.SortFlags = false
|
|
|
|
|
2020-12-02 09:09:46 +00:00
|
|
|
flags.Bool(cmdPprof, false, "enable pprof")
|
|
|
|
flags.Bool(cmdMetrics, false, "enable prometheus")
|
2019-12-21 10:26:14 +00:00
|
|
|
|
2020-11-27 15:18:53 +00:00
|
|
|
help := flags.BoolP(cmdHelp, "h", false, "show help")
|
|
|
|
version := flags.BoolP(cmdVersion, "v", false, "show version")
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2021-06-22 11:53:59 +00:00
|
|
|
flags.StringP(cmdWallet, "w", "", `path to the wallet`)
|
|
|
|
flags.String(cmdAddress, "", `address of wallet account`)
|
2023-02-03 09:46:25 +00:00
|
|
|
flags.StringArray(cmdConfig, nil, "config paths")
|
|
|
|
flags.String(cmdConfigDir, "", "config dir path")
|
2020-12-02 09:09:46 +00:00
|
|
|
flags.Duration(cfgConTimeout, defaultConnectTimeout, "gRPC connect timeout")
|
2022-11-15 15:06:53 +00:00
|
|
|
flags.Duration(cfgStreamTimeout, defaultStreamTimeout, "gRPC individual message timeout")
|
2020-12-02 09:09:46 +00:00
|
|
|
flags.Duration(cfgReqTimeout, defaultRequestTimeout, "gRPC request timeout")
|
|
|
|
flags.Duration(cfgRebalance, defaultRebalanceTimer, "gRPC connection rebalance timer")
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2022-11-24 14:49:21 +00:00
|
|
|
flags.String(cmdListenAddress, "0.0.0.0:8080", "addresses to listen")
|
|
|
|
flags.String(cfgTLSCertFile, "", "TLS certificate path")
|
|
|
|
flags.String(cfgTLSKeyFile, "", "TLS key path")
|
2022-12-16 08:45:27 +00:00
|
|
|
peers := flags.StringArrayP(cfgPeers, "p", nil, "FrostFS nodes")
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2022-04-20 09:17:20 +00:00
|
|
|
resolveMethods := flags.StringSlice(cfgResolveOrder, []string{resolver.NNSResolver, resolver.DNSResolver}, "set container name resolve order")
|
|
|
|
|
2019-12-13 16:02:48 +00:00
|
|
|
// set defaults:
|
|
|
|
|
|
|
|
// logger:
|
2020-11-27 15:18:53 +00:00
|
|
|
v.SetDefault(cfgLoggerLevel, "debug")
|
2019-12-13 16:02:48 +00:00
|
|
|
|
2022-07-29 06:34:26 +00:00
|
|
|
// pool:
|
|
|
|
v.SetDefault(cfgPoolErrorThreshold, defaultPoolErrorThreshold)
|
|
|
|
|
2023-08-25 11:53:59 +00:00
|
|
|
// frostfs:
|
|
|
|
v.SetDefault(cfgBufferMaxSizeForPut, defaultBufferMaxSizeForPut)
|
|
|
|
|
2020-04-15 08:10:42 +00:00
|
|
|
// web-server:
|
2020-12-02 09:09:46 +00:00
|
|
|
v.SetDefault(cfgWebReadBufferSize, 4096)
|
|
|
|
v.SetDefault(cfgWebWriteBufferSize, 4096)
|
2022-04-21 15:53:17 +00:00
|
|
|
v.SetDefault(cfgWebReadTimeout, time.Minute*10)
|
|
|
|
v.SetDefault(cfgWebWriteTimeout, time.Minute*5)
|
2021-02-12 12:24:52 +00:00
|
|
|
v.SetDefault(cfgWebStreamRequestBody, true)
|
|
|
|
v.SetDefault(cfgWebMaxRequestBodySize, fasthttp.DefaultMaxRequestBodySize)
|
2020-04-15 08:10:42 +00:00
|
|
|
|
2021-01-26 09:40:01 +00:00
|
|
|
// upload header
|
|
|
|
v.SetDefault(cfgUploaderHeaderEnableDefaultTimestamp, false)
|
|
|
|
|
2021-10-29 13:11:34 +00:00
|
|
|
// zip:
|
|
|
|
v.SetDefault(cfgZipCompression, false)
|
|
|
|
|
2022-07-27 06:52:08 +00:00
|
|
|
// metrics
|
|
|
|
v.SetDefault(cfgPprofAddress, "localhost:8083")
|
|
|
|
v.SetDefault(cfgPrometheusAddress, "localhost:8084")
|
|
|
|
|
|
|
|
// Binding flags
|
|
|
|
if err := v.BindPFlag(cfgPprofEnabled, flags.Lookup(cmdPprof)); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := v.BindPFlag(cfgPrometheusEnabled, flags.Lookup(cmdMetrics)); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-08-01 20:52:07 +00:00
|
|
|
if err := v.BindPFlag(cfgWalletPath, flags.Lookup(cmdWallet)); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := v.BindPFlag(cfgWalletAddress, flags.Lookup(cmdAddress)); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-12-13 16:02:48 +00:00
|
|
|
if err := v.BindPFlags(flags); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-11-24 14:49:21 +00:00
|
|
|
if err := v.BindPFlag(cfgServer+".0.address", flags.Lookup(cmdListenAddress)); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := v.BindPFlag(cfgServer+".0."+cfgTLSKeyFile, flags.Lookup(cfgTLSKeyFile)); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
if err := v.BindPFlag(cfgServer+".0."+cfgTLSCertFile, flags.Lookup(cfgTLSCertFile)); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2019-12-13 16:02:48 +00:00
|
|
|
if err := flags.Parse(os.Args); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2022-11-24 14:49:21 +00:00
|
|
|
if v.IsSet(cfgServer+".0."+cfgTLSKeyFile) && v.IsSet(cfgServer+".0."+cfgTLSCertFile) {
|
|
|
|
v.Set(cfgServer+".0."+cfgTLSEnabled, true)
|
|
|
|
}
|
|
|
|
|
2022-04-20 09:17:20 +00:00
|
|
|
if resolveMethods != nil {
|
|
|
|
v.SetDefault(cfgResolveOrder, *resolveMethods)
|
|
|
|
}
|
|
|
|
|
2019-12-13 16:02:48 +00:00
|
|
|
switch {
|
|
|
|
case help != nil && *help:
|
2022-12-16 08:45:27 +00:00
|
|
|
fmt.Printf("FrostFS HTTP Gateway %s\n", Version)
|
2019-12-13 16:02:48 +00:00
|
|
|
flags.PrintDefaults()
|
2020-11-27 15:18:53 +00:00
|
|
|
|
|
|
|
fmt.Println()
|
|
|
|
fmt.Println("Default environments:")
|
|
|
|
fmt.Println()
|
|
|
|
keys := v.AllKeys()
|
|
|
|
sort.Strings(keys)
|
|
|
|
|
|
|
|
for i := range keys {
|
|
|
|
if _, ok := ignore[keys[i]]; ok {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-08-01 20:47:20 +00:00
|
|
|
defaultValue := v.GetString(keys[i])
|
|
|
|
if len(defaultValue) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-11-27 15:18:53 +00:00
|
|
|
k := strings.Replace(keys[i], ".", "_", -1)
|
2022-08-01 20:47:20 +00:00
|
|
|
fmt.Printf("%s_%s = %s\n", Prefix, strings.ToUpper(k), defaultValue)
|
2020-11-27 15:18:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println()
|
|
|
|
fmt.Println("Peers preset:")
|
|
|
|
fmt.Println()
|
|
|
|
|
|
|
|
fmt.Printf("%s_%s_[N]_ADDRESS = string\n", Prefix, strings.ToUpper(cfgPeers))
|
2021-04-14 20:53:47 +00:00
|
|
|
fmt.Printf("%s_%s_[N]_WEIGHT = float\n", Prefix, strings.ToUpper(cfgPeers))
|
2020-11-27 15:18:53 +00:00
|
|
|
|
2019-12-13 16:02:48 +00:00
|
|
|
os.Exit(0)
|
|
|
|
case version != nil && *version:
|
2022-12-16 08:45:27 +00:00
|
|
|
fmt.Printf("FrostFS HTTP Gateway\nVersion: %s\nGoVersion: %s\n", Version, runtime.Version())
|
2019-12-13 16:02:48 +00:00
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2023-02-03 09:46:25 +00:00
|
|
|
if err := readInConfig(v); err != nil {
|
|
|
|
panic(err)
|
2021-06-28 10:24:46 +00:00
|
|
|
}
|
|
|
|
|
2019-12-21 10:26:14 +00:00
|
|
|
if peers != nil && len(*peers) > 0 {
|
|
|
|
for i := range *peers {
|
2020-11-27 15:18:53 +00:00
|
|
|
v.SetDefault(cfgPeers+"."+strconv.Itoa(i)+".address", (*peers)[i])
|
|
|
|
v.SetDefault(cfgPeers+"."+strconv.Itoa(i)+".weight", 1)
|
2021-12-07 11:57:16 +00:00
|
|
|
v.SetDefault(cfgPeers+"."+strconv.Itoa(i)+".priority", 1)
|
2019-12-21 10:26:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-13 16:02:48 +00:00
|
|
|
return v
|
|
|
|
}
|
2022-09-08 14:57:22 +00:00
|
|
|
|
2023-02-03 09:46:25 +00:00
|
|
|
func readInConfig(v *viper.Viper) error {
|
|
|
|
if v.IsSet(cmdConfig) {
|
|
|
|
if err := readConfig(v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.IsSet(cmdConfigDir) {
|
|
|
|
if err := readConfigDir(v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func readConfigDir(v *viper.Viper) error {
|
|
|
|
cfgSubConfigDir := v.GetString(cmdConfigDir)
|
|
|
|
entries, err := os.ReadDir(cfgSubConfigDir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, entry := range entries {
|
|
|
|
if entry.IsDir() {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
ext := path.Ext(entry.Name())
|
|
|
|
if ext != ".yaml" && ext != ".yml" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = mergeConfig(v, path.Join(cfgSubConfigDir, entry.Name())); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-09-08 14:57:22 +00:00
|
|
|
func readConfig(v *viper.Viper) error {
|
2023-02-03 09:46:25 +00:00
|
|
|
for _, fileName := range v.GetStringSlice(cmdConfig) {
|
|
|
|
if err := mergeConfig(v, fileName); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func mergeConfig(v *viper.Viper, fileName string) error {
|
|
|
|
cfgFile, err := os.Open(fileName)
|
2022-09-08 14:57:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-03 09:46:25 +00:00
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if errClose := cfgFile.Close(); errClose != nil {
|
|
|
|
panic(errClose)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2023-05-31 13:34:04 +00:00
|
|
|
return v.MergeConfig(cfgFile)
|
2022-09-08 14:57:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// newLogger constructs a zap.Logger instance for current application.
|
|
|
|
// Panics on failure.
|
|
|
|
//
|
|
|
|
// Logger is built from zap's production logging configuration with:
|
|
|
|
// - parameterized level (debug by default)
|
|
|
|
// - console encoding
|
|
|
|
// - ISO8601 time encoding
|
|
|
|
//
|
|
|
|
// Logger records a stack trace for all messages at or above fatal level.
|
|
|
|
//
|
|
|
|
// See also zapcore.Level, zap.NewProductionConfig, zap.AddStacktrace.
|
|
|
|
func newLogger(v *viper.Viper) (*zap.Logger, zap.AtomicLevel) {
|
|
|
|
lvl, err := getLogLevel(v)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c := zap.NewProductionConfig()
|
|
|
|
c.Level = zap.NewAtomicLevelAt(lvl)
|
|
|
|
c.Encoding = "console"
|
|
|
|
c.EncoderConfig.EncodeTime = zapcore.ISO8601TimeEncoder
|
|
|
|
|
|
|
|
l, err := c.Build(
|
|
|
|
zap.AddStacktrace(zap.NewAtomicLevelAt(zap.FatalLevel)),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("build zap logger instance: %v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
return l, c.Level
|
|
|
|
}
|
|
|
|
|
|
|
|
func getLogLevel(v *viper.Viper) (zapcore.Level, error) {
|
|
|
|
var lvl zapcore.Level
|
|
|
|
lvlStr := v.GetString(cfgLoggerLevel)
|
|
|
|
err := lvl.UnmarshalText([]byte(lvlStr))
|
|
|
|
if err != nil {
|
|
|
|
return lvl, fmt.Errorf("incorrect logger level configuration %s (%v), "+
|
|
|
|
"value should be one of %v", lvlStr, err, [...]zapcore.Level{
|
|
|
|
zapcore.DebugLevel,
|
|
|
|
zapcore.InfoLevel,
|
|
|
|
zapcore.WarnLevel,
|
|
|
|
zapcore.ErrorLevel,
|
|
|
|
zapcore.DPanicLevel,
|
|
|
|
zapcore.PanicLevel,
|
|
|
|
zapcore.FatalLevel,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return lvl, nil
|
|
|
|
}
|
2022-11-24 14:49:21 +00:00
|
|
|
|
|
|
|
func fetchServers(v *viper.Viper) []ServerInfo {
|
|
|
|
var servers []ServerInfo
|
|
|
|
|
|
|
|
for i := 0; ; i++ {
|
|
|
|
key := cfgServer + "." + strconv.Itoa(i) + "."
|
|
|
|
|
|
|
|
var serverInfo ServerInfo
|
|
|
|
serverInfo.Address = v.GetString(key + "address")
|
|
|
|
serverInfo.TLS.Enabled = v.GetBool(key + cfgTLSEnabled)
|
|
|
|
serverInfo.TLS.KeyFile = v.GetString(key + cfgTLSKeyFile)
|
|
|
|
serverInfo.TLS.CertFile = v.GetString(key + cfgTLSCertFile)
|
|
|
|
|
|
|
|
if serverInfo.Address == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
servers = append(servers, serverInfo)
|
|
|
|
}
|
|
|
|
|
|
|
|
return servers
|
|
|
|
}
|
2023-06-09 06:39:01 +00:00
|
|
|
|
|
|
|
func getPools(ctx context.Context, logger *zap.Logger, cfg *viper.Viper) (*pool.Pool, *treepool.Pool, *keys.PrivateKey) {
|
|
|
|
key, err := getFrostFSKey(cfg, logger)
|
|
|
|
if err != nil {
|
2023-08-27 15:09:02 +00:00
|
|
|
logger.Fatal(logs.CouldNotLoadFrostFSPrivateKey, zap.Error(err))
|
2023-06-09 06:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var prm pool.InitParameters
|
|
|
|
var prmTree treepool.InitParameters
|
|
|
|
|
|
|
|
prm.SetKey(&key.PrivateKey)
|
|
|
|
prmTree.SetKey(key)
|
2023-08-27 15:09:02 +00:00
|
|
|
logger.Info(logs.UsingCredentials, zap.String("FrostFS", hex.EncodeToString(key.PublicKey().Bytes())))
|
2023-06-09 06:39:01 +00:00
|
|
|
|
|
|
|
for _, peer := range fetchPeers(logger, cfg) {
|
|
|
|
prm.AddNode(peer)
|
|
|
|
prmTree.AddNode(peer)
|
|
|
|
}
|
|
|
|
|
|
|
|
connTimeout := cfg.GetDuration(cfgConTimeout)
|
|
|
|
if connTimeout <= 0 {
|
|
|
|
connTimeout = defaultConnectTimeout
|
|
|
|
}
|
|
|
|
prm.SetNodeDialTimeout(connTimeout)
|
|
|
|
prmTree.SetNodeDialTimeout(connTimeout)
|
|
|
|
|
|
|
|
streamTimeout := cfg.GetDuration(cfgStreamTimeout)
|
|
|
|
if streamTimeout <= 0 {
|
|
|
|
streamTimeout = defaultStreamTimeout
|
|
|
|
}
|
|
|
|
prm.SetNodeStreamTimeout(streamTimeout)
|
|
|
|
prmTree.SetNodeStreamTimeout(streamTimeout)
|
|
|
|
|
|
|
|
healthCheckTimeout := cfg.GetDuration(cfgReqTimeout)
|
|
|
|
if healthCheckTimeout <= 0 {
|
|
|
|
healthCheckTimeout = defaultRequestTimeout
|
|
|
|
}
|
|
|
|
prm.SetHealthcheckTimeout(healthCheckTimeout)
|
|
|
|
prmTree.SetHealthcheckTimeout(healthCheckTimeout)
|
|
|
|
|
|
|
|
rebalanceInterval := cfg.GetDuration(cfgRebalance)
|
|
|
|
if rebalanceInterval <= 0 {
|
|
|
|
rebalanceInterval = defaultRebalanceTimer
|
|
|
|
}
|
|
|
|
prm.SetClientRebalanceInterval(rebalanceInterval)
|
|
|
|
prmTree.SetClientRebalanceInterval(rebalanceInterval)
|
|
|
|
|
|
|
|
errorThreshold := cfg.GetUint32(cfgPoolErrorThreshold)
|
|
|
|
if errorThreshold <= 0 {
|
|
|
|
errorThreshold = defaultPoolErrorThreshold
|
|
|
|
}
|
|
|
|
prm.SetErrorThreshold(errorThreshold)
|
|
|
|
prm.SetLogger(logger)
|
|
|
|
prmTree.SetLogger(logger)
|
|
|
|
|
2023-06-14 08:27:33 +00:00
|
|
|
var apiGRPCDialOpts []grpc.DialOption
|
2023-07-07 13:40:13 +00:00
|
|
|
var treeGRPCDialOpts []grpc.DialOption
|
2023-06-14 08:27:33 +00:00
|
|
|
if cfg.GetBool(cfgTracingEnabled) {
|
|
|
|
interceptors := []grpc.DialOption{
|
|
|
|
grpc.WithUnaryInterceptor(grpctracing.NewUnaryClientInteceptor()),
|
|
|
|
grpc.WithStreamInterceptor(grpctracing.NewStreamClientInterceptor()),
|
|
|
|
}
|
|
|
|
treeGRPCDialOpts = append(treeGRPCDialOpts, interceptors...)
|
|
|
|
apiGRPCDialOpts = append(apiGRPCDialOpts, interceptors...)
|
|
|
|
}
|
|
|
|
prm.SetGRPCDialOptions(apiGRPCDialOpts...)
|
|
|
|
prmTree.SetGRPCDialOptions(treeGRPCDialOpts...)
|
|
|
|
|
2023-06-09 06:39:01 +00:00
|
|
|
p, err := pool.NewPool(prm)
|
|
|
|
if err != nil {
|
2023-08-27 15:09:02 +00:00
|
|
|
logger.Fatal(logs.FailedToCreateConnectionPool, zap.Error(err))
|
2023-06-09 06:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = p.Dial(ctx); err != nil {
|
2023-08-27 15:09:02 +00:00
|
|
|
logger.Fatal(logs.FailedToDialConnectionPool, zap.Error(err))
|
2023-06-09 06:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
treePool, err := treepool.NewPool(prmTree)
|
|
|
|
if err != nil {
|
2023-08-27 15:09:02 +00:00
|
|
|
logger.Fatal(logs.FailedToCreateTreePool, zap.Error(err))
|
2023-06-09 06:39:01 +00:00
|
|
|
}
|
|
|
|
if err = treePool.Dial(ctx); err != nil {
|
2023-08-27 15:09:02 +00:00
|
|
|
logger.Fatal(logs.FailedToDialTreePool, zap.Error(err))
|
2023-06-09 06:39:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return p, treePool, key
|
|
|
|
}
|
|
|
|
|
|
|
|
func fetchPeers(l *zap.Logger, v *viper.Viper) []pool.NodeParam {
|
|
|
|
var nodes []pool.NodeParam
|
|
|
|
for i := 0; ; i++ {
|
|
|
|
key := cfgPeers + "." + strconv.Itoa(i) + "."
|
|
|
|
address := v.GetString(key + "address")
|
|
|
|
weight := v.GetFloat64(key + "weight")
|
|
|
|
priority := v.GetInt(key + "priority")
|
|
|
|
|
|
|
|
if address == "" {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
if weight <= 0 { // unspecified or wrong
|
|
|
|
weight = 1
|
|
|
|
}
|
|
|
|
if priority <= 0 { // unspecified or wrong
|
|
|
|
priority = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
nodes = append(nodes, pool.NewNodeParam(priority, address, weight))
|
|
|
|
|
2023-08-27 15:09:02 +00:00
|
|
|
l.Info(logs.AddedStoragePeer,
|
2023-06-09 06:39:01 +00:00
|
|
|
zap.Int("priority", priority),
|
|
|
|
zap.String("address", address),
|
|
|
|
zap.Float64("weight", weight))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nodes
|
|
|
|
}
|
2023-08-31 16:19:57 +00:00
|
|
|
|
|
|
|
func fetchSoftMemoryLimit(cfg *viper.Viper) int64 {
|
|
|
|
softMemoryLimit := cfg.GetSizeInBytes(cfgSoftMemoryLimit)
|
|
|
|
if softMemoryLimit <= 0 {
|
|
|
|
softMemoryLimit = defaultSoftMemoryLimit
|
|
|
|
}
|
|
|
|
|
|
|
|
return int64(softMemoryLimit)
|
|
|
|
}
|
2023-10-04 11:50:37 +00:00
|
|
|
|
|
|
|
func getCacheOptions(v *viper.Viper, l *zap.Logger) *cache.Config {
|
|
|
|
cacheCfg := cache.DefaultBucketConfig(l)
|
|
|
|
|
|
|
|
cacheCfg.Lifetime = fetchCacheLifetime(v, l, cfgBucketsCacheLifetime, cacheCfg.Lifetime)
|
|
|
|
cacheCfg.Size = fetchCacheSize(v, l, cfgBucketsCacheSize, cacheCfg.Size)
|
|
|
|
|
|
|
|
return cacheCfg
|
|
|
|
}
|
|
|
|
|
|
|
|
func fetchCacheLifetime(v *viper.Viper, l *zap.Logger, cfgEntry string, defaultValue time.Duration) time.Duration {
|
|
|
|
if v.IsSet(cfgEntry) {
|
|
|
|
lifetime := v.GetDuration(cfgEntry)
|
|
|
|
if lifetime <= 0 {
|
|
|
|
l.Error("invalid lifetime, using default value (in seconds)",
|
|
|
|
zap.String("parameter", cfgEntry),
|
|
|
|
zap.Duration("value in config", lifetime),
|
|
|
|
zap.Duration("default", defaultValue))
|
|
|
|
} else {
|
|
|
|
return lifetime
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultValue
|
|
|
|
}
|
|
|
|
|
|
|
|
func fetchCacheSize(v *viper.Viper, l *zap.Logger, cfgEntry string, defaultValue int) int {
|
|
|
|
if v.IsSet(cfgEntry) {
|
|
|
|
size := v.GetInt(cfgEntry)
|
|
|
|
if size <= 0 {
|
|
|
|
l.Error("invalid cache size, using default value",
|
|
|
|
zap.String("parameter", cfgEntry),
|
|
|
|
zap.Int("value in config", size),
|
|
|
|
zap.Int("default", defaultValue))
|
|
|
|
} else {
|
|
|
|
return size
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return defaultValue
|
|
|
|
}
|