seaweedfs/weed/command/volume.go

338 lines
12 KiB
Go
Raw Normal View History

package command
import (
2019-11-17 03:40:36 +00:00
"fmt"
2018-10-11 07:08:13 +00:00
"net/http"
2020-06-10 09:52:24 +00:00
httppprof "net/http/pprof"
"os"
"runtime"
2018-10-11 07:08:13 +00:00
"runtime/pprof"
2012-08-24 05:46:54 +00:00
"strconv"
"strings"
2018-10-11 07:08:13 +00:00
"time"
"github.com/spf13/viper"
2019-11-17 03:40:36 +00:00
"google.golang.org/grpc"
2020-06-10 20:10:10 +00:00
"github.com/chrislusf/seaweedfs/weed/util/grace"
2020-03-04 08:39:47 +00:00
"github.com/chrislusf/seaweedfs/weed/pb"
2020-01-03 08:37:24 +00:00
"github.com/chrislusf/seaweedfs/weed/security"
"github.com/chrislusf/seaweedfs/weed/util/httpdown"
"google.golang.org/grpc/reflection"
"github.com/chrislusf/seaweedfs/weed/glog"
2018-10-11 08:16:33 +00:00
"github.com/chrislusf/seaweedfs/weed/pb/volume_server_pb"
"github.com/chrislusf/seaweedfs/weed/server"
2018-10-11 07:08:13 +00:00
"github.com/chrislusf/seaweedfs/weed/storage"
"github.com/chrislusf/seaweedfs/weed/util"
)
2015-01-13 08:27:51 +00:00
var (
v VolumeServerOptions
)
type VolumeServerOptions struct {
2020-06-10 20:10:10 +00:00
port *int
publicPort *int
folders []string
folderMaxLimits []int
ip *string
publicUrl *string
bindIp *string
masters *string
2015-01-13 08:27:51 +00:00
idleConnectionTimeout *int
dataCenter *string
rack *string
whiteList []string
indexType *string
fixJpgOrientation *bool
readRedirect *bool
2017-06-22 08:33:58 +00:00
cpuProfile *string
memProfile *string
compactionMBPerSecond *int
2020-01-03 08:37:24 +00:00
fileSizeLimitMB *int
2020-07-03 23:41:30 +00:00
minFreeSpacePercents []float32
2020-06-10 09:52:24 +00:00
pprof *bool
2020-06-10 20:10:10 +00:00
// pulseSeconds *int
2015-01-13 08:27:51 +00:00
}
func init() {
2012-08-24 05:46:54 +00:00
cmdVolume.Run = runVolume // break init cycle
2015-01-13 08:27:51 +00:00
v.port = cmdVolume.Flag.Int("port", 8080, "http listen port")
v.publicPort = cmdVolume.Flag.Int("port.public", 0, "port opened to public")
v.ip = cmdVolume.Flag.String("ip", util.DetectedHostAddress(), "ip or server name")
v.publicUrl = cmdVolume.Flag.String("publicUrl", "", "Publicly accessible address")
2015-01-13 08:27:51 +00:00
v.bindIp = cmdVolume.Flag.String("ip.bind", "0.0.0.0", "ip address to bind to")
v.masters = cmdVolume.Flag.String("mserver", "localhost:9333", "comma-separated master servers")
// v.pulseSeconds = cmdVolume.Flag.Int("pulseSeconds", 5, "number of seconds between heartbeats, must be smaller than or equal to the master's setting")
2017-01-10 09:01:12 +00:00
v.idleConnectionTimeout = cmdVolume.Flag.Int("idleTimeout", 30, "connection idle seconds")
2015-01-13 08:27:51 +00:00
v.dataCenter = cmdVolume.Flag.String("dataCenter", "", "current volume server's data center name")
v.rack = cmdVolume.Flag.String("rack", "", "current volume server's rack name")
v.indexType = cmdVolume.Flag.String("index", "memory", "Choose [memory|leveldb|leveldbMedium|leveldbLarge] mode for memory~performance balance.")
v.fixJpgOrientation = cmdVolume.Flag.Bool("images.fix.orientation", false, "Adjust jpg orientation when uploading.")
v.readRedirect = cmdVolume.Flag.Bool("read.redirect", true, "Redirect moved or non-local volumes.")
2017-06-22 08:33:58 +00:00
v.cpuProfile = cmdVolume.Flag.String("cpuprofile", "", "cpu profile output file")
v.memProfile = cmdVolume.Flag.String("memprofile", "", "memory profile output file")
v.compactionMBPerSecond = cmdVolume.Flag.Int("compactionMBps", 0, "limit background compaction or copying speed in mega bytes per second")
2020-01-03 08:37:24 +00:00
v.fileSizeLimitMB = cmdVolume.Flag.Int("fileSizeLimitMB", 256, "limit file size to avoid out of memory")
2020-06-10 09:52:24 +00:00
v.pprof = cmdVolume.Flag.Bool("pprof", false, "enable pprof http handlers. precludes --memprofile and --cpuprofile")
}
var cmdVolume = &Command{
2012-09-26 08:55:56 +00:00
UsageLine: "volume -port=8080 -dir=/tmp -max=5 -ip=server_name -mserver=localhost:9333",
2012-08-24 05:46:54 +00:00
Short: "start a volume server",
Long: `start a volume server to provide storage spaces
`,
}
var (
volumeFolders = cmdVolume.Flag.String("dir", os.TempDir(), "directories to store data files. dir[,dir]...")
maxVolumeCounts = cmdVolume.Flag.String("max", "8", "maximum numbers of volumes, count[,count]... If set to zero, the limit will be auto configured.")
2013-08-13 16:22:06 +00:00
volumeWhiteListOption = cmdVolume.Flag.String("whiteList", "", "comma separated Ip addresses having write permission. No limit if empty.")
2020-07-03 23:42:02 +00:00
minFreeSpacePercent = cmdVolume.Flag.String("minFreeSpacePercent", "1", "minimum free disk space (default to 1%). Low disk space will mark all volumes as ReadOnly.")
)
func runVolume(cmd *Command, args []string) bool {
2019-02-18 20:11:52 +00:00
util.LoadConfiguration("security", false)
2019-02-18 20:11:52 +00:00
2019-06-23 10:08:27 +00:00
runtime.GOMAXPROCS(runtime.NumCPU())
2020-06-10 09:52:24 +00:00
// If --pprof is set we assume the caller wants to be able to collect
// cpu and memory profiles via go tool pprof
if !*v.pprof {
grace.SetupProfiling(*v.cpuProfile, *v.memProfile)
}
2015-01-13 08:27:51 +00:00
2020-06-05 16:00:38 +00:00
v.startVolumeServer(*volumeFolders, *maxVolumeCounts, *volumeWhiteListOption, *minFreeSpacePercent)
return true
}
2020-06-05 16:00:38 +00:00
func (v VolumeServerOptions) startVolumeServer(volumeFolders, maxVolumeCounts, volumeWhiteListOption, minFreeSpacePercent string) {
2019-11-17 03:40:36 +00:00
// Set multiple folders and each folder's max volume count limit'
v.folders = strings.Split(volumeFolders, ",")
2020-06-21 15:38:00 +00:00
for _, folder := range v.folders {
if err := util.TestFolderWritable(folder); err != nil {
glog.Fatalf("Check Data Folder(-dir) Writable %s : %s", folder, err)
}
}
// set max
maxCountStrings := strings.Split(maxVolumeCounts, ",")
for _, maxString := range maxCountStrings {
if max, e := strconv.Atoi(maxString); e == nil {
2015-01-13 08:27:51 +00:00
v.folderMaxLimits = append(v.folderMaxLimits, max)
} else {
glog.Fatalf("The max specified in -max not a valid number %s", maxString)
}
}
2015-01-13 08:27:51 +00:00
if len(v.folders) != len(v.folderMaxLimits) {
glog.Fatalf("%d directories by -dir, but only %d max is set by -max", len(v.folders), len(v.folderMaxLimits))
}
2020-06-21 15:38:00 +00:00
// set minFreeSpacePercent
2020-06-05 16:00:38 +00:00
minFreeSpacePercentStrings := strings.Split(minFreeSpacePercent, ",")
for _, freeString := range minFreeSpacePercentStrings {
if value, e := strconv.ParseFloat(freeString, 32); e == nil {
2020-07-03 23:41:30 +00:00
v.minFreeSpacePercents = append(v.minFreeSpacePercents, float32(value))
} else {
2020-06-05 16:00:38 +00:00
glog.Fatalf("The value specified in -minFreeSpacePercent not a valid value %s", freeString)
}
}
2020-07-03 23:41:30 +00:00
if len(v.minFreeSpacePercents) == 1 && len(v.folders) > 1 {
for i := 0; i < len(v.folders)-1; i++ {
2020-07-03 23:41:30 +00:00
v.minFreeSpacePercents = append(v.minFreeSpacePercents, v.minFreeSpacePercents[0])
}
}
2020-07-03 23:41:30 +00:00
if len(v.folders) != len(v.minFreeSpacePercents) {
glog.Fatalf("%d directories by -dir, but only %d minFreeSpacePercent is set by -minFreeSpacePercent", len(v.folders), len(v.minFreeSpacePercents))
}
2015-01-13 08:36:44 +00:00
2019-11-17 03:40:36 +00:00
// security related white list configuration
if volumeWhiteListOption != "" {
v.whiteList = strings.Split(volumeWhiteListOption, ",")
2015-01-13 08:27:51 +00:00
}
2012-09-26 10:27:10 +00:00
if *v.ip == "" {
*v.ip = util.DetectedHostAddress()
2020-04-10 06:42:59 +00:00
glog.V(0).Infof("detected volume server ip address: %v", *v.ip)
2012-09-26 09:29:16 +00:00
}
if *v.publicPort == 0 {
*v.publicPort = *v.port
}
2015-04-08 18:08:08 +00:00
if *v.publicUrl == "" {
*v.publicUrl = *v.ip + ":" + strconv.Itoa(*v.publicPort)
}
volumeMux := http.NewServeMux()
publicVolumeMux := volumeMux
2019-11-17 03:40:36 +00:00
if v.isSeparatedPublicPort() {
publicVolumeMux = http.NewServeMux()
}
2012-08-24 05:46:54 +00:00
2020-06-10 09:52:24 +00:00
if *v.pprof {
volumeMux.HandleFunc("/debug/pprof/", httppprof.Index)
volumeMux.HandleFunc("/debug/pprof/cmdline", httppprof.Cmdline)
volumeMux.HandleFunc("/debug/pprof/profile", httppprof.Profile)
volumeMux.HandleFunc("/debug/pprof/symbol", httppprof.Symbol)
volumeMux.HandleFunc("/debug/pprof/trace", httppprof.Trace)
}
volumeNeedleMapKind := storage.NeedleMapInMemory
switch *v.indexType {
case "leveldb":
volumeNeedleMapKind = storage.NeedleMapLevelDb
case "leveldbMedium":
volumeNeedleMapKind = storage.NeedleMapLevelDbMedium
case "leveldbLarge":
volumeNeedleMapKind = storage.NeedleMapLevelDbLarge
}
masters := *v.masters
volumeServer := weed_server.NewVolumeServer(volumeMux, publicVolumeMux,
*v.ip, *v.port, *v.publicUrl,
2020-07-03 23:41:30 +00:00
v.folders, v.folderMaxLimits, v.minFreeSpacePercents,
volumeNeedleMapKind,
strings.Split(masters, ","), 5, *v.dataCenter, *v.rack,
2015-01-13 08:27:51 +00:00
v.whiteList,
*v.fixJpgOrientation, *v.readRedirect,
*v.compactionMBPerSecond,
2020-01-03 08:37:24 +00:00
*v.fileSizeLimitMB,
)
2012-08-24 05:46:54 +00:00
2019-11-17 03:40:36 +00:00
// starting grpc server
grpcS := v.startGrpcService(volumeServer)
// starting public http server
var publicHttpDown httpdown.Server
if v.isSeparatedPublicPort() {
publicHttpDown = v.startPublicHttpService(publicVolumeMux)
if nil == publicHttpDown {
glog.Fatalf("start public http service failed")
}
}
2019-11-17 03:40:36 +00:00
// starting the cluster http server
clusterHttpServer := v.startClusterHttpService(volumeMux)
stopChain := make(chan struct{})
grace.OnInterrupt(func() {
2019-11-17 03:40:36 +00:00
fmt.Println("volume server has be killed")
var startTime time.Time
// firstly, stop the public http service to prevent from receiving new user request
if nil != publicHttpDown {
startTime = time.Now()
if err := publicHttpDown.Stop(); err != nil {
glog.Warningf("stop the public http server failed, %v", err)
}
delta := time.Now().Sub(startTime).Nanoseconds() / 1e6
glog.V(0).Infof("stop public http server, elapsed %dms", delta)
2019-11-17 03:40:36 +00:00
}
startTime = time.Now()
if err := clusterHttpServer.Stop(); err != nil {
glog.Warningf("stop the cluster http server failed, %v", err)
}
delta := time.Now().Sub(startTime).Nanoseconds() / 1e6
glog.V(0).Infof("graceful stop cluster http server, elapsed [%d]", delta)
2019-11-17 03:40:36 +00:00
startTime = time.Now()
grpcS.GracefulStop()
delta = time.Now().Sub(startTime).Nanoseconds() / 1e6
glog.V(0).Infof("graceful stop gRPC, elapsed [%d]", delta)
2019-11-17 03:40:36 +00:00
startTime = time.Now()
2014-05-13 22:04:04 +00:00
volumeServer.Shutdown()
delta = time.Now().Sub(startTime).Nanoseconds() / 1e6
glog.V(0).Infof("stop volume server, elapsed [%d]", delta)
2019-11-17 03:40:36 +00:00
pprof.StopCPUProfile()
2019-11-17 03:40:36 +00:00
close(stopChain) // notify exit
2014-05-13 22:04:04 +00:00
})
2014-05-13 07:04:28 +00:00
2019-11-17 03:40:36 +00:00
select {
case <-stopChain:
}
glog.Warningf("the volume server exit.")
}
// check whether configure the public port
func (v VolumeServerOptions) isSeparatedPublicPort() bool {
return *v.publicPort != *v.port
}
func (v VolumeServerOptions) startGrpcService(vs volume_server_pb.VolumeServerServer) *grpc.Server {
2018-10-11 08:16:33 +00:00
grpcPort := *v.port + 10000
grpcL, err := util.NewListener(*v.bindIp+":"+strconv.Itoa(grpcPort), 0)
if err != nil {
glog.Fatalf("failed to listen on grpc port %d: %v", grpcPort, err)
}
2020-03-04 08:39:47 +00:00
grpcS := pb.NewGrpcServer(security.LoadServerTLS(util.GetViper(), "grpc.volume"))
2019-11-17 03:40:36 +00:00
volume_server_pb.RegisterVolumeServerServer(grpcS, vs)
2018-10-11 08:16:33 +00:00
reflection.Register(grpcS)
2019-11-17 03:40:36 +00:00
go func() {
if err := grpcS.Serve(grpcL); err != nil {
glog.Fatalf("start gRPC service failed, %s", err)
2019-02-25 08:43:36 +00:00
}
2019-11-17 03:40:36 +00:00
}()
return grpcS
}
func (v VolumeServerOptions) startPublicHttpService(handler http.Handler) httpdown.Server {
publicListeningAddress := *v.bindIp + ":" + strconv.Itoa(*v.publicPort)
2020-06-02 07:10:35 +00:00
glog.V(0).Infoln("Start Seaweed volume server", util.Version(), "public at", publicListeningAddress)
2019-11-17 03:40:36 +00:00
publicListener, e := util.NewListener(publicListeningAddress, time.Duration(*v.idleConnectionTimeout)*time.Second)
if e != nil {
glog.Fatalf("Volume server listener error:%v", e)
}
pubHttp := httpdown.HTTP{StopTimeout: 5 * time.Minute, KillTimeout: 5 * time.Minute}
publicHttpDown := pubHttp.Serve(&http.Server{Handler: handler}, publicListener)
go func() {
if err := publicHttpDown.Wait(); err != nil {
glog.Errorf("public http down wait failed, %v", err)
2019-02-25 08:43:36 +00:00
}
2019-11-17 03:40:36 +00:00
}()
return publicHttpDown
}
func (v VolumeServerOptions) startClusterHttpService(handler http.Handler) httpdown.Server {
var (
certFile, keyFile string
)
if viper.GetString("https.volume.key") != "" {
certFile = viper.GetString("https.volume.cert")
keyFile = viper.GetString("https.volume.key")
}
listeningAddress := *v.bindIp + ":" + strconv.Itoa(*v.port)
2020-06-02 07:10:35 +00:00
glog.V(0).Infof("Start Seaweed volume server %s at %s", util.Version(), listeningAddress)
2019-11-17 03:40:36 +00:00
listener, e := util.NewListener(listeningAddress, time.Duration(*v.idleConnectionTimeout)*time.Second)
if e != nil {
glog.Fatalf("Volume server listener error:%v", e)
2012-08-24 05:46:54 +00:00
}
2019-11-17 03:40:36 +00:00
httpDown := httpdown.HTTP{
KillTimeout: 5 * time.Minute,
StopTimeout: 5 * time.Minute,
CertFile: certFile,
KeyFile: keyFile}
clusterHttpServer := httpDown.Serve(&http.Server{Handler: handler}, listener)
go func() {
if e := clusterHttpServer.Wait(); e != nil {
glog.Fatalf("Volume server fail to serve: %v", e)
}
}()
return clusterHttpServer
}