seaweedfs/weed/command/volume.go

210 lines
7.2 KiB
Go
Raw Normal View History

package command
import (
2019-02-18 20:11:52 +00:00
"github.com/chrislusf/seaweedfs/weed/security"
"github.com/spf13/viper"
2018-10-11 07:08:13 +00:00
"net/http"
"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/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"
2018-10-11 08:16:33 +00:00
"google.golang.org/grpc/reflection"
)
2015-01-13 08:27:51 +00:00
var (
v VolumeServerOptions
)
type VolumeServerOptions struct {
port *int
publicPort *int
2015-01-13 08:27:51 +00:00
folders []string
folderMaxLimits []int
ip *string
publicUrl *string
2015-01-13 08:27:51 +00:00
bindIp *string
masters *string
2015-01-13 08:27:51 +00:00
pulseSeconds *int
idleConnectionTimeout *int
maxCpu *int
dataCenter *string
rack *string
whiteList []string
indexType *string
2015-01-13 08:27:51 +00:00
fixJpgOrientation *bool
readRedirect *bool
2017-06-22 08:33:58 +00:00
cpuProfile *string
memProfile *string
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")
2017-01-12 21:42:53 +00:00
v.ip = cmdVolume.Flag.String("ip", "", "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")
2015-01-13 08:27:51 +00:00
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.maxCpu = cmdVolume.Flag.Int("maxCpu", 0, "maximum number of CPUs. 0 means all available CPUs")
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")
}
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]...")
2013-08-13 16:22:06 +00:00
maxVolumeCounts = cmdVolume.Flag.String("max", "7", "maximum numbers of volumes, count[,count]...")
volumeWhiteListOption = cmdVolume.Flag.String("whiteList", "", "comma separated Ip addresses having write permission. No limit if empty.")
)
func runVolume(cmd *Command, args []string) bool {
2019-02-18 20:11:52 +00:00
weed_server.LoadConfiguration("security", false)
2015-01-13 08:27:51 +00:00
if *v.maxCpu < 1 {
*v.maxCpu = runtime.NumCPU()
2012-11-07 09:51:43 +00:00
}
2015-01-13 08:27:51 +00:00
runtime.GOMAXPROCS(*v.maxCpu)
2017-06-22 08:33:58 +00:00
util.SetupProfiling(*v.cpuProfile, *v.memProfile)
2015-01-13 08:27:51 +00:00
v.startVolumeServer(*volumeFolders, *maxVolumeCounts, *volumeWhiteListOption)
return true
}
func (v VolumeServerOptions) startVolumeServer(volumeFolders, maxVolumeCounts, volumeWhiteListOption string) {
2015-01-13 08:36:44 +00:00
//Set multiple folders and each folder's max volume count limit'
v.folders = strings.Split(volumeFolders, ",")
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))
}
2015-01-13 08:27:51 +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)
}
}
2015-01-13 08:36:44 +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 = "127.0.0.1"
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)
}
isSeperatedPublicPort := *v.publicPort != *v.port
volumeMux := http.NewServeMux()
publicVolumeMux := volumeMux
if isSeperatedPublicPort {
publicVolumeMux = http.NewServeMux()
}
2012-08-24 05:46:54 +00:00
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,
v.folders, v.folderMaxLimits,
volumeNeedleMapKind,
strings.Split(masters, ","), *v.pulseSeconds, *v.dataCenter, *v.rack,
2015-01-13 08:27:51 +00:00
v.whiteList,
*v.fixJpgOrientation, *v.readRedirect,
)
2012-08-24 05:46:54 +00:00
2015-01-13 08:36:44 +00:00
listeningAddress := *v.bindIp + ":" + strconv.Itoa(*v.port)
2018-12-17 01:37:10 +00:00
glog.V(0).Infof("Start Seaweed volume server %s at %s", util.VERSION, listeningAddress)
2015-01-13 08:27:51 +00:00
listener, e := util.NewListener(listeningAddress, time.Duration(*v.idleConnectionTimeout)*time.Second)
2012-08-24 05:46:54 +00:00
if e != nil {
2015-01-14 01:04:41 +00:00
glog.Fatalf("Volume server listener error:%v", e)
}
if isSeperatedPublicPort {
publicListeningAddress := *v.bindIp + ":" + strconv.Itoa(*v.publicPort)
glog.V(0).Infoln("Start Seaweed volume server", util.VERSION, "public at", publicListeningAddress)
publicListener, e := util.NewListener(publicListeningAddress, time.Duration(*v.idleConnectionTimeout)*time.Second)
if e != nil {
glog.Fatalf("Volume server listener error:%v", e)
}
go func() {
if e := http.Serve(publicListener, publicVolumeMux); e != nil {
glog.Fatalf("Volume server fail to serve public: %v", e)
}
}()
}
2017-06-22 08:33:58 +00:00
util.OnInterrupt(func() {
2014-05-13 22:04:04 +00:00
volumeServer.Shutdown()
pprof.StopCPUProfile()
2014-05-13 22:04:04 +00:00
})
2014-05-13 07:04:28 +00:00
2018-10-11 08:16:33 +00:00
// starting grpc server
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)
}
2019-02-18 20:11:52 +00:00
grpcS := util.NewGrpcServer(security.LoadServerTLS(viper.Sub("grpc"), "volume"))
2018-10-11 08:16:33 +00:00
volume_server_pb.RegisterVolumeServerServer(grpcS, volumeServer)
reflection.Register(grpcS)
go grpcS.Serve(grpcL)
2019-02-25 08:43:36 +00:00
if viper.GetString("https.volume.key") != "" {
if e := http.ServeTLS(listener, volumeMux,
viper.GetString("https.volume.cert"), viper.GetString("https.volume.key")); e != nil {
glog.Fatalf("Volume server fail to serve: %v", e)
}
} else {
if e := http.Serve(listener, volumeMux); e != nil {
glog.Fatalf("Volume server fail to serve: %v", e)
}
2012-08-24 05:46:54 +00:00
}
}